github.com/lbryio/lbcd@v0.22.119/btcjson/chainsvrresults_test.go (about) 1 // Copyright (c) 2014 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package btcjson_test 6 7 import ( 8 "encoding/json" 9 "reflect" 10 "testing" 11 12 "github.com/davecgh/go-spew/spew" 13 "github.com/lbryio/lbcd/btcjson" 14 "github.com/lbryio/lbcd/chaincfg/chainhash" 15 btcutil "github.com/lbryio/lbcutil" 16 ) 17 18 // TestChainSvrCustomResults ensures any results that have custom marshalling 19 // work as inteded. 20 // and unmarshal code of results are as expected. 21 func TestChainSvrCustomResults(t *testing.T) { 22 t.Parallel() 23 24 tests := []struct { 25 name string 26 result interface{} 27 expected string 28 }{ 29 { 30 name: "custom vin marshal with coinbase", 31 result: &btcjson.Vin{ 32 Coinbase: "021234", 33 Sequence: 4294967295, 34 }, 35 expected: `{"coinbase":"021234","sequence":4294967295}`, 36 }, 37 { 38 name: "custom vin marshal without coinbase", 39 result: &btcjson.Vin{ 40 Txid: "123", 41 Vout: 1, 42 ScriptSig: &btcjson.ScriptSig{ 43 Asm: "0", 44 Hex: "00", 45 }, 46 Sequence: 4294967295, 47 }, 48 expected: `{"txid":"123","vout":1,"scriptSig":{"asm":"0","hex":"00"},"sequence":4294967295}`, 49 }, 50 { 51 name: "custom vinprevout marshal with coinbase", 52 result: &btcjson.VinPrevOut{ 53 Coinbase: "021234", 54 Sequence: 4294967295, 55 }, 56 expected: `{"coinbase":"021234","sequence":4294967295}`, 57 }, 58 { 59 name: "custom vinprevout marshal without coinbase", 60 result: &btcjson.VinPrevOut{ 61 Txid: "123", 62 Vout: 1, 63 ScriptSig: &btcjson.ScriptSig{ 64 Asm: "0", 65 Hex: "00", 66 }, 67 PrevOut: &btcjson.PrevOut{ 68 Addresses: []string{"addr1"}, 69 Value: 0, 70 }, 71 Sequence: 4294967295, 72 }, 73 expected: `{"txid":"123","vout":1,"scriptSig":{"asm":"0","hex":"00"},"prevOut":{"addresses":["addr1"],"value":0,"isclaim":false,"issupport":false},"sequence":4294967295}`, 74 }, 75 } 76 77 t.Logf("Running %d tests", len(tests)) 78 for i, test := range tests { 79 marshalled, err := json.Marshal(test.result) 80 if err != nil { 81 t.Errorf("Test #%d (%s) unexpected error: %v", i, 82 test.name, err) 83 continue 84 } 85 if string(marshalled) != test.expected { 86 t.Errorf("Test #%d (%s) unexpected marhsalled data - "+ 87 "got %s, want %s", i, test.name, marshalled, 88 test.expected) 89 continue 90 } 91 } 92 } 93 94 // TestGetTxOutSetInfoResult ensures that custom unmarshalling of 95 // GetTxOutSetInfoResult works as intended. 96 func TestGetTxOutSetInfoResult(t *testing.T) { 97 t.Parallel() 98 99 tests := []struct { 100 name string 101 result string 102 want btcjson.GetTxOutSetInfoResult 103 }{ 104 { 105 name: "GetTxOutSetInfoResult - not scanning", 106 result: `{"height":123,"bestblock":"000000000000005f94116250e2407310463c0a7cf950f1af9ebe935b1c0687ab","transactions":1,"txouts":1,"bogosize":1,"hash_serialized_2":"9a0a561203ff052182993bc5d0cb2c620880bfafdbd80331f65fd9546c3e5c3e","disk_size":1,"total_amount":0.2}`, 107 want: btcjson.GetTxOutSetInfoResult{ 108 Height: 123, 109 BestBlock: func() chainhash.Hash { 110 h, err := chainhash.NewHashFromStr("000000000000005f94116250e2407310463c0a7cf950f1af9ebe935b1c0687ab") 111 if err != nil { 112 panic(err) 113 } 114 115 return *h 116 }(), 117 Transactions: 1, 118 TxOuts: 1, 119 BogoSize: 1, 120 HashSerialized: func() chainhash.Hash { 121 h, err := chainhash.NewHashFromStr("9a0a561203ff052182993bc5d0cb2c620880bfafdbd80331f65fd9546c3e5c3e") 122 if err != nil { 123 panic(err) 124 } 125 126 return *h 127 }(), 128 DiskSize: 1, 129 TotalAmount: func() btcutil.Amount { 130 a, err := btcutil.NewAmount(0.2) 131 if err != nil { 132 panic(err) 133 } 134 135 return a 136 }(), 137 }, 138 }, 139 } 140 141 t.Logf("Running %d tests", len(tests)) 142 for i, test := range tests { 143 var out btcjson.GetTxOutSetInfoResult 144 err := json.Unmarshal([]byte(test.result), &out) 145 if err != nil { 146 t.Errorf("Test #%d (%s) unexpected error: %v", i, 147 test.name, err) 148 continue 149 } 150 151 if !reflect.DeepEqual(out, test.want) { 152 t.Errorf("Test #%d (%s) unexpected unmarshalled data - "+ 153 "got %v, want %v", i, test.name, spew.Sdump(out), 154 spew.Sdump(test.want)) 155 continue 156 } 157 } 158 } 159 160 // TestChainSvrMiningInfoResults ensures GetMiningInfoResults are unmarshalled correctly 161 func TestChainSvrMiningInfoResults(t *testing.T) { 162 t.Parallel() 163 164 tests := []struct { 165 name string 166 result string 167 expected btcjson.GetMiningInfoResult 168 }{ 169 { 170 name: "mining info with integer networkhashps", 171 result: `{"networkhashps": 89790618491361}`, 172 expected: btcjson.GetMiningInfoResult{ 173 NetworkHashPS: 89790618491361, 174 }, 175 }, 176 { 177 name: "mining info with scientific notation networkhashps", 178 result: `{"networkhashps": 8.9790618491361e+13}`, 179 expected: btcjson.GetMiningInfoResult{ 180 NetworkHashPS: 89790618491361, 181 }, 182 }, 183 } 184 185 t.Logf("Running %d tests", len(tests)) 186 for i, test := range tests { 187 var miningInfoResult btcjson.GetMiningInfoResult 188 err := json.Unmarshal([]byte(test.result), &miningInfoResult) 189 if err != nil { 190 t.Errorf("Test #%d (%s) unexpected error: %v", i, 191 test.name, err) 192 continue 193 } 194 if miningInfoResult != test.expected { 195 t.Errorf("Test #%d (%s) unexpected marhsalled data - "+ 196 "got %+v, want %+v", i, test.name, miningInfoResult, 197 test.expected) 198 continue 199 } 200 } 201 }