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
6 years ago
|
package attack
|
||
|
|
||
|
import (
|
||
6 years ago
|
"math/rand"
|
||
|
"os"
|
||
6 years ago
|
"sync"
|
||
6 years ago
|
"time"
|
||
6 years ago
|
|
||
6 years ago
|
"github.com/harmony-one/harmony/log"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// Constants used for attack model.
|
||
6 years ago
|
const (
|
||
6 years ago
|
DroppingTickDuration = 2 * time.Second
|
||
|
HitRate = 10
|
||
|
DelayResponseDuration = 10 * time.Second
|
||
6 years ago
|
ConsensusIDThresholdMin = 10
|
||
|
ConsensusIDThresholdMax = 100
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// Type is the type of attack model.
|
||
|
type Type byte
|
||
6 years ago
|
|
||
6 years ago
|
// Constants of different attack models.
|
||
6 years ago
|
const (
|
||
6 years ago
|
KilledItself Type = iota
|
||
6 years ago
|
DelayResponse
|
||
6 years ago
|
IncorrectResponse
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// Model contains different models of attacking.
|
||
|
type Model struct {
|
||
6 years ago
|
AttackEnabled bool
|
||
6 years ago
|
attackType Type
|
||
|
ConsensusIDThreshold uint32
|
||
6 years ago
|
readyByConsensusThreshold bool
|
||
|
log log.Logger // Log utility
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
var attackModel *Model
|
||
6 years ago
|
var once sync.Once
|
||
|
|
||
6 years ago
|
// GetInstance returns attack model by using singleton pattern.
|
||
6 years ago
|
func GetInstance() *Model {
|
||
6 years ago
|
once.Do(func() {
|
||
6 years ago
|
attackModel = &Model{}
|
||
|
attackModel.Init()
|
||
6 years ago
|
})
|
||
6 years ago
|
return attackModel
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// Init initializes attack model.
|
||
|
func (attack *Model) Init() {
|
||
6 years ago
|
attack.AttackEnabled = false
|
||
|
attack.readyByConsensusThreshold = false
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// SetAttackEnabled sets attack model enabled.
|
||
|
func (attack *Model) SetAttackEnabled(AttackEnabled bool) {
|
||
6 years ago
|
attack.AttackEnabled = AttackEnabled
|
||
|
if AttackEnabled {
|
||
6 years ago
|
attack.attackType = Type(rand.Intn(3))
|
||
|
attack.ConsensusIDThreshold = uint32(ConsensusIDThresholdMin + rand.Intn(ConsensusIDThresholdMax-ConsensusIDThresholdMin))
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// SetLogger sets the logger for doing logging.
|
||
|
func (attack *Model) SetLogger(log log.Logger) {
|
||
6 years ago
|
attack.log = log
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// Run runs enabled attacks.
|
||
|
func (attack *Model) Run() {
|
||
6 years ago
|
attack.NodeKilledByItSelf()
|
||
|
attack.DelayResponse()
|
||
|
}
|
||
|
|
||
6 years ago
|
// NodeKilledByItSelf runs killing itself attack
|
||
6 years ago
|
func (attack *Model) NodeKilledByItSelf() {
|
||
6 years ago
|
if !attack.AttackEnabled || attack.attackType != KilledItself || !attack.readyByConsensusThreshold {
|
||
6 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if rand.Intn(HitRate) == 0 {
|
||
6 years ago
|
attack.log.Debug("******************Killing myself******************", "PID: ", os.Getpid())
|
||
6 years ago
|
os.Exit(1)
|
||
6 years ago
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// DelayResponse does attack by delaying response.
|
||
|
func (attack *Model) DelayResponse() {
|
||
6 years ago
|
if !attack.AttackEnabled || attack.attackType != DelayResponse || !attack.readyByConsensusThreshold {
|
||
6 years ago
|
return
|
||
|
}
|
||
|
if rand.Intn(HitRate) == 0 {
|
||
6 years ago
|
attack.log.Debug("******************Model: DelayResponse******************", "PID: ", os.Getpid())
|
||
6 years ago
|
time.Sleep(DelayResponseDuration)
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// IncorrectResponse returns if the attack model enable incorrect responding.
|
||
|
func (attack *Model) IncorrectResponse() bool {
|
||
6 years ago
|
if !attack.AttackEnabled || attack.attackType != IncorrectResponse || !attack.readyByConsensusThreshold {
|
||
6 years ago
|
return false
|
||
|
}
|
||
|
if rand.Intn(HitRate) == 0 {
|
||
6 years ago
|
attack.log.Debug("******************Model: IncorrectResponse******************", "PID: ", os.Getpid())
|
||
6 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 {
|
||
6 years ago
|
attack.readyByConsensusThreshold = true
|
||
6 years ago
|
}
|
||
|
}
|