You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.0 KiB
119 lines
3.0 KiB
7 years ago
|
package attack
|
||
|
|
||
|
import (
|
||
7 years ago
|
"math/rand"
|
||
|
"os"
|
||
7 years ago
|
"sync"
|
||
7 years ago
|
"time"
|
||
6 years ago
|
|
||
6 years ago
|
"github.com/harmony-one/harmony/log"
|
||
7 years ago
|
)
|
||
|
|
||
6 years ago
|
// Constants used for attack model.
|
||
7 years ago
|
const (
|
||
7 years ago
|
DroppingTickDuration = 2 * time.Second
|
||
|
HitRate = 10
|
||
|
DelayResponseDuration = 10 * time.Second
|
||
6 years ago
|
ConsensusIDThresholdMin = 10
|
||
|
ConsensusIDThresholdMax = 100
|
||
7 years ago
|
)
|
||
|
|
||
6 years ago
|
// Type is the type of attack model.
|
||
|
type Type byte
|
||
7 years ago
|
|
||
6 years ago
|
// Constants of different attack models.
|
||
7 years ago
|
const (
|
||
6 years ago
|
KilledItself Type = iota
|
||
7 years ago
|
DelayResponse
|
||
7 years ago
|
IncorrectResponse
|
||
7 years ago
|
)
|
||
|
|
||
6 years ago
|
// Model contains different models of attacking.
|
||
|
type Model struct {
|
||
7 years ago
|
AttackEnabled bool
|
||
6 years ago
|
attackType Type
|
||
|
ConsensusIDThreshold uint32
|
||
7 years ago
|
readyByConsensusThreshold bool
|
||
|
log log.Logger // Log utility
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
var attackModel *Model
|
||
7 years ago
|
var once sync.Once
|
||
|
|
||
7 years ago
|
// GetInstance returns attack model by using singleton pattern.
|
||
6 years ago
|
func GetInstance() *Model {
|
||
7 years ago
|
once.Do(func() {
|
||
6 years ago
|
attackModel = &Model{}
|
||
|
attackModel.Init()
|
||
7 years ago
|
})
|
||
6 years ago
|
return attackModel
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// Init initializes attack model.
|
||
|
func (attack *Model) Init() {
|
||
7 years ago
|
attack.AttackEnabled = false
|
||
|
attack.readyByConsensusThreshold = false
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// SetAttackEnabled sets attack model enabled.
|
||
|
func (attack *Model) SetAttackEnabled(AttackEnabled bool) {
|
||
7 years ago
|
attack.AttackEnabled = AttackEnabled
|
||
|
if AttackEnabled {
|
||
6 years ago
|
attack.attackType = Type(rand.Intn(3))
|
||
|
attack.ConsensusIDThreshold = uint32(ConsensusIDThresholdMin + rand.Intn(ConsensusIDThresholdMax-ConsensusIDThresholdMin))
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// SetLogger sets the logger for doing logging.
|
||
|
func (attack *Model) SetLogger(log log.Logger) {
|
||
7 years ago
|
attack.log = log
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// Run runs enabled attacks.
|
||
|
func (attack *Model) Run() {
|
||
7 years ago
|
attack.NodeKilledByItSelf()
|
||
|
attack.DelayResponse()
|
||
|
}
|
||
|
|
||
7 years ago
|
// NodeKilledByItSelf runs killing itself attack
|
||
6 years ago
|
func (attack *Model) NodeKilledByItSelf() {
|
||
7 years ago
|
if !attack.AttackEnabled || attack.attackType != KilledItself || !attack.readyByConsensusThreshold {
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if rand.Intn(HitRate) == 0 {
|
||
7 years ago
|
attack.log.Debug("******************Killing myself******************", "PID: ", os.Getpid())
|
||
7 years ago
|
os.Exit(1)
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
// DelayResponse does attack by delaying response.
|
||
|
func (attack *Model) DelayResponse() {
|
||
7 years ago
|
if !attack.AttackEnabled || attack.attackType != DelayResponse || !attack.readyByConsensusThreshold {
|
||
7 years ago
|
return
|
||
|
}
|
||
|
if rand.Intn(HitRate) == 0 {
|
||
6 years ago
|
attack.log.Debug("******************Model: DelayResponse******************", "PID: ", os.Getpid())
|
||
7 years ago
|
time.Sleep(DelayResponseDuration)
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
// IncorrectResponse returns if the attack model enable incorrect responding.
|
||
|
func (attack *Model) IncorrectResponse() bool {
|
||
7 years ago
|
if !attack.AttackEnabled || attack.attackType != IncorrectResponse || !attack.readyByConsensusThreshold {
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
if rand.Intn(HitRate) == 0 {
|
||
6 years ago
|
attack.log.Debug("******************Model: IncorrectResponse******************", "PID: ", os.Getpid())
|
||
7 years ago
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
6 years ago
|
// UpdateConsensusReady enables an attack type given the current consensusID.
|
||
|
func (attack *Model) UpdateConsensusReady(consensusID uint32) {
|
||
|
if consensusID > attack.ConsensusIDThreshold {
|
||
7 years ago
|
attack.readyByConsensusThreshold = true
|
||
7 years ago
|
}
|
||
|
}
|