forked from vechain/thor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds contract tests * Adds evm tests * Add test files for memory, noop, opcodes and stack * Add gas tests * Add memory table tests * Add gas_table and interpreter tests * Renames packages and handles return value * Removes unused function * Fixes linter problems * Implement Tony's suggestions * simplify noop tracer --------- Co-authored-by: Makis Christou <[email protected]> Co-authored-by: tony <[email protected]>
- Loading branch information
1 parent
d1dcdea
commit 45d9e89
Showing
10 changed files
with
1,175 additions
and
2 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,159 @@ | ||
package vm | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/params" | ||
"github.com/holiman/uint256" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var _ Logger = (*noopTracer)(nil) | ||
|
||
type noopTracer struct{} | ||
|
||
func (t *noopTracer) CaptureStart(env *EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { | ||
} | ||
func (t *noopTracer) CaptureEnd(output []byte, gasUsed uint64, err error) { | ||
} | ||
func (t *noopTracer) CaptureState(pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, rData []byte, depth int, err error) { | ||
} | ||
func (t *noopTracer) CaptureFault(pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) { | ||
} | ||
func (t *noopTracer) CaptureEnter(typ OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { | ||
} | ||
func (t *noopTracer) CaptureExit(output []byte, gasUsed uint64, err error) { | ||
} | ||
func (*noopTracer) CaptureClauseStart(gasLimit uint64) {} | ||
func (*noopTracer) CaptureClauseEnd(restGas uint64) {} | ||
|
||
func setupEvmTestContract(codeAddr *common.Address) (*EVM, *Contract) { | ||
statedb := NoopStateDB{} | ||
|
||
evmConfig := Config{ | ||
Tracer: &noopTracer{}, | ||
} | ||
|
||
evm := NewEVM( | ||
Context{ | ||
BlockNumber: big.NewInt(1), | ||
GasPrice: big.NewInt(1), | ||
CanTransfer: NoopCanTransfer, | ||
Transfer: NoopTransfer, | ||
NewContractAddress: newContractAddress, | ||
}, | ||
statedb, | ||
&ChainConfig{ChainConfig: *params.TestChainConfig}, evmConfig) | ||
|
||
contract := &Contract{ | ||
CallerAddress: common.HexToAddress("0x01"), | ||
Code: []byte{0x60, 0x02, 0x5b, 0x00}, | ||
CodeHash: common.HexToHash("somehash"), | ||
CodeAddr: codeAddr, | ||
Gas: 500000, | ||
DelegateCall: true, | ||
} | ||
|
||
contractCode := []byte{0x60, 0x00} | ||
contract.SetCode(common.BytesToHash(contractCode), contractCode) | ||
|
||
return evm, contract | ||
} | ||
|
||
func TestCall(t *testing.T) { | ||
codeAddr := common.BytesToAddress([]byte{1}) | ||
evm, _ := setupEvmTestContract(&codeAddr) | ||
|
||
caller := AccountRef(common.HexToAddress("0x01")) | ||
contractAddr := common.HexToAddress("0x1") | ||
input := []byte{} | ||
|
||
ret, leftOverGas, err := evm.Call(caller, contractAddr, input, 1000000, big.NewInt(100000)) | ||
|
||
assert.Nil(t, err) | ||
assert.Nil(t, ret) | ||
assert.NotNil(t, leftOverGas) | ||
} | ||
|
||
func TestCallCode(t *testing.T) { | ||
codeAddr := common.BytesToAddress([]byte{1}) | ||
evm, _ := setupEvmTestContract(&codeAddr) | ||
|
||
caller := AccountRef(common.HexToAddress("0x01")) | ||
contractAddr := common.HexToAddress("0x1") | ||
input := []byte{} | ||
|
||
ret, leftOverGas, err := evm.CallCode(caller, contractAddr, input, 1000000, big.NewInt(100000)) | ||
|
||
assert.Nil(t, err) | ||
assert.Nil(t, ret) | ||
assert.NotNil(t, leftOverGas) | ||
} | ||
|
||
func TestDelegateCall(t *testing.T) { | ||
codeAddr := common.BytesToAddress([]byte{1}) | ||
evm, _ := setupEvmTestContract(&codeAddr) | ||
|
||
parentCallerAddress := common.HexToAddress("0x01") | ||
objectAddress := common.HexToAddress("0x03") | ||
input := []byte{} | ||
|
||
parentContract := NewContract(AccountRef(parentCallerAddress), AccountRef(parentCallerAddress), big.NewInt(2000), 5000) | ||
childContract := NewContract(parentContract, AccountRef(objectAddress), big.NewInt(2000), 5000) | ||
|
||
ret, leftOverGas, err := evm.DelegateCall(childContract, parentContract.CallerAddress, input, 1000000) | ||
|
||
assert.Nil(t, err) | ||
assert.Nil(t, ret) | ||
assert.NotNil(t, leftOverGas) | ||
} | ||
|
||
func TestStaticCall(t *testing.T) { | ||
codeAddr := common.BytesToAddress([]byte{1}) | ||
evm, _ := setupEvmTestContract(&codeAddr) | ||
|
||
parentCallerAddress := common.HexToAddress("0x01") | ||
objectAddress := common.HexToAddress("0x03") | ||
input := []byte{} | ||
|
||
parentContract := NewContract(AccountRef(parentCallerAddress), AccountRef(parentCallerAddress), big.NewInt(2000), 5000) | ||
childContract := NewContract(parentContract, AccountRef(objectAddress), big.NewInt(2000), 5000) | ||
|
||
ret, leftOverGas, err := evm.StaticCall(childContract, parentContract.CallerAddress, input, 1000000) | ||
|
||
assert.Nil(t, err) | ||
assert.Nil(t, ret) | ||
assert.NotNil(t, leftOverGas) | ||
} | ||
|
||
func TestCreate(t *testing.T) { | ||
codeAddr := common.BytesToAddress([]byte{1}) | ||
evm, _ := setupEvmTestContract(&codeAddr) | ||
|
||
parentCallerAddress := common.HexToAddress("0x01234567A") | ||
input := []byte{} | ||
|
||
ret, addr, leftOverGas, err := evm.Create(AccountRef(parentCallerAddress), input, 1000000, big.NewInt(2000)) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, addr) | ||
assert.Nil(t, ret) | ||
assert.NotNil(t, leftOverGas) | ||
} | ||
|
||
func TestCreate2(t *testing.T) { | ||
codeAddr := common.BytesToAddress([]byte{1}) | ||
evm, _ := setupEvmTestContract(&codeAddr) | ||
|
||
parentCallerAddress := common.HexToAddress("0x01234567A") | ||
input := []byte{} | ||
|
||
ret, addr, leftOverGas, err := evm.Create2(AccountRef(parentCallerAddress), input, 10000, big.NewInt(2000), uint256.NewInt(10000)) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, addr) | ||
assert.Nil(t, ret) | ||
assert.NotNil(t, leftOverGas) | ||
} |
Oops, something went wrong.