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