state

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: AGPL-3.0 Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultMaxKeySize   = 16_000      // ~16KB
	DefaultMaxValueSize = 256_000_000 // ~256MB
)

Variables

View Source
var DefaultSpockSecretHasher = func() hash.Hasher {
	return hash.NewSHA3_256()
}

DefaultSpockSecretHasher returns a new SHA3_256 hasher

Functions

This section is empty.

Types

type ExecutionParameters added in v0.37.20

type ExecutionParameters struct {
	meter.MeterParameters
}

type ExecutionState

type ExecutionState struct {
	// contains filtered or unexported fields
}

State represents the execution state it holds draft of updates and captures all register touches

func NewExecutionState

func NewExecutionState(
	snapshot snapshot.StorageSnapshot,
	params StateParameters,
) *ExecutionState

NewExecutionState constructs a new state

func NewExecutionStateWithSpockStateHasher added in v0.33.30

func NewExecutionStateWithSpockStateHasher(
	snapshot snapshot.StorageSnapshot,
	params StateParameters,
	getHasher func() hash.Hasher,
) *ExecutionState

NewExecutionStateWithSpockStateHasher constructs a new state with a custom hasher

func (*ExecutionState) BytesWritten

func (state *ExecutionState) BytesWritten() uint64

BytesWritten returns the amount of total ledger bytes written

func (*ExecutionState) ComputationIntensities

func (state *ExecutionState) ComputationIntensities() meter.MeteredComputationIntensities

ComputationIntensities returns computation intensities

func (*ExecutionState) ComputationRemaining added in v0.38.0

func (state *ExecutionState) ComputationRemaining(kind common.ComputationKind) uint64

ComputationRemaining returns the remaining computation for the given kind.

func (*ExecutionState) DropChanges

func (state *ExecutionState) DropChanges() error

func (*ExecutionState) ExecutionParameters added in v0.37.20

func (state *ExecutionState) ExecutionParameters() ExecutionParameters

func (*ExecutionState) Finalize

func (state *ExecutionState) Finalize() *snapshot.ExecutionSnapshot

func (*ExecutionState) Get

Get returns a register value given owner and key. Storage interaction is only metered (accumulated and limited) when metering is enabled; when metering is disabled the read is neither counted nor limited.

Expected error returns during normal operation:

All other errors are exceptions (ledger failures, use after finalization).

func (*ExecutionState) InteractionUsed

func (state *ExecutionState) InteractionUsed() uint64

InteractionUsed returns the amount of ledger interaction (total ledger byte read + total ledger byte written)

func (*ExecutionState) MemoryAmounts added in v0.42.0

func (state *ExecutionState) MemoryAmounts() meter.MeteredMemoryAmounts

MemoryAmounts returns memory amounts

func (*ExecutionState) Merge

func (state *ExecutionState) Merge(other *snapshot.ExecutionSnapshot) error

Merge applies the changes from the given execution snapshot to this state. The read/write set and SPoCK are always merged. The snapshot's meter (computation, memory, events, and ledger interaction) is merged only when this state's metering is enabled, so charges from a nested transaction are applied to the caller only while the caller is metering. This keeps program cache metering deterministic: a value loaded into the cache is always fully metered (see NewChildForDerivedData), but replaying it (or a fresh load) charges the caller only when metering is enabled (internal issue #7126).

func (*ExecutionState) MeterComputation

func (state *ExecutionState) MeterComputation(usage common.ComputationUsage) error

MeterComputation meters computation usage. It is a no-op if metering is disabled.

Expected error returns during normal operation:

Returns an exception if called on a finalized state.

func (*ExecutionState) MeterEmittedEvent

func (state *ExecutionState) MeterEmittedEvent(byteSize uint64) error

MeterEmittedEvent captures the byte size of an emitted event. It is a no-op if metering is disabled.

Expected error returns during normal operation:

Returns an exception if called on a finalized state.

func (*ExecutionState) MeterMemory

func (state *ExecutionState) MeterMemory(usage common.MemoryUsage) error

MeterMemory meters memory usage. It is a no-op if metering is disabled.

Expected error returns during normal operation:

Returns an exception if called on a finalized state.

func (*ExecutionState) NewChild

func (state *ExecutionState) NewChild() *ExecutionState

NewChild generates a new child state using the parent's meter parameters.

func (*ExecutionState) NewChildForDerivedData added in v0.51.0

func (state *ExecutionState) NewChildForDerivedData() *ExecutionState

NewChildForDerivedData generates a new child state for computing a derived data value (e.g. loading a program into the programs cache). The child meters unconditionally, even when the parent runs with metering disabled.

Unlike NewChild, the child does NOT share the parent's limitsController: it gets a fresh controller with meteringEnabled=true. This is used by the derived data cache so that a value loaded into the cache always carries a fully-populated meter, independent of the caller's metering scope (see internal issue #7126). Whether those charges are applied to the caller is decided when the child is merged back, by Merge gating on the caller's metering.

When the parent's metering is disabled, the child's meter limits are lifted (weights unchanged) so that loads performed in system-critical metering-disabled scopes (e.g. fee deduction) can never fail on a limit, while still recording deterministic intensities.

func (*ExecutionState) NewChildWithMeterParams

func (state *ExecutionState) NewChildWithMeterParams(
	params ExecutionParameters,
) *ExecutionState

NewChildWithMeterParams generates a new child state using the provide meter parameters.

func (ExecutionState) RunWithMeteringDisabled added in v0.43.0

func (controller ExecutionState) RunWithMeteringDisabled(f func())

RunWithMeteringDisabled runs f with metering disabled. While metering is disabled, none of the metered quantities (computation, memory, events, and ledger interaction) are accumulated or limited. The previous metering state is restored afterwards, so nested calls behave correctly.

func (*ExecutionState) Set

func (state *ExecutionState) Set(id flow.RegisterID, value flow.RegisterValue) error

Set updates state delta with a register update. Storage interaction is only metered (accumulated and limited) when metering is enabled; when metering is disabled the write is neither counted nor limited.

Expected error returns during normal operation:

All other errors are exceptions (ledger failures, use after finalization).

func (*ExecutionState) TotalComputationLimit

func (state *ExecutionState) TotalComputationLimit() uint64

TotalComputationLimit returns total computation limit

func (*ExecutionState) TotalComputationUsed

func (state *ExecutionState) TotalComputationUsed() uint64

TotalComputationUsed returns total computation used

func (*ExecutionState) TotalMemoryEstimate

func (state *ExecutionState) TotalMemoryEstimate() uint64

TotalMemoryEstimate returns total memory used

func (*ExecutionState) TotalMemoryLimit

func (state *ExecutionState) TotalMemoryLimit() uint

TotalMemoryLimit returns total memory limit

type Meter

type Meter interface {
	// MeterComputation captures computation usage.
	//
	// Expected error returns during normal operation:
	//   - errors.LimitExceededError with errors.LimitKindComputation if the
	//     computation limit is exceeded
	MeterComputation(usage common.ComputationUsage) error
	ComputationRemaining(kind common.ComputationKind) uint64
	ComputationIntensities() meter.MeteredComputationIntensities
	TotalComputationLimit() uint64
	TotalComputationUsed() uint64

	// MeterMemory captures memory usage.
	//
	// Expected error returns during normal operation:
	//   - errors.LimitExceededError with errors.LimitKindMemory if the memory
	//     limit is exceeded
	MeterMemory(usage common.MemoryUsage) error
	MemoryAmounts() meter.MeteredMemoryAmounts
	TotalMemoryEstimate() uint64

	InteractionUsed() uint64

	// MeterEmittedEvent captures the byte size of an emitted event.
	//
	// Expected error returns during normal operation:
	//   - errors.LimitExceededError with errors.LimitKindEvent if the event
	//     byte size limit is exceeded
	MeterEmittedEvent(byteSize uint64) error

	// RunWithMeteringDisabled runs f with metering disabled.
	// While metering is disabled, usage of all metered quantities - computation,
	// memory, emitted events, and ledger interaction - is neither accumulated
	// toward the totals nor subject to its limit.
	// This function can be used to run a function that fits one of these cases:
	// - the function should not fail due to metering limits
	// - the function is not invokable by the user and has a constant execution time (e.g. fee deduction)
	// - the function is metered once before calling `RunWithMeteringDisabled` with a special weight (e.g. create account)
	// 		and doesn't need additional metering inside the function
	RunWithMeteringDisabled(f func())
}

type NestedTransactionId

type NestedTransactionId struct {
	// contains filtered or unexported fields
}

Opaque identifier used for Restarting nested transactions

func (NestedTransactionId) StateForTestingOnly

func (id NestedTransactionId) StateForTestingOnly() *ExecutionState

type NestedTransactionPreparer

type NestedTransactionPreparer interface {
	Meter

	// ExecutionParameters returns the execution parameters
	ExecutionParameters() ExecutionParameters

	// NumNestedTransactions returns the number of uncommitted nested
	// transactions.  Note that the main transaction is not considered a
	// nested transaction.
	NumNestedTransactions() int

	// IsParseRestricted returns true if the current nested transaction is in
	// parse resticted access mode.
	IsParseRestricted() bool

	MainTransactionId() NestedTransactionId

	// IsCurrent returns true if the provide id refers to the current (nested)
	// transaction.
	IsCurrent(id NestedTransactionId) bool

	// InterimReadSet returns the current read set aggregated from all
	// outstanding nested transactions.
	InterimReadSet() map[flow.RegisterID]struct{}

	// FinalizeMainTransaction finalizes the main transaction and returns
	// its execution snapshot.  The finalized main transaction will not accept
	// any new commits after this point.  This returns an error if there are
	// outstanding nested transactions.
	FinalizeMainTransaction() (*snapshot.ExecutionSnapshot, error)

	// BeginNestedTransaction creates a unrestricted nested transaction within
	// the current unrestricted (nested) transaction.  The meter parameters are
	// inherited from the current transaction.  This returns error if the
	// current nested transaction is program restricted.
	BeginNestedTransaction() (
		NestedTransactionId,
		error,
	)

	// BeginNestedTransactionForDerivedData creates a unrestricted nested
	// transaction for computing a derived data value (e.g. loading a program
	// into the programs cache). Its state meters unconditionally, so the
	// cached snapshot's meter is deterministic regardless of the caller's
	// metering scope; see ExecutionState.NewChildForDerivedData (internal
	// issue #7126). This returns error if the current nested transaction is
	// program restricted.
	BeginNestedTransactionForDerivedData() (
		NestedTransactionId,
		error,
	)

	// BeginNestedTransactionWithMeterParams creates a unrestricted nested
	// transaction within the current unrestricted (nested) transaction, using
	// the provided meter parameters. This returns error if the current nested
	// transaction is program restricted.
	BeginNestedTransactionWithMeterParams(
		params ExecutionParameters,
	) (
		NestedTransactionId,
		error,
	)

	// BeginParseRestrictedNestedTransaction creates a restricted nested
	// transaction within the current (nested) transaction.  The meter
	// parameters are inherited from the current transaction.
	BeginParseRestrictedNestedTransaction(
		location common.AddressLocation,
	) (
		NestedTransactionId,
		error,
	)

	// CommitNestedTransaction commits the changes in the current unrestricted
	// nested transaction to the parent (nested) transaction.  This returns
	// error if the expectedId does not match the current nested transaction.
	// This returns the committed execution snapshot otherwise.
	//
	// Note: The returned committed execution snapshot may be reused by another
	// transaction via AttachAndCommitNestedTransaction to update the
	// transaction bookkeeping, but the caller must manually invalidate the
	// state.
	// USE WITH EXTREME CAUTION.
	CommitNestedTransaction(
		expectedId NestedTransactionId,
	) (
		*snapshot.ExecutionSnapshot,
		error,
	)

	// CommitParseRestrictedNestedTransaction commits the changes in the
	// current restricted nested transaction to the parent (nested)
	// transaction.  This returns error if the specified location does not
	// match the tracked location. This returns the committed execution
	// snapshot otherwise.
	//
	// Note: The returned committed execution snapshot may be reused by another
	// transaction via AttachAndCommitNestedTransaction to update the
	// transaction bookkeeping, but the caller must manually invalidate the
	// state.
	// USE WITH EXTREME CAUTION.
	CommitParseRestrictedNestedTransaction(
		location common.AddressLocation,
	) (
		*snapshot.ExecutionSnapshot,
		error,
	)

	// AttachAndCommitNestedTransaction commits the changes from the cached
	// nested transaction execution snapshot to the current (nested)
	// transaction.
	AttachAndCommitNestedTransaction(
		cachedSnapshot *snapshot.ExecutionSnapshot,
	) error

	// RestartNestedTransaction merges all changes that belongs to the nested
	// transaction about to be restart (for spock/meter bookkeeping), then
	// wipes its view changes.
	RestartNestedTransaction(
		id NestedTransactionId,
	) error

	// Get returns a register value from the current (nested) transaction's
	// view. Limits are only enforced when metering is enabled.
	//
	// Expected error returns during normal operation:
	//   - errors.StateKeySizeLimitError if the key exceeds the key size limit
	//   - errors.LimitExceededError with errors.LimitKindLedgerInteraction if
	//     the storage interaction limit is exceeded
	//
	// All other errors are exceptions (e.g. ledger failures).
	Get(id flow.RegisterID) (flow.RegisterValue, error)

	// Set updates a register value in the current (nested) transaction's
	// view. Limits are only enforced when metering is enabled.
	//
	// Expected error returns during normal operation:
	//   - errors.StateKeySizeLimitError or errors.StateValueSizeLimitError if
	//     the key or value exceeds its size limit
	//   - errors.LimitExceededError with errors.LimitKindLedgerInteraction if
	//     the storage interaction limit is exceeded
	//
	// All other errors are exceptions (e.g. ledger failures).
	Set(id flow.RegisterID, value flow.RegisterValue) error

	// BaseStorageSnapshot gives access to the storage snapshot as it was without changes
	// WARNING: this should not be read mid-transaction as reads to it are not recorded in the spocks
	BaseStorageSnapshot() snapshot.StorageSnapshot
}

NestedTransactionPreparer provides active transaction states and facilitates common state management operations.

func NewTransactionState

func NewTransactionState(
	snapshot snapshot.StorageSnapshot,
	params StateParameters,
) NestedTransactionPreparer

NewTransactionState constructs a new state transaction which manages nested transactions.

func NewTransactionStateFromExecutionState added in v0.33.30

func NewTransactionStateFromExecutionState(
	startState *ExecutionState,
) NestedTransactionPreparer

NewTransactionStateFromExecutionState constructs a new state transaction directly from an execution state.

type StateParameters

type StateParameters struct {
	meter.MeterParameters
	// contains filtered or unexported fields
}

func DefaultParameters

func DefaultParameters() StateParameters

func (StateParameters) WithMaxKeySizeAllowed

func (params StateParameters) WithMaxKeySizeAllowed(
	limit uint64,
) StateParameters

WithMaxKeySizeAllowed sets limit on max key size

func (StateParameters) WithMaxValueSizeAllowed

func (params StateParameters) WithMaxValueSizeAllowed(
	limit uint64,
) StateParameters

WithMaxValueSizeAllowed sets limit on max value size

func (StateParameters) WithMeterParameters

func (params StateParameters) WithMeterParameters(
	meterParams meter.MeterParameters,
) StateParameters

WithMeterParameters sets the state's meter parameters

Jump to

Keyboard shortcuts

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