[NOD-233] Fixed iteration order when iterating over the blockIndex bucket. (#338)

This commit is contained in:
stasatdaglabs 2019-07-04 18:29:56 +03:00 committed by Svarog
parent c3c429494f
commit 7400eabc6d

View File

@ -699,7 +699,7 @@ func dbStoreBlockNode(dbTx database.Tx, node *blockNode) error {
// Write block header data to block index bucket.
blockIndexBucket := dbTx.Metadata().Bucket(blockIndexBucketName)
key := blockIndexKey(node.hash)
key := blockIndexKey(node.hash, node.blueScore)
return blockIndexBucket.Put(key, value)
}
@ -717,10 +717,13 @@ func dbStoreBlock(dbTx database.Tx, block *util.Block) error {
}
// blockIndexKey generates the binary key for an entry in the block index
// bucket. The key is composed only of the 32 byte block hash.
func blockIndexKey(blockHash *daghash.Hash) []byte {
indexKey := make([]byte, daghash.HashSize)
copy(indexKey[:], blockHash[:])
// bucket. The key is composed of the block blue score encoded as a big-endian
// 64-bit unsigned int followed by the 32 byte block hash.
// The blue score component is important for iteration order.
func blockIndexKey(blockHash *daghash.Hash, blueScore uint64) []byte {
indexKey := make([]byte, daghash.HashSize+8)
binary.BigEndian.PutUint64(indexKey[0:8], blueScore)
copy(indexKey[8:daghash.HashSize+8], blockHash[:])
return indexKey
}