/
transactionFuzzer.go
119 lines (89 loc) · 2.41 KB
/
transactionFuzzer.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
package smartbft
import (
smartbft "github.com/SmartBFT-Go/consensus/pkg/consensus"
"github.com/hyperledger/fabric/common/flogging"
"math/rand"
"time"
)
type TransactionFuzzer struct {
consensus *smartbft.Consensus
Logger *flogging.FabricLogger
}
func NewTransactionFuzzer (cons *smartbft.Consensus) *TransactionFuzzer {
logger := flogging.MustGetLogger("orderer.consensus.smartbft.protocolFuzzer")
tf := &TransactionFuzzer{
consensus: cons,
Logger: logger,
}
return tf
}
var transactionSeeds [][]byte
func AddTransactionSeeds (seed []byte){
transactionSeeds = append(transactionSeeds, seed)
}
func getNextTxSeed() []byte{
if len(transactionSeeds) == 0{
return nil
}else{
return transactionSeeds[rand.Intn(len(transactionSeeds))]
}
}
func transactionFuzz() []byte{
var req []byte
req = getNextTxSeed()
return req
//return TransactionMessageFuzzed(req)
}
func (tf *TransactionFuzzer) RunTxFuzz(){
time.Sleep(time.Duration(60)*time.Second)
tf.Logger.Errorf("Begin running transaction fuzzing process!!!!!")
c := tf.consensus.GerContoller()
//if c.ID == c.GetLeaderID(){
if c.ID == 1 || c.ID ==3 || c.ID == 5{
//if c.ID == 1 || c.ID == 2 || c.ID == 3 { //only less than 1/3 nodes can be fuzzer nodes
for {
//if c.ID == c.GetLeaderID(){ //TODO only leader node send fuzzed messages
req := transactionFuzz()
//pf.Logger.Errorf("Is next fuzzed message is nil? %b", (msg==nil))
if req != nil {
tf.BroadcastTransactionsFuzz(req)
}
//}
//TODO
time.Sleep(time.Duration(5)*time.Second)
}
}
}
func (tf *TransactionFuzzer) BroadcastTransactionsFuzz(req []byte){
c := tf.consensus.GerContoller()
probability := percentageRandom()
tf.Logger.Infof("Start broadcastTransactionFuzz, probability is: %f", probability)
if probability < 0.7 {
//1. first strategy: send to all nodes
for _, node := range c.NodesList {
// Do not send to yourself
if c.ID == node {
continue
}
c.Comm.SendTransactionFuzz(node, req)
}
}else if probability < 0.85 {
//2. second strategy: send to half nodes
for i, node := range c.NodesList {
// TODO should randomly choose nodes
if i < 6{
continue
}
c.Comm.SendTransactionFuzz(node, req)
}
}else{
//3. third strategy: send to 1/3 nodes
for i, node := range c.NodesList {
// TODO should randomly choose nodes
if i < 8{
continue
}
c.Comm.SendTransaction(node, req)
}
}
}