Commit 713ad666 authored by clabby's avatar clabby

Add preimage oracle tests

parent 41dff4ad
This diff is collapsed.
......@@ -383,21 +383,21 @@ func (_PreimageOracle *PreimageOracleTransactorSession) LoadKeccak256PreimagePar
// LoadLocalData is a paid mutator transaction binding the contract method 0xe52f0937.
//
// Solidity: function loadLocalData(uint256 _ident, bytes32 _word, uint8 _size) returns()
// Solidity: function loadLocalData(uint256 _ident, bytes32 _word, uint8 _size) returns(bytes32 key_)
func (_PreimageOracle *PreimageOracleTransactor) LoadLocalData(opts *bind.TransactOpts, _ident *big.Int, _word [32]byte, _size uint8) (*types.Transaction, error) {
return _PreimageOracle.contract.Transact(opts, "loadLocalData", _ident, _word, _size)
}
// LoadLocalData is a paid mutator transaction binding the contract method 0xe52f0937.
//
// Solidity: function loadLocalData(uint256 _ident, bytes32 _word, uint8 _size) returns()
// Solidity: function loadLocalData(uint256 _ident, bytes32 _word, uint8 _size) returns(bytes32 key_)
func (_PreimageOracle *PreimageOracleSession) LoadLocalData(_ident *big.Int, _word [32]byte, _size uint8) (*types.Transaction, error) {
return _PreimageOracle.Contract.LoadLocalData(&_PreimageOracle.TransactOpts, _ident, _word, _size)
}
// LoadLocalData is a paid mutator transaction binding the contract method 0xe52f0937.
//
// Solidity: function loadLocalData(uint256 _ident, bytes32 _word, uint8 _size) returns()
// Solidity: function loadLocalData(uint256 _ident, bytes32 _word, uint8 _size) returns(bytes32 key_)
func (_PreimageOracle *PreimageOracleTransactorSession) LoadLocalData(_ident *big.Int, _word [32]byte, _size uint8) (*types.Transaction, error) {
return _PreimageOracle.Contract.LoadLocalData(&_PreimageOracle.TransactOpts, _ident, _word, _size)
}
......@@ -62,22 +62,22 @@ contract PreimageOracle {
/// specific data.
///
/// There are 5 local data identifiers:
/// ┌────────────┬─────────────────┐
/// │ Identifier │ Data │
/// ├────────────┼─────────────────┤
/// │ 1 │ L1 Head Hash
/// │ 2 │ Output Root
/// │ 3 │ Root Claim
/// │ 4 │ L2 Block Number │
/// │ 5 │ Chain ID │
/// └────────────┴─────────────────┘
/// ┌────────────┬────────────────────────
/// │ Identifier │ Data
/// ├────────────┼────────────────────────
/// │ 1 │ L1 Head Hash (bytes32)
/// │ 2 │ Output Root (bytes32)
/// │ 3 │ Root Claim (bytes32)
/// │ 4 │ L2 Block Number (u64)
/// │ 5 │ Chain ID (u64)
/// └────────────┴────────────────────────
function loadLocalData(
uint256 _ident,
bytes32 _word,
uint8 _size
) external {
) external returns (bytes32 key_) {
// Compute the localized key from the given local identifier.
bytes32 key = PreimageKeyLib.localizeIdent(_ident);
key_ = PreimageKeyLib.localizeIdent(_ident);
// Load both parts of the local data word into storage for future
// reads.
......@@ -89,18 +89,18 @@ contract PreimageOracle {
}
// Store the first part with offset 0.
preimagePartOk[key][0] = true;
preimageParts[key][0] = part1;
preimagePartOk[key_][0] = true;
preimageParts[key_][0] = part1;
// If the size is greater than 24, we need to store a second part as well.
if (_size > 24) {
bytes32 part2 = _word << 192;
preimagePartOk[key][32] = true;
preimageParts[key][32] = part2;
preimagePartOk[key_][32] = true;
preimageParts[key_][32] = part2;
}
// Assign the length of the preimage at the localized key.
preimageLengths[key] = _size;
preimageLengths[key_] = _size;
}
/// @notice Prepares a pre-image to be read by keccak256 key, starting at
......
......@@ -43,5 +43,5 @@ interface IPreimageOracle {
uint256 _ident,
bytes32 _word,
uint8 _size
) external;
) external returns (bytes32 key_);
}
......@@ -24,6 +24,64 @@ contract PreimageOracle_Test is Test {
assertEq(key, known);
}
/// @notice Tests that context-specific data [0, 24] bytes in length can be loaded correctly.
function test_loadLocalData_onePart_succeeds() public {
uint256 ident = 1;
bytes32 word = bytes32(uint256(0xdeadbeef) << 224);
uint8 size = 4;
// Load the local data into the preimage oracle under the test contract's context.
bytes32 contextKey = oracle.loadLocalData(ident, word, size);
// Validate that the pre-image part is set
bool ok = oracle.preimagePartOk(contextKey, 0);
assertTrue(ok);
// Validate the local data part
bytes32 expectedPart = 0x0000000000000004deadbeef0000000000000000000000000000000000000000;
assertEq(oracle.preimageParts(contextKey, 0), expectedPart);
// Validate the local data length
uint256 length = oracle.preimageLengths(contextKey);
assertEq(length, size);
}
/// @notice Tests that context-specific data [0, 32] bytes in length can be loaded correctly.
function testFuzz_loadLocalData_varyingLength_succeeds(
uint256 ident,
bytes32 word,
uint256 size
) public {
// Bound the size to [0, 32]
size = bound(size, 0, 32);
// Load the local data into the preimage oracle under the test contract's context.
bytes32 contextKey = oracle.loadLocalData(ident, word, uint8(size));
// Validate that the first local data part is set
bool ok = oracle.preimagePartOk(contextKey, 0);
assertTrue(ok);
// Validate the first local data part
bytes32 expectedPart1 = bytes32(size << 192 | uint256(word) >> 64);
assertEq(oracle.preimageParts(contextKey, 0), expectedPart1);
// If the size is > 24, validate the second part. Otherwise, ensure
// that the second part is not set.
ok = oracle.preimagePartOk(contextKey, 32);
if (size > 24) {
assertTrue(ok);
// Validate the second local data part
bytes32 expectedPart2 = word << 192;
assertEq(oracle.preimageParts(contextKey, 32), expectedPart2);
} else {
assertFalse(ok);
}
// Validate the local data length
uint256 length = oracle.preimageLengths(contextKey);
assertEq(length, size);
}
/// @notice Tests that a pre-image is correctly set.
function test_loadKeccak256PreimagePart_succeeds() public {
// Set the pre-image
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment