github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/chaincode/shim/interfaces.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 shim 18 19 import ( 20 "github.com/golang/protobuf/ptypes/timestamp" 21 22 "github.com/hyperledger/fabric/protos/ledger/queryresult" 23 pb "github.com/hyperledger/fabric/protos/peer" 24 ) 25 26 // Chaincode interface must be implemented by all chaincodes. The fabric runs 27 // the transactions by calling these functions as specified. 28 type Chaincode interface { 29 // Init is called during Instantiate transaction after the chaincode container 30 // has been established for the first time, allowing the chaincode to 31 // initialize its internal data 32 Init(stub ChaincodeStubInterface) pb.Response 33 34 // Invoke is called to update or query the ledger in a proposal transaction. 35 // Updated state variables are not committed to the ledger until the 36 // transaction is committed. 37 Invoke(stub ChaincodeStubInterface) pb.Response 38 } 39 40 // ChaincodeStubInterface is used by deployable chaincode apps to access and 41 // modify their ledgers 42 type ChaincodeStubInterface interface { 43 // GetArgs returns the arguments intended for the chaincode Init and Invoke 44 // as an array of byte arrays. 45 GetArgs() [][]byte 46 47 // GetStringArgs returns the arguments intended for the chaincode Init and 48 // Invoke as a string array. Only use GetStringArgs if the client passes 49 // arguments intended to be used as strings. 50 GetStringArgs() []string 51 52 // GetFunctionAndParameters returns the first argument as the function 53 // name and the rest of the arguments as parameters in a string array. 54 // Only use GetFunctionAndParameters if the client passes arguments intended 55 // to be used as strings. 56 GetFunctionAndParameters() (string, []string) 57 58 // GetArgsSlice returns the arguments intended for the chaincode Init and 59 // Invoke as a byte array 60 GetArgsSlice() ([]byte, error) 61 62 // GetTxID returns the tx_id of the transaction proposal (see ChannelHeader 63 // in protos/common/common.proto) 64 GetTxID() string 65 66 // InvokeChaincode locally calls the specified chaincode `Invoke` using the 67 // same transaction context; that is, chaincode calling chaincode doesn't 68 // create a new transaction message. 69 // If the called chaincode is on the same channel, it simply adds the called 70 // chaincode read set and write set to the calling transaction. 71 // If the called chaincode is on a different channel, 72 // only the Response is returned to the calling chaincode; any PutState calls 73 // from the called chaincode will not have any effect on the ledger; that is, 74 // the called chaincode on a different channel will not have its read set 75 // and write set applied to the transaction. Only the calling chaincode's 76 // read set and write set will be applied to the transaction. Effectively 77 // the called chaincode on a different channel is a `Query`, which does not 78 // participate in state validation checks in subsequent commit phase. 79 // If `channel` is empty, the caller's channel is assumed. 80 InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response 81 82 // GetState returns the value of the specified `key` from the 83 // ledger. Note that GetState doesn't read data from the writeset, which 84 // has not been committed to the ledger. In other words, GetState doesn't 85 // consider data modified by PutState that has not been committed. 86 GetState(key string) ([]byte, error) 87 88 // PutState puts the specified `key` and `value` into the transaction's 89 // writeset as a data-write proposal. PutState doesn't effect the ledger 90 // until the transaction is validated and successfully committed. 91 // Simple keys must not be an empty string and must not start with null 92 // character (0x00), in order to avoid range query collisions with 93 // composite keys, which internally get prefixed with 0x00 as composite 94 // key namespace. 95 PutState(key string, value []byte) error 96 97 // DelState records the specified `key` to be deleted in the writeset of 98 // the transaction proposal. The `key` and its value will be deleted from 99 // the ledger when the transaction is validated and successfully committed. 100 DelState(key string) error 101 102 // GetStateByRange returns a range iterator over a set of keys in the 103 // ledger. The iterator can be used to iterate over all keys 104 // between the startKey (inclusive) and endKey (exclusive). 105 // The keys are returned by the iterator in lexical order. Note 106 // that startKey and endKey can be empty string, which implies unbounded range 107 // query on start or end. 108 // Call Close() on the returned StateQueryIteratorInterface object when done. 109 // The query is re-executed during validation phase to ensure result set 110 // has not changed since transaction endorsement (phantom reads detected). 111 GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error) 112 113 // GetStateByPartialCompositeKey queries the state in the ledger based on 114 // a given partial composite key. This function returns an iterator 115 // which can be used to iterate over all composite keys whose prefix matches 116 // the given partial composite key. The `objectType` and attributes are 117 // expected to have only valid utf8 strings and should not contain 118 // U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point). 119 // See related functions SplitCompositeKey and CreateCompositeKey. 120 // Call Close() on the returned StateQueryIteratorInterface object when done. 121 // The query is re-executed during validation phase to ensure result set 122 // has not changed since transaction endorsement (phantom reads detected). 123 GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error) 124 125 // CreateCompositeKey combines the given `attributes` to form a composite 126 // key. The objectType and attributes are expected to have only valid utf8 127 // strings and should not contain U+0000 (nil byte) and U+10FFFF 128 // (biggest and unallocated code point). 129 // The resulting composite key can be used as the key in PutState(). 130 CreateCompositeKey(objectType string, attributes []string) (string, error) 131 132 // SplitCompositeKey splits the specified key into attributes on which the 133 // composite key was formed. Composite keys found during range queries 134 // or partial composite key queries can therefore be split into their 135 // composite parts. 136 SplitCompositeKey(compositeKey string) (string, []string, error) 137 138 // GetQueryResult performs a "rich" query against a state database. It is 139 // only supported for state databases that support rich query, 140 // e.g.CouchDB. The query string is in the native syntax 141 // of the underlying state database. An iterator is returned 142 // which can be used to iterate (next) over the query result set. 143 // The query is NOT re-executed during validation phase, phantom reads are 144 // not detected. That is, other committed transactions may have added, 145 // updated, or removed keys that impact the result set, and this would not 146 // be detected at validation/commit time. Applications susceptible to this 147 // should therefore not use GetQueryResult as part of transactions that update 148 // ledger, and should limit use to read-only chaincode operations. 149 GetQueryResult(query string) (StateQueryIteratorInterface, error) 150 151 // GetHistoryForKey returns a history of key values across time. 152 // For each historic key update, the historic value and associated 153 // transaction id and timestamp are returned. The timestamp is the 154 // timestamp provided by the client in the proposal header. 155 // GetHistoryForKey requires peer configuration 156 // core.ledger.history.enableHistoryDatabase to be true. 157 // The query is NOT re-executed during validation phase, phantom reads are 158 // not detected. That is, other committed transactions may have updated 159 // the key concurrently, impacting the result set, and this would not be 160 // detected at validation/commit time. Applications susceptible to this 161 // should therefore not use GetHistoryForKey as part of transactions that 162 // update ledger, and should limit use to read-only chaincode operations. 163 GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error) 164 165 // GetCreator returns `SignatureHeader.Creator` (e.g. an identity) 166 // of the `SignedProposal`. This is the identity of the agent (or user) 167 // submitting the transaction. 168 GetCreator() ([]byte, error) 169 170 // GetTransient returns the `ChaincodeProposalPayload.Transient` field. 171 // It is a map that contains data (e.g. cryptographic material) 172 // that might be used to implement some form of application-level 173 // confidentiality. The contents of this field, as prescribed by 174 // `ChaincodeProposalPayload`, are supposed to always 175 // be omitted from the transaction and excluded from the ledger. 176 GetTransient() (map[string][]byte, error) 177 178 // GetBinding returns the transaction binding 179 GetBinding() ([]byte, error) 180 181 // GetSignedProposal returns the SignedProposal object, which contains all 182 // data elements part of a transaction proposal. 183 GetSignedProposal() (*pb.SignedProposal, error) 184 185 // GetTxTimestamp returns the timestamp when the transaction was created. This 186 // is taken from the transaction ChannelHeader, therefore it will indicate the 187 // client's timestamp, and will have the same value across all endorsers. 188 GetTxTimestamp() (*timestamp.Timestamp, error) 189 190 // SetEvent allows the chaincode to propose an event on the transaction 191 // proposal. If the transaction is validated and successfully committed, 192 // the event will be delivered to the current event listeners. 193 SetEvent(name string, payload []byte) error 194 } 195 196 // CommonIteratorInterface allows a chaincode to check whether any more result 197 // to be fetched from an iterator and close it when done. 198 type CommonIteratorInterface interface { 199 // HasNext returns true if the range query iterator contains additional keys 200 // and values. 201 HasNext() bool 202 203 // Close closes the iterator. This should be called when done 204 // reading from the iterator to free up resources. 205 Close() error 206 } 207 208 // StateQueryIteratorInterface allows a chaincode to iterate over a set of 209 // key/value pairs returned by range and execute query. 210 type StateQueryIteratorInterface interface { 211 // Inherit HasNext() and Close() 212 CommonIteratorInterface 213 214 // Next returns the next key and value in the range and execute query iterator. 215 Next() (*queryresult.KV, error) 216 } 217 218 // HistoryQueryIteratorInterface allows a chaincode to iterate over a set of 219 // key/value pairs returned by a history query. 220 type HistoryQueryIteratorInterface interface { 221 // Inherit HasNext() and Close() 222 CommonIteratorInterface 223 224 // Next returns the next key and value in the history query iterator. 225 Next() (*queryresult.KeyModification, error) 226 } 227 228 // MockQueryIteratorInterface allows a chaincode to iterate over a set of 229 // key/value pairs returned by range query. 230 // TODO: Once the execute query and history query are implemented in MockStub, 231 // we need to update this interface 232 type MockQueryIteratorInterface interface { 233 StateQueryIteratorInterface 234 }