-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAlert.java
More file actions
344 lines (310 loc) · 9.46 KB
/
Alert.java
File metadata and controls
344 lines (310 loc) · 9.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* 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.ArrayList;
import java.util.List;
/**
* <p>An alert is sent out by the development team to notify all peers in the network
* about a problem. The alert is displayed in the user interface and written to the
* log. It is also sent each time a node connects to another node until the relay time
* is exceeded or the alert is canceled.</p>
*
* <p>Alert Payload</p>
* <pre>
* Size Field Description
* ==== ===== ===========
* 4 bytes Version Alert version
* 8 bytes RelayUntil Relay the alert until this time (seconds)
* 8 bytes Expires Alert expires at this time (seconds)
* 4 bytes AlertID Unique identifier for this alert
* 4 bytes CancelID Cancel the alert with this identifier
* Set<int> CancelSet Set of alert identifiers to cancel
* 4 bytes Priority Relative priority
* String Comment Comment about the alert
* String Status Alert message to display and log
* String Reserved Reserved for future use
* </pre>
*/
public class Alert {
/** Alert payload */
private byte[] payload;
/** Alert signature */
private byte[] signature;
/** Alert version */
private int version;
/** Relay until time */
private long relayTime;
/** Expiration time */
private long expireTime;
/** Alert ID */
private int alertID;
/** Cancel ID */
private int cancelID;
/** Cancel set */
private List<Integer> cancelSet;
/** Min applicable version */
private int minVersion;
/** Max applicable version */
private int maxVersion;
/** Subversion */
private String subVersion;
/** Priority */
private int priority;
/** Comment */
private String comment;
/** Message */
private String message;
/** Cancel status */
private boolean isCanceled;
/**
* Creates an alert from the serialized message data
*
* @param payload Alert payload
* @param signature Alert signature
* @throws EOFException End-of-data while processing payload
* @throws IOException Error while reading serialized data
*/
public Alert(byte[] payload, byte[] signature) throws EOFException, IOException {
this.payload = payload;
this.signature = signature;
//
// Decode the payload
//
try (ByteArrayInputStream inStream = new ByteArrayInputStream(payload)) {
byte[] bytes = new byte[28];
//
// Get version, relayTime, expireTime, alertID and cancelID
//
int count = inStream.read(bytes);
if (count != 28)
throw new EOFException("End-of-data processing payload");
version = (int)Utils.readUint32LE(bytes, 0);
relayTime = Utils.readUint64LE(bytes, 4);
expireTime = Utils.readUint64LE(bytes, 12);
alertID = (int)Utils.readUint32LE(bytes, 20);
cancelID = (int)Utils.readUint32LE(bytes, 24);
//
// Get the cancel set
//
int setCount = new VarInt(inStream).toInt();
if (setCount > 0) {
cancelSet = new ArrayList<>(setCount);
for (int i=0; i<setCount; i++) {
count = inStream.read(bytes, 0, 4);
if (count != 4)
throw new EOFException("End-of-data processing payload");
cancelSet.add(Integer.valueOf((int)Utils.readUint32LE(bytes, 0)));
}
} else {
cancelSet = new ArrayList<>(1);
}
//
// Get minVersion and maxVersion
//
count = inStream.read(bytes, 0, 8);
if (count != 8)
throw new EOFException("End-of-data processing payload");
minVersion = (int)Utils.readUint32LE(bytes, 0);
maxVersion = (int)Utils.readUint32LE(bytes, 4);
//
// Get the subversion
//
int strLength = new VarInt(inStream).toInt();
if (strLength == 0) {
subVersion = "";
} else {
StringBuilder subString = new StringBuilder(strLength);
for (int i=0; i<strLength; i++) {
int codePoint = inStream.read();
if (codePoint < 0)
throw new EOFException("End-of-data processing payload");
subString.appendCodePoint(codePoint);
}
subVersion = subString.toString();
}
//
// Get the priority
//
count = inStream.read(bytes, 0, 4);
if (count != 4)
throw new EOFException("End-of-data processing payload");
priority = (int)Utils.readUint32LE(bytes, 0);
//
// Get the comment
//
strLength = new VarInt(inStream).toInt();
if (strLength == 0) {
comment = "";
} else {
StringBuilder subString = new StringBuilder(strLength);
for (int i=0; i<strLength; i++) {
int codePoint = inStream.read();
if (codePoint < 0)
throw new EOFException("End-of-data processing payload");
subString.appendCodePoint(codePoint);
}
comment = subString.toString();
}
//
// Get the alert message
//
strLength = new VarInt(inStream).toInt();
if (strLength == 0) {
message = "";
} else {
StringBuilder subString = new StringBuilder(strLength);
for (int i=0; i<strLength; i++) {
int codePoint = inStream.read();
if (codePoint < 0)
throw new EOFException("End-of-data processing payload");
subString.appendCodePoint(codePoint);
}
message = subString.toString();
}
}
}
/**
* Returns the payload
*
* @return Alert payload
*/
public byte[] getPayload() {
return payload;
}
/**
* Returns the signature
*
* @return Alert signature
*/
public byte[] getSignature() {
return signature;
}
/**
* Returns the alert ID
*
* @return Alert ID
*/
public int getID() {
return alertID;
}
/**
* Returns the cancel ID
*
* @return Cancel ID
*/
public int getCancelID() {
return cancelID;
}
/**
* Returns the cancel set
*
* @return Cancel set
*/
public List<Integer> getCancelSet() {
return cancelSet;
}
/**
* Returns the alert message
*
* @return Alert message
*/
public String getMessage() {
return message;
}
/**
* Returns the relay until time
*
* @return Relay until time
*/
public long getRelayTime() {
return relayTime;
}
/**
* Returns the expiration time
*
* @return Expiration time
*/
public long getExpireTime() {
return expireTime;
}
/**
* Returns the alert priority
*
* @return Alert priority
*/
public int getPriority() {
return priority;
}
/**
* Returns the message version
*
* @return Alert message version
*/
public int getVersion() {
return version;
}
/**
* Returns the minimum protocol version
*
* @return Minimum protocol version
*/
public int getMinVersion() {
return minVersion;
}
/**
* Returns the maximum protocol version
*
* @return Maximum protocol version
*/
public int getMaxVersion() {
return maxVersion;
}
/**
* Returns the user agent (subVersion)
*
* @return User agent
*/
public String getSubVersion() {
return subVersion;
}
/**
* Returns the developer comment
*
* @return Developer comment
*/
public String getComment() {
return comment;
}
/**
* Checks if the alert has been canceled
*
* @return TRUE if the alert has been canceled
*/
public boolean isCanceled() {
return isCanceled;
}
/**
* Sets the alert cancel status
*
* @param isCanceled TRUE if the alert has been canceled
*/
public void setCancel(boolean isCanceled) {
this.isCanceled = isCanceled;
}
}