github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 guarentees 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 // NewTxSimulator gives handle to a transaction simulator. 52 // A client can obtain more than one 'TxSimulator's for parallel execution. 53 // Any snapshoting/synchronization should be performed at the implementation level if required 54 55 // GetTxValidationCodeByTxID returns reason code of transaction validation 56 GetTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error) 57 NewTxSimulator() (TxSimulator, error) 58 // NewQueryExecutor gives handle to a query executor. 59 // A client can obtain more than one 'QueryExecutor's for parallel execution. 60 // Any synchronization should be performed at the implementation level if required 61 NewQueryExecutor() (QueryExecutor, error) 62 // NewHistoryQueryExecutor gives handle to a history query executor. 63 // A client can obtain more than one 'HistoryQueryExecutor's for parallel execution. 64 // Any synchronization should be performed at the implementation level if required 65 NewHistoryQueryExecutor() (HistoryQueryExecutor, error) 66 //Prune prunes the blocks/transactions that satisfy the given policy 67 Prune(policy commonledger.PrunePolicy) error 68 } 69 70 // ValidatedLedger represents the 'final ledger' after filtering out invalid transactions from PeerLedger. 71 // Post-v1 72 type ValidatedLedger interface { 73 commonledger.Ledger 74 } 75 76 // QueryExecutor executes the queries 77 // Get* methods are for supporting KV-based data model. ExecuteQuery method is for supporting a rich datamodel and query support 78 // 79 // ExecuteQuery method in the case of a rich data model is expected to support queries on 80 // latest state, historical state and on the intersection of state and transactions 81 type QueryExecutor interface { 82 // GetState gets the value for given namespace and key. For a chaincode, the namespace corresponds to the chaincodeId 83 GetState(namespace string, key string) ([]byte, error) 84 // GetStateMultipleKeys gets the values for multiple keys in a single call 85 GetStateMultipleKeys(namespace string, keys []string) ([][]byte, error) 86 // GetStateRangeScanIterator returns an iterator that contains all the key-values between given key ranges. 87 // startKey is included in the results and endKey is excluded. An empty startKey refers to the first available key 88 // and an empty endKey refers to the last available key. For scanning all the keys, both the startKey and the endKey 89 // can be supplied as empty strings. However, a full scan shuold be used judiciously for performance reasons. 90 // The returned ResultsIterator contains results of type *KV which is defined in protos/ledger/queryresult. 91 GetStateRangeScanIterator(namespace string, startKey string, endKey string) (commonledger.ResultsIterator, error) 92 // ExecuteQuery executes the given query and returns an iterator that contains results of type specific to the underlying data store. 93 // Only used for state databases that support query 94 // For a chaincode, the namespace corresponds to the chaincodeId 95 // The returned ResultsIterator contains results of type *KV which is defined in protos/ledger/queryresult. 96 ExecuteQuery(namespace, query string) (commonledger.ResultsIterator, error) 97 // Done releases resources occupied by the QueryExecutor 98 Done() 99 } 100 101 // HistoryQueryExecutor executes the history queries 102 type HistoryQueryExecutor interface { 103 // GetHistoryForKey retrieves the history of values for a key. 104 // The returned ResultsIterator contains results of type *KeyModification which is defined in protos/ledger/queryresult. 105 GetHistoryForKey(namespace string, key string) (commonledger.ResultsIterator, error) 106 } 107 108 // TxSimulator simulates a transaction on a consistent snapshot of the 'as recent state as possible' 109 // Set* methods are for supporting KV-based data model. ExecuteUpdate method is for supporting a rich datamodel and query support 110 type TxSimulator interface { 111 QueryExecutor 112 // SetState sets the given value for the given namespace and key. For a chaincode, the namespace corresponds to the chaincodeId 113 SetState(namespace string, key string, value []byte) error 114 // DeleteState deletes the given namespace and key 115 DeleteState(namespace string, key string) error 116 // SetMultipleKeys sets the values for multiple keys in a single call 117 SetStateMultipleKeys(namespace string, kvs map[string][]byte) error 118 // ExecuteUpdate for supporting rich data model (see comments on QueryExecutor above) 119 ExecuteUpdate(query string) error 120 // GetTxSimulationResults encapsulates the results of the transaction simulation. 121 // This should contain enough detail for 122 // - The update in the state that would be caused if the transaction is to be committed 123 // - The environment in which the transaction is executed so as to be able to decide the validity of the enviroment 124 // (at a later time on a different peer) during committing the transactions 125 // Different ledger implementation (or configurations of a single implementation) may want to represent the above two pieces 126 // of information in different way in order to support different data-models or optimize the information representations. 127 GetTxSimulationResults() ([]byte, error) 128 }