Skip to content

Commit

Permalink
Merge pull request #21 from metacraft-labs/dendreth_update
Browse files Browse the repository at this point in the history
Dendreth adapter to update the oracle in the same transaction
  • Loading branch information
auryn-macmillan authored Jun 27, 2023
2 parents 281c476 + bf56704 commit 288a10e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/evm/contracts/adapters/DendrETH/DendrETHAdapter.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.17;

import { ILightClient } from "./interfaces/IDendrETH.sol";
import { ILightClient, LightClientUpdate } from "./interfaces/IDendrETH.sol";
import { SSZ } from "../Telepathy/libraries/SimpleSerialize.sol";
import { BlockHashOracleAdapter } from "../BlockHashOracleAdapter.sol";

contract DendrETHAdapter is BlockHashOracleAdapter {
error InvalidUpdate();
error BlockHeaderNotAvailable(uint256 slot);
error InvalidBlockNumberProof();
error InvalidBlockHashProof();
Expand Down Expand Up @@ -59,4 +60,36 @@ contract DendrETHAdapter is BlockHashOracleAdapter {

_storeHash(uint256(_chainId), _blockNumber, _blockHash);
}

/// @notice Updates DendrETH Light client and stores the given block
// for the update
function storeBlockHeader(
uint32 _chainId,
uint64 _slot,
uint256 _blockNumber,
bytes32[] calldata _blockNumberProof,
bytes32 _blockHash,
bytes32[] calldata _blockHashProof,
LightClientUpdate calldata update
) external {
ILightClient lightClient = ILightClient(dendrETHAddress);

lightClient.light_client_update(update);

if (lightClient.optimisticHeaderSlot() != _slot) {
revert InvalidUpdate();
}

bytes32 blockHeaderRoot = lightClient.optimisticHeaderRoot();

if (!SSZ.verifyBlockNumber(_blockNumber, _blockNumberProof, blockHeaderRoot)) {
revert InvalidBlockNumberProof();
}

if (!SSZ.verifyBlockHash(_blockHash, _blockHashProof, blockHeaderRoot)) {
revert InvalidBlockHashProof();
}

_storeHash(uint256(_chainId), _blockNumber, _blockHash);
}
}
16 changes: 16 additions & 0 deletions packages/evm/contracts/adapters/DendrETH/interfaces/IDendrETH.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity 0.8.17;

struct LightClientUpdate {
bytes32 attestedHeaderRoot;
uint256 attestedHeaderSlot;
bytes32 finalizedHeaderRoot;
bytes32 finalizedExecutionStateRoot;
uint256[2] a;
uint256[2][2] b;
uint256[2] c;
}

interface ILightClient {
function currentIndex() external view returns (uint256);

function optimisticHeaders(uint256 index) external view returns (bytes32);

function optimisticHeaderRoot() external view returns (bytes32);

function optimisticSlots(uint256 index) external view returns (uint256);

function optimisticHeaderSlot() external view returns (uint256);

function light_client_update(LightClientUpdate calldata update) external;
}

0 comments on commit 288a10e

Please sign in to comment.