From 7c0c6bb829927a2931cd5f7dc616a7c36480472d Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 3 Jul 2018 22:28:28 -0700 Subject: [PATCH] change to attack into singleton, modify the logic --- attack/attack.go | 71 +++++++++++++++++++++++++++++++----------------- benchmark.go | 6 +--- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/attack/attack.go b/attack/attack.go index 199289efc..2e6c7860d 100644 --- a/attack/attack.go +++ b/attack/attack.go @@ -9,16 +9,29 @@ import ( ) const ( - DroppingTickDuration = 2 * time.Second - AttackEnabled = false - HitRate = 10 - DelayResponseDuration = 10 * time.Second + DroppingTickDuration = 2 * time.Second + AttackEnabled = false + HitRate = 10 + DelayResponseDuration = 10 * time.Second + ConsensusIdThresholdMin = 10 + ConsensusIdThresholdMax = 100 +) + +type AttackType byte + +const ( + KilledItself AttackType = iota + DelayResponse + IncorrectTransaction ) // AttackModel contains different models of attacking. type Attack struct { - AttackEnabled bool - log log.Logger // Log utility + AttackEnabled bool + attackType AttackType + ConsensusIdThreshold int + readyByConsensus bool + log log.Logger // Log utility } var attack *Attack @@ -28,43 +41,51 @@ var once sync.Once func GetAttackModel() *Attack { once.Do(func() { attack = &Attack{} - attack.AttackEnabled = AttackEnabled + attack.Init() }) return attack } -func (attack *Attack) SetLogger(log log.Logger) { - attack.log = log +func (attack *Attack) Init() { + attack.AttackEnabled = AttackEnabled + attack.readyByConsensus = false } -// Run runs all attack models in goroutine mode. -func (attack *Attack) Run() { - if !attack.AttackEnabled { - return +func (attack *Attack) SetAttackEnabled(AttackEnabled bool) { + attack.AttackEnabled = AttackEnabled + if AttackEnabled { + attack.attackType = AttackType(rand.Intn(3)) + attack.ConsensusIdThreshold = ConsensusIdThresholdMin + rand.Intn(ConsensusIdThresholdMax-ConsensusIdThresholdMin) } - // Adding attack model here. - go func() { - attack.NodeKilledByItSelf() - }() +} + +func (attack *Attack) SetLogger(log log.Logger) { + attack.log = log } // NodeKilledByItSelf runs killing itself attack func (attack *Attack) NodeKilledByItSelf() { - tick := time.Tick(DroppingTickDuration) - for { - <-tick - if rand.Intn(HitRate) == 0 { - attack.log.Debug("***********************Killing myself***********************", "PID: ", os.Getpid()) - os.Exit(1) - } + if !attack.AttackEnabled || attack.attackType != DelayResponse || !attack.readyByConsensus { + return + } + + if rand.Intn(HitRate) == 0 { + attack.log.Debug("******Killing myself*******", "PID: ", os.Getpid()) + os.Exit(1) } } func (attack *Attack) DelayResponse() { - if !attack.AttackEnabled { + if !attack.AttackEnabled || attack.attackType != DelayResponse || !attack.readyByConsensus { return } if rand.Intn(HitRate) == 0 { time.Sleep(DelayResponseDuration) } } + +func (attack *Attack) UpdateConsensusReady(consensusId int) { + if consensusId > attack.ConsensusIdThreshold { + attack.readyByConsensus = true + } +} diff --git a/benchmark.go b/benchmark.go index bd201b450..8c8c37626 100644 --- a/benchmark.go +++ b/benchmark.go @@ -102,7 +102,7 @@ func main() { rand.Seed(int64(time.Now().Nanosecond())) // Attack determination. - attack.GetAttackModel().AttackEnabled = attackDetermination(*attackedMode) + attack.GetAttackModel().SetAttackEnabled(attackDetermination(*attackedMode)) config := readConfigFile(*configFile) shardId := getShardId(*ip, *port, &config) @@ -148,9 +148,5 @@ func main() { }() } - // TODO(minhdoan): Enable it later after done attacking. - // Run attack. - attack.GetAttackModel().Run() - node.StartServer(*port) }