Skip to content

Commit

Permalink
fix: reduce nonce reference
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Dec 7, 2024
1 parent 7918d95 commit 0312a52
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
3 changes: 1 addition & 2 deletions node/pkg/chain/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func NewChainHelper(ctx context.Context, opts ...ChainHelperOption) (*ChainHelpe
wallet := strings.TrimPrefix(config.ReporterPk, "0x")

nonceManager := noncemanagerv2.New(primaryClient)
go nonceManager.StartAutoRefill(ctx)

delegatorUrl := os.Getenv(EnvDelegatorUrl)

Expand Down Expand Up @@ -219,5 +218,5 @@ func (t *ChainHelper) SubmitDirect(ctx context.Context, contractAddress, functio
}

func (t *ChainHelper) FlushNoncePool(ctx context.Context) error {
return t.noncemanager.Refill(ctx, t.wallet)
return t.noncemanager.Reset(ctx, t.wallet)
}
51 changes: 16 additions & 35 deletions node/pkg/chain/noncemanagerv2/noncemanagerv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"bisonai.com/miko/node/pkg/chain/utils"
"github.com/rs/zerolog/log"
)

type NonceManagerV2 struct {
Expand All @@ -17,8 +16,8 @@ type NonceManagerV2 struct {
}

const (
poolSize = 15 // expect maximum 15 submission per minute
minimumNoncePoolSize = 3
poolSize = 30 // expect maximum 15 submission per minute
minimumNoncePoolSize = 5
poolAutoRefillInterval = time.Minute
)

Expand All @@ -35,25 +34,33 @@ func (m *NonceManagerV2) GetNonce(ctx context.Context, address string) (uint64,

if _, ok := m.noncePool[address]; !ok {
m.noncePool[address] = make(chan uint64, poolSize)
if err := m.unsafeInitPool(ctx, address); err != nil {
return 0, fmt.Errorf("failed to refill nonce pool: %w", err)
}
}

if len(m.noncePool[address]) < minimumNoncePoolSize {
if err := m.unsafeRefill(ctx, address); err != nil {
return 0, fmt.Errorf("failed to refill nonce pool: %w", err)
}
m.unsafeFillPool(address)
}

nonce := <-m.noncePool[address]
return nonce, nil
}

func (m *NonceManagerV2) Refill(ctx context.Context, address string) error {
func (m *NonceManagerV2) Reset(ctx context.Context, address string) error {
m.mu.Lock()
defer m.mu.Unlock()
return m.unsafeRefill(ctx, address)
return m.unsafeInitPool(ctx, address)
}

func (m *NonceManagerV2) unsafeFillPool(address string) {
nonce := <-m.noncePool[address]
for i := 0; i < poolSize-len(m.noncePool[address]); i++ {
m.noncePool[address] <- nonce + uint64(i)
}
}

func (m *NonceManagerV2) unsafeRefill(ctx context.Context, address string) error {
func (m *NonceManagerV2) unsafeInitPool(ctx context.Context, address string) error {
currentNonce, err := utils.GetNonceFromPk(ctx, address, m.client)
if err != nil {
return err
Expand All @@ -73,29 +80,3 @@ func (m *NonceManagerV2) unsafeFlushPool(address string) {
}
}
}

func (m *NonceManagerV2) refillAll(ctx context.Context) error {
m.mu.Lock()
defer m.mu.Unlock()

for address := range m.noncePool {
if err := m.unsafeRefill(ctx, address); err != nil {
return err
}
}
return nil
}

func (m *NonceManagerV2) StartAutoRefill(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-time.After(poolAutoRefillInterval):
err := m.refillAll(ctx)
if err != nil {
log.Error().Err(err).Msg("Failed to refill nonce pool")
}
}
}
}

0 comments on commit 0312a52

Please sign in to comment.