From 5908d6234be8e23efbe8035d681f651c0b79f01b Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Thu, 5 Aug 2021 14:55:36 -0400 Subject: [PATCH 1/6] Added support for seek type and fromBlock parameters when generating event service cache keys Signed-off-by: Jim Zhang --- pkg/fabsdk/provider/chpvdr/cachekey.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/fabsdk/provider/chpvdr/cachekey.go b/pkg/fabsdk/provider/chpvdr/cachekey.go index 4326a4b4b4..98e0557880 100644 --- a/pkg/fabsdk/provider/chpvdr/cachekey.go +++ b/pkg/fabsdk/provider/chpvdr/cachekey.go @@ -8,10 +8,11 @@ package chpvdr import ( "crypto/sha256" - "strconv" + "fmt" "github.com/hyperledger/fabric-sdk-go/pkg/common/options" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/deliverclient/seek" ) // ctxtCacheKey is a lazy cache key for the context cache @@ -99,6 +100,8 @@ func (k *eventCacheKey) String() string { type params struct { permitBlockEvents bool + seekType seek.Type + fromBlock uint64 } func defaultParams() *params { @@ -109,8 +112,18 @@ func (p *params) PermitBlockEvents() { p.permitBlockEvents = true } +func (p *params) SetFromBlock(value uint64) { + p.fromBlock = value +} + +func (p *params) SetSeekType(value seek.Type) { + if value != "" { + p.seekType = value + } +} + func (p *params) getOptKey() string { // Construct opts portion - optKey := "blockEvents:" + strconv.FormatBool(p.permitBlockEvents) + optKey := fmt.Sprintf("blockEvents:%t,seekType:%s,fromBlock:%d", p.permitBlockEvents, p.seekType, p.fromBlock) return optKey } From 92bc40ab9f714355d5c460d6ef6bc5b53c0e3afa Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Fri, 6 May 2022 17:34:51 -0400 Subject: [PATCH 2/6] Add chaincode ID to the cache key calculation Signed-off-by: Jim Zhang --- ci.properties | 2 +- pkg/client/event/event.go | 4 ++++ pkg/client/event/example_test.go | 19 +++++++++++++++++++ pkg/client/event/opts.go | 9 +++++++++ pkg/fab/events/deliverclient/opts.go | 19 +++++++++++++++++++ pkg/fabsdk/provider/chpvdr/cachekey.go | 9 ++++++++- 6 files changed, 60 insertions(+), 2 deletions(-) diff --git a/ci.properties b/ci.properties index 639384b90d..3bc6df0d87 100644 --- a/ci.properties +++ b/ci.properties @@ -5,4 +5,4 @@ # GO_MIN_VER=1.14.0 -GO_MAX_VER=1.16.99 +GO_MAX_VER=1.17.99 diff --git a/pkg/client/event/event.go b/pkg/client/event/event.go index 0062d99206..6472f9cb2c 100644 --- a/pkg/client/event/event.go +++ b/pkg/client/event/event.go @@ -33,6 +33,7 @@ type Client struct { permitBlockEvents bool fromBlock uint64 seekType seek.Type + chaincodeId string eventConsumerTimeout *time.Duration } @@ -68,6 +69,9 @@ func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client opts = append(opts, deliverclient.WithBlockNum(eventClient.fromBlock)) } } + if eventClient.chaincodeId != "" { + opts = append(opts, deliverclient.WithChaincodeId(eventClient.chaincodeId)) + } if eventClient.eventConsumerTimeout != nil { opts = append(opts, dispatcher.WithEventConsumerTimeout(*eventClient.eventConsumerTimeout)) } diff --git a/pkg/client/event/example_test.go b/pkg/client/event/example_test.go index 098b0464f4..f643b161eb 100644 --- a/pkg/client/event/example_test.go +++ b/pkg/client/event/example_test.go @@ -77,6 +77,25 @@ func ExampleClient_RegisterChaincodeEvent() { } +func ExampleClient_RegisterChaincodeEvent_NewService() { + + ec, err := New(mockChannelProvider("mychannel"), WithChaincodeId("examplecc")) + if err != nil { + fmt.Println("failed to create client") + } + + registration, _, err := ec.RegisterChaincodeEvent("examplecc", "event123") + if err != nil { + fmt.Println("failed to register chaincode event") + } + defer ec.Unregister(registration) + + fmt.Println("chaincode event registered successfully") + + // Output: chaincode event registered successfully + +} + func ExampleClient_RegisterChaincodeEvent_withPayload() { // If you require payload for chaincode events you have to use WithBlockEvents() option diff --git a/pkg/client/event/opts.go b/pkg/client/event/opts.go index 5803fabc56..9315d35fc7 100644 --- a/pkg/client/event/opts.go +++ b/pkg/client/event/opts.go @@ -42,6 +42,15 @@ func WithSeekType(seek seek.Type) ClientOption { } } +// WithChaincodeId indicates the target chaincode +// Only deliverclient supports this +func WithChaincodeId(id string) ClientOption { + return func(c *Client) error { + c.chaincodeId = id + return nil + } +} + // WithEventConsumerTimeout is the timeout when sending events to a registered consumer. // If < 0, if buffer full, unblocks immediately and does not send. // If 0, if buffer full, will block and guarantee the event will be sent out. diff --git a/pkg/fab/events/deliverclient/opts.go b/pkg/fab/events/deliverclient/opts.go index f8c7e80565..6a8f2a52e0 100755 --- a/pkg/fab/events/deliverclient/opts.go +++ b/pkg/fab/events/deliverclient/opts.go @@ -19,6 +19,7 @@ type params struct { connProvider api.ConnectionProvider seekType seek.Type fromBlock uint64 + chaincodeId string respTimeout time.Duration } @@ -48,6 +49,15 @@ func WithBlockNum(value uint64) options.Opt { } } +// WithChaincodeId specifies the chaincode from which events are to be received. +func WithChaincodeId(value string) options.Opt { + return func(p options.Params) { + if setter, ok := p.(chaincodeIdSetter); ok { + setter.SetChaincodeId(value) + } + } +} + type seekTypeSetter interface { SetSeekType(value seek.Type) } @@ -56,6 +66,10 @@ type fromBlockSetter interface { SetFromBlock(value uint64) } +type chaincodeIdSetter interface { + SetChaincodeId(value string) +} + func (p *params) PermitBlockEvents() { logger.Debug("PermitBlockEvents") p.connProvider = deliverProvider @@ -79,6 +93,11 @@ func (p *params) SetSeekType(value seek.Type) { } } +func (p *params) SetChaincodeId(value string) { + logger.Debugf("ChaincodId: %d", value) + p.chaincodeId = value +} + func (p *params) SetResponseTimeout(value time.Duration) { logger.Debugf("ResponseTimeout: %s", value) p.respTimeout = value diff --git a/pkg/fabsdk/provider/chpvdr/cachekey.go b/pkg/fabsdk/provider/chpvdr/cachekey.go index 98e0557880..57184f4e5f 100644 --- a/pkg/fabsdk/provider/chpvdr/cachekey.go +++ b/pkg/fabsdk/provider/chpvdr/cachekey.go @@ -102,6 +102,7 @@ type params struct { permitBlockEvents bool seekType seek.Type fromBlock uint64 + chaincodeId string } func defaultParams() *params { @@ -122,8 +123,14 @@ func (p *params) SetSeekType(value seek.Type) { } } +func (p *params) SetChaincodeId(value string) { + if value != "" { + p.chaincodeId = value + } +} + func (p *params) getOptKey() string { // Construct opts portion - optKey := fmt.Sprintf("blockEvents:%t,seekType:%s,fromBlock:%d", p.permitBlockEvents, p.seekType, p.fromBlock) + optKey := fmt.Sprintf("blockEvents:%t,seekType:%s,fromBlock:%d,chaincodeId:%s", p.permitBlockEvents, p.seekType, p.fromBlock, p.chaincodeId) return optKey } From 31fb7c4a92ff0ead0bb303af52b2dc7029e0b45c Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Mon, 9 May 2022 15:40:25 -0400 Subject: [PATCH 3/6] Fix lint errors Signed-off-by: Jim Zhang --- Makefile | 6 +++--- pkg/client/channel/chclientrun_std.go | 1 + pkg/client/event/event.go | 7 ++++--- pkg/client/event/example_test.go | 2 +- pkg/client/event/opts.go | 6 +++--- pkg/fab/events/deliverclient/opts.go | 18 +++++++++--------- pkg/fabsdk/fabsdk_std.go | 1 + pkg/fabsdk/provider/chpvdr/cachekey.go | 8 ++++---- 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index cb12a3818c..8a0616ec5c 100644 --- a/Makefile +++ b/Makefile @@ -25,8 +25,8 @@ DOCKER_CMD ?= docker DOCKER_COMPOSE_CMD ?= docker-compose # Fabric versions used in the Makefile -FABRIC_STABLE_VERSION := 2.2.0 -FABRIC_STABLE_VERSION_MINOR := 2.2 +FABRIC_STABLE_VERSION := 2.4.3 +FABRIC_STABLE_VERSION_MINOR := 2.4 FABRIC_STABLE_VERSION_MAJOR := 2 FABRIC_PRERELEASE_VERSION := @@ -114,7 +114,7 @@ CC_MODE_LSCC = lscc # Local variables used by makefile PROJECT_NAME := fabric-sdk-go -ARCH := $(shell uname -m) +ARCH := amd64 OS_NAME := $(shell uname -s) FIXTURE_PROJECT_NAME := fabsdkgo MAKEFILE_THIS := $(lastword $(MAKEFILE_LIST)) diff --git a/pkg/client/channel/chclientrun_std.go b/pkg/client/channel/chclientrun_std.go index 3f2a459a44..7ae0b32f2e 100644 --- a/pkg/client/channel/chclientrun_std.go +++ b/pkg/client/channel/chclientrun_std.go @@ -1,3 +1,4 @@ +//go:build !pprof // +build !pprof /* diff --git a/pkg/client/event/event.go b/pkg/client/event/event.go index 6472f9cb2c..bf7370a0b1 100644 --- a/pkg/client/event/event.go +++ b/pkg/client/event/event.go @@ -33,12 +33,13 @@ type Client struct { permitBlockEvents bool fromBlock uint64 seekType seek.Type - chaincodeId string + chaincodeID string eventConsumerTimeout *time.Duration } // New returns a Client instance. Client receives events such as block, filtered block, // chaincode, and transaction status events. +// nolint: gocyclo func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error) { channelContext, err := channelProvider() @@ -69,8 +70,8 @@ func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client opts = append(opts, deliverclient.WithBlockNum(eventClient.fromBlock)) } } - if eventClient.chaincodeId != "" { - opts = append(opts, deliverclient.WithChaincodeId(eventClient.chaincodeId)) + if eventClient.chaincodeID != "" { + opts = append(opts, deliverclient.WithChaincodeID(eventClient.chaincodeID)) } if eventClient.eventConsumerTimeout != nil { opts = append(opts, dispatcher.WithEventConsumerTimeout(*eventClient.eventConsumerTimeout)) diff --git a/pkg/client/event/example_test.go b/pkg/client/event/example_test.go index f643b161eb..6990c01a0f 100644 --- a/pkg/client/event/example_test.go +++ b/pkg/client/event/example_test.go @@ -79,7 +79,7 @@ func ExampleClient_RegisterChaincodeEvent() { func ExampleClient_RegisterChaincodeEvent_NewService() { - ec, err := New(mockChannelProvider("mychannel"), WithChaincodeId("examplecc")) + ec, err := New(mockChannelProvider("mychannel"), WithChaincodeID("examplecc")) if err != nil { fmt.Println("failed to create client") } diff --git a/pkg/client/event/opts.go b/pkg/client/event/opts.go index 9315d35fc7..28d95e4720 100644 --- a/pkg/client/event/opts.go +++ b/pkg/client/event/opts.go @@ -42,11 +42,11 @@ func WithSeekType(seek seek.Type) ClientOption { } } -// WithChaincodeId indicates the target chaincode +// WithChaincodeID indicates the target chaincode // Only deliverclient supports this -func WithChaincodeId(id string) ClientOption { +func WithChaincodeID(id string) ClientOption { return func(c *Client) error { - c.chaincodeId = id + c.chaincodeID = id return nil } } diff --git a/pkg/fab/events/deliverclient/opts.go b/pkg/fab/events/deliverclient/opts.go index 6a8f2a52e0..634571fabc 100755 --- a/pkg/fab/events/deliverclient/opts.go +++ b/pkg/fab/events/deliverclient/opts.go @@ -19,7 +19,7 @@ type params struct { connProvider api.ConnectionProvider seekType seek.Type fromBlock uint64 - chaincodeId string + chaincodeID string respTimeout time.Duration } @@ -49,11 +49,11 @@ func WithBlockNum(value uint64) options.Opt { } } -// WithChaincodeId specifies the chaincode from which events are to be received. -func WithChaincodeId(value string) options.Opt { +// WithChaincodeID specifies the chaincode from which events are to be received. +func WithChaincodeID(value string) options.Opt { return func(p options.Params) { - if setter, ok := p.(chaincodeIdSetter); ok { - setter.SetChaincodeId(value) + if setter, ok := p.(chaincodeIDSetter); ok { + setter.SetChaincodeID(value) } } } @@ -66,8 +66,8 @@ type fromBlockSetter interface { SetFromBlock(value uint64) } -type chaincodeIdSetter interface { - SetChaincodeId(value string) +type chaincodeIDSetter interface { + SetChaincodeID(value string) } func (p *params) PermitBlockEvents() { @@ -93,9 +93,9 @@ func (p *params) SetSeekType(value seek.Type) { } } -func (p *params) SetChaincodeId(value string) { +func (p *params) SetChaincodeID(value string) { logger.Debugf("ChaincodId: %d", value) - p.chaincodeId = value + p.chaincodeID = value } func (p *params) SetResponseTimeout(value time.Duration) { diff --git a/pkg/fabsdk/fabsdk_std.go b/pkg/fabsdk/fabsdk_std.go index 7a70221bb0..17edff4cb6 100644 --- a/pkg/fabsdk/fabsdk_std.go +++ b/pkg/fabsdk/fabsdk_std.go @@ -1,3 +1,4 @@ +//go:build !pprof // +build !pprof /* diff --git a/pkg/fabsdk/provider/chpvdr/cachekey.go b/pkg/fabsdk/provider/chpvdr/cachekey.go index 57184f4e5f..b5a92d6de8 100644 --- a/pkg/fabsdk/provider/chpvdr/cachekey.go +++ b/pkg/fabsdk/provider/chpvdr/cachekey.go @@ -102,7 +102,7 @@ type params struct { permitBlockEvents bool seekType seek.Type fromBlock uint64 - chaincodeId string + chaincodeID string } func defaultParams() *params { @@ -123,14 +123,14 @@ func (p *params) SetSeekType(value seek.Type) { } } -func (p *params) SetChaincodeId(value string) { +func (p *params) SetChaincodeID(value string) { if value != "" { - p.chaincodeId = value + p.chaincodeID = value } } func (p *params) getOptKey() string { // Construct opts portion - optKey := fmt.Sprintf("blockEvents:%t,seekType:%s,fromBlock:%d,chaincodeId:%s", p.permitBlockEvents, p.seekType, p.fromBlock, p.chaincodeId) + optKey := fmt.Sprintf("blockEvents:%t,seekType:%s,fromBlock:%d,chaincodeId:%s", p.permitBlockEvents, p.seekType, p.fromBlock, p.chaincodeID) return optKey } From e064e56288729ded509a3ab3ff335af25469e782 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Mon, 9 May 2022 15:47:08 -0400 Subject: [PATCH 4/6] revert local temporary changes checked in by accident Signed-off-by: Jim Zhang --- Makefile | 6 +++--- ci.properties | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 8a0616ec5c..cb12a3818c 100644 --- a/Makefile +++ b/Makefile @@ -25,8 +25,8 @@ DOCKER_CMD ?= docker DOCKER_COMPOSE_CMD ?= docker-compose # Fabric versions used in the Makefile -FABRIC_STABLE_VERSION := 2.4.3 -FABRIC_STABLE_VERSION_MINOR := 2.4 +FABRIC_STABLE_VERSION := 2.2.0 +FABRIC_STABLE_VERSION_MINOR := 2.2 FABRIC_STABLE_VERSION_MAJOR := 2 FABRIC_PRERELEASE_VERSION := @@ -114,7 +114,7 @@ CC_MODE_LSCC = lscc # Local variables used by makefile PROJECT_NAME := fabric-sdk-go -ARCH := amd64 +ARCH := $(shell uname -m) OS_NAME := $(shell uname -s) FIXTURE_PROJECT_NAME := fabsdkgo MAKEFILE_THIS := $(lastword $(MAKEFILE_LIST)) diff --git a/ci.properties b/ci.properties index 3bc6df0d87..639384b90d 100644 --- a/ci.properties +++ b/ci.properties @@ -5,4 +5,4 @@ # GO_MIN_VER=1.14.0 -GO_MAX_VER=1.17.99 +GO_MAX_VER=1.16.99 From 69369f774b704565456051a0bef95728d0a02aea Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Tue, 10 May 2022 10:55:43 -0400 Subject: [PATCH 5/6] Unit test coverage Signed-off-by: Jim Zhang --- pkg/client/event/event_test.go | 20 +++++++++++++++++-- .../deliverclient/deliverclient_test.go | 1 + pkg/fabsdk/provider/chpvdr/chprovider_test.go | 5 ++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pkg/client/event/event_test.go b/pkg/client/event/event_test.go index d45ce5d1c4..1e34b5b728 100644 --- a/pkg/client/event/event_test.go +++ b/pkg/client/event/event_test.go @@ -20,11 +20,11 @@ import ( "github.com/pkg/errors" "github.com/stretchr/testify/assert" + pb "github.com/hyperledger/fabric-protos-go/peer" "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/deliverclient/seek" "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/service" "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/service/dispatcher" servicemocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/service/mocks" - pb "github.com/hyperledger/fabric-protos-go/peer" ) var ( @@ -43,7 +43,7 @@ func TestNewEventClient(t *testing.T) { t.Fatalf("Failed to create new event client: %s", err) } - _, err = New(ctx, WithBlockEvents(), WithSeekType(seek.Newest), WithBlockNum(math.MaxUint64), WithEventConsumerTimeout(500 * time.Millisecond)) + _, err = New(ctx, WithBlockEvents(), WithSeekType(seek.Newest), WithBlockNum(math.MaxUint64), WithEventConsumerTimeout(500*time.Millisecond), WithChaincodeID("testChaincode")) if err != nil { t.Fatalf("Failed to create new event client: %s", err) } @@ -55,6 +55,22 @@ func TestNewEventClient(t *testing.T) { } } +func TestNewEventClientWithFromBlock(t *testing.T) { + + fabCtx := setupCustomTestContext(t, nil) + ctx := createChannelContext(fabCtx, channelID) + + _, err := New(ctx) + if err != nil { + t.Fatalf("Failed to create new event client: %s", err) + } + + _, err = New(ctx, WithBlockEvents(), WithSeekType(seek.FromBlock), WithBlockNum(100), WithChaincodeID("testChaincode")) + if err != nil { + t.Fatalf("Failed to create new event client: %s", err) + } +} + func TestBlockEvents(t *testing.T) { eventService, eventProducer, err := newServiceWithMockProducer(defaultOpts, withBlockLedger(sourceURL)) diff --git a/pkg/fab/events/deliverclient/deliverclient_test.go b/pkg/fab/events/deliverclient/deliverclient_test.go index d7f965fc1c..20259aee8f 100755 --- a/pkg/fab/events/deliverclient/deliverclient_test.go +++ b/pkg/fab/events/deliverclient/deliverclient_test.go @@ -72,6 +72,7 @@ func TestClientConnect(t *testing.T) { ), WithSeekType(seek.FromBlock), WithBlockNum(0), + WithChaincodeID("testChaincode"), client.WithResponseTimeout(3*time.Second), ) if err != nil { diff --git a/pkg/fabsdk/provider/chpvdr/chprovider_test.go b/pkg/fabsdk/provider/chpvdr/chprovider_test.go index 0fcaf5b88c..79a4a2df29 100644 --- a/pkg/fabsdk/provider/chpvdr/chprovider_test.go +++ b/pkg/fabsdk/provider/chpvdr/chprovider_test.go @@ -1,3 +1,4 @@ +//go:build testing // +build testing /* @@ -24,6 +25,8 @@ import ( "github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig" "github.com/hyperledger/fabric-sdk-go/pkg/fab/discovery" discmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/discovery/mocks" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/client" + "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/deliverclient" "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp" "github.com/pkg/errors" @@ -83,7 +86,7 @@ func TestBasicValidChannel(t *testing.T) { assert.NotNil(t, channelConfig) assert.NotEmptyf(t, channelConfig.ID(), "Got empty channel ID from channel config") - eventService, err := channelService.EventService() + eventService, err := channelService.EventService(client.WithBlockEvents(), deliverclient.WithChaincodeID("testChaincode")) require.NoError(t, err) require.NotNil(t, eventService) From 2ae5ea692dbf3339ed58a4f54556c888d81e1f94 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Tue, 10 May 2022 12:08:19 -0400 Subject: [PATCH 6/6] Add unit test to cachekey.go Signed-off-by: Jim Zhang --- pkg/fabsdk/provider/chpvdr/chprovider_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/fabsdk/provider/chpvdr/chprovider_test.go b/pkg/fabsdk/provider/chpvdr/chprovider_test.go index 79a4a2df29..6a7fa89314 100644 --- a/pkg/fabsdk/provider/chpvdr/chprovider_test.go +++ b/pkg/fabsdk/provider/chpvdr/chprovider_test.go @@ -86,7 +86,7 @@ func TestBasicValidChannel(t *testing.T) { assert.NotNil(t, channelConfig) assert.NotEmptyf(t, channelConfig.ID(), "Got empty channel ID from channel config") - eventService, err := channelService.EventService(client.WithBlockEvents(), deliverclient.WithChaincodeID("testChaincode")) + eventService, err := channelService.EventService(client.WithBlockEvents(), deliverclient.WithSeekType("from"), deliverclient.WithBlockNum(10), deliverclient.WithChaincodeID("testChaincode")) require.NoError(t, err) require.NotNil(t, eventService)