uncomment some code to get it built. alok will work on this later

pull/76/head
Minh Doan 6 years ago
parent 3009e5a332
commit de33e8a1a1
  1. 23
      identitychain/identitychain.go
  2. 111
      identitychain/identitychain_handler.go
  3. 36
      node/node.go
  4. 3
      node/node_handler.go

@ -2,7 +2,6 @@ package identitychain
import ( import (
"fmt" "fmt"
"math"
"math/rand" "math/rand"
"net" "net"
"os" "os"
@ -72,9 +71,9 @@ func (IDC *IdentityChain) BroadCastNewConfiguration() {
} }
//BroadCast Peer Infor to Node //BroadCast Peer Infor to Node
func (IDC *IdentityChain) SendPeerInfo(Node node) { // func (IDC *IdentityChain) SendPeerInfo(Node node) {
return // return
} // }
//CreateShardAssignment //CreateShardAssignment
func (IDC *IdentityChain) CreateShardAssignment() { func (IDC *IdentityChain) CreateShardAssignment() {
@ -102,15 +101,15 @@ func (IDC *IdentityChain) generateRandomPermutations(num int) {
// SelectIds as // SelectIds as
func (IDC *IdentityChain) SelectIds() { func (IDC *IdentityChain) SelectIds() {
selectNumber := IDC.NumberOfNodesInShard - len(IDC.Identities) // selectNumber := IDC.NumberOfNodesInShard - len(IDC.Identities)
// Insert the lines below once you have a identity block // Insert the lines below once you have a identity block
// IB := IDC.GetLatestBlock() // IB := IDC.GetLatestBlock()
// currentIDS := IB.GetIdentities() // currentIDS := IB.GetIdentities()
currentIDS := IDC.Identities // currentIDS := IDC.Identities
selectNumber = int(math.Min(float64(len(IDC.PendingIdentities)), float64(selectNumber))) // selectNumber = int(math.Min(float64(len(IDC.PendingIdentities)), float64(selectNumber)))
pending := IDC.PendingIdentities[:selectNumber] // pending := IDC.PendingIdentities[:selectNumber]
IDC.SelectedIdentitites = append(currentIDS, pending...) // IDC.SelectedIdentitites = append(currentIDS, pending...)
IDC.PendingIdentities = []*node.Node{} // IDC.PendingIdentities = []*node.Node{}
} }
@ -157,9 +156,9 @@ func New(Peer p2p.Peer) *IdentityChain {
IDC.NumberOfShards = 1 //to be filled via global config IDC.NumberOfShards = 1 //to be filled via global config
IDC.NumberOfNodesInShard = 500 //to be filled via global config IDC.NumberOfNodesInShard = 500 //to be filled via global config
IDC.Identities = make([]*node.Node, 0) IDC.Identities = make([]*node.Node, 0)
IDC.PendingIdentities = make([]*node.Node, 0) // IDC.PendingIdentities = make([]*node.Node, 0)
IDC.SelectedIdentitites = make([]*node.Node, 0) IDC.SelectedIdentitites = make([]*node.Node, 0)
IDC.PowMap = make(map[p2p.Peer]string) // IDC.PowMap = make(map[p2p.Peer]string)
return &IDC return &IDC
} }

@ -1,17 +1,11 @@
package identitychain package identitychain
import ( import (
"bytes"
"fmt" "fmt"
"math/rand"
"net" "net"
"os" "os"
"strconv"
"time"
"github.com/simple-rules/harmony-benchmark/node"
"github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/p2p"
"github.com/simple-rules/harmony-benchmark/pow"
"github.com/simple-rules/harmony-benchmark/proto" "github.com/simple-rules/harmony-benchmark/proto"
proto_identity "github.com/simple-rules/harmony-benchmark/proto/identity" proto_identity "github.com/simple-rules/harmony-benchmark/proto/identity"
) )
@ -56,9 +50,9 @@ func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) {
} }
switch idMsgType { switch idMsgType {
case proto_identity.Register: case proto_identity.Register:
IDC.registerIdentity(msgPayload) // IDC.registerIdentity(msgPayload)
case proto_identity.Announce: case proto_identity.Announce:
IDC.acceptNewConnection(msgPayload) // IDC.acceptNewConnection(msgPayload)
} }
} }
@ -66,55 +60,56 @@ func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) {
} }
} }
func (IDC *IdentityChain) registerIdentity(msgPayload []byte) { // TODO(alok): You removed pow package.
payload, err := proto_identity.GetIdentityMessagePayload(msgPayload) // func (IDC *IdentityChain) registerIdentity(msgPayload []byte) {
if err != nil { // payload, err := proto_identity.GetIdentityMessagePayload(msgPayload)
IDC.log.Error("identity payload not read") // if err != nil {
} else { // IDC.log.Error("identity payload not read")
fmt.Println("identity payload read") // } else {
} // fmt.Println("identity payload read")
fmt.Println("we are now registering identities") // }
offset := 0 // fmt.Println("we are now registering identities")
proof := payload[offset : offset+32] // offset := 0
offset = offset + 32 // proof := payload[offset : offset+32]
Node := node.DeserializeWaitNode(payload[offset:]) // offset = offset + 32
req := IDC.PowMap[Node.Self] // Node := node.DeserializeWaitNode(payload[offset:])
ok, err := pow.Check(req, string(proof), []byte("")) // req := IDC.PowMap[Node.Self]
fmt.Println(err) // ok, err := pow.Check(req, string(proof), []byte(""))
if ok { // fmt.Println(err)
fmt.Println("Proof of work accepted") // if ok {
IDC.PendingIdentities = append(IDC.PendingIdentities, Node) // fmt.Println("Proof of work accepted")
fmt.Println(len(IDC.PendingIdentities)) //Fix why IDC does not have log working. // IDC.PendingIdentities = append(IDC.PendingIdentities, Node)
} else { // fmt.Println(len(IDC.PendingIdentities)) //Fix why IDC does not have log working.
fmt.Println("identity proof of work not accepted") // } else {
} // fmt.Println("identity proof of work not accepted")
} // }
// }
func (IDC *IdentityChain) acceptNewConnection(msgPayload []byte) { // func (IDC *IdentityChain) acceptNewConnection(msgPayload []byte) {
identityPayload, err := proto_identity.GetIdentityMessagePayload(msgPayload) // identityPayload, err := proto_identity.GetIdentityMessagePayload(msgPayload)
if err != nil { // if err != nil {
fmt.Println("There was a error in reading the identity payload") // fmt.Println("There was a error in reading the identity payload")
} else { // } else {
fmt.Println("accepted new connection") // fmt.Println("accepted new connection")
} // }
fmt.Println("Sleeping for 2 secs ...") // fmt.Println("Sleeping for 2 secs ...")
time.Sleep(2 * time.Second) // time.Sleep(2 * time.Second)
Node := node.DeserializeWaitNode(identityPayload) // Node := node.DeserializeWaitNode(identityPayload)
buffer := bytes.NewBuffer([]byte{}) // buffer := bytes.NewBuffer([]byte{})
src := rand.NewSource(time.Now().UnixNano()) // src := rand.NewSource(time.Now().UnixNano())
rnd := rand.New(src) // rnd := rand.New(src)
challengeNonce := int((rnd.Int31())) // challengeNonce := int((rnd.Int31()))
req := pow.NewRequest(5, []byte(strconv.Itoa(challengeNonce))) // req := pow.NewRequest(5, []byte(strconv.Itoa(challengeNonce)))
IDC.PowMap[Node.Self] = req // IDC.PowMap[Node.Self] = req
buffer.Write([]byte(req)) // buffer.Write([]byte(req))
// 32 byte block hash // // 32 byte block hash
// buffer.Write(prevBlockHash) // // buffer.Write(prevBlockHash)
// The message is missing previous BlockHash, this is because we don't actively maintain a identitychain // // The message is missing previous BlockHash, this is because we don't actively maintain a identitychain
// This canbe included in the fulfill request. // // This canbe included in the fulfill request.
// Message should be encrypted and then signed to follow PKE. // // Message should be encrypted and then signed to follow PKE.
//IDC should accept node publickey, encrypt the nonce and blockhash // //IDC should accept node publickey, encrypt the nonce and blockhash
// Then sign the message by own private key and send the message back. // // Then sign the message by own private key and send the message back.
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, buffer.Bytes()) // msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, buffer.Bytes())
p2p.SendMessage(Node.Self, msgToSend) // p2p.SendMessage(Node.Self, msgToSend)
} // }

@ -15,7 +15,6 @@ import (
"github.com/simple-rules/harmony-benchmark/db" "github.com/simple-rules/harmony-benchmark/db"
"github.com/simple-rules/harmony-benchmark/log" "github.com/simple-rules/harmony-benchmark/log"
"github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/p2p"
"github.com/simple-rules/harmony-benchmark/pow"
"github.com/simple-rules/harmony-benchmark/proto/identity" "github.com/simple-rules/harmony-benchmark/proto/identity"
"github.com/simple-rules/harmony-benchmark/syncing" "github.com/simple-rules/harmony-benchmark/syncing"
) )
@ -168,23 +167,24 @@ func NewNodefromIDC(node *Node, consensus *consensus.Consensus, db *db.LDBDataba
return node return node
} }
func (node *Node) processPOWMessage(message []byte) { // TODO(alok): Fix it or revert the cl on 11/8.
payload, err := identity.GetIdentityMessagePayload(message) // func (node *Node) processPOWMessage(message []byte) {
if err != nil { // payload, err := identity.GetIdentityMessagePayload(message)
fmt.Println("Could not read payload") // if err != nil {
} // fmt.Println("Could not read payload")
IDCPeer := node.IDCPeer // }
// 4 byte challengeNonce id // IDCPeer := node.IDCPeer
req := string(payload) // // 4 byte challengeNonce id
proof, _ := pow.Fulfil(req, []byte("")) //"This could be blockhasdata" // req := string(payload)
buffer := bytes.NewBuffer([]byte{}) // proof, _ := pow.Fulfil(req, []byte("")) //"This could be blockhasdata"
proofBytes := make([]byte, 32) //proof seems to be 32 byte here // buffer := bytes.NewBuffer([]byte{})
copy(proofBytes[:], proof) // proofBytes := make([]byte, 32) //proof seems to be 32 byte here
buffer.Write(proofBytes) // copy(proofBytes[:], proof)
buffer.Write(node.SerializeWaitNode()) // buffer.Write(proofBytes)
msgPayload := buffer.Bytes() // buffer.Write(node.SerializeWaitNode())
p2p.SendMessage(IDCPeer, identity.ConstructIdentityMessage(identity.Register, msgPayload)) // msgPayload := buffer.Bytes()
} // p2p.SendMessage(IDCPeer, identity.ConstructIdentityMessage(identity.Register, msgPayload))
// }
// SerializeWaitNode serializes the node // SerializeWaitNode serializes the node
// https://stackoverflow.com/questions/12854125/how-do-i-dump-the-struct-into-the-byte-array-without-reflection/12854659#12854659 // https://stackoverflow.com/questions/12854125/how-do-i-dump-the-struct-into-the-byte-array-without-reflection/12854659#12854659

@ -77,7 +77,8 @@ func (node *Node) NodeHandler(conn net.Conn) {
switch messageType { switch messageType {
case proto_identity.Register: case proto_identity.Register:
fmt.Println("received a identity message") fmt.Println("received a identity message")
node.processPOWMessage(msgPayload) // TODO(ak): fix it.
// node.processPOWMessage(msgPayload)
case proto_identity.Announce: case proto_identity.Announce:
node.log.Error("Announce message should be sent to IdentityChain") node.log.Error("Announce message should be sent to IdentityChain")
} }

Loading…
Cancel
Save