From 8fb0c4b53d355681da4eed2e299bfe4a06929b0a Mon Sep 17 00:00:00 2001 From: Guillaume Ballet Date: Sun, 21 Oct 2018 15:44:32 +0200 Subject: [PATCH] cmd/ewasm: Tweak the way wasm files are executed This can now be used to test code metering by running the sentinel contract --- cmd/ewasm/main.go | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/cmd/ewasm/main.go b/cmd/ewasm/main.go index 5440c57ff2..e67de3f526 100644 --- a/cmd/ewasm/main.go +++ b/cmd/ewasm/main.go @@ -25,6 +25,7 @@ import ( "os" "strconv" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/cmd/utils" @@ -59,12 +60,30 @@ func runCmd(ctx *cli.Context) error { if args.Present() { filename := args.First() - input := args.Get(1) + inputStr := args.Get(1) gas, err := strconv.ParseUint(args.Get(2), 10, 64) if err != nil { 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 { 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.Code = code + contract.Input = input + + permissiveContext := coreVM.Context{ + CanTransfer: core.CanTransfer, + Transfer: core.Transfer, + } + + evm := coreVM.NewEVM(permissiveContext, statedb, ¶ms.ChainConfig{}, coreVM.Config{}) + + evm.StateDB.SetCode(contractAddr, code) - evm := coreVM.NewEVM(coreVM.Context{}, statedb, ¶ms.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))