statedb

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: LGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Copyright 2021 Evmos Foundation This file is part of Evmos' Ethermint library.

The Ethermint library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Ethermint library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with the Ethermint library. If not, see https://github.com/Helios-Chain-Labs/ethermint/blob/main/LICENSE

Copyright 2021 Evmos Foundation This file is part of Evmos' Ethermint library.

The Ethermint library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Ethermint library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with the Ethermint library. If not, see https://github.com/Helios-Chain-Labs/ethermint/blob/main/LICENSE

Copyright 2021 Evmos Foundation This file is part of Evmos' Ethermint library.

The Ethermint library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Ethermint library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with the Ethermint library. If not, see https://github.com/Helios-Chain-Labs/ethermint/blob/main/LICENSE

Copyright 2021 Evmos Foundation This file is part of Evmos' Ethermint library.

The Ethermint library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Ethermint library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with the Ethermint library. If not, see https://github.com/Helios-Chain-Labs/ethermint/blob/main/LICENSE

Index

Constants

View Source
const StateDBContextKey = "statedb"

Variables

This section is empty.

Functions

func Transfer

func Transfer(db vm.StateDB, sender, recipient common.Address, amount *uint256.Int)

Types

type Account

type Account struct {
	Nonce    uint64
	CodeHash []byte
}

Account is the Ethereum consensus representation of accounts. These objects are stored in the storage of auth module.

func NewEmptyAccount

func NewEmptyAccount() *Account

NewEmptyAccount returns an empty account.

func (Account) IsContract

func (acct Account) IsContract() bool

IsContract returns if the account contains contract code.

type EventConverter

type EventConverter = func(sdk.Event) (*ethtypes.Log, error)

type JournalEntry

type JournalEntry interface {
	// Revert undoes the changes introduced by this journal entry.
	Revert(*StateDB)

	// Dirtied returns the Ethereum address modified by this journal entry.
	Dirtied() *common.Address
}

JournalEntry is a modification entry in the state change journal that can be Reverted on demand.

type Keeper

type Keeper interface {
	GetParams(sdk.Context) evmtypes.Params

	Transfer(ctx sdk.Context, sender, recipient sdk.AccAddress, coins sdk.Coins) error
	AddBalance(ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) error
	SubBalance(ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) error
	SetBalance(ctx sdk.Context, addr common.Address, amount *big.Int, denom string) error
	GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) *big.Int

	// Read methods
	GetAccount(ctx sdk.Context, addr common.Address) *Account
	GetState(ctx sdk.Context, addr common.Address, key common.Hash) common.Hash
	GetCode(ctx sdk.Context, codeHash common.Hash) []byte
	// the callback returns false to break early
	ForEachStorage(ctx sdk.Context, addr common.Address, cb func(key, value common.Hash) bool)

	// Write methods, only called by `StateDB.Commit()`
	SetAccount(ctx sdk.Context, addr common.Address, account Account) error
	SetState(ctx sdk.Context, addr common.Address, key common.Hash, value []byte)
	SetCode(ctx sdk.Context, codeHash []byte, code []byte)
	DeleteAccount(ctx sdk.Context, addr common.Address) error
}

Keeper provide underlying storage of StateDB

type StateDB

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

func New

func New(ctx sdk.Context, keeper Keeper, txConfig TxConfig) *StateDB

New creates a new state from a given trie.

func NewWithParams

func NewWithParams(ctx sdk.Context, keeper Keeper, txConfig TxConfig, evmDenom string) *StateDB

func (*StateDB) AddAddressToAccessList

func (s *StateDB) AddAddressToAccessList(addr common.Address)

AddAddressToAccessList adds the given address to the access list

func (*StateDB) AddBalance

func (s *StateDB) AddBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason)

AddBalance adds amount to the account associated with addr.

func (*StateDB) AddLog

func (s *StateDB) AddLog(log *ethtypes.Log)

AddLog adds a log, called by evm.

func (*StateDB) AddPreimage

func (s *StateDB) AddPreimage(_ common.Hash, _ []byte)

AddPreimage records a SHA3 preimage seen by the VM. AddPreimage performs a no-op since the EnablePreimageRecording flag is disabled on the vm.Config during state transitions. No store trie preimages are written to the database.

func (*StateDB) AddRefund

func (s *StateDB) AddRefund(gas uint64)

AddRefund adds gas to the refund counter

func (*StateDB) AddSlotToAccessList

func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash)

AddSlotToAccessList adds the given (address, slot)-tuple to the access list

func (*StateDB) AddressInAccessList

func (s *StateDB) AddressInAccessList(addr common.Address) bool

AddressInAccessList returns true if the given address is in the access list.

func (*StateDB) CacheContext

func (s *StateDB) CacheContext() sdk.Context

CacheContext returns a cache of the current context for query native state in precompiles. Implements ExtStateDB

func (*StateDB) Commit

func (s *StateDB) Commit() error

Commit writes the dirty states to keeper the StateDB object should be discarded after committed.

func (*StateDB) Context

func (s *StateDB) Context() sdk.Context

Context returns the current context for query native state in precompiles.

func (*StateDB) CreateAccount

func (s *StateDB) CreateAccount(addr common.Address)

CreateAccount explicitly creates a state object. If a state object with the address already exists the balance is carried over to the new account.

CreateAccount is called during the EVM CREATE operation. The situation might arise that a contract does the following:

1. sends funds to sha(account ++ (nonce + 1)) 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)

Carrying over the balance ensures that Ether doesn't disappear.

func (*StateDB) CreateContract

func (s *StateDB) CreateContract(addr common.Address)

CreateContract is used whenever a contract is created. This may be preceded by CreateAccount, but that is not required if it already existed in the state due to funds sent beforehand. This operation sets the 'newContract'-flag, which is required in order to correctly handle EIP-6780 'delete-in-same-transaction' logic.

func (*StateDB) Empty

func (s *StateDB) Empty(addr common.Address) bool

Empty returns whether the state object is either non-existent or empty according to the EIP161 specification (balance = nonce = code = 0)

func (*StateDB) ExecuteNativeAction

func (s *StateDB) ExecuteNativeAction(contract common.Address, converter EventConverter, action func(ctx sdk.Context) error) error

ExecuteNativeAction executes native action in isolate, the writes will be reverted when either the native action itself fail or the wrapping message call reverted.

func (*StateDB) Exist

func (s *StateDB) Exist(addr common.Address) bool

Exist reports whether the given account address exists in the state. Notably this also returns true for self-destructed accounts.

func (*StateDB) ForEachStorage

func (s *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error

ForEachStorage iterate the contract storage, the iteration order is not defined.

func (*StateDB) GetBalance

func (s *StateDB) GetBalance(addr common.Address) *uint256.Int

GetBalance retrieves the balance from the given address or 0 if object not found

func (*StateDB) GetCode

func (s *StateDB) GetCode(addr common.Address) []byte

GetCode returns the code of account, nil if not exists.

func (*StateDB) GetCodeHash

func (s *StateDB) GetCodeHash(addr common.Address) common.Hash

GetCodeHash returns the code hash of account.

func (*StateDB) GetCodeSize

func (s *StateDB) GetCodeSize(addr common.Address) int

GetCodeSize returns the code size of account.

func (*StateDB) GetCommittedState

func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash

GetCommittedState retrieves a value from the given account's committed storage trie.

func (*StateDB) GetNonce

func (s *StateDB) GetNonce(addr common.Address) uint64

GetNonce returns the nonce of account, 0 if not exists.

func (*StateDB) GetRefund

func (s *StateDB) GetRefund() uint64

GetRefund returns the current value of the refund counter.

func (*StateDB) GetState

func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash

GetState retrieves a value from the given account's storage trie.

func (*StateDB) GetStorageRoot

func (s *StateDB) GetStorageRoot(_ common.Address) common.Hash

GetStorageRoot is not relevant for Ethermint. In go-thereum, it retrieves the storage root from the given address as part of the process to generate proofs. But Ethermint uses a different kind of proofs (because it represents state differently), so this method is not important.

func (*StateDB) GetTransientState

func (s *StateDB) GetTransientState(addr common.Address, key common.Hash) common.Hash

GetTransientState gets transient storage for a given account.

func (*StateDB) HasSelfDestructed

func (s *StateDB) HasSelfDestructed(addr common.Address) bool

HasSelfDestructed returns if the contract is self-destructed in current transaction.

func (*StateDB) Keeper

func (s *StateDB) Keeper() Keeper

Keeper returns the underlying `Keeper`

func (*StateDB) Logs

func (s *StateDB) Logs() []*ethtypes.Log

Logs returns the logs of current transaction.

func (*StateDB) NativeEvents

func (s *StateDB) NativeEvents() sdk.Events

func (*StateDB) PointCache

func (s *StateDB) PointCache() *utils.PointCache

PointCache is not relevant for Etheremint. In go-ethereum, it returns the cache of evaluated curve points used in verkle tree key computation (part of the state's underlying trie). This is in turn used to calculate gas costs. Ethermint uses a different state and database than go-ethereum, and a different way to calculating gas costs, so this method is not important.

func (*StateDB) Prepare

func (s *StateDB) Prepare(
	rules params.Rules,
	sender,
	coinbase common.Address,
	dst *common.Address,
	precompiles []common.Address,
	list ethtypes.AccessList,
)

Prepare handles the preparatory steps for executing a state transition with. This method must be invoked before state transition.

Berlin fork: - Add sender to access list (2929) - Add destination to access list (2929) - Add precompiles to access list (2929) - Add the contents of the optional tx access list (2930)

Potential EIPs: - Reset access list (Berlin) - Add coinbase to access list (EIP-3651) - Reset transient storage (EIP-1153)

func (*StateDB) RevertToSnapshot

func (s *StateDB) RevertToSnapshot(revid int)

RevertToSnapshot reverts all state changes made since the given revision.

func (*StateDB) SelfDestruct

func (s *StateDB) SelfDestruct(addr common.Address)

Suicide marks the given account as suicided. This clears the account balance.

The account's state object is still available until the state is committed, getStateObject will return a non-nil account after Suicide.

func (*StateDB) Selfdestruct6780

func (s *StateDB) Selfdestruct6780(addr common.Address)

func (*StateDB) SetBalance

func (s *StateDB) SetBalance(addr common.Address, amount *big.Int)

SetBalance is called by state override

func (*StateDB) SetCode

func (s *StateDB) SetCode(addr common.Address, code []byte)

SetCode sets the code of account.

func (*StateDB) SetNonce

func (s *StateDB) SetNonce(addr common.Address, nonce uint64)

SetNonce sets the nonce of account.

func (*StateDB) SetState

func (s *StateDB) SetState(addr common.Address, key, value common.Hash)

SetState sets the contract state.

func (*StateDB) SetStorage

func (s *StateDB) SetStorage(addr common.Address, storage Storage)

SetStorage replaces the entire storage for the specified account with given storage. This function should only be used for debugging and the mutations must be discarded afterward.

func (*StateDB) SetTracer

func (s *StateDB) SetTracer(tracer *cosmostracing.Hooks)

func (*StateDB) SetTransientState

func (s *StateDB) SetTransientState(addr common.Address, key, value common.Hash)

SetTransientState sets transient storage for a given account. It adds the change to the journal so that it can be rolled back to its previous value if there is a revert.

func (*StateDB) SlotInAccessList

func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool)

SlotInAccessList returns true if the given (address, slot)-tuple is in the access list.

func (*StateDB) Snapshot

func (s *StateDB) Snapshot() int

Snapshot returns an identifier for the current revision of the state.

func (*StateDB) SubBalance

func (s *StateDB) SubBalance(addr common.Address, amount *uint256.Int, reason tracing.BalanceChangeReason)

SubBalance subtracts amount from the account associated with addr.

func (*StateDB) SubRefund

func (s *StateDB) SubRefund(gas uint64)

SubRefund removes gas from the refund counter. This method will panic if the refund counter goes below zero

func (*StateDB) Transfer

func (s *StateDB) Transfer(sender, recipient common.Address, amount *big.Int)

Transfer from one account to another

func (*StateDB) Witness

func (s *StateDB) Witness() *stateless.Witness

Witness is not relevant for Ethermint. in go-ethereum, it is used as part of an experimental feature called "cross client verification" which is disabled by default. It's ok to return nil here. cf : https://gist.github.com/karalabe/47c906f0ab4fdc5b8b791b74f084e5f9

type Storage

type Storage map[common.Hash]common.Hash

Storage represents in-memory cache/buffer of contract storage.

func (Storage) SortedKeys

func (s Storage) SortedKeys() []common.Hash

SortedKeys sort the keys for deterministic iteration

type TxConfig

type TxConfig struct {
	BlockHash common.Hash // hash of current block
	TxHash    common.Hash // hash of current tx
	TxIndex   uint        // the index of current transaction
	LogIndex  uint        // the index of next log within current block
}

TxConfig encapulates the readonly information of current tx for `StateDB`.

func NewEmptyTxConfig

func NewEmptyTxConfig(bhash common.Hash) TxConfig

NewEmptyTxConfig construct an empty TxConfig, used in context where there's no transaction, e.g. `eth_call`/`eth_estimateGas`.

func NewTxConfig

func NewTxConfig(bhash, thash common.Hash, txIndex, logIndex uint) TxConfig

NewTxConfig returns a TxConfig

Jump to

Keyboard shortcuts

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