-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from ethereum/go-txcontext
go: Combine tx context into TxContext struct
- Loading branch information
Showing
8 changed files
with
174 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,49 @@ | ||
// EVMC: Ethereum Client-VM Connector API. | ||
// Copyright 2018 The EVMC Authors. | ||
// Licensed under the Apache License, Version 2.0. See the LICENSE file. | ||
// Copyright 2019 The EVMC Authors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
//go:generate gcc -shared ../../../examples/example_vm.c -I../../../include -o example_vm.so | ||
|
||
package evmc | ||
|
||
import ( | ||
"os" | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
var modulePath = "./example_vm.so" | ||
|
||
func TestLoad(t *testing.T) { | ||
i, err := Load(os.Getenv("EVMC_PATH")) | ||
i, err := Load(modulePath) | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
defer i.Destroy() | ||
if i.Name() != "interpreter" { | ||
t.Fatal("name is not 'interpreter'") | ||
if i.Name() != "example_vm" { | ||
t.Fatalf("name is %s", i.Name()) | ||
} | ||
if i.Version()[0] < '0' || i.Version()[0] > '9' { | ||
t.Fatalf("version number is weird: %s", i.Version()) | ||
} | ||
} | ||
|
||
func TestExecute(t *testing.T) { | ||
vm, _ := Load(modulePath) | ||
defer vm.Destroy() | ||
|
||
addr := common.Address{} | ||
h := common.Hash{} | ||
output, gasLeft, err := vm.Execute(nil, Byzantium, Call, false, 1, 999, addr, addr, nil, h, nil, h) | ||
|
||
if bytes.Compare(output, []byte("Welcome to Byzantium!")) != 0 { | ||
t.Errorf("execution unexpected output: %s", output) | ||
} | ||
if gasLeft != 99 { | ||
t.Error("execution gas left is incorrect") | ||
} | ||
if i.Version()[0] != '1' { | ||
t.Fatalf("version is %s", i.Version()) | ||
if err != Failure { | ||
t.Error("execution returned unexpected error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// EVMC: Ethereum Client-VM Connector API. | ||
// Copyright 2019 The EVMC Authors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
package evmc | ||
|
||
import ( | ||
"bytes" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
type testHostContext struct{} | ||
|
||
func (host *testHostContext) AccountExists(addr common.Address) bool { | ||
return false | ||
} | ||
|
||
func (host *testHostContext) GetStorage(addr common.Address, key common.Hash) common.Hash { | ||
return common.Hash{} | ||
} | ||
|
||
func (host *testHostContext) SetStorage(addr common.Address, key common.Hash, value common.Hash) (status StorageStatus) { | ||
return StorageUnchanged | ||
} | ||
|
||
func (host *testHostContext) GetBalance(addr common.Address) common.Hash { | ||
return common.Hash{} | ||
} | ||
|
||
func (host *testHostContext) GetCodeSize(addr common.Address) int { | ||
return 0 | ||
} | ||
|
||
func (host *testHostContext) GetCodeHash(addr common.Address) common.Hash { | ||
return common.Hash{} | ||
} | ||
|
||
func (host *testHostContext) GetCode(addr common.Address) []byte { | ||
return nil | ||
} | ||
|
||
func (host *testHostContext) Selfdestruct(addr common.Address, beneficiary common.Address) { | ||
} | ||
|
||
func (host *testHostContext) GetTxContext() TxContext { | ||
txContext := TxContext{} | ||
txContext.Number = 42 | ||
return txContext | ||
} | ||
|
||
func (host *testHostContext) GetBlockHash(number int64) common.Hash { | ||
return common.Hash{} | ||
} | ||
|
||
func (host *testHostContext) EmitLog(addr common.Address, topics []common.Hash, data []byte) { | ||
} | ||
|
||
func (host *testHostContext) Call(kind CallKind, | ||
destination common.Address, sender common.Address, value *big.Int, input []byte, gas int64, depth int, | ||
static bool, salt *big.Int) (output []byte, gasLeft int64, createAddr common.Address, err error) { | ||
return nil, gas, common.Address{}, nil | ||
} | ||
|
||
func TestGetTxContext(t *testing.T) { | ||
vm, _ := Load(modulePath) | ||
defer vm.Destroy() | ||
|
||
host := &testHostContext{} | ||
code := []byte("\x43\x60\x00\x52\x59\x60\x00\xf3") | ||
|
||
addr := common.Address{} | ||
h := common.Hash{} | ||
output, gasLeft, err := vm.Execute(host, Byzantium, Call, false, 1, 100, addr, addr, nil, h, code, h) | ||
|
||
if len(output) != 20 { | ||
t.Errorf("unexpected output size: %d", len(output)) | ||
} | ||
if bytes.Compare(output[0:3], []byte("42\x00")) != 0 { | ||
t.Errorf("execution unexpected output: %s", output) | ||
} | ||
if gasLeft != 50 { | ||
t.Errorf("execution gas left is incorrect: %x", gasLeft) | ||
} | ||
if err != nil { | ||
t.Error("execution returned unexpected error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters