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

75 lines
2.0 KiB

6 years ago
package identitychain
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"log"
"time"
6 years ago
"github.com/simple-rules/harmony-benchmark/node"
"github.com/simple-rules/harmony-benchmark/utils"
)
6 years ago
// IdentityBlock has the information of one node
type IdentityBlock struct {
Timestamp int64
PrevBlockHash [32]byte
6 years ago
NumIdentities int32
6 years ago
Identities []*node.Node
}
// 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()
}
6 years ago
//Get Identities
6 years ago
func (b *IdentityBlock) GetIdentities() []*node.Node {
6 years ago
return b.Identities
}
// 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.
6 years ago
func NewBlock(Identities []*node.Node, prevBlockHash [32]byte) *IdentityBlock {
6 years ago
block := &IdentityBlock{Timestamp: time.Now().Unix(), PrevBlockHash: prevBlockHash, NumIdentities: int32(len(Identities)), Identities: Identities}
return block
}
// CalculateBlockHash returns a hash of the block
func (b *IdentityBlock) CalculateBlockHash() [32]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 {
6 years ago
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(id))
}
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.NumIdentities))
blockHash = sha256.Sum256(bytes.Join(hashes, []byte{}))
return blockHash
6 years ago
}
// NewGenesisBlock creates and returns genesis Block.
func NewGenesisBlock() *IdentityBlock {
6 years ago
var Ids []*node.Node
block := &IdentityBlock{Timestamp: time.Now().Unix(), PrevBlockHash: [32]byte{}, NumIdentities: 1, Identities: Ids}
return block
}