logger_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 (d dummyEnv) AddStructLog(StructLog) {}
  51. func TestStoreCapture(t *testing.T) {
  52. var (
  53. env = NewEnv(true, false)
  54. logger = newLogger(LogConfig{Collector: env}, env)
  55. mem = NewMemory()
  56. stack = newstack()
  57. contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), new(big.Int), new(big.Int))
  58. )
  59. stack.push(big.NewInt(1))
  60. stack.push(big.NewInt(0))
  61. var index common.Hash
  62. logger.captureState(0, SSTORE, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
  63. if len(logger.changedValues[contract.Address()]) == 0 {
  64. t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
  65. }
  66. exp := common.BigToHash(big.NewInt(1))
  67. if logger.changedValues[contract.Address()][index] != exp {
  68. t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
  69. }
  70. }
  71. func TestStorageCapture(t *testing.T) {
  72. 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")
  73. var (
  74. ref = &dummyContractRef{}
  75. contract = NewContract(ref, ref, new(big.Int), new(big.Int), new(big.Int))
  76. env = newDummyEnv(ref)
  77. logger = newLogger(LogConfig{Collector: env}, env)
  78. mem = NewMemory()
  79. stack = newstack()
  80. )
  81. logger.captureState(0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
  82. if ref.calledForEach {
  83. t.Error("didn't expect for each to be called")
  84. }
  85. logger = newLogger(LogConfig{Collector: env, FullStorage: true}, env)
  86. logger.captureState(0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
  87. if !ref.calledForEach {
  88. t.Error("expected for each to be called")
  89. }
  90. }