-
Notifications
You must be signed in to change notification settings - Fork 202
/
common.go
74 lines (62 loc) · 2.06 KB
/
common.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
package antiflood
import (
"fmt"
"sync/atomic"
"time"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/ElrondNetwork/elrond-go/process/throttle/antiflood/floodPreventers"
"github.com/ElrondNetwork/elrond-go/storage/storageUnit"
)
// DurationBootstrapingTime -
const DurationBootstrapingTime = 2 * time.Second
// FloodTheNetwork -
func FloodTheNetwork(peer p2p.Messenger, topic string, isFlooding *atomic.Value, messageSize uint64) {
for {
_ = peer.BroadcastOnChannelBlocking(topic, topic, make([]byte, messageSize))
if !isFlooding.Load().(bool) {
return
}
}
}
// CreateTopicsAndMockInterceptors -
func CreateTopicsAndMockInterceptors(
peers []p2p.Messenger,
blacklistHandlers []floodPreventers.QuotaStatusHandler,
topic string,
peerMaxNumMessages uint32,
peerMaxSize uint64,
) ([]*MessageProcessor, error) {
interceptors := make([]*MessageProcessor, len(peers))
for idx, p := range peers {
err := p.CreateTopic(topic, true)
if err != nil {
return nil, fmt.Errorf("%w, pid: %s", err, p.ID())
}
cacherCfg := storageUnit.CacheConfig{Capacity: 100, Type: storageUnit.LRUCache, Shards: 1}
antifloodPool, _ := storageUnit.NewCache(cacherCfg)
interceptors[idx] = newMessageProcessor()
statusHandlers := []floodPreventers.QuotaStatusHandler{&nilQuotaStatusHandler{}}
if len(blacklistHandlers) == len(peers) {
statusHandlers = append(statusHandlers, blacklistHandlers[idx])
}
arg := floodPreventers.ArgQuotaFloodPreventer{
Name: "test",
Cacher: antifloodPool,
StatusHandlers: statusHandlers,
BaseMaxNumMessagesPerPeer: peerMaxNumMessages,
MaxTotalSizePerPeer: peerMaxSize,
PercentReserved: 0,
IncreaseThreshold: 0,
IncreaseFactor: 0,
}
interceptors[idx].FloodPreventer, err = floodPreventers.NewQuotaFloodPreventer(arg)
if err != nil {
return nil, err
}
err = p.RegisterMessageProcessor(topic, "test", interceptors[idx])
if err != nil {
return nil, fmt.Errorf("%w, pid: %s", err, p.ID())
}
}
return interceptors, nil
}