Skip to content

Commit

Permalink
[FAB-14458] Remove version from Launch function
Browse files Browse the repository at this point in the history
The chaincode version is already determined by the implementation of the
Launch function, so it need not be supplied as its argument.

Change-Id: I4a4ef91ca008294d2816fb720e23a513f4807b94
Signed-off-by: Alessandro Sorniotti <ale.linux@sopit.net>
  • Loading branch information
ale-linux authored and C0rWin committed Mar 5, 2019
1 parent ccd89a0 commit ce4b38c
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 244 deletions.
8 changes: 2 additions & 6 deletions core/chaincode/chaincode_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ type packageProvider interface {
chaincode.PackageProvider
}

// This is a bit weird, we need to import the chaincode/lifecycle package, but there is an error,
// even if we alias it to another name, so, calling 'lifecycleIface' instead of 'lifecycle'
//go:generate counterfeiter -o mock/lifecycle.go --fake-name Lifecycle . lifecycleIface
type lifecycleIface interface {
chaincode.Lifecycle
}
//go:generate mockery -dir . -name Lifecycle -case underscore -output mock/ -outpkg mock
//go:generate mockery -dir ../common/ccprovider/ -name ChaincodeDefinition -case underscore -output mock/ -outpkg mock

//go:generate counterfeiter -o mock/chaincode_stream.go --fake-name ChaincodeStream . chaincodeStream
type chaincodeStream interface {
Expand Down
58 changes: 33 additions & 25 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,20 @@ func (cs *ChaincodeSupport) LaunchInit(ccci *ccprovider.ChaincodeContainerInfo)
// Launch starts executing chaincode if it is not already running. This method
// blocks until the peer side handler gets into ready state or encounters a fatal
// error. If the chaincode is already running, it simply returns.
func (cs *ChaincodeSupport) Launch(chainID, chaincodeName, chaincodeVersion string, qe ledger.QueryExecutor) (*Handler, error) {
cname := chaincodeName + ":" + chaincodeVersion
func (cs *ChaincodeSupport) Launch(chainID string, ccci *ccprovider.ChaincodeContainerInfo) (*Handler, error) {
cname := ccci.Name + ":" + ccci.Version

if h := cs.HandlerRegistry.Handler(cname); h != nil {
return h, nil
}

ccci, err := cs.Lifecycle.ChaincodeContainerInfo(chaincodeName, qe)
if err != nil {
// TODO: There has to be a better way to do this...
if cs.UserRunsCC {
chaincodeLogger.Error(
"You are attempting to perform an action other than Deploy on Chaincode that is not ready and you are in developer mode. Did you forget to Deploy your chaincode?",
)
}

return nil, errors.Wrapf(err, "[channel %s] failed to get chaincode container info for %s", chainID, cname)
}

if err := cs.Launcher.Launch(ccci); err != nil {
return nil, errors.Wrapf(err, "[channel %s] could not launch chaincode %s", chainID, cname)
}

h := cs.HandlerRegistry.Handler(cname)
if h == nil {
return nil, errors.Wrapf(err, "[channel %s] claimed to start chaincode container for %s but could not find handler", chainID, cname)
return nil, errors.Errorf("[channel %s] claimed to start chaincode container for %s but could not find handler", chainID, cname)
}

return h, nil
Expand Down Expand Up @@ -288,19 +277,38 @@ func processChaincodeExecutionResult(txid, ccName string, resp *pb.ChaincodeMess
}
}

func (cs *ChaincodeSupport) InvokeInit(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, input *pb.ChaincodeInput) (*pb.ChaincodeMessage, error) {
h, err := cs.Launch(txParams.ChannelID, cccid.Name, cccid.Version, txParams.TXSimulator)
if err != nil {
return nil, err
}

return cs.execute(pb.ChaincodeMessage_INIT, txParams, cccid, input, h)
}

// Invoke will invoke chaincode and return the message containing the response.
// The chaincode will be launched if it is not already running.
func (cs *ChaincodeSupport) Invoke(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, input *pb.ChaincodeInput) (*pb.ChaincodeMessage, error) {
h, err := cs.Launch(txParams.ChannelID, cccid.Name, cccid.Version, txParams.TXSimulator)
// at first we go to _lifecycle to retrieve information about the chaincode
var ccci *ccprovider.ChaincodeContainerInfo
var err error

// TODO: remove this once _lifecycle has definitions for all system chaincodes
if !cs.SystemCCProvider.IsSysCC(cccid.Name) {
ccci, err = cs.Lifecycle.ChaincodeContainerInfo(cccid.Name, txParams.TXSimulator)
if err != nil {
// TODO: There has to be a better way to do this...
if cs.UserRunsCC {
chaincodeLogger.Error(
"You are attempting to perform an action other than Deploy on Chaincode that is not ready and you are in developer mode. Did you forget to Deploy your chaincode?",
)
}

return nil, errors.Wrapf(err, "[channel %s] failed to get chaincode container info for %s", txParams.ChannelID, cccid.Name)
}
} else {
ccci = &ccprovider.ChaincodeContainerInfo{
Version: util.GetSysCCVersion(),
Name: cccid.Name,
}
}

// fill the chaincode version field from the chaincode
// container info that we got from _lifecycle
cccid.Version = ccci.Version

h, err := cs.Launch(txParams.ChannelID, ccci)
if err != nil {
return nil, err
}
Expand Down
16 changes: 13 additions & 3 deletions core/chaincode/chaincode_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/hyperledger/fabric/protoutil"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
ma "github.com/stretchr/testify/mock"
)

var globalBlockNum map[string]uint64
Expand Down Expand Up @@ -175,14 +176,24 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
config.ExecuteTimeout = 1 * time.Second
pr := platforms.NewRegistry(&golang.Platform{})
lsccImpl := lscc.New(sccp, mockAclProvider, pr)
ml := &mock.Lifecycle{}
ml.On("ChaincodeContainerInfo", "shimTestCC", ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: "shimTestCC", Version: "0"}, nil)
ml.On("ChaincodeContainerInfo", "calledCC", ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: "calledCC", Version: "0"}, nil)
ml.On("ChaincodeContainerInfo", "lscc", ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: "lscc", Version: util.GetSysCCVersion()}, nil)
ml.On("ChaincodeContainerInfo", "badccname", ma.Anything).Return(nil, errors.New("get lost"))
mcd := &mock.ChaincodeDefinition{}
mcd.On("CCVersion").Return("0")
mcd.On("Hash").Return([]byte("Hulk, (sm)hash"))
mcd.On("RequiresInit").Return(false)
ml.On("ChaincodeDefinition", "calledCC", ma.Anything).Return(mcd, nil)
chaincodeSupport := NewChaincodeSupport(
config,
"0.0.0.0:7052",
true,
ca.CertBytes(),
certGenerator,
&ccprovider.CCInfoFSImpl{},
lsccImpl,
ml,
mockAclProvider,
container.NewVMController(
map[string]container.VMProvider{
Expand Down Expand Up @@ -454,8 +465,7 @@ func initializeCC(t *testing.T, chainID, ccname string, ccSide *mockpeer.MockCCC
}

badcccid := &ccprovider.CCContext{
Name: ccname,
Version: "unknownver",
Name: "badccname",
}

//we are not going to reach the chaincode and so won't get a response from it. processDone will not
Expand Down
52 changes: 40 additions & 12 deletions core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/hyperledger/fabric/core/aclmgmt"
aclmocks "github.com/hyperledger/fabric/core/aclmgmt/mocks"
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
cm "github.com/hyperledger/fabric/core/chaincode/mock"
"github.com/hyperledger/fabric/core/chaincode/platforms"
"github.com/hyperledger/fabric/core/chaincode/platforms/golang"
"github.com/hyperledger/fabric/core/chaincode/shim"
Expand Down Expand Up @@ -67,12 +68,13 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
ma "github.com/stretchr/testify/mock"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

//initialize peer and start up. If security==enabled, login as vp
func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), error) {
func initPeer(chainIDs ...string) (*cm.Lifecycle, net.Listener, *ChaincodeSupport, func(), error) {
//start clean
finitPeer(nil, chainIDs...)

Expand Down Expand Up @@ -102,19 +104,19 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
if viper.GetBool("peer.tls.enabled") {
creds, err := credentials.NewServerTLSFromFile(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file"))
if err != nil {
return nil, nil, nil, fmt.Errorf("Failed to generate credentials %v", err)
return nil, nil, nil, nil, fmt.Errorf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
grpcServer := grpc.NewServer(opts...)

peerAddress, err := peer.GetLocalAddress()
if err != nil {
return nil, nil, nil, fmt.Errorf("Error obtaining peer address: %s", err)
return nil, nil, nil, nil, fmt.Errorf("Error obtaining peer address: %s", err)
}
lis, err := net.Listen("tcp", peerAddress)
if err != nil {
return nil, nil, nil, fmt.Errorf("Error starting peer listener %s", err)
return nil, nil, nil, nil, fmt.Errorf("Error starting peer listener %s", err)
}

ccprovider.SetChaincodesPath(ccprovider.GetChaincodeInstallPathFromViper())
Expand All @@ -124,14 +126,19 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
config.StartupTimeout = 3 * time.Minute
pr := platforms.NewRegistry(&golang.Platform{})
lsccImpl := lscc.New(sccp, mockAclProvider, pr)
ml := &cm.Lifecycle{}
ml.On("ChaincodeContainerInfo", "lscc", ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: "lscc", Version: util.GetSysCCVersion()}, nil)
ml.On("ChaincodeContainerInfo", "pthru", ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: "pthru", Version: "0"}, nil)
ml.On("ChaincodeContainerInfo", "example02", ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: "example02", Version: "0"}, nil)
ml.On("ChaincodeContainerInfo", "tmap", ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: "tmap", Version: "0"}, nil)
chaincodeSupport := NewChaincodeSupport(
config,
peerAddress,
false,
ca.CertBytes(),
certGenerator,
&ccprovider.CCInfoFSImpl{},
lsccImpl,
ml,
aclmgmt.NewACLProvider(func(string) channelconfig.Resources { return nil }),
container.NewVMController(
map[string]container.VMProvider{
Expand All @@ -158,7 +165,7 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
sccp.DeDeploySysCCs(id, ccp)
if err = peer.MockCreateChain(id); err != nil {
closeListenerAndSleep(lis)
return nil, nil, nil, err
return nil, nil, nil, nil, err
}
sccp.DeploySysCCs(id, ccp)
// any chain other than the default testchainid does not have a MSP set up -> create one
Expand All @@ -170,7 +177,7 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
go grpcServer.Serve(lis)

// was passing nil nis at top
return lis, chaincodeSupport, func() { finitPeer(lis, chainIDs...) }, nil
return ml, lis, chaincodeSupport, func() { finitPeer(lis, chainIDs...) }, nil
}

func finitPeer(lis net.Listener, chainIDs ...string) {
Expand Down Expand Up @@ -590,11 +597,18 @@ const (
chaincodePassthruGolangPath = "github.com/hyperledger/fabric/core/chaincode/testdata/src/chaincodes/passthru"
)

func runChaincodeInvokeChaincode(t *testing.T, channel1 string, channel2 string, tc tcicTc, cccid1 *ccprovider.CCContext, expectedA int, expectedB int, nextBlockNumber1, nextBlockNumber2 uint64, chaincodeSupport *ChaincodeSupport) (uint64, uint64) {
func runChaincodeInvokeChaincode(t *testing.T, channel1 string, channel2 string, tc tcicTc, cccid1 *ccprovider.CCContext, expectedA int, expectedB int, nextBlockNumber1, nextBlockNumber2 uint64, chaincodeSupport *ChaincodeSupport, ml *cm.Lifecycle) (uint64, uint64) {
var ctxt = context.Background()

// chaincode2: the chaincode that will call by chaincode1
chaincode2Name := generateChaincodeName(tc.chaincodeType)
ml.On("ChaincodeContainerInfo", chaincode2Name, ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: chaincode2Name, Version: "0"}, nil)
mcd := &cm.ChaincodeDefinition{}
mcd.On("CCName").Return(chaincode2Name)
mcd.On("CCVersion").Return("0")
mcd.On("Hash").Return([]byte("Hulk, (sm)hash"))
mcd.On("RequiresInit").Return(false)
ml.On("ChaincodeDefinition", chaincode2Name, ma.Anything).Return(mcd, nil)
chaincode2Version := "0"
chaincode2Type := tc.chaincodeType
chaincode2Path := tc.chaincodePath
Expand Down Expand Up @@ -747,7 +761,7 @@ type tcicTc struct {
func TestChaincodeInvokeChaincode(t *testing.T) {
channel := util.GetTestChainID()
channel2 := channel + "2"
lis, chaincodeSupport, cleanup, err := initPeer(channel, channel2)
ml, lis, chaincodeSupport, cleanup, err := initPeer(channel, channel2)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
Expand All @@ -765,6 +779,13 @@ func TestChaincodeInvokeChaincode(t *testing.T) {

// deploy the chaincode that will be called by the second chaincode
chaincode1Name := generateChaincodeName(pb.ChaincodeSpec_GOLANG)
ml.On("ChaincodeContainerInfo", chaincode1Name, ma.Anything).Return(&ccprovider.ChaincodeContainerInfo{Name: chaincode1Name, Version: "0"}, nil)
mcd := &cm.ChaincodeDefinition{}
mcd.On("CCName").Return(chaincode1Name)
mcd.On("CCVersion").Return("0")
mcd.On("Hash").Return([]byte("Hulk, (sm)hash"))
mcd.On("RequiresInit").Return(false)
ml.On("ChaincodeDefinition", chaincode1Name, ma.Anything).Return(mcd, nil)
chaincode1Version := "0"
chaincode1Type := pb.ChaincodeSpec_GOLANG
chaincode1Path := chaincodeExample02GolangPath
Expand Down Expand Up @@ -810,6 +831,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
nextBlockNumber1,
nextBlockNumber2,
chaincodeSupport,
ml,
)
})

Expand All @@ -830,14 +852,20 @@ func stopChaincode(ctx context.Context, chaincodeCtx *ccprovider.CCContext, chai
func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) {
chainID := util.GetTestChainID()

_, chaincodeSupport, cleanup, err := initPeer(chainID)
ml, _, chaincodeSupport, cleanup, err := initPeer(chainID)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
}
defer cleanup()

mockAclProvider.On("CheckACL", mock.Anything, mock.Anything, mock.Anything).Return(nil)
mcd := &cm.ChaincodeDefinition{}
mcd.On("CCName").Return("example02")
mcd.On("CCVersion").Return("0")
mcd.On("Hash").Return([]byte("Hulk, (sm)hash"))
mcd.On("RequiresInit").Return(false)
ml.On("ChaincodeDefinition", "example02", ma.Anything).Return(mcd, nil)

// Deploy first chaincode
cID1 := &pb.ChaincodeID{Name: "example02", Path: chaincodeExample02GolangPath, Version: "0"}
Expand Down Expand Up @@ -925,7 +953,7 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) {
func TestChaincodeInit(t *testing.T) {
chainID := util.GetTestChainID()

_, chaincodeSupport, cleanup, err := initPeer(chainID)
_, _, chaincodeSupport, cleanup, err := initPeer(chainID)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
Expand Down Expand Up @@ -993,7 +1021,7 @@ func TestQueries(t *testing.T) {

chainID := util.GetTestChainID()

_, chaincodeSupport, cleanup, err := initPeer(chainID)
_, _, chaincodeSupport, cleanup, err := initPeer(chainID)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/executetransaction_pvtdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestQueriesPrivateData(t *testing.T) {
// This test should be moved as an integration test outside of chaincode package.
t.Skip()
chainID := util.GetTestChainID()
_, chaincodeSupport, cleanup, err := initPeer(chainID)
_, _, chaincodeSupport, cleanup, err := initPeer(chainID)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
Expand Down

0 comments on commit ce4b38c

Please sign in to comment.