Skip to content

Commit

Permalink
FAB-9803 Push PeerID and NetworkID into dc
Browse files Browse the repository at this point in the history
Only the docker-controller needs the PeerID and NetworkID, but it is
constantly passed as part of the arguments through the ccintf.  This is
unnecessary, and the docker controller instance should retain the value
of these strings instead.  This CR does that.

Change-Id: I64ab1e1405dd646355e469dfda6ec880dc199f4a
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed May 10, 2018
1 parent f789a76 commit ca2360b
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 89 deletions.
2 changes: 0 additions & 2 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ func NewChaincodeSupport(
Processor: processor,
CACert: caCert,
PeerAddress: peerAddress,
PeerID: config.PeerID,
PeerNetworkID: config.PeerNetworkID,
CommonEnv: []string{
"CORE_CHAINCODE_LOGGING_LEVEL=" + config.LogLevel,
"CORE_CHAINCODE_LOGGING_SHIM=" + config.ShimLogLevel,
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/chaincode_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
aclmgmt.GetACLProvider(),
container.NewVMController(
map[string]container.VMProvider{
dockercontroller.ContainerType: dockercontroller.NewProvider(),
dockercontroller.ContainerType: dockercontroller.NewProvider("", ""),
inproccontroller.ContainerType: ipRegistry,
},
),
Expand Down
4 changes: 0 additions & 4 deletions core/chaincode/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const (
)

type Config struct {
PeerNetworkID string
PeerID string
TLSEnabled bool
Keepalive time.Duration
ExecuteTimeout time.Duration
Expand All @@ -45,8 +43,6 @@ func (c *Config) load() {
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)

c.PeerNetworkID = viper.GetString("peer.networkId")
c.PeerID = viper.GetString("peer.id")
c.TLSEnabled = viper.GetBool("peer.tls.enabled")

c.Keepalive = toSeconds(viper.GetString("chaincode.keepalive"), 0)
Expand Down
2 changes: 0 additions & 2 deletions core/chaincode/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ func TestGlobalConfig(t *testing.T) {
viper.Set("chaincode.logging.shim", "WARNING")

config := chaincode.GlobalConfig()
assert.Equal(t, "test-peer-network-id", config.PeerNetworkID)
assert.Equal(t, "test-peer-id", config.PeerID)
assert.Equal(t, true, config.TLSEnabled)
assert.Equal(t, 50*time.Second, config.Keepalive)
assert.Equal(t, 20*time.Hour, config.ExecuteTimeout)
Expand Down
8 changes: 1 addition & 7 deletions core/chaincode/container_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ type ContainerRuntime struct {
CACert []byte
CommonEnv []string
PeerAddress string
PeerID string
PeerNetworkID string
}

type platformBuilder struct {
Expand All @@ -65,7 +63,7 @@ func (c *ContainerRuntime) Start(ctxt context.Context, cccid *ccprovider.CCConte
return err
}

chaincodeLogger.Debugf("start container: %s(networkid:%s,peerid:%s)", cname, c.PeerNetworkID, c.PeerID)
chaincodeLogger.Debugf("start container: %s(networkid:%s,peerid:%s)", cname)
chaincodeLogger.Debugf("start container with args: %s", strings.Join(lc.Args, " "))
chaincodeLogger.Debugf("start container with env:\n\t%s", strings.Join(lc.Envs, "\n\t"))

Expand All @@ -77,8 +75,6 @@ func (c *ContainerRuntime) Start(ctxt context.Context, cccid *ccprovider.CCConte
FilesToUpload: lc.Files,
CCID: ccintf.CCID{
ChaincodeSpec: cds.ChaincodeSpec,
NetworkID: c.PeerNetworkID,
PeerID: c.PeerID,
Version: cccid.Version,
},
}
Expand All @@ -97,8 +93,6 @@ func (c *ContainerRuntime) Stop(ctxt context.Context, cccid *ccprovider.CCContex
scr := container.StopContainerReq{
CCID: ccintf.CCID{
ChaincodeSpec: cds.ChaincodeSpec,
NetworkID: c.PeerNetworkID,
PeerID: c.PeerID,
Version: cccid.Version,
},
Timeout: 0,
Expand Down
24 changes: 6 additions & 18 deletions core/chaincode/container_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,8 @@ func TestContainerRuntimeStart(t *testing.T) {
for _, tc := range tests {
fakeProcessor := &mock.Processor{}
cr := &chaincode.ContainerRuntime{
Processor: fakeProcessor,
PeerAddress: "peer.example.com",
PeerID: "peer-id",
PeerNetworkID: "peer-network-id",
Processor: fakeProcessor,
PeerAddress: "peer.example.com",
}

ccctx := ccprovider.NewCCContext("context-chain-id", "context-name", "context-version", "context-tx-id", false, nil, nil)
Expand Down Expand Up @@ -202,8 +200,6 @@ func TestContainerRuntimeStart(t *testing.T) {
assert.Equal(t, startReq.CCID, ccintf.CCID{
ChaincodeSpec: cds.ChaincodeSpec,
Version: "context-version",
NetworkID: "peer-network-id",
PeerID: "peer-id",
})
}
}
Expand All @@ -224,10 +220,8 @@ func TestContainerRuntimeStartErrors(t *testing.T) {
fakeProcessor.ProcessReturns(tc.processErr)

cr := &chaincode.ContainerRuntime{
Processor: fakeProcessor,
PeerAddress: "peer.example.com",
PeerID: "peer-id",
PeerNetworkID: "peer-network-id",
Processor: fakeProcessor,
PeerAddress: "peer.example.com",
}

ccctx := ccprovider.NewCCContext("context-chain-id", "context-name", "context-version", "context-tx-id", false, nil, nil)
Expand All @@ -254,9 +248,7 @@ func TestContainerRuntimeStop(t *testing.T) {
for _, tc := range tests {
fakeProcessor := &mock.Processor{}
cr := &chaincode.ContainerRuntime{
Processor: fakeProcessor,
PeerID: "peer-id",
PeerNetworkID: "peer-network-id",
Processor: fakeProcessor,
}

ccctx := ccprovider.NewCCContext("context-chain-id", "context-name", "context-version", "context-tx-id", false, nil, nil)
Expand All @@ -282,8 +274,6 @@ func TestContainerRuntimeStop(t *testing.T) {
assert.Equal(t, stopReq.CCID, ccintf.CCID{
ChaincodeSpec: cds.ChaincodeSpec,
Version: "context-version",
NetworkID: "peer-network-id",
PeerID: "peer-id",
})
}
}
Expand All @@ -302,9 +292,7 @@ func TestContainerRuntimeStopErrors(t *testing.T) {
fakeProcessor.ProcessReturns(tc.processErr)

cr := &chaincode.ContainerRuntime{
Processor: fakeProcessor,
PeerID: "peer-id",
PeerNetworkID: "peer-network-id",
Processor: fakeProcessor,
}

ccctx := ccprovider.NewCCContext("context-chain-id", "context-name", "context-version", "context-tx-id", false, nil, nil)
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
aclmgmt.GetACLProvider(),
container.NewVMController(
map[string]container.VMProvider{
dockercontroller.ContainerType: dockercontroller.NewProvider(),
dockercontroller.ContainerType: dockercontroller.NewProvider("", ""),
inproccontroller.ContainerType: ipRegistry,
},
),
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/systemchaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func initSysCCTests() (*oldSysCCInfo, net.Listener, *ChaincodeSupport, error) {
aclmgmt.GetACLProvider(),
container.NewVMController(
map[string]container.VMProvider{
dockercontroller.ContainerType: dockercontroller.NewProvider(),
dockercontroller.ContainerType: dockercontroller.NewProvider("", ""),
inproccontroller.ContainerType: ipRegistry,
},
),
Expand Down
2 changes: 0 additions & 2 deletions core/container/ccintf/ccintf.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func GetCCHandlerKey() string {
//CCID encapsulates chaincode ID
type CCID struct {
ChaincodeSpec *pb.ChaincodeSpec
NetworkID string
PeerID string
Version string
}

Expand Down
49 changes: 25 additions & 24 deletions core/container/dockercontroller/dockercontroller.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package dockercontroller
Expand Down Expand Up @@ -59,6 +49,8 @@ type getClient func() (dockerClient, error)
type DockerVM struct {
id string
getClientFnc getClient
PeerID string
NetworkID string
}

// dockerClient represents a docker client
Expand Down Expand Up @@ -90,21 +82,30 @@ type dockerClient interface {
}

// Controller implements container.VMProvider
type Provider struct{}
type Provider struct {
PeerID string
NetworkID string
}

// NewProvider creates a new instance of Provider
func NewProvider() *Provider {
return &Provider{}
func NewProvider(peerID, networkID string) *Provider {
return &Provider{
PeerID: peerID,
NetworkID: networkID,
}
}

// NewVM creates a new DockerVM instance
func (p *Provider) NewVM() container.VM {
return NewDockerVM()
return NewDockerVM(p.PeerID, p.NetworkID)
}

// NewDockerVM returns a new DockerVM instance
func NewDockerVM() *DockerVM {
vm := DockerVM{}
func NewDockerVM(peerID, networkID string) *DockerVM {
vm := DockerVM{
PeerID: peerID,
NetworkID: networkID,
}
vm.getClientFnc = getDockerClient
return &vm
}
Expand Down Expand Up @@ -479,12 +480,12 @@ func (vm *DockerVM) Destroy(ctxt context.Context, ccid ccintf.CCID, force bool,
func (vm *DockerVM) GetVMName(ccid ccintf.CCID, format func(string) (string, error)) (string, error) {
name := ccid.GetName()

if ccid.NetworkID != "" && ccid.PeerID != "" {
name = fmt.Sprintf("%s-%s-%s", ccid.NetworkID, ccid.PeerID, name)
} else if ccid.NetworkID != "" {
name = fmt.Sprintf("%s-%s", ccid.NetworkID, name)
} else if ccid.PeerID != "" {
name = fmt.Sprintf("%s-%s", ccid.PeerID, name)
if vm.NetworkID != "" && vm.PeerID != "" {
name = fmt.Sprintf("%s-%s-%s", vm.NetworkID, vm.PeerID, name)
} else if vm.NetworkID != "" {
name = fmt.Sprintf("%s-%s", vm.NetworkID, name)
} else if vm.PeerID != "" {
name = fmt.Sprintf("%s-%s", vm.PeerID, name)
}

if format != nil {
Expand Down
88 changes: 62 additions & 26 deletions core/container/dockercontroller/dockercontroller_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package dockercontroller
Expand Down Expand Up @@ -46,7 +36,7 @@ import (
func TestRealPath(t *testing.T) {
coreutil.SetupTestConfig()
ctxt := context.Background()
dc := NewDockerVM()
dc := NewDockerVM("", "")
ccid := ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "simple"}}}
reader := getCodeChainBytesInMem()

Expand Down Expand Up @@ -249,27 +239,73 @@ func Test_Destroy(t *testing.T) {

type testCase struct {
name string
vm *DockerVM
ccid ccintf.CCID
formatFunc func(string) (string, error)
expectedOutput string
}

func TestGetVMName(t *testing.T) {
dvm := DockerVM{}
var tc []testCase

tc = append(tc,
testCase{"mycc", ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "mycc"}}, NetworkID: "dev", PeerID: "peer0", Version: "1.0"}, formatImageName, fmt.Sprintf("%s-%s", "dev-peer0-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("dev-peer0-mycc-1.0"))))},
testCase{"mycc-nonetworkid", ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "mycc"}}, PeerID: "peer1", Version: "1.0"}, formatImageName, fmt.Sprintf("%s-%s", "peer1-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("peer1-mycc-1.0"))))},
testCase{"myCC-UCids", ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, NetworkID: "Dev", PeerID: "Peer0", Version: "1.0"}, formatImageName, fmt.Sprintf("%s-%s", "dev-peer0-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("Dev-Peer0-myCC-1.0"))))},
testCase{"myCC-idsWithSpecialChars", ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, NetworkID: "Dev$dev", PeerID: "Peer*0", Version: "1.0"}, formatImageName, fmt.Sprintf("%s-%s", "dev-dev-peer-0-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("Dev$dev-Peer*0-myCC-1.0"))))},
testCase{"mycc-nopeerid", ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "mycc"}}, NetworkID: "dev", Version: "1.0"}, formatImageName, fmt.Sprintf("%s-%s", "dev-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("dev-mycc-1.0"))))},
testCase{"myCC-LCids", ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, NetworkID: "dev", PeerID: "peer0", Version: "1.0"}, formatImageName, fmt.Sprintf("%s-%s", "dev-peer0-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("dev-peer0-myCC-1.0"))))},
testCase{"myCC-preserveCase", ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, NetworkID: "Dev", PeerID: "Peer0", Version: "1.0"}, nil, fmt.Sprintf("%s", "Dev-Peer0-myCC-1.0")},
testCase{"invalidCharsFormatFunction", ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, NetworkID: "Dev", PeerID: "Peer0", Version: "1.0"}, formatInvalidChars, fmt.Sprintf("%s", "inv-lid-character--")})
tc := []testCase{
{
name: "mycc",
vm: &DockerVM{NetworkID: "dev", PeerID: "peer0"},
ccid: ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "mycc"}}, Version: "1.0"},
formatFunc: formatImageName,
expectedOutput: fmt.Sprintf("%s-%s", "dev-peer0-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("dev-peer0-mycc-1.0")))),
},
{
name: "mycc-nonetworkid",
vm: &DockerVM{PeerID: "peer1"},
ccid: ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "mycc"}}, Version: "1.0"},
formatFunc: formatImageName,
expectedOutput: fmt.Sprintf("%s-%s", "peer1-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("peer1-mycc-1.0")))),
},
{
name: "myCC-UCids",
vm: &DockerVM{NetworkID: "Dev", PeerID: "Peer0"},
ccid: ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, Version: "1.0"},
formatFunc: formatImageName,
expectedOutput: fmt.Sprintf("%s-%s", "dev-peer0-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("Dev-Peer0-myCC-1.0")))),
},
{
name: "myCC-idsWithSpecialChars",
vm: &DockerVM{NetworkID: "Dev$dev", PeerID: "Peer*0"},
ccid: ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, Version: "1.0"},
formatFunc: formatImageName,
expectedOutput: fmt.Sprintf("%s-%s", "dev-dev-peer-0-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("Dev$dev-Peer*0-myCC-1.0")))),
},
{
name: "mycc-nopeerid",
vm: &DockerVM{NetworkID: "dev"},
ccid: ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "mycc"}}, Version: "1.0"},
formatFunc: formatImageName,
expectedOutput: fmt.Sprintf("%s-%s", "dev-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("dev-mycc-1.0")))),
},
{
name: "myCC-LCids",
vm: &DockerVM{NetworkID: "dev", PeerID: "peer0"},
ccid: ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, Version: "1.0"},
formatFunc: formatImageName,
expectedOutput: fmt.Sprintf("%s-%s", "dev-peer0-mycc-1.0", hex.EncodeToString(util.ComputeSHA256([]byte("dev-peer0-myCC-1.0")))),
},
{
name: "myCC-preserveCase",
vm: &DockerVM{NetworkID: "Dev", PeerID: "Peer0"},
ccid: ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, Version: "1.0"},
formatFunc: nil,
expectedOutput: fmt.Sprintf("%s", "Dev-Peer0-myCC-1.0")},
{
name: "invalidCharsFormatFunction",
vm: &DockerVM{NetworkID: "Dev", PeerID: "Peer0"},
ccid: ccintf.CCID{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: "myCC"}}, Version: "1.0"},
formatFunc: formatInvalidChars,
expectedOutput: fmt.Sprintf("%s", "inv-lid-character--"),
},
}

for _, test := range tc {
name, err := dvm.GetVMName(test.ccid, test.formatFunc)
name, err := test.vm.GetVMName(test.ccid, test.formatFunc)
assert.Nil(t, err, "Expected nil error")
assert.Equal(t, test.expectedOutput, name, "Unexpected output for test case name: %s", test.name)
}
Expand Down
5 changes: 4 additions & 1 deletion peer/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,10 @@ func registerChaincodeSupport(grpcServer *comm.GRPCServer, ccEndpoint string, ca
&ccprovider.CCInfoFSImpl{},
aclmgmt.GetACLProvider(),
container.NewVMController(map[string]container.VMProvider{
dockercontroller.ContainerType: dockercontroller.NewProvider(),
dockercontroller.ContainerType: dockercontroller.NewProvider(
viper.GetString("peer.id"),
viper.GetString("peer.networkId"),
),
inproccontroller.ContainerType: ipRegistry,
}),
sccp,
Expand Down

0 comments on commit ca2360b

Please sign in to comment.