Skip to content

Commit

Permalink
[FAB-4023] SDK Go - apply CC interest after Connect
Browse files Browse the repository at this point in the history
Change-Id: I0900d2f0fa8a7263418bf83e67f879b82f1f8db5
Signed-off-by: Sandra Vrtikapa <[email protected]>
  • Loading branch information
sandrask committed May 29, 2017
1 parent b9f8928 commit 9204ed8
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
50 changes: 44 additions & 6 deletions fabric-client/events/eventhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type EventHub interface {
// The EventHubExt interface allows extensions of the SDK to add functionality to EventHub overloads.
type EventHubExt interface {
SetInterests(block bool)
AddChaincodeInterest(ChaincodeID string, EventName string)
}

// eventClientFactory creates an EventsClient instance
Expand Down Expand Up @@ -210,17 +209,48 @@ func (eventHub *eventHub) UnregisterBlockEvent(callback func(*common.Block)) {
}
}

// AddChaincodeInterest adds interest for specific CHAINCODE events.
func (eventHub *eventHub) AddChaincodeInterest(ChaincodeID string, EventName string) {
eventHub.interestedEvents = append(eventHub.interestedEvents, &pb.Interest{
// addChaincodeInterest adds interest for specific CHAINCODE events.
func (eventHub *eventHub) addChaincodeInterest(ChaincodeId string, EventName string) {
ccInterest := &pb.Interest{
EventType: pb.EventType_CHAINCODE,
RegInfo: &pb.Interest_ChaincodeRegInfo{
ChaincodeRegInfo: &pb.ChaincodeReg{
ChaincodeId: ChaincodeID,
ChaincodeId: ChaincodeId,
EventName: EventName,
},
},
})
}

eventHub.interestedEvents = append(eventHub.interestedEvents, ccInterest)

if eventHub.IsConnected() {
eventHub.client.RegisterAsync([]*pb.Interest{ccInterest})
}

}

// removeChaincodeInterest remove interest for specific CHAINCODE event
func (eventHub *eventHub) removeChaincodeInterest(ChaincodeId string, EventName string) {
ccInterest := &pb.Interest{
EventType: pb.EventType_CHAINCODE,
RegInfo: &pb.Interest_ChaincodeRegInfo{
ChaincodeRegInfo: &pb.ChaincodeReg{
ChaincodeId: ChaincodeId,
EventName: EventName,
},
},
}

for i, v := range eventHub.interestedEvents {
if v.EventType == ccInterest.EventType && *(v.GetChaincodeRegInfo()) == *(ccInterest.GetChaincodeRegInfo()) {
eventHub.interestedEvents = append(eventHub.interestedEvents[:i], eventHub.interestedEvents[i+1:]...)
}
}

if eventHub.IsConnected() {
eventHub.client.UnregisterAsync([]*pb.Interest{ccInterest})
}

}

// SetPeerAddr ...
Expand Down Expand Up @@ -267,6 +297,10 @@ func (eventHub *eventHub) Connect() error {
return fmt.Errorf("eventHub.peerAddr is empty")
}

if eventHub.interestedEvents == nil || len(eventHub.interestedEvents) == 0 {
return fmt.Errorf("You must register for at least one event before connecting")
}

if eventHub.client == nil {
eventsClient, _ := eventHub.eventsClientFactory.newEventsClient(eventHub.peerAddr, eventHub.peerTLSCertificate, eventHub.peerTLSServerHostOverride, 5, eventHub)
eventHub.client = eventsClient
Expand Down Expand Up @@ -346,6 +380,8 @@ func (eventHub *eventHub) RegisterChaincodeEvent(ccid string, eventname string,
eventHub.mtx.Lock()
defer eventHub.mtx.Unlock()

eventHub.addChaincodeInterest(ccid, eventname)

cbe := ChainCodeCBE{CCID: ccid, EventNameFilter: eventname, CallbackFunc: callback}
cbeArray := eventHub.chaincodeRegistrants[ccid]
if cbeArray == nil && len(cbeArray) <= 0 {
Expand All @@ -369,6 +405,8 @@ func (eventHub *eventHub) UnregisterChaincodeEvent(cbe *ChainCodeCBE) {
eventHub.mtx.Lock()
defer eventHub.mtx.Unlock()

eventHub.removeChaincodeInterest(cbe.CCID, cbe.EventNameFilter)

cbeArray := eventHub.chaincodeRegistrants[cbe.CCID]
if len(cbeArray) <= 0 {
logger.Debugf("No event registration for ccid %s \n", cbe.CCID)
Expand Down
46 changes: 46 additions & 0 deletions fabric-client/events/eventhub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,49 @@ func TestRegisterBlockEvent(t *testing.T) {
func testCallback(block *common.Block) {
fmt.Println("testCallback called on block")
}

func TestRegisterChaincodeEvent(t *testing.T) {
eventHub, _ := createMockedEventHub(t)
if t.Failed() {
return
}

// Interest in block event is registered by default
if len(eventHub.interestedEvents) != 1 {
t.Fatalf("Transaction callback should be registered by default")
}

cbe := eventHub.RegisterChaincodeEvent("testCC", "eventID", testChaincodeCallback)

if len(eventHub.interestedEvents) != 2 {
t.Fatalf("Failed to register interest for CC event")
}

interest := eventHub.interestedEvents[1]

if interest.EventType != pb.EventType_CHAINCODE {
t.Fatalf("Expecting chaincode event type, got (%v)", interest.EventType)
}

ccRegInfo := interest.GetChaincodeRegInfo()

if ccRegInfo.ChaincodeId != "testCC" {
t.Fatalf("Expecting chaincode id (%s), got (%s)", "testCC", ccRegInfo.ChaincodeId)
}

if ccRegInfo.EventName != "eventID" {
t.Fatalf("Expecting event id (%s), got (%s)", "eventID", ccRegInfo.EventName)
}

eventHub.UnregisterChaincodeEvent(cbe)

if len(eventHub.interestedEvents) != 1 {
t.Fatalf("Expecting one registered interest, got %d", len(eventHub.interestedEvents))
}

}

// private test callback to be executed on chaincode event
func testChaincodeCallback(ce *ChaincodeEvent) {
fmt.Printf("Received CC event: %v\n", ce)
}

0 comments on commit 9204ed8

Please sign in to comment.