Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Replace fast sync with batch processing #132

Merged
merged 11 commits into from
Nov 20, 2024
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Improvements

* [#132](https://github.com/babylonlabs-io/finality-provider/pull/132) Replace fast sync with batch processing

### Documentation

[#120](https://github.com/babylonlabs-io/finality-provider/pull/120) Spec of
Expand Down
39 changes: 4 additions & 35 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,41 +186,10 @@ func (bc *BabylonController) SubmitFinalitySig(
proof []byte, // TODO: have a type for proof
sig *btcec.ModNScalar,
) (*types.TxResponse, error) {
cmtProof := cmtcrypto.Proof{}
if err := cmtProof.Unmarshal(proof); err != nil {
return nil, err
}

msg := &finalitytypes.MsgAddFinalitySig{
Signer: bc.mustGetTxSigner(),
FpBtcPk: bbntypes.NewBIP340PubKeyFromBTCPK(fpPk),
BlockHeight: block.Height,
PubRand: bbntypes.NewSchnorrPubRandFromFieldVal(pubRand),
Proof: &cmtProof,
BlockAppHash: block.Hash,
FinalitySig: bbntypes.NewSchnorrEOTSSigFromModNScalar(sig),
}

unrecoverableErrs := []*sdkErr.Error{
finalitytypes.ErrInvalidFinalitySig,
finalitytypes.ErrPubRandNotFound,
btcstakingtypes.ErrFpAlreadySlashed,
}

expectedErrs := []*sdkErr.Error{
finalitytypes.ErrDuplicatedFinalitySig,
}

res, err := bc.reliablySendMsg(msg, expectedErrs, unrecoverableErrs)
if err != nil {
return nil, err
}

if res == nil {
return &types.TxResponse{}, nil
}

return &types.TxResponse{TxHash: res.TxHash, Events: res.Events}, nil
return bc.SubmitBatchFinalitySigs(
fpPk, []*types.BlockInfo{block}, []*btcec.FieldVal{pubRand},
[][]byte{proof}, []*btcec.ModNScalar{sig},
)
}

// SubmitBatchFinalitySigs submits a batch of finality signatures to Babylon
Expand Down
105 changes: 51 additions & 54 deletions finality-provider/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@ import (
)

const (
defaultChainType = "babylon"
defaultLogLevel = zapcore.InfoLevel
defaultLogDirname = "logs"
defaultLogFilename = "fpd.log"
defaultFinalityProviderKeyName = "finality-provider"
DefaultRPCPort = 12581
defaultConfigFileName = "fpd.conf"
defaultNumPubRand = 70000 // support running of 1 week with block production time as 10s
defaultNumPubRandMax = 100000
defaultMinRandHeightGap = 35000
defaultStatusUpdateInterval = 20 * time.Second
defaultRandomInterval = 30 * time.Second
defaultSubmitRetryInterval = 1 * time.Second
defaultFastSyncInterval = 10 * time.Second
defaultSyncFpStatusInterval = 30 * time.Second
defaultFastSyncLimit = 10
defaultFastSyncGap = 3
defaultMaxSubmissionRetries = 20
defaultBitcoinNetwork = "signet"
defaultDataDirname = "data"
defaultChainType = "babylon"
defaultLogLevel = zapcore.InfoLevel
defaultLogDirname = "logs"
defaultLogFilename = "fpd.log"
defaultFinalityProviderKeyName = "finality-provider"
DefaultRPCPort = 12581
defaultConfigFileName = "fpd.conf"
defaultNumPubRand = 70000 // support running of 1 week with block production time as 10s
defaultNumPubRandMax = 100000
defaultMinRandHeightGap = 35000
defaultBatchSubmissionSize = 1000
defaultStatusUpdateInterval = 20 * time.Second
defaultRandomInterval = 30 * time.Second
defaultSubmitRetryInterval = 1 * time.Second
defaultSyncFpStatusInterval = 30 * time.Second
defaultSignatureSubmissionInterval = 1 * time.Second
defaultMaxSubmissionRetries = 20
defaultBitcoinNetwork = "signet"
defaultDataDirname = "data"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add a config for batch processing size? what's the current code behavior if it lags way behind?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the fp lags behind, the poller should quickly poll blocks (that's why we should have a short polling interval). Then the fp will do batch submission

)

var (
Expand All @@ -56,19 +55,18 @@ var (
type Config struct {
LogLevel string `long:"loglevel" description:"Logging level for all subsystems" choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"fatal"`
// ChainType and ChainID (if any) of the chain config identify a consumer chain
ChainType string `long:"chaintype" description:"the type of the consumer chain" choice:"babylon"`
NumPubRand uint32 `long:"numPubRand" description:"The number of Schnorr public randomness for each commitment"`
NumPubRandMax uint32 `long:"numpubrandmax" description:"The upper bound of the number of Schnorr public randomness for each commitment"`
MinRandHeightGap uint32 `long:"minrandheightgap" description:"The minimum gap between the last committed rand height and the current Babylon block height"`
StatusUpdateInterval time.Duration `long:"statusupdateinterval" description:"The interval between each update of finality-provider status"`
RandomnessCommitInterval time.Duration `long:"randomnesscommitinterval" description:"The interval between each attempt to commit public randomness"`
SubmissionRetryInterval time.Duration `long:"submissionretryinterval" description:"The interval between each attempt to submit finality signature or public randomness after a failure"`
MaxSubmissionRetries uint32 `long:"maxsubmissionretries" description:"The maximum number of retries to submit finality signature or public randomness"`
FastSyncInterval time.Duration `long:"fastsyncinterval" description:"The interval between each try of fast sync, which is disabled if the value is 0"`
FastSyncLimit uint32 `long:"fastsynclimit" description:"The maximum number of blocks to catch up for each fast sync"`
FastSyncGap uint64 `long:"fastsyncgap" description:"The block gap that will trigger the fast sync"`
EOTSManagerAddress string `long:"eotsmanageraddress" description:"The address of the remote EOTS manager; Empty if the EOTS manager is running locally"`
SyncFpStatusInterval time.Duration `long:"syncfpstatusinterval" description:"The duration of time that it should sync FP status with the client blockchain"`
ChainType string `long:"chaintype" description:"the type of the consumer chain" choice:"babylon"`
NumPubRand uint32 `long:"numPubRand" description:"The number of Schnorr public randomness for each commitment"`
NumPubRandMax uint32 `long:"numpubrandmax" description:"The upper bound of the number of Schnorr public randomness for each commitment"`
MinRandHeightGap uint32 `long:"minrandheightgap" description:"The minimum gap between the last committed rand height and the current Babylon block height"`
MaxSubmissionRetries uint32 `long:"maxsubmissionretries" description:"The maximum number of retries to submit finality signature or public randomness"`
EOTSManagerAddress string `long:"eotsmanageraddress" description:"The address of the remote EOTS manager; Empty if the EOTS manager is running locally"`
BatchSubmissionSize uint32 `long:"batchsubmissionsize" description:"The size of a batch in one submission"`
StatusUpdateInterval time.Duration `long:"statusupdateinterval" description:"The interval between each update of finality-provider status"`
RandomnessCommitInterval time.Duration `long:"randomnesscommitinterval" description:"The interval between each attempt to commit public randomness"`
SubmissionRetryInterval time.Duration `long:"submissionretryinterval" description:"The interval between each attempt to submit finality signature or public randomness after a failure"`
SyncFpStatusInterval time.Duration `long:"syncfpstatusinterval" description:"The duration of time that it should sync FP status with the client blockchain"`
SignatureSubmissionInterval time.Duration `long:"signaturesubmissioninterval" description:"The interval between each finality signature(s) submission"`

BitcoinNetwork string `long:"bitcoinnetwork" description:"Bitcoin network to run on" choise:"mainnet" choice:"regtest" choice:"testnet" choice:"simnet" choice:"signet"`

Expand All @@ -91,27 +89,26 @@ func DefaultConfigWithHome(homePath string) Config {
bbnCfg.KeyDirectory = homePath
pollerCfg := DefaultChainPollerConfig()
cfg := Config{
ChainType: defaultChainType,
LogLevel: defaultLogLevel.String(),
DatabaseConfig: DefaultDBConfigWithHomePath(homePath),
BabylonConfig: &bbnCfg,
PollerConfig: &pollerCfg,
NumPubRand: defaultNumPubRand,
NumPubRandMax: defaultNumPubRandMax,
MinRandHeightGap: defaultMinRandHeightGap,
StatusUpdateInterval: defaultStatusUpdateInterval,
RandomnessCommitInterval: defaultRandomInterval,
SubmissionRetryInterval: defaultSubmitRetryInterval,
FastSyncInterval: defaultFastSyncInterval,
FastSyncLimit: defaultFastSyncLimit,
FastSyncGap: defaultFastSyncGap,
MaxSubmissionRetries: defaultMaxSubmissionRetries,
BitcoinNetwork: defaultBitcoinNetwork,
BTCNetParams: defaultBTCNetParams,
EOTSManagerAddress: defaultEOTSManagerAddress,
RPCListener: DefaultRPCListener,
Metrics: metrics.DefaultFpConfig(),
SyncFpStatusInterval: defaultSyncFpStatusInterval,
ChainType: defaultChainType,
LogLevel: defaultLogLevel.String(),
DatabaseConfig: DefaultDBConfigWithHomePath(homePath),
BabylonConfig: &bbnCfg,
PollerConfig: &pollerCfg,
NumPubRand: defaultNumPubRand,
NumPubRandMax: defaultNumPubRandMax,
MinRandHeightGap: defaultMinRandHeightGap,
BatchSubmissionSize: defaultBatchSubmissionSize,
StatusUpdateInterval: defaultStatusUpdateInterval,
RandomnessCommitInterval: defaultRandomInterval,
SubmissionRetryInterval: defaultSubmitRetryInterval,
SignatureSubmissionInterval: defaultSignatureSubmissionInterval,
MaxSubmissionRetries: defaultMaxSubmissionRetries,
BitcoinNetwork: defaultBitcoinNetwork,
BTCNetParams: defaultBTCNetParams,
EOTSManagerAddress: defaultEOTSManagerAddress,
RPCListener: DefaultRPCListener,
Metrics: metrics.DefaultFpConfig(),
SyncFpStatusInterval: defaultSyncFpStatusInterval,
}

if err := cfg.Validate(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions finality-provider/config/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import "time"

var (
defaultBufferSize = uint32(1000)
defaultPollingInterval = 20 * time.Second
defaultPollingInterval = 1 * time.Second
defaultStaticStartHeight = uint64(1)
)

type ChainPollerConfig struct {
BufferSize uint32 `long:"buffersize" description:"The maximum number of Babylon blocks that can be stored in the buffer"`
PollInterval time.Duration `long:"pollinterval" description:"The interval between each polling of Babylon blocks"`
PollInterval time.Duration `long:"pollinterval" description:"The interval between each polling of blocks; the value should be set depending on the block production time but could be set smaller for quick catching up"`
StaticChainScanningStartHeight uint64 `long:"staticchainscanningstartheight" description:"The static height from which we start polling the chain"`
AutoChainScanningMode bool `long:"autochainscanningmode" description:"Automatically discover the height from which to start polling the chain"`
}
Expand Down
108 changes: 0 additions & 108 deletions finality-provider/service/fastsync.go

This file was deleted.

Loading
Loading