Skip to content

Commit

Permalink
fix test fixture generator
Browse files Browse the repository at this point in the history
  • Loading branch information
alistair-singh committed Dec 20, 2024
1 parent 8899527 commit 6b984bf
Show file tree
Hide file tree
Showing 3 changed files with 376 additions and 10 deletions.
21 changes: 11 additions & 10 deletions relayer/cmd/generate_beacon_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func generateInboundFixtureCmd() *cobra.Command {
}

cmd.Flags().String("beacon-config", "/tmp/snowbridge/beacon-relay.json", "Path to the beacon relay config")
cmd.Flags().String("execution-config", "/tmp/snowbridge/execution-relay-asset-hub.json", "Path to the beacon relay config")
cmd.Flags().String("execution-config", "/tmp/snowbridge/execution-relay-asset-hub-0.json", "Path to the beacon relay config")
cmd.Flags().Uint32("nonce", 1, "Nonce of the inbound message")
cmd.Flags().String("test_case", "register_token", "Inbound test case")
return cmd
Expand Down Expand Up @@ -206,7 +206,7 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
client := api.NewBeaconClient(conf.Source.Beacon.Endpoint, conf.Source.Beacon.StateEndpoint)
s := syncer.New(client, &store, p)

viper.SetConfigFile("/tmp/snowbridge/execution-relay-asset-hub.json")
viper.SetConfigFile("/tmp/snowbridge/execution-relay-asset-hub-0.json")

if err = viper.ReadInConfig(); err != nil {
return err
Expand Down Expand Up @@ -390,13 +390,16 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
log.WithFields(log.Fields{
"location": pathToInboundQueueFixtureData,
}).Info("writing result file")
err = writeRawDataFile(fmt.Sprintf("%s", pathToInboundQueueFixtureData), rendered)
err = writeRawDataFile(pathToInboundQueueFixtureData, rendered)
if err != nil {
return err
}

// Generate test fixture in next period (require waiting a long time)
waitUntilNextPeriod, err := cmd.Flags().GetBool("wait_until_next_period")
if err != nil {
return fmt.Errorf("could not parse flag wait_until_next_period: %w", err)
}
if waitUntilNextPeriod {
log.Info("waiting finalized_update in next period (5 hours later), be patient and wait...")
for {
Expand Down Expand Up @@ -537,11 +540,6 @@ func generateExecutionUpdate(cmd *cobra.Command, _ []string) error {
return nil
}

func generateInboundTestFixture(ctx context.Context, beaconEndpoint string) error {

return nil
}

func getEthereumEvent(ctx context.Context, gatewayContract *contracts.Gateway, channelID executionConf.ChannelID, nonce uint32) (*contracts.GatewayOutboundMessageAccepted, error) {
maxBlockNumber := uint64(10000)

Expand Down Expand Up @@ -613,6 +611,9 @@ func getBeaconBlockContainingExecutionHeader(s syncer.Syncer, messageBlockNumber
log.WithField("beaconHeaderSlot", beaconHeaderSlot).Info("getting beacon block by slot")

beaconBlock, blockNumber, err = getBeaconBlockAndBlockNumber(s, beaconHeaderSlot)
if err != nil {
return api.BeaconBlockResponse{}, 0, err
}
}

return beaconBlock, blockNumber, nil
Expand Down Expand Up @@ -813,7 +814,7 @@ func generateInboundFixture(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
if testCase != "register_token" && testCase != "send_token" && testCase != "send_token_to_penpal" {
if testCase != "register_token" && testCase != "send_token" && testCase != "send_token_to_penpal" && testCase != "send_native_eth" {
return fmt.Errorf("invalid test case: %s", testCase)
}

Expand All @@ -830,7 +831,7 @@ func generateInboundFixture(cmd *cobra.Command, _ []string) error {
}

pathToInboundQueueFixtureTestCaseData := fmt.Sprintf(pathToInboundQueueFixtureTestCaseData, testCase)
err = writeRawDataFile(fmt.Sprintf("%s", pathToInboundQueueFixtureTestCaseData), rendered)
err = writeRawDataFile(pathToInboundQueueFixtureTestCaseData, rendered)
if err != nil {
return err
}
Expand Down
270 changes: 270 additions & 0 deletions relayer/templates/beacon-fixtures.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
// Generated, do not edit!
// See README.md for instructions to generate
#![cfg_attr(not(feature = "std"), no_std)]

use hex_literal::hex;
use snowbridge_beacon_primitives::{
types::deneb, AncestryProof, BeaconHeader, ExecutionProof, NextSyncCommitteeUpdate,
SyncAggregate, SyncCommittee, VersionedExecutionPayloadHeader,
};
use snowbridge_core::inbound::{InboundQueueFixture, Log, Message, Proof};
use sp_core::U256;
use sp_std::{boxed::Box, vec};

const SC_SIZE: usize = 512;
const SC_BITS_SIZE: usize = 64;
type CheckpointUpdate = snowbridge_beacon_primitives::CheckpointUpdate<SC_SIZE>;
type Update = snowbridge_beacon_primitives::Update<SC_SIZE, SC_BITS_SIZE>;


pub fn make_checkpoint() -> Box<CheckpointUpdate> {
Box::new(CheckpointUpdate {
header: BeaconHeader {
slot: {{CheckpointUpdate.Header.Slot}},
proposer_index: {{CheckpointUpdate.Header.ProposerIndex}},
parent_root: hex!("{{CheckpointUpdate.Header.ParentRoot}}").into(),
state_root: hex!("{{CheckpointUpdate.Header.StateRoot}}").into(),
body_root: hex!("{{CheckpointUpdate.Header.BodyRoot}}").into(),
},
current_sync_committee: SyncCommittee {
pubkeys: [
{{#CheckpointUpdate.CurrentSyncCommittee.Pubkeys}}
hex!("{{.}}").into(),
{{/CheckpointUpdate.CurrentSyncCommittee.Pubkeys}}
],
aggregate_pubkey: hex!("{{CheckpointUpdate.CurrentSyncCommittee.AggregatePubkey}}").into(),
},
current_sync_committee_branch: vec![
{{#CheckpointUpdate.CurrentSyncCommitteeBranch}}
hex!("{{.}}").into(),
{{/CheckpointUpdate.CurrentSyncCommitteeBranch}}
],
validators_root: hex!("{{CheckpointUpdate.ValidatorsRoot}}").into(),
block_roots_root: hex!("{{CheckpointUpdate.BlockRootsRoot}}").into(),
block_roots_branch: vec![
{{#CheckpointUpdate.BlockRootsBranch}}
hex!("{{.}}").into(),
{{/CheckpointUpdate.BlockRootsBranch}}
],
})
}

pub fn make_sync_committee_update() -> Box<Update> {
Box::new(Update {
attested_header: BeaconHeader {
slot: {{SyncCommitteeUpdate.AttestedHeader.Slot}},
proposer_index: {{SyncCommitteeUpdate.AttestedHeader.ProposerIndex}},
parent_root: hex!("{{SyncCommitteeUpdate.AttestedHeader.ParentRoot}}").into(),
state_root: hex!("{{SyncCommitteeUpdate.AttestedHeader.StateRoot}}").into(),
body_root: hex!("{{SyncCommitteeUpdate.AttestedHeader.BodyRoot}}").into(),
},
sync_aggregate: SyncAggregate{
sync_committee_bits: hex!("{{SyncCommitteeUpdate.SyncAggregate.SyncCommitteeBits}}"),
sync_committee_signature: hex!("{{SyncCommitteeUpdate.SyncAggregate.SyncCommitteeSignature}}").into(),
},
signature_slot: {{SyncCommitteeUpdate.SignatureSlot}},
next_sync_committee_update: Some(NextSyncCommitteeUpdate {
next_sync_committee: SyncCommittee {
pubkeys: [
{{#SyncCommitteeUpdate.NextSyncCommitteeUpdate.NextSyncCommittee.Pubkeys}}
hex!("{{.}}").into(),
{{/SyncCommitteeUpdate.NextSyncCommitteeUpdate.NextSyncCommittee.Pubkeys}}
],
aggregate_pubkey: hex!("{{SyncCommitteeUpdate.NextSyncCommitteeUpdate.NextSyncCommittee.AggregatePubkey}}").into(),
},
next_sync_committee_branch: vec![
{{#SyncCommitteeUpdate.NextSyncCommitteeUpdate.NextSyncCommitteeBranch}}
hex!("{{.}}").into(),
{{/SyncCommitteeUpdate.NextSyncCommitteeUpdate.NextSyncCommitteeBranch}}
],
}),
finalized_header: BeaconHeader{
slot: {{SyncCommitteeUpdate.FinalizedHeader.Slot}},
proposer_index: {{SyncCommitteeUpdate.FinalizedHeader.ProposerIndex}},
parent_root: hex!("{{SyncCommitteeUpdate.FinalizedHeader.ParentRoot}}").into(),
state_root: hex!("{{SyncCommitteeUpdate.FinalizedHeader.StateRoot}}").into(),
body_root: hex!("{{SyncCommitteeUpdate.FinalizedHeader.BodyRoot}}").into(),
},
finality_branch: vec![
{{#SyncCommitteeUpdate.FinalityBranch}}
hex!("{{.}}").into(),
{{/SyncCommitteeUpdate.FinalityBranch}}
],
block_roots_root: hex!("{{SyncCommitteeUpdate.BlockRootsRoot}}").into(),
block_roots_branch: vec![
{{#SyncCommitteeUpdate.BlockRootsBranch}}
hex!("{{.}}").into(),
{{/SyncCommitteeUpdate.BlockRootsBranch}}
],
})
}

pub fn make_finalized_header_update() -> Box<Update> {
Box::new(Update {
attested_header: BeaconHeader {
slot: {{FinalizedHeaderUpdate.AttestedHeader.Slot}},
proposer_index: {{FinalizedHeaderUpdate.AttestedHeader.ProposerIndex}},
parent_root: hex!("{{FinalizedHeaderUpdate.AttestedHeader.ParentRoot}}").into(),
state_root: hex!("{{FinalizedHeaderUpdate.AttestedHeader.StateRoot}}").into(),
body_root: hex!("{{FinalizedHeaderUpdate.AttestedHeader.BodyRoot}}").into(),
},
sync_aggregate: SyncAggregate{
sync_committee_bits: hex!("{{FinalizedHeaderUpdate.SyncAggregate.SyncCommitteeBits}}"),
sync_committee_signature: hex!("{{FinalizedHeaderUpdate.SyncAggregate.SyncCommitteeSignature}}").into(),
},
signature_slot: {{FinalizedHeaderUpdate.SignatureSlot}},
next_sync_committee_update: None,
finalized_header: BeaconHeader {
slot: {{FinalizedHeaderUpdate.FinalizedHeader.Slot}},
proposer_index: {{FinalizedHeaderUpdate.FinalizedHeader.ProposerIndex}},
parent_root: hex!("{{FinalizedHeaderUpdate.FinalizedHeader.ParentRoot}}").into(),
state_root: hex!("{{FinalizedHeaderUpdate.FinalizedHeader.StateRoot}}").into(),
body_root: hex!("{{FinalizedHeaderUpdate.FinalizedHeader.BodyRoot}}").into(),
},
finality_branch: vec![
{{#FinalizedHeaderUpdate.FinalityBranch}}
hex!("{{.}}").into(),
{{/FinalizedHeaderUpdate.FinalityBranch}}
],
block_roots_root: hex!("{{FinalizedHeaderUpdate.BlockRootsRoot}}").into(),
block_roots_branch: vec![
{{#FinalizedHeaderUpdate.BlockRootsBranch}}
hex!("{{.}}").into(),
{{/FinalizedHeaderUpdate.BlockRootsBranch}}
]
})
}

pub fn make_execution_proof() -> Box<ExecutionProof> {
Box::new(ExecutionProof {
header: BeaconHeader {
slot: {{HeaderUpdate.Header.Slot}},
proposer_index: {{HeaderUpdate.Header.ProposerIndex}},
parent_root: hex!("{{HeaderUpdate.Header.ParentRoot}}").into(),
state_root: hex!("{{HeaderUpdate.Header.StateRoot}}").into(),
body_root: hex!("{{HeaderUpdate.Header.BodyRoot}}").into(),
},
{{#HeaderUpdate.AncestryProof}}
ancestry_proof: Some(AncestryProof {
header_branch: vec![
{{#HeaderUpdate.AncestryProof.HeaderBranch}}
hex!("{{.}}").into(),
{{/HeaderUpdate.AncestryProof.HeaderBranch}}
],
finalized_block_root: hex!("{{HeaderUpdate.AncestryProof.FinalizedBlockRoot}}").into(),
}),
{{/HeaderUpdate.AncestryProof}}
{{^HeaderUpdate.AncestryProof}}
ancestry_proof: None,
{{/HeaderUpdate.AncestryProof}}
execution_header: VersionedExecutionPayloadHeader::Deneb(deneb::ExecutionPayloadHeader {
parent_hash: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.ParentHash}}").into(),
fee_recipient: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.FeeRecipient}}").into(),
state_root: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.StateRoot}}").into(),
receipts_root: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.ReceiptsRoot}}").into(),
logs_bloom: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.LogsBloom}}").into(),
prev_randao: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.PrevRandao}}").into(),
block_number: {{HeaderUpdate.ExecutionHeader.Deneb.BlockNumber}},
gas_limit: {{HeaderUpdate.ExecutionHeader.Deneb.GasLimit}},
gas_used: {{HeaderUpdate.ExecutionHeader.Deneb.GasUsed}},
timestamp: {{HeaderUpdate.ExecutionHeader.Deneb.Timestamp}},
extra_data: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.ExtraData}}").into(),
base_fee_per_gas: U256::from({{HeaderUpdate.ExecutionHeader.Deneb.BaseFeePerGas}}u64),
block_hash: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.BlockHash}}").into(),
transactions_root: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.TransactionsRoot}}").into(),
withdrawals_root: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.WithdrawalsRoot}}").into(),
blob_gas_used: {{HeaderUpdate.ExecutionHeader.Deneb.BlobGasUsed}},
excess_blob_gas: {{HeaderUpdate.ExecutionHeader.Deneb.ExcessBlobGas}},
}),
execution_branch: vec![
{{#HeaderUpdate.ExecutionBranch}}
hex!("{{.}}").into(),
{{/HeaderUpdate.ExecutionBranch}}
],
})
}

pub fn make_inbound_fixture() -> InboundQueueFixture {
InboundQueueFixture {
message: Message {
event_log: Log {
address: hex!("{{InboundMessage.EventLog.Address}}").into(),
topics: vec![
{{#InboundMessage.EventLog.Topics}}
hex!("{{.}}").into(),
{{/InboundMessage.EventLog.Topics}}
],
data: hex!("{{InboundMessage.EventLog.Data}}").into(),
},
proof: Proof {
block_hash: hex!("{{InboundMessage.Proof.BlockHash}}").into(),
tx_index: {{InboundMessage.Proof.TxIndex}},
receipt_proof: (vec![
{{#InboundMessage.Proof.ReceiptProof.Keys}}
hex!("{{.}}").to_vec(),
{{/InboundMessage.Proof.ReceiptProof.Keys}}
], vec![
{{#InboundMessage.Proof.ReceiptProof.Values}}
hex!("{{.}}").to_vec(),
{{/InboundMessage.Proof.ReceiptProof.Values}}
]),
execution_proof: ExecutionProof {
header: BeaconHeader {
slot: {{HeaderUpdate.Header.Slot}},
proposer_index: {{HeaderUpdate.Header.ProposerIndex}},
parent_root: hex!("{{HeaderUpdate.Header.ParentRoot}}").into(),
state_root: hex!("{{HeaderUpdate.Header.StateRoot}}").into(),
body_root: hex!("{{HeaderUpdate.Header.BodyRoot}}").into(),
},
{{#HeaderUpdate.AncestryProof}}
ancestry_proof: Some(AncestryProof {
header_branch: vec![
{{#HeaderUpdate.AncestryProof.HeaderBranch}}
hex!("{{.}}").into(),
{{/HeaderUpdate.AncestryProof.HeaderBranch}}
],
finalized_block_root: hex!("{{HeaderUpdate.AncestryProof.FinalizedBlockRoot}}").into(),
}),
{{/HeaderUpdate.AncestryProof}}
{{^HeaderUpdate.AncestryProof}}
ancestry_proof: None,
{{/HeaderUpdate.AncestryProof}}
execution_header: VersionedExecutionPayloadHeader::Deneb(deneb::ExecutionPayloadHeader {
parent_hash: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.ParentHash}}").into(),
fee_recipient: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.FeeRecipient}}").into(),
state_root: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.StateRoot}}").into(),
receipts_root: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.ReceiptsRoot}}").into(),
logs_bloom: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.LogsBloom}}").into(),
prev_randao: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.PrevRandao}}").into(),
block_number: {{HeaderUpdate.ExecutionHeader.Deneb.BlockNumber}},
gas_limit: {{HeaderUpdate.ExecutionHeader.Deneb.GasLimit}},
gas_used: {{HeaderUpdate.ExecutionHeader.Deneb.GasUsed}},
timestamp: {{HeaderUpdate.ExecutionHeader.Deneb.Timestamp}},
extra_data: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.ExtraData}}").into(),
base_fee_per_gas: U256::from({{HeaderUpdate.ExecutionHeader.Deneb.BaseFeePerGas}}u64),
block_hash: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.BlockHash}}").into(),
transactions_root: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.TransactionsRoot}}").into(),
withdrawals_root: hex!("{{HeaderUpdate.ExecutionHeader.Deneb.WithdrawalsRoot}}").into(),
blob_gas_used: {{HeaderUpdate.ExecutionHeader.Deneb.BlobGasUsed}},
excess_blob_gas: {{HeaderUpdate.ExecutionHeader.Deneb.ExcessBlobGas}},
}),
execution_branch: vec![
{{#HeaderUpdate.ExecutionBranch}}
hex!("{{.}}").into(),
{{/HeaderUpdate.ExecutionBranch}}
],
}
},
},
finalized_header: BeaconHeader {
slot: {{FinalizedHeaderUpdate.FinalizedHeader.Slot}},
proposer_index: {{FinalizedHeaderUpdate.FinalizedHeader.ProposerIndex}},
parent_root: hex!("{{FinalizedHeaderUpdate.FinalizedHeader.ParentRoot}}").into(),
state_root: hex!("{{FinalizedHeaderUpdate.FinalizedHeader.StateRoot}}").into(),
body_root: hex!("{{FinalizedHeaderUpdate.FinalizedHeader.BodyRoot}}").into(),
},
block_roots_root: hex!("{{FinalizedHeaderUpdate.BlockRootsRoot}}").into(),
}
}
Loading

0 comments on commit 6b984bf

Please sign in to comment.