InvHandle handles the inventory message(block, transaction and consensus) from peer.
(data *msgTypes.MsgPayload, p2p p2p.P2P, pid *evtActor.PID, args ...interface{})
| 508 | // InvHandle handles the inventory message(block, |
| 509 | // transaction and consensus) from peer. |
| 510 | func InvHandle(data *msgTypes.MsgPayload, p2p p2p.P2P, pid *evtActor.PID, args ...interface{}) { |
| 511 | log.Trace("[p2p]receive inv message", data.Addr, data.Id) |
| 512 | var inv = data.Payload.(*msgTypes.Inv) |
| 513 | |
| 514 | remotePeer := p2p.GetPeer(data.Id) |
| 515 | if remotePeer == nil { |
| 516 | log.Debug("[p2p]remotePeer invalid in InvHandle") |
| 517 | return |
| 518 | } |
| 519 | if len(inv.P.Blk) == 0 { |
| 520 | log.Debug("[p2p]empty inv payload in InvHandle") |
| 521 | return |
| 522 | } |
| 523 | var id common.Uint256 |
| 524 | str := inv.P.Blk[0].ToHexString() |
| 525 | log.Debugf("[p2p]the inv type: 0x%x block len: %d, %s\n", |
| 526 | inv.P.InvType, len(inv.P.Blk), str) |
| 527 | |
| 528 | invType := common.InventoryType(inv.P.InvType) |
| 529 | switch invType { |
| 530 | case common.TRANSACTION: |
| 531 | log.Debug("[p2p]receive transaction message", id) |
| 532 | // TODO check the ID queue |
| 533 | id = inv.P.Blk[0] |
| 534 | trn, err := ledger.DefLedger.GetTransaction(id) |
| 535 | if trn == nil || err != nil { |
| 536 | msg := msgpack.NewTxnDataReq(id) |
| 537 | err = p2p.Send(remotePeer, msg) |
| 538 | if err != nil { |
| 539 | log.Warn(err) |
| 540 | return |
| 541 | } |
| 542 | } |
| 543 | case common.BLOCK: |
| 544 | log.Debug("[p2p]receive block message") |
| 545 | for _, id = range inv.P.Blk { |
| 546 | log.Debug("[p2p]receive inv-block message, hash is ", id) |
| 547 | // TODO check the ID queue |
| 548 | isContainBlock, err := ledger.DefLedger.IsContainBlock(id) |
| 549 | if err != nil { |
| 550 | log.Warn(err) |
| 551 | return |
| 552 | } |
| 553 | if !isContainBlock && msgTypes.LastInvHash != id { |
| 554 | msgTypes.LastInvHash = id |
| 555 | // send the block request |
| 556 | log.Infof("[p2p]inv request block hash: %x", id) |
| 557 | msg := msgpack.NewBlkDataReq(id) |
| 558 | err = p2p.Send(remotePeer, msg) |
| 559 | if err != nil { |
| 560 | log.Warn(err) |
| 561 | return |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | case common.CONSENSUS: |
| 566 | log.Debug("[p2p]receive consensus message") |
| 567 | id = inv.P.Blk[0] |