parent
8e73fb7ada
commit
25c0a29135
@ -1,49 +1,44 @@ |
||||
package waitnode |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net" |
||||
"os" |
||||
"bytes" |
||||
"crypto/sha256" |
||||
|
||||
"github.com/simple-rules/harmony-benchmark/log" |
||||
"github.com/simple-rules/harmony-benchmark/p2p" |
||||
"github.com/simple-rules/harmony-benchmark/proto/identity" |
||||
"github.com/simple-rules/harmony-benchmark/utils" |
||||
) |
||||
|
||||
//WaitNode is for nodes waiting to join consensus
|
||||
type WaitNode struct { |
||||
Peer p2p.Peer |
||||
Log log.Logger |
||||
ID []byte |
||||
} |
||||
|
||||
// StartServer a server and process the request by a handler.
|
||||
func (node *WaitNode) StartServer(add p2p.Peer) { |
||||
func (node *WaitNode) StartServer() { |
||||
node.Log.Debug("Starting waitnode on server %d", "node", node.Peer.Ip, "port", node.Peer.Port) |
||||
node.connectIdentityChain(add.Port) |
||||
} |
||||
|
||||
func (node *WaitNode) connectIdentityChain(port string) { |
||||
func (node *WaitNode) connectIdentityChain(peer p2p.Peer) { |
||||
// replace by p2p peer
|
||||
identityChainIP := "127.0.0.1" |
||||
fmt.Println("Connecting to identity chain") |
||||
conn, err := net.Dial("tcp4", identityChainIP+":"+port) |
||||
defer conn.Close() |
||||
if err != nil { |
||||
node.Log.Crit("Socket listen port failed", "port", port, "err", err) |
||||
os.Exit(1) |
||||
} |
||||
//for {
|
||||
// conn, err := listen.Accept()
|
||||
// if err != nil {
|
||||
// node.log.Crit("Error listening on port. Exiting.", "port", port)
|
||||
// continue
|
||||
// }
|
||||
// }
|
||||
p2p.SendMessage(peer, identity.ConstructIdentityMessage(identity.REGISTER, node.ID)) |
||||
|
||||
} |
||||
func calculateHash(num string) []byte { |
||||
var hashes [][]byte |
||||
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(num)) |
||||
hash := sha256.Sum256(bytes.Join(hashes, []byte{})) |
||||
return hash[:] |
||||
} |
||||
|
||||
// New Create a new Node
|
||||
func New(Peer p2p.Peer) *WaitNode { |
||||
node := WaitNode{} |
||||
node.Peer = Peer |
||||
node.ID = calculateHash(Peer.Ip) |
||||
node.Log = log.New() |
||||
return &node |
||||
} |
||||
|
Loading…
Reference in new issue