From 9266fdc26474f8edefb25c8591be24dd842d1957 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Thu, 5 Jul 2018 14:09:22 -0700 Subject: [PATCH] make sure attack_enabled default to false so that tnx client wont get attacked. also add more attack in validator --- attack/attack.go | 23 +++++++++++------------ consensus/consensus_validator.go | 6 ++++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/attack/attack.go b/attack/attack.go index 2c0e13dd4..e87f3bc31 100644 --- a/attack/attack.go +++ b/attack/attack.go @@ -10,7 +10,6 @@ import ( const ( DroppingTickDuration = 2 * time.Second - AttackEnabled = false HitRate = 10 DelayResponseDuration = 10 * time.Second ConsensusIdThresholdMin = 10 @@ -27,11 +26,11 @@ const ( // AttackModel contains different models of attacking. type Attack struct { - AttackEnabled bool - attackType AttackType - ConsensusIdThreshold uint32 - readyByConsensus bool - log log.Logger // Log utility + AttackEnabled bool + attackType AttackType + ConsensusIdThreshold uint32 + readyByConsensusThreshold bool + log log.Logger // Log utility } var attack *Attack @@ -47,8 +46,8 @@ func GetInstance() *Attack { } func (attack *Attack) Init() { - attack.AttackEnabled = AttackEnabled - attack.readyByConsensus = false + attack.AttackEnabled = false + attack.readyByConsensusThreshold = false } func (attack *Attack) SetAttackEnabled(AttackEnabled bool) { @@ -70,7 +69,7 @@ func (attack *Attack) Run() { // NodeKilledByItSelf runs killing itself attack func (attack *Attack) NodeKilledByItSelf() { - if !attack.AttackEnabled || attack.attackType != KilledItself || !attack.readyByConsensus { + if !attack.AttackEnabled || attack.attackType != KilledItself || !attack.readyByConsensusThreshold { return } @@ -81,7 +80,7 @@ func (attack *Attack) NodeKilledByItSelf() { } func (attack *Attack) DelayResponse() { - if !attack.AttackEnabled || attack.attackType != DelayResponse || !attack.readyByConsensus { + if !attack.AttackEnabled || attack.attackType != DelayResponse || !attack.readyByConsensusThreshold { return } if rand.Intn(HitRate) == 0 { @@ -91,7 +90,7 @@ func (attack *Attack) DelayResponse() { } func (attack *Attack) IncorrectResponse() bool { - if !attack.AttackEnabled || attack.attackType != IncorrectResponse || !attack.readyByConsensus { + if !attack.AttackEnabled || attack.attackType != IncorrectResponse || !attack.readyByConsensusThreshold { return false } if rand.Intn(HitRate) == 0 { @@ -103,6 +102,6 @@ func (attack *Attack) IncorrectResponse() bool { func (attack *Attack) UpdateConsensusReady(consensusId uint32) { if consensusId > attack.ConsensusIdThreshold { - attack.readyByConsensus = true + attack.readyByConsensusThreshold = true } } diff --git a/consensus/consensus_validator.go b/consensus/consensus_validator.go index c1c360df1..076de3183 100644 --- a/consensus/consensus_validator.go +++ b/consensus/consensus_validator.go @@ -101,6 +101,12 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) { consensus.blocksReceived[consensusId] = &BlockConsensusStatus{blockHeader, consensus.state} consensus.mutex.Unlock() + // Add attack model of IncorrectResponse. + if attack.GetInstance().IncorrectResponse() { + consensus.Log.Warn("IncorrectResponse attacked") + return + } + // check consensus Id if consensusId != consensus.consensusId { consensus.Log.Warn("Received message with wrong consensus Id", "myConsensusId", consensus.consensusId, "theirConsensusId", consensusId, "consensus", consensus)