github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/ledger_interface.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package ledger 18 19 import ( 20 commonledger "github.com/hyperledger/fabric/common/ledger" 21 "github.com/hyperledger/fabric/protos/common" 22 "github.com/hyperledger/fabric/protos/peer" 23 ) 24 25 // PeerLedgerProvider provides handle to ledger instances 26 type PeerLedgerProvider interface { 27 // Create creates a new ledger with the given genesis block. 28 // This function guarantees that the creation of ledger and committing the genesis block would an atomic action 29 // The chain id retrieved from the genesis block is treated as a ledger id 30 Create(genesisBlock *common.Block) (PeerLedger, error) 31 // Open opens an already created ledger 32 Open(ledgerID string) (PeerLedger, error) 33 // Exists tells whether the ledger with given id exists 34 Exists(ledgerID string) (bool, error) 35 // List lists the ids of the existing ledgers 36 List() ([]string, error) 37 // Close closes the PeerLedgerProvider 38 Close() 39 } 40 41 // PeerLedger differs from the OrdererLedger in that PeerLedger locally maintain a bitmask 42 // that tells apart valid transactions from invalid ones 43 type PeerLedger interface { 44 commonledger.Ledger 45 // GetTransactionByID retrieves a transaction by id 46 GetTransactionByID(txID string) (*peer.ProcessedTransaction, error) 47 // GetBlockByHash returns a block given it's hash 48 GetBlockByHash(blockHash []byte) (*common.Block, error) 49 // GetBlockByTxID returns a block which contains a transaction 50 GetBlockByTxID(txID string) (*common.Block, error) 51 // GetTxValidationCodeByTxID returns reason code of transaction validation 52 GetTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error) 53 // NewTxSimulator gives handle to a transaction simulator. 54 // A client can obtain more than one 'TxSimulator's for parallel execution. 55 // Any snapshoting/synchronization should be performed at the implementation level if required 56 NewTxSimulator() (TxSimulator, error) 57 // NewQueryExecutor gives handle to a query executor. 58 // A client can obtain more than one 'QueryExecutor's for parallel execution. 59 // Any synchronization should be performed at the implementation level if required 60 NewQueryExecutor() (QueryExecutor, error) 61 // NewHistoryQueryExecutor gives handle to a history query executor. 62 // A client can obtain more than one 'HistoryQueryExecutor's for parallel execution. 63 // Any synchronization should be performed at the implementation level if required 64 NewHistoryQueryExecutor() (HistoryQueryExecutor, error) 65 //Prune prunes the blocks/transactions that satisfy the given policy 66 Prune(policy commonledger.PrunePolicy) error 67 } 68 69 // ValidatedLedger represents the 'final ledger' after filtering out invalid transactions from PeerLedger. 70 // Post-v1 71 type ValidatedLedger interface { 72 commonledger.Ledger 73 } 74 75 // QueryExecutor executes the queries 76 // Get* methods are for supporting KV-based data model. ExecuteQuery method is for supporting a rich datamodel and query support 77 // 78 // ExecuteQuery method in the case of a rich data model is expected to support queries on 79 // latest state, historical state and on the intersection of state and transactions 80 type QueryExecutor interface { 81 // GetState gets the value for given namespace and key. For a chaincode, the namespace corresponds to the chaincodeId 82 GetState(namespace string, key string) ([]byte, error) 83 // GetStateMultipleKeys gets the values for multiple keys in a single call 84 GetStateMultipleKeys(namespace string, keys []string) ([][]byte, error) 85 // GetStateRangeScanIterator returns an iterator that contains all the key-values between given key ranges. 86 // startKey is included in the results and endKey is excluded. An empty startKey refers to the first available key 87 // and an empty endKey refers to the last available key. For scanning all the keys, both the startKey and the endKey 88 // can be supplied as empty strings. However, a full scan shuold be used judiciously for performance reasons. 89 // The returned ResultsIterator contains results of type *KV which is defined in protos/ledger/queryresult. 90 GetStateRangeScanIterator(namespace string, startKey string, endKey string) (commonledger.ResultsIterator, error) 91 // ExecuteQuery executes the given query and returns an iterator that contains results of type specific to the underlying data store. 92 // Only used for state databases that support query 93 // For a chaincode, the namespace corresponds to the chaincodeId 94 // The returned ResultsIterator contains results of type *KV which is defined in protos/ledger/queryresult. 95 ExecuteQuery(namespace, query string) (commonledger.ResultsIterator, error) 96 // Done releases resources occupied by the QueryExecutor 97 Done() 98 } 99 100 // HistoryQueryExecutor executes the history queries 101 type HistoryQueryExecutor interface { 102 // GetHistoryForKey retrieves the history of values for a key. 103 // The returned ResultsIterator contains results of type *KeyModification which is defined in protos/ledger/queryresult. 104 GetHistoryForKey(namespace string, key string) (commonledger.ResultsIterator, error) 105 } 106 107 // TxSimulator simulates a transaction on a consistent snapshot of the 'as recent state as possible' 108 // Set* methods are for supporting KV-based data model. ExecuteUpdate method is for supporting a rich datamodel and query support 109 type TxSimulator interface { 110 QueryExecutor 111 // SetState sets the given value for the given namespace and key. For a chaincode, the namespace corresponds to the chaincodeId 112 SetState(namespace string, key string, value []byte) error 113 // DeleteState deletes the given namespace and key 114 DeleteState(namespace string, key string) error 115 // SetMultipleKeys sets the values for multiple keys in a single call 116 SetStateMultipleKeys(namespace string, kvs map[string][]byte) error 117 // ExecuteUpdate for supporting rich data model (see comments on QueryExecutor above) 118 ExecuteUpdate(query string) error 119 // GetTxSimulationResults encapsulates the results of the transaction simulation. 120 // This should contain enough detail for 121 // - The update in the state that would be caused if the transaction is to be committed 122 // - The environment in which the transaction is executed so as to be able to decide the validity of the environment 123 // (at a later time on a different peer) during committing the transactions 124 // Different ledger implementation (or configurations of a single implementation) may want to represent the above two pieces 125 // of information in different way in order to support different data-models or optimize the information representations. 126 GetTxSimulationResults() ([]byte, error) 127 }