noncemanager

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package noncemanager provides interfaces and implementations for managing transaction nonces in a blockchain environment. It supports both SQL and Redis-backed storage implementations with concurrent request limiting and atomic nonce allocation.

Index

Constants

View Source
const BestGuessConcurrency = 32

BestGuessConcurrency is the recommended default concurrency limit for most use cases

View Source
const MaxConcurrentRequests = 64

MaxConcurrentRequests defines the maximum number of concurrent nonce requests allowed. The blockchain mempool can usually only hold 64 transactions from the same fromAddress.

Variables

This section is empty.

Functions

This section is empty.

Types

type NonceContext

type NonceContext struct {
	// Nonce is the allocated nonce value
	Nonce big.Int
	// Cancel releases the nonce reservation, making it available for reuse
	Cancel func()
	// Consume commits the nonce, marking it as permanently used
	Consume func() error
}

NonceContext represents a reserved nonce with associated lifecycle management functions

type NonceManager

type NonceManager interface {
	// GetNonce atomically reserves and returns the next available nonce.
	// The returned NonceContext allows the caller to either consume or cancel the nonce.
	// This operation may block if the maximum concurrent request limit is reached.
	GetNonce(ctx context.Context) (*NonceContext, error)

	// FastForwardNonce sets the nonce sequence to start from the given value,
	// abandoning all nonces below it. This is typically used when recovering
	// from blockchain state inconsistencies.
	FastForwardNonce(ctx context.Context, nonce big.Int) error

	// Replenish ensures a sufficient number of nonces are available starting
	// from the given nonce value. This is used to pre-populate the nonce pool.
	Replenish(ctx context.Context, nonce big.Int) error
}

NonceManager defines the interface for managing transaction nonces. Implementations must provide thread-safe operations for nonce allocation, replenishment, and fast-forwarding.

type OpenConnectionsLimiter

type OpenConnectionsLimiter struct {
	Semaphore chan struct{}
	WG        sync.WaitGroup
}

OpenConnectionsLimiter controls the number of concurrent nonce allocation requests to prevent overwhelming the blockchain mempool with too many transactions from the same address.

func NewOpenConnectionsLimiter

func NewOpenConnectionsLimiter(maxConcurrent int) *OpenConnectionsLimiter

NewOpenConnectionsLimiter creates a new limiter with the specified maximum concurrent requests. If maxConcurrent exceeds MaxConcurrentRequests, it will be capped at that value.

Directories

Path Synopsis
Package redis provides a Redis-backed implementation of the NonceManager interface.
Package redis provides a Redis-backed implementation of the NonceManager interface.
Package sql provides a SQL database-backed implementation of the NonceManager interface.
Package sql provides a SQL database-backed implementation of the NonceManager interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL