logger_test.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/params"
  22. )
  23. type dummyContractRef struct {
  24. calledForEach bool
  25. }
  26. func (dummyContractRef) ReturnGas(*big.Int) {}
  27. func (dummyContractRef) Address() common.Address { return common.Address{} }
  28. func (dummyContractRef) Value() *big.Int { return new(big.Int) }
  29. func (dummyContractRef) SetCode(common.Hash, []byte) {}
  30. func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
  31. d.calledForEach = true
  32. }
  33. func (d *dummyContractRef) SubBalance(amount *big.Int) {}
  34. func (d *dummyContractRef) AddBalance(amount *big.Int) {}
  35. func (d *dummyContractRef) SetBalance(*big.Int) {}
  36. func (d *dummyContractRef) SetNonce(uint64) {}
  37. func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) }
  38. type dummyStateDB struct {
  39. NoopStateDB
  40. ref *dummyContractRef
  41. }
  42. func (d dummyStateDB) GetAccount(common.Address) Account {
  43. return d.ref
  44. }
  45. func TestStoreCapture(t *testing.T) {
  46. var (
  47. env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
  48. logger = NewStructLogger(nil)
  49. mem = NewMemory()
  50. stack = newstack()
  51. contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), new(big.Int))
  52. )
  53. stack.push(big.NewInt(1))
  54. stack.push(big.NewInt(0))
  55. var index common.Hash
  56. logger.CaptureState(env, 0, SSTORE, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
  57. if len(logger.changedValues[contract.Address()]) == 0 {
  58. t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
  59. }
  60. exp := common.BigToHash(big.NewInt(1))
  61. if logger.changedValues[contract.Address()][index] != exp {
  62. t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
  63. }
  64. }
  65. func TestStorageCapture(t *testing.T) {
  66. t.Skip("implementing this function is difficult. it requires all sort of interfaces to be implemented which isn't trivial. The value (the actual test) isn't worth it")
  67. var (
  68. ref = &dummyContractRef{}
  69. contract = NewContract(ref, ref, new(big.Int), new(big.Int))
  70. env = NewEVM(Context{}, dummyStateDB{ref: ref}, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
  71. logger = NewStructLogger(nil)
  72. mem = NewMemory()
  73. stack = newstack()
  74. )
  75. logger.CaptureState(env, 0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
  76. if ref.calledForEach {
  77. t.Error("didn't expect for each to be called")
  78. }
  79. logger = NewStructLogger(&LogConfig{FullStorage: true})
  80. logger.CaptureState(env, 0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
  81. if !ref.calledForEach {
  82. t.Error("expected for each to be called")
  83. }
  84. }