This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds import execution header subcommand (paritytech#691)
* Adds import execution header subcommand * Script to import an execution header * Tiny bit of cleanup * Update relayer/cmd/import_execution_header.go Co-authored-by: David Dunn <26876072+doubledup@users.noreply.github.com> * Minor cleanup. Co-authored-by: David Dunn <26876072+doubledup@users.noreply.github.com>
- Loading branch information
1 parent
afe3560
commit e452118
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/snowfork/snowbridge/relayer/chain/parachain" | ||
"github.com/snowfork/snowbridge/relayer/crypto/sr25519" | ||
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer" | ||
"github.com/snowfork/snowbridge/relayer/relays/beacon/writer" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func importExecutionHeaderCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "import-execution-header", | ||
Short: "Import the provided execution header.", | ||
Args: cobra.ExactArgs(0), | ||
RunE: importExecutionHeaderFn, | ||
} | ||
|
||
cmd.Flags().String("beacon-header", "", "Beacon header hash whose execution header will be imported") | ||
cmd.MarkFlagRequired("beacon-header") | ||
|
||
cmd.Flags().String("parachain-endpoint", "", "Parachain API URL") | ||
cmd.MarkFlagRequired("parachain-endpoint") | ||
|
||
cmd.Flags().String("lodestar-endpoint", "", "Lodestar API URL") | ||
cmd.MarkFlagRequired("lodestar-endpoint") | ||
|
||
cmd.Flags().String("private-key-file", "", "File containing the private key for the relayer") | ||
cmd.MarkFlagRequired("private-key-file") | ||
|
||
return cmd | ||
} | ||
|
||
func importExecutionHeaderFn(cmd *cobra.Command, _ []string) error { | ||
|
||
err := func() error { | ||
ctx := cmd.Context() | ||
|
||
eg, ctx := errgroup.WithContext(ctx) | ||
|
||
parachainEndpoint, _ := cmd.Flags().GetString("parachain-endpoint") | ||
privateKeyFile, _ := cmd.Flags().GetString("private-key-file") | ||
lodestarEndpoint, _ := cmd.Flags().GetString("lodestar-endpoint") | ||
beaconHeader, _ := cmd.Flags().GetString("beacon-header") | ||
|
||
keypair, err := getKeyPair(privateKeyFile) | ||
if err != nil { | ||
return fmt.Errorf("get keypair from file: %w", err) | ||
} | ||
|
||
paraconn := parachain.NewConnection(parachainEndpoint, keypair.AsKeyringPair()) | ||
err = paraconn.Connect(ctx) | ||
if err != nil { | ||
return fmt.Errorf("connect to parachain: %w", err) | ||
} | ||
|
||
writer := writer.NewParachainWriter(paraconn, 32) | ||
err = writer.Start(ctx, eg) | ||
if err != nil { | ||
return fmt.Errorf("start parachain conn: %w", err) | ||
} | ||
|
||
log.WithField("hash", beaconHeader).Info("will be syncing execution header for beacon hash") | ||
|
||
syncer := syncer.New(lodestarEndpoint, 32, 256) | ||
|
||
beaconHeaderHash := common.HexToHash(beaconHeader) | ||
|
||
update, err := syncer.GetHeaderUpdate(beaconHeaderHash) | ||
if err != nil { | ||
return fmt.Errorf("get header update: %w", err) | ||
} | ||
log.WithField("slot", update.Block.Slot).Info("found block at slot") | ||
|
||
syncAggregate, err := syncer.GetSyncAggregateForSlot(uint64(update.Block.Slot) + 1) | ||
if err != nil { | ||
return fmt.Errorf("get sync aggregate: %w", err) | ||
} | ||
log.Info("found sync aggregate") | ||
|
||
update.SyncAggregate = syncAggregate | ||
|
||
err = writer.WriteToParachainAndWatch(ctx, "EthereumBeaconClient.import_execution_header", update) | ||
if err != nil { | ||
return fmt.Errorf("write to parachain: %w", err) | ||
} | ||
log.Info("imported execution header") | ||
|
||
return nil | ||
}() | ||
if err != nil { | ||
log.WithError(err).Error("error importing execution header") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getKeyPair(privateKeyFile string) (*sr25519.Keypair, error) { | ||
var cleanedKeyURI string | ||
content, err := ioutil.ReadFile(privateKeyFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot read key file: %w", err) | ||
} | ||
cleanedKeyURI = strings.TrimSpace(string(content)) | ||
keypair, err := sr25519.NewKeypairFromSeed(cleanedKeyURI, 42) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse private key URI: %w", err) | ||
} | ||
|
||
return keypair, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters