|
|
@ -1,6 +1,7 @@ |
|
|
|
package identitychain |
|
|
|
package identitychain |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"net" |
|
|
|
"net" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
@ -8,17 +9,17 @@ import ( |
|
|
|
"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/waitnode" |
|
|
|
"github.com/simple-rules/harmony-benchmark/waitnode" |
|
|
|
"github.com/simple-rules/harmony-benchmark/proto" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var mutex sync.Mutex |
|
|
|
var mutex sync.Mutex |
|
|
|
var IdentityPerBlock := 100000 |
|
|
|
var IdentityPerBlock = 100000 |
|
|
|
|
|
|
|
|
|
|
|
// IdentityChain (Blockchain) keeps Identities per epoch, currently centralized!
|
|
|
|
// IdentityChain (Blockchain) keeps Identities per epoch, currently centralized!
|
|
|
|
type IdentityChain struct { |
|
|
|
type IdentityChain struct { |
|
|
|
Identities []*IdentityBlock |
|
|
|
Identities []*IdentityBlock |
|
|
|
PendingIdentities []*waitnode.WaitNode |
|
|
|
PendingIdentities []*waitnode.WaitNode |
|
|
|
log log.Logger |
|
|
|
log log.Logger |
|
|
|
Peer p2p.Peer |
|
|
|
Peer p2p.Peer |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//IdentityChainHandler handles registration of new Identities
|
|
|
|
//IdentityChainHandler handles registration of new Identities
|
|
|
@ -26,29 +27,30 @@ func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) { |
|
|
|
// Read p2p message payload
|
|
|
|
// Read p2p message payload
|
|
|
|
content, err := p2p.ReadMessageContent(conn) |
|
|
|
content, err := p2p.ReadMessageContent(conn) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
IDC.log.Error("Read p2p data failed", "err", err, "node", node) |
|
|
|
IDC.log.Error("Read p2p data failed") |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fmt.Printf("content is %b", content) |
|
|
|
|
|
|
|
|
|
|
|
msgCategory, err := proto.GetMessageCategory(content) |
|
|
|
// msgCategory, err := proto.GetMessageCategory(content)
|
|
|
|
if err != nil { |
|
|
|
// if err != nil {
|
|
|
|
IDC.log.Error("Read node type failed", "err", err, "node", node) |
|
|
|
// IDC.log.Error("Read node type failed", "err", err, "node", node)
|
|
|
|
return |
|
|
|
// return
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
msgType, err := proto.GetMessageType(content) |
|
|
|
// msgType, err := proto.GetMessageType(content)
|
|
|
|
if err != nil { |
|
|
|
// if err != nil {
|
|
|
|
IDC.log.Error("Read action type failed", "err", err, "node", node) |
|
|
|
// IDC.log.Error("Read action type failed", "err", err, "node", node)
|
|
|
|
return |
|
|
|
// return
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
msgPayload, err := proto.GetMessagePayload(content) |
|
|
|
// msgPayload, err := proto.GetMessagePayload(content)
|
|
|
|
if err != nil { |
|
|
|
// if err != nil {
|
|
|
|
IDC.log.Error("Read message payload failed", "err", err, "node", node) |
|
|
|
// IDC.log.Error("Read message payload failed", "err", err, "node", node)
|
|
|
|
return |
|
|
|
// return
|
|
|
|
} |
|
|
|
// }
|
|
|
|
NewWaitNode := *waitnode.DeserializeWaitNode(msgPayload) |
|
|
|
// NewWaitNode := *waitnode.DeserializeWaitNode(msgPayload)
|
|
|
|
IDC.PendingIdentities = append(IDC.PendingIdentities, NewWaitNode) |
|
|
|
// IDC.PendingIdentities = append(IDC.PendingIdentities, NewWaitNode)
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetLatestBlock gests the latest block at the end of the chain
|
|
|
|
// GetLatestBlock gests the latest block at the end of the chain
|
|
|
@ -59,37 +61,40 @@ func (IDC *IdentityChain) GetLatestBlock() *IdentityBlock { |
|
|
|
return IDC.Identities[len(IDC.Identities)-1] |
|
|
|
return IDC.Identities[len(IDC.Identities)-1] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//CreateNewBlock is to create the Blocks to be added to the chain
|
|
|
|
//UpdateIdentityChain is to create the Blocks to be added to the chain
|
|
|
|
func (IDC *IdentityChain) MakeNewBlock() *IdentityBlock { |
|
|
|
func (IDC *IdentityChain) UpdateIdentityChain() { |
|
|
|
|
|
|
|
|
|
|
|
if len(IDC.Identities) == 0 { |
|
|
|
|
|
|
|
return NewGenesisBlock() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//If there are no more Identities registring the blockchain is dead
|
|
|
|
//If there are no more Identities registring the blockchain is dead
|
|
|
|
if len(IDC.PendingIdentities) == 0 { |
|
|
|
if len(IDC.PendingIdentities) == 0 { |
|
|
|
// This is abd, because previous block might not be alive
|
|
|
|
// This is abd, because previous block might not be alive
|
|
|
|
return IDC.GetLatestBlock() |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
prevBlock := IDC.GetLatestBlock() |
|
|
|
if len(IDC.Identities) == 0 { |
|
|
|
NewIdentities := IDC.PendingIdentities[:IdentityPerBlock] |
|
|
|
block := NewGenesisBlock() |
|
|
|
IDC.PendingIdentities = IDC.PendingIdentities[IdentityPerBlock]: |
|
|
|
IDC.Identities = append(IDC.Identities, block) |
|
|
|
//All other blocks are dropped.
|
|
|
|
} else { |
|
|
|
IDBlock = NewBlock(NewIdentities,prevBlock.CalculateBlockHash()) |
|
|
|
prevBlock := IDC.GetLatestBlock() |
|
|
|
IDC.Identities = append(IDBlock,IDC.Identities) |
|
|
|
prevBlockHash := prevBlock.CalculateBlockHash() |
|
|
|
|
|
|
|
NewIdentities := IDC.PendingIdentities[:IdentityPerBlock] |
|
|
|
|
|
|
|
IDC.PendingIdentities = IDC.PendingIdentities[IdentityPerBlock:] |
|
|
|
|
|
|
|
//All other blocks are dropped.
|
|
|
|
|
|
|
|
IDBlock := NewBlock(NewIdentities, prevBlockHash) |
|
|
|
|
|
|
|
IDC.Identities = append(IDC.Identities, IDBlock) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (IDC *IdentityChain) listenOnPort() { |
|
|
|
func (IDC *IdentityChain) listenOnPort() { |
|
|
|
listen, err := net.Listen("tcp4", IDC.Peer.Ip + ":" IDC.Peer.Port) |
|
|
|
listen, err := net.Listen("tcp4", IDC.Peer.Ip+":"+IDC.Peer.Port) |
|
|
|
defer listen.Close() |
|
|
|
defer listen.Close() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
IDC.log.Crit("Socket listen port failed", "port", port, "err", err) |
|
|
|
IDC.log.Crit("Socket listen port failed", "port", IDC.Peer.Port, "err", err) |
|
|
|
os.Exit(1) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
} |
|
|
|
for { |
|
|
|
for { |
|
|
|
conn, err := listen.Accept() |
|
|
|
conn, err := listen.Accept() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
IDC.log.Crit("Error listening on port. Exiting.", "port", port) |
|
|
|
IDC.log.Crit("Error listening on port. Exiting.", "port", IDC.Peer.Port) |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
go IDC.IdentityChainHandler(conn) |
|
|
|
go IDC.IdentityChainHandler(conn) |
|
|
@ -97,12 +102,5 @@ func (IDC *IdentityChain) listenOnPort() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
var IDC IdentityChain |
|
|
|
fmt.Print("Hi") |
|
|
|
var nullPeer p2p.Peer |
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
|
|
genesisBlock := &IdentityBlock{nullPeer, 0} |
|
|
|
|
|
|
|
mutex.Lock() |
|
|
|
|
|
|
|
IDC.Identities = append(IDC.Identities, genesisBlock) |
|
|
|
|
|
|
|
mutex.Unlock() |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|