runopts

package
v1.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package runopts provides configuration options for specops.Code.Run().

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultContractAddress

func DefaultContractAddress() common.Address

DefaultContractAddress returns the default address used as [Contract.Address].

func DefaultFromAddress

func DefaultFromAddress() common.Address

DefaultFromAddress returns the default address from which the contract is called.

Types

type Captured

type Captured[T any] struct {
	Val T
	// contains filtered or unexported fields
}

A Captured value is an Option that stores part of the Configuration for later inspection. After Run() and similar functions return, the Val field will be populated.

A set of constructors is provided for commonly captured values.

Example
const (
	slot  = 42
	value = 314159
)

code := Code{
	Fn(SSTORE, PUSH(slot), PUSH(value)),
}

// All runopts.Captured[T] values are passed to Run() to be populated, after
// which, their Val fields can be used.
db := runopts.CaptureStateDB()
if _, err := code.Run(nil, db); err != nil {
	log.Fatal(err)
}

got := db.Val.GetState(
	runopts.DefaultContractAddress(),
	common.BigToHash(big.NewInt(slot)),
)
fmt.Println(new(uint256.Int).SetBytes(got[:]))
Output:

314159

func Capture

func Capture[T any](fn func(*Configuration) T) *Captured[T]

Capture returns a Captured value that is valid _after_ being passed as an option to Run(). [fn] must extract and return the value to capture.

func CaptureBytecode

func CaptureBytecode() *Captured[[]byte]

CaptureBytecode captures a copy of the compiled bytecode.

func CaptureConfig

func CaptureConfig() *Captured[*Configuration]

CaptureConfig captures the entire Configuration.

func CaptureStateDB

func CaptureStateDB() *Captured[vm.StateDB]

CaptureStateDB captures the vm.StateDB used for storage of accounts (i.e. balances, code, storage, etc).

func (*Captured[T]) Apply

func (c *Captured[T]) Apply(cfg *Configuration) error

Apply implements the Option interface, storing the value to be captured.

type Configuration

type Configuration struct {
	Contract        *Contract
	From            common.Address
	Value           *uint256.Int
	NoErrorOnRevert bool // see Run() re errors
	// vm.NewEVM()
	BlockCtx    vm.BlockContext
	TxCtx       vm.TxContext
	StateDB     vm.StateDB
	ChainConfig *params.ChainConfig
	VMConfig    vm.Config
}

A Configuration carries all values that can be modified to configure a call to specops.Code.Run(). It is intially set by Run() and then passed to all Options to be modified.

The vm.StateDB will be initialised to an empty but valid database that MAY be populated by an Option or even entirely replaced. The code for [Contract.Address] MUST NOT be prepopulated but storage and balance MAY be altered.

type Contract

type Contract struct {
	Address common.Address
	// contains filtered or unexported fields
}

Contract defines how the compiled SpecOps bytecode will be "deployed" before being run. DefaultContractAddress returns the default address with which Contracts are constructed.

func NewContract

func NewContract(bytecode []byte) *Contract

NewContract returns a new Contract with the specified bytecode.

func (*Contract) Bytecode

func (c *Contract) Bytecode() []byte

Bytecode returns a copy of the code to be deployed.

type Func

type Func func(*Configuration) error

A Func converts a function into an Option by calling itself as Apply().

func (Func) Apply

func (f Func) Apply(c *Configuration) error

Apply returns f(c).

type Option

type Option interface {
	Apply(*Configuration) error
}

An Option modifies a Configuration.

func ContractAddress

func ContractAddress(a common.Address) Option

ContractAddress sets the address to which the compiled bytecode will be "deployed" before being run.

func From

func From(a common.Address) Option

From sets the address calling the contract; i.e. the value pushed to the stack by the CALLER opcode.

func GenesisAlloc

func GenesisAlloc(alloc types.GenesisAlloc) Option

GenesisAlloc preloads the state with code, storage values, and balances described in the alloc. This can be used for testing interaction with other contracts.

func NoErrorOnRevert

func NoErrorOnRevert() Option

NoErrorOnRevert signals to Run() that it must return a nil error if the Code compiled and was successfully executed but the execution itself reverted. The error will still be available in the vm.ExecutionResult.

func Value

func Value[U Unsigned](v U) Option

Value sets the value sent when calling the contract; i.e. the value pushed to the stack by the CALLVALUE opcode.

func WithDebugger

func WithDebugger(dbg *evmdebug.Debugger) Option

WithDebugger returns an Option that sets Configuration.VMConfig.Tracer to dbg.Tracer(), intercepting every opcode execution. See evmdebug for details.

func WithNewDebugger

func WithNewDebugger() (*evmdebug.Debugger, Option)

WithNewDebugger is a convenience function for constructing a new Debugger, passing it to WithDebugger(), and returning both the Debugger and the Option.

type Unsigned

type Unsigned interface {
	uint256.Int | *uint256.Int | uint | uint64
}

An Unsigned type is an unsigned integer.

Jump to

Keyboard shortcuts

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