github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/protocol_state/kvstore_storage.go (about) 1 package protocol_state 2 3 import ( 4 "github.com/onflow/flow-go/model/flow" 5 "github.com/onflow/flow-go/state/protocol" 6 "github.com/onflow/flow-go/storage/badger/transaction" 7 ) 8 9 // ProtocolKVStore persists different snapshots of the Protocol State's Key-Calue stores [KV-stores]. 10 // Here, we augment the low-level primitives provided by `storage.ProtocolKVStore` with logic for 11 // encoding and decoding the state snapshots into abstract representation `protocol_state.KVStoreAPI`. 12 // 13 // At the abstraction level here, we can only handle protocol state snapshots, whose data models are 14 // supported by the current software version. There might be serialized snapshots with legacy versions 15 // in the database that are not supported anymore by this software version. 16 type ProtocolKVStore interface { 17 // StoreTx returns an anonymous function (intended to be executed as part of a database transaction), 18 // which persists the given KV-store snapshot as part of a DB tx. Per convention, all implementations 19 // of `protocol.KVStoreReader` should be able to successfully encode their state into a data blob. 20 // If the encoding fails, the anonymous function returns an error upon call. 21 // 22 // Expected errors of the returned anonymous function: 23 // - storage.ErrAlreadyExists if a KV-store snapshot with the given id is already stored. 24 StoreTx(stateID flow.Identifier, kvStore protocol.KVStoreReader) func(*transaction.Tx) error 25 26 // IndexTx returns an anonymous function intended to be executed as part of a database transaction. 27 // In a nutshell, we want to maintain a map from `blockID` to `stateID`, where `blockID` references the 28 // block that _proposes_ the updated key-value store. 29 // Upon call, the anonymous function persists the specific map entry in the node's database. 30 // Protocol convention: 31 // - Consider block B, whose ingestion might potentially lead to an updated KV store. For example, 32 // the KV store changes if we seal some execution results emitting specific service events. 33 // - For the key `blockID`, we use the identity of block B which _proposes_ this updated KV store. 34 // - CAUTION: The updated state requires confirmation by a QC and will only become active at the 35 // child block, _after_ validating the QC. 36 // 37 // Expected errors of the returned anonymous function: 38 // - storage.ErrAlreadyExists if a KV store for the given blockID has already been indexed. 39 IndexTx(blockID flow.Identifier, stateID flow.Identifier) func(*transaction.Tx) error 40 41 // ByID retrieves the KV store snapshot with the given ID. 42 // Expected errors during normal operations: 43 // - storage.ErrNotFound if no snapshot with the given Identifier is known. 44 // - kvstore.ErrUnsupportedVersion if the version of the stored snapshot not supported by this implementation 45 ByID(id flow.Identifier) (KVStoreAPI, error) 46 47 // ByBlockID retrieves the kv-store snapshot that the block with the given ID proposes. 48 // CAUTION: this store snapshot requires confirmation by a QC and will only become active at the child block, 49 // _after_ validating the QC. Protocol convention: 50 // - Consider block B, whose ingestion might potentially lead to an updated KV store state. 51 // For example, the state changes if we seal some execution results emitting specific service events. 52 // - For the key `blockID`, we use the identity of block B which _proposes_ this updated KV store. As value, 53 // the hash of the resulting state at the end of processing B is to be used. 54 // - CAUTION: The updated state requires confirmation by a QC and will only become active at the child block, 55 // _after_ validating the QC. 56 // 57 // Expected errors during normal operations: 58 // - storage.ErrNotFound if no snapshot has been indexed for the given block. 59 // - kvstore.ErrUnsupportedVersion if the version of the stored snapshot not supported by this implementation 60 ByBlockID(blockID flow.Identifier) (KVStoreAPI, error) 61 }