github.com/klaytn/klaytn@v1.12.1/blockchain/types/log_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/types/log_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package types 22 23 import ( 24 "encoding/json" 25 "fmt" 26 "reflect" 27 "testing" 28 29 "github.com/davecgh/go-spew/spew" 30 "github.com/klaytn/klaytn/common" 31 "github.com/klaytn/klaytn/common/hexutil" 32 ) 33 34 var unmarshalLogTests = map[string]struct { 35 input string 36 want *Log 37 wantError error 38 }{ 39 "ok": { 40 input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x000000000000000000000000000000000000000000000001a055690d9db80000","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`, 41 want: &Log{ 42 Address: common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"), 43 BlockHash: common.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"), 44 BlockNumber: 2019236, 45 Data: hexutil.MustDecode("0x000000000000000000000000000000000000000000000001a055690d9db80000"), 46 Index: 2, 47 TxIndex: 3, 48 TxHash: common.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"), 49 Topics: []common.Hash{ 50 common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 51 common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"), 52 }, 53 }, 54 }, 55 "empty data": { 56 input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`, 57 want: &Log{ 58 Address: common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"), 59 BlockHash: common.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"), 60 BlockNumber: 2019236, 61 Data: []byte{}, 62 Index: 2, 63 TxIndex: 3, 64 TxHash: common.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"), 65 Topics: []common.Hash{ 66 common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 67 common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"), 68 }, 69 }, 70 }, 71 "missing block fields (pending logs)": { 72 input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","data":"0x","logIndex":"0x0","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`, 73 want: &Log{ 74 Address: common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"), 75 BlockHash: common.Hash{}, 76 BlockNumber: 0, 77 Data: []byte{}, 78 Index: 0, 79 TxIndex: 3, 80 TxHash: common.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"), 81 Topics: []common.Hash{ 82 common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 83 }, 84 }, 85 }, 86 "Removed: true": { 87 input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3","removed":true}`, 88 want: &Log{ 89 Address: common.HexToAddress("0xecf8f87f810ecf450940c9f60066b4a7a501d6a7"), 90 BlockHash: common.HexToHash("0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056"), 91 BlockNumber: 2019236, 92 Data: []byte{}, 93 Index: 2, 94 TxIndex: 3, 95 TxHash: common.HexToHash("0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e"), 96 Topics: []common.Hash{ 97 common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), 98 }, 99 Removed: true, 100 }, 101 }, 102 "missing data": { 103 input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`, 104 wantError: fmt.Errorf("missing required field 'data' for Log"), 105 }, 106 } 107 108 func TestUnmarshalLog(t *testing.T) { 109 dumper := spew.ConfigState{DisableMethods: true, Indent: " "} 110 for name, test := range unmarshalLogTests { 111 var log *Log 112 err := json.Unmarshal([]byte(test.input), &log) 113 checkError(t, name, err, test.wantError) 114 if test.wantError == nil && err == nil { 115 if !reflect.DeepEqual(log, test.want) { 116 t.Errorf("test %q:\nGOT %sWANT %s", name, dumper.Sdump(log), dumper.Sdump(test.want)) 117 } 118 } 119 } 120 } 121 122 func checkError(t *testing.T, testname string, got, want error) bool { 123 if got == nil { 124 if want != nil { 125 t.Errorf("test %q: got no error, want %q", testname, want) 126 return false 127 } 128 return true 129 } 130 if want == nil { 131 t.Errorf("test %q: unexpected error %q", testname, got) 132 } else if got.Error() != want.Error() { 133 t.Errorf("test %q: got error %q, want %q", testname, got, want) 134 } 135 return false 136 }