gas_table_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2017 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 vm
  17. import (
  18. "math"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/hexutil"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. func TestMemoryGasCost(t *testing.T) {
  28. tests := []struct {
  29. size uint64
  30. cost uint64
  31. overflow bool
  32. }{
  33. {0x1fffffffe0, 36028809887088637, false},
  34. {0x1fffffffe1, 0, true},
  35. }
  36. for i, tt := range tests {
  37. v, err := memoryGasCost(&Memory{}, tt.size)
  38. if (err == ErrGasUintOverflow) != tt.overflow {
  39. t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == ErrGasUintOverflow, tt.overflow)
  40. }
  41. if v != tt.cost {
  42. t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)
  43. }
  44. }
  45. }
  46. var eip2200Tests = []struct {
  47. original byte
  48. gaspool uint64
  49. input string
  50. used uint64
  51. refund uint64
  52. failure error
  53. }{
  54. {0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil}, // 0 -> 0 -> 0
  55. {0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil}, // 0 -> 0 -> 1
  56. {0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil}, // 0 -> 1 -> 0
  57. {0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil}, // 0 -> 1 -> 2
  58. {0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil}, // 0 -> 1 -> 1
  59. {1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil}, // 1 -> 0 -> 0
  60. {1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil}, // 1 -> 0 -> 1
  61. {1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil}, // 1 -> 0 -> 2
  62. {1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil}, // 1 -> 2 -> 0
  63. {1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil}, // 1 -> 2 -> 3
  64. {1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil}, // 1 -> 2 -> 1
  65. {1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil}, // 1 -> 2 -> 2
  66. {1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil}, // 1 -> 1 -> 0
  67. {1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil}, // 1 -> 1 -> 2
  68. {1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil}, // 1 -> 1 -> 1
  69. {0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1
  70. {1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0
  71. {1, 2306, "0x6001600055", 2306, 0, ErrOutOfGas}, // 1 -> 1 (2300 sentry + 2xPUSH)
  72. {1, 2307, "0x6001600055", 806, 0, nil}, // 1 -> 1 (2301 sentry + 2xPUSH)
  73. }
  74. func TestEIP2200(t *testing.T) {
  75. for i, tt := range eip2200Tests {
  76. address := common.BytesToAddress([]byte("contract"))
  77. statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
  78. statedb.CreateAccount(address)
  79. statedb.SetCode(address, hexutil.MustDecode(tt.input))
  80. statedb.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original}))
  81. statedb.Finalise(true) // Push the state into the "original" slot
  82. vmctx := BlockContext{
  83. CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true },
  84. Transfer: func(StateDB, common.Address, common.Address, *big.Int) {},
  85. }
  86. vmenv := NewEVM(vmctx, TxContext{}, statedb, statedb, params.AllEthashProtocolChanges, Config{ExtraEips: []int{2200}})
  87. _, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int))
  88. if err != tt.failure {
  89. t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure)
  90. }
  91. if used := tt.gaspool - gas; used != tt.used {
  92. t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used)
  93. }
  94. if refund := vmenv.StateDB.GetRefund(); refund != tt.refund {
  95. t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund)
  96. }
  97. }
  98. }