logger_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2021 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 logger
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "math/big"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/state"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. type dummyContractRef struct {
  28. calledForEach bool
  29. }
  30. func (dummyContractRef) Address() common.Address { return common.Address{} }
  31. func (dummyContractRef) Value() *big.Int { return new(big.Int) }
  32. func (dummyContractRef) SetCode(common.Hash, []byte) {}
  33. func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
  34. d.calledForEach = true
  35. }
  36. func (d *dummyContractRef) SubBalance(amount *big.Int) {}
  37. func (d *dummyContractRef) AddBalance(amount *big.Int) {}
  38. func (d *dummyContractRef) SetBalance(*big.Int) {}
  39. func (d *dummyContractRef) SetNonce(uint64) {}
  40. func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) }
  41. type dummyStatedb struct {
  42. state.StateDB
  43. }
  44. func (*dummyStatedb) GetRefund() uint64 { return 1337 }
  45. func (*dummyStatedb) GetState(_ common.Address, _ common.Hash) common.Hash { return common.Hash{} }
  46. func (*dummyStatedb) SetState(_ common.Address, _ common.Hash, _ common.Hash) {}
  47. func TestStoreCapture(t *testing.T) {
  48. var (
  49. logger = NewStructLogger(nil)
  50. env = vm.NewEVM(vm.BlockContext{}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: logger})
  51. contract = vm.NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 100000)
  52. )
  53. contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)}
  54. var index common.Hash
  55. logger.CaptureStart(env, common.Address{}, contract.Address(), false, nil, 0, nil)
  56. _, err := env.Interpreter().Run(contract, []byte{}, false)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if len(logger.storage[contract.Address()]) == 0 {
  61. t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(),
  62. len(logger.storage[contract.Address()]))
  63. }
  64. exp := common.BigToHash(big.NewInt(1))
  65. if logger.storage[contract.Address()][index] != exp {
  66. t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
  67. }
  68. }
  69. // Tests that blank fields don't appear in logs when JSON marshalled, to reduce
  70. // logs bloat and confusion. See https://github.com/ethereum/go-ethereum/issues/24487
  71. func TestStructLogMarshalingOmitEmpty(t *testing.T) {
  72. tests := []struct {
  73. name string
  74. log *StructLog
  75. want string
  76. }{
  77. {"empty err and no fields", &StructLog{},
  78. `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
  79. {"with err", &StructLog{Err: fmt.Errorf("this failed")},
  80. `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP","error":"this failed"}`},
  81. {"with mem", &StructLog{Memory: make([]byte, 2), MemorySize: 2},
  82. `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memory":"0x0000","memSize":2,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
  83. {"with 0-size mem", &StructLog{Memory: make([]byte, 0)},
  84. `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
  85. }
  86. for _, tt := range tests {
  87. t.Run(tt.name, func(t *testing.T) {
  88. blob, err := json.Marshal(tt.log)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. if have, want := string(blob), tt.want; have != want {
  93. t.Fatalf("mismatched results\n\thave: %v\n\twant: %v", have, want)
  94. }
  95. })
  96. }
  97. }