vm_test_util.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package tests
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "math/big"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/hexutil"
  24. "github.com/ethereum/go-ethereum/common/math"
  25. "github.com/ethereum/go-ethereum/core"
  26. "github.com/ethereum/go-ethereum/core/state"
  27. "github.com/ethereum/go-ethereum/core/vm"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/params"
  31. )
  32. // VMTest checks EVM execution without block or transaction context.
  33. // See https://github.com/ethereum/tests/wiki/VM-Tests for the test format specification.
  34. type VMTest struct {
  35. json vmJSON
  36. }
  37. func (t *VMTest) UnmarshalJSON(data []byte) error {
  38. return json.Unmarshal(data, &t.json)
  39. }
  40. type vmJSON struct {
  41. Env stEnv `json:"env"`
  42. Exec vmExec `json:"exec"`
  43. Logs common.UnprefixedHash `json:"logs"`
  44. GasRemaining *math.HexOrDecimal64 `json:"gas"`
  45. Out hexutil.Bytes `json:"out"`
  46. Pre core.GenesisAlloc `json:"pre"`
  47. Post core.GenesisAlloc `json:"post"`
  48. PostStateRoot common.Hash `json:"postStateRoot"`
  49. }
  50. //go:generate gencodec -type vmExec -field-override vmExecMarshaling -out gen_vmexec.go
  51. type vmExec struct {
  52. Address common.Address `json:"address" gencodec:"required"`
  53. Caller common.Address `json:"caller" gencodec:"required"`
  54. Origin common.Address `json:"origin" gencodec:"required"`
  55. Code []byte `json:"code" gencodec:"required"`
  56. Data []byte `json:"data" gencodec:"required"`
  57. Value *big.Int `json:"value" gencodec:"required"`
  58. GasLimit uint64 `json:"gas" gencodec:"required"`
  59. GasPrice *big.Int `json:"gasPrice" gencodec:"required"`
  60. }
  61. type vmExecMarshaling struct {
  62. Address common.UnprefixedAddress
  63. Caller common.UnprefixedAddress
  64. Origin common.UnprefixedAddress
  65. Code hexutil.Bytes
  66. Data hexutil.Bytes
  67. Value *math.HexOrDecimal256
  68. GasLimit math.HexOrDecimal64
  69. GasPrice *math.HexOrDecimal256
  70. }
  71. func (t *VMTest) Run(vmconfig vm.Config) error {
  72. statedb := MakePreState(ethdb.NewMemDatabase(), t.json.Pre)
  73. ret, gasRemaining, err := t.exec(statedb, vmconfig)
  74. if t.json.GasRemaining == nil {
  75. if err == nil {
  76. return fmt.Errorf("gas unspecified (indicating an error), but VM returned no error")
  77. }
  78. if gasRemaining > 0 {
  79. return fmt.Errorf("gas unspecified (indicating an error), but VM returned gas remaining > 0")
  80. }
  81. return nil
  82. }
  83. // Test declares gas, expecting outputs to match.
  84. if !bytes.Equal(ret, t.json.Out) {
  85. return fmt.Errorf("return data mismatch: got %x, want %x", ret, t.json.Out)
  86. }
  87. if gasRemaining != uint64(*t.json.GasRemaining) {
  88. return fmt.Errorf("remaining gas %v, want %v", gasRemaining, *t.json.GasRemaining)
  89. }
  90. for addr, account := range t.json.Post {
  91. for k, wantV := range account.Storage {
  92. if haveV := statedb.GetState(addr, k); haveV != wantV {
  93. return fmt.Errorf("wrong storage value at %x:\n got %x\n want %x", k, haveV, wantV)
  94. }
  95. }
  96. }
  97. // if root := statedb.IntermediateRoot(false); root != t.json.PostStateRoot {
  98. // return fmt.Errorf("post state root mismatch, got %x, want %x", root, t.json.PostStateRoot)
  99. // }
  100. if logs := rlpHash(statedb.Logs()); logs != common.Hash(t.json.Logs) {
  101. return fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, t.json.Logs)
  102. }
  103. return nil
  104. }
  105. func (t *VMTest) exec(statedb *state.StateDB, vmconfig vm.Config) ([]byte, uint64, error) {
  106. evm := t.newEVM(statedb, vmconfig)
  107. e := t.json.Exec
  108. return evm.Call(vm.AccountRef(e.Caller), e.Address, e.Data, e.GasLimit, e.Value)
  109. }
  110. func (t *VMTest) newEVM(statedb *state.StateDB, vmconfig vm.Config) *vm.EVM {
  111. initialCall := true
  112. canTransfer := func(db vm.StateDB, address common.Address, amount *big.Int) bool {
  113. if initialCall {
  114. initialCall = false
  115. return true
  116. }
  117. return core.CanTransfer(db, address, amount)
  118. }
  119. transfer := func(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {}
  120. context := vm.Context{
  121. CanTransfer: canTransfer,
  122. Transfer: transfer,
  123. GetHash: vmTestBlockHash,
  124. Origin: t.json.Exec.Origin,
  125. Coinbase: t.json.Env.Coinbase,
  126. BlockNumber: new(big.Int).SetUint64(t.json.Env.Number),
  127. Time: new(big.Int).SetUint64(t.json.Env.Timestamp),
  128. GasLimit: t.json.Env.GasLimit,
  129. Difficulty: t.json.Env.Difficulty,
  130. GasPrice: t.json.Exec.GasPrice,
  131. }
  132. vmconfig.NoRecursion = true
  133. return vm.NewEVM(context, statedb, params.MainnetChainConfig, vmconfig)
  134. }
  135. func vmTestBlockHash(n uint64) common.Hash {
  136. return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
  137. }