From ca61713dc0e8551b31885c56766d84a6a385fd5b Mon Sep 17 00:00:00 2001 From: Alok Kothari Date: Mon, 20 Aug 2018 16:00:02 -0700 Subject: [PATCH 1/2] making identity block --- identitychain/identityblock.go | 62 ++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/identitychain/identityblock.go b/identitychain/identityblock.go index 5fb55b132..1cadc2f6f 100644 --- a/identitychain/identityblock.go +++ b/identitychain/identityblock.go @@ -1,9 +1,67 @@ package identitychain -import "github.com/simple-rules/harmony-benchmark/p2p" +import ( + "bytes" + "crypto/sha256" + "encoding/gob" + "log" + "time" + + "github.com/simple-rules/harmony-benchmark/utils" + "github.com/simple-rules/harmony-benchmark/waitnode" +) // IdentityBlock has the information of one node type IdentityBlock struct { - Peer p2p.Peer + Timestamp int64 + PrevBlockHash [32]byte NumIdentities int32 + Identities []*waitnode.WaitNode +} + +// Serialize serializes the block +func (b *IdentityBlock) Serialize() []byte { + var result bytes.Buffer + encoder := gob.NewEncoder(&result) + err := encoder.Encode(b) + if err != nil { + log.Panic(err) + } + return result.Bytes() +} + +// DeserializeBlock deserializes a block +func DeserializeBlock(d []byte) *IdentityBlock { + var block IdentityBlock + decoder := gob.NewDecoder(bytes.NewReader(d)) + err := decoder.Decode(&block) + if err != nil { + log.Panic(err) + } + return &block +} + +// NewBlock creates and returns a new block. +func NewBlock(Identities []*waitnode.WaitNode, prevBlockHash [32]byte) *IdentityBlock { + numTxs := int32(len(Identities)) + var Ids []*waitnode.WaitNode + for _, ids := range Identities { + Ids = append(Ids, ids) + } + block := &IdentityBlock{Timestamp: time.Now().Unix(), PrevBlockHash: prevBlockHash, NumIdentities: numTxs, Identities: Ids} + return block +} + +// CalculateBlockHash returns a hash of the block +func (b *IdentityBlock) CalculateBlockHash() []byte { + var hashes [][]byte + var blockHash [32]byte + hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.Timestamp)) + hashes = append(hashes, b.PrevBlockHash[:]) + for _, id := range b.Identities { + hashes = append(hashes, id) + } + hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.ShardId)) + blockHash = sha256.Sum256(bytes.Join(hashes, []byte{})) + return blockHash[:] } From 744cb2b4ead947be4aeef0d06293416b2fcc6fbf Mon Sep 17 00:00:00 2001 From: Alok Kothari Date: Mon, 20 Aug 2018 16:11:04 -0700 Subject: [PATCH 2/2] more changes to identity block --- identitychain/identityblock.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/identitychain/identityblock.go b/identitychain/identityblock.go index 1cadc2f6f..53c7403b1 100644 --- a/identitychain/identityblock.go +++ b/identitychain/identityblock.go @@ -43,12 +43,12 @@ func DeserializeBlock(d []byte) *IdentityBlock { // NewBlock creates and returns a new block. func NewBlock(Identities []*waitnode.WaitNode, prevBlockHash [32]byte) *IdentityBlock { - numTxs := int32(len(Identities)) + numIds := int32(len(Identities)) var Ids []*waitnode.WaitNode for _, ids := range Identities { Ids = append(Ids, ids) } - block := &IdentityBlock{Timestamp: time.Now().Unix(), PrevBlockHash: prevBlockHash, NumIdentities: numTxs, Identities: Ids} + block := &IdentityBlock{Timestamp: time.Now().Unix(), PrevBlockHash: prevBlockHash, NumIdentities: numIds, Identities: Ids} return block } @@ -65,3 +65,11 @@ func (b *IdentityBlock) CalculateBlockHash() []byte { blockHash = sha256.Sum256(bytes.Join(hashes, []byte{})) return blockHash[:] } + +// NewGenesisBlock creates and returns genesis Block. +func NewGenesisBlock() *IdentityBlock { + numTxs := 0 + var Ids []*waitnode.WaitNode + block := &IdentityBlock{Timestamp: time.Now().Unix(), PrevBlockHash: [32]byte{}, NumIdentities: 1, Identities: Ids} + return block +}