diff --git a/attack/attack.go b/attack/attack.go index 45e2ead55..2c0e13dd4 100644 --- a/attack/attack.go +++ b/attack/attack.go @@ -22,7 +22,7 @@ type AttackType byte const ( KilledItself AttackType = iota DelayResponse - IncorrectTransaction + IncorrectResponse ) // AttackModel contains different models of attacking. @@ -75,7 +75,7 @@ func (attack *Attack) NodeKilledByItSelf() { } if rand.Intn(HitRate) == 0 { - attack.log.Debug("******Killing myself*******", "PID: ", os.Getpid()) + attack.log.Debug("******************Killing myself******************", "PID: ", os.Getpid()) os.Exit(1) } } @@ -85,10 +85,22 @@ func (attack *Attack) DelayResponse() { return } if rand.Intn(HitRate) == 0 { + attack.log.Debug("******************Attack: DelayResponse******************", "PID: ", os.Getpid()) time.Sleep(DelayResponseDuration) } } +func (attack *Attack) IncorrectResponse() bool { + if !attack.AttackEnabled || attack.attackType != IncorrectResponse || !attack.readyByConsensus { + return false + } + if rand.Intn(HitRate) == 0 { + attack.log.Debug("******************Attack: IncorrectResponse******************", "PID: ", os.Getpid()) + return true + } + return false +} + func (attack *Attack) UpdateConsensusReady(consensusId uint32) { if consensusId > attack.ConsensusIdThreshold { attack.readyByConsensus = true diff --git a/consensus/consensus_validator.go b/consensus/consensus_validator.go index a8c4d0ee3..c1c360df1 100644 --- a/consensus/consensus_validator.go +++ b/consensus/consensus_validator.go @@ -206,6 +206,9 @@ func (consensus *Consensus) processChallengeMessage(payload []byte) { // Verify block data and the aggregated signatures + // Update readyByConsensus for attack. + attack.GetInstance().UpdateConsensusReady(consensusId) + // check leader Id leaderPrivKey := consensus.leader.Ip + consensus.leader.Port reg, _ := regexp.Compile("[^0-9]+") @@ -217,6 +220,14 @@ func (consensus *Consensus) processChallengeMessage(payload []byte) { } consensus.mutex.Lock() + + // Add attack model of IncorrectResponse. + if attack.GetInstance().IncorrectResponse() { + consensus.Log.Warn("IncorrectResponse attacked") + consensus.mutex.Unlock() + return + } + // check block hash if bytes.Compare(blockHash[:], consensus.blockHash[:]) != 0 { consensus.Log.Warn("Block hash doesn't match", "consensus", consensus) @@ -235,9 +246,6 @@ func (consensus *Consensus) processChallengeMessage(payload []byte) { // If I received previous block (which haven't been processed. I will roll up to current block if everything checks. } - // Update readyByConsensus for attack. - attack.GetInstance().UpdateConsensusReady(consensusId) - // TODO: verify aggregated commits with real schnor cosign verification // TODO: return the signature(response) to leader