Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync gas price #1188

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 102 additions & 91 deletions relayer/cmd/generate_beacon_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func generateBeaconFixtureCmd() *cobra.Command {
}

cmd.Flags().String("config", "/tmp/snowbridge/beacon-relay.json", "Path to the beacon relay config")
cmd.Flags().Bool("wait_until_next_period", true, "Waiting until next period")
cmd.Flags().String("execution-config", "/tmp/snowbridge/execution-relay-asset-hub.json", "Path to the execution relay config")
cmd.Flags().Bool("wait-until-next-period", true, "Waiting until next period")
cmd.Flags().Uint32("nonce", 1, "Nonce of the inbound message")
cmd.Flags().Bool("finality-update-only", false, "Generate finality update only")
return cmd
}

Expand Down Expand Up @@ -83,9 +85,9 @@ 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.json", "Path to the execution relay config")
cmd.Flags().Uint32("nonce", 1, "Nonce of the inbound message")
cmd.Flags().String("test_case", "register_token", "Inbound test case")
cmd.Flags().String("test-case", "register_token", "Inbound test case")
return cmd
}

Expand All @@ -106,7 +108,7 @@ type InboundFixture struct {
const (
pathToBeaconTestFixtureFiles = "polkadot-sdk/bridges/snowbridge/pallets/ethereum-client/tests/fixtures"
pathToInboundQueueFixtureTemplate = "polkadot-sdk/bridges/snowbridge/templates/beacon-fixtures.mustache"
pathToInboundQueueFixtureData = "polkadot-sdk/bridges/snowbridge/pallets/ethereum-client/fixtures/src/lib.rs"
pathToEthereumClientFixtureData = "polkadot-sdk/bridges/snowbridge/pallets/ethereum-client/fixtures/src/lib.rs"
pathToInboundQueueFixtureTestCaseTemplate = "polkadot-sdk/bridges/snowbridge/templates/inbound-fixtures.mustache"
pathToInboundQueueFixtureTestCaseData = "polkadot-sdk/bridges/snowbridge/pallets/inbound-queue/fixtures/src/%s.rs"
)
Expand Down Expand Up @@ -206,8 +208,11 @@ 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")

executionConfigFile, err := cmd.Flags().GetString("execution-config")
if err != nil {
return err
}
viper.SetConfigFile(executionConfigFile)
if err = viper.ReadInConfig(); err != nil {
return err
}
Expand Down Expand Up @@ -261,91 +266,104 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
}
log.Info("created sync committee update file")

// get inbound message data start
channelID := executionConfig.Source.ChannelID
address := common.HexToAddress(executionConfig.Source.Contracts.Gateway)
gatewayContract, err := contracts.NewGateway(address, ethconn.Client())
if err != nil {
return err
}
nonce, err := cmd.Flags().GetUint32("nonce")
if err != nil {
return err
}
event, err := getEthereumEvent(ctx, gatewayContract, channelID, nonce)
if err != nil {
return err
}
receiptTrie, err := headerCache.GetReceiptTrie(ctx, event.Raw.BlockHash)
if err != nil {
return err
}
inboundMessage, err := ethereum.MakeMessageFromEvent(&event.Raw, receiptTrie)
if err != nil {
return err
}
messageBlockNumber := event.Raw.BlockNumber
var headerUpdate beaconjson.HeaderUpdate
var messageJSON parachain.MessageJSON
var beaconFinalizedUpdate *scale.Update

log.WithFields(log.Fields{
"message": inboundMessage,
"blockHash": event.Raw.BlockHash.Hex(),
"blockNumber": messageBlockNumber,
}).Info("event is at block")
finalityUpdateOnly, err := cmd.Flags().GetBool("finality-update-only")
if finalityUpdateOnly {
beaconFinalizedUpdate, err = getFinalizedUpdate(*s, syncCommitteeUpdate.FinalizedHeader.Slot+1)
if err != nil {
return err
}
} else {
// get inbound message data start
channelID := executionConfig.Source.ChannelID
address := common.HexToAddress(executionConfig.Source.Contracts.Gateway)
gatewayContract, err := contracts.NewGateway(address, ethconn.Client())
if err != nil {
return err
}
nonce, err := cmd.Flags().GetUint32("nonce")
if err != nil {
return err
}
event, err := getEthereumEvent(ctx, gatewayContract, channelID, nonce)
if err != nil {
return err
}
receiptTrie, err := headerCache.GetReceiptTrie(ctx, event.Raw.BlockHash)
if err != nil {
return err
}
inboundMessage, err := ethereum.MakeMessageFromEvent(&event.Raw, receiptTrie)
if err != nil {
return err
}
messageBlockNumber := event.Raw.BlockNumber

finalizedUpdateAfterMessage, err := getFinalizedUpdate(*s, messageBlockNumber)
if err != nil {
return err
}
log.WithFields(log.Fields{
"message": inboundMessage,
"blockHash": event.Raw.BlockHash.Hex(),
"blockNumber": messageBlockNumber,
}).Info("event is at block")

finalizedHeaderSlot := uint64(finalizedUpdateAfterMessage.Payload.FinalizedHeader.Slot)
beaconFinalizedUpdate, err = getFinalizedUpdate(*s, messageBlockNumber)
if err != nil {
return err
}

beaconBlock, blockNumber, err := getBeaconBlockContainingExecutionHeader(*s, messageBlockNumber, finalizedHeaderSlot)
if err != nil {
return fmt.Errorf("get beacon block containing header: %w", err)
}
finalizedHeaderSlot := uint64(beaconFinalizedUpdate.Payload.FinalizedHeader.Slot)

beaconBlockSlot, err := strconv.ParseUint(beaconBlock.Data.Message.Slot, 10, 64)
if err != nil {
return err
}
beaconBlock, blockNumber, err := getBeaconBlockContainingExecutionHeader(*s, messageBlockNumber, finalizedHeaderSlot)
if err != nil {
return fmt.Errorf("get beacon block containing header: %w", err)
}

if blockNumber == messageBlockNumber {
log.WithFields(log.Fields{
"slot": beaconBlock.Data.Message.Slot,
"blockHash": beaconBlock.Data.Message.Body.ExecutionPayload.BlockHash,
"blockNumber": blockNumber,
}).WithError(err).Info("found execution header containing event")
}
beaconBlockSlot, err := strconv.ParseUint(beaconBlock.Data.Message.Slot, 10, 64)
if err != nil {
return err
}

checkPoint := cache.Proof{
FinalizedBlockRoot: finalizedUpdateAfterMessage.FinalizedHeaderBlockRoot,
BlockRootsTree: finalizedUpdateAfterMessage.BlockRootsTree,
Slot: uint64(finalizedUpdateAfterMessage.Payload.FinalizedHeader.Slot),
}
headerUpdateScale, err := s.GetHeaderUpdateBySlotWithCheckpoint(beaconBlockSlot, &checkPoint)
if err != nil {
return fmt.Errorf("get header update: %w", err)
}
inboundMessage.Proof.ExecutionProof = headerUpdateScale
headerUpdate := headerUpdateScale.ToJSON()
if blockNumber == messageBlockNumber {
log.WithFields(log.Fields{
"slot": beaconBlock.Data.Message.Slot,
"blockHash": beaconBlock.Data.Message.Body.ExecutionPayload.BlockHash,
"blockNumber": blockNumber,
}).WithError(err).Info("found execution header containing event")
}

log.WithField("blockNumber", blockNumber).Info("found beacon block by slot")
checkPoint := cache.Proof{
FinalizedBlockRoot: beaconFinalizedUpdate.FinalizedHeaderBlockRoot,
BlockRootsTree: beaconFinalizedUpdate.BlockRootsTree,
Slot: uint64(beaconFinalizedUpdate.Payload.FinalizedHeader.Slot),
}
headerUpdateScale, err := s.GetHeaderUpdateBySlotWithCheckpoint(beaconBlockSlot, &checkPoint)
if err != nil {
return fmt.Errorf("get header update: %w", err)
}
inboundMessage.Proof.ExecutionProof = headerUpdateScale
headerUpdate = headerUpdateScale.ToJSON()

messageJSON := inboundMessage.ToJSON()
log.WithField("blockNumber", blockNumber).Info("found beacon block by slot")

err = writeJSONToFile(headerUpdate, fmt.Sprintf("%s/%s", pathToBeaconTestFixtureFiles, "execution-proof.json"))
if err != nil {
return err
}
log.Info("created execution update file")
err = writeJSONToFile(messageJSON, fmt.Sprintf("%s/%s", pathToBeaconTestFixtureFiles, "inbound-message.json"))
if err != nil {
return err
}
log.Info("created inbound message file")
// get inbound message data end
messageJSON = inboundMessage.ToJSON()

finalizedUpdate := finalizedUpdateAfterMessage.Payload.ToJSON()
err = writeJSONToFile(headerUpdate, fmt.Sprintf("%s/%s", pathToBeaconTestFixtureFiles, "execution-proof.json"))
if err != nil {
return err
}
log.Info("created execution update file")
err = writeJSONToFile(messageJSON, fmt.Sprintf("%s/%s", pathToBeaconTestFixtureFiles, "inbound-message.json"))
if err != nil {
return err
}
log.Info("created inbound message file")
// get inbound message data end
headerUpdate.RemoveLeadingZeroHashes()
messageJSON.RemoveLeadingZeroHashes()
}
finalizedUpdate := beaconFinalizedUpdate.Payload.ToJSON()
if finalizedUpdate.AttestedHeader.Slot <= initialSyncHeaderSlot {
return fmt.Errorf("AttestedHeader slot should be greater than initialSyncHeaderSlot")
}
Expand All @@ -371,8 +389,6 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
initialSync.RemoveLeadingZeroHashes()
syncCommitteeUpdate.RemoveLeadingZeroHashes()
finalizedUpdate.RemoveLeadingZeroHashes()
headerUpdate.RemoveLeadingZeroHashes()
messageJSON.RemoveLeadingZeroHashes()

data := Data{
CheckpointUpdate: initialSync,
Expand All @@ -388,15 +404,15 @@ func generateBeaconTestFixture(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("render inbound queue benchmark fixture: %w", err)
}
log.WithFields(log.Fields{
"location": pathToInboundQueueFixtureData,
"location": pathToEthereumClientFixtureData,
}).Info("writing result file")
err = writeRawDataFile(fmt.Sprintf("%s", pathToInboundQueueFixtureData), rendered)
err = writeRawDataFile(fmt.Sprintf("%s", pathToEthereumClientFixtureData), 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")
waitUntilNextPeriod, err := cmd.Flags().GetBool("wait-until-next-period")
if waitUntilNextPeriod {
log.Info("waiting finalized_update in next period (5 hours later), be patient and wait...")
for {
Expand Down Expand Up @@ -537,11 +553,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 @@ -809,7 +820,7 @@ func generateInboundFixture(cmd *cobra.Command, _ []string) error {
messageJSON.RemoveLeadingZeroHashes()

// writing inbound fixture by test case
testCase, err := cmd.Flags().GetString("test_case")
testCase, err := cmd.Flags().GetString("test-case")
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion relayer/relays/beacon/header/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package header

import (
"context"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/snowfork/snowbridge/relayer/relays/beacon/config"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/api"
Expand All @@ -11,7 +13,6 @@ import (
"github.com/snowfork/snowbridge/relayer/relays/beacon/store"
"github.com/snowfork/snowbridge/relayer/relays/testutil"
"github.com/stretchr/testify/require"
"testing"
)

// Verifies that the closest checkpoint is populated successfully if it is not populated in the first place.
Expand Down Expand Up @@ -43,6 +44,7 @@ func TestSyncInterimFinalizedUpdate_WithDataFromAPI(t *testing.T) {
common.HexToHash("0x5119c1f71943a3eea34ddc48c7fe399d4b66f939350036431847ed0913448749"): headerAtSlot4571072,
}
client.BlocksAtSlot = map[uint64]api.BeaconBlockResponse{
4571072: blockAtSlot4571137,
4571137: blockAtSlot4571137,
}

Expand Down Expand Up @@ -100,6 +102,7 @@ func TestSyncInterimFinalizedUpdate_WithDataFromStore(t *testing.T) {
common.HexToHash("0x5119c1f71943a3eea34ddc48c7fe399d4b66f939350036431847ed0913448749"): headerAtSlot4571072,
}
client.BlocksAtSlot = map[uint64]api.BeaconBlockResponse{
4571072: blockAtSlot4571137,
4571137: blockAtSlot4571137,
}

Expand Down Expand Up @@ -164,7 +167,9 @@ func TestSyncInterimFinalizedUpdate_WithDataFromStoreWithDifferentBlocks(t *test
client.Header = map[common.Hash]api.BeaconHeader{
common.HexToHash("0x968a372336b4e08a6bbd25e9f31b336d322ede1e5c70763f61d2241ad3d66d36"): headerAtSlot4570752,
}
blockAtSlot4571137, err := testutil.GetBlockAtSlot(4571137)
client.BlocksAtSlot = map[uint64]api.BeaconBlockResponse{
4570752: blockAtSlot4571137,
4570818: blockAtSlot4570818,
}

Expand Down
3 changes: 1 addition & 2 deletions relayer/relays/beacon/header/syncer/api/api_deneb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
beaconjson "github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/json"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale"
"github.com/snowfork/snowbridge/relayer/relays/beacon/state"
"github.com/snowfork/snowbridge/relayer/relays/util"
Expand Down Expand Up @@ -55,7 +54,7 @@ func DenebExecutionPayloadToScale(e *state.ExecutionPayloadDeneb) (scale.Executi
}, nil
}

func DenebJsonExecutionPayloadHeaderToScale(e *beaconjson.FullExecutionPayloadHeaderJson) (scale.ExecutionPayloadHeaderDeneb, error) {
func DenebJsonExecutionPayloadHeaderToScale(e ExecutionHeaderResponse) (scale.ExecutionPayloadHeaderDeneb, error) {
var executionPayloadHeader scale.ExecutionPayloadHeaderDeneb
var baseFeePerGas big.Int
baseFeePerGasU64, err := util.ToUint64(e.BaseFeePerGas)
Expand Down
Loading
Loading