Skip to content

Commit

Permalink
FAB-1859 move sys ccs to scc pkg
Browse files Browse the repository at this point in the history
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 <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Jan 26, 2017
1 parent dffcaf4 commit f73825f
Show file tree
Hide file tree
Showing 28 changed files with 294 additions and 311 deletions.
30 changes: 24 additions & 6 deletions core/chaincode/ccproviderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/protos/peer"
pb "github.com/hyperledger/fabric/protos/peer"
)

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

// ccProviderContextImpl contains the state that is passed around to calls to methods of ccProviderImpl
type ccProviderContextImpl struct {
ctx *CCContext
ctx *ccprovider.CCContext
}

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

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

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

// Execute executes the chaincode given context and spec (invocation or deploy)
func (c *ccProviderImpl) Execute(ctxt context.Context, cccid interface{}, spec interface{}) (*pb.Response, *pb.ChaincodeEvent, error) {
return Execute(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
}

// ExecuteWithErrorFilder executes the chaincode given context and spec and returns payload
func (c *ccProviderImpl) ExecuteWithErrorFilter(ctxt context.Context, cccid interface{}, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
return ExecuteWithErrorFilter(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
}

// ExecuteWithErrorFilder executes the chaincode given context and spec and returns payload
func (c *ccProviderImpl) Stop(ctxt context.Context, cccid interface{}, spec *pb.ChaincodeDeploymentSpec) error {
if theChaincodeSupport != nil {
return theChaincodeSupport.Stop(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
}
panic("ChaincodeSupport not initialized")
}

// ReleaseContext frees up resources held by the context
func (c *ccProviderImpl) ReleaseContext() {
c.txsim.Done()
Expand Down
68 changes: 8 additions & 60 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/core/container"
"github.com/hyperledger/fabric/core/container/ccintf"
"github.com/hyperledger/fabric/core/ledger"
Expand Down Expand Up @@ -66,59 +67,6 @@ func getTxSimulator(context context.Context) ledger.TxSimulator {
return nil
}

//CCContext pass this around instead of string of args
type CCContext struct {
//ChainID chain id
ChainID string

//Name chaincode name
Name string

//Version used to construct the chaincode image and register
Version string

//TxID is the transaction id for the proposal (if any)
TxID string

//Syscc is this a system chaincode
Syscc bool

//Proposal for this invoke (if any)
//this is kept here just in case we need to pass something
//from this to the chaincode
Proposal *pb.Proposal

//this is not set but computed (note that this is not exported. use GetCanonicalName)
canonicalName string
}

//NewCCContext just construct a new struct with whatever args
func NewCCContext(cid, name, version, txid string, syscc bool, prop *pb.Proposal) *CCContext {
//version CANNOT be empty. The chaincode namespace has to use version and chain name.
//All system chaincodes share the same version given by utils.GetSysCCVersion. Note
//that neither Chain Name or Version are stored in a chaincodes state on the ledger
if version == "" {
panic(fmt.Sprintf("---empty version---(chain=%s,chaincode=%s,version=%s,txid=%s,syscc=%t,proposal=%p", cid, name, version, txid, syscc, prop))
}

canName := name + ":" + version + "/" + cid

cccid := &CCContext{cid, name, version, txid, syscc, prop, canName}

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)

return cccid
}

//GetCanonicalName returns the canonical name associated with the proposal context
func (cccid *CCContext) GetCanonicalName() string {
if cccid.canonicalName == "" {
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))
}

return cccid.canonicalName
}

//
//chaincode runtime environment encapsulates handler and container environment
//This is where the VM that's running the chaincode would hook in
Expand Down Expand Up @@ -317,7 +265,7 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
}

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

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

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

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

// 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.
func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid *CCContext, spec interface{}) (*pb.ChaincodeID, *pb.ChaincodeInput, error) {
func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid *ccprovider.CCContext, spec interface{}) (*pb.ChaincodeID, *pb.ChaincodeInput, error) {
//build the chaincode
var cID *pb.ChaincodeID
var cMsg *pb.ChaincodeInput
Expand Down Expand Up @@ -622,7 +570,7 @@ func (chaincodeSupport *ChaincodeSupport) getVMType(cds *pb.ChaincodeDeploymentS
}

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

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

// Execute executes a transaction and waits for it to complete until a timeout value.
func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, cccid *CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error) {
func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error) {
canName := cccid.GetCanonicalName()
chaincodeSupport.runningChaincodes.Lock()
//we expect the chaincode to be running... sanity check
Expand Down
11 changes: 6 additions & 5 deletions core/chaincode/chaincodeexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/common/ccprovider"
pb "github.com/hyperledger/fabric/protos/peer"
)

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

// GetChaincodeDataFromLCCC gets chaincode data from LCCC given name
func GetChaincodeDataFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainID string, chaincodeID string) (*ChaincodeData, error) {
func GetChaincodeDataFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainID string, chaincodeID string) (*ccprovider.ChaincodeData, error) {
version := util.GetSysCCVersion()
cccid := NewCCContext(chainID, "lccc", version, txid, true, prop)
cccid := ccprovider.NewCCContext(chainID, "lccc", version, txid, true, prop)
res, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getccdata"), []byte(chainID), []byte(chaincodeID)})
if err == nil {
if res.Status != shim.OK {
return nil, fmt.Errorf("%s", res.Message)
}
cd := &ChaincodeData{}
cd := &ccprovider.ChaincodeData{}
err = proto.Unmarshal(res.Payload, cd)
if err != nil {
return nil, err
Expand All @@ -73,7 +74,7 @@ func GetChaincodeDataFromLCCC(ctxt context.Context, txid string, prop *pb.Propos
}

// ExecuteChaincode executes a given chaincode given chaincode name and arguments
func ExecuteChaincode(ctxt context.Context, cccid *CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
func ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
var spec *pb.ChaincodeInvocationSpec
var err error
var res *pb.Response
Expand Down
3 changes: 2 additions & 1 deletion core/chaincode/concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/common/ccprovider"
pb "github.com/hyperledger/fabric/protos/peer"

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

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

cccid := NewCCContext(chainID, "nkpi", "0", "", false, nil)
cccid := ccprovider.NewCCContext(chainID, "nkpi", "0", "", false, nil)

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

Expand Down
5 changes: 3 additions & 2 deletions core/chaincode/exectransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
"golang.org/x/net/context"

"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/events/producer"
pb "github.com/hyperledger/fabric/protos/peer"
)

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

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

0 comments on commit f73825f

Please sign in to comment.