|
|
@ -5,6 +5,7 @@ import ( |
|
|
|
"crypto/sha256" |
|
|
|
"crypto/sha256" |
|
|
|
"encoding/gob" |
|
|
|
"encoding/gob" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"harmony-benchmark/utils" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
) |
|
|
|
) |
|
|
@ -14,11 +15,11 @@ type Block struct { |
|
|
|
// Header
|
|
|
|
// Header
|
|
|
|
Timestamp int64 |
|
|
|
Timestamp int64 |
|
|
|
PrevBlockHash [32]byte |
|
|
|
PrevBlockHash [32]byte |
|
|
|
Hash [32]byte |
|
|
|
|
|
|
|
NumTransactions int32 |
|
|
|
NumTransactions int32 |
|
|
|
TransactionIds [][32]byte |
|
|
|
TransactionIds [][32]byte |
|
|
|
Transactions []*Transaction // Transactions
|
|
|
|
Transactions []*Transaction // Transactions
|
|
|
|
ShardId uint32 |
|
|
|
ShardId uint32 |
|
|
|
|
|
|
|
Hash [32]byte |
|
|
|
// Signature...
|
|
|
|
// Signature...
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -67,6 +68,25 @@ func (b *Block) HashTransactions() []byte { |
|
|
|
return txHash[:] |
|
|
|
return txHash[:] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CalculateBlockHash returns a hash of the block
|
|
|
|
|
|
|
|
func (b *Block) 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.TransactionIds { |
|
|
|
|
|
|
|
hashes = append(hashes, id[:]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for _, tx := range b.Transactions { |
|
|
|
|
|
|
|
hashes = append(hashes, tx.ID[:]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.ShardId)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blockHash = sha256.Sum256(bytes.Join(hashes, []byte{})) |
|
|
|
|
|
|
|
return blockHash[:] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewBlock creates and returns a neew block.
|
|
|
|
// NewBlock creates and returns a neew block.
|
|
|
|
func NewBlock(transactions []*Transaction, prevBlockHash [32]byte, shardId uint32) *Block { |
|
|
|
func NewBlock(transactions []*Transaction, prevBlockHash [32]byte, shardId uint32) *Block { |
|
|
|
numTxs := int32(len(transactions)) |
|
|
|
numTxs := int32(len(transactions)) |
|
|
@ -75,8 +95,8 @@ func NewBlock(transactions []*Transaction, prevBlockHash [32]byte, shardId uint3 |
|
|
|
for _, tx := range transactions { |
|
|
|
for _, tx := range transactions { |
|
|
|
txIds = append(txIds, tx.ID) |
|
|
|
txIds = append(txIds, tx.ID) |
|
|
|
} |
|
|
|
} |
|
|
|
block := &Block{time.Now().Unix(), prevBlockHash, [32]byte{}, numTxs, txIds, transactions, shardId} |
|
|
|
block := &Block{time.Now().Unix(), prevBlockHash, numTxs, txIds, transactions, shardId, [32]byte{}} |
|
|
|
copy(block.Hash[:], block.HashTransactions()[:]) // TODO(Minh): the blockhash should be a hash of everything in the block
|
|
|
|
copy(block.Hash[:], block.CalculateBlockHash()[:]) |
|
|
|
|
|
|
|
|
|
|
|
return block |
|
|
|
return block |
|
|
|
} |
|
|
|
} |
|
|
|