Skip to content

Commit

Permalink
move rust fixtures into a single file per test
Browse files Browse the repository at this point in the history
  • Loading branch information
gjermundgaraba committed Dec 10, 2024
1 parent a4ac4aa commit 68a50d2
Show file tree
Hide file tree
Showing 24 changed files with 852 additions and 617 deletions.
21 changes: 13 additions & 8 deletions e2e/interchaintestv8/e2esuite/light_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ func (s *TestSuite) UpdateEthClient(ctx context.Context, ibcContractAddress stri
headers = []ethereumligthclient.Header{}
}

wasmClientState, _ := s.GetUnionClientState(ctx, simd, s.EthereumLightClientID)
wasmClientState, unionClientState := s.GetUnionClientState(ctx, simd, s.EthereumLightClientID)
_, unionConsensusState = s.GetUnionConsensusState(ctx, simd, s.EthereumLightClientID, wasmClientState.LatestHeight)

for i, header := range headers {
var updatedHeaders []ethereumligthclient.Header
for _, header := range headers {
logHeader("Updating eth light client", header)
headerBz := simd.Config().EncodingConfig.Codec.MustMarshal(&header)
wasmHeader := ibcwasmtypes.ClientMessage{
Expand All @@ -202,8 +203,7 @@ func (s *TestSuite) UpdateEthClient(ctx context.Context, ibcContractAddress stri
s.LastEtheruemLightClientUpdate = header.ConsensusUpdate.AttestedHeader.Beacon.Slot
fmt.Println("Updated eth light client to block number", s.LastEtheruemLightClientUpdate)

err = rustFixtureGenerator.GenerateRustFixture(fmt.Sprintf("update_header_%d", i), header)
s.Require().NoError(err)
updatedHeaders = append(updatedHeaders, header)

time.Sleep(10 * time.Second)

Expand All @@ -212,6 +212,11 @@ func (s *TestSuite) UpdateEthClient(ctx context.Context, ibcContractAddress stri
break
}
}
rustFixtureGenerator.AddFixtureStep("updated_light_client", types.UpdateClientFixture{
ClientState: unionClientState,
ConsensusState: unionConsensusState,
Updates: updatedHeaders,
})

s.Require().Greater(s.LastEtheruemLightClientUpdate, uint64(minimumUpdateTo))
}
Expand Down Expand Up @@ -317,10 +322,10 @@ func (s *TestSuite) createUnionLightClient(ctx context.Context, simdRelayerUser
s.Require().NoError(err)
s.Require().Equal("08-wasm-0", s.EthereumLightClientID)

err = rustFixtureGenerator.GenerateRustFixture("initial_client_state", ethClientState)
s.Require().NoError(err)
err = rustFixtureGenerator.GenerateRustFixture("initial_consensus_state", ethConsensusState)
s.Require().NoError(err)
rustFixtureGenerator.AddFixtureStep("initial_state", types.InitialStateFixture{
ClientState: ethClientState,
ConsensusState: ethConsensusState,
})
}

func (s *TestSuite) createDummyLightClient(ctx context.Context, simdRelayerUser ibc.Wallet) {
Expand Down
6 changes: 3 additions & 3 deletions e2e/interchaintestv8/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.23.2

require (
cosmossdk.io/api v0.7.6
cosmossdk.io/collections v0.4.0
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.3.0
cosmossdk.io/x/tx v0.13.5
Expand All @@ -19,7 +20,7 @@ require (
github.com/cosmos/solidity-ibc-eureka/abigen v0.0.0
github.com/docker/docker v24.0.9+incompatible
github.com/ethereum/go-ethereum v1.14.12
github.com/kurtosis-tech/kurtosis/api/golang v1.4.1
github.com/kurtosis-tech/kurtosis/api/golang v1.4.2
github.com/rs/zerolog v1.33.0
github.com/strangelove-ventures/interchaintest/v8 v8.3.0
github.com/stretchr/testify v1.10.0
Expand All @@ -35,7 +36,6 @@ require (
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/iam v1.1.9 // indirect
cloud.google.com/go/storage v1.41.0 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core v0.11.1 // indirect
cosmossdk.io/depinject v1.0.0 // indirect
cosmossdk.io/log v1.4.1 // indirect
Expand Down Expand Up @@ -307,8 +307,8 @@ require (
replace github.com/cosmos/solidity-ibc-eureka/abigen => ../../abigen

replace (
github.com/cosmos/ibc-go/v9 => github.com/cosmos/ibc-go/v9 v9.0.0-20241123151201-3d84b47307b9
github.com/cosmos/ibc-go/modules/light-clients/08-wasm => github.com/cosmos/ibc-go/modules/light-clients/08-wasm v0.0.0-20241123151201-3d84b47307b9
github.com/cosmos/ibc-go/v9 => github.com/cosmos/ibc-go/v9 v9.0.0-20241123151201-3d84b47307b9
)

replace (
Expand Down
4 changes: 2 additions & 2 deletions e2e/interchaintestv8/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kurtosis-tech/kurtosis-portal/api/golang v0.0.0-20230818182330-1a86869414d2 h1:izciXrFyFR+ihJ7nLTOkoIX5GzBPIp8gVKlw94gIc98=
github.com/kurtosis-tech/kurtosis-portal/api/golang v0.0.0-20230818182330-1a86869414d2/go.mod h1:bWSMQK3WHVTGHX9CjxPAb/LtzcmfOxID2wdzakSWQxo=
github.com/kurtosis-tech/kurtosis/api/golang v1.4.1 h1:V/T5k7t1iKgFof1cGhyLh396YKdTehUqO97AsTPDy+k=
github.com/kurtosis-tech/kurtosis/api/golang v1.4.1/go.mod h1:9T22P7Vv3j5g6sbm78DxHQ4s9C4Cj3s9JjFQ7DFyYpM=
github.com/kurtosis-tech/kurtosis/api/golang v1.4.2 h1:x9jpXBGuLTWuILVUZWZtgDYY9amhyhzRVHxDFlYEJB4=
github.com/kurtosis-tech/kurtosis/api/golang v1.4.2/go.mod h1:9T22P7Vv3j5g6sbm78DxHQ4s9C4Cj3s9JjFQ7DFyYpM=
github.com/kurtosis-tech/kurtosis/contexts-config-store v0.0.0-20230818184218-f4e3e773463b h1:hMoIM99QKcYQqsnK4AF7Lovi9ZD9ac6lZLZ5D/jx2x8=
github.com/kurtosis-tech/kurtosis/contexts-config-store v0.0.0-20230818184218-f4e3e773463b/go.mod h1:4pFdrRwDz5R+Fov2ZuTaPhAVgjA2jhGh1Izf832sX7A=
github.com/kurtosis-tech/kurtosis/grpc-file-transfer/golang v0.0.0-20230803130419-099ee7a4e3dc h1:7IlEpSehmWcNXOFpNP24Cu5HQI3af7GCBQw//m+LnvQ=
Expand Down
10 changes: 6 additions & 4 deletions e2e/interchaintestv8/ibc_eureka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (s *IbcEurekaTestSuite) SetupSuite(ctx context.Context, proofType operator.
eth, simd := s.ChainA, s.ChainB

var prover string
shouldGenerateRustFixtures := false
s.Require().True(s.Run("Set up environment", func() {
err := os.Chdir("../..")
s.Require().NoError(err)
Expand Down Expand Up @@ -121,10 +122,12 @@ func (s *IbcEurekaTestSuite) SetupSuite(ctx context.Context, proofType operator.
s.generateSolidityFixtures = true
}

shouldGenerateRustFixtures := os.Getenv(testvalues.EnvKeyGenerateRustFixtures) == testvalues.EnvValueGenerateFixtures_True
s.rustFixtureGenerator = types.NewRustFixtureGenerator(s.GetTopLevelTestName(), shouldGenerateRustFixtures)
shouldGenerateRustFixtures = os.Getenv(testvalues.EnvKeyGenerateRustFixtures) == testvalues.EnvValueGenerateFixtures_True
}))

// Needs to be added here so the cleanup is called after the test suite is done
s.rustFixtureGenerator = types.NewRustFixtureGenerator(&s.Suite, shouldGenerateRustFixtures)

s.Require().True(s.Run("Deploy ethereum contracts", func() {
args := append([]string{
"--trust-level", testvalues.DefaultTrustLevel.String(),
Expand Down Expand Up @@ -1214,7 +1217,7 @@ func (s *IbcEurekaTestSuite) getCommitmentProof(ctx context.Context, path []byte
RevisionHeight: s.LastEtheruemLightClientUpdate,
})

err = s.rustFixtureGenerator.GenerateRustFixture("commitment_proof", &types.CommitmentProofFixture{
s.rustFixtureGenerator.AddFixtureStep("commitment_proof", &types.CommitmentProofFixture{
Path: path,
StorageProof: storageProof,
ProofHeight: clienttypes.Height{
Expand All @@ -1224,7 +1227,6 @@ func (s *IbcEurekaTestSuite) getCommitmentProof(ctx context.Context, path []byte
ClientState: unionClientState,
ConsensusState: unionConsensusState,
})
s.Require().NoError(err)
}

return simd.Config().EncodingConfig.Codec.MustMarshal(&storageProof)
Expand Down
2 changes: 1 addition & 1 deletion e2e/interchaintestv8/testvalues/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const (
// SP1ICS07FixturesDir is the directory where the SP1ICS07 fixtures are stored.
SP1ICS07FixturesDir = "test/sp1-ics07/fixtures"
// RustFixturesDir is the directory where the Rust fixtures are stored.
RustFixturesDir = "packages/ethereum-test-utils/src/test/fixtures"
RustFixturesDir = "packages/ethereum-test-utils/src/fixtures"
// RelayerConfigFilePath is the path to generate the relayer config file.
RelayerConfigFilePath = "programs/relayer/config.json"
// E2EDeployScriptPath is the path to the E2E deploy script.
Expand Down
79 changes: 64 additions & 15 deletions e2e/interchaintestv8/types/rust_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/stretchr/testify/suite"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"

"github.com/srdtrk/solidity-ibc-eureka/e2e/v8/testvalues"
ethereumlightclient "github.com/srdtrk/solidity-ibc-eureka/e2e/v8/types/ethereumlightclient"
)

type InitialStateFixture struct {
ClientState ethereumlightclient.ClientState `json:"client_state"`
ConsensusState ethereumlightclient.ConsensusState `json:"consensus_state"`
}

type CommitmentProofFixture struct {
Path []byte `json:"path"`
StorageProof ethereumlightclient.StorageProof `json:"storage_proof"`
Expand All @@ -19,43 +27,84 @@ type CommitmentProofFixture struct {
ConsensusState ethereumlightclient.ConsensusState `json:"consensus_state"`
}

type UpdateClientFixture struct {
ClientState ethereumlightclient.ClientState `json:"client_state"`
ConsensusState ethereumlightclient.ConsensusState `json:"consensus_state"`
Updates []ethereumlightclient.Header `json:"updates"`
}

type Step struct {
Name string `json:"name"`
Data interface{} `json:"data"`
}

type RustFixture struct {
Steps []Step `json:"steps"`
}

type RustFixtureGenerator struct {
shouldGenerateFixture bool
prefix string

// fixtureCount is used to create a clear order of fixtures
fixtureCount uint
fixture RustFixture
}

// NewRustFixtureGenerator creates a new RustFixtureGenerator
// If shouldGenerateFixture is false, the generator will not generate any fixtures
func NewRustFixtureGenerator(prefix string, shouldGenerateFixture bool) *RustFixtureGenerator {
return &RustFixtureGenerator{
prefix: prefix,
func NewRustFixtureGenerator(s *suite.Suite, shouldGenerateFixture bool) *RustFixtureGenerator {
rustFixtureGenerator := &RustFixtureGenerator{
shouldGenerateFixture: shouldGenerateFixture,
}

fixtureName := getTopLevelTestName(s)

if shouldGenerateFixture {
s.T().Cleanup(func() {
s.T().Logf("Writing fixtures for %s", fixtureName)
if err := rustFixtureGenerator.writeFixtures(fixtureName); err != nil {
s.T().Logf("Error writing fixtures: %v", err)
}
})
}

return rustFixtureGenerator
}

// GenerateRustFixture generates a fixture by json marshalling jsonMarshalble and saves it to a file
func (g *RustFixtureGenerator) GenerateRustFixture(name string, jsonMarshalble interface{}) error {
func (g *RustFixtureGenerator) AddFixtureStep(stepName string, jsonMarshalble interface{}) {
if !g.shouldGenerateFixture {
return
}

g.fixture.Steps = append(g.fixture.Steps, Step{
Name: stepName,
Data: jsonMarshalble,
})
}

func (g *RustFixtureGenerator) ShouldGenerateFixture() bool {
return g.shouldGenerateFixture
}

func (g *RustFixtureGenerator) writeFixtures(fixtureName string) error {
if !g.shouldGenerateFixture {
return nil
}
filePath := fmt.Sprintf("%s/%s.json", testvalues.RustFixturesDir, fixtureName)

fixturesBz, err := json.MarshalIndent(jsonMarshalble, "", " ")
fixturesBz, err := json.MarshalIndent(g.fixture, "", " ")
if err != nil {
return err
}

g.fixtureCount++

fixtureName := fmt.Sprintf("%s_%d_%s", g.prefix, g.fixtureCount, name)
filePath := fmt.Sprintf("%s/%s.json", testvalues.RustFixturesDir, fixtureName)

// nolint:gosec
return os.WriteFile(filePath, fixturesBz, 0o644)
}

func (g *RustFixtureGenerator) ShouldGenerateFixture() bool {
return g.shouldGenerateFixture
func getTopLevelTestName(s *suite.Suite) string {
parts := strings.Split(s.T().Name(), "/")
if len(parts) >= 2 {
return parts[1]
}

return s.T().Name()
}
9 changes: 5 additions & 4 deletions packages/ethereum-light-client/src/membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mod test {
use crate::{
client_state::ClientState,
consensus_state::ConsensusState,
test::commitment_proof_fixture::CommitmentProofFixture,
test::fixture_types::CommitmentProof,
types::{storage_proof::StorageProof, wrappers::WrappedBytes},
};

Expand All @@ -101,9 +101,10 @@ mod test {

#[test]
fn test_with_fixture() {
let commitment_proof_fixture: CommitmentProofFixture = fixtures::load(
"TestICS20TransferNativeCosmosCoinsToEthereumAndBack_Groth16_4_commitment_proof",
);
let fixture: fixtures::StepFixture =
fixtures::load("TestICS20TransferNativeCosmosCoinsToEthereumAndBack_Groth16");

let commitment_proof_fixture: CommitmentProof = fixture.get_data_at_step(2);

let trusted_consensus_state = commitment_proof_fixture.consensus_state;
let client_state = commitment_proof_fixture.client_state;
Expand Down

This file was deleted.

39 changes: 39 additions & 0 deletions packages/ethereum-light-client/src/test/fixture_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use serde::{Deserialize, Serialize};

use crate::{
client_state::ClientState,
consensus_state::ConsensusState,
types::{height::Height, light_client::Header, storage_proof::StorageProof},
};

// TODO: Remove this file once these types are in a separate package #143

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub enum DataType {
InitialState(Box<InitialState>),
CommitmentProof(Box<CommitmentProof>),
UpdateClient(Box<UpdateClient>),
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct InitialState {
pub client_state: ClientState,
pub consensus_state: ConsensusState,
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct CommitmentProof {
#[serde(with = "ethereum_utils::base64")]
pub path: Vec<u8>,
pub storage_proof: StorageProof,
pub proof_height: Height,
pub client_state: ClientState,
pub consensus_state: ConsensusState,
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct UpdateClient {
pub client_state: ClientState,
pub consensus_state: ConsensusState,
pub updates: Vec<Header>,
}
2 changes: 1 addition & 1 deletion packages/ethereum-light-client/src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod commitment_proof_fixture;
pub mod fixture_types;
17 changes: 9 additions & 8 deletions packages/ethereum-light-client/src/types/sync_committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@ pub fn compute_sync_committee_period_at_slot(

#[cfg(test)]
mod test {
use crate::types::{
light_client::Header,
sync_committee::{SyncAggregate, SyncCommittee},
};
use crate::test::fixture_types::UpdateClient;
use crate::types::sync_committee::{SyncAggregate, SyncCommittee};

use alloy_primitives::{hex::FromHex, B256};
use alloy_rpc_types_beacon::BlsSignature;
Expand Down Expand Up @@ -171,10 +169,13 @@ mod test {
assert!(sync_aggregate.validate_signature_supermajority());

// valid sync aggregate from fixtures with supermajority
let fixture: Header = fixtures::load(
"TestICS20TransferNativeCosmosCoinsToEthereumAndBack_Groth16_3_update_header_0",
);
let sync_aggregate = fixture.consensus_update.sync_aggregate;
let fixture: fixtures::StepFixture =
fixtures::load("TestICS20TransferNativeCosmosCoinsToEthereumAndBack_Groth16");
let client_update: UpdateClient = fixture.get_data_at_step(1);
let sync_aggregate = client_update.updates[0]
.consensus_update
.sync_aggregate
.clone();
assert!(sync_aggregate.validate_signature_supermajority());
}
}
Loading

0 comments on commit 68a50d2

Please sign in to comment.