github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/protocol_kv_store.go (about) 1 package storage 2 3 import ( 4 "github.com/onflow/flow-go/model/flow" 5 "github.com/onflow/flow-go/storage/badger/transaction" 6 ) 7 8 // ProtocolKVStore persists different snapshots of key-value stores [KV-stores]. At this level, the API 9 // deals with versioned data blobs, each representing a Snapshot of the Protocol State. The *current* 10 // implementation allows to retrieve snapshots from the database (e.g. to answer external API calls) even 11 // for legacy protocol states whose versions are not support anymore. However, this _may_ change in the 12 // future, where only versioned snapshots can be retrieved that are also supported by the current software. 13 // TODO maybe rename to `ProtocolStateSnapshots` (?) because at this low level, we are not exposing the 14 // KV-store, it is just an encoded data blob 15 type ProtocolKVStore interface { 16 // StoreTx returns an anonymous function (intended to be executed as part of a badger transaction), 17 // which persists the given KV-store snapshot as part of a DB tx. 18 // Expected errors of the returned anonymous function: 19 // - storage.ErrAlreadyExists if a KV-store snapshot with the given id is already stored. 20 StoreTx(stateID flow.Identifier, data *flow.PSKeyValueStoreData) func(*transaction.Tx) error 21 22 // IndexTx returns an anonymous function intended to be executed as part of a database transaction. 23 // In a nutshell, we want to maintain a map from `blockID` to `stateID`, where `blockID` references the 24 // block that _proposes_ the updated key-value store. 25 // Upon call, the anonymous function persists the specific map entry in the node's database. 26 // Protocol convention: 27 // - Consider block B, whose ingestion might potentially lead to an updated KV store. For example, 28 // the KV store changes if we seal some execution results emitting specific service events. 29 // - For the key `blockID`, we use the identity of block B which _proposes_ this updated KV store. 30 // - CAUTION: The updated state requires confirmation by a QC and will only become active at the 31 // child block, _after_ validating the QC. 32 // 33 // Expected errors during normal operations: 34 // - storage.ErrAlreadyExists if a KV store for the given blockID has already been indexed. 35 IndexTx(blockID flow.Identifier, stateID flow.Identifier) func(*transaction.Tx) error 36 37 // ByID retrieves the KV store snapshot with the given ID. 38 // Expected errors during normal operations: 39 // - storage.ErrNotFound if no snapshot with the given Identifier is known. 40 ByID(id flow.Identifier) (*flow.PSKeyValueStoreData, error) 41 42 // ByBlockID retrieves the kv-store snapshot that the block with the given ID proposes. 43 // CAUTION: this store snapshot requires confirmation by a QC and will only become active at the child block, 44 // _after_ validating the QC. Protocol convention: 45 // - Consider block B, whose ingestion might potentially lead to an updated KV store state. 46 // For example, the state changes if we seal some execution results emitting specific service events. 47 // - For the key `blockID`, we use the identity of block B which _proposes_ this updated KV store. As value, 48 // the hash of the resulting state at the end of processing B is to be used. 49 // - CAUTION: The updated state requires confirmation by a QC and will only become active at the child block, 50 // _after_ validating the QC. 51 // 52 // Expected errors during normal operations: 53 // - storage.ErrNotFound if no snapshot has been indexed for the given block. 54 ByBlockID(blockID flow.Identifier) (*flow.PSKeyValueStoreData, error) 55 }