|
| 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 | +// Interfaces to allow testing of chaincode apps with mocked up stubs |
| 18 | +package shim |
| 19 | + |
| 20 | +import ( |
| 21 | + gp "google/protobuf" |
| 22 | + |
| 23 | + "github.com/hyperledger/fabric/core/chaincode/shim/crypto/attr" |
| 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, function string, args []string) ([]byte, error) |
| 32 | + |
| 33 | + // Invoke is called for every Invoke transactions. The chaincode may change |
| 34 | + // its state variables |
| 35 | + Invoke(stub ChaincodeStubInterface, function string, args []string) ([]byte, error) |
| 36 | + |
| 37 | + // Query is called for Query transactions. The chaincode may only read |
| 38 | + // (but not modify) its state variables and return the result |
| 39 | + Query(stub ChaincodeStubInterface, function string, args []string) ([]byte, error) |
| 40 | +} |
| 41 | + |
| 42 | +// ChaincodeStubInterface is used by deployable chaincode apps to access and modify their ledgers |
| 43 | +type ChaincodeStubInterface interface { |
| 44 | + // Get the arguments to the stub call as a 2D byte array |
| 45 | + GetArgs() [][]byte |
| 46 | + |
| 47 | + // Get the arguments to the stub call as a string array |
| 48 | + GetStringArgs() []string |
| 49 | + |
| 50 | + // InvokeChaincode locally calls the specified chaincode `Invoke` using the |
| 51 | + // same transaction context; that is, chaincode calling chaincode doesn't |
| 52 | + // create a new transaction message. |
| 53 | + InvokeChaincode(chaincodeName string, args [][]byte) ([]byte, error) |
| 54 | + |
| 55 | + // QueryChaincode locally calls the specified chaincode `Query` using the |
| 56 | + // same transaction context; that is, chaincode calling chaincode doesn't |
| 57 | + // create a new transaction message. |
| 58 | + QueryChaincode(chaincodeName string, args [][]byte) ([]byte, error) |
| 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 | + // RangeQueryState 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 and endKey, inclusive. The order in which keys are |
| 73 | + // returned by the iterator is random. |
| 74 | + RangeQueryState(startKey, endKey string) (StateRangeQueryIteratorInterface, error) |
| 75 | + |
| 76 | + // CreateTable creates a new table given the table name and column definitions |
| 77 | + CreateTable(name string, columnDefinitions []*ColumnDefinition) error |
| 78 | + |
| 79 | + // GetTable returns the table for the specified table name or ErrTableNotFound |
| 80 | + // if the table does not exist. |
| 81 | + GetTable(tableName string) (*Table, error) |
| 82 | + |
| 83 | + // DeleteTable deletes an entire table and all associated rows. |
| 84 | + DeleteTable(tableName string) error |
| 85 | + |
| 86 | + // InsertRow inserts a new row into the specified table. |
| 87 | + // Returns - |
| 88 | + // true and no error if the row is successfully inserted. |
| 89 | + // false and no error if a row already exists for the given key. |
| 90 | + // false and a TableNotFoundError if the specified table name does not exist. |
| 91 | + // false and an error if there is an unexpected error condition. |
| 92 | + InsertRow(tableName string, row Row) (bool, error) |
| 93 | + |
| 94 | + // ReplaceRow updates the row in the specified table. |
| 95 | + // Returns - |
| 96 | + // true and no error if the row is successfully updated. |
| 97 | + // false and no error if a row does not exist the given key. |
| 98 | + // flase and a TableNotFoundError if the specified table name does not exist. |
| 99 | + // false and an error if there is an unexpected error condition. |
| 100 | + ReplaceRow(tableName string, row Row) (bool, error) |
| 101 | + |
| 102 | + // GetRow fetches a row from the specified table for the given key. |
| 103 | + GetRow(tableName string, key []Column) (Row, error) |
| 104 | + |
| 105 | + // GetRows returns multiple rows based on a partial key. For example, given table |
| 106 | + // | A | B | C | D | |
| 107 | + // where A, C and D are keys, GetRows can be called with [A, C] to return |
| 108 | + // all rows that have A, C and any value for D as their key. GetRows could |
| 109 | + // also be called with A only to return all rows that have A and any value |
| 110 | + // for C and D as their key. |
| 111 | + GetRows(tableName string, key []Column) (<-chan Row, error) |
| 112 | + |
| 113 | + // DeleteRow deletes the row for the given key from the specified table. |
| 114 | + DeleteRow(tableName string, key []Column) error |
| 115 | + |
| 116 | + // ReadCertAttribute is used to read an specific attribute from the transaction certificate, |
| 117 | + // *attributeName* is passed as input parameter to this function. |
| 118 | + // Example: |
| 119 | + // attrValue,error:=stub.ReadCertAttribute("position") |
| 120 | + ReadCertAttribute(attributeName string) ([]byte, error) |
| 121 | + |
| 122 | + // VerifyAttribute is used to verify if the transaction certificate has an attribute |
| 123 | + // with name *attributeName* and value *attributeValue* which are the input parameters |
| 124 | + // received by this function. |
| 125 | + // Example: |
| 126 | + // containsAttr, error := stub.VerifyAttribute("position", "Software Engineer") |
| 127 | + VerifyAttribute(attributeName string, attributeValue []byte) (bool, error) |
| 128 | + |
| 129 | + // VerifyAttributes does the same as VerifyAttribute but it checks for a list of |
| 130 | + // attributes and their respective values instead of a single attribute/value pair |
| 131 | + // Example: |
| 132 | + // containsAttrs, error:= stub.VerifyAttributes(&attr.Attribute{"position", "Software Engineer"}, &attr.Attribute{"company", "ACompany"}) |
| 133 | + VerifyAttributes(attrs ...*attr.Attribute) (bool, error) |
| 134 | + |
| 135 | + // VerifySignature verifies the transaction signature and returns `true` if |
| 136 | + // correct and `false` otherwise |
| 137 | + VerifySignature(certificate, signature, message []byte) (bool, error) |
| 138 | + |
| 139 | + // GetCallerCertificate returns caller certificate |
| 140 | + GetCallerCertificate() ([]byte, error) |
| 141 | + |
| 142 | + // GetCallerMetadata returns caller metadata |
| 143 | + GetCallerMetadata() ([]byte, error) |
| 144 | + |
| 145 | + // GetBinding returns the transaction binding |
| 146 | + GetBinding() ([]byte, error) |
| 147 | + |
| 148 | + // GetPayload returns transaction payload, which is a `ChaincodeSpec` defined |
| 149 | + // in fabric/protos/chaincode.proto |
| 150 | + GetPayload() ([]byte, error) |
| 151 | + |
| 152 | + // GetTxTimestamp returns transaction created timestamp, which is currently |
| 153 | + // taken from the peer receiving the transaction. Note that this timestamp |
| 154 | + // may not be the same with the other peers' time. |
| 155 | + GetTxTimestamp() (*gp.Timestamp, error) |
| 156 | + |
| 157 | + // SetEvent saves the event to be sent when a transaction is made part of a block |
| 158 | + SetEvent(name string, payload []byte) error |
| 159 | +} |
| 160 | + |
| 161 | +// StateRangeQueryIteratorInterface allows a chaincode to iterate over a range of |
| 162 | +// key/value pairs in the state. |
| 163 | +type StateRangeQueryIteratorInterface interface { |
| 164 | + |
| 165 | + // HasNext returns true if the range query iterator contains additional keys |
| 166 | + // and values. |
| 167 | + HasNext() bool |
| 168 | + |
| 169 | + // Next returns the next key and value in the range query iterator. |
| 170 | + Next() (string, []byte, error) |
| 171 | + |
| 172 | + // Close closes the range query iterator. This should be called when done |
| 173 | + // reading from the iterator to free up resources. |
| 174 | + Close() error |
| 175 | +} |
0 commit comments