github.com/aychain/blockbook@v0.1.1-0.20181121092459-6d1fc7e07c5b/bchain/coins/eth/ethparser_test.go (about) 1 // +build unittest 2 3 package eth 4 5 import ( 6 "blockbook/bchain" 7 "encoding/hex" 8 "math/big" 9 "reflect" 10 "testing" 11 ) 12 13 func TestEthParser_GetAddrDescFromAddress(t *testing.T) { 14 type args struct { 15 address string 16 } 17 tests := []struct { 18 name string 19 args args 20 want string 21 wantErr bool 22 }{ 23 { 24 name: "with 0x prefix", 25 args: args{address: "0x81b7e08f65bdf5648606c89998a9cc8164397647"}, 26 want: "81b7e08f65bdf5648606c89998a9cc8164397647", 27 }, 28 { 29 name: "without 0x prefix", 30 args: args{address: "47526228d673e9f079630d6cdaff5a2ed13e0e60"}, 31 want: "47526228d673e9f079630d6cdaff5a2ed13e0e60", 32 }, 33 { 34 name: "odd address", 35 args: args{address: "7526228d673e9f079630d6cdaff5a2ed13e0e60"}, 36 want: "07526228d673e9f079630d6cdaff5a2ed13e0e60", 37 }, 38 { 39 name: "ErrAddressMissing", 40 args: args{address: ""}, 41 want: "", 42 wantErr: true, 43 }, 44 { 45 name: "error - not eth address", 46 args: args{address: "1JKgN43B9SyLuZH19H5ECvr4KcfrbVHzZ6"}, 47 want: "", 48 wantErr: true, 49 }, 50 } 51 for _, tt := range tests { 52 t.Run(tt.name, func(t *testing.T) { 53 p := NewEthereumParser() 54 got, err := p.GetAddrDescFromAddress(tt.args.address) 55 if (err != nil) != tt.wantErr { 56 t.Errorf("EthParser.GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) 57 return 58 } 59 h := hex.EncodeToString(got) 60 if !reflect.DeepEqual(h, tt.want) { 61 t.Errorf("EthParser.GetAddrDescFromAddress() = %v, want %v", h, tt.want) 62 } 63 }) 64 } 65 } 66 67 var ( 68 testTx1, testTx2 bchain.Tx 69 testTxPacked1 = "08aebf0a1205012a05f20018a0f73622081234567890abcdef2a24f025caaf00000000000000000000000000000000000000000000000000000000000002253220e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d38f095af014092f4c1d5054a14682b7903a11098cf770c7aef4aa02a85b3f3601a5214dacc9c61754a0c4616fc5323dc946e89eb272302580162011b6a201bd40a31122c03918df6d166d740a6a3a22f08a25934ceb1688c62977661c80c7220607fbc15c1f7995a4258f5a9bccc63b040362d1991d5efe1361c56222e4ca89f" 70 testTxPacked2 = "08ece40212050430e234001888a4012201213220cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b38889eaf0140fa83c3d5054a14555ee11fbddc0e49a9bab358a8941ad95ffdb48f52143e3a3d69dc66ba10737f531ed088954a9ec89d97580a6201296a20f7161c170d43573ad9c8d701cdaf714ff2a548a562b0dc639230d17889fcd40572203c4977fc90385a27efa0032e17b49fd575b2826cb56e3d1ecf21524f2a94f915" 71 ) 72 73 func init() { 74 75 testTx1 = bchain.Tx{ 76 Blocktime: 1521515026, 77 Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307831323334353637383930616263646566222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22626c6f636b4e756d626572223a223078326263616630222c2266726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d", 78 Time: 1521515026, 79 Txid: "0xe6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d", 80 Vin: []bchain.Vin{ 81 { 82 Addresses: []string{"0xdacc9c61754a0c4616fc5323dc946e89eb272302"}, 83 }, 84 }, 85 Vout: []bchain.Vout{ 86 { 87 ValueSat: *big.NewInt(1311768467294899695), 88 ScriptPubKey: bchain.ScriptPubKey{ 89 Addresses: []string{"0x682b7903a11098cf770c7aef4aa02a85b3f3601a"}, 90 }, 91 }, 92 }, 93 } 94 95 testTx2 = bchain.Tx{ 96 Blocktime: 1521533434, 97 Hex: "7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a2230783231222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22626c6f636b4e756d626572223a223078326263663038222c2266726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861222c2276223a2230783239222c2272223a22307866373136316331373064343335373361643963386437303163646166373134666632613534386135363262306463363339323330643137383839666364343035222c2273223a22307833633439373766633930333835613237656661303033326531376234396664353735623238323663623536653364316563663231353234663261393466393135227d", 98 Time: 1521533434, 99 Txid: "0xcd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b", 100 Vin: []bchain.Vin{ 101 { 102 Addresses: []string{"0x3e3a3d69dc66ba10737f531ed088954a9ec89d97"}, 103 }, 104 }, 105 Vout: []bchain.Vout{ 106 { 107 ValueSat: *big.NewInt(33), 108 ScriptPubKey: bchain.ScriptPubKey{ 109 Addresses: []string{"0x555ee11fbddc0e49a9bab358a8941ad95ffdb48f"}, 110 }, 111 }, 112 }, 113 } 114 } 115 116 func TestEthereumParser_PackTx(t *testing.T) { 117 type args struct { 118 tx *bchain.Tx 119 height uint32 120 blockTime int64 121 } 122 tests := []struct { 123 name string 124 p *EthereumParser 125 args args 126 want string 127 wantErr bool 128 }{ 129 { 130 name: "1", 131 args: args{ 132 tx: &testTx1, 133 height: 2870000, 134 blockTime: 1521515026, 135 }, 136 want: testTxPacked1, 137 }, 138 { 139 name: "2", 140 args: args{ 141 tx: &testTx2, 142 height: 2871048, 143 blockTime: 1521533434, 144 }, 145 want: testTxPacked2, 146 }, 147 } 148 p := NewEthereumParser() 149 for _, tt := range tests { 150 t.Run(tt.name, func(t *testing.T) { 151 got, err := p.PackTx(tt.args.tx, tt.args.height, tt.args.blockTime) 152 if (err != nil) != tt.wantErr { 153 t.Errorf("EthereumParser.PackTx() error = %v, wantErr %v", err, tt.wantErr) 154 return 155 } 156 h := hex.EncodeToString(got) 157 if !reflect.DeepEqual(h, tt.want) { 158 t.Errorf("EthereumParser.PackTx() = %v, want %v", h, tt.want) 159 } 160 }) 161 } 162 } 163 164 func TestEthereumParser_UnpackTx(t *testing.T) { 165 type args struct { 166 hex string 167 } 168 tests := []struct { 169 name string 170 p *EthereumParser 171 args args 172 want *bchain.Tx 173 want1 uint32 174 wantErr bool 175 }{ 176 { 177 name: "1", 178 args: args{hex: testTxPacked1}, 179 want: &testTx1, 180 want1: 2870000, 181 }, 182 { 183 name: "2", 184 args: args{hex: testTxPacked2}, 185 want: &testTx2, 186 want1: 2871048, 187 }, 188 } 189 p := NewEthereumParser() 190 for _, tt := range tests { 191 t.Run(tt.name, func(t *testing.T) { 192 b, err := hex.DecodeString(tt.args.hex) 193 if err != nil { 194 panic(err) 195 } 196 got, got1, err := p.UnpackTx(b) 197 if (err != nil) != tt.wantErr { 198 t.Errorf("EthereumParser.UnpackTx() error = %v, wantErr %v", err, tt.wantErr) 199 return 200 } 201 if !reflect.DeepEqual(got, tt.want) { 202 t.Errorf("EthereumParser.UnpackTx() got = %v, want %v", got, tt.want) 203 } 204 if got1 != tt.want1 { 205 t.Errorf("EthereumParser.UnpackTx() got1 = %v, want %v", got1, tt.want1) 206 } 207 }) 208 } 209 }