github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/polis/polisparser_test.go (about) 1 // +build unittest 2 3 package polis 4 5 import ( 6 "blockbook/bchain" 7 "blockbook/bchain/coins/btc" 8 "bytes" 9 "encoding/hex" 10 "fmt" 11 "github.com/martinboehm/btcutil/chaincfg" 12 "io/ioutil" 13 "math/big" 14 "os" 15 "path/filepath" 16 "reflect" 17 "testing" 18 ) 19 20 type testBlock struct { 21 size int 22 time int64 23 txs []string 24 } 25 26 var testParseBlockTxs = map[int]testBlock{ 27 // Simple POW block 28 50000: { 29 size: 1393, 30 time: 1520175937, 31 txs: []string{ 32 "b68057244d6ad2df0017bad1cd8a24487e21404b52873c59876a484c93f5a69e", 33 "3d27b82972196ecce604c2923bc9105cd683352fd56f8ba052dee57a296a2d71", 34 "d22704ddad675d652a6b501694d9e026f19b41842946ea285490315cb944c674", 35 "7be2e53414c9480ea1590e40cfa8361ac7594f7435f919a6eeff96367b0dffa9", 36 "e2486a9610698888c4baad7001385e95aca053ab9fc7cc9d15280c9c835c975c", 37 }, 38 }, 39 // Simple POS block 40 280000: { 41 size: 275, 42 time: 1549070495, 43 txs: []string{ 44 "9a820cb226364e852ec5d13bc3ead1ad127bf28ef2808919571200a1262b46b5", 45 "fcca99e281fa0c43085dfe82c24b4367ff21d3a148539e781c061fe29a793ab1", 46 }, 47 }, 48 } 49 50 func TestMain(m *testing.M) { 51 c := m.Run() 52 chaincfg.ResetParams() 53 os.Exit(c) 54 } 55 56 func helperLoadBlock(t *testing.T, height int) []byte { 57 name := fmt.Sprintf("block_dump.%d", height) 58 path := filepath.Join("testdata", name) 59 60 d, err := ioutil.ReadFile(path) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 d = bytes.TrimSpace(d) 66 67 b := make([]byte, hex.DecodedLen(len(d))) 68 _, err = hex.Decode(b, d) 69 if err != nil { 70 t.Fatal(err) 71 } 72 73 return b 74 } 75 76 func TestParseBlock(t *testing.T) { 77 p := NewPolisParser(GetChainParams("main"), &btc.Configuration{}) 78 79 for height, tb := range testParseBlockTxs { 80 b := helperLoadBlock(t, height) 81 82 blk, err := p.ParseBlock(b) 83 if err != nil { 84 t.Errorf("ParseBlock() error %v", err) 85 } 86 87 if blk.Size != tb.size { 88 t.Errorf("ParseBlock() block size: got %d, want %d", blk.Size, tb.size) 89 } 90 91 if blk.Time != tb.time { 92 t.Errorf("ParseBlock() block time: got %d, want %d", blk.Time, tb.time) 93 } 94 95 if len(blk.Txs) != len(tb.txs) { 96 t.Errorf("ParseBlock() number of transactions: got %d, want %d", len(blk.Txs), len(tb.txs)) 97 } 98 99 for ti, tx := range tb.txs { 100 if blk.Txs[ti].Txid != tx { 101 t.Errorf("ParseBlock() transaction %d: got %s, want %s", ti, blk.Txs[ti].Txid, tx) 102 } 103 } 104 } 105 } 106 107 func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) { 108 type args struct { 109 address string 110 } 111 tests := []struct { 112 name string 113 args args 114 want string 115 wantErr bool 116 }{ 117 { 118 name: "P2PKH1", 119 args: args{address: "P9hRjWq6tMqhroxswc2f5jp2ND2py8YEnu"}, 120 want: "76a9140c26ca7967e6fe946f00bf81bcd3b86f43538edf88ac", 121 wantErr: false, 122 }, 123 } 124 parser := NewPolisParser(GetChainParams("main"), &btc.Configuration{}) 125 126 for _, tt := range tests { 127 t.Run(tt.name, func(t *testing.T) { 128 got, err := parser.GetAddrDescFromAddress(tt.args.address) 129 if (err != nil) != tt.wantErr { 130 t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) 131 return 132 } 133 h := hex.EncodeToString(got) 134 if !reflect.DeepEqual(h, tt.want) { 135 t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want) 136 } 137 }) 138 } 139 } 140 141 func Test_GetAddressesFromAddrDesc(t *testing.T) { 142 type args struct { 143 script string 144 } 145 tests := []struct { 146 name string 147 args args 148 want []string 149 want2 bool 150 wantErr bool 151 }{ 152 { 153 name: "P2PKH1", 154 args: args{script: "76a9140c26ca7967e6fe946f00bf81bcd3b86f43538edf88ac"}, 155 want: []string{"P9hRjWq6tMqhroxswc2f5jp2ND2py8YEnu"}, 156 want2: true, 157 wantErr: false, 158 }, 159 } 160 161 parser := NewPolisParser(GetChainParams("main"), &btc.Configuration{}) 162 163 for _, tt := range tests { 164 t.Run(tt.name, func(t *testing.T) { 165 b, _ := hex.DecodeString(tt.args.script) 166 got, got2, err := parser.GetAddressesFromAddrDesc(b) 167 if (err != nil) != tt.wantErr { 168 t.Errorf("GetAddressesFromAddrDesc() error = %v, wantErr %v", err, tt.wantErr) 169 return 170 } 171 if !reflect.DeepEqual(got, tt.want) { 172 t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want) 173 } 174 if !reflect.DeepEqual(got2, tt.want2) { 175 t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2) 176 } 177 }) 178 } 179 } 180 181 var ( 182 testTx1 bchain.Tx 183 testTxPacked1 = "0004e3868bca91b06e020000000198160d0ba0168003897358f1a6d2a2499a8e93dc6d341613b960ed2083de3fe0010000006b483045022100c531bd672ed3cb1a9285191ac168f1627e6075a54f5cf4a22ee8ec717b9a047f0220068241d9cc10adf1ddcc30aef4e62863d663af50371ebc639d11532ddf6e636e012102182bc5cd0a82c43c7ed9c4acc4b735e04e7f3275b43ff2514b8a1beb1feb5493feffffff021e470d8d0e0000001976a914344bf2db193190967d3b8da659a3ce2fde5f44a588acb63dd7a4300000001976a91414c343cae45bbcf7a27b8284b8c328587f6cc45588ac85e30400" 184 ) 185 186 func init() { 187 testTx1 = bchain.Tx{ 188 Hex: "020000000198160d0ba0168003897358f1a6d2a2499a8e93dc6d341613b960ed2083de3fe0010000006b483045022100c531bd672ed3cb1a9285191ac168f1627e6075a54f5cf4a22ee8ec717b9a047f0220068241d9cc10adf1ddcc30aef4e62863d663af50371ebc639d11532ddf6e636e012102182bc5cd0a82c43c7ed9c4acc4b735e04e7f3275b43ff2514b8a1beb1feb5493feffffff021e470d8d0e0000001976a914344bf2db193190967d3b8da659a3ce2fde5f44a588acb63dd7a4300000001976a91414c343cae45bbcf7a27b8284b8c328587f6cc45588ac85e30400", 189 Blocktime: 1554132023, 190 Txid: "6882e77c916c5442d09e295b88fbb8a2fac6dbb988975bb00dbded088e0229a9", 191 LockTime: 320389, 192 Version: 2, 193 Vin: []bchain.Vin{ 194 { 195 ScriptSig: bchain.ScriptSig{ 196 Hex: "483045022100c531bd672ed3cb1a9285191ac168f1627e6075a54f5cf4a22ee8ec717b9a047f0220068241d9cc10adf1ddcc30aef4e62863d663af50371ebc639d11532ddf6e636e012102182bc5cd0a82c43c7ed9c4acc4b735e04e7f3275b43ff2514b8a1beb1feb5493", 197 }, 198 Txid: "e03fde8320ed60b91316346ddc938e9a49a2d2a6f1587389038016a00b0d1698", 199 Vout: 1, 200 Sequence: 4294967294, 201 }, 202 }, 203 Vout: []bchain.Vout{ 204 { 205 ValueSat: *big.NewInt(62495999774), 206 N: 0, 207 ScriptPubKey: bchain.ScriptPubKey{ 208 Hex: "76a914344bf2db193190967d3b8da659a3ce2fde5f44a588ac", 209 Addresses: []string{ 210 "PDMhGxFYTaomhzSqWKHbUzx7smYUZvZVjd", 211 }, 212 }, 213 }, 214 { 215 ValueSat: *big.NewInt(208923999670), 216 N: 1, 217 ScriptPubKey: bchain.ScriptPubKey{ 218 Hex: "76a91414c343cae45bbcf7a27b8284b8c328587f6cc45588ac", 219 Addresses: []string{ 220 "PAUxb3g3DZNrjgbRidZy3NC9TNhVrPRzAR", 221 }, 222 }, 223 }, 224 }, 225 } 226 } 227 228 func Test_PackTx(t *testing.T) { 229 type args struct { 230 tx bchain.Tx 231 height uint32 232 blockTime int64 233 parser *PolisParser 234 } 235 tests := []struct { 236 name string 237 args args 238 want string 239 wantErr bool 240 }{ 241 { 242 name: "polis-1", 243 args: args{ 244 tx: testTx1, 245 height: 320390, 246 blockTime: 1554132023, 247 parser: NewPolisParser(GetChainParams("main"), &btc.Configuration{}), 248 }, 249 want: testTxPacked1, 250 wantErr: false, 251 }, 252 } 253 for _, tt := range tests { 254 t.Run(tt.name, func(t *testing.T) { 255 got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime) 256 if (err != nil) != tt.wantErr { 257 t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr) 258 return 259 } 260 h := hex.EncodeToString(got) 261 if !reflect.DeepEqual(h, tt.want) { 262 t.Errorf("packTx() = %v, want %v", h, tt.want) 263 } 264 }) 265 } 266 } 267 268 func Test_UnpackTx(t *testing.T) { 269 type args struct { 270 packedTx string 271 parser *PolisParser 272 } 273 tests := []struct { 274 name string 275 args args 276 want *bchain.Tx 277 want1 uint32 278 wantErr bool 279 }{ 280 { 281 name: "polis-1", 282 args: args{ 283 packedTx: testTxPacked1, 284 parser: NewPolisParser(GetChainParams("main"), &btc.Configuration{}), 285 }, 286 want: &testTx1, 287 want1: 320390, 288 wantErr: false, 289 }, 290 } 291 for _, tt := range tests { 292 t.Run(tt.name, func(t *testing.T) { 293 b, _ := hex.DecodeString(tt.args.packedTx) 294 got, got1, err := tt.args.parser.UnpackTx(b) 295 if (err != nil) != tt.wantErr { 296 t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr) 297 return 298 } 299 if !reflect.DeepEqual(got, tt.want) { 300 t.Errorf("unpackTx() got = %v, want %v", got, tt.want) 301 } 302 if got1 != tt.want1 { 303 t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1) 304 } 305 }) 306 } 307 }