github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 Deploy transaction after the container has been 30 // established, allowing the chaincode to initialize its internal data 31 Init(stub ChaincodeStubInterface) pb.Response 32 // Invoke is called for every Invoke transactions. The chaincode may change 33 // its state variables 34 Invoke(stub ChaincodeStubInterface) pb.Response 35 } 36 37 // ChaincodeStubInterface is used by deployable chaincode apps to access and modify their ledgers 38 type ChaincodeStubInterface interface { 39 // Get the arguments to the stub call as a 2D byte array 40 GetArgs() [][]byte 41 42 // Get the arguments to the stub call as a string array 43 GetStringArgs() []string 44 45 // Get the function which is the first argument and the rest of the arguments 46 // as parameters 47 GetFunctionAndParameters() (string, []string) 48 49 // Get the transaction ID 50 GetTxID() string 51 52 // InvokeChaincode locally calls the specified chaincode `Invoke` using the 53 // same transaction context; that is, chaincode calling chaincode doesn't 54 // create a new transaction message. If the called chaincode is on a different 55 // channel, only the Response is returned to the caller; any PutState calls 56 // will not have any effect on the ledger of the channel; effectively it is 57 // a `Query`. If `channel` is empty, the caller's channel is assumed. 58 InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response 59 60 // GetState returns the byte array value specified by the `key`. 61 GetState(key string) ([]byte, error) 62 63 // PutState writes the specified `value` and `key` into the ledger. 64 PutState(key string, value []byte) error 65 66 // DelState removes the specified `key` and its value from the ledger. 67 DelState(key string) error 68 69 // GetStateByRange function can be invoked by a chaincode to query of a range 70 // of keys in the state. Assuming the startKey and endKey are in lexical 71 // an iterator will be returned that can be used to iterate over all keys 72 // between the startKey (inclusive) and endKey (exclusive). The order in which keys are 73 // returned by the iterator is random. 74 GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error) 75 76 // GetStateByPartialCompositeKey function can be invoked by a chaincode to query the 77 // state based on a given partial composite key. This function returns an 78 // iterator which can be used to iterate over all composite keys whose prefix 79 // matches the given partial composite key. This function should be used only for 80 // a partial composite key. For a full composite key, an iter with empty response 81 // would be returned. The objectType and attributes are expected to have only 82 // valid utf8 strings and should not contain U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point) 83 GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error) 84 85 // Given a list of attributes, CreateCompositeKey function combines these attributes 86 // to form a composite key. The objectType and attributes are expected to have only 87 // valid utf8 strings and should not contain U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point) 88 CreateCompositeKey(objectType string, attributes []string) (string, error) 89 90 // Given a composite key, SplitCompositeKey function splits the key into attributes 91 // on which the composite key was formed. 92 SplitCompositeKey(compositeKey string) (string, []string, error) 93 94 // GetQueryResult function can be invoked by a chaincode to perform a 95 // rich query against state database. Only supported by state database implementations 96 // that support rich query. The query string is in the syntax of the underlying 97 // state database. An iterator is returned which can be used to iterate (next) over 98 // the query result set 99 GetQueryResult(query string) (StateQueryIteratorInterface, error) 100 101 // GetHistoryForKey function can be invoked by a chaincode to return a history of 102 // key values across time. GetHistoryForKey is intended to be used for read-only queries. 103 GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error) 104 105 // GetCreator returns SignatureHeader.Creator of the signedProposal 106 // this Stub refers to. 107 GetCreator() ([]byte, error) 108 109 // GetTransient returns the ChaincodeProposalPayload.transient field. 110 // It is a map that contains data (e.g. cryptographic material) 111 // that might be used to implement some form of application-level confidentiality. The contents 112 // of this field, as prescribed by ChaincodeProposalPayload, are supposed to always 113 // be omitted from the transaction and excluded from the ledger. 114 GetTransient() (map[string][]byte, error) 115 116 // GetBinding returns the transaction binding 117 GetBinding() ([]byte, error) 118 119 // GetSignedProposal return the signed signedProposal this stub refers to. 120 GetSignedProposal() (*pb.SignedProposal, error) 121 122 // GetArgsSlice returns the arguments to the stub call as a byte array 123 GetArgsSlice() ([]byte, error) 124 125 // GetTxTimestamp returns the timestamp when the transaction was created. This 126 // is taken from the transaction ChannelHeader, so it will be the same across 127 // all endorsers. 128 GetTxTimestamp() (*timestamp.Timestamp, error) 129 130 // SetEvent saves the event to be sent when a transaction is made part of a block 131 SetEvent(name string, payload []byte) error 132 } 133 134 // CommonIteratorInterface allows a chaincode to check whether any more result 135 //to be fetched from an iterate and close it when needed. 136 type CommonIteratorInterface interface { 137 // HasNext returns true if the range query iterator contains additional keys 138 // and values. 139 HasNext() bool 140 141 // Close closes the range query iterator. This should be called when done 142 // reading from the iterator to free up resources. 143 Close() error 144 } 145 146 // StateQueryIteratorInterface allows a chaincode to iterate over a set of 147 // key/value pairs returned by range and execute query. 148 type StateQueryIteratorInterface interface { 149 // Inherit HasNext() and Close() 150 CommonIteratorInterface 151 152 // Next returns the next key and value in the range and execute query iterator. 153 Next() (*queryresult.KV, error) 154 } 155 156 // HistoryQueryIteratorInterface allows a chaincode to iterate over a set of 157 // key/value pairs returned by a history query. 158 type HistoryQueryIteratorInterface interface { 159 // Inherit HasNext() and Close() 160 CommonIteratorInterface 161 162 // Next returns the next key and value in the history query iterator. 163 Next() (*queryresult.KeyModification, error) 164 } 165 166 // MockQueryIteratorInterface allows a chaincode to iterate over a set of 167 // key/value pairs returned by range query. 168 // TODO: Once the execute query and history query are implemented in MockStub, 169 // we need to update this interface 170 type MockQueryIteratorInterface interface { 171 StateQueryIteratorInterface 172 }