logger_test.go 3.5 KB

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