forked from iotaledger/goshimmer
/
spamming_functions.go
69 lines (60 loc) · 1.92 KB
/
spamming_functions.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
package evilspammer
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/cockroachdb/errors"
"github.com/izuc/zipp/client/evilwallet"
"github.com/izuc/zipp/packages/core/ledger/vm/devnetvm"
)
func DataSpammingFunction(s *Spammer) {
clt := s.Clients.GetClient()
// sleep randomly to avoid issuing blocks in different goroutines at once
time.Sleep(time.Duration(rand.Float64()*20) * time.Millisecond)
if err := evilwallet.RateSetterSleep(clt, s.UseRateSetter); err != nil {
s.ErrCounter.CountError(err)
}
blkID, err := clt.PostData([]byte(fmt.Sprintf("SPAM")))
if err != nil {
s.ErrCounter.CountError(ErrFailSendDataBlock)
}
count := s.State.txSent.Add(1)
if count%int64(s.SpamDetails.Rate*2) == 0 {
s.log.Debugf("Last sent block, ID: %s; blkCount: %d", blkID, count)
}
s.State.batchPrepared.Add(1)
s.CheckIfAllSent()
}
func CustomConflictSpammingFunc(s *Spammer) {
conflictBatch, aliases, err := s.EvilWallet.PrepareCustomConflictsSpam(s.EvilScenario)
if err != nil {
s.log.Debugf(errors.Newf("%v: %w", ErrFailToPrepareBatch, err).Error())
s.ErrCounter.CountError(errors.Newf("%v: %w", ErrFailToPrepareBatch, err))
}
for _, txs := range conflictBatch {
clients := s.Clients.GetClients(len(txs))
if len(txs) > len(clients) {
s.log.Debug(ErrFailToPrepareBatch)
s.ErrCounter.CountError(ErrInsufficientClients)
}
// send transactions in parallel
wg := sync.WaitGroup{}
for i, tx := range txs {
wg.Add(1)
go func(clt evilwallet.Client, tx *devnetvm.Transaction) {
defer wg.Done()
// sleep randomly to avoid issuing blocks in different goroutines at once
time.Sleep(time.Duration(rand.Float64()*100) * time.Millisecond)
if err = evilwallet.RateSetterSleep(clt, s.UseRateSetter); err != nil {
s.ErrCounter.CountError(err)
}
s.PostTransaction(tx, clt)
}(clients[i], tx)
}
wg.Wait()
}
s.State.batchPrepared.Add(1)
s.EvilWallet.ClearAliases(aliases)
s.CheckIfAllSent()
}