Skip to content

Commit 85318ca

Browse files
committed
Chaincode access to Proposal fields
This change-set enables the chaincode to obtain access to the proposal's transient and creator fields. This change-set comes in the context of https://jira.hyperledger.org/browse/FAB-1323 Change-Id: Ia72de7c95b32cf6a69627d590647f224cdc36329 Signed-off-by: Angelo De Caro <[email protected]>
1 parent dca94df commit 85318ca

19 files changed

+548
-432
lines changed

core/chaincode/handler.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
ccintf "github.com/hyperledger/fabric/core/container/ccintf"
2929
"github.com/hyperledger/fabric/core/ledger"
3030
pb "github.com/hyperledger/fabric/protos/peer"
31+
"github.com/hyperledger/fabric/protos/utils"
3132
"github.com/looplab/fsm"
3233
logging "github.com/op/go-logging"
3334
"golang.org/x/net/context"
@@ -1082,9 +1083,17 @@ func (handler *Handler) enterEndState(e *fsm.Event, state string) {
10821083
}
10831084

10841085
func (handler *Handler) setChaincodeProposal(prop *pb.Proposal, msg *pb.ChaincodeMessage) error {
1085-
chaincodeLogger.Debug("setting chaincode proposal...")
1086+
chaincodeLogger.Debug("Setting chaincode proposal context...")
10861087
if prop != nil {
1087-
chaincodeLogger.Debug("TODO pass Proposal to chaincode...")
1088+
chaincodeLogger.Debug("Proposal different from nil. Creating chaincode proposal context...")
1089+
1090+
proposalContext, err := utils.GetChaincodeProposalContext(prop)
1091+
if err != nil {
1092+
chaincodeLogger.Debug("Failed getting proposal context from proposal [%s]", err)
1093+
return fmt.Errorf("Failed getting proposal context from proposal [%s]", err)
1094+
}
1095+
1096+
msg.ProposalContext = proposalContext
10881097
}
10891098
return nil
10901099
}

core/chaincode/shim/chaincode.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ var chaincodeLogger = logging.MustGetLogger("shim")
4545
// ChaincodeStub is an object passed to chaincode for shim side handling of
4646
// APIs.
4747
type ChaincodeStub struct {
48-
TxID string
49-
chaincodeEvent *pb.ChaincodeEvent
50-
args [][]byte
51-
handler *Handler
48+
TxID string
49+
proposalContext *pb.ChaincodeProposalContext
50+
chaincodeEvent *pb.ChaincodeEvent
51+
args [][]byte
52+
handler *Handler
5253
}
5354

5455
// Peer address derived from command line or env var
@@ -266,17 +267,18 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
266267
// -- init stub ---
267268
// ChaincodeInvocation functionality
268269

269-
func (stub *ChaincodeStub) init(handler *Handler, txid string, input *pb.ChaincodeInput) {
270+
func (stub *ChaincodeStub) init(handler *Handler, txid string, input *pb.ChaincodeInput, proposalContext *pb.ChaincodeProposalContext) {
270271
stub.TxID = txid
271272
stub.args = input.Args
272273
stub.handler = handler
274+
stub.proposalContext = proposalContext
273275
}
274276

275277
func InitTestStub(funargs ...string) *ChaincodeStub {
276278
stub := ChaincodeStub{}
277279
allargs := util.ToChaincodeArgs(funargs...)
278280
newCI := &pb.ChaincodeInput{Args: allargs}
279-
stub.init(&Handler{}, "TEST-txid", newCI)
281+
stub.init(&Handler{}, "TEST-txid", newCI, nil) // TODO: add msg.ProposalContext
280282
return &stub
281283
}
282284

@@ -655,12 +657,20 @@ func deleteRowInternal(stub ChaincodeStubInterface, tableName string, key []Colu
655657

656658
// GetCallerCertificate returns caller certificate
657659
func (stub *ChaincodeStub) GetCallerCertificate() ([]byte, error) {
658-
return nil, nil
660+
if stub.proposalContext != nil {
661+
return stub.proposalContext.Transient, nil
662+
}
663+
664+
return nil, errors.New("Creator field not set.")
659665
}
660666

661667
// GetCallerMetadata returns caller metadata
662668
func (stub *ChaincodeStub) GetCallerMetadata() ([]byte, error) {
663-
return nil, nil
669+
if stub.proposalContext != nil {
670+
return stub.proposalContext.Transient, nil
671+
}
672+
673+
return nil, errors.New("Transient field not set.")
664674
}
665675

666676
// GetBinding returns the transaction binding

core/chaincode/shim/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (handler *Handler) handleInit(msg *pb.ChaincodeMessage) {
227227
// Call chaincode's Run
228228
// Create the ChaincodeStub which the chaincode can use to callback
229229
stub := new(ChaincodeStub)
230-
stub.init(handler, msg.Txid, input)
230+
stub.init(handler, msg.Txid, input, msg.ProposalContext)
231231
res, err := handler.cc.Init(stub)
232232

233233
if err != nil {
@@ -288,7 +288,7 @@ func (handler *Handler) handleTransaction(msg *pb.ChaincodeMessage) {
288288
// Call chaincode's Run
289289
// Create the ChaincodeStub which the chaincode can use to callback
290290
stub := new(ChaincodeStub)
291-
stub.init(handler, msg.Txid, input)
291+
stub.init(handler, msg.Txid, input, msg.ProposalContext)
292292
res, err := handler.cc.Invoke(stub)
293293

294294
if err != nil {

0 commit comments

Comments
 (0)