Skip to content

Commit

Permalink
Categorise chaincode operations by peer and gateway (#194)
Browse files Browse the repository at this point in the history
Previous implementation was standalone functions, with all requiring
both a gRPC connection and a client signing identity. This required:

- An underisably large number of prameters.
- Unwritten conventions on parameter ordering.
- Documentation comments to highlight when the connection should be to a
specific peer or to an organization gateway.
- Duplication of arguments across multiple functions for calling code
driving the chaincode lifecycle.

This implementation defines a Peer and Gateway struct that hold a gRPC
connection and a client identity. Each of these types defines methods
appropriate to their logical role to:

- Avoid the need to provide gRPC connection and client identity
arguments on every call.
- Provide calling code with logical context for what they are
interacting with.
- Allow IDE code completion to suggest appropriate methods for the
connection type.

For example, this pseudo-code flow:

    chaincode.Install(ctx, peerConnection, id, chaincodePackage)
    chaincode.Approve(ctx, gatewayConnection, id, chaincodeDefinition)
    chaincode.Commit(ctx, gatewayConnection, id, chaincodeDefinition)

becomes:

    peer.Install(ctx, chaincodePackage)
    gateway.Approve(ctx, chaincodeDefinition)
    gateway.Commit(ctx, chaincodeDefinition)

Signed-off-by: Mark S. Lewis <[email protected]>
  • Loading branch information
bestbeforetoday authored Sep 19, 2024
1 parent b61ba94 commit 29e9edb
Show file tree
Hide file tree
Showing 30 changed files with 747 additions and 736 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ unit-test: unit-test-go unit-test-node
.PHONY: unit-test-go
unit-test-go:
cd '$(base_dir)' && \
go test -coverprofile='$(base_dir)/coverage.out' '$(go_dir)/...'
go test -race -coverprofile='$(base_dir)/coverage.out' '$(go_dir)/...'

.PHONY: unit-test-node
unit-test-node:
Expand Down
86 changes: 0 additions & 86 deletions pkg/chaincode/approve.go

This file was deleted.

28 changes: 17 additions & 11 deletions pkg/chaincode/approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package chaincode
package chaincode_test

import (
"context"

"github.com/hyperledger/fabric-admin-sdk/pkg/chaincode"
"github.com/hyperledger/fabric-protos-go-apiv2/common"
"github.com/hyperledger/fabric-protos-go-apiv2/gateway"
"github.com/hyperledger/fabric-protos-go-apiv2/peer"
Expand Down Expand Up @@ -85,11 +86,11 @@ func AssertEqualStatus(expected error, actual error) {

var _ = Describe("Approve", func() {
var channelName string
var chaincodeDefinition *Definition
var chaincodeDefinition *chaincode.Definition

BeforeEach(func() {
channelName = "CHANNEL"
chaincodeDefinition = &Definition{
chaincodeDefinition = &chaincode.Definition{
Name: "CHAINCODE",
Version: "1.0",
Sequence: 1,
Expand Down Expand Up @@ -125,11 +126,12 @@ var _ = Describe("Approve", func() {
})

mockSigner := NewMockSigner(controller, "", nil, nil)
gateway := chaincode.NewGateway(mockConnection, mockSigner)

ctx, cancel := context.WithCancel(specCtx)
cancel()

err := Approve(ctx, mockConnection, mockSigner, chaincodeDefinition)
err := gateway.Approve(ctx, chaincodeDefinition)

Expect(endorseCtxErr).To(BeIdenticalTo(context.Canceled), "endorse context error")
Expect(submitCtxErr).To(BeIdenticalTo(context.Canceled), "submit context error")
Expand All @@ -148,8 +150,9 @@ var _ = Describe("Approve", func() {
Return(expectedErr)

mockSigner := NewMockSigner(controller, "", nil, nil)
gateway := chaincode.NewGateway(mockConnection, mockSigner)

err := Approve(specCtx, mockConnection, mockSigner, chaincodeDefinition)
err := gateway.Approve(specCtx, chaincodeDefinition)

Expect(err).To(MatchError(expectedErr))
AssertEqualStatus(expectedErr, err)
Expand Down Expand Up @@ -177,8 +180,9 @@ var _ = Describe("Approve", func() {
Return(expectedErr)

mockSigner := NewMockSigner(controller, "", nil, nil)
gateway := chaincode.NewGateway(mockConnection, mockSigner)

err := Approve(specCtx, mockConnection, mockSigner, chaincodeDefinition)
err := gateway.Approve(specCtx, chaincodeDefinition)

Expect(err).To(MatchError(expectedErr))
AssertEqualStatus(expectedErr, err)
Expand All @@ -201,15 +205,16 @@ var _ = Describe("Approve", func() {
Return(expectedErr)

mockSigner := NewMockSigner(controller, "", nil, nil)
gateway := chaincode.NewGateway(mockConnection, mockSigner)

err := Approve(specCtx, mockConnection, mockSigner, chaincodeDefinition)
err := gateway.Approve(specCtx, chaincodeDefinition)

Expect(err).To(MatchError(expectedErr))
AssertEqualStatus(expectedErr, err)
})

DescribeTable("Proposal content",
func(specCtx SpecContext, newInput func(*Definition) *Definition, newExpected func(*lifecycle.ApproveChaincodeDefinitionForMyOrgArgs) *lifecycle.ApproveChaincodeDefinitionForMyOrgArgs) {
func(specCtx SpecContext, newInput func(*chaincode.Definition) *chaincode.Definition, newExpected func(*lifecycle.ApproveChaincodeDefinitionForMyOrgArgs) *lifecycle.ApproveChaincodeDefinitionForMyOrgArgs) {
input := newInput(chaincodeDefinition)
expected := newExpected(&lifecycle.ApproveChaincodeDefinitionForMyOrgArgs{
Name: chaincodeDefinition.Name,
Expand Down Expand Up @@ -242,8 +247,9 @@ var _ = Describe("Approve", func() {
})

mockSigner := NewMockSigner(controller, "", nil, nil)
gateway := chaincode.NewGateway(mockConnection, mockSigner)

err := Approve(specCtx, mockConnection, mockSigner, input)
err := gateway.Approve(specCtx, input)
Expect(err).NotTo(HaveOccurred())

invocationSpec := AssertUnmarshalInvocationSpec(endorseRequest.GetProposedTransaction())
Expand All @@ -257,7 +263,7 @@ var _ = Describe("Approve", func() {
},
Entry(
"Proposal includes specified package ID",
func(in *Definition) *Definition {
func(in *chaincode.Definition) *chaincode.Definition {
in.PackageID = "PACKAGE_ID"
return in
},
Expand All @@ -272,7 +278,7 @@ var _ = Describe("Approve", func() {
),
Entry(
"Proposal includes unspecified chaincode source with no package ID specified",
func(in *Definition) *Definition {
func(in *chaincode.Definition) *chaincode.Definition {
return in
},
func(in *lifecycle.ApproveChaincodeDefinitionForMyOrgArgs) *lifecycle.ApproveChaincodeDefinitionForMyOrgArgs {
Expand Down
9 changes: 5 additions & 4 deletions pkg/chaincode/chaincodeCCAAS_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package chaincode
package chaincode_test

import (
"archive/tar"
Expand All @@ -9,22 +9,23 @@ import (
"path/filepath"
"strings"

"github.com/hyperledger/fabric-admin-sdk/pkg/chaincode"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Package", func() {
It("CCaaS", func() {
dummyConnection := Connection{
dummyConnection := chaincode.Connection{
Address: "127.0.0.1:8080",
DialTimeout: "10s",
TLSRequired: false,
}
dummyMeta := Metadata{
dummyMeta := chaincode.Metadata{
Type: "ccaas",
Label: "basic-asset",
}
err := PackageCCAAS(dummyConnection, dummyMeta, tmpDir, "chaincode.tar.gz")
err := chaincode.PackageCCAAS(dummyConnection, dummyMeta, tmpDir, "chaincode.tar.gz")
Expect(err).NotTo(HaveOccurred())
// so far no plan to verify the file
file, err := os.Open(tmpDir + "/chaincode.tar.gz")
Expand Down
2 changes: 1 addition & 1 deletion pkg/chaincode/chaincode_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package chaincode
package chaincode_test

import (
"testing"
Expand Down
66 changes: 0 additions & 66 deletions pkg/chaincode/checkcommitreadiness.go

This file was deleted.

16 changes: 10 additions & 6 deletions pkg/chaincode/checkcommitreadiness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package chaincode
package chaincode_test

import (
"context"

"github.com/hyperledger/fabric-admin-sdk/pkg/chaincode"
"github.com/hyperledger/fabric-protos-go-apiv2/gateway"
"github.com/hyperledger/fabric-protos-go-apiv2/peer/lifecycle"
. "github.com/onsi/ginkgo/v2"
Expand All @@ -21,11 +22,11 @@ import (

var _ = Describe("CheckCommitReadiness", func() {
var channelName string
var chaincodeDefinition *Definition
var chaincodeDefinition *chaincode.Definition

BeforeEach(func() {
channelName = "mockchannel"
chaincodeDefinition = &Definition{
chaincodeDefinition = &chaincode.Definition{
Name: "CHAINCODE",
Version: "1.0",
Sequence: 1,
Expand All @@ -48,11 +49,12 @@ var _ = Describe("CheckCommitReadiness", func() {
})

mockSigner := NewMockSigner(controller, "", nil, nil)
gateway := chaincode.NewGateway(mockConnection, mockSigner)

ctx, cancel := context.WithCancel(specCtx)
cancel()

_, _ = CheckCommitReadiness(ctx, mockConnection, mockSigner, chaincodeDefinition)
_, _ = gateway.CheckCommitReadiness(ctx, chaincodeDefinition)

Expect(evaluateCtxErr).To(BeIdenticalTo(context.Canceled))
})
Expand All @@ -69,8 +71,9 @@ var _ = Describe("CheckCommitReadiness", func() {
Return(expectedErr)

mockSigner := NewMockSigner(controller, "", nil, nil)
gateway := chaincode.NewGateway(mockConnection, mockSigner)

_, err := CheckCommitReadiness(specCtx, mockConnection, mockSigner, chaincodeDefinition)
_, err := gateway.CheckCommitReadiness(specCtx, chaincodeDefinition)

Expect(err).To(MatchError(expectedErr))
AssertEqualStatus(expectedErr, err)
Expand All @@ -96,8 +99,9 @@ var _ = Describe("CheckCommitReadiness", func() {
}).
Times(1)
mockSigner := NewMockSigner(controller, "", nil, nil)
gateway := chaincode.NewGateway(mockConnection, mockSigner)

_, err := CheckCommitReadiness(specCtx, mockConnection, mockSigner, chaincodeDefinition)
_, err := gateway.CheckCommitReadiness(specCtx, chaincodeDefinition)
Expect(err).NotTo(HaveOccurred())

invocationSpec := AssertUnmarshalInvocationSpec(evaluateRequest.GetProposedTransaction())
Expand Down
Loading

0 comments on commit 29e9edb

Please sign in to comment.