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: benchmark pub randomness commit #190

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 68 additions & 10 deletions finality-provider/service/fp_instance_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package service_test

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

"fmt"
bbntypes "github.com/babylonlabs-io/babylon/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"math/rand"
"os"
"path/filepath"
"testing"

"github.com/babylonlabs-io/babylon/testutil/datagen"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"
Expand All @@ -36,7 +36,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 @@ -60,7 +60,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 @@ -124,7 +124,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 @@ -139,7 +139,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 @@ -152,7 +159,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 @@ -194,3 +201,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")
}
}
})
}
}
Loading