gtxbuf

package
v0.0.0-...-37c92ef Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2025 License: Apache-2.0 Imports: 7 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer[S, T any] struct {
	// contains filtered or unexported fields
}

Buffer is a validator-local transaction buffer. The type parameter S is chain state, and type paramter T is a transaction.

Methods on Buffer are safe for concurrent use.

func New

func New[S, T any](
	ctx context.Context,
	log *slog.Logger,
	addTxFunc func(ctx context.Context, state S, tx T) (S, error),
	txDeleterFunc func(ctx context.Context, reject []T) func(tx T) bool,
) *Buffer[S, T]

New returns a new Buffer for the given state and transaction types.

The addTxFunc must return a new copy of the given state, representing the updated state as a result of applying the transaction. During a call to AddTx, errors are returned directly to the caller. During rebase, an error returned by addTxFunc is assumed to be fatal, unless it is wrapped in TxInvalidError.

The txDeleterFunc argument is used to produce a function passed to slices.Delete, so that returned function must report true for transactions that must be removed from the buffer. In most cases, that will involve creating a map of reject values, and returning a function closing over the map, reporting presence of the given transaction in that map.

func (*Buffer[S, T]) AddTx

func (b *Buffer[S, T]) AddTx(ctx context.Context, tx T) error

AddTx attempts to modify b's state by temporarily executing the transaction t, using the addTxFunc passed to New. Any error returned by addTxFunc is returned directly.

func (*Buffer[S, T]) Buffered

func (b *Buffer[S, T]) Buffered(ctx context.Context, dst []T) []T

Buffered returns a copy of the pending transactions in b. The copied values are appended to dst, which may be nil, and the result is returned.

func (*Buffer[S, T]) Initialize

func (b *Buffer[S, T]) Initialize(ctx context.Context, initialState S) (ok bool)

Initialize must be the first method called on a new buffer, to initialize the state. If called a second time, Initialize will panic.

The initial state is not part of New because it is assumed that the buffer is needed to propagate through the driver before the genesis state may be ready.

func (*Buffer[S, T]) Rebase

func (b *Buffer[S, T]) Rebase(
	ctx context.Context, newBase S, applied []T,
) (invalidated []T, err error)

Rebase changes b's root state to newBase, then calls the txDeleterFunc to remove pending transactions that are present in the applied slice. It is important that the addTxFunc uses TxInvalidError to wrap errors for transactions that are no longer valid with the new state; errors not wrapped in TxInvalidError are assumed to be fatal.

func (*Buffer[S, T]) Wait

func (b *Buffer[S, T]) Wait()

Wait blocks until all background work for b is finished. Initiate a clean shutdown by closing the context passed to New.

type TxInvalidError

type TxInvalidError struct {
	Err error
}

TxInvalidError indicates that a transaction failed to apply against the state. For correct Buffer behavior, the addTxFunc argument passed to New must wrap the returned error in TxInvalidError, to indicate that a particular transaction could not be applied to the state. Any other error type is effectively fatal to the Buffer.

func (TxInvalidError) Error

func (e TxInvalidError) Error() string

func (TxInvalidError) Unwrap

func (e TxInvalidError) Unwrap() error

Jump to

Keyboard shortcuts

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