diff --git a/attack/attack.go b/attack/attack.go index 2e6c7860d..45e2ead55 100644 --- a/attack/attack.go +++ b/attack/attack.go @@ -29,7 +29,7 @@ const ( type Attack struct { AttackEnabled bool attackType AttackType - ConsensusIdThreshold int + ConsensusIdThreshold uint32 readyByConsensus bool log log.Logger // Log utility } @@ -37,8 +37,8 @@ type Attack struct { var attack *Attack var once sync.Once -// GetAttackModel returns attack model by using singleton pattern. -func GetAttackModel() *Attack { +// GetInstance returns attack model by using singleton pattern. +func GetInstance() *Attack { once.Do(func() { attack = &Attack{} attack.Init() @@ -55,7 +55,7 @@ func (attack *Attack) SetAttackEnabled(AttackEnabled bool) { attack.AttackEnabled = AttackEnabled if AttackEnabled { attack.attackType = AttackType(rand.Intn(3)) - attack.ConsensusIdThreshold = ConsensusIdThresholdMin + rand.Intn(ConsensusIdThresholdMax-ConsensusIdThresholdMin) + attack.ConsensusIdThreshold = uint32(ConsensusIdThresholdMin + rand.Intn(ConsensusIdThresholdMax-ConsensusIdThresholdMin)) } } @@ -63,9 +63,14 @@ func (attack *Attack) SetLogger(log log.Logger) { attack.log = log } +func (attack *Attack) Run() { + attack.NodeKilledByItSelf() + attack.DelayResponse() +} + // NodeKilledByItSelf runs killing itself attack func (attack *Attack) NodeKilledByItSelf() { - if !attack.AttackEnabled || attack.attackType != DelayResponse || !attack.readyByConsensus { + if !attack.AttackEnabled || attack.attackType != KilledItself || !attack.readyByConsensus { return } @@ -84,7 +89,7 @@ func (attack *Attack) DelayResponse() { } } -func (attack *Attack) UpdateConsensusReady(consensusId int) { +func (attack *Attack) UpdateConsensusReady(consensusId uint32) { if consensusId > attack.ConsensusIdThreshold { attack.readyByConsensus = true } diff --git a/benchmark.go b/benchmark.go index 8c8c37626..0667ece5b 100644 --- a/benchmark.go +++ b/benchmark.go @@ -102,7 +102,7 @@ func main() { rand.Seed(int64(time.Now().Nanosecond())) // Attack determination. - attack.GetAttackModel().SetAttackEnabled(attackDetermination(*attackedMode)) + attack.GetInstance().SetAttackEnabled(attackDetermination(*attackedMode)) config := readConfigFile(*configFile) shardId := getShardId(*ip, *port, &config) @@ -122,7 +122,7 @@ func main() { node := node.NewNode(&consensus) // Set logger to attack model. - attack.GetAttackModel().SetLogger(consensus.Log) + attack.GetInstance().SetLogger(consensus.Log) clientPeer := getClientPeer(&config) // If there is a client configured in the node list. diff --git a/consensus/consensus_validator.go b/consensus/consensus_validator.go index caf23d232..a8c4d0ee3 100644 --- a/consensus/consensus_validator.go +++ b/consensus/consensus_validator.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/binary" "encoding/gob" + "harmony-benchmark/attack" "harmony-benchmark/blockchain" "harmony-benchmark/p2p" "regexp" @@ -234,6 +235,9 @@ 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 diff --git a/p2p/peer.go b/p2p/peer.go index 3e32a11e7..e63df3ac9 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -80,7 +80,7 @@ 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.GetAttackModel().DelayResponse() + attack.GetInstance().Run() sendWithSocketClient(ip, port, message) return