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

111 lines
2.9 KiB

package identitychain
import (
6 years ago
"net"
"os"
"sync"
6 years ago
"github.com/simple-rules/harmony-benchmark/log"
"github.com/simple-rules/harmony-benchmark/p2p"
"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
//IdentityChainHandler handles registration of new Identities
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", "err", err, "node", node)
return
}
msgCategory, err := proto.GetMessageCategory(content)
if err != nil {
IDC.log.Error("Read node type failed", "err", err, "node", node)
return
}
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
}
content, err := p2p.ReadMessageContent(conn)
if err != nil {
IDC.log.Error("Read p2p data failed", "err", err, "node", node)
return
}
}
// 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
//CreateNewBlock is to create the Blocks to be added to the chain
func (IDC *IdentityChain) MakeNewBlock() *IdentityBlock {
if len(IDC.Identities) == 0 {
return NewGenesisBlock()
}
//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 IDC.GetLatestBlock()
}
prevBlock := IDC.GetLatestBlock()
NewIdentities := IDC.PendingIdentities[:IdentityPerBlock]
IDC.PendingIdentities = IDC.PendingIdentities[IdentityPerBlock]:
//All other blocks are dropped.
IDBlock = NewBlock(NewIdentities,prevBlock.CalculateBlockHash())
IDC.Identities = append(IDBlock,IDC.Identities)
}
}
func (IDC *IdentityChain) listenOnPort() {
listen, err := net.Listen("tcp4", IDC.Peer.Ip + ":" IDC.Peer.Port)
6 years ago
defer listen.Close()
if err != nil {
IDC.log.Crit("Socket listen port failed", "port", port, "err", err)
os.Exit(1)
}
for {
conn, err := listen.Accept()
if err != nil {
IDC.log.Crit("Error listening on port. Exiting.", "port", port)
continue
}
go IDC.IdentityChainHandler(conn)
}
}
func main() {
var IDC IdentityChain
var nullPeer p2p.Peer
go func() {
genesisBlock := &IdentityBlock{nullPeer, 0}
mutex.Lock()
IDC.Identities = append(IDC.Identities, genesisBlock)
mutex.Unlock()
}()
}