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

Commit b76434a

Browse files
committed
[ FAB-3432] ChaincodeEvent should contain channel ID
Change-Id: I06f3ef63c34bf0ad1c52196e7572dce1881ecf60 Signed-off-by: Bob Stasyszyn <bob.stasyszyn@securekey.com>
1 parent f550a1d commit b76434a

File tree

3 files changed

+282
-37
lines changed

3 files changed

+282
-37
lines changed

fabric-client/events/eventhub.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ type ChaincodeEvent struct {
9797
TxId string
9898
EventName string
9999
Payload []byte
100+
ChannelID string
100101
}
101102

102103
// ChainCodeCBE ...
@@ -294,10 +295,10 @@ func (eventHub *eventHub) Recv(msg *pb.Event) (bool, error) {
294295
}
295296

296297
for _, tdata := range blockEvent.Block.Data.Data {
297-
if ccEvent, err := getChainCodeEvent(tdata); err != nil {
298+
if ccEvent, channelID, err := getChainCodeEvent(tdata); err != nil {
298299
logger.Warningf("getChainCodeEvent return error: %v\n", err)
299300
} else if ccEvent != nil {
300-
eventHub.notifyChaincodeRegistrants(ccEvent, true)
301+
eventHub.notifyChaincodeRegistrants(channelID, ccEvent, true)
301302
}
302303
}
303304
return true, nil
@@ -306,7 +307,7 @@ func (eventHub *eventHub) Recv(msg *pb.Event) (bool, error) {
306307
logger.Debugf("Recv ccEvent:%v\n", ccEvent)
307308

308309
if ccEvent != nil {
309-
eventHub.notifyChaincodeRegistrants(ccEvent.ChaincodeEvent, false)
310+
eventHub.notifyChaincodeRegistrants("", ccEvent.ChaincodeEvent, false)
310311
}
311312
return true, nil
312313
default:
@@ -493,58 +494,60 @@ func (eventHub *eventHub) getTXRegistrant(txID string) func(string, error) {
493494
}
494495

495496
// getChainCodeEvents parses block events for chaincode events associated with individual transactions
496-
func getChainCodeEvent(tdata []byte) (*pb.ChaincodeEvent, error) {
497+
func getChainCodeEvent(tdata []byte) (event *pb.ChaincodeEvent, channelID string, err error) {
497498

498499
if tdata == nil {
499-
return nil, errors.New("Cannot extract payload from nil transaction")
500+
return nil, "", errors.New("Cannot extract payload from nil transaction")
500501
}
501502

502503
if env, err := utils.GetEnvelopeFromBlock(tdata); err != nil {
503-
return nil, fmt.Errorf("Error getting tx from block(%s)", err)
504+
return nil, "", fmt.Errorf("Error getting tx from block(%s)", err)
504505
} else if env != nil {
505506
// get the payload from the envelope
506507
payload, err := utils.GetPayload(env)
507508
if err != nil {
508-
return nil, fmt.Errorf("Could not extract payload from envelope, err %s", err)
509+
return nil, "", fmt.Errorf("Could not extract payload from envelope, err %s", err)
509510
}
510511

511512
channelHeaderBytes := payload.Header.ChannelHeader
512513
channelHeader := &common.ChannelHeader{}
513514
err = proto.Unmarshal(channelHeaderBytes, channelHeader)
514515
if err != nil {
515-
return nil, fmt.Errorf("Could not extract channel header from envelope, err %s", err)
516+
return nil, "", fmt.Errorf("Could not extract channel header from envelope, err %s", err)
516517
}
517518

519+
channelID := channelHeader.ChannelId
520+
518521
// Chaincode events apply to endorser transaction only
519522
if common.HeaderType(channelHeader.Type) == common.HeaderType_ENDORSER_TRANSACTION {
520523
tx, err := utils.GetTransaction(payload.Data)
521524
if err != nil {
522-
return nil, fmt.Errorf("Error unmarshalling transaction payload for block event: %s", err)
525+
return nil, "", fmt.Errorf("Error unmarshalling transaction payload for block event: %s", err)
523526
}
524527
chaincodeActionPayload, err := utils.GetChaincodeActionPayload(tx.Actions[0].Payload)
525528
if err != nil {
526-
return nil, fmt.Errorf("Error unmarshalling transaction action payload for block event: %s", err)
529+
return nil, "", fmt.Errorf("Error unmarshalling transaction action payload for block event: %s", err)
527530
}
528531
propRespPayload, err := utils.GetProposalResponsePayload(chaincodeActionPayload.Action.ProposalResponsePayload)
529532
if err != nil {
530-
return nil, fmt.Errorf("Error unmarshalling proposal response payload for block event: %s", err)
533+
return nil, "", fmt.Errorf("Error unmarshalling proposal response payload for block event: %s", err)
531534
}
532535
caPayload, err := utils.GetChaincodeAction(propRespPayload.Extension)
533536
if err != nil {
534-
return nil, fmt.Errorf("Error unmarshalling chaincode action for block event: %s", err)
537+
return nil, "", fmt.Errorf("Error unmarshalling chaincode action for block event: %s", err)
535538
}
536539
ccEvent, err := utils.GetChaincodeEvents(caPayload.Events)
537540

538541
if ccEvent != nil {
539-
return ccEvent, nil
542+
return ccEvent, channelID, nil
540543
}
541544
}
542545
}
543-
return nil, nil
546+
return nil, "", nil
544547
}
545548

546549
// Utility function to fire callbacks for chaincode registrants
547-
func (eventHub *eventHub) notifyChaincodeRegistrants(ccEvent *pb.ChaincodeEvent, patternMatch bool) {
550+
func (eventHub *eventHub) notifyChaincodeRegistrants(channelID string, ccEvent *pb.ChaincodeEvent, patternMatch bool) {
548551

549552
cbeArray := eventHub.getChaincodeRegistrants(ccEvent.ChaincodeId)
550553
if len(cbeArray) <= 0 {
@@ -564,6 +567,7 @@ func (eventHub *eventHub) notifyChaincodeRegistrants(ccEvent *pb.ChaincodeEvent,
564567
TxId: ccEvent.TxId,
565568
EventName: ccEvent.EventName,
566569
Payload: ccEvent.Payload,
570+
ChannelID: channelID,
567571
})
568572
}
569573
}

fabric-client/events/eventhub_test.go

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import (
3434
)
3535

3636
func TestDeadlock(t *testing.T) {
37+
channelID := "testchannel"
38+
ccID := "testccid"
39+
3740
eventHub, clientFactory := createMockedEventHub(t)
3841
if t.Failed() {
3942
return
@@ -65,7 +68,10 @@ func TestDeadlock(t *testing.T) {
6568
})
6669

6770
go client.MockEvent(&pb.Event{
68-
Event: buildMockTxEvent(transactionID),
71+
Event: (&MockTxEventBuilder{
72+
TxID: transactionID,
73+
ChannelID: channelID,
74+
}).Build(),
6975
})
7076

7177
// Wait for the TX event and then unregister
@@ -78,13 +84,16 @@ func TestDeadlock(t *testing.T) {
7884
go flood(eventsPerThread, threads, func() {
7985
eventName := generateTxID()
8086
received := newCompletionHandler(timeout)
81-
registration := eventHub.RegisterChaincodeEvent("testccid", eventName, func(event *ChaincodeEvent) {
87+
registration := eventHub.RegisterChaincodeEvent(ccID, eventName, func(event *ChaincodeEvent) {
8288
ccCompletion.done()
8389
received.done()
8490
})
8591

8692
go client.MockEvent(&pb.Event{
87-
Event: buildMockCCEvent("testccid", eventName),
93+
Event: (&MockCCEventBuilder{
94+
CCID: ccID,
95+
EventName: eventName,
96+
}).Build(),
8897
})
8998

9099
// Wait for the CC event and then unregister
@@ -109,6 +118,112 @@ func TestDeadlock(t *testing.T) {
109118
}
110119
}
111120

121+
func TestChaincodeEvent(t *testing.T) {
122+
ccID := "someccid"
123+
eventName := "someevent"
124+
125+
eventHub, clientFactory := createMockedEventHub(t)
126+
if t.Failed() {
127+
return
128+
}
129+
130+
fmt.Printf("EventHub Chaincode event test\n")
131+
132+
client := clientFactory.clients[0]
133+
if client == nil {
134+
t.Fatalf("No client")
135+
}
136+
137+
eventReceived := make(chan *ChaincodeEvent)
138+
139+
// Register for CC event
140+
registration := eventHub.RegisterChaincodeEvent(ccID, eventName, func(event *ChaincodeEvent) {
141+
eventReceived <- event
142+
})
143+
144+
// Publish CC event
145+
go client.MockEvent(&pb.Event{
146+
Event: (&MockCCEventBuilder{
147+
CCID: ccID,
148+
EventName: eventName,
149+
}).Build(),
150+
})
151+
152+
// Wait for the CC event
153+
var event *ChaincodeEvent
154+
select {
155+
case event = <-eventReceived:
156+
eventHub.UnregisterChaincodeEvent(registration)
157+
case <-time.After(time.Second * 5):
158+
t.Fatalf("Timed out waiting for CC event")
159+
}
160+
161+
// Check CC event
162+
if event.ChaincodeId != ccID {
163+
t.Fatalf("Expecting chaincode ID [%s] but got [%s]", ccID, event.ChaincodeId)
164+
}
165+
if event.EventName != eventName {
166+
t.Fatalf("Expecting event name [%s] but got [%s]", eventName, event.EventName)
167+
}
168+
}
169+
170+
func TestChaincodeBlockEvent(t *testing.T) {
171+
channelID := "somechannelid"
172+
ccID := "someccid"
173+
eventName := "someevent"
174+
txID := generateTxID()
175+
176+
eventHub, clientFactory := createMockedEventHub(t)
177+
if t.Failed() {
178+
return
179+
}
180+
181+
client := clientFactory.clients[0]
182+
if client == nil {
183+
t.Fatalf("No client")
184+
}
185+
186+
eventReceived := make(chan *ChaincodeEvent)
187+
188+
// Register for CC event
189+
registration := eventHub.RegisterChaincodeEvent(ccID, eventName, func(event *ChaincodeEvent) {
190+
eventReceived <- event
191+
})
192+
193+
// Publish CC event
194+
go client.MockEvent(&pb.Event{
195+
Event: (&MockCCBlockEventBuilder{
196+
CCID: ccID,
197+
EventName: eventName,
198+
ChannelID: channelID,
199+
TxID: txID,
200+
}).Build(),
201+
})
202+
203+
// Wait for CC event
204+
var event *ChaincodeEvent
205+
select {
206+
case event = <-eventReceived:
207+
eventHub.UnregisterChaincodeEvent(registration)
208+
case <-time.After(time.Second * 5):
209+
t.Fatalf("Timed out waiting for CC event")
210+
}
211+
212+
// Check CC event
213+
if event.ChannelID != channelID {
214+
t.Fatalf("Expecting channel ID [%s] but got [%s]", channelID, event.ChannelID)
215+
}
216+
if event.ChaincodeId != ccID {
217+
t.Fatalf("Expecting chaincode ID [%s] but got [%s]", ccID, event.ChaincodeId)
218+
}
219+
if event.EventName != eventName {
220+
t.Fatalf("Expecting event name [%s] but got [%s]", eventName, event.EventName)
221+
}
222+
if event.TxId == "" {
223+
t.Fatalf("Expecting TxID [%s] but got [%s]", txID, event.TxId)
224+
}
225+
}
226+
112227
// completionHandler waits for a single event with a timeout
113228
type completionHandler struct {
114229
completed chan bool

0 commit comments

Comments
 (0)