github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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 pb "github.com/hyperledger/fabric/protos/peer" 23 ) 24 25 // Chaincode interface must be implemented by all chaincodes. The fabric runs 26 // the transactions by calling these functions as specified. 27 type Chaincode interface { 28 // Init is called during Deploy transaction after the container has been 29 // established, allowing the chaincode to initialize its internal data 30 Init(stub ChaincodeStubInterface) pb.Response 31 // Invoke is called for every Invoke transactions. The chaincode may change 32 // its state variables 33 Invoke(stub ChaincodeStubInterface) pb.Response 34 } 35 36 // ChaincodeStubInterface is used by deployable chaincode apps to access and modify their ledgers 37 type ChaincodeStubInterface interface { 38 // Get the arguments to the stub call as a 2D byte array 39 GetArgs() [][]byte 40 41 // Get the arguments to the stub call as a string array 42 GetStringArgs() []string 43 44 // Get the function which is the first argument and the rest of the arguments 45 // as parameters 46 GetFunctionAndParameters() (string, []string) 47 48 // Get the transaction ID 49 GetTxID() string 50 51 // InvokeChaincode locally calls the specified chaincode `Invoke` using the 52 // same transaction context; that is, chaincode calling chaincode doesn't 53 // create a new transaction message. If the called chaincode is on a different 54 // channel, only the Response is returned to the caller; any PutState calls 55 // will not have any effect on the ledger of the channel; effectively it is 56 // a `Query`. If `channel` is empty, the caller's channel is assumed. 57 InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response 58 59 // GetState returns the byte array value specified by the `key`. 60 GetState(key string) ([]byte, error) 61 62 // PutState writes the specified `value` and `key` into the ledger. 63 PutState(key string, value []byte) error 64 65 // DelState removes the specified `key` and its value from the ledger. 66 DelState(key string) error 67 68 // GetStateByRange function can be invoked by a chaincode to query of a range 69 // of keys in the state. Assuming the startKey and endKey are in lexical 70 // an iterator will be returned that can be used to iterate over all keys 71 // between the startKey (inclusive) and endKey (exclusive). The order in which keys are 72 // returned by the iterator is random. 73 GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error) 74 75 // GetStateByPartialCompositeKey function can be invoked by a chaincode to query the 76 // state based on a given partial composite key. This function returns an 77 // iterator which can be used to iterate over all composite keys whose prefix 78 // matches the given partial composite key. This function should be used only for 79 // a partial composite key. For a full composite key, an iter with empty response 80 // would be returned. The objectType and attributes are expected to have only 81 // valid utf8 strings and should not contain U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point) 82 GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error) 83 84 // Given a list of attributes, CreateCompositeKey function combines these attributes 85 // to form a composite key. The objectType and attributes are expected to have only 86 // valid utf8 strings and should not contain U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point) 87 CreateCompositeKey(objectType string, attributes []string) (string, error) 88 89 // Given a composite key, SplitCompositeKey function splits the key into attributes 90 // on which the composite key was formed. 91 SplitCompositeKey(compositeKey string) (string, []string, error) 92 93 // GetQueryResult function can be invoked by a chaincode to perform a 94 // rich query against state database. Only supported by state database implementations 95 // that support rich query. The query string is in the syntax of the underlying 96 // state database. An iterator is returned which can be used to iterate (next) over 97 // the query result set 98 GetQueryResult(query string) (StateQueryIteratorInterface, error) 99 100 // GetHistoryForKey function can be invoked by a chaincode to return a history of 101 // key values across time. GetHistoryForKey is intended to be used for read-only queries. 102 GetHistoryForKey(key string) (StateQueryIteratorInterface, error) 103 104 // GetCreator returns SignatureHeader.Creator of the proposal 105 // this Stub refers to. 106 GetCreator() ([]byte, error) 107 108 // GetTransient returns the ChaincodeProposalPayload.transient field. 109 // It is a map that contains data (e.g. cryptographic material) 110 // that might be used to implement some form of application-level confidentiality. The contents 111 // of this field, as prescribed by ChaincodeProposalPayload, are supposed to always 112 // be omitted from the transaction and excluded from the ledger. 113 GetTransient() (map[string][]byte, error) 114 115 // GetBinding returns the transaction binding 116 GetBinding() ([]byte, error) 117 118 // GetArgsSlice returns the arguments to the stub call as a byte array 119 GetArgsSlice() ([]byte, error) 120 121 // GetTxTimestamp returns transaction created timestamp, which is currently 122 // taken from the peer receiving the transaction. Note that this timestamp 123 // may not be the same with the other peers' time. 124 GetTxTimestamp() (*timestamp.Timestamp, error) 125 126 // SetEvent saves the event to be sent when a transaction is made part of a block 127 SetEvent(name string, payload []byte) error 128 } 129 130 // StateQueryIteratorInterface allows a chaincode to iterate over a set of 131 // key/value pairs in the state. 132 type StateQueryIteratorInterface interface { 133 134 // HasNext returns true if the range query iterator contains additional keys 135 // and values. 136 HasNext() bool 137 138 // Next returns the next key and value in the range query iterator. 139 Next() (string, []byte, error) 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 }