1

I am going to run the virtual ethereum machine from the code, pass to it a compiled smart contract code.

But I do not know how to do it.

It's my code: package main

import (
    "context"

    "github.com/ethereum/go-ethereum/core/vm"
)

func main() {
    var code string = "6060604052600a8060106000396000f360606040526008565b00"
    vm := vm.NewEVM(context.Background())

}

To create an instance of a virtual machine

vm := vm.NewEVM()

I have to pass parameters such as:

- ctx Context 
- statedb StateDB
- chainConfig *params.ChainConfig
- vmConfig Config

Where to get these parameters?

  • 1
    Did you consider to use the "evm" utility of geth (github.com/ethereum/go-ethereum)? It is mentioned in the README.md. Once you install this utility you can run your code with the following command: evm --code 60ff60ff --debug – Briomkez Jun 12 '18 at 19:36
  • Yes I know about it but I would just like to do the same from the code. I can not find anywhere how to do it. – EricEnticman Jun 12 '18 at 19:41
  • Ah, ok. I think that the best place to get some insight about these data structures is the go ethereum code base itself. In particular I suggest you to take a look to the file core/vm/evm.go Here you can find the definition of the struct context and understand what is needed. – Briomkez Jun 12 '18 at 19:49
1

1) Context is created this way:

context := NewEVMContext(msg, header, bc, author)

Function definition is located in core/evm.go:

// NewEVMContext creates a new context for use in the EVM.
func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author *common.Address) vm.Context {

2) You get StateDB like this:

statedb, err := blockchain.StateAt(block.Root())
if err != nil {
    log.Error("Can't get StateAt() , probably I am not running on a full node","block_num",block.Number().Uint64())
    return  err
}

Where blockchain is core.Blockchain object and block is a types.Block object

3) ChainConfig is created in params/config.go

MainnetChainConfig = &ChainConfig{
    ChainId:        big.NewInt(1),
    HomesteadBlock: big.NewInt(1150000),
    DAOForkBlock:   big.NewInt(1920000),
    DAOForkSupport: true,
    EIP150Block:    big.NewInt(2463000),
    EIP150Hash:     common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
    EIP155Block:    big.NewInt(2675000),
    EIP158Block:    big.NewInt(2675000),
    ByzantiumBlock: big.NewInt(4370000),

    Ethash: new(EthashConfig),
}

4) vm config is created like this:

    vm_cfg:=&vm.Config{
        Debug: false,
        EnableJit: false,
        ForceJit: false,
    }

Obviously , you have to import all these modules. Your import statement will be something like this:

import(
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/consensus/misc"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/core/vm"
    "github.com/ethereum/go-ethereum/eth"
    "github.com/ethereum/go-ethereum/log"
    "github.com/ethereum/go-ethereum/params"
    "github.com/ethereum/go-ethereum/cmd/utils"
}

The cmd/evm directory of Ethereum's geth distribution contains example code that you need to run an arbitrary EVM assembly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.