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.go

121 lines
3.3 KiB

package identitychain
import (
"fmt"
6 years ago
"net"
"os"
"sync"
6 years ago
"github.com/simple-rules/harmony-benchmark/log"
"github.com/simple-rules/harmony-benchmark/p2p"
6 years ago
"github.com/simple-rules/harmony-benchmark/proto"
"github.com/simple-rules/harmony-benchmark/waitnode"
)
var mutex sync.Mutex
6 years ago
var identityPerBlock = 100000
// IdentityChain (Blockchain) keeps Identities per epoch, currently centralized!
type IdentityChain struct {
Identities []*IdentityBlock
PendingIdentities []*waitnode.WaitNode
6 years ago
log log.Logger
Peer p2p.Peer
}
6 years ago
func (IDC *IdentityChain) shard() {
return
}
6 years ago
//IdentityChainHandler handles registration of new Identities
6 years ago
// This could have been its seperate package like consensus, but am avoiding creating a lot of packages.
6 years ago
func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) {
6 years ago
// Read p2p message payload
content, err := p2p.ReadMessageContent(conn)
if err != nil {
IDC.log.Error("Read p2p data failed")
6 years ago
return
}
fmt.Printf("content is %b", content)
6 years ago
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")
}
fmt.Println(msgCategory)
// msgType, err := proto.GetMessageType(content)
// if err != nil {
// IDC.log.Error("Read action type failed", "err", err, "node", node)
// return
// }
// msgPayload, err := proto.GetMessagePayload(content)
// if err != nil {
// IDC.log.Error("Read message payload failed", "err", err, "node", node)
// return
// }
// NewWaitNode := *waitnode.DeserializeWaitNode(msgPayload)
// IDC.PendingIdentities = append(IDC.PendingIdentities, NewWaitNode)
6 years ago
}
// GetLatestBlock gests the latest block at the end of the chain
func (IDC *IdentityChain) GetLatestBlock() *IdentityBlock {
if len(IDC.Identities) == 0 {
return nil
}
return IDC.Identities[len(IDC.Identities)-1]
6 years ago
}
6 years ago
//UpdateIdentityChain is to create the Blocks to be added to the chain
func (IDC *IdentityChain) UpdateIdentityChain() {
6 years ago
//If there are no more Identities registring the blockchain is dead
if len(IDC.PendingIdentities) == 0 {
// This is abd, because previous block might not be alive
return
6 years ago
}
if len(IDC.Identities) == 0 {
block := NewGenesisBlock()
IDC.Identities = append(IDC.Identities, block)
} else {
prevBlock := IDC.GetLatestBlock()
prevBlockHash := prevBlock.CalculateBlockHash()
6 years ago
NewIdentities := IDC.PendingIdentities[:identityPerBlock]
IDC.PendingIdentities = []*waitnode.WaitNode{}
//All other blocks are dropped, we need to inform them that they are dropped?
IDBlock := NewBlock(NewIdentities, prevBlockHash)
IDC.Identities = append(IDC.Identities, IDBlock)
6 years ago
}
6 years ago
}
func (IDC *IdentityChain) listenOnPort() {
6 years ago
fmt.Print(IDC.Peer.Port)
listen, err := net.Listen("tcp4", ":"+IDC.Peer.Port)
6 years ago
if err != nil {
6 years ago
IDC.log.Crit("Socket listen port failed")
6 years ago
os.Exit(1)
6 years ago
} else {
IDC.log.Info("Identity chain is now listening..")
6 years ago
}
6 years ago
defer listen.Close()
6 years ago
for {
conn, err := listen.Accept()
if err != nil {
IDC.log.Crit("Error listening on port. Exiting.", "port", IDC.Peer.Port)
6 years ago
continue
}
go IDC.IdentityChainHandler(conn)
}
}
6 years ago
// New Create a new Node
func New(Peer p2p.Peer) *IdentityChain {
IDC := IdentityChain{}
IDC.Peer = Peer
return &IDC
}