example

package
v0.0.0-...-bf5c25a Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2020 License: LGPL-3.0-or-later Imports: 3 Imported by: 0

README

Example

This folder provide an example to make use of evm.

To see the documentations of interfaces, refer to README.md.

To get more usage and test about this project, refer to package tests.

Address

a byte array of length 20 implements Address interface.

BlockChain

Struct BlockChain implements BlockChain interface.

Account

Struct Account implements Account interface.

// Account is account
type Account struct {
	addr    *Address
  // code is bytecodes if account is an contract account or nil if not
	code    []byte
  // balance stores the money this account has
	balance uint64
  // nonce is used to privide randomness for this account to create
  // new contract
	nonce   uint64
  // suicide marks if this account is suicided
	suicide bool
}

main.go

An example usage of this project.

// write your solidity files first and use solcjs tool to generate binary and // abi files
// Or use the files we provide
func main() {
  // read binary files and store bytecodes in code
  // these bytecodes are used by evm to get and deploy contract code but
  // not the contract logic itself
  // (a bit confusing but following evm convention)
	code, err := util.ReadBinFile("../sols/output/Balance_sol_Balance.bin")
	if err != nil {
		panic(err)
	}
  // generate dependency
  // including blockchain, gas and database
	bc := example.NewBlockchain()
	memoryDB := db.NewMemory(bc.NewAccount)
	var gas uint64
	gas = 10000000
  
  // new a virtual machine to deploy and run the contract
  // code generated above is used as input to evm.create to get contract code
	vm := evm.New(bc, memoryDB, &evm.Context{
		Input: code,
		Value: 0,
		Gas:   &gas,
	})
  
  // pass a random address as caller to create contract
	var caller = example.RandomAddress()
  // now code is actual contract logic and callee is contract address
	code, callee, err := vm.Create(caller)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%x\n", code)

	gas = 1000000
  // to call a function inside a contract and pass inputs to it
  // use abi.Pack(abiFiles, function name, inputs...)
	payload, err := abi.Pack("../sols/output/Balance_sol_Balance.abi", "get")
	if err != nil {
		panic(err)
	}
  // use payload generated above as input to a contract
  // and call evm.Call(caller, callee, contract code)
	output, err := evm.New(bc, memoryDB, &evm.Context{
		Input: payload,
		Value: 0,
		Gas:   &gas,
	}).Call(caller, callee, code)
	fmt.Printf("%x\n", output)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

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

Account is account

func NewAccount

func NewAccount(addr *Address) *Account

NewAccount is the constructor of Account

func (*Account) AddBalance

func (a *Account) AddBalance(balance uint64) error

AddBalance is the implementation of interface Note: In fact, we should avoid overflow

func (*Account) Copy

func (a *Account) Copy() evm.Account

Copy return a copy of an account Note: Please reimplement this if account structure changed

func (*Account) GetAddress

func (a *Account) GetAddress() evm.Address

GetAddress is the implementation of interface

func (*Account) GetBalance

func (a *Account) GetBalance() uint64

GetBalance is the implementation of interface

func (*Account) GetCode

func (a *Account) GetCode() []byte

GetCode is the implementation of interface

func (*Account) GetCodeHash

func (a *Account) GetCodeHash() []byte

GetCodeHash return the hash of account code, please return [32]byte, and return [32]byte{0, ..., 0} if code is empty

func (*Account) GetNonce

func (a *Account) GetNonce() uint64

GetNonce is the implementation of interface

func (*Account) HasSuicide

func (a *Account) HasSuicide() bool

HasSuicide is the implementation of interface

func (*Account) SetCode

func (a *Account) SetCode(code []byte)

SetCode is the implementation of interface

func (*Account) SetNonce

func (a *Account) SetNonce(nonce uint64)

SetNonce is the implementation of interface

func (*Account) SubBalance

func (a *Account) SubBalance(balance uint64) error

SubBalance is the implementation of interface

func (*Account) Suicide

func (a *Account) Suicide()

Suicide is the implementation of interface

type Address

type Address [20]byte

Address is the address

func BytesToAddress

func BytesToAddress(bytes []byte) *Address

BytesToAddress convert bytes to address

func HexToAddress

func HexToAddress(hex string) *Address

HexToAddress convert hex string to address, string may begin with 0x, 0X or nothing

func RandomAddress

func RandomAddress() *Address

RandomAddress return a random address

func ZeroAddress

func ZeroAddress() *Address

ZeroAddress return zero address

func (*Address) Bytes

func (a *Address) Bytes() []byte

Bytes is the implementation of interface

func (*Address) Copy

func (a *Address) Copy() *Address

Copy return the copy of address

type Blockchain

type Blockchain struct {
}

Blockchain is the implementation of blockchain

func NewBlockchain

func NewBlockchain() *Blockchain

NewBlockchain is the constructor of Blockchain

func (*Blockchain) BytesToAddress

func (bc *Blockchain) BytesToAddress(bytes []byte) evm.Address

BytesToAddress is the implementation of interface

func (*Blockchain) Create2Address

func (bc *Blockchain) Create2Address(caller evm.Address, salt, code []byte) evm.Address

Create2Address is the implementation of interface

func (*Blockchain) CreateAddress

func (bc *Blockchain) CreateAddress(caller evm.Address, nonce uint64) evm.Address

CreateAddress is the implementation of interface

func (*Blockchain) GetBlockHash

func (bc *Blockchain) GetBlockHash(num uint64) []byte

GetBlockHash is the implementation of interface

func (*Blockchain) NewAccount

func (bc *Blockchain) NewAccount(address evm.Address) evm.Account

NewAccount is the implementation of interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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