github.com/kaituanwang/hyperledger@v2.0.1+incompatible/core/ledger/pvtdatastorage/store.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package pvtdatastorage 8 9 import ( 10 "github.com/hyperledger/fabric/core/ledger" 11 "github.com/hyperledger/fabric/core/ledger/pvtdatapolicy" 12 ) 13 14 // Provider provides handle to specific 'Store' that in turn manages 15 // private write sets for a ledger 16 type Provider interface { 17 OpenStore(id string) (Store, error) 18 Close() 19 } 20 21 // Store manages the permanent storage of private write sets for a ledger 22 // Because the pvt data is supposed to be in sync with the blocks in the 23 // ledger, both should logically happen in an atomic operation. In order 24 // to accomplish this, an implementation of this store should provide 25 // support for a two-phase like commit/rollback capability. 26 // The expected use is such that - first the private data will be given to 27 // this store (via `Prepare` function) and then the block is appended to the block storage. 28 // Finally, one of the functions `Commit` or `Rollback` is invoked on this store based 29 // on whether the block was written successfully or not. The store implementation 30 // is expected to survive a server crash between the call to `Prepare` and `Commit`/`Rollback` 31 type Store interface { 32 // Init initializes the store. This function is expected to be invoked before using the store 33 Init(btlPolicy pvtdatapolicy.BTLPolicy) 34 // InitLastCommittedBlockHeight sets the last committed block height into the pvt data store 35 // This function is used in a special case where the peer is started up with the blockchain 36 // from an earlier version of a peer when the pvt data feature (and hence this store) was not 37 // available. This function is expected to be called only this situation and hence is 38 // expected to throw an error if the store is not empty. On a successful return from this 39 // function the state of the store is expected to be same as of calling the prepare/commit 40 // function for block `0` through `blockNum` with no pvt data 41 InitLastCommittedBlock(blockNum uint64) error 42 // GetPvtDataByBlockNum returns only the pvt data corresponding to the given block number 43 // The pvt data is filtered by the list of 'ns/collections' supplied in the filter 44 // A nil filter does not filter any results 45 GetPvtDataByBlockNum(blockNum uint64, filter ledger.PvtNsCollFilter) ([]*ledger.TxPvtData, error) 46 // GetMissingPvtDataInfoForMostRecentBlocks returns the missing private data information for the 47 // most recent `maxBlock` blocks which miss at least a private data of a eligible collection. 48 GetMissingPvtDataInfoForMostRecentBlocks(maxBlock int) (ledger.MissingPvtDataInfo, error) 49 // Commit commits the pvt data as well as both the eligible and ineligible 50 // missing private data --- `eligible` denotes that the missing private data belongs to a collection 51 // for which this peer is a member; `ineligible` denotes that the missing private data belong to a 52 // collection for which this peer is not a member. 53 Commit(blockNum uint64, pvtData []*ledger.TxPvtData, missingPvtData ledger.TxMissingPvtDataMap) error 54 // ProcessCollsEligibilityEnabled notifies the store when the peer becomes eligible to receive data for an 55 // existing collection. Parameter 'committingBlk' refers to the block number that contains the corresponding 56 // collection upgrade transaction and the parameter 'nsCollMap' contains the collections for which the peer 57 // is now eligible to receive pvt data 58 ProcessCollsEligibilityEnabled(committingBlk uint64, nsCollMap map[string][]string) error 59 // CommitPvtDataOfOldBlocks commits the pvtData (i.e., previously missing data) of old blocks. 60 // The parameter `blocksPvtData` refers a list of old block's pvtdata which are missing in the pvtstore. 61 // This call stores an additional entry called `lastUpdatedOldBlocksList` which keeps the exact list 62 // of updated blocks. This list would be used during recovery process. Once the stateDB is updated with 63 // these pvtData, the `lastUpdatedOldBlocksList` must be removed. During the peer startup, 64 // if the `lastUpdatedOldBlocksList` exists, stateDB needs to be updated with the appropriate pvtData. 65 CommitPvtDataOfOldBlocks(blocksPvtData map[uint64][]*ledger.TxPvtData) error 66 // GetLastUpdatedOldBlocksPvtData returns the pvtdata of blocks listed in `lastUpdatedOldBlocksList` 67 GetLastUpdatedOldBlocksPvtData() (map[uint64][]*ledger.TxPvtData, error) 68 // ResetLastUpdatedOldBlocksList removes the `lastUpdatedOldBlocksList` entry from the store 69 ResetLastUpdatedOldBlocksList() error 70 // IsEmpty returns true if the store does not have any block committed yet 71 IsEmpty() (bool, error) 72 // LastCommittedBlockHeight returns the height of the last committed block 73 LastCommittedBlockHeight() (uint64, error) 74 // Shutdown stops the store 75 Shutdown() 76 } 77 78 // PrivateDataConfig encapsulates the configuration for private data storage on the ledger 79 type PrivateDataConfig struct { 80 // PrivateDataConfig is used to configure a private data storage provider 81 *ledger.PrivateDataConfig 82 // StorePath is the filesystem path for private data storage. 83 // It is internally computed by the ledger component, 84 // so it is not in ledger.PrivateDataConfig and not exposed to other components. 85 StorePath string 86 } 87 88 // ErrIllegalCall is to be thrown by a store impl if the store does not expect a call to Prepare/Commit/Rollback/InitLastCommittedBlock 89 type ErrIllegalCall struct { 90 msg string 91 } 92 93 func (err *ErrIllegalCall) Error() string { 94 return err.msg 95 } 96 97 // ErrIllegalArgs is to be thrown by a store impl if the args passed are not allowed 98 type ErrIllegalArgs struct { 99 msg string 100 } 101 102 func (err *ErrIllegalArgs) Error() string { 103 return err.msg 104 } 105 106 // ErrOutOfRange is to be thrown for the request for the data that is not yet committed 107 type ErrOutOfRange struct { 108 msg string 109 } 110 111 func (err *ErrOutOfRange) Error() string { 112 return err.msg 113 }