This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
inventory.go
138 lines (124 loc) · 3.5 KB
/
inventory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package synch
import (
"context"
"errors"
"fmt"
"github.com/Qitmeer/qitmeer/common/hash"
"github.com/Qitmeer/qitmeer/core/types"
"github.com/Qitmeer/qitmeer/p2p/common"
"github.com/Qitmeer/qitmeer/p2p/peers"
pb "github.com/Qitmeer/qitmeer/p2p/proto/v1"
libp2pcore "github.com/libp2p/go-libp2p-core"
)
func (s *Sync) sendInventoryRequest(ctx context.Context, pe *peers.Peer, inv *pb.Inventory) error {
ctx, cancel := context.WithTimeout(ctx, ReqTimeout)
defer cancel()
stream, err := s.Send(ctx, inv, RPCInventory, pe.GetID())
if err != nil {
log.Trace(fmt.Sprintf("Failed to send inventory request to peer=%v, err=%v", pe.GetID(), err.Error()))
return err
}
defer func() {
if err := stream.Reset(); err != nil {
log.Error(fmt.Sprintf("Failed to reset stream with protocol %s,%v", stream.Protocol(), err))
}
}()
code, errMsg, err := ReadRspCode(stream, s.Encoding())
if err != nil {
return err
}
if !code.IsSuccess() {
s.Peers().IncrementBadResponses(stream.Conn().RemotePeer(), "inventory request rsp")
return errors.New(errMsg)
}
return err
}
func (s *Sync) inventoryHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) *common.Error {
pe := s.peers.Get(stream.Conn().RemotePeer())
if pe == nil {
return ErrPeerUnknown
}
ctx, cancel := context.WithTimeout(ctx, HandleTimeout)
var err error
defer func() {
cancel()
}()
m, ok := msg.(*pb.Inventory)
if !ok {
err = fmt.Errorf("message is not type *pb.Inventory")
return ErrMessage(err)
}
err = s.handleInventory(m, pe)
if err != nil {
return ErrMessage(err)
}
e := s.EncodeResponseMsg(stream, nil)
if e != nil {
return e
}
return nil
}
func (s *Sync) handleInventory(msg *pb.Inventory, pe *peers.Peer) error {
if len(msg.Invs) <= 0 {
return nil
}
txs := []*hash.Hash{}
hasBlocks := false
for _, inv := range msg.Invs {
h := changePBHashToHash(inv.Hash)
if InvType(inv.Type) == InvTypeBlock {
hasBlocks = true
} else if InvType(inv.Type) == InvTypeTx {
if s.p2p.Config().DisableRelayTx {
continue
}
if s.haveInventory(inv) {
continue
}
txs = append(txs, h)
}
}
if hasBlocks {
//s.peerSync.GetBlocks(pe, blocks)
s.peerSync.UpdateGraphState(pe)
}
if len(txs) > 0 {
go s.peerSync.getTxs(pe, txs)
}
return nil
}
// haveInventory returns whether or not the inventory represented by the passed
// inventory vector is known. This includes checking all of the various places
// inventory can be when it is in different states such as blocks that are part
// of the main chain, on a side chain, in the orphan pool, and transactions that
// are in the memory pool (either the main pool or orphan pool).
func (s *Sync) haveInventory(invVect *pb.InvVect) bool {
h := changePBHashToHash(invVect.Hash)
switch InvType(invVect.Type) {
case InvTypeBlock:
// Ask chain if the block is known to it in any form (main
// chain, side chain, or orphan).
return s.p2p.BlockChain().HaveBlock(h)
case InvTypeTx:
// Ask the transaction memory pool if the transaction is known
// to it in any form (main pool or orphan).
if s.p2p.TxMemPool().HaveTransaction(h) {
return true
}
prevOut := types.TxOutPoint{Hash: *h}
for i := uint32(0); i < 2; i++ {
prevOut.OutIndex = i
entry, err := s.p2p.BlockChain().FetchUtxoEntry(prevOut)
if err != nil {
return false
}
if entry != nil && !entry.IsSpent() {
return true
}
}
return false
}
// The requested inventory is is an unsupported type, so just claim
// it is known to avoid requesting it.
return true
}