diff --git a/aws-code/loghost/main.go b/aws-code/loghost/main.go new file mode 100644 index 000000000..ba30bafc4 --- /dev/null +++ b/aws-code/loghost/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "bufio" + "fmt" + "net" + "os" +) + +const ( + CONN_HOST = "localhost" + CONN_PORT = "3000" + CONN_TYPE = "tcp" + CONN_URL = CONN_HOST + ":" + CONN_PORT +) + +func main() { + // Listen for incoming connections. + l, err := net.Listen(CONN_TYPE, CONN_URL) + if err != nil { + fmt.Println("Error listening:", err.Error()) + os.Exit(1) + } + // Close the listener when the application closes. + defer l.Close() + fmt.Println("Listening on " + CONN_URL) + for { + // Listen for an incoming connection. + conn, err := l.Accept() + if err != nil { + fmt.Println("Error accepting: ", err.Error()) + os.Exit(1) + } + // Handle connections in a new goroutine. + go handleRequest(conn) + } +} + +// Handles incoming requests. +func handleRequest(conn net.Conn) { + for { + data, err := bufio.NewReader(conn).ReadString('\n') + if err != nil { + fmt.Println("Error reading:", err.Error()) + break + } + fmt.Println(data) + } +} diff --git a/benchmark.go b/benchmark.go index 0f659e1aa..9a5667d2a 100644 --- a/benchmark.go +++ b/benchmark.go @@ -86,10 +86,10 @@ func main() { // Setup a logger to stdout and log file. logFileName := fmt.Sprintf("./%v/%v.log", *logFolder, *port) h := log.MultiHandler( - log.Must.FileHandler(logFileName, log.LogfmtFormat()), - log.StdoutHandler) - // In cases where you just want a stdout logger, use the following one instead. - // h := log.CallerFileHandler(log.StdoutHandler) + log.StdoutHandler, + log.Must.FileHandler(logFileName, log.LogfmtFormat()), // Log to file + // log.Must.NetHandler("tcp", ":3000", log.JSONFormat()) // Log to remote + ) log.Root().SetHandler(h) consensus := consensus.NewConsensus(*ip, *port, shardId, peers, leader) diff --git a/client/txgen/main.go b/client/txgen/main.go index 4fbf605c7..ba6f0018a 100644 --- a/client/txgen/main.go +++ b/client/txgen/main.go @@ -257,10 +257,10 @@ func main() { // Setup a logger to stdout and log file. logFileName := fmt.Sprintf("./%v/tx-generator.log", *logFolder) h := log.MultiHandler( - log.Must.FileHandler(logFileName, log.LogfmtFormat()), - log.StdoutHandler) - // In cases where you just want a stdout logger, use the following one instead. - // h := log.CallerFileHandler(log.StdoutHandler) + log.StdoutHandler, + log.Must.FileHandler(logFileName, log.LogfmtFormat()), // Log to file + // log.Must.NetHandler("tcp", ":3000", log.JSONFormat()) // Log to remote + ) log.Root().SetHandler(h) // Nodes containing utxopools to mirror the shards' data in the network diff --git a/common/message.go b/common/message.go index b5a6d0853..c1dafc80d 100644 --- a/common/message.go +++ b/common/message.go @@ -4,6 +4,7 @@ import ( "errors" ) +// TODO: Fix the comments below. /* Node will process the content of the p2p message @@ -23,7 +24,15 @@ n - 2 bytes - actual message payload */ +// NODE_TYPE_BYTES is # of bytes message category const NODE_TYPE_BYTES = 1 + +// ACTION_TYPE_BYTES is # of bytes for message type which can be +// - for COMMITTEE category +// 0x00: consensus +// 0x01: sharding ... +// - for NODE category +// 0x00: transaction ... const ACTION_TYPE_BYTES = 1 // The CATEGORY of messages diff --git a/consensus/consensus.go b/consensus/consensus.go index b6681e864..97d6ff5d7 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -146,6 +146,7 @@ func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) C if consensus.IsLeader { consensus.ReadySignal = make(chan int) + // TODO: why do we need to do go rountine here. // send a signal to indicate it's ready to run consensus go func() { consensus.ReadySignal <- 1 diff --git a/kill_node.sh b/kill_node.sh index 9f8994133..f24623082 100755 --- a/kill_node.sh +++ b/kill_node.sh @@ -1,4 +1,4 @@ -for pid in `/bin/ps -fu $USER| grep "slave.go\|slave -port\|leader\|benchmark\|txgen" | grep -v "grep" | awk '{print $2}'`; +for pid in `/bin/ps -fu $USER| grep "benchmark\|txgen" | grep -v "grep" | awk '{print $2}'`; do echo 'Killed process: '$pid kill -9 $pid diff --git a/p2p/peer.go b/p2p/peer.go index 0b618a948..4cafdec17 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -6,6 +6,7 @@ import ( "log" "net" "strings" + "sync" ) // Peer is the object for a p2p peer (node) @@ -28,10 +29,16 @@ func BroadcastMessage(peers []Peer, msg []byte) { // Construct broadcast p2p message content := ConstructP2pMessage(byte(17), msg) - // TODO(rj): Can optimize by calling goroutine. + var wg sync.WaitGroup + wg.Add(len(peers)) for _, peer := range peers { - send(peer.Ip, peer.Port, content) + // send(peer.Ip, peer.Port, content) + go func() { + defer wg.Done() + send(peer.Ip, peer.Port, content) + }() } + wg.Wait() } // ConstructP2pMessage constructs the p2p message as [messageType, contentSize, content]