-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
416 lines (364 loc) · 10.3 KB
/
main.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
"container/ring"
"crypto/rand"
"flag"
"fmt"
"github.com/iotaledger/iota.go/address"
"github.com/iotaledger/iota.go/api"
"github.com/iotaledger/iota.go/bundle"
"github.com/iotaledger/iota.go/checksum"
"github.com/iotaledger/iota.go/consts"
. "github.com/iotaledger/iota.go/guards/validators"
"github.com/iotaledger/iota.go/pow"
"github.com/iotaledger/iota.go/signing"
"github.com/iotaledger/iota.go/transaction"
"github.com/iotaledger/iota.go/trinary"
"github.com/pebbe/zmq4"
"strings"
"sync"
"sync/atomic"
"time"
)
func must(err error) {
if err != nil {
panic(err)
}
}
var instancesNum = flag.Int("instances", 5, "spammer instance counts")
var node = flag.String("node", "http://127.0.0.1:14265", "node to use")
var nodes = flag.String("nodes", "", "nodes to use")
var depth = flag.Int("depth", 1, "depth for gtta")
var mwm = flag.Int("mwm", 1, "mwm for pow")
var tag = flag.String("tag", "SPAMMER", "tag of txs")
var addr = flag.String("addr", strings.Repeat("9", 81), "the target address of the spam")
var zmq = flag.Bool("zmq", false, "use a zmq stream of txs as tips")
var valueBundles = flag.Bool("value", false, "spam value bundles")
var valueEntries = flag.Int("value-entries", 1, "value entries")
var valueSecLvl = flag.Int("value-sec-lvl", 2, "value sec level")
var zmqURL = flag.String("zmq-url", "tcp://127.0.0.1:5556", "the url of the zmq stream")
var zmqBuf = flag.Int("zmq-buf", 50, "the size of the zmq tx ring buffer")
var zmqNoTipSel = flag.Bool("zmq-no-tip-sel", false, "whether to not perform normal spam with tip-selection until the zmq buffer is full")
var bcBatchSize = flag.Int("bc-batch-size", 100, "how many txs to batch before submitting them to the node")
var targetAddr trinary.Hash
var emptySeed = strings.Repeat("9", 81)
func main() {
flag.Parse()
*addr = trinary.Pad(*addr, 81)
var err error
targetAddr, err = checksum.AddChecksum(*addr, true, consts.AddressChecksumTrytesSize)
must(err)
if *zmq {
for i := 0; i < *instancesNum; i++ {
go zmqSpammer()
}
if !*zmqNoTipSel {
accSpammer(*zmqBuf)
}
} else {
if len(*nodes) > 0 {
split := strings.Split(*nodes, ",")
for _, n := range split {
for i := 0; i < *instancesNum; i++ {
accSpammer(-1, n)
}
}
} else {
for i := 0; i < *instancesNum; i++ {
accSpammer(-1)
}
}
}
pad := strings.Repeat("", 10)
const pointsCount = 5
points := [pointsCount]int64{}
var index int
var tps float64
for {
s := atomic.LoadInt64(&spammed)
points[index] = s
index++
if index == 5 {
index = 0
var deltaSum int64
for i := 0; i < pointsCount-1; i++ {
deltaSum += points[i+1] - points[i]
}
tps = float64(deltaSum) / float64(pointsCount)
}
fmt.Printf("%s\r", pad)
fmt.Printf("\rspammed %d (tps %.2f)", s, tps)
<-time.After(time.Duration(1) * time.Second)
}
<-make(chan struct{})
}
const seedLength = 81
var tryteAlphabetLength = byte(len(consts.TryteAlphabet))
func GenerateSeed() (string, error) {
var by [seedLength]byte
if _, err := rand.Read(by[:]); err != nil {
return "", err
}
var seed string
for _, b := range by {
seed += string(consts.TryteAlphabet[b%tryteAlphabetLength])
}
return seed, nil
}
func accSpammer(stopAfter int, nodeToUse ...string) {
_, powFunc := pow.GetFastestProofOfWorkImpl()
_ = powFunc
var n string
if len(nodeToUse) > 0 {
n = nodeToUse[0]
} else {
n = *node
}
iotaAPI, err := api.ComposeAPI(api.HTTPClientSettings{URI: n, LocalProofOfWorkFunc: powFunc})
must(err)
spamTransfer := []bundle.Transfer{{Address: targetAddr, Tag: *tag}}
var bndl []trinary.Trytes
if *valueBundles {
seed, err := GenerateSeed()
if err != nil {
panic(err)
}
trnsf := []bundle.Transfer{}
inputs := []api.Input{}
for i := 0; i < *valueEntries; i++ {
addr, err := address.GenerateAddress(seed, uint64(i), consts.SecurityLevel(*valueSecLvl), true)
if err != nil {
fmt.Printf("error creating address: %s\n", err.Error())
panic(err)
}
trnsf = append(trnsf, bundle.Transfer{
Address: addr,
Tag: *tag,
Value: 100000000,
})
inputs = append(inputs, api.Input{
Address: addr,
KeyIndex: uint64(i),
Security: consts.SecurityLevel(*valueSecLvl),
Balance: 100000000,
})
}
bndl, err = PrepareTransfers(iotaAPI, seed, trnsf, api.PrepareTransfersOptions{Inputs: inputs,})
if err != nil {
fmt.Printf("error preparing transfer: %s\n", err.Error())
panic(err)
}
} else {
bndl, err = iotaAPI.PrepareTransfers(emptySeed, spamTransfer, api.PrepareTransfersOptions{})
if err != nil {
fmt.Printf("error preparing transfer: %s\n", err.Error())
panic(err)
}
}
go func() {
for {
tips, err := iotaAPI.GetTransactionsToApprove(uint64(*depth))
if err != nil {
fmt.Printf("error sending: %s\n", err.Error())
continue
}
powedBndl, err := iotaAPI.AttachToTangle(tips.TrunkTransaction, tips.BranchTransaction, uint64(*mwm), bndl)
if err != nil {
fmt.Printf("error doing PoW: %s\n", err.Error())
continue
}
_, err = iotaAPI.BroadcastTransactions(powedBndl...)
if err != nil {
fmt.Printf("error sending: %s\n", err.Error())
continue
}
if stopAfter != -1 {
stopAfter--
if stopAfter == 0 {
break
}
} else {
atomic.AddInt64(&spammed, 1)
}
}
}()
}
var spammed int64 = 0
func zmqSpammer() {
socket, err := zmq4.NewSocket(zmq4.SUB)
must(err)
must(socket.SetSubscribe("tx"))
err = socket.Connect(*zmqURL)
must(err)
var rMu sync.Mutex
r := ring.New(*zmqBuf)
go func() {
for {
msg, err := socket.Recv(0)
must(err)
split := strings.Split(msg, " ")
if len(split) != 13 {
continue
}
rMu.Lock()
r.Value = split[1]
r = r.Next()
rMu.Unlock()
}
}()
// wait for ring buffer to be filled up
for {
ready := true
filled := 0
r.Do(func(v interface{}) {
if v == nil {
ready = false
return
}
filled++
})
if ready {
break
}
fmt.Printf("\rwaiting for ring buffer to be filled (%d/%d)", filled, *zmqBuf)
<-time.After(time.Duration(1) * time.Second)
}
fmt.Printf("\nbuffer filled...")
_, powFunc := pow.GetFastestProofOfWorkImpl()
_ = powFunc
iotaAPI, err := api.ComposeAPI(api.HTTPClientSettings{URI: *node, LocalProofOfWorkFunc: powFunc})
must(err)
spamTransfer := []bundle.Transfer{{Address: targetAddr, Tag: *tag}}
bndl, err := iotaAPI.PrepareTransfers(emptySeed, spamTransfer, api.PrepareTransfersOptions{})
must(err)
for {
toBroadcast := []trinary.Trytes{}
for i := 0; i < *bcBatchSize; i++ {
rMu.Lock()
trunk := r.Prev().Value.(string)
branch := r.Next().Value.(string)
rMu.Unlock()
powedBndl, err := iotaAPI.AttachToTangle(trunk, branch, uint64(*mwm), bndl)
must(err)
tx, err := transaction.AsTransactionObject(powedBndl[0])
must(err)
hash := transaction.TransactionHash(tx)
rMu.Lock()
r.Value = hash
r = r.Next()
rMu.Unlock()
toBroadcast = append(toBroadcast, powedBndl[0])
}
_, err = iotaAPI.BroadcastTransactions(toBroadcast...)
must(err)
atomic.AddInt64(&spammed, int64(*bcBatchSize))
toBroadcast = []trinary.Trytes{}
}
}
func getPrepareTransfersDefaultOptions(options api.PrepareTransfersOptions) api.PrepareTransfersOptions {
if options.Security == 0 {
options.Security = consts.SecurityLevelMedium
}
if options.Inputs == nil {
options.Inputs = []api.Input{}
}
return options
}
func PrepareTransfers(apii *api.API, seed trinary.Trytes, transfers bundle.Transfers, opts api.PrepareTransfersOptions) ([]trinary.Trytes, error) {
opts = getPrepareTransfersDefaultOptions(opts)
if err := Validate(ValidateSeed(seed), ValidateSecurityLevel(opts.Security)); err != nil {
return nil, err
}
for i := range transfers {
if err := Validate(ValidateAddresses(transfers[i].Value != 0, transfers[i].Address)); err != nil {
return nil, err
}
}
var timestamp uint64
txs := transaction.Transactions{}
if opts.Timestamp != nil {
timestamp = *opts.Timestamp
} else {
timestamp = uint64(time.Now().UnixNano() / int64(time.Second))
}
var totalOutput uint64
for i := range transfers {
totalOutput += transfers[i].Value
}
// add transfers
outEntries, err := bundle.TransfersToBundleEntries(timestamp, transfers...)
if err != nil {
return nil, err
}
for i := range outEntries {
txs = bundle.AddEntry(txs, outEntries[i])
}
// add input transactions
var totalInput uint64
for i := range opts.Inputs {
if err := Validate(ValidateAddresses(opts.Inputs[i].Balance != 0, opts.Inputs[i].Address)); err != nil {
return nil, err
}
totalInput += opts.Inputs[i].Balance
input := &opts.Inputs[i]
bndlEntry := bundle.BundleEntry{
Address: input.Address[:consts.HashTrytesSize],
Value: -int64(input.Balance),
Length: uint64(input.Security),
Timestamp: timestamp,
}
txs = bundle.AddEntry(txs, bndlEntry)
}
// verify whether provided inputs fulfill threshold value
if totalInput < totalOutput {
return nil, consts.ErrInsufficientBalance
}
// finalize bundle by adding the bundle hash
finalizedBundle, err := bundle.Finalize(txs)
if err != nil {
return nil, err
}
// compute signatures for all input txs
normalizedBundleHash := signing.NormalizedBundleHash(finalizedBundle[0].Bundle)
signedFrags := []trinary.Trytes{}
for i := range opts.Inputs {
input := &opts.Inputs[i]
subseed, err := signing.Subseed(seed, input.KeyIndex)
if err != nil {
return nil, err
}
var sec consts.SecurityLevel
if input.Security == 0 {
sec = consts.SecurityLevelMedium
} else {
sec = input.Security
}
prvKey, err := signing.Key(subseed, sec)
if err != nil {
return nil, err
}
frags := make([]trinary.Trytes, input.Security)
for i := 0; i < int(input.Security); i++ {
signedFragTrits, err := signing.SignatureFragment(
normalizedBundleHash[i*consts.HashTrytesSize/3:(i+1)*consts.HashTrytesSize/3],
prvKey[i*consts.KeyFragmentLength:(i+1)*consts.KeyFragmentLength],
)
if err != nil {
return nil, err
}
frags[i] = trinary.MustTritsToTrytes(signedFragTrits)
}
signedFrags = append(signedFrags, frags...)
}
// add signed fragments to txs
var indexFirstInputTx int
for i := range txs {
if txs[i].Value < 0 {
indexFirstInputTx = i
break
}
}
txs = bundle.AddTrytes(txs, signedFrags, indexFirstInputTx)
// finally return built up txs as raw trytes
return transaction.MustFinalTransactionTrytes(txs), nil
}