Skip to content

Commit f73825f

Browse files
author
Srinivasan Muralidharan
committed
FAB-1859 move sys ccs to scc pkg
https://jira.hyperledger.org/browse/FAB-1859 The refactor and cleanup of "ccprovider" package (core/common/ccprovider) paved way for further cleanup. As a first step let us move back all the system chaincode (lscc, vscc, cscc, qscc, escc) to be in their own packages under system_chaincodes. Change-Id: Idfe91e351c97e97c614df808e3b1af9d207fd65a Signed-off-by: Srinivasan Muralidharan <[email protected]>
1 parent dffcaf4 commit f73825f

28 files changed

+294
-311
lines changed

core/chaincode/ccproviderimpl.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/hyperledger/fabric/core/common/ccprovider"
2323
"github.com/hyperledger/fabric/core/ledger"
24-
"github.com/hyperledger/fabric/protos/peer"
24+
pb "github.com/hyperledger/fabric/protos/peer"
2525
)
2626

2727
// ccProviderFactory implements the ccprovider.ChaincodeProviderFactory
@@ -47,7 +47,7 @@ type ccProviderImpl struct {
4747

4848
// ccProviderContextImpl contains the state that is passed around to calls to methods of ccProviderImpl
4949
type ccProviderContextImpl struct {
50-
ctx *CCContext
50+
ctx *ccprovider.CCContext
5151
}
5252

5353
// GetContext returns a context for the supplied ledger, with the appropriate tx simulator
@@ -65,13 +65,13 @@ func (c *ccProviderImpl) GetContext(ledger ledger.PeerLedger) (context.Context,
6565
// GetCCContext returns an interface that encapsulates a
6666
// chaincode context; the interface is required to avoid
6767
// referencing the chaincode package from the interface definition
68-
func (c *ccProviderImpl) GetCCContext(cid, name, version, txid string, syscc bool, prop *peer.Proposal) interface{} {
69-
ctx := NewCCContext(cid, name, version, txid, syscc, prop)
68+
func (c *ccProviderImpl) GetCCContext(cid, name, version, txid string, syscc bool, prop *pb.Proposal) interface{} {
69+
ctx := ccprovider.NewCCContext(cid, name, version, txid, syscc, prop)
7070
return &ccProviderContextImpl{ctx: ctx}
7171
}
7272

7373
// GetCCValidationInfoFromLCCC returns the VSCC and the policy listed in LCCC for the supplied chaincode
74-
func (c *ccProviderImpl) GetCCValidationInfoFromLCCC(ctxt context.Context, txid string, prop *peer.Proposal, chainID string, chaincodeID string) (string, []byte, error) {
74+
func (c *ccProviderImpl) GetCCValidationInfoFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainID string, chaincodeID string) (string, []byte, error) {
7575
data, err := GetChaincodeDataFromLCCC(ctxt, txid, prop, chainID, chaincodeID)
7676
if err != nil {
7777
return "", nil, err
@@ -87,10 +87,28 @@ func (c *ccProviderImpl) GetCCValidationInfoFromLCCC(ctxt context.Context, txid
8787
}
8888

8989
// ExecuteChaincode executes the chaincode specified in the context with the specified arguments
90-
func (c *ccProviderImpl) ExecuteChaincode(ctxt context.Context, cccid interface{}, args [][]byte) (*peer.Response, *peer.ChaincodeEvent, error) {
90+
func (c *ccProviderImpl) ExecuteChaincode(ctxt context.Context, cccid interface{}, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
9191
return ExecuteChaincode(ctxt, cccid.(*ccProviderContextImpl).ctx, args)
9292
}
9393

94+
// Execute executes the chaincode given context and spec (invocation or deploy)
95+
func (c *ccProviderImpl) Execute(ctxt context.Context, cccid interface{}, spec interface{}) (*pb.Response, *pb.ChaincodeEvent, error) {
96+
return Execute(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
97+
}
98+
99+
// ExecuteWithErrorFilder executes the chaincode given context and spec and returns payload
100+
func (c *ccProviderImpl) ExecuteWithErrorFilter(ctxt context.Context, cccid interface{}, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
101+
return ExecuteWithErrorFilter(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
102+
}
103+
104+
// ExecuteWithErrorFilder executes the chaincode given context and spec and returns payload
105+
func (c *ccProviderImpl) Stop(ctxt context.Context, cccid interface{}, spec *pb.ChaincodeDeploymentSpec) error {
106+
if theChaincodeSupport != nil {
107+
return theChaincodeSupport.Stop(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
108+
}
109+
panic("ChaincodeSupport not initialized")
110+
}
111+
94112
// ReleaseContext frees up resources held by the context
95113
func (c *ccProviderImpl) ReleaseContext() {
96114
c.txsim.Done()

core/chaincode/chaincode_support.go

+8-60
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/hyperledger/fabric/common/flogging"
3535
"github.com/hyperledger/fabric/core/chaincode/shim"
36+
"github.com/hyperledger/fabric/core/common/ccprovider"
3637
"github.com/hyperledger/fabric/core/container"
3738
"github.com/hyperledger/fabric/core/container/ccintf"
3839
"github.com/hyperledger/fabric/core/ledger"
@@ -66,59 +67,6 @@ func getTxSimulator(context context.Context) ledger.TxSimulator {
6667
return nil
6768
}
6869

69-
//CCContext pass this around instead of string of args
70-
type CCContext struct {
71-
//ChainID chain id
72-
ChainID string
73-
74-
//Name chaincode name
75-
Name string
76-
77-
//Version used to construct the chaincode image and register
78-
Version string
79-
80-
//TxID is the transaction id for the proposal (if any)
81-
TxID string
82-
83-
//Syscc is this a system chaincode
84-
Syscc bool
85-
86-
//Proposal for this invoke (if any)
87-
//this is kept here just in case we need to pass something
88-
//from this to the chaincode
89-
Proposal *pb.Proposal
90-
91-
//this is not set but computed (note that this is not exported. use GetCanonicalName)
92-
canonicalName string
93-
}
94-
95-
//NewCCContext just construct a new struct with whatever args
96-
func NewCCContext(cid, name, version, txid string, syscc bool, prop *pb.Proposal) *CCContext {
97-
//version CANNOT be empty. The chaincode namespace has to use version and chain name.
98-
//All system chaincodes share the same version given by utils.GetSysCCVersion. Note
99-
//that neither Chain Name or Version are stored in a chaincodes state on the ledger
100-
if version == "" {
101-
panic(fmt.Sprintf("---empty version---(chain=%s,chaincode=%s,version=%s,txid=%s,syscc=%t,proposal=%p", cid, name, version, txid, syscc, prop))
102-
}
103-
104-
canName := name + ":" + version + "/" + cid
105-
106-
cccid := &CCContext{cid, name, version, txid, syscc, prop, canName}
107-
108-
chaincodeLogger.Infof("NewCCCC (chain=%s,chaincode=%s,version=%s,txid=%s,syscc=%t,proposal=%p,canname=%s", cid, name, version, txid, syscc, prop, cccid.canonicalName)
109-
110-
return cccid
111-
}
112-
113-
//GetCanonicalName returns the canonical name associated with the proposal context
114-
func (cccid *CCContext) GetCanonicalName() string {
115-
if cccid.canonicalName == "" {
116-
panic(fmt.Sprintf("cccid not constructed using NewCCContext(chain=%s,chaincode=%s,version=%s,txid=%s,syscc=%t)", cccid.ChainID, cccid.Name, cccid.Version, cccid.TxID, cccid.Syscc))
117-
}
118-
119-
return cccid.canonicalName
120-
}
121-
12270
//
12371
//chaincode runtime environment encapsulates handler and container environment
12472
//This is where the VM that's running the chaincode would hook in
@@ -317,7 +265,7 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
317265
}
318266

319267
// Based on state of chaincode send either init or ready to move to ready state
320-
func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Context, cccid *CCContext, initArgs [][]byte, timeout time.Duration) error {
268+
func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Context, cccid *ccprovider.CCContext, initArgs [][]byte, timeout time.Duration) error {
321269
canName := cccid.GetCanonicalName()
322270
chaincodeSupport.runningChaincodes.Lock()
323271
//if its in the map, there must be a connected stream...nothing to do
@@ -362,7 +310,7 @@ func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Contex
362310
}
363311

364312
//get args and env given chaincodeID
365-
func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *CCContext, cLang pb.ChaincodeSpec_Type) (args []string, envs []string, err error) {
313+
func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCContext, cLang pb.ChaincodeSpec_Type) (args []string, envs []string, err error) {
366314
canName := cccid.GetCanonicalName()
367315
envs = []string{"CORE_CHAINCODE_ID_NAME=" + canName}
368316
//if TLS is enabled, pass TLS material to chaincode
@@ -407,7 +355,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *CCContext, cLang
407355
}
408356

409357
// launchAndWaitForRegister will launch container if not already running. Use the targz to create the image if not found
410-
func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.Context, cccid *CCContext, cds *pb.ChaincodeDeploymentSpec, cLang pb.ChaincodeSpec_Type, targz io.Reader) error {
358+
func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec, cLang pb.ChaincodeSpec_Type, targz io.Reader) error {
411359
canName := cccid.GetCanonicalName()
412360
if canName == "" {
413361
return fmt.Errorf("chaincode name not set")
@@ -472,7 +420,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
472420
}
473421

474422
//Stop stops a chaincode if running
475-
func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *CCContext, cds *pb.ChaincodeDeploymentSpec) error {
423+
func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
476424
canName := cccid.GetCanonicalName()
477425
if canName == "" {
478426
return fmt.Errorf("chaincode name not set")
@@ -504,7 +452,7 @@ func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *C
504452
}
505453

506454
// Launch will launch the chaincode if not running (if running return nil) and will wait for handler of the chaincode to get into FSM ready state.
507-
func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid *CCContext, spec interface{}) (*pb.ChaincodeID, *pb.ChaincodeInput, error) {
455+
func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid *ccprovider.CCContext, spec interface{}) (*pb.ChaincodeID, *pb.ChaincodeInput, error) {
508456
//build the chaincode
509457
var cID *pb.ChaincodeID
510458
var cMsg *pb.ChaincodeInput
@@ -622,7 +570,7 @@ func (chaincodeSupport *ChaincodeSupport) getVMType(cds *pb.ChaincodeDeploymentS
622570
}
623571

624572
// Deploy deploys the chaincode if not in development mode where user is running the chaincode.
625-
func (chaincodeSupport *ChaincodeSupport) Deploy(context context.Context, cccid *CCContext, cds *pb.ChaincodeDeploymentSpec) (*pb.ChaincodeDeploymentSpec, error) {
573+
func (chaincodeSupport *ChaincodeSupport) Deploy(context context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) (*pb.ChaincodeDeploymentSpec, error) {
626574
cLang := cds.ChaincodeSpec.Type
627575
canName := cccid.GetCanonicalName()
628576

@@ -682,7 +630,7 @@ func createTransactionMessage(txid string, cMsg *pb.ChaincodeInput) (*pb.Chainco
682630
}
683631

684632
// Execute executes a transaction and waits for it to complete until a timeout value.
685-
func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, cccid *CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error) {
633+
func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error) {
686634
canName := cccid.GetCanonicalName()
687635
chaincodeSupport.runningChaincodes.Lock()
688636
//we expect the chaincode to be running... sanity check

core/chaincode/chaincodeexec.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/golang/protobuf/proto"
2525
"github.com/hyperledger/fabric/common/util"
2626
"github.com/hyperledger/fabric/core/chaincode/shim"
27+
"github.com/hyperledger/fabric/core/common/ccprovider"
2728
pb "github.com/hyperledger/fabric/protos/peer"
2829
)
2930

@@ -40,7 +41,7 @@ func createCIS(ccname string, args [][]byte) (*pb.ChaincodeInvocationSpec, error
4041
// GetCDSFromLCCC gets chaincode deployment spec from LCCC
4142
func GetCDSFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainID string, chaincodeID string) ([]byte, error) {
4243
version := util.GetSysCCVersion()
43-
cccid := NewCCContext(chainID, "lccc", version, txid, true, prop)
44+
cccid := ccprovider.NewCCContext(chainID, "lccc", version, txid, true, prop)
4445
res, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getdepspec"), []byte(chainID), []byte(chaincodeID)})
4546
if err != nil {
4647
return nil, fmt.Errorf("Execute getdepspec(%s, %s) of LCCC error: %s", chainID, chaincodeID, err)
@@ -53,15 +54,15 @@ func GetCDSFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainI
5354
}
5455

5556
// GetChaincodeDataFromLCCC gets chaincode data from LCCC given name
56-
func GetChaincodeDataFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainID string, chaincodeID string) (*ChaincodeData, error) {
57+
func GetChaincodeDataFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainID string, chaincodeID string) (*ccprovider.ChaincodeData, error) {
5758
version := util.GetSysCCVersion()
58-
cccid := NewCCContext(chainID, "lccc", version, txid, true, prop)
59+
cccid := ccprovider.NewCCContext(chainID, "lccc", version, txid, true, prop)
5960
res, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getccdata"), []byte(chainID), []byte(chaincodeID)})
6061
if err == nil {
6162
if res.Status != shim.OK {
6263
return nil, fmt.Errorf("%s", res.Message)
6364
}
64-
cd := &ChaincodeData{}
65+
cd := &ccprovider.ChaincodeData{}
6566
err = proto.Unmarshal(res.Payload, cd)
6667
if err != nil {
6768
return nil, err
@@ -73,7 +74,7 @@ func GetChaincodeDataFromLCCC(ctxt context.Context, txid string, prop *pb.Propos
7374
}
7475

7576
// ExecuteChaincode executes a given chaincode given chaincode name and arguments
76-
func ExecuteChaincode(ctxt context.Context, cccid *CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
77+
func ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
7778
var spec *pb.ChaincodeInvocationSpec
7879
var err error
7980
var res *pb.Response

core/chaincode/concurrency_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323

2424
"github.com/hyperledger/fabric/common/util"
25+
"github.com/hyperledger/fabric/core/common/ccprovider"
2526
pb "github.com/hyperledger/fabric/protos/peer"
2627

2728
"golang.org/x/net/context"
@@ -53,7 +54,7 @@ func TestExecuteConcurrentInvokes(t *testing.T) {
5354

5455
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: chaincodeID, Input: &pb.ChaincodeInput{Args: args}}
5556

56-
cccid := NewCCContext(chainID, "nkpi", "0", "", false, nil)
57+
cccid := ccprovider.NewCCContext(chainID, "nkpi", "0", "", false, nil)
5758

5859
defer theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
5960

core/chaincode/exectransaction.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import (
2525
"golang.org/x/net/context"
2626

2727
"github.com/hyperledger/fabric/core/chaincode/shim"
28+
"github.com/hyperledger/fabric/core/common/ccprovider"
2829
"github.com/hyperledger/fabric/events/producer"
2930
pb "github.com/hyperledger/fabric/protos/peer"
3031
)
3132

3233
//Execute - execute proposal, return original response of chaincode
33-
func Execute(ctxt context.Context, cccid *CCContext, spec interface{}) (*pb.Response, *pb.ChaincodeEvent, error) {
34+
func Execute(ctxt context.Context, cccid *ccprovider.CCContext, spec interface{}) (*pb.Response, *pb.ChaincodeEvent, error) {
3435
var err error
3536
var cds *pb.ChaincodeDeploymentSpec
3637
var ci *pb.ChaincodeInvocationSpec
@@ -111,7 +112,7 @@ func Execute(ctxt context.Context, cccid *CCContext, spec interface{}) (*pb.Resp
111112

112113
// ExecuteWithErrorFilter is similar to Execute, but filters error contained in chaincode response and returns Payload of response only.
113114
// Mostly used by unit-test.
114-
func ExecuteWithErrorFilter(ctxt context.Context, cccid *CCContext, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
115+
func ExecuteWithErrorFilter(ctxt context.Context, cccid *ccprovider.CCContext, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
115116
res, event, err := Execute(ctxt, cccid, spec)
116117
if err != nil {
117118
chaincodeLogger.Errorf("ExecuteWithErrorFilter %s error: %s", cccid.Name, err)

0 commit comments

Comments
 (0)