log_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "encoding/json"
  19. "reflect"
  20. "testing"
  21. "github.com/davecgh/go-spew/spew"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/hexutil"
  24. )
  25. var unmarshalLogTests = map[string]struct {
  26. input string
  27. want *Log
  28. wantError error
  29. }{
  30. "ok": {
  31. input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x000000000000000000000000000000000000000000000001a055690d9db80000","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
  32. want: &Log{
  33. Address: common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"),
  34. BlockHash: common.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"),
  35. BlockNumber: 2019236,
  36. Data: hexutil.MustDecode("0x000000000000000000000000000000000000000000000001a055690d9db80000"),
  37. Index: 2,
  38. TxIndex: 3,
  39. TxHash: common.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"),
  40. Topics: []common.Hash{
  41. common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
  42. common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"),
  43. },
  44. },
  45. },
  46. "empty data": {
  47. input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
  48. want: &Log{
  49. Address: common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"),
  50. BlockHash: common.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"),
  51. BlockNumber: 2019236,
  52. Data: []byte{},
  53. Index: 2,
  54. TxIndex: 3,
  55. TxHash: common.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"),
  56. Topics: []common.Hash{
  57. common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
  58. common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"),
  59. },
  60. },
  61. },
  62. "missing block fields (pending logs)": {
  63. input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","data":"0x","logIndex":"0x0","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
  64. want: &Log{
  65. Address: common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"),
  66. BlockHash: common.Hash{},
  67. BlockNumber: 0,
  68. Data: []byte{},
  69. Index: 0,
  70. TxIndex: 3,
  71. TxHash: common.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"),
  72. Topics: []common.Hash{
  73. common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
  74. },
  75. },
  76. },
  77. "Removed: true": {
  78. input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3","removed":true}`,
  79. want: &Log{
  80. Address: common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"),
  81. BlockHash: common.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"),
  82. BlockNumber: 2019236,
  83. Data: []byte{},
  84. Index: 2,
  85. TxIndex: 3,
  86. TxHash: common.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"),
  87. Topics: []common.Hash{
  88. common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
  89. },
  90. Removed: true,
  91. },
  92. },
  93. "missing data": {
  94. input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
  95. wantError: errMissingLogFields,
  96. },
  97. }
  98. func TestUnmarshalLog(t *testing.T) {
  99. dumper := spew.ConfigState{DisableMethods: true, Indent: " "}
  100. for name, test := range unmarshalLogTests {
  101. var log *Log
  102. err := json.Unmarshal([]byte(test.input), &log)
  103. checkError(t, name, err, test.wantError)
  104. if test.wantError == nil && err == nil {
  105. if !reflect.DeepEqual(log, test.want) {
  106. t.Errorf("test %q:\nGOT %sWANT %s", name, dumper.Sdump(log), dumper.Sdump(test.want))
  107. }
  108. }
  109. }
  110. }
  111. func checkError(t *testing.T, testname string, got, want error) bool {
  112. if got == nil {
  113. if want != nil {
  114. t.Errorf("test %q: got no error, want %q", testname, want)
  115. return false
  116. }
  117. return true
  118. }
  119. if want == nil {
  120. t.Errorf("test %q: unexpected error %q", testname, got)
  121. } else if got.Error() != want.Error() {
  122. t.Errorf("test %q: got error %q, want %q", testname, got, want)
  123. }
  124. return false
  125. }