vm_test_util.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/rawdb"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/vm"
  29. "github.com/ethereum/go-ethereum/crypto"
  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, snapshotter bool) error {
  72. snaps, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter)
  73. if snapshotter {
  74. preRoot := statedb.IntermediateRoot(false)
  75. defer func() {
  76. if _, err := snaps.Journal(preRoot); err != nil {
  77. panic(err)
  78. }
  79. }()
  80. }
  81. ret, gasRemaining, err := t.exec(statedb, vmconfig)
  82. if t.json.GasRemaining == nil {
  83. if err == nil {
  84. return fmt.Errorf("gas unspecified (indicating an error), but VM returned no error")
  85. }
  86. if gasRemaining > 0 {
  87. return fmt.Errorf("gas unspecified (indicating an error), but VM returned gas remaining > 0")
  88. }
  89. return nil
  90. }
  91. // Test declares gas, expecting outputs to match.
  92. if !bytes.Equal(ret, t.json.Out) {
  93. return fmt.Errorf("return data mismatch: got %x, want %x", ret, t.json.Out)
  94. }
  95. if gasRemaining != uint64(*t.json.GasRemaining) {
  96. return fmt.Errorf("remaining gas %v, want %v", gasRemaining, *t.json.GasRemaining)
  97. }
  98. for addr, account := range t.json.Post {
  99. for k, wantV := range account.Storage {
  100. if haveV := statedb.GetState(addr, k); haveV != wantV {
  101. return fmt.Errorf("wrong storage value at %x:\n got %x\n want %x", k, haveV, wantV)
  102. }
  103. }
  104. }
  105. // if root := statedb.IntermediateRoot(false); root != t.json.PostStateRoot {
  106. // return fmt.Errorf("post state root mismatch, got %x, want %x", root, t.json.PostStateRoot)
  107. // }
  108. if logs := rlpHash(statedb.Logs()); logs != common.Hash(t.json.Logs) {
  109. return fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, t.json.Logs)
  110. }
  111. return nil
  112. }
  113. func (t *VMTest) exec(statedb *state.StateDB, vmconfig vm.Config) ([]byte, uint64, error) {
  114. evm := t.newEVM(statedb, vmconfig)
  115. e := t.json.Exec
  116. return evm.Call(vm.AccountRef(e.Caller), e.Address, e.Data, e.GasLimit, e.Value)
  117. }
  118. func (t *VMTest) newEVM(statedb *state.StateDB, vmconfig vm.Config) *vm.EVM {
  119. initialCall := true
  120. canTransfer := func(db vm.StateDB, address common.Address, amount *big.Int) bool {
  121. if initialCall {
  122. initialCall = false
  123. return true
  124. }
  125. return core.CanTransfer(db, address, amount)
  126. }
  127. transfer := func(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {}
  128. txContext := vm.TxContext{
  129. Origin: t.json.Exec.Origin,
  130. GasPrice: t.json.Exec.GasPrice,
  131. }
  132. context := vm.BlockContext{
  133. CanTransfer: canTransfer,
  134. Transfer: transfer,
  135. GetHash: vmTestBlockHash,
  136. Coinbase: t.json.Env.Coinbase,
  137. BlockNumber: new(big.Int).SetUint64(t.json.Env.Number),
  138. Time: new(big.Int).SetUint64(t.json.Env.Timestamp),
  139. GasLimit: t.json.Env.GasLimit,
  140. Difficulty: t.json.Env.Difficulty,
  141. }
  142. vmconfig.NoRecursion = true
  143. return vm.NewEVM(context, txContext, statedb, statedb, params.MainnetChainConfig, vmconfig)
  144. }
  145. func vmTestBlockHash(n uint64) common.Hash {
  146. return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
  147. }