-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNotFoundMessage.java
More file actions
91 lines (88 loc) · 3.46 KB
/
NotFoundMessage.java
File metadata and controls
91 lines (88 loc) · 3.46 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
/**
* Copyright 2013 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.util.Iterator;
/**
* <p>A 'notfound' message is returned when one or more items in a 'getdata' request
* were not found.<p>
*
* <p>NotFound Message:</p>
* <pre>
* Size Field Description
* ==== ===== ===========
* VarInt Count Number of inventory vectors
* Variable InvVector One or more inventory vectors
* </pre>
*
* <p>Inventory Vector:</p>
* <pre>
* Size Field Description
* ==== ===== ===========
* 4 bytes Type 0=Error, 1=Transaction, 2=Block
* 32 bytes Hash Object hash
* </pre>
*/
public class NotFoundMessage {
/**
* Process a 'notfound' message
*
* @param msg Message
* @param inStream Message data stream
* @throws EOFException End-of-data processing input stream
* @throws IOException Unable to read input stream
* @throws VerificationException Verification error
*/
public static void processNotFoundMessage(Message msg, ByteArrayInputStream inStream)
throws EOFException, IOException, VerificationException {
byte[] bytes = new byte[36];
//
// Get the number of inventory vectors
//
int invCount = new VarInt(inStream).toInt();
if (invCount < 0 || invCount > 50000)
throw new VerificationException("More than 50,000 entries in 'notfound' message");
//
// Process the inventory vectors
//
for (int i=0; i<invCount; i++) {
int count = inStream.read(bytes);
if (count < 36)
throw new EOFException("'inv' message is too short");
int type = (int)Utils.readUint32LE(bytes, 0);
Sha256Hash hash = new Sha256Hash(Utils.reverseBytes(bytes, 4, 32));
synchronized(Parameters.lock) {
//
// Remove the request from the processedRequests list and put it
// back on the pendingRequests list. The network listener will
// then send the request to a different peer or discard it if
// all of the available peers have been contacted.
//
Iterator<PeerRequest> it = Parameters.processedRequests.iterator();
while (it.hasNext()) {
PeerRequest request = it.next();
if (request.getType()==type && request.getHash().equals(hash)) {
it.remove();
Parameters.pendingRequests.add(request);
break;
}
}
}
}
}
}