github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/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  	// If the key does not exist in the state database, (nil, nil) is returned.
    87  	GetState(key string) ([]byte, error)
    88  
    89  	// PutState puts the specified `key` and `value` into the transaction's
    90  	// writeset as a data-write proposal. PutState doesn't effect the ledger
    91  	// until the transaction is validated and successfully committed.
    92  	// Simple keys must not be an empty string and must not start with null
    93  	// character (0x00), in order to avoid range query collisions with
    94  	// composite keys, which internally get prefixed with 0x00 as composite
    95  	// key namespace.
    96  	PutState(key string, value []byte) error
    97  
    98  	// DelState records the specified `key` to be deleted in the writeset of
    99  	// the transaction proposal. The `key` and its value will be deleted from
   100  	// the ledger when the transaction is validated and successfully committed.
   101  	DelState(key string) error
   102  
   103  	// GetStateByRange returns a range iterator over a set of keys in the
   104  	// ledger. The iterator can be used to iterate over all keys
   105  	// between the startKey (inclusive) and endKey (exclusive).
   106  	// The keys are returned by the iterator in lexical order. Note
   107  	// that startKey and endKey can be empty string, which implies unbounded range
   108  	// query on start or end.
   109  	// Call Close() on the returned StateQueryIteratorInterface object when done.
   110  	// The query is re-executed during validation phase to ensure result set
   111  	// has not changed since transaction endorsement (phantom reads detected).
   112  	GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)
   113  
   114  	// GetStateByPartialCompositeKey queries the state in the ledger based on
   115  	// a given partial composite key. This function returns an iterator
   116  	// which can be used to iterate over all composite keys whose prefix matches
   117  	// the given partial composite key. The `objectType` and attributes are
   118  	// expected to have only valid utf8 strings and should not contain
   119  	// U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point).
   120  	// See related functions SplitCompositeKey and CreateCompositeKey.
   121  	// Call Close() on the returned StateQueryIteratorInterface object when done.
   122  	// The query is re-executed during validation phase to ensure result set
   123  	// has not changed since transaction endorsement (phantom reads detected).
   124  	GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error)
   125  
   126  	// CreateCompositeKey combines the given `attributes` to form a composite
   127  	// key. The objectType and attributes are expected to have only valid utf8
   128  	// strings and should not contain U+0000 (nil byte) and U+10FFFF
   129  	// (biggest and unallocated code point).
   130  	// The resulting composite key can be used as the key in PutState().
   131  	CreateCompositeKey(objectType string, attributes []string) (string, error)
   132  
   133  	// SplitCompositeKey splits the specified key into attributes on which the
   134  	// composite key was formed. Composite keys found during range queries
   135  	// or partial composite key queries can therefore be split into their
   136  	// composite parts.
   137  	SplitCompositeKey(compositeKey string) (string, []string, error)
   138  
   139  	// GetQueryResult performs a "rich" query against a state database. It is
   140  	// only supported for state databases that support rich query,
   141  	// e.g.CouchDB. The query string is in the native syntax
   142  	// of the underlying state database. An iterator is returned
   143  	// which can be used to iterate (next) over the query result set.
   144  	// The query is NOT re-executed during validation phase, phantom reads are
   145  	// not detected. That is, other committed transactions may have added,
   146  	// updated, or removed keys that impact the result set, and this would not
   147  	// be detected at validation/commit time.  Applications susceptible to this
   148  	// should therefore not use GetQueryResult as part of transactions that update
   149  	// ledger, and should limit use to read-only chaincode operations.
   150  	GetQueryResult(query string) (StateQueryIteratorInterface, error)
   151  
   152  	// GetHistoryForKey returns a history of key values across time.
   153  	// For each historic key update, the historic value and associated
   154  	// transaction id and timestamp are returned. The timestamp is the
   155  	// timestamp provided by the client in the proposal header.
   156  	// GetHistoryForKey requires peer configuration
   157  	// core.ledger.history.enableHistoryDatabase to be true.
   158  	// The query is NOT re-executed during validation phase, phantom reads are
   159  	// not detected. That is, other committed transactions may have updated
   160  	// the key concurrently, impacting the result set, and this would not be
   161  	// detected at validation/commit time. Applications susceptible to this
   162  	// should therefore not use GetHistoryForKey as part of transactions that
   163  	// update ledger, and should limit use to read-only chaincode operations.
   164  	GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error)
   165  
   166  	// GetCreator returns `SignatureHeader.Creator` (e.g. an identity)
   167  	// of the `SignedProposal`. This is the identity of the agent (or user)
   168  	// submitting the transaction.
   169  	GetCreator() ([]byte, error)
   170  
   171  	// GetTransient returns the `ChaincodeProposalPayload.Transient` field.
   172  	// It is a map that contains data (e.g. cryptographic material)
   173  	// that might be used to implement some form of application-level
   174  	// confidentiality. The contents of this field, as prescribed by
   175  	// `ChaincodeProposalPayload`, are supposed to always
   176  	// be omitted from the transaction and excluded from the ledger.
   177  	GetTransient() (map[string][]byte, error)
   178  
   179  	// GetBinding returns the transaction binding
   180  	GetBinding() ([]byte, error)
   181  
   182  	// GetSignedProposal returns the SignedProposal object, which contains all
   183  	// data elements part of a transaction proposal.
   184  	GetSignedProposal() (*pb.SignedProposal, error)
   185  
   186  	// GetTxTimestamp returns the timestamp when the transaction was created. This
   187  	// is taken from the transaction ChannelHeader, therefore it will indicate the
   188  	// client's timestamp, and will have the same value across all endorsers.
   189  	GetTxTimestamp() (*timestamp.Timestamp, error)
   190  
   191  	// SetEvent allows the chaincode to propose an event on the transaction
   192  	// proposal. If the transaction is validated and successfully committed,
   193  	// the event will be delivered to the current event listeners.
   194  	SetEvent(name string, payload []byte) error
   195  }
   196  
   197  // CommonIteratorInterface allows a chaincode to check whether any more result
   198  // to be fetched from an iterator and close it when done.
   199  type CommonIteratorInterface interface {
   200  	// HasNext returns true if the range query iterator contains additional keys
   201  	// and values.
   202  	HasNext() bool
   203  
   204  	// Close closes the iterator. This should be called when done
   205  	// reading from the iterator to free up resources.
   206  	Close() error
   207  }
   208  
   209  // StateQueryIteratorInterface allows a chaincode to iterate over a set of
   210  // key/value pairs returned by range and execute query.
   211  type StateQueryIteratorInterface interface {
   212  	// Inherit HasNext() and Close()
   213  	CommonIteratorInterface
   214  
   215  	// Next returns the next key and value in the range and execute query iterator.
   216  	Next() (*queryresult.KV, error)
   217  }
   218  
   219  // HistoryQueryIteratorInterface allows a chaincode to iterate over a set of
   220  // key/value pairs returned by a history query.
   221  type HistoryQueryIteratorInterface interface {
   222  	// Inherit HasNext() and Close()
   223  	CommonIteratorInterface
   224  
   225  	// Next returns the next key and value in the history query iterator.
   226  	Next() (*queryresult.KeyModification, error)
   227  }
   228  
   229  // MockQueryIteratorInterface allows a chaincode to iterate over a set of
   230  // key/value pairs returned by range query.
   231  // TODO: Once the execute query and history query are implemented in MockStub,
   232  // we need to update this interface
   233  type MockQueryIteratorInterface interface {
   234  	StateQueryIteratorInterface
   235  }