Skip to content

Commit

Permalink
chore: small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
merklefruit committed Sep 11, 2024
1 parent 3ee83c3 commit 013d9e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
21 changes: 11 additions & 10 deletions bolt-contracts/src/contracts/BoltChallenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ contract BoltChallenger {
// unimplemented!();
}

/// @notice Prove the block header data of a recent block.
/// @dev Only works with block headers that are less than 256 blocks old.
/// @param header The RLP-encoded block header to prove.
function proveRecentBlockHeaderData(
bytes calldata header
)
Expand All @@ -39,8 +41,8 @@ contract BoltChallenger {
uint256 baseFee
)
{
// RLP decode the header
// https://github.com/ethereum/go-ethereum/blob/master/core/types/block.go
// RLP decode the block header and extract the necessary fields
// ref: https://github.com/ethereum/go-ethereum/blob/master/core/types/block.go
RLPReader.RLPItem[] memory headerFields = header.toRLPItem().readList();
transactionsRoot = headerFields[4].readBytes32();
blockNumber = headerFields[8].readUint256();
Expand All @@ -49,37 +51,36 @@ contract BoltChallenger {
timestamp = headerFields[11].readUint256();
baseFee = headerFields[15].readUint256();

if (blockhash(blockNumber) == bytes32(0) || blockNumber < block.number - 256) {
bytes32 trustedBlockHash = blockhash(blockNumber);
if (trustedBlockHash == bytes32(0) || blockNumber < block.number - 256) {
revert BlockIsTooOld();
}

// verify that the block hash matches the one in the EVM
if (keccak256(header) != blockhash(blockNumber)) {
if (keccak256(header) != trustedBlockHash) {
revert InvalidBlockHash();
}
}

/// @notice Prove the account data of an account at a given state root.
/// @dev This function assumes that the provided state root and account proof match.
/// @param account The account address to prove.
/// @param stateRoot The TRUSTED state root to prove against. Checking how the state root is obtained
/// is the responsibility of the caller.
/// @param trustedStateRoot The state root to prove against.
/// @param accountProof The MPT account proof to prove the account data.
/// @return nonce The nonce of the account at the given state root height.
/// @return balance The balance of the account at the given state root height.
function proveAccountData(
address account,
bytes32 stateRoot,
bytes32 trustedStateRoot,
bytes calldata accountProof
) public pure returns (uint256 nonce, uint256 balance) {
(bool exists, bytes memory accountRLP) =
SecureMerkleTrie.get(abi.encodePacked(account), accountProof, stateRoot);
SecureMerkleTrie.get(abi.encodePacked(account), accountProof, trustedStateRoot);

if (!exists) {
revert AccountDoesNotExist();
}

// decode the account RLP into nonce and balance
// RLP decode the account and extract the nonce and balance
RLPReader.RLPItem[] memory accountFields = accountRLP.toRLPItem().readList();
nonce = accountFields[0].readUint256();
balance = accountFields[1].readUint256();
Expand Down
4 changes: 1 addition & 3 deletions bolt-contracts/test/BoltChallenger.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ contract BoltChallengerTest is Test {
// for recent blocks, we can simply use the blockhash function in the EVM.
bytes32 trustedBlockHash = 0x531b257cc7ecda14d12007aae5f45924789ea70ab20e3e28d67025028fed61a9;

// Read the RLP-encoded block header from a file
// Read the RLP-encoded block header from a file (obtained via `debug_getRawHeader` RPC call)
bytes memory headerRLP = vm.parseBytes(vm.readFile("./test/testdata/header_rlp.hex"));

// In prod, this check would be done using the blockhash function in the EVM,
// using the RLP-decoded block number as the input. This is just a sanity check here.
assertEq(keccak256(headerRLP), trustedBlockHash);

// RLP decode the header
Expand Down

0 comments on commit 013d9e9

Please sign in to comment.