Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
feat: implemented operator and re-enabled forge tests (#10)
Browse files Browse the repository at this point in the history
* imp: some prep

* imp: save progress

* feat: implemented contract

* feat: all compiling

* imp(contracts): contract improvements

* feat: fixtures can be generated

* feat: basic tests passing

* ci: re-enabled forge tests

* ci: fixed
  • Loading branch information
srdtrk authored Jun 23, 2024
1 parent 9e283f9 commit bd048e7
Show file tree
Hide file tree
Showing 19 changed files with 453 additions and 149 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/foundry-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ name: Foundry Test

on:
workflow_dispatch:
# Disabled until Foundry tests are fixed
# push:
# branches: [ main ]
# pull_request:
push:
branches: [ main ]
pull_request:

env:
FOUNDRY_PROFILE: ci
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --exclude sp1-ics07-tendermint-program-update-client --locked --all-features
args: --workspace --exclude sp1-ics07-tendermint-update-client --locked --all-features
env:
RUST_BACKTRACE: 1
- name: Build
uses: actions-rs/cargo@v1
with:
command: build
args: --workspace --exclude sp1-ics07-tendermint-program-update-client --locked --all-features
args: --workspace --exclude sp1-ics07-tendermint-update-client --locked --all-features
32 changes: 21 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git", tag = "v1.0.7-test
sp1-helper = { git = "https://github.com/succinctlabs/sp1.git", tag = "v1.0.7-testnet" }

sp1-ics07-tendermint-shared = { path = "./packages/shared/" }
sp1-ics07-tendermint-update-client = { path = "./programs/update-client/" }

tendermint-light-client-verifier = { version = "0.36.0", default-features = false }
ibc-client-tendermint = { version = "0.53.0", default-features = false, features = ["serde"] }
Expand All @@ -32,20 +33,23 @@ ibc-client-tendermint-types = { version = "0.53.0", default-features = false }
serde = { version = "1.0", default-features = false }
sha2 = { version = "0.10.8", default-features = false }
alloy-sol-types = "0.7.2"
serde_cbor = "0.11.2"
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

# Used by the operator
sp1-sdk = { git = "https://github.com/succinctlabs/sp1.git", tag = "v1.0.7-testnet" }
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
tendermint = { version = "0.36.0", default-features = false }
ibc-core-client-types = { version = "0.53.0", default-features = false }
ibc-core-commitment-types = { version = "0.53.0", default-features = false }
time = { version = "0.3", default-features = false, features = ["macros", "parsing"] }
# tendermint-light-client-verifier = { version = "0.35.0", default-features = false, features = [
# "rust-crypto",
# ] }
alloy-primitives = "0.7.2"
bincode = "1.3.3"
itertools = "0.12.1"
serde_cbor = "0.11.2"
dotenv = "0.15.0"
subtle-encoding = "0.5.1"
ethers = "2.0.14"
Expand Down
9 changes: 9 additions & 0 deletions contracts/fixtures/mock_fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"trustedClientState": "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000002034c200000000000000000000000000000000000000000000000000044c1ff252000000000000000000000000000000000000000000000000000000044c1ff2520000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076d6f6368612d3400000000000000000000000000000000000000000000000000",
"trustedConsensusState": "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000017db9605c70f5a75000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020994a2058af458e1a97f19bfa85c99ba5db6534473e8040e671a45431f78651bf0000000000000000000000000000000000000000000000000000000000000020b73058223e788b1b021cd25d9d8b80f1e21f742014d270fc850bacda1e63c419",
"targetConsensusState": "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000017db962167923e1a000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002092a23d699a0d59f2e1b034c129b0051fb711a3d0d1f2e895fce82e933b8e36fd0000000000000000000000000000000000000000000000000000000000000020b73058223e788b1b021cd25d9d8b80f1e21f742014d270fc850bacda1e63c419",
"targetHeight": 2110668,
"vkey": "0x0028d036f6404b30d0c8b3fdb4e64852b04f78d54115702b299f18afb8b00ef4",
"publicValues": "0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000002034c2000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000002034cc00000000000000000000000000000000000000000000000017db9605c70f5a75000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020994a2058af458e1a97f19bfa85c99ba5db6534473e8040e671a45431f78651bf0000000000000000000000000000000000000000000000000000000000000020b73058223e788b1b021cd25d9d8b80f1e21f742014d270fc850bacda1e63c41900000000000000000000000000000000000000000000000017db962167923e1a000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002092a23d699a0d59f2e1b034c129b0051fb711a3d0d1f2e895fce82e933b8e36fd0000000000000000000000000000000000000000000000000000000000000020b73058223e788b1b021cd25d9d8b80f1e21f742014d270fc850bacda1e63c41900000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000044c1ff252000000000000000000000000000000000000000000000000000017db9e47e5d87cc800000000000000000000000000000000000000000000000000000000000000076d6f6368612d3400000000000000000000000000000000000000000000000000",
"proof": "0x00000000"
}
68 changes: 58 additions & 10 deletions contracts/src/SP1ICS07Tendermint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {ISP1Verifier} from "@sp1-contracts/ISP1Verifier.sol";
/// @notice This contract implements an ICS07 IBC tendermint light client.
contract SP1ICS07Tendermint {
/// @notice The verification key for the program.
bytes32 public ics07ProgramVkey;
bytes32 public ics07UpdateClientProgramVkey;
// @notice The SP1 verifier contract.
ISP1Verifier public verifier;

Expand All @@ -18,6 +18,9 @@ contract SP1ICS07Tendermint {
// @notice The mapping from height to consensus state
mapping(uint64 => ICS07Tendermint.ConsensusState) public consensusStates;

/// Allowed clock drift in nanoseconds
uint64 public constant ALLOWED_SP1_CLOCK_DRIFT = 6_000_000_000_000; // 6000 seconds

// @notice The constructor sets the program verification key.
// @param _ics07ProgramVkey The verification key for the program.
// @param _verifier The address of the SP1 verifier contract.
Expand All @@ -27,30 +30,75 @@ contract SP1ICS07Tendermint {
bytes memory _clientState,
bytes memory _consensusState
) {
ics07ProgramVkey = _ics07ProgramVkey;
ics07UpdateClientProgramVkey = _ics07ProgramVkey;
verifier = ISP1Verifier(_verifier);

clientState = abi.decode(_clientState, (ICS07Tendermint.ClientState));
ICS07Tendermint.ConsensusState memory consensusState = abi.decode(
_consensusState,
(ICS07Tendermint.ConsensusState)
);
consensusStates[consensusState.timestamp] = consensusState;
consensusStates[
clientState.latest_height.revision_height
] = consensusState;
}

/// @notice The entrypoint for verifying the proof.
/// @param proof The encoded proof.
/// @param publicValues The encoded public values.
// TODO: modify the return tyoe and the public values to match the actual program.
function verifyIcs07Proof(
function verifyIcs07UpdateClientProof(
bytes memory proof,
bytes memory publicValues
) public view returns (uint32, uint32, uint32) {
verifier.verifyProof(ics07ProgramVkey, publicValues, proof);
(uint32 n, uint32 a, uint32 b) = abi.decode(
) public {
SP1ICS07TendermintOutput memory output = abi.decode(
publicValues,
(uint32, uint32, uint32)
(SP1ICS07TendermintOutput)
);

require(
clientState.is_frozen == false,
"SP1ICS07Tendermint: client is frozen"
);
return (n, a, b);
require(
block.timestamp * 1e9 <= output.env.now + ALLOWED_SP1_CLOCK_DRIFT,
"SP1ICS07Tendermint: invalid timestamp"
);

// TODO: verify that the client state and the saved consensus state match the public values.
// More checks need to be made here get the trusted consensus clientState and etc

verifier.verifyProof(ics07UpdateClientProgramVkey, publicValues, proof);

// adding the new consensus state to the mapping
clientState.latest_height = output.new_height;
consensusStates[output.new_consensus_state.timestamp] = output
.new_consensus_state;
}

/// The public value output for the sp1 program.
struct SP1ICS07TendermintOutput {
/// The trusted consensus state.
ICS07Tendermint.ConsensusState trusted_consensus_state;
/// The new consensus state with the verified header.
ICS07Tendermint.ConsensusState new_consensus_state;
/// The validation environment.
Env env;
/// trusted height
ICS07Tendermint.Height trusted_height;
/// new height
ICS07Tendermint.Height new_height;
}

/// The environment output for the sp1 program.
struct Env {
/// The chain ID of the chain that the client is tracking.
string chain_id;
/// Fraction of validator overlap needed to update header
ICS07Tendermint.TrustThreshold trust_threshold;
/// Duration of the period since the `LatestTimestamp` during which the
/// submitted headers are valid for upgrade
uint64 trusting_period;
/// Timestamp in nanoseconds
uint64 now;
}
}
8 changes: 0 additions & 8 deletions contracts/src/fixtures/fixture.json

This file was deleted.

Loading

0 comments on commit bd048e7

Please sign in to comment.