github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/sync/sync_handler_test.go (about) 1 /* 2 * Copyright (C) 2018 The ontology Authors 3 * This file is part of The ontology library. 4 * 5 * The ontology is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * The ontology is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with The ontology. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 package sync 20 21 import ( 22 "bytes" 23 "encoding/binary" 24 "encoding/hex" 25 "testing" 26 "time" 27 28 "github.com/ontio/ontology-crypto/keypair" 29 "github.com/ontio/ontology/common" 30 "github.com/sixexorg/magnetic-ring/config" 31 "github.com/ontio/ontology/common/log" 32 "github.com/ontio/ontology/core/ledger" 33 "github.com/ontio/ontology/core/payload" 34 ct "github.com/ontio/ontology/core/types" 35 "github.com/ontio/ontology/events" 36 msgCommon "github.com/sixexorg/magnetic-ring/p2pserver/common" 37 "github.com/sixexorg/magnetic-ring/p2pserver/message/msg_pack" 38 "github.com/sixexorg/magnetic-ring/p2pserver/message/types" 39 "github.com/sixexorg/magnetic-ring/p2pserver/net/netserver" 40 "github.com/sixexorg/magnetic-ring/p2pserver/net/protocol" 41 "github.com/sixexorg/magnetic-ring/p2pserver/peer" 42 "github.com/stretchr/testify/assert" 43 ) 44 45 var ( 46 network p2p.P2P 47 ) 48 49 func init() { 50 log.Init(log.PATH, log.Stdout) 51 // Start local network server and create message router 52 _, pub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 53 network = netserver.NewNetServer(pub) 54 55 events.Init() 56 // Initial a ledger 57 var err error 58 ledger.DefLedger, err = ledger.NewLedger(config.DEFAULT_DATA_DIR) 59 if err != nil { 60 log.Fatalf("NewLedger error %s", err) 61 } 62 63 _, pubKey1, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 64 _, pubKey2, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 65 _, pubKey3, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 66 _, pubKey4, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 67 68 bookkeepers := []keypair.PublicKey{pubKey1, pubKey2, pubKey3, pubKey4} 69 err = ledger.DefLedger.Init(bookkeepers) 70 if err != nil { 71 log.Fatalf("DefLedger.Init error %s", err) 72 } 73 } 74 75 // TestVersionHandle tests Function VersionHandle handling a version message 76 func TestVersionHandle(t *testing.T) { 77 // Simulate a remote peer to connect to the local 78 remotePeer := peer.NewPeer() 79 assert.NotNil(t, remotePeer) 80 81 network.AddPeerSyncAddress("127.0.0.1:50010", remotePeer) 82 83 var testID uint64 84 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 85 key := keypair.SerializePublicKey(testPub) 86 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 87 assert.Nil(t, err) 88 89 // Construct a version packet 90 vpl := types.VersionPayload{ 91 Version: 1, 92 Services: 12345678, 93 TimeStamp: uint32(time.Now().UTC().UnixNano()), 94 SyncPort: 20334, 95 HttpInfoPort: 20335, 96 ConsPort: 20336, 97 UserAgent: 0x00, 98 StartHeight: 12345, 99 IsConsensus: false, 100 Nonce: testID, 101 } 102 vpl.Relay = 0 103 vpl.Cap[msgCommon.HTTP_INFO_FLAG] = 0x01 104 105 buf, err := msgpack.NewVersion(vpl, testPub) 106 assert.Nil(t, err) 107 108 msg := &msgCommon.MsgPayload{ 109 Id: testID, 110 Addr: "127.0.0.1:50010", 111 Payload: buf, 112 } 113 114 // Invoke VersionHandle to handle the msg 115 VersionHandle(msg, network, nil) 116 117 // Get the remote peer from the neighbor peers by peer id 118 tempPeer := network.GetPeer(testID) 119 assert.NotNil(t, tempPeer) 120 121 assert.Equal(t, tempPeer.GetID(), vpl.Nonce) 122 assert.Equal(t, tempPeer.GetVersion(), vpl.Version) 123 assert.Equal(t, tempPeer.GetServices(), vpl.Services) 124 assert.Equal(t, tempPeer.GetSyncPort(), vpl.SyncPort) 125 assert.Equal(t, tempPeer.GetHttpInfoPort(), vpl.HttpInfoPort) 126 assert.Equal(t, tempPeer.GetConsPort(), vpl.ConsPort) 127 assert.Equal(t, tempPeer.GetHeight(), vpl.StartHeight) 128 assert.Equal(t, tempPeer.GetSyncState(), uint32(msgCommon.HAND_SHAKE)) 129 130 network.DelNbrNode(testID) 131 } 132 133 // TestVerAckHandle tests Function VerAckHandle handling a version ack 134 func TestVerAckHandle(t *testing.T) { 135 // Simulate a remote peer to be added to the neighbor peers 136 var testID uint64 137 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 138 key := keypair.SerializePublicKey(testPub) 139 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 140 assert.Nil(t, err) 141 142 remotePeer := peer.NewPeer() 143 assert.NotNil(t, remotePeer) 144 145 remotePeer.SetHttpInfoPort(20335) 146 remotePeer.SetBookKeeperAddr(testPub) 147 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 148 20337, testID, 0, 12345) 149 network.AddNbrNode(remotePeer) 150 remotePeer.SetSyncState(msgCommon.HAND_SHAKE) 151 152 // Construct a version ack packet 153 buf, err := msgpack.NewVerAck(false) 154 assert.Nil(t, err) 155 156 msg := &msgCommon.MsgPayload{ 157 Id: testID, 158 Addr: "127.0.0.1:50010", 159 Payload: buf, 160 } 161 162 // Invoke VerAckHandle to handle the msg 163 VerAckHandle(msg, network, nil) 164 165 // Get the remote peer from the neighbor peers by peer id 166 tempPeer := network.GetPeer(testID) 167 assert.NotNil(t, tempPeer) 168 assert.Equal(t, tempPeer.GetSyncState(), uint32(msgCommon.ESTABLISH)) 169 assert.Equal(t, tempPeer.GetPubKey(), testPub) 170 171 network.DelNbrNode(testID) 172 } 173 174 // TestAddrReqHandle tests Function AddrReqHandle handling an address req 175 func TestAddrReqHandle(t *testing.T) { 176 // Simulate a remote peer to be added to the neighbor peers 177 var testID uint64 178 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 179 key := keypair.SerializePublicKey(testPub) 180 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 181 assert.Nil(t, err) 182 183 remotePeer := peer.NewPeer() 184 assert.NotNil(t, remotePeer) 185 186 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 187 20337, testID, 0, 12345) 188 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 189 190 network.AddNbrNode(remotePeer) 191 192 // Construct an address request packet 193 buf, err := msgpack.NewAddrReq() 194 assert.Nil(t, err) 195 196 msg := &msgCommon.MsgPayload{ 197 Id: testID, 198 Addr: "127.0.0.1:50010", 199 Payload: buf, 200 } 201 202 // Invoke AddrReqHandle to handle the msg 203 AddrReqHandle(msg, network, nil) 204 205 network.DelNbrNode(testID) 206 } 207 208 // TestHeadersReqHandle tests Function HeadersReqHandle handling a header req 209 func TestHeadersReqHandle(t *testing.T) { 210 // Simulate a remote peer to be added to the neighbor peers 211 var testID uint64 212 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 213 key := keypair.SerializePublicKey(testPub) 214 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 215 assert.Nil(t, err) 216 217 remotePeer := peer.NewPeer() 218 assert.NotNil(t, remotePeer) 219 220 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 221 20337, testID, 0, 12345) 222 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 223 224 network.AddNbrNode(remotePeer) 225 226 // Construct a headers request of packet 227 headerHash := ledger.DefLedger.GetCurrentHeaderHash() 228 buf, err := msgpack.NewHeadersReq(headerHash) 229 230 msg := &msgCommon.MsgPayload{ 231 Id: testID, 232 Addr: "127.0.0.1:50010", 233 Payload: buf, 234 } 235 236 // Invoke HeadersReqhandle to handle the msg 237 HeadersReqHandle(msg, network, nil) 238 network.DelNbrNode(testID) 239 } 240 241 // TestPingHandle tests Function PingHandle handling a ping message 242 func TestPingHandle(t *testing.T) { 243 // Simulate a remote peer to be added to the neighbor peers 244 var testID uint64 245 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 246 key := keypair.SerializePublicKey(testPub) 247 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 248 assert.Nil(t, err) 249 250 remotePeer := peer.NewPeer() 251 assert.NotNil(t, remotePeer) 252 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 253 20337, testID, 0, 12345) 254 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 255 256 network.AddNbrNode(remotePeer) 257 258 // Construct a ping packet 259 height := ledger.DefLedger.GetCurrentBlockHeight() 260 assert.Nil(t, err) 261 262 buf, err := msgpack.NewPingMsg(uint64(height)) 263 assert.Nil(t, err) 264 265 msg := &msgCommon.MsgPayload{ 266 Id: testID, 267 Addr: "127.0.0.1:50010", 268 Payload: buf, 269 } 270 271 // Invoke PingHandle to handle the msg 272 PingHandle(msg, network, nil) 273 274 network.DelNbrNode(testID) 275 } 276 277 // TestPingHandle tests Function PingHandle handling a pong message 278 func TestPongHandle(t *testing.T) { 279 // Simulate a remote peer to be added to the neighbor peers 280 var testID uint64 281 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 282 key := keypair.SerializePublicKey(testPub) 283 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 284 assert.Nil(t, err) 285 286 remotePeer := peer.NewPeer() 287 assert.NotNil(t, remotePeer) 288 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 289 20337, testID, 0, 12345) 290 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 291 292 network.AddNbrNode(remotePeer) 293 294 // Construct a pong packet 295 height := ledger.DefLedger.GetCurrentBlockHeight() 296 assert.Nil(t, err) 297 298 buf, err := msgpack.NewPongMsg(uint64(height)) 299 assert.Nil(t, err) 300 301 msg := &msgCommon.MsgPayload{ 302 Id: testID, 303 Addr: "127.0.0.1:50010", 304 Payload: buf, 305 } 306 307 // Invoke PingHandle to handle the msg 308 PongHandle(msg, network, nil) 309 310 network.DelNbrNode(testID) 311 } 312 313 // TestBlkHeaderHandle tests Function BlkHeaderHandle handling a sync header msg 314 func TestBlkHeaderHandle(t *testing.T) { 315 // Simulate a remote peer to be added to the neighbor peers 316 var testID uint64 317 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 318 key := keypair.SerializePublicKey(testPub) 319 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 320 assert.Nil(t, err) 321 322 remotePeer := peer.NewPeer() 323 assert.NotNil(t, remotePeer) 324 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 325 20337, testID, 0, 12345) 326 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 327 328 network.AddNbrNode(remotePeer) 329 330 // Construct a sync header packet 331 hash := ledger.DefLedger.GetBlockHash(0) 332 assert.NotEqual(t, hash, common.UINT256_EMPTY) 333 334 headers, err := GetHeadersFromHash(hash, hash) 335 assert.Nil(t, err) 336 337 buf, err := msgpack.NewHeaders(headers) 338 assert.Nil(t, err) 339 340 msg := &msgCommon.MsgPayload{ 341 Id: testID, 342 Addr: "127.0.0.1:50010", 343 Payload: buf, 344 } 345 346 // Invoke BlkHeaderHandle to handle the msg 347 BlkHeaderHandle(msg, network, nil) 348 349 network.DelNbrNode(testID) 350 } 351 352 // TestBlockHandle tests Function BlockHandle handling a block message 353 func TestBlockHandle(t *testing.T) { 354 // Simulate a remote peer to be added to the neighbor peers 355 var testID uint64 356 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 357 key := keypair.SerializePublicKey(testPub) 358 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 359 assert.Nil(t, err) 360 361 remotePeer := peer.NewPeer() 362 assert.NotNil(t, remotePeer) 363 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 364 20337, testID, 0, 12345) 365 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 366 367 network.AddNbrNode(remotePeer) 368 369 // Construct a block packet 370 hash := ledger.DefLedger.GetBlockHash(0) 371 assert.NotEqual(t, hash, common.UINT256_EMPTY) 372 373 block, err := ledger.DefLedger.GetBlockByHash(hash) 374 assert.Nil(t, err) 375 376 buf, err := msgpack.NewBlock(block) 377 assert.Nil(t, err) 378 379 msg := &msgCommon.MsgPayload{ 380 Id: testID, 381 Addr: "127.0.0.1:50010", 382 Payload: buf, 383 } 384 385 // Invoke BlockHandle to handle the msg 386 BlockHandle(msg, network, nil) 387 388 network.DelNbrNode(testID) 389 } 390 391 // TestConsensusHandle tests Function ConsensusHandle handling a consensus message 392 func TestConsensusHandle(t *testing.T) { 393 var testID uint64 394 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 395 key := keypair.SerializePublicKey(testPub) 396 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 397 assert.Nil(t, err) 398 399 hash := ledger.DefLedger.GetBlockHash(0) 400 assert.NotEqual(t, hash, common.UINT256_EMPTY) 401 402 cpl := &types.ConsensusPayload{ 403 Version: 1, 404 PrevHash: hash, 405 Height: 0, 406 BookkeeperIndex: 0, 407 Timestamp: uint32(time.Now().UTC().UnixNano()), 408 Data: []byte{}, 409 Owner: testPub, 410 Signature: []byte{}, 411 } 412 413 buf, err := msgpack.NewConsensus(cpl) 414 assert.Nil(t, err) 415 416 msg := &msgCommon.MsgPayload{ 417 Id: testID, 418 Addr: "127.0.0.1:50010", 419 Payload: buf, 420 } 421 422 ConsensusHandle(msg, network, nil) 423 } 424 425 // TestNotFoundHandle tests Function NotFoundHandle handling a not found message 426 func TestNotFoundHandle(t *testing.T) { 427 tempStr := "3369930accc1ddd067245e8edadcd9bea207ba5e1753ac18a51df77a343bfe92" 428 hex, _ := hex.DecodeString(tempStr) 429 var hash common.Uint256 430 hash.Deserialize(bytes.NewReader(hex)) 431 432 buf, err := msgpack.NewNotFound(hash) 433 assert.Nil(t, err) 434 435 msg := &msgCommon.MsgPayload{ 436 Id: 0, 437 Addr: "127.0.0.1:50010", 438 Payload: buf, 439 } 440 441 NotFoundHandle(msg, network, nil) 442 } 443 444 // TestTransactionHandle tests Function TransactionHandle handling a transaction message 445 func TestTransactionHandle(t *testing.T) { 446 code := []byte("ont") 447 vmcode := vmtypes.VmCode{ 448 VmType: vmtypes.Native, 449 Code: code, 450 } 451 452 invokeCodePayload := &payload.InvokeCode{ 453 Code: vmcode, 454 } 455 456 tx := &ct.Transaction{ 457 Version: 0, 458 TxType: ct.Invoke, 459 Payload: invokeCodePayload, 460 } 461 462 buf, err := msgpack.NewTxn(tx) 463 assert.Nil(t, err) 464 465 msg := &msgCommon.MsgPayload{ 466 Id: 0, 467 Addr: "127.0.0.1:50010", 468 Payload: buf, 469 } 470 471 TransactionHandle(msg, network, nil) 472 } 473 474 // TestAddrHandle tests Function AddrHandle handling a neighbor address response message 475 func TestAddrHandle(t *testing.T) { 476 nodeAddrs := []msgCommon.PeerAddr{} 477 buf, err := msgpack.NewAddrs(nodeAddrs) 478 assert.Nil(t, err) 479 480 msg := &msgCommon.MsgPayload{ 481 Id: 0, 482 Addr: "127.0.0.1:50010", 483 Payload: buf, 484 } 485 486 AddrHandle(msg, network, nil) 487 } 488 489 // TestDataReqHandle tests Function DataReqHandle handling a data req(block/Transaction) 490 func TestDataReqHandle(t *testing.T) { 491 var testID uint64 492 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 493 key := keypair.SerializePublicKey(testPub) 494 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 495 assert.Nil(t, err) 496 497 remotePeer := peer.NewPeer() 498 assert.NotNil(t, remotePeer) 499 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 500 20337, testID, 0, 12345) 501 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 502 503 network.AddNbrNode(remotePeer) 504 505 hash := ledger.DefLedger.GetBlockHash(0) 506 assert.NotEqual(t, hash, common.UINT256_EMPTY) 507 buf, err := msgpack.NewBlkDataReq(hash) 508 assert.Nil(t, err) 509 510 msg := &msgCommon.MsgPayload{ 511 Id: testID, 512 Addr: "127.0.0.1:50010", 513 Payload: buf, 514 } 515 516 DataReqHandle(msg, network, nil) 517 518 tempStr := "3369930accc1ddd067245e8edadcd9bea207ba5e1753ac18a51df77a343bfe92" 519 hex, _ := hex.DecodeString(tempStr) 520 var txHash common.Uint256 521 txHash.Deserialize(bytes.NewReader(hex)) 522 buf, err = msgpack.NewTxnDataReq(txHash) 523 assert.Nil(t, err) 524 525 msg = &msgCommon.MsgPayload{ 526 Id: testID, 527 Addr: "127.0.0.1:50010", 528 Payload: buf, 529 } 530 531 DataReqHandle(msg, network, nil) 532 533 network.DelNbrNode(testID) 534 } 535 536 // TestInvHandle tests Function InvHandle handling an inventory message 537 func TestInvHandle(t *testing.T) { 538 var testID uint64 539 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 540 key := keypair.SerializePublicKey(testPub) 541 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 542 assert.Nil(t, err) 543 544 remotePeer := peer.NewPeer() 545 assert.NotNil(t, remotePeer) 546 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 547 20337, testID, 0, 12345) 548 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 549 550 network.AddNbrNode(remotePeer) 551 552 hash := ledger.DefLedger.GetBlockHash(0) 553 assert.NotEqual(t, hash, common.UINT256_EMPTY) 554 555 buf := bytes.NewBuffer([]byte{}) 556 hash.Serialize(buf) 557 invPayload := msgpack.NewInvPayload(common.BLOCK, 1, buf.Bytes()) 558 buffer, err := msgpack.NewInv(invPayload) 559 assert.Nil(t, err) 560 561 msg := &msgCommon.MsgPayload{ 562 Id: testID, 563 Addr: "127.0.0.1:50010", 564 Payload: buffer, 565 } 566 567 InvHandle(msg, network, nil) 568 569 network.DelNbrNode(testID) 570 } 571 572 // TestDisconnectHandle tests Function DisconnectHandle handling a disconnect event 573 func TestDisconnectHandle(t *testing.T) { 574 var testID uint64 575 _, testPub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 576 key := keypair.SerializePublicKey(testPub) 577 err := binary.Read(bytes.NewBuffer(key[:8]), binary.LittleEndian, &(testID)) 578 assert.Nil(t, err) 579 580 remotePeer := peer.NewPeer() 581 assert.NotNil(t, remotePeer) 582 remotePeer.UpdateInfo(time.Now(), 1, 12345678, 20336, 583 20337, testID, 0, 12345) 584 remotePeer.SyncLink.SetAddr("127.0.0.1:50010") 585 586 network.AddNbrNode(remotePeer) 587 588 var hdr types.MsgHdr 589 cmd := msgCommon.DISCONNECT_TYPE 590 copy(hdr.CMD[0:uint32(len(cmd))], cmd) 591 var buf bytes.Buffer 592 binary.Write(&buf, binary.LittleEndian, hdr) 593 msgbuf := buf.Bytes() 594 595 msg := &msgCommon.MsgPayload{ 596 Id: testID, 597 Addr: "127.0.0.1:50010", 598 Payload: msgbuf, 599 } 600 601 DisconnectHandle(msg, network, nil) 602 network.DelNbrNode(testID) 603 }