Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Fix for beefy (paritytech#863)
Browse files Browse the repository at this point in the history
* For sno-516

* Fix beefy relayer

* Revert change

* Improve scan beefy commitments

* Block waiting handover commit & Error handing

* Allow update in nextValidatorSet without handover

* Fix resolve comments

* Some polish
  • Loading branch information
yrong authored Jun 21, 2023
1 parent eba4c95 commit 3f08d9b
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ node_modules/
.direnv
.envrc
parachain/build_rs_cov.profraw
compiler_config.json
2 changes: 1 addition & 1 deletion core/packages/contracts/scripts/configure-beefy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function createMerkleTree(leaves: Buffer[]) {
const leafHashes = leaves.map((value) => hasher(value))
const tree = new MerkleTree(leafHashes, hasher, {
sortLeaves: false,
sortPairs: true,
sortPairs: false,
})
return tree
}
Expand Down
2 changes: 1 addition & 1 deletion core/packages/contracts/src/BeefyClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ contract BeefyClient is Ownable {
revert PrevRandaoNotCaptured();
}

if (commitment.validatorSetID != currentValidatorSet.id) {
if (commitment.validatorSetID != currentValidatorSet.id && commitment.validatorSetID != nextValidatorSet.id) {
revert InvalidCommitment();
}

Expand Down
1 change: 0 additions & 1 deletion core/packages/test/config/beefy-relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"endpoint": "ws://127.0.0.1:8546",
"gas-limit": null
},
"start-block": 0,
"descendants-until-final": 3,
"contracts": {
"BeefyClient": null
Expand Down
11 changes: 7 additions & 4 deletions relayer/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
"version": "0.2.0",
"configurations": [
{
"name": "Start Relayer",
"name": "Start Beefy Relayer",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": [
"run",
"beefy",
"--config",
"/tmp/snowbridge-e2e-config/config.toml"
"/tmp/snowbridge/beefy-relay.json",
"--ethereum.private-key",
"0x935b65c833ced92c43ef9de6bff30703d941bd92a2637cb00cfad389f5862109"
],
"env": {
"SNOWBRIDGE_BEEFY_KEY": "0x935b65c833ced92c43ef9de6bff30703d941bd92a2637cb00cfad389f5862109",
"SNOWBRIDGE_MESSAGE_KEY": "0x8013383de6e5a891e7754ae1ef5a21e7661f1fe67cd47ca8ebf4acd6de66879a",
"SNOWBRIDGE_PARACHAIN_KEY": "//Relay",
"SNOWBRIDGE_RELAYCHAIN_KEY": "//Alice",
},
"SNOWBRIDGE_RELAYCHAIN_KEY": "//Alice"
}
}
]
}
1 change: 0 additions & 1 deletion relayer/chain/relaychain/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func (conn *Connection) GetMMRRootHash(blockHash types.Hash) (types.Hash, error)
func (co *Connection) GenerateProofForBlock(
blockNumber uint64,
latestBeefyBlockHash types.Hash,
beefyActivationBlock uint64,
) (types.GenerateMMRProofResponse, error) {
log.WithFields(log.Fields{
"blockNumber": blockNumber,
Expand Down
9 changes: 4 additions & 5 deletions relayer/cmd/parachain_head_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ func ParachainHeadProofFn(cmd *cobra.Command, _ []string) error {
copy(beefyBlockHash[:], beefyBlockHashHex[0:32])

relayChainBlock, _ := cmd.Flags().GetUint64("relaychain-block")
mmrProof, err := conn.GenerateProofForBlock(relayChainBlock, beefyBlockHash, 0)
mmrProof, err := conn.GenerateProofForBlock(relayChainBlock, beefyBlockHash)
if err != nil {
log.WithError(err).Error("Cannot connect.")
return err
}
log.WithFields(log.Fields{
"relayChainBlock": relayChainBlock,
"beefyBlockHash": beefyBlockHash,
"beefyActivationBlock": 0,
"mmrProof": mmrProof,
"relayChainBlock": relayChainBlock,
"beefyBlockHash": beefyBlockHash,
"mmrProof": mmrProof,
}).Info("conn.GenerateProofForBlock")

paraID, _ := cmd.Flags().GetUint32("parachain-id")
Expand Down
2 changes: 1 addition & 1 deletion relayer/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/snowfork/snowbridge/relayer

go 1.18
go 1.20

require (
github.com/cbroglie/mustache v1.4.0
Expand Down
6 changes: 2 additions & 4 deletions relayer/relays/beefy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ type Config struct {
}

type SourceConfig struct {
Polkadot config.PolkadotConfig `mapstructure:"polkadot"`
// Block number when Beefy was activated
BeefyActivationBlock uint64 `mapstructure:"beefy-activation-block"`
FastForwardDepth uint64 `mapstructure:"fast-forward-depth"`
Polkadot config.PolkadotConfig `mapstructure:"polkadot"`
FastForwardDepth uint64 `mapstructure:"fast-forward-depth"`
}

type SinkConfig struct {
Expand Down
63 changes: 31 additions & 32 deletions relayer/relays/beefy/polkadot-listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (li *PolkadotListener) Start(
}
li.beefyAuthoritiesKey = storageKey

requests := make(chan Request)
requests := make(chan Request, 1)

eg.Go(func() error {
defer close(requests)
Expand All @@ -61,7 +61,7 @@ func (li *PolkadotListener) scanCommitments(
currentValidatorSet uint64,
requests chan<- Request,
) error {
in, err := ScanSafeCommitments(ctx, li.conn.Metadata(), li.conn.API(), currentBeefyBlock+1, li.config.Source.BeefyActivationBlock)
in, err := ScanSafeCommitments(ctx, li.conn.Metadata(), li.conn.API(), currentBeefyBlock+1)
if err != nil {
return fmt.Errorf("scan commitments: %w", err)
}
Expand All @@ -78,18 +78,28 @@ func (li *PolkadotListener) scanCommitments(
return fmt.Errorf("scan safe commitments: %w", result.Error)
}

if result.SignedCommitment.Commitment.ValidatorSetID == currentValidatorSet+1 {
// Workaround for https://github.com/paritytech/polkadot/pull/6577
if uint64(result.MMRProof.Leaf.BeefyNextAuthoritySet.ID) != result.SignedCommitment.Commitment.ValidatorSetID+1 {
log.WithFields(log.Fields{
"commitment": log.Fields{
"blockNumber": result.SignedCommitment.Commitment.BlockNumber,
"validatorSetID": result.SignedCommitment.Commitment.ValidatorSetID,
},
}).Info("Discarded invalid handover commitment with BeefyNextAuthoritySet not change")
continue
}
currentValidatorSet++
committedBeefyBlock := result.SignedCommitment.Commitment.BlockNumber
validatorSetID := result.SignedCommitment.Commitment.ValidatorSetID
nextValidatorSetID := uint64(result.MMRProof.Leaf.BeefyNextAuthoritySet.ID)

logEntry := log.WithFields(log.Fields{
"commitment": log.Fields{
"blockNumber": committedBeefyBlock,
"validatorSetID": validatorSetID,
"nextValidatorSetID": nextValidatorSetID,
},
"validatorSetID": currentValidatorSet,
})
if validatorSetID < currentValidatorSet || validatorSetID > currentValidatorSet+1 {
return fmt.Errorf("commitment has unexpected validatorSetID: blockNumber=%v validatorSetID=%v expectedValidatorSetID=%v",
committedBeefyBlock,
validatorSetID,
currentValidatorSet,
)
}

if validatorSetID == currentValidatorSet+1 && validatorSetID == nextValidatorSetID-1 {

validators, err := li.queryBeefyAuthorities(result.BlockHash)
if err != nil {
return fmt.Errorf("fetch beefy authorities at block %v: %w", result.BlockHash, err)
Expand All @@ -106,15 +116,12 @@ func (li *PolkadotListener) scanCommitments(
case <-ctx.Done():
return ctx.Err()
case requests <- task:
logEntry.Info("New commitment with handover added to channel")
currentValidatorSet++
}
} else if result.SignedCommitment.Commitment.ValidatorSetID == currentValidatorSet {
} else if (validatorSetID == currentValidatorSet || validatorSetID == currentValidatorSet+1) && validatorSetID == nextValidatorSetID {
if result.Depth > li.config.Source.FastForwardDepth {
log.WithFields(log.Fields{
"commitment": log.Fields{
"blockNumber": result.SignedCommitment.Commitment.BlockNumber,
"validatorSetID": result.SignedCommitment.Commitment.ValidatorSetID,
},
}).Warn("Discarded commitment with depth not fast forward")
logEntry.Warn("Discarded commitment with depth not fast forward")
continue
}

Expand All @@ -133,20 +140,12 @@ func (li *PolkadotListener) scanCommitments(
// drop task if it can't be processed immediately
select {
case requests <- task:
logEntry.Info("New commitment added to channel")
default:
log.WithFields(log.Fields{
"commitment": log.Fields{
"blockNumber": result.SignedCommitment.Commitment.BlockNumber,
"validatorSetID": result.SignedCommitment.Commitment.ValidatorSetID,
},
}).Info("Discarded commitment")
logEntry.Warn("Discarded commitment fail adding to channel")
}
} else {
return fmt.Errorf("commitment has unexpected validatorSetID: blockNumber=%v validatorSetID=%v expectedValidatorSetID=%v",
result.SignedCommitment.Commitment.BlockNumber,
result.SignedCommitment.Commitment.ValidatorSetID,
currentValidatorSet,
)
logEntry.Warn("Discarded invalid commitment")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions relayer/relays/beefy/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ type ScanSafeCommitmentsResult struct {
Error error
}

func ScanSafeCommitments(ctx context.Context, meta *types.Metadata, api *gsrpc.SubstrateAPI, startBlock uint64, beefyActivationBlock uint64) (<-chan ScanSafeCommitmentsResult, error) {
func ScanSafeCommitments(ctx context.Context, meta *types.Metadata, api *gsrpc.SubstrateAPI, startBlock uint64) (<-chan ScanSafeCommitmentsResult, error) {
out := make(chan ScanSafeCommitmentsResult)
go scanSafeCommitments(ctx, meta, api, startBlock, beefyActivationBlock, out)
go scanSafeCommitments(ctx, meta, api, startBlock, out)
return out, nil
}

func scanSafeCommitments(ctx context.Context, meta *types.Metadata, api *gsrpc.SubstrateAPI, startBlock uint64, beefyActivationBlock uint64, out chan<- ScanSafeCommitmentsResult) {
func scanSafeCommitments(ctx context.Context, meta *types.Metadata, api *gsrpc.SubstrateAPI, startBlock uint64, out chan<- ScanSafeCommitmentsResult) {
defer close(out)

sendError := func(err error) {
Expand Down
1 change: 0 additions & 1 deletion relayer/relays/parachain/beefy-listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ func (li *BeefyListener) generateProof(ctx context.Context, input *ProofInput, h
mmrProof, err := li.relaychainConn.GenerateProofForBlock(
input.RelayBlockNumber+1,
latestBeefyBlockHash,
li.config.BeefyActivationBlock,
)
if err != nil {
return nil, fmt.Errorf("generate MMR leaf proof: %w", err)
Expand Down
4 changes: 1 addition & 3 deletions relayer/relays/parachain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ type SourceConfig struct {
Parachain config.ParachainConfig `mapstructure:"parachain"`
Ethereum config.EthereumConfig `mapstructure:"ethereum"`
Contracts SourceContractsConfig `mapstructure:"contracts"`
// Block number when Beefy was activated
BeefyActivationBlock uint64 `mapstructure:"beefy-activation-block"`
LaneID uint32 `mapstructure:"lane-id"`
LaneID uint32 `mapstructure:"lane-id"`
}

type SourceContractsConfig struct {
Expand Down

0 comments on commit 3f08d9b

Please sign in to comment.