cmd/ewasm: Tweak the way wasm files are executed

This can now be used to test code metering
by running the sentinel contract
This commit is contained in:
Guillaume Ballet 2018-10-21 15:44:32 +02:00
parent b2482f5a4a
commit 8fb0c4b53d

View file

@ -25,6 +25,7 @@ import (
"os" "os"
"strconv" "strconv"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
@ -59,12 +60,30 @@ func runCmd(ctx *cli.Context) error {
if args.Present() { if args.Present() {
filename := args.First() filename := args.First()
input := args.Get(1) inputStr := args.Get(1)
gas, err := strconv.ParseUint(args.Get(2), 10, 64) gas, err := strconv.ParseUint(args.Get(2), 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("Error parsing gas number: %v", err) return fmt.Errorf("Error parsing gas number: %v", err)
} }
// Convert the input
input := []byte{}
if inputStr[0:2] != "0x" {
return fmt.Errorf("Invalid input, it should be a hexadecimal number starting with 0x")
}
inputStr = inputStr[2:]
if len(inputStr)%2 == 1 {
inputStr = "0" + inputStr
}
for inputStr != "" {
x, err := strconv.ParseUint(inputStr[0:2], 16, 8)
if err != nil {
return fmt.Errorf("Invalid byte in input")
}
input = append(input, byte(x))
inputStr = inputStr[2:]
}
if fd, err := os.Open(filename); err == nil { if fd, err := os.Open(filename); err == nil {
fi, _ := fd.Stat() fi, _ := fd.Stat()
@ -84,16 +103,17 @@ func runCmd(ctx *cli.Context) error {
contract := coreVM.NewContract(coreVM.AccountRef(callerAddr), coreVM.AccountRef(contractAddr), big.NewInt(100), gas) contract := coreVM.NewContract(coreVM.AccountRef(callerAddr), coreVM.AccountRef(contractAddr), big.NewInt(100), gas)
contract.Code = code contract.Code = code
contract.Input = input
permissiveContext := coreVM.Context{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
}
evm := coreVM.NewEVM(permissiveContext, statedb, &params.ChainConfig{}, coreVM.Config{})
evm.StateDB.SetCode(contractAddr, code)
evm := coreVM.NewEVM(coreVM.Context{}, statedb, &params.ChainConfig{}, coreVM.Config{})
// &ewasm.Contract{
// StateDB: statedb,
// Gas: 100,
// Address: &contractAddr,
// CodeAddr: &codeAddr,
// Module: m,
// VM: vm,
// }
output, leftOver, err := evm.Call(coreVM.AccountRef(callerAddr), contractAddr, input, gas, big.NewInt(0)) output, leftOver, err := evm.Call(coreVM.AccountRef(callerAddr), contractAddr, input, gas, big.NewInt(0))