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

Commit

Permalink
feat: basic tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
srdtrk committed Jun 23, 2024
1 parent 040e75d commit e034473
Showing 1 changed file with 103 additions and 34 deletions.
137 changes: 103 additions & 34 deletions contracts/test/SP1ICS07Tendermint.t.sol
Original file line number Diff line number Diff line change
@@ -1,60 +1,129 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import "forge-std/console.sol";
import {Test} from "forge-std/Test.sol";
import {stdJson} from "forge-std/StdJson.sol";
import {ICS07Tendermint} from "ibc-lite-shared/ics07-tendermint/ICS07Tendermint.sol";
import {SP1ICS07Tendermint} from "../src/SP1ICS07Tendermint.sol";
import {SP1Verifier} from "@sp1-contracts/SP1Verifier.sol";
import {SP1MockVerifier} from "@sp1-contracts/SP1MockVerifier.sol";

struct SP1ProofFixtureJson {
uint32 a;
uint32 b;
uint32 n;
bytes proof;
bytes publicValues;
struct SP1TendermintFixtureJson {
bytes trustedClientState;
bytes trustedConsensusState;
bytes targetConsensusState;
uint64 targetHeight;
bytes32 vkey;
bytes publicValues;
bytes proof;
}

contract SP1ICS07TendermintTest is Test {
contract SP1TendermintTest is Test {
using stdJson for string;

SP1ICS07Tendermint public ics07Tendermint;
// TODO: Test non-mock ics07Tendermint, once we have a way to generate the fixture.
// SP1ICS07Tendermint public ics07Tendermint;
SP1ICS07Tendermint public mockIcs07Tendermint;

function loadFixture() public view returns (SP1ProofFixtureJson memory) {
string memory root = vm.projectRoot();
string memory path = string.concat(root, "/src/fixtures/fixture.json");
string memory json = vm.readFile(path);
bytes memory jsonBytes = json.parseRaw(".");
return abi.decode(jsonBytes, (SP1ProofFixtureJson));
}

// TODO: fix after some rust code
function setUp() public {
SP1ProofFixtureJson memory fixture = loadFixture();
/*
SP1TendermintFixtureJson memory fixture = loadFixture("fixture.json");
SP1Verifier verifier = new SP1Verifier();
ics07Tendermint = new SP1ICS07Tendermint(
fixture.vkey,
address(verifier)
address(verifier),
fixture.trustedClientState,
fixture.trustedConsensusState
);
*/

SP1TendermintFixtureJson memory mockFixture = loadFixture(
"mock_fixture.json"
);
SP1MockVerifier mockVerifier = new SP1MockVerifier();
mockIcs07Tendermint = new SP1ICS07Tendermint(
mockFixture.vkey,
address(mockVerifier),
mockFixture.trustedClientState,
mockFixture.trustedConsensusState
);
}

function loadFixture(
string memory fileName
) public view returns (SP1TendermintFixtureJson memory) {
string memory root = vm.projectRoot();
string memory path = string.concat(root, "/fixtures/", fileName);
string memory json = vm.readFile(path);
bytes memory trustedClientState = json.readBytes(".trustedClientState");
bytes memory trustedConsensusState = json.readBytes(
".trustedConsensusState"
);
bytes memory targetConsensusState = json.readBytes(
".targetConsensusState"
);
uint64 targetHeight = uint64(json.readUint(".targetHeight"));
bytes32 vkey = json.readBytes32(".vkey");
bytes memory publicValues = json.readBytes(".publicValues");
bytes memory proof = json.readBytes(".proof");

SP1TendermintFixtureJson memory fixture = SP1TendermintFixtureJson({
trustedClientState: trustedClientState,
trustedConsensusState: trustedConsensusState,
targetConsensusState: targetConsensusState,
targetHeight: targetHeight,
vkey: vkey,
publicValues: publicValues,
proof: proof
});

return fixture;
}

function test_ValidSP1ICS07TendermintProof() public view {
SP1ProofFixtureJson memory fixture = loadFixture();
(uint32 n, uint32 a, uint32 b) = ics07Tendermint.verifyIcs07Proof(
fixture.proof,
// Confirm that submitting an empty proof passes the mock verifier.
function test_ValidMockTendermint() public {
SP1TendermintFixtureJson memory fixture = loadFixture(
"mock_fixture.json"
);

(
string memory chain_id,
ICS07Tendermint.TrustThreshold memory trust_level,
ICS07Tendermint.Height memory latest_height,
uint64 trusting_period,
uint64 unbonding_period,
bool is_frozen
) = mockIcs07Tendermint.clientState();

assert(keccak256(bytes(chain_id)) == keccak256(bytes("mocha-4")));
assert(trust_level.numerator == 1);
assert(trust_level.denominator == 3);
assert(latest_height.revision_number == 4);
assert(latest_height.revision_height == 2110658);
assert(trusting_period == 1_209_600_000_000_000);
assert(unbonding_period == 1_209_600_000_000_000);
assert(is_frozen == false);

mockIcs07Tendermint.verifyIcs07UpdateClientProof(
bytes(""),
fixture.publicValues
);
assert(n == fixture.n);
assert(a == fixture.a);
assert(b == fixture.b);
}

function testFail_InvalidSP1ICS07TendermintProof() public view {
SP1ProofFixtureJson memory fixture = loadFixture();
// Confirm that submitting a non-empty proof with the mock verifier fails. This typically
// indicates that the user has passed in a real proof to the mock verifier.
function testFail_Invalid_MockTendermint() public {
SP1TendermintFixtureJson memory fixture = loadFixture(
"mock_fixture.json"
);

// Create a fake proof.
bytes memory fakeProof = new bytes(fixture.proof.length);
mockIcs07Tendermint.verifyIcs07UpdateClientProof(
bytes("aa"),
fixture.publicValues
);

ics07Tendermint.verifyIcs07Proof(fakeProof, fixture.publicValues);
// assert(mockIcs07Tendermint.latestHeader() == fixture.targetHeaderHash);
// assert(mockIcs07Tendermint.latestHeight() == fixture.targetHeight);
}
}

0 comments on commit e034473

Please sign in to comment.