@@ -29,9 +29,9 @@ import (
29
29
30
30
"github.com/golang/protobuf/proto"
31
31
"github.com/golang/protobuf/ptypes/timestamp"
32
- "github.com/hyperledger/fabric/common/util"
33
32
"github.com/hyperledger/fabric/core/comm"
34
33
pb "github.com/hyperledger/fabric/protos/peer"
34
+ "github.com/hyperledger/fabric/protos/utils"
35
35
"github.com/op/go-logging"
36
36
"github.com/spf13/viper"
37
37
"golang.org/x/net/context"
@@ -49,11 +49,16 @@ const (
49
49
// ChaincodeStub is an object passed to chaincode for shim side handling of
50
50
// APIs.
51
51
type ChaincodeStub struct {
52
- TxID string
53
- proposalContext * pb.ChaincodeProposalContext
54
- chaincodeEvent * pb.ChaincodeEvent
55
- args [][]byte
56
- handler * Handler
52
+ TxID string
53
+ chaincodeEvent * pb.ChaincodeEvent
54
+ args [][]byte
55
+ handler * Handler
56
+ proposal * pb.Proposal
57
+
58
+ // Additional fields extracted from the proposal
59
+ creator []byte
60
+ transient map [string ][]byte
61
+ binding []byte
57
62
}
58
63
59
64
// Peer address derived from command line or env var
@@ -270,20 +275,32 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
270
275
// -- init stub ---
271
276
// ChaincodeInvocation functionality
272
277
273
- func (stub * ChaincodeStub ) init (handler * Handler , txid string , input * pb.ChaincodeInput , proposalContext * pb.ChaincodeProposalContext ) {
278
+ func (stub * ChaincodeStub ) init (handler * Handler , txid string , input * pb.ChaincodeInput , proposal * pb.Proposal ) error {
274
279
stub .TxID = txid
275
280
stub .args = input .Args
276
281
stub .handler = handler
277
- stub .proposalContext = proposalContext
278
- }
282
+ stub .proposal = proposal
283
+
284
+ // TODO: sanity check: verify that every call to init with a nil
285
+ // proposal is a legitimate one, meaning it is an internal call
286
+ // to system chaincodes.
287
+ if proposal != nil {
288
+ // Extract creator, transient, binding...
289
+ var err error
290
+ stub .creator , stub .transient , err = utils .GetChaincodeProposalContext (proposal )
291
+ if err != nil {
292
+ return fmt .Errorf ("Failed extracting proposal fields. [%s]" , err )
293
+ }
279
294
280
- // InitTestStub initializes an appropriate stub for testing chaincode
281
- func InitTestStub (funargs ... string ) * ChaincodeStub {
282
- stub := ChaincodeStub {}
283
- allargs := util .ToChaincodeArgs (funargs ... )
284
- newCI := & pb.ChaincodeInput {Args : allargs }
285
- stub .init (& Handler {}, "TEST-txid" , newCI , nil ) // TODO: add msg.ProposalContext
286
- return & stub
295
+ // TODO: txid must uniquely identity the transaction.
296
+ // Remove this comment once replay attack protection will be in place
297
+ stub .binding , err = utils .ComputeProposalBinding (proposal )
298
+ if err != nil {
299
+ return fmt .Errorf ("Failed computing binding from proposal. [%s]" , err )
300
+ }
301
+ }
302
+
303
+ return nil
287
304
}
288
305
289
306
// GetTxID returns the transaction ID
@@ -505,33 +522,34 @@ func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params [
505
522
return
506
523
}
507
524
508
- // GetCallerCertificate returns caller certificate
509
- func (stub * ChaincodeStub ) GetCallerCertificate () ([]byte , error ) {
510
- if stub .proposalContext != nil {
511
- return stub .proposalContext .Transient , nil
512
- }
513
-
514
- return nil , errors .New ("Creator field not set." )
525
+ // GetCreator returns SignatureHeader.Creator of the proposal
526
+ // this Stub refers to.
527
+ func (stub * ChaincodeStub ) GetCreator () ([]byte , error ) {
528
+ return stub .creator , nil
515
529
}
516
530
517
- // GetCallerMetadata returns caller metadata
518
- func ( stub * ChaincodeStub ) GetCallerMetadata () ([] byte , error ) {
519
- if stub . proposalContext != nil {
520
- return stub . proposalContext . Transient , nil
521
- }
522
-
523
- return nil , errors . New ( "Transient field not set." )
531
+ // GetTransient returns the ChaincodeProposalPayload.transient field.
532
+ // It is a map that contains data (e.g. cryptographic material)
533
+ // that might be used to implement some form of application-level confidentiality. The contents
534
+ // of this field, as prescribed by ChaincodeProposalPayload, are supposed to always
535
+ // be omitted from the transaction and excluded from the ledger.
536
+ func ( stub * ChaincodeStub ) GetTransient () ( map [ string ][] byte , error ) {
537
+ return stub . transient , nil
524
538
}
525
539
526
540
// GetBinding returns the transaction binding
527
541
func (stub * ChaincodeStub ) GetBinding () ([]byte , error ) {
528
- return nil , nil
542
+ return stub . binding , nil
529
543
}
530
544
531
- // GetPayload returns transaction payload, which is a `ChaincodeSpec` defined
532
- // in fabric/protos/chaincode.proto
533
- func (stub * ChaincodeStub ) GetPayload () ([]byte , error ) {
534
- return nil , nil
545
+ // GetArgsSlice returns the arguments to the stub call as a byte array
546
+ func (stub * ChaincodeStub ) GetArgsSlice () ([]byte , error ) {
547
+ args := stub .GetArgs ()
548
+ res := []byte {}
549
+ for _ , barg := range args {
550
+ res = append (res , barg ... )
551
+ }
552
+ return res , nil
535
553
}
536
554
537
555
// GetTxTimestamp returns transaction created timestamp, which is currently
0 commit comments