change to attack into singleton, modify the logic

pull/27/head
Minh Doan 6 years ago
parent 373433531d
commit 7c0c6bb829
  1. 71
      attack/attack.go
  2. 6
      benchmark.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
}
}

@ -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)
}

Loading…
Cancel
Save