Skip to content

Commit c203933

Browse files
binhndenyeart
authored andcommitted
[FAB-2045] Document chaincode interfaces
This usability bug is about "read your own writes" which includes many functions of the chaincode interfaces. The document on those functions aren't accurate, and this CR provides correction for those. Additionally, chaincode function comments were not consistent across interfaces.go and chaincode.go. Comment content has been combined into interfaces.go, while chaincode.go now refers the reader to look at the comment in interfaces.go. This approach will resolve the comment issues caused by dual maintenance. Change-Id: I1dad9d188ccb759c6d326fa559f9735793cccd94 Signed-off-by: Binh Q. Nguyen <[email protected]> Signed-off-by: David Enyeart <[email protected]>
1 parent ed02047 commit c203933

File tree

3 files changed

+142
-111
lines changed

3 files changed

+142
-111
lines changed

core/chaincode/shim/chaincode.go

+24-54
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,7 @@ func (stub *ChaincodeStub) GetTxID() string {
371371

372372
// ------------- Call Chaincode functions ---------------
373373

374-
// InvokeChaincode locally calls the specified chaincode `Invoke` using the
375-
// same transaction context; that is, chaincode calling chaincode doesn't
376-
// create a new transaction message.
374+
// InvokeChaincode documentation can be found in interfaces.go
377375
func (stub *ChaincodeStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response {
378376
// Internally we handle chaincode name as a composite name
379377
if channel != "" {
@@ -384,45 +382,38 @@ func (stub *ChaincodeStub) InvokeChaincode(chaincodeName string, args [][]byte,
384382

385383
// --------- State functions ----------
386384

387-
// GetState returns the byte array value specified by the `key`.
385+
// GetState documentation can be found in interfaces.go
388386
func (stub *ChaincodeStub) GetState(key string) ([]byte, error) {
389387
return stub.handler.handleGetState(key, stub.TxID)
390388
}
391389

392-
// PutState writes the specified `value` and `key` into the ledger.
393-
// Simple keys must not be an empty string and must not start with null
394-
// character (0x00), in order to avoid range query collisions with
395-
// composite keys, which internally get prefixed with 0x00 as composite
396-
// key namespace.
390+
// PutState documentation can be found in interfaces.go
397391
func (stub *ChaincodeStub) PutState(key string, value []byte) error {
398392
if key == "" {
399393
return fmt.Errorf("key must not be an empty string")
400394
}
401395
return stub.handler.handlePutState(key, value, stub.TxID)
402396
}
403397

404-
// DelState removes the specified `key` and its value from the ledger.
398+
// DelState documentation can be found in interfaces.go
405399
func (stub *ChaincodeStub) DelState(key string) error {
406400
return stub.handler.handleDelState(key, stub.TxID)
407401
}
408402

409-
// CommonIterator allows a chaincode to iterate over a set of
410-
// key/value pairs in the state.
403+
// CommonIterator documentation can be found in interfaces.go
411404
type CommonIterator struct {
412405
handler *Handler
413406
uuid string
414407
response *pb.QueryResponse
415408
currentLoc int
416409
}
417410

418-
//GeneralQueryIterator allows a chaincode to iterate the result
419-
//of range and execute query.
411+
// StateQueryIterator documentation can be found in interfaces.go
420412
type StateQueryIterator struct {
421413
*CommonIterator
422414
}
423415

424-
//GeneralQueryIterator allows a chaincode to iterate the result
425-
//of history query.
416+
// HistoryQueryIterator documentation can be found in interfaces.go
426417
type HistoryQueryIterator struct {
427418
*CommonIterator
428419
}
@@ -442,13 +433,7 @@ func (stub *ChaincodeStub) handleGetStateByRange(startKey, endKey string) (State
442433
return &StateQueryIterator{CommonIterator: &CommonIterator{stub.handler, stub.TxID, response, 0}}, nil
443434
}
444435

445-
// GetStateByRange function can be invoked by a chaincode to query of a range
446-
// of keys in the state. Assuming the startKey and endKey are in lexical order,
447-
// an iterator will be returned that can be used to iterate over all keys
448-
// between the startKey and endKey. The startKey is inclusive whereas the endKey
449-
// is exclusive. The keys are returned by the iterator in lexical order. Note
450-
// that startKey and endKey can be empty string, which implies unbounded range
451-
// query on start or end.
436+
// GetStateByRange documentation can be found in interfaces.go
452437
func (stub *ChaincodeStub) GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error) {
453438
if startKey == "" {
454439
startKey = emptyKeySubstitute
@@ -459,11 +444,7 @@ func (stub *ChaincodeStub) GetStateByRange(startKey, endKey string) (StateQueryI
459444
return stub.handleGetStateByRange(startKey, endKey)
460445
}
461446

462-
// GetQueryResult function can be invoked by a chaincode to perform a
463-
// rich query against state database. Only supported by state database implementations
464-
// that support rich query. The query string is in the syntax of the underlying
465-
// state database. An iterator is returned which can be used to iterate (next) over
466-
// the query result set
447+
// GetQueryResult documentation can be found in interfaces.go
467448
func (stub *ChaincodeStub) GetQueryResult(query string) (StateQueryIteratorInterface, error) {
468449
response, err := stub.handler.handleGetQueryResult(query, stub.TxID)
469450
if err != nil {
@@ -472,8 +453,7 @@ func (stub *ChaincodeStub) GetQueryResult(query string) (StateQueryIteratorInter
472453
return &StateQueryIterator{CommonIterator: &CommonIterator{stub.handler, stub.TxID, response, 0}}, nil
473454
}
474455

475-
// GetHistoryForKey function can be invoked by a chaincode to return a history of
476-
// key values across time. GetHistoryForKey is intended to be used for read-only queries.
456+
// GetHistoryForKey documentation can be found in interfaces.go
477457
func (stub *ChaincodeStub) GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error) {
478458
response, err := stub.handler.handleGetHistoryForKey(key, stub.TxID)
479459
if err != nil {
@@ -482,12 +462,12 @@ func (stub *ChaincodeStub) GetHistoryForKey(key string) (HistoryQueryIteratorInt
482462
return &HistoryQueryIterator{CommonIterator: &CommonIterator{stub.handler, stub.TxID, response, 0}}, nil
483463
}
484464

485-
//CreateCompositeKey combines the given attributes to form a composite key.
465+
//CreateCompositeKey documentation can be found in interfaces.go
486466
func (stub *ChaincodeStub) CreateCompositeKey(objectType string, attributes []string) (string, error) {
487467
return createCompositeKey(objectType, attributes)
488468
}
489469

490-
//SplitCompositeKey splits the key into attributes on which the composite key was formed.
470+
//SplitCompositeKey documentation can be found in interfaces.go
491471
func (stub *ChaincodeStub) SplitCompositeKey(compositeKey string) (string, []string, error) {
492472
return splitCompositeKey(compositeKey)
493473
}
@@ -574,8 +554,7 @@ func (iter *HistoryQueryIterator) Next() (*queryresult.KeyModification, error) {
574554
}
575555
}
576556

577-
// HasNext returns true if the range query iterator contains additional keys
578-
// and values.
557+
// HasNext documentation can be found in interfaces.go
579558
func (iter *CommonIterator) HasNext() bool {
580559
if iter.currentLoc < len(iter.response.Results) || iter.response.HasMore {
581560
return true
@@ -649,19 +628,18 @@ func (iter *CommonIterator) nextResult(rType resultType) (commonledger.QueryResu
649628
return nil, errors.New("Invalid iterator state")
650629
}
651630

652-
// Close closes the range query iterator. This should be called when done
653-
// reading from the iterator to free up resources.
631+
// Close documentation can be found in interfaces.go
654632
func (iter *CommonIterator) Close() error {
655633
_, err := iter.handler.handleQueryStateClose(iter.response.Id, iter.uuid)
656634
return err
657635
}
658636

659-
// GetArgs returns the argument list
637+
// GetArgs documentation can be found in interfaces.go
660638
func (stub *ChaincodeStub) GetArgs() [][]byte {
661639
return stub.args
662640
}
663641

664-
// GetStringArgs returns the arguments as array of strings
642+
// GetStringArgs documentation can be found in interfaces.go
665643
func (stub *ChaincodeStub) GetStringArgs() []string {
666644
args := stub.GetArgs()
667645
strargs := make([]string, 0, len(args))
@@ -671,8 +649,7 @@ func (stub *ChaincodeStub) GetStringArgs() []string {
671649
return strargs
672650
}
673651

674-
// GetFunctionAndParameters returns the first arg as the function and the rest
675-
// as argument string array
652+
// GetFunctionAndParameters documentation can be found in interfaces.go
676653
func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params []string) {
677654
allargs := stub.GetStringArgs()
678655
function = ""
@@ -684,32 +661,27 @@ func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params [
684661
return
685662
}
686663

687-
// GetCreator returns SignatureHeader.Creator of the signedProposal
688-
// this Stub refers to.
664+
// GetCreator documentation can be found in interfaces.go
689665
func (stub *ChaincodeStub) GetCreator() ([]byte, error) {
690666
return stub.creator, nil
691667
}
692668

693-
// GetTransient returns the ChaincodeProposalPayload.transient field.
694-
// It is a map that contains data (e.g. cryptographic material)
695-
// that might be used to implement some form of application-level confidentiality. The contents
696-
// of this field, as prescribed by ChaincodeProposalPayload, are supposed to always
697-
// be omitted from the transaction and excluded from the ledger.
669+
// GetTransient documentation can be found in interfaces.go
698670
func (stub *ChaincodeStub) GetTransient() (map[string][]byte, error) {
699671
return stub.transient, nil
700672
}
701673

702-
// GetBinding returns the transaction binding
674+
// GetBinding documentation can be found in interfaces.go
703675
func (stub *ChaincodeStub) GetBinding() ([]byte, error) {
704676
return stub.binding, nil
705677
}
706678

707-
// GetSignedProposal return the signed signedProposal this stub refers to.
679+
// GetSignedProposal documentation can be found in interfaces.go
708680
func (stub *ChaincodeStub) GetSignedProposal() (*pb.SignedProposal, error) {
709681
return stub.signedProposal, nil
710682
}
711683

712-
// GetArgsSlice returns the arguments to the stub call as a byte array
684+
// GetArgsSlice documentation can be found in interfaces.go
713685
func (stub *ChaincodeStub) GetArgsSlice() ([]byte, error) {
714686
args := stub.GetArgs()
715687
res := []byte{}
@@ -719,9 +691,7 @@ func (stub *ChaincodeStub) GetArgsSlice() ([]byte, error) {
719691
return res, nil
720692
}
721693

722-
// GetTxTimestamp returns the timestamp when the transaction was created. This
723-
// is taken from the transaction ChannelHeader, so it will be the same across
724-
// all endorsers.
694+
// GetTxTimestamp documentation can be found in interfaces.go
725695
func (stub *ChaincodeStub) GetTxTimestamp() (*timestamp.Timestamp, error) {
726696
hdr, err := utils.GetHeader(stub.proposal.Header)
727697
if err != nil {
@@ -737,7 +707,7 @@ func (stub *ChaincodeStub) GetTxTimestamp() (*timestamp.Timestamp, error) {
737707

738708
// ------------- ChaincodeEvent API ----------------------
739709

740-
// SetEvent saves the event to be sent when a transaction is made part of a block
710+
// SetEvent documentation can be found in interfaces.go
741711
func (stub *ChaincodeStub) SetEvent(name string, payload []byte) error {
742712
if name == "" {
743713
return errors.New("Event name can not be nil string.")

0 commit comments

Comments
 (0)