logger_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. )
  24. type dummyContractRef struct {
  25. calledForEach bool
  26. }
  27. func (dummyContractRef) ReturnGas(*big.Int) {}
  28. func (dummyContractRef) Address() common.Address { return common.Address{} }
  29. func (dummyContractRef) Value() *big.Int { return new(big.Int) }
  30. func (dummyContractRef) SetCode(common.Hash, []byte) {}
  31. func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
  32. d.calledForEach = true
  33. }
  34. func (d *dummyContractRef) SubBalance(amount *big.Int) {}
  35. func (d *dummyContractRef) AddBalance(amount *big.Int) {}
  36. func (d *dummyContractRef) SetBalance(*big.Int) {}
  37. func (d *dummyContractRef) SetNonce(uint64) {}
  38. func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) }
  39. type dummyStatedb struct {
  40. state.StateDB
  41. }
  42. func (*dummyStatedb) GetRefund() uint64 { return 1337 }
  43. func TestStoreCapture(t *testing.T) {
  44. var (
  45. env = NewEVM(Context{}, &dummyStatedb{}, params.TestChainConfig, Config{})
  46. logger = NewStructLogger(nil)
  47. mem = NewMemory()
  48. stack = newstack()
  49. contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
  50. )
  51. stack.push(big.NewInt(1))
  52. stack.push(big.NewInt(0))
  53. var index common.Hash
  54. logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, contract, 0, nil)
  55. if len(logger.changedValues[contract.Address()]) == 0 {
  56. t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
  57. }
  58. exp := common.BigToHash(big.NewInt(1))
  59. if logger.changedValues[contract.Address()][index] != exp {
  60. t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
  61. }
  62. }