core/vm: fix some issues with metering

This commit is contained in:
Guillaume Ballet 2018-10-09 11:05:18 +02:00
parent be16aabe6f
commit c4b48d1196
3 changed files with 13 additions and 10 deletions

View file

@ -24,10 +24,8 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/go-interpreter/wagon/wasm" "github.com/go-interpreter/wagon/wasm"
@ -78,11 +76,9 @@ func NewEWASMInterpreter(evm *EVM, cfg Config) Interpreter {
var meteringContract *Contract var meteringContract *Contract
if metering { if metering {
meteringContractAddress := common.HexToAddress("0x000000000000000000000000000000000000000a") meteringContractAddress := common.HexToAddress("0x000000000000000000000000000000000000000a")
meteringContract = NewContract(nil, AccountRef(meteringContractAddress), &big.Int{}, 0)
meteringCode := evm.StateDB.GetCode(meteringContractAddress) meteringCode := evm.StateDB.GetCode(meteringContractAddress)
meteringContract.SetCallCode(&meteringContractAddress, crypto.Keccak256Hash(meteringCode), meteringCode)
module, err := wasm.ReadModule(bytes.NewReader(evm.StateDB.GetCode(meteringContractAddress)), nil) module, err := wasm.ReadModule(bytes.NewReader(meteringCode), nil)
if err != nil { if err != nil {
panic(fmt.Sprintf("Error loading the metering contract: %v", err)) panic(fmt.Sprintf("Error loading the metering contract: %v", err))
} }

View file

@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/go-interpreter/wagon/exec" "github.com/go-interpreter/wagon/exec"
"github.com/go-interpreter/wagon/wasm" "github.com/go-interpreter/wagon/wasm"
) )
@ -697,7 +698,11 @@ func sentinel(in *InterpreterEWASM, input []byte) ([]byte, error) {
in.contract = savedContract in.contract = savedContract
in.vm = savedVM in.vm = savedVM
}() }()
meteringContractAddress := common.HexToAddress("0x000000000000000000000000000000000000000a")
meteringCode := in.StateDB.GetCode(meteringContractAddress)
in.contract = NewContract(in.contract, AccountRef(meteringContractAddress), &big.Int{}, 0)
in.contract = in.meteringContract in.contract = in.meteringContract
in.contract.SetCallCode(&meteringContractAddress, crypto.Keccak256Hash(meteringCode), meteringCode)
in.vm = in.meteringVM in.vm = in.meteringVM
meteredCode, err := in.meteringVM.ExecCode(0) meteredCode, err := in.meteringVM.ExecCode(0)
return meteredCode.([]byte), err return meteredCode.([]byte), err

View file

@ -151,6 +151,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
} }
ewasmOptions := make(map[string]string) ewasmOptions := make(map[string]string)
if len(config.EWASMInterpreter) > 0 {
for _, option := range strings.Split(config.EWASMInterpreter, ",") { for _, option := range strings.Split(config.EWASMInterpreter, ",") {
opt := strings.Split(option, ":") opt := strings.Split(option, ":")
if len(opt) != 2 || len(opt[0]) == 0 { if len(opt) != 2 || len(opt[0]) == 0 {
@ -158,6 +159,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
} }
ewasmOptions[opt[0]] = opt[1] ewasmOptions[opt[0]] = opt[1]
} }
}
var ( var (
vmConfig = vm.Config{ vmConfig = vm.Config{