Я собираюсь запустить виртуальную машину Эфириума из кода, передать ей скомпилированный код смарт-контракта.
Но я не знаю, как это сделать.
Это мой код: пакет main
import (
"context"
"github.com/ethereum/go-ethereum/core/vm"
)
func main() {
var code string = "6060604052600a8060106000396000f360606040526008565b00"
vm := vm.NewEVM(context.Background())
}
Чтобы создать экземпляр виртуальной машины
vm := vm.NewEVM()
Мне нужно передать такие параметры, как:
- ctx Context
- statedb StateDB
- chainConfig *params.ChainConfig
- vmConfig Config
Где взять эти параметры?
1) Контекст создается таким образом:
context := NewEVMContext(msg, header, bc, author)
Определение функции находится в 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) Вы получаете StateDB следующим образом:
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
}
Где blockchain
находится core.Blockchain
объект и block
является types.Block
объектом
3) ChainConfig
создается в 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_cfg:=&vm.Config{
Debug: false,
EnableJit: false,
ForceJit: false,
}
Очевидно, вам придется импортировать все эти модули. Ваше import
заявление будет примерно таким:
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"
}
Каталог дистрибутива cmd/evm
Ethereum geth
содержит пример кода, который вам понадобится для запуска произвольной сборки EVM.
Бриомкез
evm --code 60ff60ff --debug
Эрик Энтикман
Бриомкез
core/vm/evm.go
Здесь вы можете найти определение контекста структуры и понять, что нужно.