diff --git a/attack/attack.go b/attack/attack.go index f859700a4..778390d2b 100644 --- a/attack/attack.go +++ b/attack/attack.go @@ -1,7 +1,6 @@ package attack import ( - "harmony-benchmark/consensus" "harmony-benchmark/log" "math/rand" "os" @@ -9,9 +8,10 @@ import ( ) const ( - DroppingTickDuration = 2 * time.Second - AttackEnabled = false - HitRate = 10 + DroppingTickDuration = 2 * time.Second + AttackEnabled = false + HitRate = 10 + DelayResponseDuration = 10 * time.Second ) // AttackModel contains different models of attacking. @@ -19,10 +19,10 @@ type Attack struct { log log.Logger // Log utility } -func New(consensus *consensus.Consensus) *Attack { +func New(log log.Logger) *Attack { attackModel := Attack{} // Logger - attackModel.log = consensus.Log + attackModel.log = log return &attackModel } @@ -48,3 +48,12 @@ func (attack *Attack) NodeKilledByItSelf() { } } } + +func DelayResponse() { + if !AttackEnabled { + return + } + if rand.Intn(HitRate) == 0 { + time.Sleep(DelayResponseDuration) + } +} diff --git a/benchmark.go b/benchmark.go index 33bf33ad9..4c2548b61 100644 --- a/benchmark.go +++ b/benchmark.go @@ -101,7 +101,7 @@ func main() { consensus := consensus.NewConsensus(*ip, *port, shardId, peers, leader) node := node.NewNode(&consensus) - attack := attack.New(&consensus) + attack := attack.New(consensus.Log) clientPeer := getClientPeer(&config) // If there is a client configured in the node list. diff --git a/harmony-benchmark b/harmony-benchmark new file mode 100755 index 000000000..c45f72ef5 Binary files /dev/null and b/harmony-benchmark differ diff --git a/p2p/peer.go b/p2p/peer.go index 4cafdec17..a623048bf 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -3,6 +3,7 @@ package p2p import ( "bytes" "encoding/binary" + "harmony-benchmark/attack" "log" "net" "strings" @@ -77,6 +78,9 @@ func sendWithSocketClient(ip, port string, message []byte) (res string) { // Send a message to another node with given port. func send(ip, port string, message []byte) (returnMessage string) { + // Add attack code here. + attack.DelayResponse() + sendWithSocketClient(ip, port, message) return }