From ea906067c2e10f1d1e68738883a4675a02f355d0 Mon Sep 17 00:00:00 2001 From: Alistair Singh Date: Wed, 31 Jan 2024 14:51:25 +0200 Subject: [PATCH 1/6] identify and reject non mandatory commitments --- relayer/relays/beefy/polkadot-listener.go | 6 +++ relayer/relays/beefy/scanner.go | 47 +++++++++++++++-------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/relayer/relays/beefy/polkadot-listener.go b/relayer/relays/beefy/polkadot-listener.go index 96cc45fc4..62865c207 100644 --- a/relayer/relays/beefy/polkadot-listener.go +++ b/relayer/relays/beefy/polkadot-listener.go @@ -99,6 +99,7 @@ func (li *PolkadotListener) scanCommitments( }, "validatorSetID": currentValidatorSet, "IsHandover": validatorSetID == currentValidatorSet+1, + "Mandatory": result.MandatoryCommitment, "lastSyncedBeefyBlock": lastSyncedBeefyBlock, }) @@ -112,6 +113,11 @@ func (li *PolkadotListener) scanCommitments( Proof: result.MMRProof, } + if !result.MandatoryCommitment { + logEntry.Info("Skipped due to not mandatory commitment.") + continue + } + if validatorSetID == currentValidatorSet+1 && validatorSetID == nextValidatorSetID-1 { task.IsHandover = true select { diff --git a/relayer/relays/beefy/scanner.go b/relayer/relays/beefy/scanner.go index 3188812f9..2c797eab2 100644 --- a/relayer/relays/beefy/scanner.go +++ b/relayer/relays/beefy/scanner.go @@ -13,10 +13,11 @@ import ( ) type ScanBlocksResult struct { - BlockNumber uint64 - BlockHash types.Hash - Depth uint64 - Error error + BlockNumber uint64 + BlockHash types.Hash + Depth uint64 + HasAuthorityChangeDigest bool + Error error } func ScanBlocks(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64) (chan ScanBlocksResult, error) { @@ -50,6 +51,16 @@ func scanBlocks(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64, return } + hasAuthorityChangeDigest := false + for _, it := range finalizedHeader.Digest { + if it.IsConsensus { + consensus := it.AsConsensus + if consensus.ConsensusEngineID == types.ConsensusEngineID(types.U32(0x46454542)) && consensus.Bytes[0] == 0x01 { + hasAuthorityChangeDigest = true + } + } + } + finalizedBlockNumber := uint64(finalizedHeader.Number) if current > finalizedBlockNumber { select { @@ -69,7 +80,7 @@ func scanBlocks(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64, select { case <-ctx.Done(): return - case out <- ScanBlocksResult{BlockNumber: current, BlockHash: blockHash, Depth: finalizedBlockNumber - current}: + case out <- ScanBlocksResult{BlockNumber: current, BlockHash: blockHash, Depth: finalizedBlockNumber - current, HasAuthorityChangeDigest: hasAuthorityChangeDigest}: } current++ @@ -77,11 +88,12 @@ func scanBlocks(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64, } type ScanCommitmentsResult struct { - SignedCommitment types.SignedCommitment - BlockNumber uint64 - BlockHash types.Hash - Depth uint64 - Error error + SignedCommitment types.SignedCommitment + BlockNumber uint64 + BlockHash types.Hash + Depth uint64 + MandatoryCommitment bool + Error error } func ScanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64) (<-chan ScanCommitmentsResult, error) { @@ -156,18 +168,19 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui select { case <-ctx.Done(): return - case out <- ScanCommitmentsResult{BlockNumber: result.BlockNumber, BlockHash: result.BlockHash, SignedCommitment: *commitment, Depth: result.Depth}: + case out <- ScanCommitmentsResult{BlockNumber: result.BlockNumber, BlockHash: result.BlockHash, SignedCommitment: *commitment, Depth: result.Depth, MandatoryCommitment: result.HasAuthorityChangeDigest}: } } } } type ScanSafeCommitmentsResult struct { - SignedCommitment types.SignedCommitment - MMRProof merkle.SimplifiedMMRProof - BlockHash types.Hash - Depth uint64 - Error error + SignedCommitment types.SignedCommitment + MMRProof merkle.SimplifiedMMRProof + BlockHash types.Hash + Depth uint64 + MandatoryCommitment bool + Error error } func ScanSafeCommitments(ctx context.Context, meta *types.Metadata, api *gsrpc.SubstrateAPI, startBlock uint64) (<-chan ScanSafeCommitmentsResult, error) { @@ -231,7 +244,7 @@ func scanSafeCommitments(ctx context.Context, meta *types.Metadata, api *gsrpc.S select { case <-ctx.Done(): return - case out <- ScanSafeCommitmentsResult{result.SignedCommitment, proof, blockHash, result.Depth, nil}: + case out <- ScanSafeCommitmentsResult{result.SignedCommitment, proof, blockHash, result.Depth, result.MandatoryCommitment, nil}: } } From aecb56a787f1c2a9fcd03c3e971d2aa7cdc8fab8 Mon Sep 17 00:00:00 2001 From: Alistair Singh Date: Wed, 31 Jan 2024 15:16:45 +0200 Subject: [PATCH 2/6] check the right block --- relayer/relays/beefy/scanner.go | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/relayer/relays/beefy/scanner.go b/relayer/relays/beefy/scanner.go index 2c797eab2..8e6543fdc 100644 --- a/relayer/relays/beefy/scanner.go +++ b/relayer/relays/beefy/scanner.go @@ -13,11 +13,10 @@ import ( ) type ScanBlocksResult struct { - BlockNumber uint64 - BlockHash types.Hash - Depth uint64 - HasAuthorityChangeDigest bool - Error error + BlockNumber uint64 + BlockHash types.Hash + Depth uint64 + Error error } func ScanBlocks(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64) (chan ScanBlocksResult, error) { @@ -51,16 +50,6 @@ func scanBlocks(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64, return } - hasAuthorityChangeDigest := false - for _, it := range finalizedHeader.Digest { - if it.IsConsensus { - consensus := it.AsConsensus - if consensus.ConsensusEngineID == types.ConsensusEngineID(types.U32(0x46454542)) && consensus.Bytes[0] == 0x01 { - hasAuthorityChangeDigest = true - } - } - } - finalizedBlockNumber := uint64(finalizedHeader.Number) if current > finalizedBlockNumber { select { @@ -80,7 +69,7 @@ func scanBlocks(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64, select { case <-ctx.Done(): return - case out <- ScanBlocksResult{BlockNumber: current, BlockHash: blockHash, Depth: finalizedBlockNumber - current, HasAuthorityChangeDigest: hasAuthorityChangeDigest}: + case out <- ScanBlocksResult{BlockNumber: current, BlockHash: blockHash, Depth: finalizedBlockNumber - current}: } current++ @@ -141,6 +130,16 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui return } + mandatoryCommitment := false + for _, it := range block.Block.Header.Digest { + if it.IsConsensus { + consensus := it.AsConsensus + if consensus.ConsensusEngineID == types.ConsensusEngineID(types.U32(0x46454542)) && consensus.Bytes[0] == 0x01 { + mandatoryCommitment = true + } + } + } + var commitment *types.SignedCommitment for j := range block.Justifications { sc := types.OptionalSignedCommitment{} @@ -168,7 +167,7 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui select { case <-ctx.Done(): return - case out <- ScanCommitmentsResult{BlockNumber: result.BlockNumber, BlockHash: result.BlockHash, SignedCommitment: *commitment, Depth: result.Depth, MandatoryCommitment: result.HasAuthorityChangeDigest}: + case out <- ScanCommitmentsResult{BlockNumber: result.BlockNumber, BlockHash: result.BlockHash, SignedCommitment: *commitment, Depth: result.Depth, MandatoryCommitment: mandatoryCommitment}: } } } From c22d4e0969eae058bb826f03ba30ea44f6bb09da Mon Sep 17 00:00:00 2001 From: Alistair Singh Date: Thu, 1 Feb 2024 00:47:43 +0200 Subject: [PATCH 3/6] add mandator flag --- relayer/relays/beefy/scanner.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/relayer/relays/beefy/scanner.go b/relayer/relays/beefy/scanner.go index 8e6543fdc..766d0106e 100644 --- a/relayer/relays/beefy/scanner.go +++ b/relayer/relays/beefy/scanner.go @@ -2,6 +2,7 @@ package beefy import ( "context" + "encoding/binary" "fmt" "time" @@ -131,12 +132,17 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui } mandatoryCommitment := false - for _, it := range block.Block.Header.Digest { - if it.IsConsensus { - consensus := it.AsConsensus - if consensus.ConsensusEngineID == types.ConsensusEngineID(types.U32(0x46454542)) && consensus.Bytes[0] == 0x01 { - mandatoryCommitment = true - } + for _, digest := range block.Block.Header.Digest { + if !digest.IsConsensus { + continue + } + consensus := digest.AsConsensus + // Mandatory commitments contain a BEEF digest of type ConsensusLog::AuthoritiesChange (0x01) + // which signifies the the change of session authorities. + // https://github.com/paritytech/polkadot-sdk/blob/6a168ad57ad13ea0896f7684120f4fa15bfef474/substrate/primitives/consensus/beefy/src/lib.rs#L254C2-L254C19 + if decodeEngineId(uint32(consensus.ConsensusEngineID)) == "BEEF" && consensus.Bytes[0] == 0x01 { + mandatoryCommitment = true + break } } @@ -305,3 +311,9 @@ func verifyProof(meta *types.Metadata, api *gsrpc.SubstrateAPI, proof merkle.Sim return actualRoot == expectedRoot, nil } + +func decodeEngineId(engineId uint32) string { + idAsBytes := make([]byte, 4) + binary.LittleEndian.PutUint32(idAsBytes, engineId) + return string(idAsBytes) +} From 1e0c7e7076689c85446302a8a1762e7a0efea62b Mon Sep 17 00:00:00 2001 From: Alistair Singh Date: Thu, 1 Feb 2024 00:52:24 +0200 Subject: [PATCH 4/6] remove skip check --- relayer/relays/beefy/polkadot-listener.go | 5 ----- relayer/relays/beefy/scanner.go | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/relayer/relays/beefy/polkadot-listener.go b/relayer/relays/beefy/polkadot-listener.go index 62865c207..d78ae2ac0 100644 --- a/relayer/relays/beefy/polkadot-listener.go +++ b/relayer/relays/beefy/polkadot-listener.go @@ -113,11 +113,6 @@ func (li *PolkadotListener) scanCommitments( Proof: result.MMRProof, } - if !result.MandatoryCommitment { - logEntry.Info("Skipped due to not mandatory commitment.") - continue - } - if validatorSetID == currentValidatorSet+1 && validatorSetID == nextValidatorSetID-1 { task.IsHandover = true select { diff --git a/relayer/relays/beefy/scanner.go b/relayer/relays/beefy/scanner.go index 766d0106e..309f7bb44 100644 --- a/relayer/relays/beefy/scanner.go +++ b/relayer/relays/beefy/scanner.go @@ -78,12 +78,12 @@ func scanBlocks(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64, } type ScanCommitmentsResult struct { - SignedCommitment types.SignedCommitment - BlockNumber uint64 - BlockHash types.Hash - Depth uint64 - MandatoryCommitment bool - Error error + SignedCommitment types.SignedCommitment + BlockNumber uint64 + BlockHash types.Hash + Depth uint64 + IsMandatory bool + Error error } func ScanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock uint64) (<-chan ScanCommitmentsResult, error) { @@ -131,7 +131,7 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui return } - mandatoryCommitment := false + isMandatory := false for _, digest := range block.Block.Header.Digest { if !digest.IsConsensus { continue @@ -141,7 +141,7 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui // which signifies the the change of session authorities. // https://github.com/paritytech/polkadot-sdk/blob/6a168ad57ad13ea0896f7684120f4fa15bfef474/substrate/primitives/consensus/beefy/src/lib.rs#L254C2-L254C19 if decodeEngineId(uint32(consensus.ConsensusEngineID)) == "BEEF" && consensus.Bytes[0] == 0x01 { - mandatoryCommitment = true + isMandatory = true break } } @@ -173,7 +173,7 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui select { case <-ctx.Done(): return - case out <- ScanCommitmentsResult{BlockNumber: result.BlockNumber, BlockHash: result.BlockHash, SignedCommitment: *commitment, Depth: result.Depth, MandatoryCommitment: mandatoryCommitment}: + case out <- ScanCommitmentsResult{BlockNumber: result.BlockNumber, BlockHash: result.BlockHash, SignedCommitment: *commitment, Depth: result.Depth, IsMandatory: isMandatory}: } } } @@ -249,7 +249,7 @@ func scanSafeCommitments(ctx context.Context, meta *types.Metadata, api *gsrpc.S select { case <-ctx.Done(): return - case out <- ScanSafeCommitmentsResult{result.SignedCommitment, proof, blockHash, result.Depth, result.MandatoryCommitment, nil}: + case out <- ScanSafeCommitmentsResult{result.SignedCommitment, proof, blockHash, result.Depth, result.IsMandatory, nil}: } } From 89326d0895de54356c1f1574fe9860b25e843c17 Mon Sep 17 00:00:00 2001 From: Alistair Singh Date: Thu, 1 Feb 2024 00:53:23 +0200 Subject: [PATCH 5/6] remove checks --- relayer/relays/beefy/polkadot-listener.go | 2 +- relayer/relays/beefy/scanner.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/relayer/relays/beefy/polkadot-listener.go b/relayer/relays/beefy/polkadot-listener.go index d78ae2ac0..0dbf11d81 100644 --- a/relayer/relays/beefy/polkadot-listener.go +++ b/relayer/relays/beefy/polkadot-listener.go @@ -99,7 +99,7 @@ func (li *PolkadotListener) scanCommitments( }, "validatorSetID": currentValidatorSet, "IsHandover": validatorSetID == currentValidatorSet+1, - "Mandatory": result.MandatoryCommitment, + "IsMandatory": result.IsMandatory, "lastSyncedBeefyBlock": lastSyncedBeefyBlock, }) diff --git a/relayer/relays/beefy/scanner.go b/relayer/relays/beefy/scanner.go index 309f7bb44..d53e10e51 100644 --- a/relayer/relays/beefy/scanner.go +++ b/relayer/relays/beefy/scanner.go @@ -180,12 +180,12 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui } type ScanSafeCommitmentsResult struct { - SignedCommitment types.SignedCommitment - MMRProof merkle.SimplifiedMMRProof - BlockHash types.Hash - Depth uint64 - MandatoryCommitment bool - Error error + SignedCommitment types.SignedCommitment + MMRProof merkle.SimplifiedMMRProof + BlockHash types.Hash + Depth uint64 + IsMandatory bool + Error error } func ScanSafeCommitments(ctx context.Context, meta *types.Metadata, api *gsrpc.SubstrateAPI, startBlock uint64) (<-chan ScanSafeCommitmentsResult, error) { From 64408a5bf00aa9636203ddb04bc1a37983ffcd09 Mon Sep 17 00:00:00 2001 From: ron Date: Sun, 4 Feb 2024 17:49:56 +0800 Subject: [PATCH 6/6] Skip mandatory commitment --- relayer/cmd/scan_beefy.go | 6 ++- relayer/relays/beefy/main.go | 31 ++++++++++---- relayer/relays/beefy/parameters.go | 8 ++++ relayer/relays/beefy/polkadot-listener.go | 52 ++++++++++++++--------- web/packages/test/config/beefy-relay.json | 2 +- web/packages/test/scripts/build-binary.sh | 3 +- 6 files changed, 70 insertions(+), 32 deletions(-) diff --git a/relayer/cmd/scan_beefy.go b/relayer/cmd/scan_beefy.go index 32c9ad456..7ee6f4efd 100644 --- a/relayer/cmd/scan_beefy.go +++ b/relayer/cmd/scan_beefy.go @@ -62,7 +62,11 @@ func ScanBeefyFn(cmd *cobra.Command, _ []string) error { "validator-set-id": validatorSetID, }).Info("Connected to relaychain.") - commitments, err := polkadotListener.Start(ctx, eg, beefyBlock, validatorSetID) + var currentState beefy.BeefyState + currentState.CurrentValidatorSetId = validatorSetID + currentState.LatestBeefyBlock = beefyBlock + + commitments, err := polkadotListener.Start(ctx, eg, currentState) if err != nil { logrus.WithError(err).Fatalf("could not start") } diff --git a/relayer/relays/beefy/main.go b/relayer/relays/beefy/main.go index af7a6b9fa..b0c759068 100644 --- a/relayer/relays/beefy/main.go +++ b/relayer/relays/beefy/main.go @@ -57,16 +57,15 @@ func (relay *Relay) Start(ctx context.Context, eg *errgroup.Group) error { return fmt.Errorf("create ethereum connection: %w", err) } - initialBeefyBlock, initialValidatorSetID, err := relay.getInitialState(ctx) + currentState, err := relay.getCurrentState(ctx) if err != nil { return fmt.Errorf("fetch BeefyClient current state: %w", err) } log.WithFields(log.Fields{ - "beefyBlock": initialBeefyBlock, - "validatorSetID": initialValidatorSetID, + "currentState": currentState, }).Info("Retrieved current BeefyClient state") - requests, err := relay.polkadotListener.Start(ctx, eg, initialBeefyBlock, initialValidatorSetID) + requests, err := relay.polkadotListener.Start(ctx, eg, currentState) if err != nil { return fmt.Errorf("initialize polkadot listener: %w", err) } @@ -79,11 +78,12 @@ func (relay *Relay) Start(ctx context.Context, eg *errgroup.Group) error { return nil } -func (relay *Relay) getInitialState(ctx context.Context) (uint64, uint64, error) { +func (relay *Relay) getCurrentState(ctx context.Context) (BeefyState, error) { + var currentState BeefyState address := common.HexToAddress(relay.config.Sink.Contracts.BeefyClient) beefyClient, err := contracts.NewBeefyClient(address, relay.ethereumConn.Client()) if err != nil { - return 0, 0, err + return currentState, err } callOpts := bind.CallOpts{ @@ -92,13 +92,26 @@ func (relay *Relay) getInitialState(ctx context.Context) (uint64, uint64, error) latestBeefyBlock, err := beefyClient.LatestBeefyBlock(&callOpts) if err != nil { - return 0, 0, err + return currentState, err } currentValidatorSet, err := beefyClient.CurrentValidatorSet(&callOpts) if err != nil { - return 0, 0, err + return currentState, err } - return latestBeefyBlock, currentValidatorSet.Id.Uint64(), nil + nextValidatorSet, err := beefyClient.NextValidatorSet(&callOpts) + if err != nil { + return currentState, err + } + + currentState = BeefyState{ + LatestBeefyBlock: latestBeefyBlock, + CurrentValidatorSetId: currentValidatorSet.Id.Uint64(), + CurrentValidatorSetRoot: currentValidatorSet.Root, + NextValidatorSetId: nextValidatorSet.Id.Uint64(), + NextValidatorSetRoot: nextValidatorSet.Root, + } + + return currentState, nil } diff --git a/relayer/relays/beefy/parameters.go b/relayer/relays/beefy/parameters.go index 94e9ba5aa..ab8330ab4 100644 --- a/relayer/relays/beefy/parameters.go +++ b/relayer/relays/beefy/parameters.go @@ -236,3 +236,11 @@ func proofToLog(proof contracts.BeefyClientValidatorProof) logrus.Fields { "Proof": hexProof, } } + +type BeefyState struct { + LatestBeefyBlock uint64 + CurrentValidatorSetId uint64 + CurrentValidatorSetRoot [32]byte + NextValidatorSetId uint64 + NextValidatorSetRoot [32]byte +} diff --git a/relayer/relays/beefy/polkadot-listener.go b/relayer/relays/beefy/polkadot-listener.go index 0dbf11d81..1a1628374 100644 --- a/relayer/relays/beefy/polkadot-listener.go +++ b/relayer/relays/beefy/polkadot-listener.go @@ -32,8 +32,7 @@ func NewPolkadotListener( func (li *PolkadotListener) Start( ctx context.Context, eg *errgroup.Group, - currentBeefyBlock uint64, - currentValidatorSetID uint64, + currentState BeefyState, ) (<-chan Request, error) { storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "Beefy", "Authorities", nil, nil) if err != nil { @@ -45,7 +44,7 @@ func (li *PolkadotListener) Start( eg.Go(func() error { defer close(requests) - err := li.scanCommitments(ctx, currentBeefyBlock, currentValidatorSetID, requests) + err := li.scanCommitments(ctx, currentState, requests) if err != nil { return err } @@ -57,16 +56,15 @@ func (li *PolkadotListener) Start( func (li *PolkadotListener) scanCommitments( ctx context.Context, - currentBeefyBlock uint64, - currentValidatorSet uint64, + currentState BeefyState, requests chan<- Request, ) error { - in, err := ScanSafeCommitments(ctx, li.conn.Metadata(), li.conn.API(), currentBeefyBlock+1) + lastSyncedBeefyBlock := currentState.LatestBeefyBlock + currentValidatorSet := currentState.CurrentValidatorSetId + in, err := ScanSafeCommitments(ctx, li.conn.Metadata(), li.conn.API(), lastSyncedBeefyBlock+1) if err != nil { return fmt.Errorf("scan commitments: %w", err) } - lastSyncedBeefyBlock := currentBeefyBlock - for { select { case <-ctx.Done(): @@ -83,14 +81,6 @@ func (li *PolkadotListener) scanCommitments( validatorSetID := result.SignedCommitment.Commitment.ValidatorSetID nextValidatorSetID := uint64(result.MMRProof.Leaf.BeefyNextAuthoritySet.ID) - if validatorSetID != currentValidatorSet && validatorSetID != currentValidatorSet+1 { - return fmt.Errorf("commitment has unexpected validatorSetID: blockNumber=%v validatorSetID=%v expectedValidatorSetID=%v", - committedBeefyBlock, - validatorSetID, - currentValidatorSet, - ) - } - logEntry := log.WithFields(log.Fields{ "commitment": log.Fields{ "blockNumber": committedBeefyBlock, @@ -98,7 +88,7 @@ func (li *PolkadotListener) scanCommitments( "nextValidatorSetID": nextValidatorSetID, }, "validatorSetID": currentValidatorSet, - "IsHandover": validatorSetID == currentValidatorSet+1, + "IsHandover": validatorSetID > currentValidatorSet, "IsMandatory": result.IsMandatory, "lastSyncedBeefyBlock": lastSyncedBeefyBlock, }) @@ -107,20 +97,28 @@ 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) + if err != nil { + return fmt.Errorf("fetch beefy authoritie set at block %v: %w", result.BlockHash, err) + } task := Request{ Validators: validators, SignedCommitment: result.SignedCommitment, Proof: result.MMRProof, } - if validatorSetID == currentValidatorSet+1 && validatorSetID == nextValidatorSetID-1 { + if validatorSetID > currentValidatorSet { + if currentAuthoritySet.Root == currentState.NextValidatorSetRoot && committedBeefyBlock < lastSyncedBeefyBlock+li.config.UpdatePeriod { + logEntry.Info("Discarded commitment with beefy authorities not change") + continue + } task.IsHandover = true select { case <-ctx.Done(): return ctx.Err() case requests <- task: logEntry.Info("New commitment with handover added to channel") - currentValidatorSet++ + currentValidatorSet = validatorSetID lastSyncedBeefyBlock = committedBeefyBlock } } else if validatorSetID == currentValidatorSet { @@ -176,3 +174,19 @@ func (li *PolkadotListener) queryBeefyNextAuthoritySet(blockHash types.Hash) (ty return nextAuthoritySet, nil } + +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) + ok, err := li.conn.API().RPC.State.GetStorage(storageKey, &authoritySet, blockHash) + if err != nil { + return authoritySet, err + } + if !ok { + return authoritySet, fmt.Errorf("beefy AuthoritySet not found") + } + + return authoritySet, nil +} diff --git a/web/packages/test/config/beefy-relay.json b/web/packages/test/config/beefy-relay.json index 119df296e..c2f492950 100644 --- a/web/packages/test/config/beefy-relay.json +++ b/web/packages/test/config/beefy-relay.json @@ -5,7 +5,7 @@ }, "beefy-activation-block": 0, "fast-forward-depth": 20, - "update-period": 0 + "update-period": 20 }, "sink": { "ethereum": { diff --git a/web/packages/test/scripts/build-binary.sh b/web/packages/test/scripts/build-binary.sh index 7bd383fc8..ef28b2256 100755 --- a/web/packages/test/scripts/build-binary.sh +++ b/web/packages/test/scripts/build-binary.sh @@ -17,8 +17,7 @@ build_binaries() { # Check that all 3 binaries are available and no changes made in the polkadot and substrate dirs if [[ ! -e "target/release/polkadot" || ! -e "target/release/polkadot-execute-worker" || ! -e "target/release/polkadot-prepare-worker" || "$changes_detected" -eq 1 ]]; then echo "Building polkadot binary, due to changes detected in polkadot or substrate, or binaries not found" - # Increase session length to 2 mins - ROCOCO_EPOCH_DURATION=20 cargo build --release --locked --bin polkadot --bin polkadot-execute-worker --bin polkadot-prepare-worker + cargo build --release --locked --bin polkadot --bin polkadot-execute-worker --bin polkadot-prepare-worker else echo "No changes detected in polkadot or substrate and binaries are available, not rebuilding relaychain binaries." fi