diff --git a/blockdag/dagio.go b/blockdag/dagio.go index 05698cf5c..4ad53a6a2 100644 --- a/blockdag/dagio.go +++ b/blockdag/dagio.go @@ -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 }