-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGetDataMessage.java
More file actions
283 lines (276 loc) · 11.5 KB
/
GetDataMessage.java
File metadata and controls
283 lines (276 loc) · 11.5 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* Copyright 2013-2014 Ronald W Hoffman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package JavaBitcoin;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* <p>The 'getdata' message is used to request one or more blocks and transactions.
* Blocks are returned as 'block' messages and transactions are returned as 'tx'
* messages. Any entries that are not found are returned as a 'notfound' response.</p>
*
* <p>GetData Message:</p>
* <pre>
* Size Field Definition
* ==== ===== ==========
* VarInt Count Number of inventory vectors
* Variable InvVectors One or more inventory vectors
* </pre>
*
* <p>Inventory Vector:</p>
* <pre>
* Size Field Description
* ==== ===== ===========
* 4 bytes Type 0=Error, 1=Transaction, 2=Block, 3=Filtered block
* 32 bytes Hash Object hash
* </pre>
*/
public class GetDataMessage {
/**
* Create a 'getdata' message
*
* @param peer Peer node
* @param type Request type (INV_TX or INV_BLOCK)
* @param hashList Hash list
* @return Message
*/
public static Message buildGetDataMessage(Peer peer, int type, List<Sha256Hash> hashList) {
int varCount = hashList.size();
byte[] varBytes = VarInt.encode(varCount);
byte[] msgData = new byte[varBytes.length+varCount*36];
//
// Build the message payload
//
System.arraycopy(varBytes, 0, msgData, 0, varBytes.length);
int offset = varBytes.length;
for (int i=0; i<varCount; i++) {
Sha256Hash hash = hashList.get(i);
Utils.uint32ToByteArrayLE(type, msgData, offset);
System.arraycopy(Utils.reverseBytes(hash.getBytes()), 0, msgData, offset+4, 32);
offset+=36;
}
//
// Build the message
//
ByteBuffer buffer = MessageHeader.buildMessage("getdata", msgData);
return new Message(buffer, peer,
(type==Parameters.INV_BLOCK?MessageHeader.INVBLOCK_CMD:MessageHeader.INVTX_CMD));
}
/**
* Process a 'getdata' message
*
* @param msg Message
* @param inStream Message data stream
* @throws EOFException End-of-data while processing message data
* @throws IOException Unable to read message data
* @throws VerificationException Data verification failed
*/
public static void processGetDataMessage(Message msg, ByteArrayInputStream inStream)
throws EOFException, IOException, VerificationException {
Peer peer = msg.getPeer();
int blocksSent = 0;
int txSent = 0;
//
// Get the number of inventory entries
//
int varCount = new VarInt(inStream).toInt();
if (varCount < 0 || varCount > 1000)
throw new VerificationException("More than 1000 inventory entries in 'getdata' message");
//
// Process each request
//
// If this is a restarted request, we need to skip over the requests that have already
// been processed as indicated by the restart index contained in the message.
//
List<byte[]> notFound = new LinkedList<>();
byte[] invBytes = new byte[36];
int restart = msg.getRestartIndex();
msg.setRestartIndex(0);
if (restart != 0)
inStream.skip(restart*36);
for (int i=restart; i<varCount; i++) {
//
// Defer the request if we have sent 25 blocks in the current batch
//
if (blocksSent == 25) {
msg.setRestartIndex(i);
break;
}
int count = inStream.read(invBytes);
if (count < 36)
throw new EOFException("End-of-data while processing 'getdata' message");
int invType = (int)Utils.readUint32LE(invBytes, 0);
Sha256Hash hash = new Sha256Hash(Utils.reverseBytes(invBytes, 4, 32));
if (invType == Parameters.INV_TX) {
//
// Send a transaction from the transaction memory pool. We won't send more
// than 500 transactions for a single 'getdata' request
//
if (txSent < 500) {
StoredTransaction tx;
synchronized(Parameters.lock) {
tx = Parameters.txMap.get(hash);
}
if (tx != null) {
txSent++;
ByteBuffer buffer = MessageHeader.buildMessage("tx", tx.getBytes());
Message txMsg = new Message(buffer, peer, MessageHeader.TX_CMD);
Parameters.networkListener.sendMessage(txMsg);
synchronized(Parameters.lock) {
Parameters.txSent++;
}
} else {
notFound.add(Arrays.copyOf(invBytes, 36));
}
} else {
notFound.add(Arrays.copyOf(invBytes, 36));
}
} else if (invType == Parameters.INV_BLOCK) {
//
// Send a block from the database or an archive file. We will send the
// blocks in increments of 10 to avoid running out of storage. If more
// then 10 blocks are requested, the request will be deferred until 10
// have been sent, then the request will resume with the next 10 blocks.
//
try {
Block block = Parameters.blockStore.getBlock(hash);
if (block != null) {
blocksSent++;
ByteBuffer buffer = MessageHeader.buildMessage("block", block.bitcoinSerialize());
Message blockMsg = new Message(buffer, peer, MessageHeader.BLOCK_CMD);
Parameters.networkListener.sendMessage(blockMsg);
synchronized(Parameters.lock) {
Parameters.blocksSent++;
}
} else {
notFound.add(Arrays.copyOf(invBytes, 36));
}
} catch (BlockStoreException exc) {
notFound.add(Arrays.copyOf(invBytes, 36));
}
} else if (invType == Parameters.INV_FILTERED_BLOCK) {
//
// Send a filtered block if the peer has loaded a Bloom filter
//
BloomFilter filter = peer.getBloomFilter();
if (filter == null)
continue;
//
// Get the block from the database and return not found if we don't have it
//
Block block;
try {
block = Parameters.blockStore.getBlock(hash);
} catch (BlockStoreException exc) {
block = null;
}
if (block == null) {
//
// Change the inventory type to INV_BLOCK so the client doesn't choke
// on the 'notfound' message
//
Utils.uint32ToByteArrayLE(Parameters.INV_BLOCK, invBytes, 0);
notFound.add(Arrays.copyOf(invBytes, 36));
continue;
}
//
// Find any matching transactions in the block
//
List<Sha256Hash> matches = filter.findMatches(block);
//
// Send a 'merkleblock' message followed by 'tx' messages for the matches
//
sendMatchedTransactions(peer, block, matches);
} else {
//
// Unrecognized message type
//
notFound.add(Arrays.copyOf(invBytes, 36));
}
}
//
// Create a 'notfound' response if we didn't find all of the requested items
//
if (!notFound.isEmpty()) {
varCount = notFound.size();
byte[] varBytes = VarInt.encode(varCount);
byte[] msgData = new byte[varCount*36+varBytes.length];
System.arraycopy(varBytes, 0, msgData, 0, varBytes.length);
int offset = varBytes.length;
for (byte[] invItem : notFound) {
System.arraycopy(invItem, 0, msgData, offset, 36);
offset += 36;
}
ByteBuffer buffer = MessageHeader.buildMessage("notfound", msgData);
msg.setBuffer(buffer);
msg.setCommand(MessageHeader.NOTFOUND_CMD);
}
}
/**
* Sends a 'merkleblock' message followed by 'tx' messages for the matched transaction
*
* @param peer Destination peer
* @param block Block containing the transactions
* @param matches List of matching transactions
* @throws IOException Error creating serialized data stream
*/
public static void sendMatchedTransactions(Peer peer, Block block, List<Sha256Hash> matches)
throws IOException {
//
// Build the index list for the matching transactions
//
List<Integer> txIndexes;
List<Transaction> txList = null;
if (matches.isEmpty()) {
txIndexes = new ArrayList<>();
} else {
txIndexes = new ArrayList<>(matches.size());
txList = block.getTransactions();
int index = 0;
for (Transaction tx : txList) {
if (matches.contains(tx.getHash()))
txIndexes.add(Integer.valueOf(index));
index++;
}
}
//
// Build and send the 'merkleblock' message
//
Message blockMsg = MerkleBlockMessage.buildMerkleBlockMessage(peer, block, txIndexes);
Parameters.networkListener.sendMessage(blockMsg);
synchronized(Parameters.lock) {
Parameters.filteredBlocksSent++;
}
//
// Send 'tx' messages for each matching transaction
//
for (Integer txIndex : txIndexes) {
Transaction tx = txList.get(txIndex.intValue());
byte[] txData = tx.getBytes();
ByteBuffer buffer = MessageHeader.buildMessage("tx", txData);
Message txMsg = new Message(buffer, peer, MessageHeader.TX_CMD);
Parameters.networkListener.sendMessage(txMsg);
synchronized(Parameters.lock) {
Parameters.txSent++;
}
}
}
}