Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit e59c782

Browse files
committed
Add chaincode ID to the cache key calculation
1 parent 9eb2c43 commit e59c782

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

ci.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
#
66

77
GO_MIN_VER=1.14.0
8-
GO_MAX_VER=1.16.99
8+
GO_MAX_VER=1.17.99

pkg/client/event/event.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Client struct {
3333
permitBlockEvents bool
3434
fromBlock uint64
3535
seekType seek.Type
36+
chaincodeId string
3637
eventConsumerTimeout *time.Duration
3738
}
3839

@@ -68,6 +69,9 @@ func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client
6869
opts = append(opts, deliverclient.WithBlockNum(eventClient.fromBlock))
6970
}
7071
}
72+
if eventClient.chaincodeId != "" {
73+
opts = append(opts, deliverclient.WithChaincodeId(eventClient.chaincodeId))
74+
}
7175
if eventClient.eventConsumerTimeout != nil {
7276
opts = append(opts, dispatcher.WithEventConsumerTimeout(*eventClient.eventConsumerTimeout))
7377
}

pkg/client/event/example_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ func ExampleClient_RegisterChaincodeEvent() {
7777

7878
}
7979

80+
func ExampleClient_RegisterChaincodeEvent_NewService() {
81+
82+
ec, err := New(mockChannelProvider("mychannel"), WithChaincodeId("examplecc"))
83+
if err != nil {
84+
fmt.Println("failed to create client")
85+
}
86+
87+
registration, _, err := ec.RegisterChaincodeEvent("examplecc", "event123")
88+
if err != nil {
89+
fmt.Println("failed to register chaincode event")
90+
}
91+
defer ec.Unregister(registration)
92+
93+
fmt.Println("chaincode event registered successfully")
94+
95+
// Output: chaincode event registered successfully
96+
97+
}
98+
8099
func ExampleClient_RegisterChaincodeEvent_withPayload() {
81100

82101
// If you require payload for chaincode events you have to use WithBlockEvents() option

pkg/client/event/opts.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ func WithSeekType(seek seek.Type) ClientOption {
4242
}
4343
}
4444

45+
// WithChaincodeId indicates the target chaincode
46+
// Only deliverclient supports this
47+
func WithChaincodeId(id string) ClientOption {
48+
return func(c *Client) error {
49+
c.chaincodeId = id
50+
return nil
51+
}
52+
}
53+
4554
// WithEventConsumerTimeout is the timeout when sending events to a registered consumer.
4655
// If < 0, if buffer full, unblocks immediately and does not send.
4756
// If 0, if buffer full, will block and guarantee the event will be sent out.

pkg/fab/events/deliverclient/opts.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type params struct {
1919
connProvider api.ConnectionProvider
2020
seekType seek.Type
2121
fromBlock uint64
22+
chaincodeId string
2223
respTimeout time.Duration
2324
}
2425

@@ -48,6 +49,15 @@ func WithBlockNum(value uint64) options.Opt {
4849
}
4950
}
5051

52+
// WithChaincodeId specifies the chaincode from which events are to be received.
53+
func WithChaincodeId(value string) options.Opt {
54+
return func(p options.Params) {
55+
if setter, ok := p.(chaincodeIdSetter); ok {
56+
setter.SetChaincodeId(value)
57+
}
58+
}
59+
}
60+
5161
type seekTypeSetter interface {
5262
SetSeekType(value seek.Type)
5363
}
@@ -56,6 +66,10 @@ type fromBlockSetter interface {
5666
SetFromBlock(value uint64)
5767
}
5868

69+
type chaincodeIdSetter interface {
70+
SetChaincodeId(value string)
71+
}
72+
5973
func (p *params) PermitBlockEvents() {
6074
logger.Debug("PermitBlockEvents")
6175
p.connProvider = deliverProvider
@@ -79,6 +93,11 @@ func (p *params) SetSeekType(value seek.Type) {
7993
}
8094
}
8195

96+
func (p *params) SetChaincodeId(value string) {
97+
logger.Debugf("ChaincodId: %d", value)
98+
p.chaincodeId = value
99+
}
100+
82101
func (p *params) SetResponseTimeout(value time.Duration) {
83102
logger.Debugf("ResponseTimeout: %s", value)
84103
p.respTimeout = value

pkg/fabsdk/provider/chpvdr/cachekey.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type params struct {
102102
permitBlockEvents bool
103103
seekType seek.Type
104104
fromBlock uint64
105+
chaincodeId string
105106
}
106107

107108
func defaultParams() *params {
@@ -122,8 +123,14 @@ func (p *params) SetSeekType(value seek.Type) {
122123
}
123124
}
124125

126+
func (p *params) SetChaincodeId(value string) {
127+
if value != "" {
128+
p.chaincodeId = value
129+
}
130+
}
131+
125132
func (p *params) getOptKey() string {
126133
// Construct opts portion
127-
optKey := fmt.Sprintf("blockEvents:%t,seekType:%s,fromBlock:%d", p.permitBlockEvents, p.seekType, p.fromBlock)
134+
optKey := fmt.Sprintf("blockEvents:%t,seekType:%s,fromBlock:%d,chaincodeId:%s", p.permitBlockEvents, p.seekType, p.fromBlock, p.chaincodeId)
128135
return optKey
129136
}

0 commit comments

Comments
 (0)