From 25c0a29135d9292b3585b524786b0dff16cec8cc Mon Sep 17 00:00:00 2001 From: alok Date: Tue, 21 Aug 2018 00:22:03 -0700 Subject: [PATCH] waitnode identity chain and identity block --- identitychain/identityblock.go | 2 +- identitychain/identitychain.go | 5 +++-- proto/identity/identity.go | 23 +++++++++++++++++++-- waitnode/waitNode.go | 37 +++++++++++++++------------------- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/identitychain/identityblock.go b/identitychain/identityblock.go index 395417b35..32c0723b6 100644 --- a/identitychain/identityblock.go +++ b/identitychain/identityblock.go @@ -56,7 +56,7 @@ func (b *IdentityBlock) CalculateBlockHash() []byte { for _, id := range b.Identities { hashes = append(hashes, id) } - hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.ShardId)) + hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.NumIdentities) blockHash = sha256.Sum256(bytes.Join(hashes, []byte{})) return blockHash[:] } diff --git a/identitychain/identitychain.go b/identitychain/identitychain.go index 1e4114162..ffb12db1b 100644 --- a/identitychain/identitychain.go +++ b/identitychain/identitychain.go @@ -17,6 +17,7 @@ type IdentityChain struct { Identities []*IdentityBlock PendingIdentities []*waitnode.WaitNode log log.Logger + Peer p2p.Peer } //IdentityChainHandler handles registration of new Identities @@ -80,8 +81,8 @@ func (IDC *IdentityChain) MakeNewBlock() *IdentityBlock { } } -func (IDC *IdentityChain) listenOnPort(port string) { - listen, err := net.Listen("tcp4", ":"+port) +func (IDC *IdentityChain) listenOnPort() { + listen, err := net.Listen("tcp4", IDC.Peer.Ip + ":" IDC.Peer.Port) defer listen.Close() if err != nil { IDC.log.Crit("Socket listen port failed", "port", port, "err", err) diff --git a/proto/identity/identity.go b/proto/identity/identity.go index 95ac0c652..5458f348c 100644 --- a/proto/identity/identity.go +++ b/proto/identity/identity.go @@ -1,12 +1,22 @@ package identity import ( + "bytes" "errors" + + "github.com/simple-rules/harmony-benchmark/proto" ) // the number of bytes consensus message type occupies const IDENTITY_MESSAGE_TYPE_BYTES = 1 +type IdentityMessageType byte + +const ( + IDENTITY IdentityMessageType = iota + // TODO: add more types +) + type MessageType int const ( @@ -28,7 +38,7 @@ func (msgType MessageType) String() string { // GetIdentityMessageType Get the consensus message type from the identity message func GetIdentityMessageType(message []byte) (MessageType, error) { if len(message) < 1 { - return 0, errors.New("Failed to get consensus message type: no data available.") + return 0, errors.New("Failed to get identity message type: no data available.") } return MessageType(message[0]), nil } @@ -36,7 +46,16 @@ func GetIdentityMessageType(message []byte) (MessageType, error) { // GetIdentityMessagePayload message payload from the identity message func GetIdentityMessagePayload(message []byte) ([]byte, error) { if len(message) < 2 { - return []byte{}, errors.New("Failed to get consensus message payload: no data available.") + return []byte{}, errors.New("Failed to get identity message payload: no data available.") } return message[IDENTITY_MESSAGE_TYPE_BYTES:], nil } + +// Concatenate msgType as one byte with payload, and return the whole byte array +func ConstructIdentityMessage(identityMessageType MessageType, payload []byte) []byte { + byteBuffer := bytes.NewBuffer([]byte{byte(proto.IDENTITY)}) + byteBuffer.WriteByte(byte(IDENTITY)) + byteBuffer.WriteByte(byte(identityMessageType)) + byteBuffer.Write(payload) + return byteBuffer.Bytes() +} diff --git a/waitnode/waitNode.go b/waitnode/waitNode.go index c1dce71f7..14aa8d3cb 100644 --- a/waitnode/waitNode.go +++ b/waitnode/waitNode.go @@ -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 }