forked from libsv/go-p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer_handler_mock.go
161 lines (122 loc) · 3.59 KB
/
peer_handler_mock.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package p2p
import (
"fmt"
"sync"
"github.com/libsv/go-p2p/chaincfg/chainhash"
"github.com/libsv/go-p2p/wire"
)
type MockPeerHandler struct {
mu sync.RWMutex
transactionGet []wire.InvVect
transactionGetBytes map[string][]byte
transactionSent []wire.MsgTx
transactionAnnouncements []wire.InvVect
transactionRejection []wire.MsgReject
transaction []wire.MsgTx
blockAnnouncements []wire.InvVect
block []wire.Message
blockTransactionHashes [][]*chainhash.Hash
}
func NewMockPeerHandler() *MockPeerHandler {
return &MockPeerHandler{
blockTransactionHashes: make([][]*chainhash.Hash, 0),
}
}
func (m *MockPeerHandler) HandleTransactionsGet(msgs []*wire.InvVect, _ PeerI) ([][]byte, error) {
m.mu.Lock()
defer m.mu.Unlock()
rawTxs := make([][]byte, 0)
for _, msg := range msgs {
m.transactionGet = append(m.transactionGet, *msg)
bytes, ok := m.transactionGetBytes[msg.Hash.String()]
if !ok {
return nil, fmt.Errorf("no bytes for transaction %s", msg.Hash.String())
}
rawTxs = append(rawTxs, bytes)
}
return rawTxs, nil
}
func (m *MockPeerHandler) GetTransactionGet() []wire.InvVect {
m.mu.RLock()
defer m.mu.RUnlock()
return m.transactionGet
}
func (m *MockPeerHandler) HandleTransactionSent(msg *wire.MsgTx, _ PeerI) error {
m.mu.Lock()
defer m.mu.Unlock()
m.transactionSent = append(m.transactionSent, *msg)
return nil
}
func (m *MockPeerHandler) GetTransactionSent() []wire.MsgTx {
m.mu.RLock()
defer m.mu.RUnlock()
return m.transactionSent
}
func (m *MockPeerHandler) HandleTransactionAnnouncement(msg *wire.InvVect, _ PeerI) error {
m.mu.Lock()
defer m.mu.Unlock()
m.transactionAnnouncements = append(m.transactionAnnouncements, *msg)
return nil
}
func (m *MockPeerHandler) GetTransactionAnnouncement() []wire.InvVect {
m.mu.RLock()
defer m.mu.RUnlock()
return m.transactionAnnouncements
}
func (m *MockPeerHandler) HandleTransactionRejection(rejMsg *wire.MsgReject, _ PeerI) error {
m.mu.Lock()
defer m.mu.Unlock()
m.transactionRejection = append(m.transactionRejection, *rejMsg)
return nil
}
func (m *MockPeerHandler) GetTransactionRejection() []wire.MsgReject {
m.mu.RLock()
defer m.mu.RUnlock()
return m.transactionRejection
}
func (m *MockPeerHandler) HandleTransaction(msg *wire.MsgTx, _ PeerI) error {
m.mu.Lock()
defer m.mu.Unlock()
m.transaction = append(m.transaction, *msg)
return nil
}
func (m *MockPeerHandler) GetTransaction() []wire.MsgTx {
m.mu.RLock()
defer m.mu.RUnlock()
return m.transaction
}
func (m *MockPeerHandler) HandleBlockAnnouncement(msg *wire.InvVect, _ PeerI) error {
m.mu.Lock()
defer m.mu.Unlock()
m.blockAnnouncements = append(m.blockAnnouncements, *msg)
return nil
}
func (m *MockPeerHandler) GetBlockAnnouncement() []wire.InvVect {
m.mu.RLock()
defer m.mu.RUnlock()
return m.blockAnnouncements
}
func (m *MockPeerHandler) HandleBlock(msg wire.Message, _ PeerI) error {
m.mu.Lock()
defer m.mu.Unlock()
blockIdx := len(m.block)
m.block = append(m.block, msg)
m.blockTransactionHashes = append(m.blockTransactionHashes, make([]*chainhash.Hash, 0))
if blockMsg, ok := msg.(*wire.MsgBlock); ok {
for _, tx := range blockMsg.Transactions {
ttx := tx.TxHash()
m.blockTransactionHashes[blockIdx] = append(m.blockTransactionHashes[blockIdx], &ttx)
}
}
return nil
}
func (m *MockPeerHandler) GetBlock() []wire.Message {
m.mu.RLock()
defer m.mu.RUnlock()
return m.block
}
func (m *MockPeerHandler) GetBlockTransactions(index int) []*chainhash.Hash {
m.mu.RLock()
defer m.mu.RUnlock()
return m.blockTransactionHashes[index]
}