Skip to content

Commit

Permalink
Merge "Chaincode access to Proposal fields"
Browse files Browse the repository at this point in the history
  • Loading branch information
christo4ferris authored and Gerrit Code Review committed Jan 17, 2017
2 parents 906bd6f + 85318ca commit 1642e88
Show file tree
Hide file tree
Showing 19 changed files with 548 additions and 432 deletions.
13 changes: 11 additions & 2 deletions core/chaincode/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
ccintf "github.com/hyperledger/fabric/core/container/ccintf"
"github.com/hyperledger/fabric/core/ledger"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/looplab/fsm"
logging "github.com/op/go-logging"
"golang.org/x/net/context"
Expand Down Expand Up @@ -1082,9 +1083,17 @@ func (handler *Handler) enterEndState(e *fsm.Event, state string) {
}

func (handler *Handler) setChaincodeProposal(prop *pb.Proposal, msg *pb.ChaincodeMessage) error {
chaincodeLogger.Debug("setting chaincode proposal...")
chaincodeLogger.Debug("Setting chaincode proposal context...")
if prop != nil {
chaincodeLogger.Debug("TODO pass Proposal to chaincode...")
chaincodeLogger.Debug("Proposal different from nil. Creating chaincode proposal context...")

proposalContext, err := utils.GetChaincodeProposalContext(prop)
if err != nil {
chaincodeLogger.Debug("Failed getting proposal context from proposal [%s]", err)
return fmt.Errorf("Failed getting proposal context from proposal [%s]", err)
}

msg.ProposalContext = proposalContext
}
return nil
}
Expand Down
26 changes: 18 additions & 8 deletions core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ var chaincodeLogger = logging.MustGetLogger("shim")
// ChaincodeStub is an object passed to chaincode for shim side handling of
// APIs.
type ChaincodeStub struct {
TxID string
chaincodeEvent *pb.ChaincodeEvent
args [][]byte
handler *Handler
TxID string
proposalContext *pb.ChaincodeProposalContext
chaincodeEvent *pb.ChaincodeEvent
args [][]byte
handler *Handler
}

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

func (stub *ChaincodeStub) init(handler *Handler, txid string, input *pb.ChaincodeInput) {
func (stub *ChaincodeStub) init(handler *Handler, txid string, input *pb.ChaincodeInput, proposalContext *pb.ChaincodeProposalContext) {
stub.TxID = txid
stub.args = input.Args
stub.handler = handler
stub.proposalContext = proposalContext
}

func InitTestStub(funargs ...string) *ChaincodeStub {
stub := ChaincodeStub{}
allargs := util.ToChaincodeArgs(funargs...)
newCI := &pb.ChaincodeInput{Args: allargs}
stub.init(&Handler{}, "TEST-txid", newCI)
stub.init(&Handler{}, "TEST-txid", newCI, nil) // TODO: add msg.ProposalContext
return &stub
}

Expand Down Expand Up @@ -436,12 +438,20 @@ func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params [

// GetCallerCertificate returns caller certificate
func (stub *ChaincodeStub) GetCallerCertificate() ([]byte, error) {
return nil, nil
if stub.proposalContext != nil {
return stub.proposalContext.Transient, nil
}

return nil, errors.New("Creator field not set.")
}

// GetCallerMetadata returns caller metadata
func (stub *ChaincodeStub) GetCallerMetadata() ([]byte, error) {
return nil, nil
if stub.proposalContext != nil {
return stub.proposalContext.Transient, nil
}

return nil, errors.New("Transient field not set.")
}

// GetBinding returns the transaction binding
Expand Down
4 changes: 2 additions & 2 deletions core/chaincode/shim/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (handler *Handler) handleInit(msg *pb.ChaincodeMessage) {
// Call chaincode's Run
// Create the ChaincodeStub which the chaincode can use to callback
stub := new(ChaincodeStub)
stub.init(handler, msg.Txid, input)
stub.init(handler, msg.Txid, input, msg.ProposalContext)
res, err := handler.cc.Init(stub)

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

if err != nil {
Expand Down
Loading

0 comments on commit 1642e88

Please sign in to comment.