waitnode identity chain and identity block

pull/61/head
alok 6 years ago
parent 8e73fb7ada
commit 25c0a29135
  1. 2
      identitychain/identityblock.go
  2. 5
      identitychain/identitychain.go
  3. 23
      proto/identity/identity.go
  4. 35
      waitnode/waitNode.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[:]
}

@ -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)

@ -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()
}

@ -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)
p2p.SendMessage(peer, identity.ConstructIdentityMessage(identity.REGISTER, node.ID))
}
//for {
// conn, err := listen.Accept()
// if err != nil {
// node.log.Crit("Error listening on port. Exiting.", "port", port)
// continue
// }
// }
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…
Cancel
Save