Skip to content

Commit

Permalink
Fix updating with current beefy state
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed May 28, 2024
1 parent 0faba1f commit 5eba01b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
10 changes: 6 additions & 4 deletions relayer/relays/beefy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ func NewRelay(config *Config, ethereumKeypair *secp256k1.Keypair) (*Relay, error

log.Info("Beefy relay created")

return &Relay{
relayer := Relay{
config: config,
relaychainConn: relaychainConn,
ethereumConn: ethereumConn,
polkadotListener: polkadotListener,
ethereumWriter: ethereumWriter,
}, nil
}
polkadotListener.relayer = &relayer
return &relayer, nil
}

func (relay *Relay) Start(ctx context.Context, eg *errgroup.Group) error {
Expand All @@ -57,7 +59,7 @@ func (relay *Relay) Start(ctx context.Context, eg *errgroup.Group) error {
return fmt.Errorf("create ethereum connection: %w", err)
}

currentState, err := relay.getCurrentState(ctx)
currentState, err := relay.CurrentState(ctx)
if err != nil {
return fmt.Errorf("fetch BeefyClient current state: %w", err)
}
Expand All @@ -78,7 +80,7 @@ func (relay *Relay) Start(ctx context.Context, eg *errgroup.Group) error {
return nil
}

func (relay *Relay) getCurrentState(ctx context.Context) (BeefyState, error) {
func (relay *Relay) CurrentState(ctx context.Context) (BeefyState, error) {
var currentState BeefyState
address := common.HexToAddress(relay.config.Sink.Contracts.BeefyClient)
beefyClient, err := contracts.NewBeefyClient(address, relay.ethereumConn.Client())
Expand Down
16 changes: 11 additions & 5 deletions relayer/relays/beefy/polkadot-listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PolkadotListener struct {
config *SourceConfig
conn *relaychain.Connection
beefyAuthoritiesKey types.StorageKey
relayer *Relay
}

func NewPolkadotListener(
Expand Down Expand Up @@ -96,18 +97,23 @@ func (li *PolkadotListener) scanCommitments(
if err != nil {
return fmt.Errorf("fetch beefy authorities at block %v: %w", result.BlockHash, err)
}
currentAuthoritySet, err := li.queryBeefyAuthoritySet(result.BlockHash)
nextAuthoritySet, err := li.queryBeefyNextAuthoritySet(result.BlockHash)
if err != nil {
return fmt.Errorf("fetch beefy authoritie set at block %v: %w", result.BlockHash, err)
return fmt.Errorf("fetch beefy authorities set at block %v: %w", result.BlockHash, err)
}
task := Request{
Validators: validators,
SignedCommitment: result.SignedCommitment,
Proof: result.MMRProof,
}

currentState, err = li.relayer.CurrentState(ctx)
if err != nil {
return fmt.Errorf("get current state from beefy LC %w", err)
}

if validatorSetID > currentValidatorSet {
if currentAuthoritySet.Root == currentState.NextValidatorSetRoot && committedBeefyBlock < lastSyncedBeefyBlock+li.config.UpdatePeriod {
if nextAuthoritySet.Root == currentState.NextValidatorSetRoot && committedBeefyBlock < lastSyncedBeefyBlock+li.config.UpdatePeriod {
logEntry.Info("Discarded commitment with beefy authorities not change")
continue
}
Expand Down Expand Up @@ -162,7 +168,7 @@ func (li *PolkadotListener) queryBeefyAuthorities(blockHash types.Hash) ([]subst

func (li *PolkadotListener) queryBeefyNextAuthoritySet(blockHash types.Hash) (types.BeefyNextAuthoritySet, error) {
var nextAuthoritySet types.BeefyNextAuthoritySet
storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "MmrLeaf", "BeefyNextAuthorities", nil, nil)
storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "BeefyMmrLeaf", "BeefyNextAuthorities", nil, nil)
ok, err := li.conn.API().RPC.State.GetStorage(storageKey, &nextAuthoritySet, blockHash)
if err != nil {
return nextAuthoritySet, err
Expand All @@ -178,7 +184,7 @@ type BeefyAuthoritySet = types.BeefyNextAuthoritySet

func (li *PolkadotListener) queryBeefyAuthoritySet(blockHash types.Hash) (BeefyAuthoritySet, error) {
var authoritySet BeefyAuthoritySet
storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "MmrLeaf", "BeefyAuthorities", nil, nil)
storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "BeefyMmrLeaf", "BeefyAuthorities", nil, nil)
ok, err := li.conn.API().RPC.State.GetStorage(storageKey, &authoritySet, blockHash)
if err != nil {
return authoritySet, err
Expand Down
4 changes: 0 additions & 4 deletions relayer/relays/beefy/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui
sendError(fmt.Errorf("fetch block: %w", err))
return
}
log.WithFields(log.Fields{
"blockNumber": result.BlockNumber,
"depth": result.Depth,
}).Info("fetch block")

var commitment *types.SignedCommitment
for j := range block.Justifications {
Expand Down

0 comments on commit 5eba01b

Please sign in to comment.