logger_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2016 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/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/state"
  22. "github.com/ethereum/go-ethereum/params"
  23. "github.com/holiman/uint256"
  24. )
  25. type dummyContractRef struct {
  26. calledForEach bool
  27. }
  28. func (dummyContractRef) ReturnGas(*big.Int) {}
  29. func (dummyContractRef) Address() common.Address { return common.Address{} }
  30. func (dummyContractRef) Value() *big.Int { return new(big.Int) }
  31. func (dummyContractRef) SetCode(common.Hash, []byte) {}
  32. func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
  33. d.calledForEach = true
  34. }
  35. func (d *dummyContractRef) SubBalance(amount *big.Int) {}
  36. func (d *dummyContractRef) AddBalance(amount *big.Int) {}
  37. func (d *dummyContractRef) SetBalance(*big.Int) {}
  38. func (d *dummyContractRef) SetNonce(uint64) {}
  39. func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) }
  40. type dummyStatedb struct {
  41. state.StateDB
  42. }
  43. func (*dummyStatedb) GetRefund() uint64 { return 1337 }
  44. func TestStoreCapture(t *testing.T) {
  45. var (
  46. env = NewEVM(BlockContext{}, TxContext{}, &dummyStatedb{}, &dummyStatedb{}, params.TestChainConfig, Config{})
  47. logger = NewStructLogger(nil)
  48. contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
  49. scope = &ScopeContext{
  50. Memory: NewMemory(),
  51. Stack: newstack(),
  52. Contract: contract,
  53. }
  54. )
  55. scope.Stack.push(uint256.NewInt(1))
  56. scope.Stack.push(new(uint256.Int))
  57. var index common.Hash
  58. logger.CaptureState(env, 0, SSTORE, 0, 0, scope, nil, 0, nil)
  59. if len(logger.storage[contract.Address()]) == 0 {
  60. t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(),
  61. len(logger.storage[contract.Address()]))
  62. }
  63. exp := common.BigToHash(big.NewInt(1))
  64. if logger.storage[contract.Address()][index] != exp {
  65. t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
  66. }
  67. }