certichain

Blockchain built from scratch for verifying document's legitimacy.
Log | Files | Refs | README | LICENSE

commit e0291d931af4363aae9b268bbe3909f69e3c192a
parent 9da3bad92dc36d04333f2e336a20dd2ba5584592
Author: Shivakumar <[email protected]>
Date:   Sat, 30 May 2026 16:55:24 +0530

feat(storage): add helper functions for blockchain/
- add HeightKey for O(1) retrival without walking the chain
- add TxIndexKey for retrival of anchored merkle root

Signed-off-by: Shivakumar <[email protected]>

Diffstat:
Minternal/storage/store.go | 21++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/internal/storage/store.go b/internal/storage/store.go @@ -14,7 +14,10 @@ package storage -import "errors" +import ( + "encoding/binary" + "errors" +) // Store defines the interface for the blockchain's key-value storage layer. // It provides basic CRUD operations and prefix-based iteration. @@ -39,6 +42,8 @@ const ( PrefixTransaction = "tx:" LastHashKey = "chain:last_hash" PrefixBatch = "batch:" + PrefixHeight = "height:" + PrefixTxIndex = "txidx:" ) // BlockKey returns the database key for a block hash by prepending the block prefix. @@ -56,6 +61,20 @@ func BatchKey(batchID string) []byte { return append([]byte(PrefixBatch), []byte(batchID)...) } +// HeightKey returns the database key for a block height. +// It uses big-endian encoding to ensure heights are sorted numerically in the database. +func HeightKey(height uint64) []byte { + key := make([]byte, len(PrefixHeight)+8) + copy(key, PrefixHeight) + binary.BigEndian.PutUint64(key[len(PrefixHeight):], height) + return key +} + +// TxIndexKey returns the database key for a transaction index lookup. +func TxIndexKey(txID []byte) []byte { + return append([]byte(PrefixTxIndex), txID...) +} + // LastHash returns the database key for the last block hash. func LastHash() []byte { return []byte(LastHashKey)