store.go (3194B)
1 // Copyright 2026 Shivakumar (Certichain) 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package storage 16 17 import ( 18 "encoding/binary" 19 "errors" 20 ) 21 22 // Batch defines the interface for atomic write operations. 23 type Batch interface { 24 // Set stages a key-value pair in the batch. 25 Set(key, value []byte) error 26 // Commit persists all staged operations to the underlying store atomatically. 27 Commit() error 28 // Discard releases resources associated with the batch. 29 // It is a no-op if the batch is already been commited. 30 Discard() 31 } 32 33 // Store defines the interface for the blockchain's key-value storage layer. 34 // It provides basic CRUD operations and prefix-based iteration. 35 type Store interface { 36 Get(key []byte) ([]byte, error) 37 Set(key, value []byte) error 38 Delete(key []byte) error 39 Has(key []byte) (bool, error) 40 Iterate(prefix []byte, fn func(key, value []byte) error) error 41 NewBatch() (Batch, error) 42 Close() error 43 } 44 45 var ( 46 ErrNotFound = errors.New("storage: key not found") 47 ErrEmptyKey = errors.New("storage: empty key") 48 ErrEmptyValue = errors.New("storage: empty value") 49 ErrClosed = errors.New("storage: store not initialized or closed") 50 ) 51 52 const ( 53 PrefixBlock = "block:" 54 PrefixTransaction = "tx:" 55 LastHashKey = "chain:last_hash" 56 PrefixBatch = "batch:" 57 PrefixHeight = "height:" 58 PrefixTxIndex = "txidx:" 59 PrefixInstitution = "institution:" 60 ) 61 62 // BlockKey returns the database key for a block hash by prepending the block prefix. 63 func BlockKey(hash []byte) []byte { 64 return append([]byte(PrefixBlock), hash...) 65 } 66 67 // TxKey returns the database key for a transaction ID by prepending the transaction prefix. 68 func TxKey(txID []byte) []byte { 69 return append([]byte(PrefixTransaction), txID...) 70 } 71 72 // BatchKey returns the database key for a batch ID by prepending the batch prefix. 73 func BatchKey(batchID string) []byte { 74 return append([]byte(PrefixBatch), []byte(batchID)...) 75 } 76 77 // HeightKey returns the database key for a block height. 78 // It uses big-endian encoding to ensure heights are sorted numerically in the database. 79 func HeightKey(height uint64) []byte { 80 key := make([]byte, len(PrefixHeight)+8) 81 copy(key, PrefixHeight) 82 binary.BigEndian.PutUint64(key[len(PrefixHeight):], height) 83 return key 84 } 85 86 // TxIndexKey returns the database key for a transaction index lookup. 87 func TxIndexKey(txID []byte) []byte { 88 return append([]byte(PrefixTxIndex), txID...) 89 } 90 91 // LastHash returns the database key for the last block hash. 92 func LastHash() []byte { 93 return []byte(LastHashKey) 94 } 95 96 // InstitutionKey returns the institution did. 97 func InstitutionKey(did string) []byte { 98 return append([]byte(PrefixInstitution), []byte(did)...) 99 }