github.com/deso-protocol/core@v1.2.9/lib/network_test.go (about) 1 package lib 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "reflect" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/btcsuite/btcd/btcec" 12 13 "github.com/btcsuite/btcd/wire" 14 "github.com/bxcodec/faker" 15 merkletree "github.com/deso-protocol/go-merkle-tree" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 var pkForTesting1 = []byte{ 21 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 22 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 23 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2} 24 25 var postHashForTesting1 = BlockHash{ 26 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 27 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 28 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1} 29 30 var expectedVer = &MsgDeSoVersion{ 31 Version: 1, 32 Services: SFFullNode, 33 TstampSecs: 2, 34 Nonce: uint64(0xffffffffffffffff), 35 UserAgent: "abcdef", 36 StartBlockHeight: 4, 37 MinFeeRateNanosPerKB: 10, 38 } 39 40 func TestVersionConversion(t *testing.T) { 41 assert := assert.New(t) 42 require := require.New(t) 43 _ = assert 44 _ = require 45 46 { 47 data, err := expectedVer.ToBytes(false) 48 assert.NoError(err) 49 50 testVer := NewMessage(MsgTypeVersion) 51 err = testVer.FromBytes(data) 52 assert.NoError(err) 53 54 assert.Equal(expectedVer, testVer) 55 } 56 57 assert.Equalf(7, reflect.TypeOf(expectedVer).Elem().NumField(), 58 "Number of fields in VERSION message is different from expected. "+ 59 "Did you add a new field? If so, make sure the serialization code "+ 60 "works, add the new field to the test case, and fix this error.") 61 } 62 63 func TestVerack(t *testing.T) { 64 assert := assert.New(t) 65 require := require.New(t) 66 _ = assert 67 _ = require 68 69 networkType := NetworkType_MAINNET 70 var buf bytes.Buffer 71 72 nonce := uint64(12345678910) 73 _, err := WriteMessage(&buf, &MsgDeSoVerack{Nonce: nonce}, networkType) 74 require.NoError(err) 75 verBytes := buf.Bytes() 76 testMsg, _, err := ReadMessage(bytes.NewReader(verBytes), 77 networkType) 78 require.NoError(err) 79 require.Equal(&MsgDeSoVerack{Nonce: nonce}, testMsg) 80 } 81 82 var expectedBlockHeader = &MsgDeSoHeader{ 83 Version: 1, 84 PrevBlockHash: &BlockHash{ 85 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 86 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 87 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 88 0x32, 0x33, 89 }, 90 TransactionMerkleRoot: &BlockHash{ 91 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 92 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 0x51, 0x52, 0x53, 93 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60, 0x61, 0x62, 0x63, 94 0x64, 0x65, 95 }, 96 // Use full uint64 values to make sure serialization and de-serialization work 97 TstampSecs: uint64(1678943210), 98 Height: uint64(1321012345), 99 Nonce: uint64(12345678901234), 100 ExtraNonce: uint64(101234123456789), 101 } 102 103 func TestHeaderConversionAndReadWriteMessage(t *testing.T) { 104 assert := assert.New(t) 105 require := require.New(t) 106 _ = assert 107 _ = require 108 networkType := NetworkType_MAINNET 109 110 { 111 data, err := expectedBlockHeader.ToBytes(false) 112 assert.NoError(err) 113 114 testHdr := NewMessage(MsgTypeHeader) 115 err = testHdr.FromBytes(data) 116 assert.NoError(err) 117 118 assert.Equal(expectedBlockHeader, testHdr) 119 120 // Test read write. 121 var buf bytes.Buffer 122 payload, err := WriteMessage(&buf, expectedBlockHeader, networkType) 123 assert.NoError(err) 124 // Form the header from the payload and make sure it matches. 125 hdrFromPayload := NewMessage(MsgTypeHeader).(*MsgDeSoHeader) 126 assert.NotNil(hdrFromPayload, "NewMessage(MsgTypeHeader) should not return nil.") 127 assert.Equal(uint64(0), hdrFromPayload.Nonce, "NewMessage(MsgTypeHeader) should initialize Nonce to empty byte slice.") 128 err = hdrFromPayload.FromBytes(payload) 129 assert.NoError(err) 130 assert.Equal(expectedBlockHeader, hdrFromPayload) 131 132 hdrBytes := buf.Bytes() 133 testMsg, data, err := ReadMessage(bytes.NewReader(hdrBytes), 134 networkType) 135 assert.NoError(err) 136 assert.Equal(expectedBlockHeader, testMsg) 137 138 // Compute the header payload bytes so we can compare them. 139 hdrPayload, err := expectedBlockHeader.ToBytes(false) 140 assert.NoError(err) 141 assert.Equal(hdrPayload, data) 142 } 143 144 assert.Equalf(7, reflect.TypeOf(expectedBlockHeader).Elem().NumField(), 145 "Number of fields in HEADER message is different from expected. "+ 146 "Did you add a new field? If so, make sure the serialization code "+ 147 "works, add the new field to the test case, and fix this error.") 148 } 149 150 func TestGetHeadersSerialization(t *testing.T) { 151 assert := assert.New(t) 152 require := require.New(t) 153 _ = assert 154 _ = require 155 156 hash1 := expectedBlockHeader.PrevBlockHash 157 hash2 := expectedBlockHeader.TransactionMerkleRoot 158 159 getHeaders := &MsgDeSoGetHeaders{ 160 StopHash: hash1, 161 BlockLocator: []*BlockHash{hash1, hash2, hash1}, 162 } 163 164 messageBytes, err := getHeaders.ToBytes(false) 165 require.NoError(err) 166 newMessage := &MsgDeSoGetHeaders{} 167 err = newMessage.FromBytes(messageBytes) 168 require.NoError(err) 169 require.Equal(getHeaders, newMessage) 170 } 171 172 func TestHeaderBundleSerialization(t *testing.T) { 173 assert := assert.New(t) 174 require := require.New(t) 175 _ = assert 176 _ = require 177 178 hash1 := expectedBlockHeader.PrevBlockHash 179 180 headerBundle := &MsgDeSoHeaderBundle{ 181 Headers: []*MsgDeSoHeader{expectedBlockHeader, expectedBlockHeader}, 182 TipHash: hash1, 183 TipHeight: 12345, 184 } 185 186 messageBytes, err := headerBundle.ToBytes(false) 187 require.NoError(err) 188 newMessage := &MsgDeSoHeaderBundle{} 189 err = newMessage.FromBytes(messageBytes) 190 require.NoError(err) 191 require.Equal(headerBundle, newMessage) 192 } 193 194 func TestEnumExtras(t *testing.T) { 195 assert := assert.New(t) 196 require := require.New(t) 197 _ = assert 198 _ = require 199 200 // For all the enum strings we've defined, ensure we return 201 // a non-nil NewMessage. 202 for ii := uint8(1); !strings.Contains(MsgType(ii).String(), "UNRECOGNIZED"); ii++ { 203 assert.NotNilf(NewMessage(MsgType(ii)), "String() defined for MsgType (%v) but NewMessage() returns nil.", MsgType(ii)) 204 } 205 206 // For all the NewMessage() calls that return non-nil, ensure we have a String() 207 for ii := uint8(1); NewMessage(MsgType(ii)) != nil; ii++ { 208 hasString := !strings.Contains(MsgType(ii).String(), "UNRECOGNIZED") 209 assert.Truef(hasString, "String() undefined for MsgType (%v) but NewMessage() returns non-nil.", MsgType(ii)) 210 } 211 } 212 213 func TestReadWrite(t *testing.T) { 214 assert := assert.New(t) 215 require := require.New(t) 216 _ = assert 217 _ = require 218 219 networkType := NetworkType_MAINNET 220 var buf bytes.Buffer 221 222 payload, err := WriteMessage(&buf, expectedVer, networkType) 223 assert.NoError(err) 224 // Form the version from the payload and make sure it matches. 225 verFromPayload := NewMessage(MsgTypeVersion) 226 assert.NotNil(verFromPayload, "NewMessage(MsgTypeVersion) should not return nil.") 227 err = verFromPayload.FromBytes(payload) 228 assert.NoError(err) 229 assert.Equal(expectedVer, verFromPayload) 230 231 verBytes := buf.Bytes() 232 testMsg, data, err := ReadMessage(bytes.NewReader(verBytes), 233 networkType) 234 assert.NoError(err) 235 assert.Equal(expectedVer, testMsg) 236 237 // Compute the version payload bytes so we can compare them. 238 verPayload, err := expectedVer.ToBytes(false) 239 assert.NoError(err) 240 assert.Equal(verPayload, data) 241 242 // Incorrect network type should error. 243 _, _, err = ReadMessage(bytes.NewReader(verBytes), 244 NetworkType_TESTNET) 245 assert.Error(err, "Incorrect network should fail.") 246 247 // Payload too large should error. 248 bigBytes := make([]byte, MaxMessagePayload*1.1) 249 _, _, err = ReadMessage(bytes.NewReader(bigBytes), 250 NetworkType_MAINNET) 251 assert.Error(err, "Payload too large should fail.") 252 } 253 254 var expectedBlock = &MsgDeSoBlock{ 255 Header: expectedBlockHeader, 256 Txns: []*MsgDeSoTxn{ 257 { 258 TxInputs: []*DeSoInput{ 259 { 260 TxID: *CopyBytesIntoBlockHash([]byte{ 261 // random bytes 262 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 263 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 264 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 265 0x31, 0x32, 266 }), 267 Index: 111, 268 }, 269 { 270 TxID: *CopyBytesIntoBlockHash([]byte{ 271 // random bytes 272 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 273 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 274 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x90, 275 0x91, 0x92, 276 }), 277 Index: 222, 278 }, 279 }, 280 TxOutputs: []*DeSoOutput{ 281 { 282 PublicKey: []byte{ 283 // random bytes 284 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 285 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 286 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 287 0x21, 0x22, 0x23, 288 }, 289 AmountNanos: 333, 290 }, 291 { 292 PublicKey: []byte{ 293 // random bytes 294 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x10, 295 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x30, 296 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 297 0x21, 0x22, 0x23, 298 }, 299 AmountNanos: 333, 300 }, 301 }, 302 TxnMeta: &BlockRewardMetadataa{ 303 ExtraData: []byte{ 304 // random bytes 305 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x10, 306 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x90, 307 }, 308 }, 309 // random bytes 310 PublicKey: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 311 ExtraData: map[string][]byte{"dummykey": []byte{0x01, 0x02, 0x03, 0x04, 0x05}}, 312 //Signature: []byte{0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90}, 313 }, 314 { 315 TxInputs: []*DeSoInput{ 316 { 317 TxID: *CopyBytesIntoBlockHash([]byte{ 318 // random bytes 319 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 320 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 321 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 322 0x31, 0x32, 323 }), 324 Index: 111, 325 }, 326 { 327 TxID: *CopyBytesIntoBlockHash([]byte{ 328 // random bytes 329 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 330 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 331 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x90, 332 0x91, 0x92, 333 }), 334 Index: 222, 335 }, 336 }, 337 TxOutputs: []*DeSoOutput{ 338 { 339 PublicKey: []byte{ 340 // random bytes 341 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 342 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 343 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 344 0x21, 0x22, 0x23, 345 }, 346 AmountNanos: 333, 347 }, 348 { 349 PublicKey: []byte{ 350 // random bytes 351 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x30, 352 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x10, 353 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 354 0x21, 0x22, 0x23, 355 }, 356 AmountNanos: 333, 357 }, 358 }, 359 TxnMeta: &BlockRewardMetadataa{ 360 ExtraData: []byte{ 361 // random bytes 362 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x90, 363 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x10, 364 }, 365 }, 366 // random bytes 367 PublicKey: []byte{0x55, 0x66, 0x77, 0x88, 0x11, 0x22, 0x33, 0x44, 0x99}, 368 //Signature: []byte{0x50, 0x60, 0x70, 0x80, 0x90, 0x10, 0x20, 0x30, 0x40}, 369 }, 370 }, 371 372 BlockProducerInfo: &BlockProducerInfo{ 373 PublicKey: []byte{ 374 // random bytes 375 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x30, 376 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x10, 377 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 378 0x21, 0x22, 0x23, 379 }, 380 }, 381 } 382 383 var expectedV0Header = &MsgDeSoHeader{ 384 Version: 0, 385 PrevBlockHash: &BlockHash{ 386 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 387 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 388 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 389 0x32, 0x33, 390 }, 391 TransactionMerkleRoot: &BlockHash{ 392 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 393 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 0x51, 0x52, 0x53, 394 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60, 0x61, 0x62, 0x63, 395 0x64, 0x65, 396 }, 397 TstampSecs: uint64(0x70717273), 398 Height: uint64(99999), 399 Nonce: uint64(123456), 400 } 401 402 func TestBlockSerialize(t *testing.T) { 403 assert := assert.New(t) 404 require := require.New(t) 405 _ = assert 406 _ = require 407 408 // Add a signature to the block 409 priv, err := btcec.NewPrivateKey(btcec.S256()) 410 require.NoError(err) 411 expectedBlock.BlockProducerInfo.Signature, err = priv.Sign([]byte{0x01, 0x02, 0x03}) 412 require.NoError(err) 413 414 data, err := expectedBlock.ToBytes(false) 415 require.NoError(err) 416 417 testBlock := NewMessage(MsgTypeBlock).(*MsgDeSoBlock) 418 err = testBlock.FromBytes(data) 419 require.NoError(err) 420 421 assert.Equal(*expectedBlock, *testBlock) 422 } 423 424 func TestBlockSerializeNoBlockProducerInfo(t *testing.T) { 425 assert := assert.New(t) 426 require := require.New(t) 427 _ = assert 428 _ = require 429 430 // Add a signature to the block 431 blockWithoutProducerInfo := *expectedBlock 432 blockWithoutProducerInfo.BlockProducerInfo = nil 433 434 data, err := blockWithoutProducerInfo.ToBytes(false) 435 require.NoError(err) 436 437 testBlock := NewMessage(MsgTypeBlock).(*MsgDeSoBlock) 438 err = testBlock.FromBytes(data) 439 require.NoError(err) 440 441 assert.Equal(blockWithoutProducerInfo, *testBlock) 442 } 443 444 func TestBlockRewardTransactionSerialize(t *testing.T) { 445 assert := assert.New(t) 446 require := require.New(t) 447 _ = assert 448 _ = require 449 450 // Add a signature to the block 451 priv, err := btcec.NewPrivateKey(btcec.S256()) 452 require.NoError(err) 453 expectedBlock.BlockProducerInfo.Signature, err = priv.Sign([]byte{0x01, 0x02, 0x03}) 454 require.NoError(err) 455 456 data, err := expectedBlock.Txns[0].ToBytes(false) 457 require.NoError(err) 458 459 testTxn := NewMessage(MsgTypeTxn).(*MsgDeSoTxn) 460 err = testTxn.FromBytes(data) 461 require.NoError(err) 462 require.Equal(expectedBlock.Txns[0], testTxn) 463 } 464 465 func TestSerializeInv(t *testing.T) { 466 assert := assert.New(t) 467 require := require.New(t) 468 _ = assert 469 _ = require 470 471 invMsg := &MsgDeSoInv{ 472 InvList: []*InvVect{ 473 { 474 Type: InvTypeBlock, 475 Hash: BlockHash{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, 476 }, 477 { 478 Type: InvTypeTx, 479 Hash: BlockHash{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, 480 }, 481 }, 482 IsSyncResponse: true, 483 } 484 485 bb, err := invMsg.ToBytes(false) 486 require.NoError(err) 487 invMsgFromBuf := &MsgDeSoInv{} 488 invMsgFromBuf.FromBytes(bb) 489 require.Equal(*invMsg, *invMsgFromBuf) 490 } 491 492 func TestSerializeAddresses(t *testing.T) { 493 require := require.New(t) 494 495 addrs := &MsgDeSoAddr{ 496 AddrList: []*SingleAddr{ 497 { 498 Timestamp: time.Unix(1000, 0), 499 Services: SFFullNode, 500 IP: []byte{0x01, 0x02, 0x03, 0x04}, 501 Port: 12345, 502 }, 503 { 504 Timestamp: time.Unix(100000, 0), 505 Services: 0, 506 IP: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}, 507 Port: 54321, 508 }, 509 }, 510 } 511 512 bb, err := addrs.ToBytes(false) 513 require.NoError(err) 514 parsedAddrs := &MsgDeSoAddr{} 515 err = parsedAddrs.FromBytes(bb) 516 require.NoError(err) 517 require.Equal(addrs, parsedAddrs) 518 } 519 520 func TestSerializeGetBlocks(t *testing.T) { 521 require := require.New(t) 522 523 msg := &MsgDeSoGetBlocks{ 524 HashList: []*BlockHash{ 525 {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, 526 {2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, 527 {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0}, 528 }, 529 } 530 531 bb, err := msg.ToBytes(false) 532 require.NoError(err) 533 parsedMsg := &MsgDeSoGetBlocks{} 534 err = parsedMsg.FromBytes(bb) 535 require.NoError(err) 536 require.Equal(msg, parsedMsg) 537 } 538 539 func TestSerializePingPong(t *testing.T) { 540 require := require.New(t) 541 542 { 543 msg := &MsgDeSoPing{ 544 Nonce: uint64(1234567891011), 545 } 546 547 bb, err := msg.ToBytes(false) 548 require.NoError(err) 549 parsedMsg := &MsgDeSoPing{} 550 err = parsedMsg.FromBytes(bb) 551 require.NoError(err) 552 require.Equal(msg, parsedMsg) 553 } 554 { 555 msg := &MsgDeSoPong{ 556 Nonce: uint64(1234567891011), 557 } 558 559 bb, err := msg.ToBytes(false) 560 require.NoError(err) 561 parsedMsg := &MsgDeSoPong{} 562 err = parsedMsg.FromBytes(bb) 563 require.NoError(err) 564 require.Equal(msg, parsedMsg) 565 } 566 } 567 568 func TestSerializeGetTransactions(t *testing.T) { 569 require := require.New(t) 570 571 msg := &MsgDeSoGetTransactions{ 572 HashList: []*BlockHash{ 573 {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, 574 {2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, 575 {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0}, 576 }, 577 } 578 579 bb, err := msg.ToBytes(false) 580 require.NoError(err) 581 parsedMsg := &MsgDeSoGetTransactions{} 582 err = parsedMsg.FromBytes(bb) 583 require.NoError(err) 584 require.Equal(msg, parsedMsg) 585 } 586 587 func TestSerializeTransactionBundle(t *testing.T) { 588 require := require.New(t) 589 590 msg := &MsgDeSoTransactionBundle{ 591 Transactions: expectedBlock.Txns, 592 } 593 594 bb, err := msg.ToBytes(false) 595 require.NoError(err) 596 parsedMsg := &MsgDeSoTransactionBundle{} 597 err = parsedMsg.FromBytes(bb) 598 require.NoError(err) 599 require.Equal(msg, parsedMsg) 600 } 601 602 func TestSerializeMempool(t *testing.T) { 603 require := require.New(t) 604 605 { 606 msg := &MsgDeSoMempool{} 607 networkType := NetworkType_MAINNET 608 var buf bytes.Buffer 609 _, err := WriteMessage(&buf, msg, networkType) 610 require.NoError(err) 611 verBytes := buf.Bytes() 612 testMsg, _, err := ReadMessage(bytes.NewReader(verBytes), 613 networkType) 614 require.NoError(err) 615 require.Equal(msg, testMsg) 616 } 617 } 618 619 func TestSerializeGetAddr(t *testing.T) { 620 require := require.New(t) 621 622 { 623 msg := &MsgDeSoGetAddr{} 624 networkType := NetworkType_MAINNET 625 var buf bytes.Buffer 626 _, err := WriteMessage(&buf, msg, networkType) 627 require.NoError(err) 628 verBytes := buf.Bytes() 629 testMsg, _, err := ReadMessage(bytes.NewReader(verBytes), 630 networkType) 631 require.NoError(err) 632 require.Equal(msg, testMsg) 633 } 634 } 635 636 func TestSerializeBitcoinExchange(t *testing.T) { 637 assert := assert.New(t) 638 require := require.New(t) 639 _ = assert 640 _ = require 641 642 bitcoinTxBytes, err := hex.DecodeString("0100000000010171bb05b9f14c063412df904395b4a53ba195b60e38db395f4857dcf801f4a07e0100000017160014187f260400f5fe38ad6d83f839ec19fd57e49d9ffdffffff01d0471f000000000017a91401a68eb55a152f2d12775c371a9cb2052df5fe3887024730440220077b9ad6612e491924516ceceb78d2667bca35e89f402718787b949144d0e0c0022014c503ece0f8c1a3b2dfc77e198ff90c3ef5932285b9697d83b298854838054d0121030e8c515e19a966e882f4c9dcb8f9d47e09de282d8b52364789df207468ed9405e7f50900") 643 require.NoError(err) 644 bitcoinTx := wire.MsgTx{} 645 bitcoinTx.Deserialize(bytes.NewReader(bitcoinTxBytes)) 646 647 txMeta := &BitcoinExchangeMetadata{ 648 BitcoinTransaction: &bitcoinTx, 649 BitcoinBlockHash: &BlockHash{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, 650 BitcoinMerkleRoot: &BlockHash{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, 651 BitcoinMerkleProof: []*merkletree.ProofPart{ 652 { 653 IsRight: true, 654 Hash: []byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0}, 655 }, 656 { 657 IsRight: true, 658 Hash: []byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0}, 659 }, 660 }, 661 } 662 663 data, err := txMeta.ToBytes(false) 664 require.NoError(err) 665 666 testMeta, err := NewTxnMetadata(TxnTypeBitcoinExchange) 667 require.NoError(err) 668 err = testMeta.FromBytes(data) 669 require.NoError(err) 670 require.Equal(testMeta, txMeta) 671 } 672 673 func TestSerializePrivateMessage(t *testing.T) { 674 assert := assert.New(t) 675 require := require.New(t) 676 _ = assert 677 _ = require 678 679 txMeta := &PrivateMessageMetadata{ 680 RecipientPublicKey: pkForTesting1, 681 EncryptedText: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 682 TimestampNanos: uint64(1234578901234), 683 } 684 685 data, err := txMeta.ToBytes(false) 686 require.NoError(err) 687 688 testMeta, err := NewTxnMetadata(TxnTypePrivateMessage) 689 require.NoError(err) 690 err = testMeta.FromBytes(data) 691 require.NoError(err) 692 require.Equal(testMeta, txMeta) 693 } 694 695 func TestSerializeLike(t *testing.T) { 696 assert := assert.New(t) 697 require := require.New(t) 698 _ = assert 699 _ = require 700 701 txMeta := &LikeMetadata{LikedPostHash: &postHashForTesting1} 702 703 data, err := txMeta.ToBytes(false) 704 require.NoError(err) 705 706 testMeta, err := NewTxnMetadata(TxnTypeLike) 707 require.NoError(err) 708 err = testMeta.FromBytes(data) 709 require.NoError(err) 710 require.Equal(txMeta, testMeta) 711 } 712 713 func TestSerializeUnlike(t *testing.T) { 714 assert := assert.New(t) 715 require := require.New(t) 716 _ = assert 717 _ = require 718 719 txMeta := &LikeMetadata{ 720 LikedPostHash: &postHashForTesting1, 721 IsUnlike: true, 722 } 723 724 data, err := txMeta.ToBytes(false) 725 require.NoError(err) 726 727 testMeta, err := NewTxnMetadata(TxnTypeLike) 728 require.NoError(err) 729 err = testMeta.FromBytes(data) 730 require.NoError(err) 731 require.Equal(txMeta, testMeta) 732 } 733 734 func TestSerializeFollow(t *testing.T) { 735 assert := assert.New(t) 736 require := require.New(t) 737 _ = assert 738 _ = require 739 740 txMeta := &FollowMetadata{FollowedPublicKey: pkForTesting1} 741 742 data, err := txMeta.ToBytes(false) 743 require.NoError(err) 744 745 testMeta, err := NewTxnMetadata(TxnTypeFollow) 746 require.NoError(err) 747 err = testMeta.FromBytes(data) 748 require.NoError(err) 749 require.Equal(txMeta, testMeta) 750 } 751 752 func TestSerializeUnfollow(t *testing.T) { 753 assert := assert.New(t) 754 require := require.New(t) 755 _ = assert 756 _ = require 757 758 txMeta := &FollowMetadata{ 759 FollowedPublicKey: pkForTesting1, 760 IsUnfollow: true, 761 } 762 763 data, err := txMeta.ToBytes(false) 764 require.NoError(err) 765 766 testMeta, err := NewTxnMetadata(TxnTypeFollow) 767 require.NoError(err) 768 err = testMeta.FromBytes(data) 769 require.NoError(err) 770 require.Equal(testMeta, txMeta) 771 } 772 773 func TestSerializeSubmitPost(t *testing.T) { 774 assert := assert.New(t) 775 require := require.New(t) 776 _ = assert 777 _ = require 778 779 txMeta := &SubmitPostMetadata{ 780 PostHashToModify: pkForTesting1, 781 ParentStakeID: pkForTesting1, 782 Body: []byte("This is a body text"), 783 CreatorBasisPoints: 10 * 100, 784 StakeMultipleBasisPoints: 2 * 100 * 100, 785 TimestampNanos: uint64(1234567890123), 786 IsHidden: true, 787 } 788 789 data, err := txMeta.ToBytes(false) 790 require.NoError(err) 791 792 testMeta, err := NewTxnMetadata(TxnTypeSubmitPost) 793 require.NoError(err) 794 err = testMeta.FromBytes(data) 795 require.NoError(err) 796 require.Equal(testMeta, txMeta) 797 } 798 799 func TestSerializeUpdateProfile(t *testing.T) { 800 assert := assert.New(t) 801 require := require.New(t) 802 _ = assert 803 _ = require 804 805 txMeta := &UpdateProfileMetadata{ 806 ProfilePublicKey: pkForTesting1, 807 NewUsername: []byte("new username"), 808 NewDescription: []byte("new description"), 809 NewProfilePic: []byte("profile pic data"), 810 NewCreatorBasisPoints: 10 * 100, 811 NewStakeMultipleBasisPoints: 2 * 100 * 100, 812 } 813 814 data, err := txMeta.ToBytes(false) 815 require.NoError(err) 816 817 testMeta, err := NewTxnMetadata(TxnTypeUpdateProfile) 818 require.NoError(err) 819 err = testMeta.FromBytes(data) 820 require.NoError(err) 821 require.Equal(testMeta, txMeta) 822 } 823 824 func TestSerializeCreatorCoin(t *testing.T) { 825 assert := assert.New(t) 826 require := require.New(t) 827 _ = assert 828 _ = require 829 830 txMeta := &CreatorCoinMetadataa{} 831 txMeta.ProfilePublicKey = []byte{ 832 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 833 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 834 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 835 0x00, 0x01} 836 faker.FakeData(&txMeta) 837 838 data, err := txMeta.ToBytes(false) 839 require.NoError(err) 840 841 testMeta, err := NewTxnMetadata(TxnTypeCreatorCoin) 842 require.NoError(err) 843 err = testMeta.FromBytes(data) 844 require.NoError(err) 845 require.Equal(txMeta, testMeta) 846 } 847 848 func TestSerializeCreatorCoinTransfer(t *testing.T) { 849 assert := assert.New(t) 850 require := require.New(t) 851 _ = assert 852 _ = require 853 854 txMeta := &CreatorCoinTransferMetadataa{} 855 txMeta.ProfilePublicKey = []byte{ 856 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 857 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 858 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 859 0x00, 0x01, 0x02} 860 faker.FakeData(&txMeta) 861 862 data, err := txMeta.ToBytes(false) 863 require.NoError(err) 864 865 testMeta, err := NewTxnMetadata(TxnTypeCreatorCoinTransfer) 866 require.NoError(err) 867 err = testMeta.FromBytes(data) 868 require.NoError(err) 869 require.Equal(txMeta, testMeta) 870 } 871 872 func TestSerializeCreateNFT(t *testing.T) { 873 assert := assert.New(t) 874 require := require.New(t) 875 _ = assert 876 _ = require 877 878 txMeta := &CreateNFTMetadata{} 879 txMeta.NFTPostHash = &BlockHash{ 880 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 881 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 882 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1} 883 txMeta.NumCopies = uint64(100) 884 txMeta.HasUnlockable = true 885 txMeta.IsForSale = true 886 txMeta.MinBidAmountNanos = 9876 887 txMeta.NFTRoyaltyToCreatorBasisPoints = 1234 888 txMeta.NFTRoyaltyToCoinBasisPoints = 4321 889 890 data, err := txMeta.ToBytes(false) 891 require.NoError(err) 892 893 testMeta, err := NewTxnMetadata(TxnTypeCreateNFT) 894 require.NoError(err) 895 err = testMeta.FromBytes(data) 896 require.NoError(err) 897 require.Equal(txMeta, testMeta) 898 } 899 900 func TestSerializeUpdateNFT(t *testing.T) { 901 assert := assert.New(t) 902 require := require.New(t) 903 _ = assert 904 _ = require 905 906 txMeta := &UpdateNFTMetadata{} 907 txMeta.NFTPostHash = &BlockHash{ 908 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 909 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 910 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1} 911 txMeta.SerialNumber = uint64(99) 912 txMeta.IsForSale = true 913 txMeta.MinBidAmountNanos = 9876 914 915 data, err := txMeta.ToBytes(false) 916 require.NoError(err) 917 918 testMeta, err := NewTxnMetadata(TxnTypeUpdateNFT) 919 require.NoError(err) 920 err = testMeta.FromBytes(data) 921 require.NoError(err) 922 require.Equal(txMeta, testMeta) 923 } 924 925 func TestSerializeAcceptNFTBid(t *testing.T) { 926 assert := assert.New(t) 927 require := require.New(t) 928 _ = assert 929 _ = require 930 931 txMeta := &AcceptNFTBidMetadata{} 932 txMeta.NFTPostHash = &BlockHash{ 933 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 934 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 935 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1} 936 txMeta.SerialNumber = uint64(99) 937 txMeta.BidderPKID = PublicKeyToPKID([]byte{ 938 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 939 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 940 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 941 0x00, 0x01, 0x02}) 942 txMeta.BidAmountNanos = 999 943 txMeta.BidderInputs = []*DeSoInput{ 944 { 945 TxID: *CopyBytesIntoBlockHash([]byte{ 946 // random bytes 947 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 948 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 949 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 950 0x31, 0x32, 951 }), 952 Index: 111, 953 }, 954 { 955 TxID: *CopyBytesIntoBlockHash([]byte{ 956 // random bytes 957 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 958 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 959 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x90, 960 0x91, 0x92, 961 }), 962 Index: 222, 963 }, 964 } 965 txMeta.UnlockableText = []byte("accept nft bid") 966 967 data, err := txMeta.ToBytes(false) 968 require.NoError(err) 969 970 testMeta, err := NewTxnMetadata(TxnTypeAcceptNFTBid) 971 require.NoError(err) 972 err = testMeta.FromBytes(data) 973 require.NoError(err) 974 require.Equal(txMeta, testMeta) 975 } 976 977 func TestSerializeNFTBid(t *testing.T) { 978 assert := assert.New(t) 979 require := require.New(t) 980 _ = assert 981 _ = require 982 983 txMeta := &NFTBidMetadata{} 984 txMeta.NFTPostHash = &BlockHash{ 985 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 986 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 987 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1} 988 txMeta.SerialNumber = uint64(99) 989 txMeta.BidAmountNanos = uint64(123456789) 990 991 data, err := txMeta.ToBytes(false) 992 require.NoError(err) 993 994 testMeta, err := NewTxnMetadata(TxnTypeNFTBid) 995 require.NoError(err) 996 err = testMeta.FromBytes(data) 997 require.NoError(err) 998 require.Equal(txMeta, testMeta) 999 } 1000 1001 func TestSerializeNFTTransfer(t *testing.T) { 1002 assert := assert.New(t) 1003 require := require.New(t) 1004 _ = assert 1005 _ = require 1006 1007 txMeta := &NFTTransferMetadata{} 1008 txMeta.NFTPostHash = &BlockHash{ 1009 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1010 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1011 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1} 1012 txMeta.SerialNumber = uint64(99) 1013 txMeta.ReceiverPublicKey = []byte{ 1014 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 1015 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 1016 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 1017 0x00, 0x01, 0x02} 1018 txMeta.UnlockableText = []byte("accept nft bid") 1019 1020 data, err := txMeta.ToBytes(false) 1021 require.NoError(err) 1022 1023 testMeta, err := NewTxnMetadata(TxnTypeNFTTransfer) 1024 require.NoError(err) 1025 err = testMeta.FromBytes(data) 1026 require.NoError(err) 1027 require.Equal(txMeta, testMeta) 1028 } 1029 1030 func TestAcceptNFTTransfer(t *testing.T) { 1031 assert := assert.New(t) 1032 require := require.New(t) 1033 _ = assert 1034 _ = require 1035 1036 txMeta := &AcceptNFTTransferMetadata{} 1037 txMeta.NFTPostHash = &BlockHash{ 1038 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1039 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1040 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1} 1041 txMeta.SerialNumber = uint64(99) 1042 1043 data, err := txMeta.ToBytes(false) 1044 require.NoError(err) 1045 1046 testMeta, err := NewTxnMetadata(TxnTypeAcceptNFTTransfer) 1047 require.NoError(err) 1048 err = testMeta.FromBytes(data) 1049 require.NoError(err) 1050 require.Equal(txMeta, testMeta) 1051 } 1052 1053 func TestBurnNFT(t *testing.T) { 1054 assert := assert.New(t) 1055 require := require.New(t) 1056 _ = assert 1057 _ = require 1058 1059 txMeta := &BurnNFTMetadata{} 1060 txMeta.NFTPostHash = &BlockHash{ 1061 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1062 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1063 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1} 1064 txMeta.SerialNumber = uint64(99) 1065 1066 data, err := txMeta.ToBytes(false) 1067 require.NoError(err) 1068 1069 testMeta, err := NewTxnMetadata(TxnTypeBurnNFT) 1070 require.NoError(err) 1071 err = testMeta.FromBytes(data) 1072 require.NoError(err) 1073 require.Equal(txMeta, testMeta) 1074 } 1075 1076 func TestDecodeHeaderVersion0(t *testing.T) { 1077 assert := assert.New(t) 1078 require := require.New(t) 1079 _, _ = assert, require 1080 1081 // This header was serialized on an old branch that does not incorporate the v1 changes 1082 headerHex := "0000000002030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465737271709f86010040e20100" 1083 headerBytes, err := hex.DecodeString(headerHex) 1084 require.NoError(err) 1085 v0Header := &MsgDeSoHeader{} 1086 v0Header.FromBytes(headerBytes) 1087 1088 require.Equal(expectedV0Header, v0Header) 1089 1090 // Serialize the expected header and verify the same hex is produced 1091 expectedBytes, err := expectedV0Header.ToBytes(false) 1092 require.NoError(err) 1093 1094 require.Equal(expectedBytes, headerBytes) 1095 } 1096 1097 func TestDecodeBlockVersion0(t *testing.T) { 1098 assert := assert.New(t) 1099 require := require.New(t) 1100 _, _ = assert, require 1101 1102 blockHex := "500000000002030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465737271709f86010040e2010002bd010201020304050607080910111213141516171819202122232425262728293031326f4142434445464748495061626364656667686970818283848586878889909192de0102010203040506070809102122232425262728293021222324252627282930212223cd02313233343536373839104142434445464748493021222324252627282930212223cd02011514919293949596979899107172737475767778799009112233445566778899010864756d6d796b657905010203040500ae010221222324252627282930111213141516171819200102030405060708091031326f6162636465666768697041424344454647484950818283848586878889909192de0102212223242526272829300102030405060708091021222324252627282930212223cd02414243444546474849303132333435363738391021222324252627282930212223cd020115147172737475767778799091929394959697989910095566778811223344990000" 1103 blockBytes, err := hex.DecodeString(blockHex) 1104 require.NoError(err) 1105 v0Block := &MsgDeSoBlock{} 1106 v0Block.FromBytes(blockBytes) 1107 1108 expectedV0Block := *expectedBlock 1109 expectedV0Block.Header = expectedV0Header 1110 expectedV0Block.BlockProducerInfo = nil 1111 1112 require.Equal(&expectedV0Block, v0Block) 1113 1114 // Serialize the expected block and verify the same hex is produced 1115 expectedBytes, err := expectedV0Block.ToBytes(false) 1116 require.NoError(err) 1117 1118 require.Equal(expectedBytes, blockBytes) 1119 }