precompile

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2022 License: GPL-3.0, LGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	UsedAddresses = []common.Address{}

	// ReservedRanges contains addresses ranges that are reserved
	// for precompiles and cannot be used as EOA or deployed contracts.
	ReservedRanges = []AddressRange{
		{

			common.HexToAddress("0x0100000000000000000000000000000000000000"),
			common.HexToAddress("0x01000000000000000000000000000000000000ff"),
		},
	}
)

Designated addresses of stateful precompiles Note: it is important that none of these addresses conflict with each other or any other precompiles in core/vm/contracts.go. We start at 0x0100000000000000000000000000000000000000 and will increment by 1 from here to reduce the risk of conflicts.

Functions

func CalculateFunctionSelector

func CalculateFunctionSelector(functionSignature string) []byte

CalculateFunctionSelector returns the 4 byte function selector that results from [functionSignature] Ex. the function setBalance(addr address, balance uint256) should be passed in as the string: "setBalance(address,uint256)"

func CheckConfigure

func CheckConfigure(chainConfig ChainConfig, parentTimestamp *big.Int, blockContext BlockContext, precompileConfig StatefulPrecompileConfig, state StateDB)

CheckConfigure checks if [config] is activated by the transition from block at [parentTimestamp] to the timestamp set in [blockContext]. If it does, then it calls Configure on [precompileConfig] to make the necessary state update to enable the StatefulPrecompile. Note: this function is called within genesis to configure the starting state if [precompileConfig] specifies that it should be configured at genesis, or happens during block processing to update the state before processing the given block. TODO: add ability to call Configure at different timestamps, so that developers can easily re-configure by updating the stateful precompile config. Assumes that [config] is non-nil.

Types

type AddressRange

type AddressRange struct {
	Start common.Address
	End   common.Address
}

AddressRange represents a continuous range of addresses

func (*AddressRange) Contains

func (a *AddressRange) Contains(addr common.Address) bool

Contains returns true iff [addr] is contained within the (inclusive)

type BlockContext

type BlockContext interface {
	Number() *big.Int
	Timestamp() *big.Int
}

BlockContext defines an interface that provides information to a stateful precompile about the block that activates the upgrade. The precompile can access this information to initialize its state.

type ChainConfig

type ChainConfig interface {
}

ChainContext defines an interface that provides information to a stateful precompile about the chain configuration. The precompile can access this information to initialize its state.

type PrecompileAccessibleState

type PrecompileAccessibleState interface {
	GetStateDB() StateDB
	GetBlockContext() BlockContext
	NativeAssetCall(caller common.Address, input []byte, suppliedGas uint64, gasGost uint64, readOnly bool) (ret []byte, remainingGas uint64, err error)
}

PrecompileAccessibleState defines the interface exposed to stateful precompile contracts

type RunStatefulPrecompileFunc

type RunStatefulPrecompileFunc func(accessibleState PrecompileAccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error)

type StateDB

type StateDB interface {
	GetState(common.Address, common.Hash) common.Hash
	SetState(common.Address, common.Hash, common.Hash)

	SetCode(common.Address, []byte)

	SetNonce(common.Address, uint64)
	GetNonce(common.Address) uint64

	GetBalance(common.Address) *big.Int
	AddBalance(common.Address, *big.Int)
	SubBalance(common.Address, *big.Int)

	SubBalanceMultiCoin(common.Address, common.Hash, *big.Int)
	AddBalanceMultiCoin(common.Address, common.Hash, *big.Int)
	GetBalanceMultiCoin(common.Address, common.Hash) *big.Int

	CreateAccount(common.Address)
	Exist(common.Address) bool
}

StateDB is the interface for accessing EVM state

type StatefulPrecompileConfig

type StatefulPrecompileConfig interface {
	// Address returns the address where the stateful precompile is accessible.
	Address() common.Address
	// Timestamp returns the timestamp at which this stateful precompile should be enabled.
	// 1) 0 indicates that the precompile should be enabled from genesis.
	// 2) n indicates that the precompile should be enabled in the first block with timestamp >= [n].
	// 3) nil indicates that the precompile is never enabled.
	Timestamp() *big.Int
	// Configure is called on the first block where the stateful precompile should be enabled.
	// This allows the stateful precompile to configure its own state via [StateDB] as necessary.
	// This function must be deterministic since it will impact the EVM state. If a change to the
	// config causes a change to the state modifications made in Configure, then it cannot be safely
	// made to the config after the network upgrade has gone into effect.
	//
	// Configure is called on the first block where the stateful precompile should be enabled. This
	// provides the config the ability to set its initial state and should only modify the state within
	// its own address space.
	Configure(ChainConfig, StateDB, BlockContext)
	// Contract returns a thread-safe singleton that can be used as the StatefulPrecompiledContract when
	// this config is enabled.
	Contract() StatefulPrecompiledContract
}

StatefulPrecompileConfig defines the interface for a stateful precompile to

type StatefulPrecompiledContract

type StatefulPrecompiledContract interface {
	// Run executes the precompiled contract.
	Run(accessibleState PrecompileAccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error)
}

StatefulPrecompiledContract is the interface for executing a precompiled contract

Jump to

Keyboard shortcuts

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