state

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	ClassTrieHeight           = 251
	ContractTrieHeight        = 251
	ContractStorageTrieHeight = 251
)

Variables

View Source
var (
	ErrContractAlreadyDeployed    = errors.New("contract already deployed")
	ErrNoHistoryValue             = errors.New("no history value found")
	ErrHistoricalTrieNotSupported = errors.New("cannot support historical trie")
)
View Source
var (
	SystemContract1Address = felt.One[felt.Address]() // block-hash storage contract
	// https://community.starknet.io/t/starknet-v0-13-4-pre-release-notes/115257#p-2358763-stateful-compression-11
	SystemContract2Address   = felt.FromUint64[felt.Address](2) // global counter
	SystemContractsClassHash = felt.Zero[felt.ClassHash]()
	SystemContracts          = [...]felt.Address{SystemContract1Address, SystemContract2Address}
)

System contracts are protocol-level contracts (0x1, 0x2) that hold storage but have no Cairo class; their class hash is always zero. They are not part of StateDiff.DeployedContracts and are auto-created during state commit so their storage trie can exist.

Functions

func DeleteClass

func DeleteClass(w db.KeyValueWriter, classHash *felt.ClassHash) error

func DeleteClassHashHistory

func DeleteClassHashHistory(w db.KeyValueWriter, addr *felt.Address, blockNum uint64) error

func DeleteContract

func DeleteContract(w db.KeyValueWriter, addr *felt.Address) error

func DeleteNonceHistory

func DeleteNonceHistory(w db.KeyValueWriter, addr *felt.Address, blockNum uint64) error

func DeleteStorageHistory

func DeleteStorageHistory(
	w db.KeyValueWriter,
	addr *felt.Address,
	key *felt.Felt,
	blockNum uint64,
) error

func GetClass

func GetClass(
	r db.KeyValueReader,
	classHash *felt.ClassHash,
) (*core.DeclaredClassDefinition, error)

func GetContract

func GetContract(r db.KeyValueReader, addr *felt.Address) (stateContract, error)

Gets a contract instance from the database. Returns db.ErrKeyNotFound when the contract does not exist, for backward compatibility. TODO: return more precise error (e.g. ErrContractNotDeployed) once deprecated state is removed.

func GetStateObject

func GetStateObject(r db.KeyValueReader, state *State, addr *felt.Address) (*stateObject, error)

func HasClass

func HasClass(r db.KeyValueReader, classHash *felt.ClassHash) (bool, error)

func HasContract

func HasContract(r db.KeyValueReader, addr *felt.Address) (bool, error)

func IsSystemContract added in v0.16.6

func IsSystemContract(addr *felt.Address) bool

IsSystemContract reports whether addr is a protocol system contract (0x1, 0x2) that has no Cairo class.

func NewStateHistory

func NewStateHistory(
	blockNum uint64,
	stateRoot *felt.StateRootHash,
	db *StateDB,
) (stateHistory, error)

func WriteClass

func WriteClass(
	w db.KeyValueWriter,
	classHash *felt.ClassHash,
	class *core.DeclaredClassDefinition,
) error

func WriteClassHashHistory

func WriteClassHashHistory(
	w db.KeyValueWriter,
	addr *felt.Address,
	blockNum uint64,
	classHash *felt.ClassHash,
) error

func WriteContract

func WriteContract(
	w db.KeyValueWriter,
	addr *felt.Address,
	nonce felt.Felt,
	classHash felt.ClassHash,
	deployHeight uint64,
) error

func WriteContractRecord added in v0.17.0

func WriteContractRecord(w db.KeyValueWriter, addr *felt.Address, contract *stateContract) error

WriteContractRecord stores a whole contract record

func WriteNonceHistory

func WriteNonceHistory(
	w db.KeyValueWriter,
	addr *felt.Address,
	blockNum uint64,
	nonce *felt.Felt,
) error

func WriteStorageHistory

func WriteStorageHistory(
	w db.KeyValueWriter,
	addr *felt.Address,
	key *felt.Felt,
	blockNum uint64,
	value *felt.Felt,
) error

Types

type State

type State struct {
	StateReader
	// contains filtered or unexported fields
}

State extends StateReader with mutation operations.

func New

func New(stateRoot *felt.StateRootHash, db *StateDB, batch db.Batch) (*State, error)

New creates a writable state at the given root. The caller must provide a non-nil batch. Should be used for operations, where state mutations are required. Read operations are also supported.

func (*State) Revert

func (s *State) Revert(header *core.Header, update *core.StateUpdate) error

Revert a given state update. The block number is the block number of the state update.

func (*State) Update

func (s *State) Update(
	header *core.Header,
	update *core.StateUpdate,
	declaredClasses map[felt.ClassHash]core.ClassDefinition,
	skipVerifyNewRoot bool,
) error

Applies a state update to a given state. If any error is encountered, state is not updated. After a state update is applied, the root of the state must match the given new root in the state update.

type StateDB

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

func NewStateDB

func NewStateDB(disk db.KeyValueStore, triedb database.TrieDB) *StateDB

func (*StateDB) ClassTrie

func (s *StateDB) ClassTrie(stateComm *felt.StateRootHash) (*trie2.Trie, error)

Opens a class trie for the given state root

func (*StateDB) ContractStorageTrie

func (s *StateDB) ContractStorageTrie(
	stateComm *felt.StateRootHash,
	owner *felt.Address,
) (*trie2.Trie, error)

Opens a contract storage trie for the given state root and contract address

func (*StateDB) ContractTrie

func (s *StateDB) ContractTrie(stateComm *felt.StateRootHash) (*trie2.Trie, error)

Opens a contract trie for the given state root

type StateReader

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

StateReader is a read-only view of the state at a given root.

func NewStateReader added in v0.16.0

func NewStateReader(stateRoot *felt.StateRootHash, db *StateDB) (*StateReader, error)

NewStateReader creates a read-only view of the state at the given root. Should be used for read operations that don't require state mutations.

func (*StateReader) Class added in v0.16.0

func (s *StateReader) Class(classHash *felt.ClassHash) (*core.DeclaredClassDefinition, error)

func (*StateReader) ClassTrie added in v0.16.0

func (s *StateReader) ClassTrie() (core.TrieReader, error)

func (*StateReader) Commitment added in v0.16.0

func (s *StateReader) Commitment(protocolVersion string) (felt.StateRootHash, error)

func (*StateReader) CompiledClassHash added in v0.16.0

func (s *StateReader) CompiledClassHash(
	classHash *felt.SierraClassHash,
) (felt.CasmClassHash, error)

func (*StateReader) CompiledClassHashAt added in v0.16.0

func (s *StateReader) CompiledClassHashAt(
	classHash *felt.SierraClassHash,
	blockNumber uint64,
) (felt.CasmClassHash, error)

func (*StateReader) CompiledClassHashV2 added in v0.16.0

func (s *StateReader) CompiledClassHashV2(
	classHash *felt.SierraClassHash,
) (felt.CasmClassHash, error)

func (*StateReader) ContractClassHash added in v0.16.0

func (s *StateReader) ContractClassHash(addr *felt.Address) (felt.ClassHash, error)

func (*StateReader) ContractClassHashAt added in v0.16.0

func (s *StateReader) ContractClassHashAt(
	addr *felt.Address,
	blockNum uint64,
) (felt.ClassHash, error)

Returns the class hash of a contract at a given block number.

func (*StateReader) ContractDeployedAt added in v0.16.0

func (s *StateReader) ContractDeployedAt(addr *felt.Address, blockNum uint64) (bool, error)

func (*StateReader) ContractMetadata added in v0.16.7

func (s *StateReader) ContractMetadata(
	addr *felt.Address,
) (classHash felt.ClassHash, nonce, storageRoot felt.Felt, err error)

func (*StateReader) ContractNonce added in v0.16.0

func (s *StateReader) ContractNonce(addr *felt.Address) (felt.Felt, error)

func (*StateReader) ContractNonceAt added in v0.16.0

func (s *StateReader) ContractNonceAt(addr *felt.Address, blockNum uint64) (felt.Felt, error)

Returns the nonce of a contract at a given block number.

func (*StateReader) ContractStorage added in v0.16.0

func (s *StateReader) ContractStorage(addr *felt.Address, key *felt.Felt) (felt.Felt, error)

ContractStorage reads a storage slot directly from the trie at this reader's root. Missing slots read as zero.

func (*StateReader) ContractStorageAt added in v0.16.0

func (s *StateReader) ContractStorageAt(
	addr *felt.Address,
	key *felt.Felt,
	blockNum uint64,
) (felt.Felt, error)

Returns the storage value of a contract at a given storage key at a given block number.

func (*StateReader) ContractStorageLastUpdatedAt added in v0.16.0

func (s *StateReader) ContractStorageLastUpdatedAt(
	addr *felt.Address,
	key *felt.Felt,
	blockNum uint64,
) (uint64, error)

Returns the block number at which a given storage slot key of a given contract was last updated, up to and including the given block number.

func (*StateReader) ContractStorageLastUpdatedBlock added in v0.16.0

func (s *StateReader) ContractStorageLastUpdatedBlock(
	addr *felt.Address,
	key *felt.Felt,
) (uint64, error)

ContractStorageLastUpdatedBlock returns the most recent block number at which a given storage slot key of a given contract was last updated.

func (*StateReader) ContractStorageTrie added in v0.16.0

func (s *StateReader) ContractStorageTrie(addr *felt.Address) (core.TrieReader, error)

func (*StateReader) ContractTrie added in v0.16.0

func (s *StateReader) ContractTrie() (core.TrieReader, error)

func (*StateReader) GetReverseStateDiff added in v0.16.0

func (s *StateReader) GetReverseStateDiff(
	blockNum uint64,
	diff *core.StateDiff,
) (core.StateDiff, error)

type Storage

type Storage map[felt.Felt]*felt.Felt

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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