add logic of attack determination for each node

pull/27/head
Minh Doan 6 years ago
parent 96e39524b6
commit 373433531d
  1. 29
      attack/attack.go
  2. 25
      benchmark.go
  3. 2
      p2p/peer.go

@ -4,6 +4,7 @@ import (
"harmony-benchmark/log" "harmony-benchmark/log"
"math/rand" "math/rand"
"os" "os"
"sync"
"time" "time"
) )
@ -16,19 +17,29 @@ const (
// AttackModel contains different models of attacking. // AttackModel contains different models of attacking.
type Attack struct { type Attack struct {
log log.Logger // Log utility AttackEnabled bool
log log.Logger // Log utility
} }
func New(log log.Logger) *Attack { var attack *Attack
attackModel := Attack{} var once sync.Once
// Logger
attackModel.log = log // GetAttackModel returns attack model by using singleton pattern.
return &attackModel func GetAttackModel() *Attack {
once.Do(func() {
attack = &Attack{}
attack.AttackEnabled = AttackEnabled
})
return attack
}
func (attack *Attack) SetLogger(log log.Logger) {
attack.log = log
} }
// Run runs all attack models in goroutine mode. // Run runs all attack models in goroutine mode.
func (attack *Attack) Run() { func (attack *Attack) Run() {
if !AttackEnabled { if !attack.AttackEnabled {
return return
} }
// Adding attack model here. // Adding attack model here.
@ -49,8 +60,8 @@ func (attack *Attack) NodeKilledByItSelf() {
} }
} }
func DelayResponse() { func (attack *Attack) DelayResponse() {
if !AttackEnabled { if !attack.AttackEnabled {
return return
} }
if rand.Intn(HitRate) == 0 { if rand.Intn(HitRate) == 0 {

@ -15,6 +15,10 @@ import (
"time" "time"
) )
const (
AttackProbability = 20
)
func getShardId(myIp, myPort string, config *[][]string) string { func getShardId(myIp, myPort string, config *[][]string) string {
for _, node := range *config { for _, node := range *config {
ip, port, shardId := node[0], node[1], node[3] ip, port, shardId := node[0], node[1], node[3]
@ -74,16 +78,32 @@ func readConfigFile(configFile string) [][]string {
return result return result
} }
func attackDetermination(attackedMode int) bool {
switch attackedMode {
case 0:
return false
case 1:
return true
case 2:
return rand.Intn(100) < AttackProbability
}
return false
}
func main() { func main() {
ip := flag.String("ip", "127.0.0.1", "IP of the node") ip := flag.String("ip", "127.0.0.1", "IP of the node")
port := flag.String("port", "9000", "port of the node.") port := flag.String("port", "9000", "port of the node.")
configFile := flag.String("config_file", "config.txt", "file containing all ip addresses") configFile := flag.String("config_file", "config.txt", "file containing all ip addresses")
logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution") logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution")
attackedMode := flag.Int("attacked_mode", 0, "0 means not attacked, 1 means attacked, 2 means being open to be selected as attacked")
flag.Parse() flag.Parse()
// Set up randomization seed. // Set up randomization seed.
rand.Seed(int64(time.Now().Nanosecond())) rand.Seed(int64(time.Now().Nanosecond()))
// Attack determination.
attack.GetAttackModel().AttackEnabled = attackDetermination(*attackedMode)
config := readConfigFile(*configFile) config := readConfigFile(*configFile)
shardId := getShardId(*ip, *port, &config) shardId := getShardId(*ip, *port, &config)
peers := getPeers(*ip, *port, shardId, &config) peers := getPeers(*ip, *port, shardId, &config)
@ -101,7 +121,8 @@ func main() {
consensus := consensus.NewConsensus(*ip, *port, shardId, peers, leader) consensus := consensus.NewConsensus(*ip, *port, shardId, peers, leader)
node := node.NewNode(&consensus) node := node.NewNode(&consensus)
attack := attack.New(consensus.Log) // Set logger to attack model.
attack.GetAttackModel().SetLogger(consensus.Log)
clientPeer := getClientPeer(&config) clientPeer := getClientPeer(&config)
// If there is a client configured in the node list. // If there is a client configured in the node list.
@ -129,7 +150,7 @@ func main() {
// TODO(minhdoan): Enable it later after done attacking. // TODO(minhdoan): Enable it later after done attacking.
// Run attack. // Run attack.
attack.Run() attack.GetAttackModel().Run()
node.StartServer(*port) node.StartServer(*port)
} }

@ -80,7 +80,7 @@ func sendWithSocketClient(ip, port string, message []byte) (res string) {
// Send a message to another node with given port. // Send a message to another node with given port.
func send(ip, port string, message []byte) (returnMessage string) { func send(ip, port string, message []byte) (returnMessage string) {
// Add attack code here. // Add attack code here.
attack.DelayResponse() attack.GetAttackModel().DelayResponse()
sendWithSocketClient(ip, port, message) sendWithSocketClient(ip, port, message)
return return

Loading…
Cancel
Save