merge configreader

pull/47/head
Richard Liu 6 years ago
parent b21859108c
commit 12c83d422c
  1. 3
      aws-experiment-launch/experiment/commander/main.go
  2. 15
      aws-experiment-launch/experiment/soldier/main.go
  3. 21
      aws-experiment-launch/experiment/utils/utils.go
  4. 17
      benchmark.go
  5. 59
      client/txgen/main.go
  6. 65
      configreader/main.go

@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"harmony-benchmark/aws-experiment-launch/experiment/utils"
"harmony-benchmark/configreader"
"io"
"log"
"net"
@ -40,7 +41,7 @@ func readConfigFile() [][]string {
panic(err)
}
if result, err := utils.ReadDistributionConfig(DistributionFileName); err != nil {
if result, err := configreader.ReadConfigFile(DistributionFileName); err != nil {
panic(err)
} else {
return result

@ -7,6 +7,7 @@ import (
"fmt"
"harmony-benchmark/aws-experiment-launch/experiment/soldier/s3"
"harmony-benchmark/aws-experiment-launch/experiment/utils"
"harmony-benchmark/configreader"
"io"
"io/ioutil"
"log"
@ -286,7 +287,7 @@ func runCmd(name string, args ...string) error {
}
func runInstance() error {
config := readConfigFile(globalSession.localConfigFileName)
config, _ := configreader.ReadConfigFile(globalSession.localConfigFileName)
myConfig := getMyConfig(setting.ip, setting.port, &config)
@ -309,18 +310,6 @@ func runClient() error {
return runCmd("./txgen", "-config_file", globalSession.localConfigFileName, "-log_folder", globalSession.logFolder)
}
func readConfigFile(configFile string) [][]string {
file, _ := os.Open(configFile)
fscanner := bufio.NewScanner(file)
result := [][]string{}
for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
result = append(result, p)
}
return result
}
func getMyConfig(myIP string, myPort string, config *[][]string) []string {
for _, node := range *config {
ip, port := node[0], node[1]

@ -1,12 +1,9 @@
package utils
import (
"bufio"
"io"
"log"
"net/http"
"os"
"strings"
)
func DownloadFile(filepath string, url string) error {
@ -31,21 +28,3 @@ func DownloadFile(filepath string, url string) error {
}
return nil
}
func ReadDistributionConfig(filename string) ([][]string, error) {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
log.Fatal("Failed to read config file ", filename)
return nil, err
}
fscanner := bufio.NewScanner(file)
result := [][]string{}
for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
result = append(result, p)
}
log.Println(result)
return result, nil
}

@ -1,17 +1,16 @@
package main
import (
"bufio"
"flag"
"fmt"
"harmony-benchmark/attack"
"harmony-benchmark/configreader"
"harmony-benchmark/consensus"
"harmony-benchmark/log"
"harmony-benchmark/node"
"harmony-benchmark/p2p"
"math/rand"
"os"
"strings"
"time"
"github.com/shirou/gopsutil/process"
@ -68,18 +67,6 @@ func getClientPeer(config *[][]string) *p2p.Peer {
return nil
}
func readConfigFile(configFile string) [][]string {
file, _ := os.Open(configFile)
fscanner := bufio.NewScanner(file)
result := [][]string{}
for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
result = append(result, p)
}
return result
}
func attackDetermination(attackedMode int) bool {
switch attackedMode {
case 0:
@ -127,7 +114,7 @@ func main() {
// Attack determination.
attack.GetInstance().SetAttackEnabled(attackDetermination(*attackedMode))
config := readConfigFile(*configFile)
config, _ := configreader.ReadConfigFile(*configFile)
shardID := getShardId(*ip, *port, &config)
peers := getPeers(*ip, *port, shardID, &config)
leader := getLeader(shardID, &config)

@ -1,20 +1,18 @@
package main
import (
"bufio"
"encoding/hex"
"flag"
"fmt"
"harmony-benchmark/blockchain"
"harmony-benchmark/client"
"harmony-benchmark/configreader"
"harmony-benchmark/consensus"
"harmony-benchmark/log"
"harmony-benchmark/node"
"harmony-benchmark/p2p"
"math/rand"
"os"
"strconv"
"strings"
"sync"
"time"
)
@ -166,55 +164,6 @@ UTXOLOOP:
return txs, crossTxs
}
// Gets all the validator peers
func getValidators(config string) []p2p.Peer {
file, _ := os.Open(config)
fscanner := bufio.NewScanner(file)
var peerList []p2p.Peer
for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
ip, port, status := p[0], p[1], p[2]
if status == "leader" || status == "client" {
continue
}
peer := p2p.Peer{Port: port, Ip: ip}
peerList = append(peerList, peer)
}
return peerList
}
// Gets all the leader peers and corresponding shard Ids
func getLeadersAndShardIds(config *[][]string) ([]p2p.Peer, []uint32) {
var peerList []p2p.Peer
var shardIds []uint32
for _, node := range *config {
ip, port, status, shardId := node[0], node[1], node[2], node[3]
if status == "leader" {
peerList = append(peerList, p2p.Peer{Ip: ip, Port: port})
val, err := strconv.Atoi(shardId)
if err == nil {
shardIds = append(shardIds, uint32(val))
} else {
log.Error("[Generator] Error parsing the shard Id ", shardId)
}
}
}
return peerList, shardIds
}
// Parse the config file and return a 2d array containing the file data
func readConfigFile(configFile string) [][]string {
file, _ := os.Open(configFile)
fscanner := bufio.NewScanner(file)
result := [][]string{}
for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
result = append(result, p)
}
return result
}
// Gets the port of the client node in the config
func getClientPort(config *[][]string) string {
for _, node := range *config {
@ -255,8 +204,8 @@ func main() {
flag.Parse()
// Read the configs
config := readConfigFile(*configFile)
leaders, shardIds := getLeadersAndShardIds(&config)
config, _ := configreader.ReadConfigFile(*configFile)
leaders, shardIds := configreader.GetLeadersAndShardIds(&config)
setting.numOfAddress = 10000
// Do cross shard tx if there are more than one shard
@ -360,6 +309,6 @@ func main() {
// Send a stop message to stop the nodes at the end
msg := node.ConstructStopMessage()
peers := append(getValidators(*configFile), leaders...)
peers := append(configreader.GetValidators(*configFile), leaders...)
p2p.BroadcastMessage(peers, msg)
}

@ -0,0 +1,65 @@
package configreader
import (
"bufio"
"harmony-benchmark/p2p"
"log"
"os"
"strconv"
"strings"
)
// Gets all the validator peers
func GetValidators(config string) []p2p.Peer {
file, _ := os.Open(config)
fscanner := bufio.NewScanner(file)
var peerList []p2p.Peer
for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
ip, port, status := p[0], p[1], p[2]
if status == "leader" || status == "client" {
continue
}
peer := p2p.Peer{Port: port, Ip: ip}
peerList = append(peerList, peer)
}
return peerList
}
// Gets all the leader peers and corresponding shard Ids
func GetLeadersAndShardIds(config *[][]string) ([]p2p.Peer, []uint32) {
var peerList []p2p.Peer
var shardIds []uint32
for _, node := range *config {
ip, port, status, shardId := node[0], node[1], node[2], node[3]
if status == "leader" {
peerList = append(peerList, p2p.Peer{Ip: ip, Port: port})
val, err := strconv.Atoi(shardId)
if err == nil {
shardIds = append(shardIds, uint32(val))
} else {
log.Print("[Generator] Error parsing the shard Id ", shardId)
}
}
}
return peerList, shardIds
}
// Parse the config file and return a 2d array containing the file data
func ReadConfigFile(filename string) ([][]string, error) {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
log.Fatal("Failed to read config file ", filename)
return nil, err
}
fscanner := bufio.NewScanner(file)
result := [][]string{}
for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
result = append(result, p)
}
log.Println(result)
return result, nil
}
Loading…
Cancel
Save