Skip to content

Commit

Permalink
Merge branch 'main' into gai/combine-register-finality-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Dec 5, 2024
2 parents 6ca0564 + 7d7f4f8 commit 57dd537
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [#182](https://github.com/babylonlabs-io/finality-provider/pull/182) Remove fp manager
* [#184](https://github.com/babylonlabs-io/finality-provider/pull/184) eots manager sign record store
* [#189](https://github.com/babylonlabs-io/finality-provider/pull/189) Remove `fpd register-finality-provider` cmd
* [#190](https://github.com/babylonlabs-io/finality-provider/pull/190) Benchmark pub rand

### Bug Fixes

Expand Down
74 changes: 66 additions & 8 deletions finality-provider/service/fp_instance_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package service_test

import (
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"

"github.com/babylonlabs-io/babylon/testutil/datagen"
bbntypes "github.com/babylonlabs-io/babylon/types"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/babylonlabs-io/babylon/testutil/datagen"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"

"github.com/babylonlabs-io/finality-provider/clientcontroller"
"github.com/babylonlabs-io/finality-provider/eotsmanager"
eotscfg "github.com/babylonlabs-io/finality-provider/eotsmanager/config"
Expand All @@ -37,7 +37,7 @@ func FuzzCommitPubRandList(f *testing.F) {
mockClientController := testutil.PrepareMockedClientController(t, r, randomStartingHeight, currentHeight, 0)
mockClientController.EXPECT().QueryFinalityProviderVotingPower(gomock.Any(), gomock.Any()).
Return(uint64(0), nil).AnyTimes()
_, fpIns, cleanUp := startFinalityProviderAppWithRegisteredFp(t, r, mockClientController, true, randomStartingHeight)
_, fpIns, cleanUp := startFinalityProviderAppWithRegisteredFp(t, r, mockClientController, true, randomStartingHeight, testutil.TestPubRandNum)
defer cleanUp()

expectedTxHash := testutil.GenRandomHexStr(r, 32)
Expand All @@ -61,7 +61,7 @@ func FuzzSubmitFinalitySigs(f *testing.F) {
startingBlock := &types.BlockInfo{Height: randomStartingHeight, Hash: testutil.GenRandomByteArray(r, 32)}
mockClientController := testutil.PrepareMockedClientController(t, r, randomStartingHeight, currentHeight, 0)
mockClientController.EXPECT().QueryLatestFinalizedBlocks(gomock.Any()).Return(nil, nil).AnyTimes()
_, fpIns, cleanUp := startFinalityProviderAppWithRegisteredFp(t, r, mockClientController, true, randomStartingHeight)
_, fpIns, cleanUp := startFinalityProviderAppWithRegisteredFp(t, r, mockClientController, true, randomStartingHeight, testutil.TestPubRandNum)
defer cleanUp()

// commit pub rand
Expand Down Expand Up @@ -125,7 +125,7 @@ func FuzzDetermineStartHeight(f *testing.F) {
}}
mockClientController.EXPECT().QueryLatestFinalizedBlocks(uint64(1)).Return(finalizedBlocks, nil).AnyTimes()

_, fpIns, cleanUp := startFinalityProviderAppWithRegisteredFp(t, r, mockClientController, false, randomStartingHeight)
_, fpIns, cleanUp := startFinalityProviderAppWithRegisteredFp(t, r, mockClientController, false, randomStartingHeight, testutil.TestPubRandNum)
defer cleanUp()
fpIns.MustUpdateStateAfterFinalitySigSubmission(lastVotedHeight)

Expand All @@ -140,7 +140,14 @@ func FuzzDetermineStartHeight(f *testing.F) {
})
}

func startFinalityProviderAppWithRegisteredFp(t *testing.T, r *rand.Rand, cc clientcontroller.ClientController, isStaticStartHeight bool, startingHeight uint64) (*service.FinalityProviderApp, *service.FinalityProviderInstance, func()) {
func startFinalityProviderAppWithRegisteredFp(
t *testing.T,
r *rand.Rand,
cc clientcontroller.ClientController,
isStaticStartHeight bool,
startingHeight uint64,
numPubRand uint32,
) (*service.FinalityProviderApp, *service.FinalityProviderInstance, func()) {
logger := zap.NewNop()
// create an EOTS manager
eotsHomeDir := filepath.Join(t.TempDir(), "eots-home")
Expand All @@ -153,7 +160,7 @@ func startFinalityProviderAppWithRegisteredFp(t *testing.T, r *rand.Rand, cc cli
// create finality-provider app with randomized config
fpHomeDir := filepath.Join(t.TempDir(), "fp-home")
fpCfg := config.DefaultConfigWithHome(fpHomeDir)
fpCfg.NumPubRand = testutil.TestPubRandNum
fpCfg.NumPubRand = numPubRand
fpCfg.PollerConfig.AutoChainScanningMode = !isStaticStartHeight
fpCfg.PollerConfig.StaticChainScanningStartHeight = startingHeight
db, err := fpCfg.DatabaseConfig.GetDBBackend()
Expand Down Expand Up @@ -214,3 +221,54 @@ func startFinalityProviderAppWithRegisteredFp(t *testing.T, r *rand.Rand, cc cli

return app, fpIns, cleanUp
}

func setupBenchmarkEnvironment(t *testing.T, seed int64, numPubRand uint32) (*types.BlockInfo, *service.FinalityProviderInstance, func()) {
r := rand.New(rand.NewSource(seed))

randomStartingHeight := uint64(r.Int63n(100) + 1)
currentHeight := randomStartingHeight + uint64(r.Int63n(10)+2)
startingBlock := &types.BlockInfo{
Height: randomStartingHeight,
Hash: testutil.GenRandomByteArray(r, 32),
}

// Mock client controller setup
mockClientController := testutil.PrepareMockedClientController(t, r, randomStartingHeight, currentHeight, 0)
mockClientController.EXPECT().QueryFinalityProviderVotingPower(gomock.Any(), gomock.Any()).
Return(uint64(0), nil).AnyTimes()

// Set up finality provider app
_, fpIns, cleanUp := startFinalityProviderAppWithRegisteredFp(t, r, mockClientController, true, randomStartingHeight, numPubRand)

// Configure additional mocks
expectedTxHash := testutil.GenRandomHexStr(r, 32)
mockClientController.EXPECT().
CommitPubRandList(fpIns.GetBtcPk(), startingBlock.Height+1, gomock.Any(), gomock.Any(), gomock.Any()).
Return(&types.TxResponse{TxHash: expectedTxHash}, nil).AnyTimes()
mockClientController.EXPECT().QueryLastCommittedPublicRand(gomock.Any(), uint64(1)).Return(nil, nil).AnyTimes()

return startingBlock, fpIns, cleanUp
}
func BenchmarkCommitPubRand(b *testing.B) {
for _, numPubRand := range []uint32{10, 50, 100, 200, 500, 1000, 5000, 10000, 25000, 50000, 75000, 100000} {
b.Run(fmt.Sprintf("numPubRand=%d", numPubRand), func(b *testing.B) {
t := &testing.T{}
startingBlock, fpIns, cleanUp := setupBenchmarkEnvironment(t, 42, numPubRand)
defer cleanUp()

// exclude setup time
b.ResetTimer()

for i := 0; i < b.N; i++ {
res, err := fpIns.CommitPubRand(startingBlock.Height)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}

if res == nil {
b.Fatalf("unexpected result")
}
}
})
}
}

0 comments on commit 57dd537

Please sign in to comment.