Skip to content

Commit

Permalink
Rate limit beefy updates (#1098)
Browse files Browse the repository at this point in the history
* Rate limit beefy updates

* Improve log

* Set session length to 2 mins

* Comments

* Update relayer/relays/beefy/config.go

Co-authored-by: Vincent Geddes <[email protected]>

* Update relayer/relays/beefy/config.go

Co-authored-by: Vincent Geddes <[email protected]>

* Set to zero for local setup

---------

Co-authored-by: Vincent Geddes <[email protected]>
  • Loading branch information
yrong and vgeddes authored Jan 18, 2024
1 parent 2710a25 commit 28e4f09
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
7 changes: 5 additions & 2 deletions relayer/relays/beefy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ type Config struct {
}

type SourceConfig struct {
Polkadot config.PolkadotConfig `mapstructure:"polkadot"`
FastForwardDepth uint64 `mapstructure:"fast-forward-depth"`
Polkadot config.PolkadotConfig `mapstructure:"polkadot"`
// Depth to ignore the beefy updates too far away (in number of blocks)
FastForwardDepth uint64 `mapstructure:"fast-forward-depth"`
// Period to sample the beefy updates (in number of blocks)
UpdatePeriod uint64 `mapstructure:"update-period"`
}

type SinkConfig struct {
Expand Down
6 changes: 5 additions & 1 deletion relayer/relays/beefy/ethereum-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ func (wr *EthereumWriter) submit(ctx context.Context, task Request) error {
return err
}

log.WithFields(logrus.Fields{"tx": tx.Hash().Hex(), "blockNumber": task.SignedCommitment.Commitment.BlockNumber}).Debug("Transaction SubmitFinal succeeded")
log.WithFields(logrus.Fields{
"tx": tx.Hash().Hex(),
"blockNumber": task.SignedCommitment.Commitment.BlockNumber,
"IsHandover": task.IsHandover,
}).Debug("Transaction SubmitFinal succeeded")

return nil

Expand Down
14 changes: 11 additions & 3 deletions relayer/relays/beefy/polkadot-listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (li *PolkadotListener) scanCommitments(
if err != nil {
return fmt.Errorf("scan commitments: %w", err)
}
lastSyncedBeefyBlock := currentBeefyBlock

for {
select {
Expand All @@ -78,7 +79,7 @@ func (li *PolkadotListener) scanCommitments(
return fmt.Errorf("scan safe commitments: %w", result.Error)
}

committedBeefyBlock := result.SignedCommitment.Commitment.BlockNumber
committedBeefyBlock := uint64(result.SignedCommitment.Commitment.BlockNumber)
validatorSetID := result.SignedCommitment.Commitment.ValidatorSetID
nextValidatorSetID := uint64(result.MMRProof.Leaf.BeefyNextAuthoritySet.ID)

Expand All @@ -96,8 +97,9 @@ func (li *PolkadotListener) scanCommitments(
"validatorSetID": validatorSetID,
"nextValidatorSetID": nextValidatorSetID,
},
"validatorSetID": currentValidatorSet,
"IsHandover": validatorSetID == currentValidatorSet+1,
"validatorSetID": currentValidatorSet,
"IsHandover": validatorSetID == currentValidatorSet+1,
"lastSyncedBeefyBlock": lastSyncedBeefyBlock,
})

validators, err := li.queryBeefyAuthorities(result.BlockHash)
Expand All @@ -118,18 +120,24 @@ func (li *PolkadotListener) scanCommitments(
case requests <- task:
logEntry.Info("New commitment with handover added to channel")
currentValidatorSet++
lastSyncedBeefyBlock = committedBeefyBlock
}
} else if validatorSetID == currentValidatorSet {
if result.Depth > li.config.FastForwardDepth {
logEntry.Warn("Discarded commitment with depth not fast forward")
continue
}
if committedBeefyBlock < lastSyncedBeefyBlock+li.config.UpdatePeriod {
logEntry.Info("Discarded commitment with sampling")
continue
}

// drop task if it can't be processed immediately
select {
case <-ctx.Done():
return ctx.Err()
case requests <- task:
lastSyncedBeefyBlock = committedBeefyBlock
logEntry.Info("New commitment added to channel")
default:
logEntry.Warn("Discarded commitment fail adding to channel")
Expand Down
3 changes: 2 additions & 1 deletion web/packages/test/config/beefy-relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"endpoint": "ws://127.0.0.1:9944"
},
"beefy-activation-block": 0,
"fast-forward-depth": 20
"fast-forward-depth": 20,
"update-period": 0
},
"sink": {
"ethereum": {
Expand Down
3 changes: 2 additions & 1 deletion web/packages/test/scripts/build-binary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ 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"
cargo build --release --locked --bin polkadot --bin polkadot-execute-worker --bin polkadot-prepare-worker
# Increase session length to 2 mins
ROCOCO_EPOCH_DURATION=20 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
Expand Down

0 comments on commit 28e4f09

Please sign in to comment.