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 ¶
- type Account
- func (a *Account) AddBalance(balance uint64) error
- func (a *Account) Copy() evm.Account
- func (a *Account) GetAddress() evm.Address
- func (a *Account) GetBalance() uint64
- func (a *Account) GetCode() []byte
- func (a *Account) GetCodeHash() []byte
- func (a *Account) GetNonce() uint64
- func (a *Account) HasSuicide() bool
- func (a *Account) SetCode(code []byte)
- func (a *Account) SetNonce(nonce uint64)
- func (a *Account) SubBalance(balance uint64) error
- func (a *Account) Suicide()
- type Address
- type Blockchain
- func (bc *Blockchain) BytesToAddress(bytes []byte) evm.Address
- func (bc *Blockchain) Create2Address(caller evm.Address, salt, code []byte) evm.Address
- func (bc *Blockchain) CreateAddress(caller evm.Address, nonce uint64) evm.Address
- func (bc *Blockchain) GetBlockHash(num uint64) []byte
- func (bc *Blockchain) NewAccount(address evm.Address) evm.Account
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 (*Account) AddBalance ¶
AddBalance is the implementation of interface Note: In fact, we should avoid overflow
func (*Account) Copy ¶
Copy return a copy of an account Note: Please reimplement this if account structure changed
func (*Account) GetAddress ¶
GetAddress is the implementation of interface
func (*Account) GetBalance ¶
GetBalance is the implementation of interface
func (*Account) GetCodeHash ¶
GetCodeHash return the hash of account code, please return [32]byte, and return [32]byte{0, ..., 0} if code is empty
func (*Account) HasSuicide ¶
HasSuicide is the implementation of interface
func (*Account) SubBalance ¶
SubBalance is the implementation of interface
type Address ¶
type Address [20]byte
Address is the address
func BytesToAddress ¶
BytesToAddress convert bytes to address
func HexToAddress ¶
HexToAddress convert hex string to address, string may begin with 0x, 0X or nothing
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 ¶
Create2Address is the implementation of interface
func (*Blockchain) CreateAddress ¶
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