@@ -11,6 +11,8 @@ import (
1111 "net"
1212 "reflect"
1313 "testing"
14+
15+ "github.com/blinklabs-io/cdnsd/internal/handshake"
1416)
1517
1618func TestMsgVersionEncodeDecode (t * testing.T ) {
@@ -116,3 +118,102 @@ func TestMsgAddrEncodeDecode(t *testing.T) {
116118 }
117119 }
118120}
121+
122+ func TestMsgGetHeadersEncodeDecode (t * testing.T ) {
123+ testDefs := []struct {
124+ message Message
125+ binaryHex string
126+ }{
127+ // Generated by our own code
128+ {
129+ binaryHex : "015b6ef2d3c1f3cdcadfd9a030ba1811efdd17740f14e166489760741d075992e00000000000000000000000000000000000000000000000000000000000000000" ,
130+ message : & MsgGetHeaders {
131+ Locator : [][32 ]byte {
132+ func () [32 ]byte {
133+ foo , _ := hex .DecodeString ("5b6ef2d3c1f3cdcadfd9a030ba1811efdd17740f14e166489760741d075992e0" )
134+ return [32 ]byte (foo )
135+ }(),
136+ },
137+ StopHash : [32 ]byte {},
138+ },
139+ },
140+ }
141+ for _ , testDef := range testDefs {
142+ binaryData , err := hex .DecodeString (testDef .binaryHex )
143+ if err != nil {
144+ t .Fatalf ("unexpected error decoding hex: %s" , err )
145+ }
146+ testMsg := new (MsgGetHeaders )
147+ if err := testMsg .Decode (binaryData ); err != nil {
148+ t .Fatalf ("unexpected error decoding message: %s" , err )
149+ }
150+ if ! reflect .DeepEqual (testMsg , testDef .message ) {
151+ t .Fatalf ("did not get expected message after decode:\n got: %#v\n wanted: %#v" , testMsg , testDef .message )
152+ }
153+ testEncoded := testMsg .Encode ()
154+ testEncodedHex := hex .EncodeToString (testEncoded )
155+ if testEncodedHex != testDef .binaryHex {
156+ t .Fatalf ("did not get expected binary hex after encode:\n got: %s\n wanted: %s" , testEncodedHex , testDef .binaryHex )
157+ }
158+ }
159+ }
160+
161+ func TestMsgHeadersEncodeDecode (t * testing.T ) {
162+ testDefs := []struct {
163+ message Message
164+ binaryHex string
165+ }{
166+ // Modified (truncated) from data captured from hsd
167+ {
168+ binaryHex : "0232bb5134a541385e000000005b6ef2d3c1f3cdcadfd9a030ba1811efdd17740f14e166489760741d075992e00000000000000000000000000000000000000000000000000000000000000000000000150000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000059919422c20530ece2b328adf63ec3f35a10e79375731687a81dfa7cd83a24e728b17095216d5e211ba1f61031416a51efca54eacb8c9059440c4671b0625bbe00000000ffff001c0000000000000000000000000000000000000000000000000000000000000000032a8839c741385e000000000000000000a5e40e8ba291bd7e8649747fa7fb8a7af39f5bacdb7433cd2f59710000000000000000000000000000000000000000000000000000000000000000000000060000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000038180556e54cd70fd131130e154be4569945a6526d45add9ade6dbdfa34f54e25d1d56a37915cd1337b007806116ff623d973bec006280154472a6677eacbe0700000000ffff001c0000000000000000000000000000000000000000000000000000000000000000" ,
169+ message : & MsgHeaders {
170+ Headers : []* handshake.BlockHeader {
171+ & handshake.BlockHeader {
172+ Nonce : 0x3451bb32 ,
173+ Time : 0x5e3841a5 ,
174+ PrevBlock : [32 ]uint8 {0x5b , 0x6e , 0xf2 , 0xd3 , 0xc1 , 0xf3 , 0xcd , 0xca , 0xdf , 0xd9 , 0xa0 , 0x30 , 0xba , 0x18 , 0x11 , 0xef , 0xdd , 0x17 , 0x74 , 0xf , 0x14 , 0xe1 , 0x66 , 0x48 , 0x97 , 0x60 , 0x74 , 0x1d , 0x7 , 0x59 , 0x92 , 0xe0 },
175+ NameRoot : [32 ]uint8 {0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 },
176+ ExtraNonce : [24 ]uint8 {0x0 , 0x0 , 0x0 , 0x15 , 0x0 , 0x0 , 0x0 , 0x4 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 },
177+ ReservedRoot : [32 ]uint8 {0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 },
178+ WitnessRoot : [32 ]uint8 {0x59 , 0x91 , 0x94 , 0x22 , 0xc2 , 0x5 , 0x30 , 0xec , 0xe2 , 0xb3 , 0x28 , 0xad , 0xf6 , 0x3e , 0xc3 , 0xf3 , 0x5a , 0x10 , 0xe7 , 0x93 , 0x75 , 0x73 , 0x16 , 0x87 , 0xa8 , 0x1d , 0xfa , 0x7c , 0xd8 , 0x3a , 0x24 , 0xe7 },
179+ MerkleRoot : [32 ]uint8 {0x28 , 0xb1 , 0x70 , 0x95 , 0x21 , 0x6d , 0x5e , 0x21 , 0x1b , 0xa1 , 0xf6 , 0x10 , 0x31 , 0x41 , 0x6a , 0x51 , 0xef , 0xca , 0x54 , 0xea , 0xcb , 0x8c , 0x90 , 0x59 , 0x44 , 0xc , 0x46 , 0x71 , 0xb0 , 0x62 , 0x5b , 0xbe },
180+ Version : 0x0 ,
181+ Bits : 0x1c00ffff ,
182+ Mask : [32 ]uint8 {0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 },
183+ },
184+ & handshake.BlockHeader {
185+ Nonce : 0x39882a03 ,
186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, 0x54 , 0xe2 },
192+ MerkleRoot : [32 ]uint8 {0x5d , 0x1d , 0x56 , 0xa3 , 0x79 , 0x15 , 0xcd , 0x13 , 0x37 , 0xb0 , 0x7 , 0x80 , 0x61 , 0x16 , 0xff , 0x62 , 0x3d , 0x97 , 0x3b , 0xec , 0x0 , 0x62 , 0x80 , 0x15 , 0x44 , 0x72 , 0xa6 , 0x67 , 0x7e , 0xac , 0xbe , 0x7 },
193+ Version : 0x0 ,
194+ Bits : 0x1c00ffff ,
195+ Mask : [32 ]uint8 {0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 },
196+ },
197+ },
198+ },
199+ },
200+ }
201+ for _ , testDef := range testDefs {
202+ binaryData , err := hex .DecodeString (testDef .binaryHex )
203+ if err != nil {
204+ t .Fatalf ("unexpected error decoding hex: %s" , err )
205+ }
206+ testMsg := new (MsgHeaders )
207+ if err := testMsg .Decode (binaryData ); err != nil {
208+ t .Fatalf ("unexpected error decoding message: %s" , err )
209+ }
210+ if ! reflect .DeepEqual (testMsg , testDef .message ) {
211+ t .Fatalf ("did not get expected message after decode:\n got: %#v\n wanted: %#v" , testMsg , testDef .message )
212+ }
213+ testEncoded := testMsg .Encode ()
214+ testEncodedHex := hex .EncodeToString (testEncoded )
215+ if testEncodedHex != testDef .binaryHex {
216+ t .Fatalf ("did not get expected binary hex after encode:\n got: %s\n wanted: %s" , testEncodedHex , testDef .binaryHex )
217+ }
218+ }
219+ }
0 commit comments