@ -11,6 +11,7 @@ import (
"os"
"os"
"strings"
"strings"
"time"
"time"
"log"
)
)
func newRandTransaction ( ) blockchain . Transaction {
func newRandTransaction ( ) blockchain . Transaction {
@ -22,14 +23,14 @@ func newRandTransaction() blockchain.Transaction {
return tx
return tx
}
}
func getPeers ( Ip , Port , config string ) [ ] p2p . Peer {
func getValidators ( config string ) [ ] p2p . Peer {
file , _ := os . Open ( config )
file , _ := os . Open ( config )
fscanner := bufio . NewScanner ( file )
fscanner := bufio . NewScanner ( file )
var peerList [ ] p2p . Peer
var peerList [ ] p2p . Peer
for fscanner . Scan ( ) {
for fscanner . Scan ( ) {
p := strings . Split ( fscanner . Text ( ) , " " )
p := strings . Split ( fscanner . Text ( ) , " " )
ip , port , status := p [ 0 ] , p [ 1 ] , p [ 2 ]
ip , port , status := p [ 0 ] , p [ 1 ] , p [ 2 ]
if status == "leader" || ip == Ip && port == Port {
if status == "leader" {
continue
continue
}
}
peer := p2p . Peer { Port : port , Ip : ip }
peer := p2p . Peer { Port : port , Ip : ip }
@ -37,15 +38,39 @@ func getPeers(Ip, Port, config string) []p2p.Peer {
}
}
return peerList
return peerList
}
}
func main ( ) {
ip := flag . String ( "ip" , "127.0.0.1" , "IP of the leader" )
func getLeaders ( config * [ ] [ ] string ) [ ] p2p . Peer {
port := flag . String ( "port" , "9000" , "port of the leader." )
var peerList [ ] p2p . Peer
for _ , node := range * config {
ip , port , status := node [ 0 ] , node [ 1 ] , node [ 2 ]
if status == "leader" {
peerList = append ( peerList , p2p . Peer { Ip : ip , Port : port } )
}
}
return peerList
}
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 main ( ) {
configFile := flag . String ( "config_file" , "local_config.txt" , "file containing all ip addresses and config" )
configFile := flag . String ( "config_file" , "local_config.txt" , "file containing all ip addresses and config" )
//getLeader to get ip,port and get totaltime I want to run
flag . Parse ( )
config := readConfigFile ( * configFile )
start := time . Now ( )
start := time . Now ( )
totalTime := 60.0
totalTime := 60.0
txs := make ( [ ] blockchain . Transaction , 10 )
txs := make ( [ ] blockchain . Transaction , 10 )
leaders := getLeaders ( & config )
for true {
for true {
t := time . Now ( )
t := time . Now ( )
if t . Sub ( start ) . Seconds ( ) >= totalTime {
if t . Sub ( start ) . Seconds ( ) >= totalTime {
@ -57,13 +82,11 @@ func main() {
}
}
msg := node . ConstructTransactionListMessage ( txs )
msg := node . ConstructTransactionListMessage ( txs )
p2p . SendMessage ( p2p . Peer { * ip , * port , "n/a" } , msg )
log . Printf ( "[Generator] Sending txs to %d leader[s]\n" , len ( leaders ) )
p2p . BroadcastMessage ( leaders , msg )
time . Sleep ( 1 * time . Second ) // 10 transactions per second
time . Sleep ( 1 * time . Second ) // 10 transactions per second
}
}
msg := node . ConstructStopMessage ( )
msg := node . ConstructStopMessage ( )
var leaderPeer p2p . Peer
peers := append ( getValidators ( * configFile ) , leaders ... )
leaderPeer . Ip = * ip
leaderPeer . Port = * port
peers := append ( getPeers ( * ip , * port , * configFile ) , leaderPeer )
p2p . BroadcastMessage ( peers , msg )
p2p . BroadcastMessage ( peers , msg )
}
}