From 27cb93df2f8a274758a86a2d18f745e7d98591c1 Mon Sep 17 00:00:00 2001 From: Lazar Date: Fri, 22 Nov 2024 10:47:56 +0100 Subject: [PATCH] better fp distribution with stakers --- container/container.go | 8 ++++---- harness/app.go | 5 ++++- harness/btcstaker.go | 15 +++++++++++++-- harness/finalityprovider.go | 29 ++++++++++++++++++++++++++--- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/container/container.go b/container/container.go index eb626a2..f1ead10 100644 --- a/container/container.go +++ b/container/container.go @@ -224,8 +224,8 @@ func (m *Manager) RunBabylondResource( "--covenant-quorum=1 --covenant-pks=%s "+ "--min-signed-per-window=0 && "+ // never jail sluggish fps "chmod -R 777 /home && "+ - "sed -i -e 's/iavl-cache-size = 781250/iavl-cache-size = 50000/' /home/node0/babylond/config/app.toml && "+ // disable the cache otherwise we go OOM - //"sed -i -e 's/iavl-disable-fastnode = false/iavl-disable-fastnode = true/' /home/node0/babylond/config/app.toml && "+ + "sed -i -e 's/iavl-cache-size = 781250/iavl-cache-size = 0/' /home/node0/babylond/config/app.toml && "+ // disable the cache otherwise we go OOM + "sed -i -e 's/iavl-disable-fastnode = false/iavl-disable-fastnode = true/' /home/node0/babylond/config/app.toml && "+ `sed -i -e 's/timeout_commit = "5s"/timeout_commit = "2s"/' /home/node0/babylond/config/config.toml &&`+ "babylond start --home=/home/node0/babylond --rpc.pprof_laddr=0.0.0.0:6060", epochInterval, slashingPkScript, baseHeaderHex, bbn.NewBIP340PubKeyFromBTCPK(CovenantPubKey).MarshalHex()), @@ -279,8 +279,8 @@ func (m *Manager) RunBabylondResource( cmd2 := []string{ "sh", "-c", fmt.Sprintf( "chmod -R 777 /home && ls -la &&" + - "sed -i -e 's/iavl-cache-size = 781250/iavl-cache-size = 50000/' /home/node1/babylond/config/app.toml && " + // disable the cache otherwise we go OOM - //"sed -i -e 's/iavl-disable-fastnode = false/iavl-disable-fastnode = true/' /home/node1/babylond/config/app.toml && " + + "sed -i -e 's/iavl-cache-size = 781250/iavl-cache-size = 0/' /home/node1/babylond/config/app.toml && " + // disable the cache otherwise we go OOM + "sed -i -e 's/iavl-disable-fastnode = false/iavl-disable-fastnode = true/' /home/node1/babylond/config/app.toml && " + `sed -i -e 's/timeout_commit = "5s"/timeout_commit = "2s"/' /home/node1/babylond/config/config.toml &&` + "babylond start --home=/home/node1/babylond --rpc.pprof_laddr=0.0.0.0:6060", ), diff --git a/harness/app.go b/harness/app.go index c983891..09c2d60 100644 --- a/harness/app.go +++ b/harness/app.go @@ -88,7 +88,10 @@ func startHarness(cmdCtx context.Context, cfg config.Config) error { if err != nil { return err } - stakers = append(stakers, NewBTCStaker(tm, stakerSender, fpMgr.randomFp().btcPk.MustToBTCPK(), tm.fundingRequests, tm.fundingResponse)) + + rndFpChunk := fpMgr.getRandomChunk(3) + + stakers = append(stakers, NewBTCStaker(tm, stakerSender, rndFpChunk, tm.fundingRequests, tm.fundingResponse)) } // periodically check if we need to fund the staker diff --git a/harness/btcstaker.go b/harness/btcstaker.go index 205ae14..eb873b1 100644 --- a/harness/btcstaker.go +++ b/harness/btcstaker.go @@ -32,6 +32,7 @@ type BTCStaker struct { tm *TestManager client *SenderWithBabylonClient fpPK *btcec.PublicKey + fpPKChunk []*btcec.PublicKey fundingRequest chan sdk.AccAddress fundingResponse chan sdk.AccAddress } @@ -39,14 +40,14 @@ type BTCStaker struct { func NewBTCStaker( tm *TestManager, client *SenderWithBabylonClient, - finalityProviderPublicKey *btcec.PublicKey, + finalityProvidersPublicKey []*btcec.PublicKey, fundingRequest chan sdk.AccAddress, fundingResponse chan sdk.AccAddress, ) *BTCStaker { return &BTCStaker{ tm: tm, client: client, - fpPK: finalityProviderPublicKey, + fpPKChunk: finalityProvidersPublicKey, fundingRequest: fundingRequest, fundingResponse: fundingResponse, } @@ -89,6 +90,9 @@ func (s *BTCStaker) runForever(ctx context.Context, stakerAddress btcutil.Addres continue } + // each round rnd FP to delegate + s.fpPK = s.randomFpPK() + if err = s.buildAndSendStakingTransaction(ctx, stakerAddress, stakerPk, ¶msResp.Params); err != nil { fmt.Printf("🚫 Err in BTC Staker (%s), err: %v\n", s.client.BabylonAddress.String(), err) if strings.Contains(err.Error(), "insufficient funds") { @@ -584,3 +588,10 @@ func bbnPksToBtcPks(pks []bbn.BIP340PubKey) ([]*btcec.PublicKey, error) { } return btcPks, nil } + +func (s *BTCStaker) randomFpPK() *btcec.PublicKey { + r.Seed(time.Now().UnixNano()) + randomIndex := r.Intn(len(s.fpPKChunk)) + + return s.fpPKChunk[randomIndex] +} diff --git a/harness/finalityprovider.go b/harness/finalityprovider.go index 2828ce0..801350c 100644 --- a/harness/finalityprovider.go +++ b/harness/finalityprovider.go @@ -469,10 +469,32 @@ func (fpi *FinalityProviderInstance) hasVotingPower(ctx context.Context, b *Bloc return true, nil } -func (fpm *FinalityProviderManager) randomFp() *FinalityProviderInstance { - randomIndex := r.Intn(len(fpm.finalityProviders)) +func (fpm *FinalityProviderManager) getRandomChunk(chunkSize int) []*btcec.PublicKey { + if len(fpm.finalityProviders) == 0 { + return []*btcec.PublicKey{} + } + + if chunkSize >= len(fpm.finalityProviders) || len(fpm.finalityProviders) == 1 { + return fpiToBtcPks(fpm.finalityProviders) + } + + r.Seed(time.Now().UnixNano()) + + startIndex := r.Intn(len(fpm.finalityProviders) - chunkSize + 1) + + chunk := fpm.finalityProviders[startIndex : startIndex+chunkSize] + + return fpiToBtcPks(chunk) +} + +func fpiToBtcPks(fpi []*FinalityProviderInstance) []*btcec.PublicKey { + fpPKs := make([]*btcec.PublicKey, 0, len(fpi)) + + for _, fp := range fpi { + fpPKs = append(fpPKs, fp.btcPk.MustToBTCPK()) + } - return fpm.finalityProviders[randomIndex] + return fpPKs } func (fpm *FinalityProviderManager) queryFinalizedBlockForever(ctx context.Context) { @@ -582,6 +604,7 @@ func (fpi *FinalityProviderInstance) submitFinalitySigForever(ctx context.Contex } if !hasVp { + time.Sleep(500 * time.Millisecond) continue }