tevm

package
v0.0.0-...-5c8f8ed Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2019 License: MIT Imports: 19 Imported by: 0

README

Test EVM

A unit-testing library for solidity code

Intro

The tevm package makes it easy to compile and test solidity code using go test.

In short, the library glues together the solc solidity compiler, the geth EVM, and go test in order to make testing easy and high-fidelity. The code includes features like content-based compilation caching to make the comile-test-debug cycle faster.

Setup

Make sure you have the latest version of geth by running

go get -t -d -u github.com/ethereum/go-ethereum

Change into the Test EVM Daemon project directory

cd seth/tevm/tevmd

Install the Go package

go install

Run the daemon

tevmd

You should see output similar to the following:

2018/01/12 15:25:06 default account: 0x52fdfc072182654f163f5f0f9a621d729566c74d
2018/01/12 15:25:06 binding to :8043...

The default account will be funded with 1 eth and node will be listening on http://localhost:8043.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account [32 + 8 + 1]byte

an account is a tuple of (balance, nonce, suicided)

func (*Account) Balance

func (a *Account) Balance() seth.Int

func (*Account) Nonce

func (a *Account) Nonce() uint64

func (*Account) SetBalance

func (a *Account) SetBalance(v *big.Int)

func (*Account) SetNonce

func (a *Account) SetNonce(n uint64)

func (*Account) SetSuicided

func (a *Account) SetSuicided(t bool)

func (*Account) Suicided

func (a *Account) Suicided() bool

type Chain

type Chain struct {
	// Debugf, if non-nil, is used to log debugging information
	// about transactions being executed, mined, etc.
	Debugf func(format string, args ...interface{})
	State  State
	// contains filtered or unexported fields
}

A Chain is a model of the state of the blockchain. The fields in this type are not threadsafe and must not be accessed concurrently. The methods on this type are threadsafe.

func NewChain

func NewChain() *Chain

NewChain creates a new fake blockchain. In its initial state, the chain has no accounts with non-zero balances, and no deployed contracts.

func NewFork

func NewFork(c *seth.Client, blocknum int64) *Chain

NewFork creates a new fake blockchain that operates like a fork of the chain backing the given client at the given block number.

tevm "forks" work by overlaying state updates on top of the existing chain state, and chain state is fetched lazily as calls are made. Consequently, it costs basically nothing to make a "fork," because no data is actually copied.

func (*Chain) AddBalance

func (c *Chain) AddBalance(addr *seth.Address, v *big.Int)

AddBalance adds to the balance of an account.

func (*Chain) AtBlock

func (c *Chain) AtBlock(n int64) *Chain

AtBlock returns the chain state at a given block number. As a special case, -1 is interpreted as the pending block (i.e. the current chain state), and -2 is interpreted as the latest block (i.e. the chain state just before the pending block).

func (*Chain) BalanceOf

func (c *Chain) BalanceOf(addr *seth.Address) *big.Int

BalanceOf returns the balance of an address, in Wei.

func (*Chain) Call

func (c *Chain) Call(sender, dst *seth.Address, sig string, args ...seth.EtherType) ([]byte, error)

Call executes a transaction that represents a call initiated by 'sender' to the destination address.

'sig' must be in the canonical method signature encoding.

func (*Chain) Client

func (c *Chain) Client() *seth.Client

Client creates a seth.Client that talks to the fake chain. The client can be used to test unmodified code using the seth library against the mock chain.

func (*Chain) Copy

func (c *Chain) Copy() *Chain

Copy returns a new logical copy of the chain. Copy avoids making a deep copy of the state.

func (*Chain) Create

func (c *Chain) Create(sender *seth.Address, code []byte) (seth.Address, error)

Create executes a transation that deploys the given code to a new contract address, and returns the address of the newly created contract.

func (*Chain) CreateAt

func (c *Chain) CreateAt(addr, sender *seth.Address, code []byte) error

CreateAt creates a new contract at the given address. This does not do bookkeeping in the same way that Create does. In particular, it does not increment the sender nonce or enforce callstack limits.

func (*Chain) EstimateGas

func (c *Chain) EstimateGas(sender, dst *seth.Address, sig string, args ...seth.EtherType) (uint64, error)

EstimateGas estimates the amount of gas that the given transaction will use.

func (*Chain) Execute

func (c *Chain) Execute(req *seth.RPCRequest, res *seth.RPCResponse) error

Execute implements seth.Transport.

func (*Chain) Logs

func (c *Chain) Logs() []seth.Log

Logs returns all of the logs emitted by transactions in this chain.

NOTE: if the chain is using a fallback chain, the returned log values do not include logs from that fallback chain.

func (*Chain) MarshalJSON

func (c *Chain) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Chain) Mine

func (c *Chain) Mine(tx *seth.Transaction) (ret []byte, h seth.Hash, err error)

Mine executes a transaction and returns the return value of the transaction (if any) and the transaction hash. Unlike the other methods of executing a transaction on a Chain, this method updates the pending block and saves the transaction and its receipt in the state tree so that they can be retrieved later. Additionally, this method respects the amount of gas sent in the transaction, rather than offering all of the gas in the block to the transaction, which more faithfully mimics the behavior of an actual ethereum node.

func (*Chain) NewAccount

func (c *Chain) NewAccount(ether int) seth.Address

NewAccount creates a new account with some ether in it. The balance of the new account will be 'ether' * 10**18

func (*Chain) Seal

func (c *Chain) Seal()

Seal seals the current block (c.Pending) and replaces it with a new pending block with the same parameters (but with an update block number and hash, and zeroed gas used).

func (*Chain) Send

func (c *Chain) Send(sender, dst *seth.Address, value *big.Int) error

Send creates a transaction that sends ether from one address to another.

func (*Chain) Sender

func (c *Chain) Sender(from *seth.Address) *seth.Sender

Sender creates a Sender from a sending address. This can be used to test unmodified Go code using the seth library against a synthetic blockchain.

func (*Chain) ServeHTTP

func (s *Chain) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

func (*Chain) StaticCall

func (c *Chain) StaticCall(sender, dst *seth.Address, sig string, args ...seth.EtherType) ([]byte, error)

StaticCall yields the result of the given transaction in the pending block without comitting the state changes to the chain.

func (*Chain) SubBalance

func (c *Chain) SubBalance(addr *seth.Address, v *big.Int)

SubBalance subtracts from the balance of an account.

func (*Chain) UnmarshalJSON

func (c *Chain) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type State

type State struct {
	// Fallback is used when a lookup for data on an account
	// fails for in-memory state. Fallback always reads state
	// from a specific block number, and local modifications
	// always take precedence over fallback state.
	Fallback struct {
		*seth.Client
		Block int64
	}

	Refund seth.Uint64
	Trace  func(fn string, args ...interface{}) `json:"-"`

	Pending *seth.Block

	Accounts     Tree
	Code         Tree
	Storage      Tree // key = hash(address, pointer)
	Preimage     Tree
	Transactions Tree // key = txhash, value = serialized tx
	Receipts     Tree // key = txhash, value = serialized rx
	Blocks       Tree // key = n2h(blocknum) = hash, value = serialized block

	Logs []*types.Log
	// contains filtered or unexported fields
}

State database for the EVM.

func (*State) StateDB

func (s *State) StateDB() vm.StateDB

StateDB returns a view of s that implements vm.StateDB.

type Tree

type Tree tree

Tree is a tree that stores byte-based key-value pairs using a copy-on-write binary tree so that its state can be snapshotted in constant time.

The zero value of a Tree is the empty set.

func (*Tree) CopyAt

func (t *Tree) CopyAt(snap int) Tree

CopyAt returns a logical copy of thre tree at a given snapshot. (As an optimization, the data itself is not copied.) Updates to the returned Tree will not be reflected in t.

A safe copy of the current state of the tree can be obtained through code like

t.CopyAt(t.Snapshot())

func (*Tree) Delete

func (t *Tree) Delete(key []byte) bool

Delete removes a value from the tree

func (*Tree) Get

func (t *Tree) Get(key []byte) []byte

Get gets a value from the tree

func (*Tree) Insert

func (t *Tree) Insert(key, value []byte)

Insert inserts a key-value pair into the trie

func (*Tree) Iterate

func (t *Tree) Iterate(fn func(k, v []byte) bool)

Iterate iterates the key-value space in sorted order

func (*Tree) Rollback

func (t *Tree) Rollback(snap int)

Rollback reverts the tree to an old snapshot state. Note that the tree can not be rolled forward; rolling back to a prior snapshot is irreversible.

func (*Tree) Snapshot

func (t *Tree) Snapshot() int

Snapshot returns a snapshot number for the current state of the tree

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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