-
Notifications
You must be signed in to change notification settings - Fork 4
/
spammer.js
351 lines (323 loc) · 13.5 KB
/
spammer.js
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
345
346
347
348
349
350
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var performance = require("performance-now");
var IOTA = require("iota.lib.js");
// ---------------------------------------
// Configure your spammer properties here:
// ---------------------------------------
var REPEATER_ON = false; // REPEATER_ON = false: If you don't want the repeater functionality.
var SPAM_ON = true; // SPAMER_ON = false: If you don't want the spammer functionality (no PoW!).
// ^^^^^^^^^^^ One of the 2 options should be true,
// otherwise it aint doing nothing!
var VALUESPAM_ON = false; // VALUESPAM_ON = false:: If you don't want to spam with value (iota).
var USER_SEED = "USER_SEED"; // seed that contains iota and will be used for spamming with value.
var SPAM_MESSAGE = "SPAMSPAMSPAM"; // only A-Z and 9 allowed!
var SPAM_TAG = "YOURTAG" // only A-Z and 9 allowed!
var SPAM_FREQUENCY = 10 // minimum spam interval in seconds.
var SPAM_DEPTH_MIN = 3 // How deep to search for transactions to approve (minimum)
var SPAM_DEPTH_MAX = 12 // How deep to search for transactions to approve (maximum)
var IRI_PORT = 14600 // Must match your port configuration for iri process
var TESTNET = false; // Set to true only if you are using testnet.
var TIMER_INTERVAL = 900 // The msec interval between spammer wake-up
// -------- end of configrable part ------
// -------- javascript code --------------
// -------- Do not modify!! --------------
var allnine = '999999999999999999999999999999999999999999999999999999999999999999999999999999999';
var ignore_tips = null;
var new_tips = [];
var new_tips_step = []
var previous_milestone_idx = 0;
var current_milestone_idx = 0;
var lock = false;
var lock_spam = false;
var next_spam_time = 0;
var spam_count = 0;
var spam_timesum = 0;
var spam_starttime = 0;
var trunk_tx;
var branch_tx;
var iri_is_synced = false;
var wanted_milestone;
var enable_spam = false;
var balance_found = false;
var iota = new IOTA({
'host': 'http://localhost',
'port': IRI_PORT
});
var transfers = [{
'address': allnine,
'value': 0,
'message': SPAM_MESSAGE,
'tag': SPAM_TAG
}];
process.argv.forEach((val, index) => {
wanted_milestone = 0;
if (index == 2) {
wanted_milestone = val;
console.log(Date().toLocaleString() +"");
console.log(Date().toLocaleString() +" Repeater will start when milestone "+wanted_milestone+" is reached!");
console.log(Date().toLocaleString() +"");
}
});
// Start reading from stdin so we don't exit.
process.stdin.resume();
process.on('SIGINT', function () {
iota.api.interruptAttachingToTangle(function(e,s) {
if (e == null) {
console.log(Date().toLocaleString() +" *Attachting stopped");
process.exit(1);
} else {
console.log(Date().toLocaleString() +" *Attachting stopped with error");
console.log(Date().toLocaleString() + e)
process.exit(1);
}
});
});
function collect_tips_at_startup() {
ignore_tips = new Set();
iota.api.getNodeInfo(function(e,s) {
var solidMilestone = s.latestSolidSubtangleMilestoneIndex;
var milestone = s.latestMilestoneIndex;
if ((solidMilestone == 0) || (solidMilestone != milestone)) {
console.log("*INFO: waiting for synced with network.");
lock = false;
return;
}
iota.api.getTips(function(e,s) {
if (e != null) {
console.log(Date().toLocaleString() +" *ERROR: cannot get tips at startup");
console.log(Date().toLocaleString() +" iota.lib.js returns this error:");
console.log(Date().toLocaleString() + e);
process.exit(1);
}
var tips = s['hashes'];
for (var i=0;i<tips.length;i++) {
var key = tips[i].slice(0,12);
ignore_tips.add(key);
}
lock = false;
return;
});
});
}
function collect_fresh_arrived() {
// collect the new tips since startup or the last milestone
iota.api.getTips(function(e,s) {
if (e != null) {
console.log(Date().toLocaleString() +" *ERROR: cannot get tips");
console.log(Date().toLocaleString() +" iota.lib.js returns this error:");
console.log(Date().toLocaleString() + e);
process.exit(1);
}
var tips = s['hashes'];
for (var i=0;i<tips.length;i++) {
var key = tips[i].slice(0,12);
if (ignore_tips.has(key)==false) {
ignore_tips.add(key);
new_tips.push(tips[i]);
new_tips_step.push(tips[i]);
console.log("*INFO New tip: "+tips[i]);
}
}
// if milestone has changed, then re-broadcast the newcomers
if (previous_milestone_idx != current_milestone_idx) {
previous_milestone_idx = current_milestone_idx;
broadcast_fresh_arrived();
}
else {
if (new_tips_step.length > 9) {
broadcast_intermediate();
}
else {
lock = false
return;
}
}
});
}
function broadcast_fresh_arrived() {
if (new_tips.length > 0) {
console.log(Date().toLocaleString() +" *INFO --- Milestone has changed, rebroadcasting the "+new_tips.length+" most recent txs");
iota.api.getTrytes(new_tips, function(e,s) {
if (e != null) {
console.log(Date().toLocaleString() +" *ERROR cannot get trytes");
console.log(Date().toLocaleString() +" iota.lib.js returns this error:");
console.log(Date().toLocaleString() + e);
process.exit(1);
}
var trytes = s['trytes'];
iota.api.broadcastTransactions(trytes, function(e,s) {
if (e != null) {
console.log(Date().toLocaleString() +" *ERROR cannot broadcast");
console.log(Date().toLocaleString() +" iota.lib.js returns this error:");
console.log(Date().toLocaleString() + e);
process.exit(1);
}
new_tips = [];
new_tips_step = [];
lock = false;
});
});
}
else {
lock = false;
}
}
function broadcast_intermediate() {
if (new_tips_step.length > 0) {
console.log(Date().toLocaleString() +" *INFO --- rebroadcasting the "+new_tips_step.length+" most recent txs (intermediate step)");
iota.api.getTrytes(new_tips_step, function(e,s) {
if (e != null) {
console.log(Date().toLocaleString() +" *ERROR cannot get trytes");
console.log(Date().toLocaleString() +" iota.lib.js returns this error:");
console.log(Date().toLocaleString() + e);
process.exit(1);
}
var trytes = s['trytes'];
iota.api.broadcastTransactions(trytes, function(e,s) {
if (e != null) {
console.log(Date().toLocaleString() +" *ERROR cannot broadcast");
console.log(Date().toLocaleString() +" iota.lib.js returns this error:");
console.log(Date().toLocaleString() + e);
process.exit(1);
}
new_tips_step = [];
lock = false;
});
});
}
else {
lock = false;
}
}
function spam_spam_spam() {
spam_starttime = performance();
var seed = allnine;
// Change the seed if value spamming is enabled and balance has been found.
if(VALUESPAM_ON == true && balance_found == true) seed = USER_SEED;
var weight = 15;
if (TESTNET==true) weight = 9;
var depth = Math.floor(Math.random()*(SPAM_DEPTH_MAX-SPAM_DEPTH_MIN+1)+SPAM_DEPTH_MIN);
iota.api.sendTransfer(seed,depth,weight,transfers,function(e,s) {
if (e != null) {
console.log(Date().toLocaleString() +" *ERROR sendTransfer() failed");
console.log(Date().toLocaleString() +" iota.lib.js returns this error:");
console.log(Date().toLocaleString() + e);
process.exit(1);
}
spam_count++;
var ellapsed = performance()-spam_starttime;
spam_timesum += ellapsed;
if (VALUESPAM_ON == true) {
console.log(Date().toLocaleString() +" *INFO Spam type: value , spam count: "+spam_count+", last spam took "+Math.floor(ellapsed/1000)+" seconds, search depth was "+depth);
} else {
console.log(Date().toLocaleString() +" *INFO Spam type: message, spam count: "+spam_count+", last spam took "+Math.floor(ellapsed/1000)+" seconds, search depth was "+depth);
}
console.log(Date().toLocaleString() +" *INFO Average spam duration: "+Math.floor(spam_timesum/1000)/spam_count+" seconds (deliberate delays not included.)");
lock_spam = false;
});
}
function onMyTimer() {
if (lock) return;
lock = true;
// First, check if synced
iota.api.getNodeInfo(function(e,s) {
if (e) {
console.log(Date().toLocaleString() +" *INFO Waiting for iri connection.");
lock = false;
return;
}
var milestone = s.latestMilestone;
var solidMilestone = s.latestSolidSubtangleMilestone;
current_milestone_idx = s.latestMilestoneIndex;
if (milestone == allnine || solidMilestone == allnine || s.latestSolidSubtangleMilestoneIndex < s.latestMilestoneIndex ) {
console.log(Date().toLocaleString() +"*INFO Waiting for synchronization with network. Latest milestone idx: "+current_milestone_idx+". Latest solid milestone idx: "+s.latestSolidSubtangleMilestoneIndex);
iri_is_synced = false;
lock = false;
return;
}
else {
if (wanted_milestone > 0) {
console.log(Date().toLocaleString() +"wanted milestone is "+wanted_milestone);
if (s.latestSolidSubtangleMilestoneIndex < wanted_milestone) {
console.log(Date().toLocaleString() +"*INFO Waiting for synchronization with network. Latest milestone idx: "+current_milestone_idx+". Latest solid milestone idx: "+s.latestSolidSubtangleMilestoneIndex );
iri_is_synced = false;
}
else {
console.log("Date().toLocaleString() +*INFO Synchronized! Latest milestone idx: "+current_milestone_idx+". Latest solid milestone idx: "+s.latestSolidSubtangleMilestoneIndex );
iri_is_synced = true;
}
lock = false;
}
else {
console.log(Date().toLocaleString() +"*INFO Synchronized! Latest milestone idx: "+current_milestone_idx+". Latest solid milestone idx: "+s.latestSolidSubtangleMilestoneIndex );
iri_is_synced = true;
lock = false;
}
}
// synced is true
});
//enable_spam = false;
if (REPEATER_ON==true) {
if (ignore_tips == null) {
// at startup
collect_tips_at_startup();
}
else {
// when running
collect_fresh_arrived();
}
}
else {
lock = false;
}
if (iri_is_synced && enable_spam == true) {
if (lock_spam == false) {
lock_spam = true;
var now = performance();
if (SPAM_ON==true && now>next_spam_time) {
next_spam_time = now+SPAM_FREQUENCY*1000;
spam_spam_spam();
}
else {
lock_spam = false;
}
}
}
if (iri_is_synced && enable_spam == false) {
if (SPAM_ON == true && VALUESPAM_ON == true) {
console.log(Date().toLocaleString() +' *INFO Checking the seed for any balance...');
iota.api.getInputs(USER_SEED, function(e,s) {
if(s) {
var inputs = s.inputs;
if (inputs.length > 0) {
balance_found = true;
transfers = [{
'address': inputs[0].address,
'value': inputs[0].balance,
'message': SPAM_MESSAGE,
'tag': SPAM_TAG
}];
console.log(Date().toLocaleString() +' *INFO Value spamming started from/to address ' + inputs[0].address + ' with ' + inputs[0].balance+' iota.');
} else {
console.log(Date().toLocaleString() +' *INFO No balance was found in the seed!')
process.exit(1);
}
enable_spam = true;
} else {
console.log(Date().toLocaleString() + e);
process.exit(1);
}
});
} else {
enable_spam = true;
}
}
}
if(VALUESPAM_ON == true && SPAM_ON == true) {
console.log(Date().toLocaleString() +" RUNNING REPEATER: "+REPEATER_ON+", RUNNING VALUE SPAMMER: "+VALUESPAM_ON);
}
else {
console.log(Date().toLocaleString() + "RUNNING REPEATER: "+REPEATER_ON+", RUNNING MESSAGE SPAMMER: "+SPAM_ON);
}
onMyTimer();
setInterval(onMyTimer, TIMER_INTERVAL);