-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from compound-finance/jflatow/type-storage
Remove dynamic type-checking from the Oracle contracts
- Loading branch information
Showing
11 changed files
with
263 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
pragma solidity ^0.5.9; | ||
pragma experimental ABIEncoderV2; | ||
|
||
/** | ||
* @title The Open Oracle Data Base Contract | ||
* @author Compound Labs, Inc. | ||
*/ | ||
contract OpenOracleData { | ||
/** | ||
* @notice The event emitted when a source writes to its storage | ||
*/ | ||
//event Write(address indexed source, <Key> indexed key, uint timestamp, <Value> value); | ||
|
||
/** | ||
* @notice Write a bunch of signed datum to the authenticated storage mapping | ||
* @param message The payload containing the timestamp, and (key, value) pairs | ||
* @param signature The cryptographic signature of the message payload, authorizing the source to write | ||
*/ | ||
//function put(bytes calldata message, bytes calldata signature) external; | ||
|
||
/** | ||
* @notice Read a single key with a pre-defined type signature from an authenticated source | ||
* @param source The verifiable author of the data | ||
* @param key The selector for the value to return | ||
* @return The claimed Unix timestamp for the data and the encoded value (defaults to (0, 0x)) | ||
*/ | ||
//function get(address source, <Key> key) external view returns (uint, <Value>); | ||
|
||
/** | ||
* @notice Recovers the source address which signed a message | ||
* @dev Comparing to a claimed address would add nothing, | ||
* as the caller could simply perform the recover and claim that address. | ||
* @param message The data that was presumably signed | ||
* @param signature The fingerprint of the data + private key | ||
* @return The source address which signed the message, presumably | ||
*/ | ||
function source(bytes memory message, bytes memory signature) public pure returns (address) { | ||
(bytes32 r, bytes32 s, uint8 v) = abi.decode(signature, (bytes32, bytes32, uint8)); | ||
bytes32 hash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", keccak256(message))); | ||
return ecrecover(hash, v, r, s); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
pragma solidity ^0.5.9; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "./OpenOracleData.sol"; | ||
|
||
/** | ||
* @title The Open Oracle Price Data Contract | ||
* @author Compound Labs, Inc. | ||
*/ | ||
contract OpenOraclePriceData is OpenOracleData { | ||
/** | ||
* @notice The event emitted when a source writes to its storage | ||
*/ | ||
event Write(address indexed source, string indexed key, uint timestamp, uint value); | ||
|
||
/** | ||
* @notice The fundamental unit of storage for a reporter source | ||
*/ | ||
struct Datum { | ||
uint timestamp; | ||
uint value; | ||
} | ||
|
||
/** | ||
* @notice The most recent authenticated data from all sources | ||
* @dev This is private because dynamic mapping keys preclude auto-generated getters. | ||
*/ | ||
mapping(address => mapping(string => Datum)) private data; | ||
|
||
/** | ||
* @notice Write a bunch of signed datum to the authenticated storage mapping | ||
* @param message The payload containing the timestamp, and (key, value) pairs | ||
* @param signature The cryptographic signature of the message payload, authorizing the source to write | ||
*/ | ||
function put(bytes calldata message, bytes calldata signature) external { | ||
// Recover the source address | ||
address source = source(message, signature); | ||
|
||
// Decode all the data tuples | ||
(uint timestamp, bytes[] memory pairs) = abi.decode(message, (uint, bytes[])); | ||
for (uint j = 0; j < pairs.length; j++) { | ||
(string memory key, uint value) = abi.decode(pairs[j], (string, uint)); | ||
|
||
// Only update if newer than stored, according to source | ||
Datum storage prior = data[source][key]; | ||
if (prior.timestamp >= timestamp) { | ||
continue; | ||
} | ||
|
||
// Update storage | ||
data[source][key] = Datum(timestamp, value); | ||
emit Write(source, key, timestamp, value); | ||
} | ||
} | ||
|
||
/** | ||
* @notice Read a single key from an authenticated source | ||
* @param source The verifiable author of the data | ||
* @param key The selector for the value to return | ||
* @return The claimed Unix timestamp for the data and the encoded value (defaults to (0, 0)) | ||
*/ | ||
function get(address source, string calldata key) external view returns (uint, uint) { | ||
Datum storage datum = data[source][key]; | ||
return (datum.timestamp, datum.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.