The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/identitychain/identitychain_handler.go

121 lines
3.7 KiB

package identitychain
import (
6 years ago
"bytes"
"fmt"
6 years ago
"math/rand"
"net"
"os"
6 years ago
"strconv"
"time"
6 years ago
"github.com/simple-rules/harmony-benchmark/node"
"github.com/simple-rules/harmony-benchmark/p2p"
6 years ago
"github.com/simple-rules/harmony-benchmark/pow"
"github.com/simple-rules/harmony-benchmark/proto"
proto_identity "github.com/simple-rules/harmony-benchmark/proto/identity"
)
//IdentityChainHandler handles registration of new Identities
// This could have been its seperate package like consensus, but am avoiding creating a lot of packages.
func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) {
content, err := p2p.ReadMessageContent(conn)
if err != nil {
IDC.log.Error("Read p2p data failed")
return
}
msgCategory, err := proto.GetMessageCategory(content)
if err != nil {
IDC.log.Error("Read message category failed", "err", err)
return
}
if msgCategory != proto.IDENTITY {
IDC.log.Error("Identity Chain Recieved incorrect protocol message")
os.Exit(1)
6 years ago
} else {
fmt.Println("Message category is correct")
}
msgType, err := proto.GetMessageType(content)
if err != nil {
IDC.log.Error("Read action type failed")
return
}
msgPayload, err := proto.GetMessagePayload(content)
if err != nil {
IDC.log.Error("Read message payload failed")
return
}
switch msgCategory {
case proto.IDENTITY:
6 years ago
actionType := proto_identity.IdentityMessageType(msgType)
switch actionType {
6 years ago
case proto_identity.IDENTITY:
idMsgType, err := proto_identity.GetIdentityMessageType(msgPayload)
if err != nil {
fmt.Println("Error finding the identity message type")
}
switch idMsgType {
case proto_identity.REGISTER:
IDC.registerIdentity(msgPayload)
case proto_identity.Announce:
6 years ago
IDC.acceptNewConnection(msgPayload)
}
}
}
}
6 years ago
func (IDC *IdentityChain) registerIdentity(msgPayload []byte) {
6 years ago
payload, err := proto_identity.GetIdentityMessagePayload(msgPayload)
6 years ago
if err != nil {
IDC.log.Error("identity payload not read")
} else {
fmt.Println("identity payload read")
}
6 years ago
fmt.Println("we are now registering identities")
6 years ago
offset := 0
6 years ago
proof := payload[offset : offset+32]
offset = offset + 32
Node := node.DeserializeWaitNode(payload[offset:])
6 years ago
req := IDC.PowMap[Node.Self]
6 years ago
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")
}
6 years ago
}
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")
6 years ago
} else {
fmt.Println("accepted new connection")
6 years ago
}
6 years ago
fmt.Println("Sleeping for 2 secs ...")
6 years ago
time.Sleep(2 * time.Second)
6 years ago
Node := node.DeserializeWaitNode(identityPayload)
6 years ago
buffer := bytes.NewBuffer([]byte{})
6 years ago
src := rand.NewSource(time.Now().UnixNano())
rnd := rand.New(src)
6 years ago
challengeNonce := int((rnd.Int31()))
req := pow.NewRequest(5, []byte(strconv.Itoa(challengeNonce)))
IDC.PowMap[Node.Self] = req
buffer.Write([]byte(req))
6 years ago
// 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.
6 years ago
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.REGISTER, buffer.Bytes())
6 years ago
p2p.SendMessage(Node.Self, msgToSend)
6 years ago
}