Skip to content

Commit

Permalink
refactor pub rand commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Dec 18, 2024
1 parent e995884 commit d3a44ab
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 224 deletions.
4 changes: 2 additions & 2 deletions finality-provider/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Config struct {
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"`
TimestampingDelayBlocks uint32 `long:"timestampingdelayblocks" description:"The delay, measured in blocks, between a randomness commit submission and the randomness is BTC-timestamped"`
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"`
Expand Down Expand Up @@ -94,7 +94,7 @@ func DefaultConfigWithHome(homePath string) Config {
PollerConfig: &pollerCfg,
NumPubRand: defaultNumPubRand,
NumPubRandMax: defaultNumPubRandMax,
MinRandHeightGap: defaultMinRandHeightGap,
TimestampingDelayBlocks: defaultMinRandHeightGap,
BatchSubmissionSize: defaultBatchSubmissionSize,
StatusUpdateInterval: defaultStatusUpdateInterval,
RandomnessCommitInterval: defaultRandomInterval,
Expand Down
16 changes: 6 additions & 10 deletions finality-provider/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,14 @@ func (app *FinalityProviderApp) SyncAllFinalityProvidersStatus() error {

continue
}
// power == 0 and slashed_height == 0, change to INACTIVE if the current status is ACTIVE
if oldStatus == proto.FinalityProviderStatus_ACTIVE {
app.fps.MustSetFpStatus(fp.BtcPk, proto.FinalityProviderStatus_INACTIVE)

app.logger.Debug(
"the finality-provider status is changed to INACTIVE",
zap.String("fp_btc_pk", pkHex),
zap.String("old_status", oldStatus.String()),
)
app.fps.MustSetFpStatus(fp.BtcPk, proto.FinalityProviderStatus_INACTIVE)

continue
}
app.logger.Debug(
"the finality-provider status is changed to INACTIVE",
zap.String("fp_btc_pk", pkHex),
zap.String("old_status", oldStatus.String()),
)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions finality-provider/service/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package service_test
import (
"errors"
"fmt"
btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"

btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"

"github.com/babylonlabs-io/babylon/testutil/datagen"
bbntypes "github.com/babylonlabs-io/babylon/types"
finalitytypes "github.com/babylonlabs-io/babylon/x/finality/types"
Expand Down Expand Up @@ -254,7 +255,7 @@ func FuzzStatusUpdate(f *testing.F) {
votingPower := uint64(r.Intn(2))
mockClientController.EXPECT().QueryFinalityProviderVotingPower(gomock.Any(), currentHeight).Return(votingPower, nil).AnyTimes()
mockClientController.EXPECT().Close().Return(nil).AnyTimes()
mockClientController.EXPECT().QueryLatestFinalizedBlocks(gomock.Any()).Return(nil, nil).AnyTimes()
mockClientController.EXPECT().QueryLatestFinalizedBlocks(gomock.Any()).Return([]*types.BlockInfo{{Height: randomStartingHeight - 1}}, nil).AnyTimes()
mockClientController.EXPECT().QueryFinalityProviderHighestVotedHeight(gomock.Any()).Return(uint64(0), nil).AnyTimes()
mockClientController.EXPECT().QueryLastCommittedPublicRand(gomock.Any(), uint64(1)).Return(nil, nil).AnyTimes()
mockClientController.EXPECT().SubmitFinalitySig(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&types.TxResponse{TxHash: ""}, nil).AnyTimes()
Expand All @@ -279,7 +280,7 @@ func FuzzStatusUpdate(f *testing.F) {

if votingPower > 0 {
waitForStatus(t, fpIns, proto.FinalityProviderStatus_ACTIVE)
} else if fpIns.GetStatus() == proto.FinalityProviderStatus_ACTIVE {
} else {
waitForStatus(t, fpIns, proto.FinalityProviderStatus_INACTIVE)
}
})
Expand Down
8 changes: 5 additions & 3 deletions finality-provider/service/benchmark_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package service

import (
"fmt"
"github.com/babylonlabs-io/finality-provider/types"
"go.uber.org/zap"
"time"

"go.uber.org/zap"

"github.com/babylonlabs-io/finality-provider/types"
)

// CommitPubRandTiming - helper struct used to capture times for benchmark
Expand All @@ -26,7 +28,7 @@ func (fp *FinalityProviderInstance) HelperCommitPubRand(tipHeight uint64) (*type
case lastCommittedHeight == uint64(0):
// the finality-provider has never submitted public rand before
startHeight = tipHeight + 1
case lastCommittedHeight < uint64(fp.cfg.MinRandHeightGap)+tipHeight:
case lastCommittedHeight < uint64(fp.cfg.TimestampingDelayBlocks)+tipHeight:
// (should not use subtraction because they are in the type of uint64)
// we are running out of the randomness
startHeight = lastCommittedHeight + 1
Expand Down
80 changes: 41 additions & 39 deletions finality-provider/service/chain_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@ func (cp *ChainPoller) blockWithRetry(height uint64) (*types.BlockInfo, error) {
func (cp *ChainPoller) waitForActivation() {
// ensure that the startHeight is no lower than the activated height
for {
activatedHeight, err := cp.cc.QueryActivatedHeight()
if err != nil {
cp.logger.Debug("failed to query the consumer chain for the activated height", zap.Error(err))
} else {
if cp.nextHeight < activatedHeight {
cp.nextHeight = activatedHeight
}
return
}
select {
case <-time.After(cp.cfg.PollInterval):
activatedHeight, err := cp.cc.QueryActivatedHeight()
if err != nil {
cp.logger.Debug("failed to query the consumer chain for the activated height", zap.Error(err))
} else {
if cp.nextHeight < activatedHeight {
cp.nextHeight = activatedHeight
}
return
}

continue
case <-cp.quit:
return
}
Expand All @@ -169,37 +169,39 @@ func (cp *ChainPoller) pollChain() {
var failedCycles uint32

for {
// start polling in the first iteration
blockToRetrieve := cp.nextHeight
block, err := cp.blockWithRetry(blockToRetrieve)
if err != nil {
failedCycles++
cp.logger.Debug(
"failed to query the consumer chain for the block",
zap.Uint32("current_failures", failedCycles),
zap.Uint64("block_to_retrieve", blockToRetrieve),
zap.Error(err),
)
} else {
// no error and we got the header we wanted to get, bump the state and push
// notification about data
cp.nextHeight = blockToRetrieve + 1
failedCycles = 0
cp.metrics.RecordLastPolledHeight(block.Height)

cp.logger.Info("the poller retrieved the block from the consumer chain",
zap.Uint64("height", block.Height))

// push the data to the channel
// Note: if the consumer is too slow -- the buffer is full
// the channel will block, and we will stop retrieving data from the node
cp.blockInfoChan <- block
}

if failedCycles > maxFailedCycles {
cp.logger.Fatal("the poller has reached the max failed cycles, exiting")
}
select {
case <-time.After(cp.cfg.PollInterval):
blockToRetrieve := cp.nextHeight
block, err := cp.blockWithRetry(blockToRetrieve)
if err != nil {
failedCycles++
cp.logger.Debug(
"failed to query the consumer chain for the block",
zap.Uint32("current_failures", failedCycles),
zap.Uint64("block_to_retrieve", blockToRetrieve),
zap.Error(err),
)
} else {
// no error and we got the header we wanted to get, bump the state and push
// notification about data
cp.nextHeight = blockToRetrieve + 1
failedCycles = 0
cp.metrics.RecordLastPolledHeight(block.Height)

cp.logger.Info("the poller retrieved the block from the consumer chain",
zap.Uint64("height", block.Height))

// push the data to the channel
// Note: if the consumer is too slow -- the buffer is full
// the channel will block, and we will stop retrieving data from the node
cp.blockInfoChan <- block
}

if failedCycles > maxFailedCycles {
cp.logger.Fatal("the poller has reached the max failed cycles, exiting")
}
continue
case req := <-cp.skipHeightChan:
// no need to skip heights if the target height is not higher
// than the next height to retrieve
Expand Down
Loading

0 comments on commit d3a44ab

Please sign in to comment.