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

Commit fe98dcb

Browse files
author
Karl Einholz
committed
Adding Extension interfaces to Chain and EventHub.
Also, EventHub had another exposed propbuf left. Change-Id: I141996ba22ea85af281cafedfbb5d53c074fc5a6 Signed-off-by: Karl Einholz <karl.einholz@securekey.com>
1 parent dacc9ca commit fe98dcb

File tree

2 files changed

+91
-21
lines changed

2 files changed

+91
-21
lines changed

fabric-client/chain.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ type Chain interface {
7676
SendTransaction(tx *Transaction) ([]*TransactionResponse, error)
7777
SendInstallProposal(chaincodeName string, chaincodePath string, chaincodeVersion string, chaincodePackage []byte, targets []Peer) ([]*TransactionProposalResponse, string, error)
7878
SendInstantiateProposal(chaincodeName string, chainID string, args []string, chaincodePath string, chaincodeVersion string, targets []Peer) ([]*TransactionProposalResponse, string, error)
79+
80+
QueryExtensionInterface() ChainExtension
81+
}
82+
83+
// The ChainExtension interface allows extensions of the SDK to add functionality to Chain overloads.
84+
type ChainExtension interface {
85+
GetClientContext() Client
86+
87+
SignPayload(payload []byte) (*SignedEnvelope, error)
88+
BroadcastEnvelope(envelope *SignedEnvelope) ([]*TransactionResponse, error)
89+
90+
// TODO: This should go somewhere else?
91+
GetProposalBytes(tp *TransactionProposal) ([]byte, error)
7992
}
8093

8194
type chain struct {
@@ -167,6 +180,20 @@ func NewChain(name string, client Client) (Chain, error) {
167180
return c, nil
168181
}
169182

183+
func (c *chain) QueryExtensionInterface() ChainExtension {
184+
return c
185+
}
186+
187+
// GetClientContext returns the Client that was passed in to NewChain
188+
func (c *chain) GetClientContext() Client {
189+
return c.clientContext
190+
}
191+
192+
// GetProposalBytes returns the serialized transaction.
193+
func (c *chain) GetProposalBytes(tp *TransactionProposal) ([]byte, error) {
194+
return proto.Marshal(tp.signedProposal)
195+
}
196+
170197
// GetName ...
171198
/**
172199
* Get the chain name.
@@ -358,7 +385,7 @@ func (c *chain) CreateChannel(request CreateChannelRequest) error {
358385
err.Error())
359386
}
360387
// Send request
361-
responseMap, err := c.broadcastEnvelope(&SignedEnvelope{
388+
responseMap, err := c.BroadcastEnvelope(&SignedEnvelope{
362389
signature: signedEnvelope.Signature,
363390
Payload: signedEnvelope.Payload,
364391
})
@@ -788,12 +815,12 @@ func (c *chain) SendTransaction(tx *Transaction) ([]*TransactionResponse, error)
788815
}
789816

790817
// here's the envelope
791-
envelope, err := c.signPayload(paylBytes)
818+
envelope, err := c.SignPayload(paylBytes)
792819
if err != nil {
793820
return nil, err
794821
}
795822

796-
transactionResponses, err := c.broadcastEnvelope(envelope)
823+
transactionResponses, err := c.BroadcastEnvelope(envelope)
797824
if err != nil {
798825
return nil, err
799826
}
@@ -944,7 +971,7 @@ func (c *chain) SendInstantiateProposal(chaincodeName string, chainID string,
944971
return transactionProposalResponse, txID, err
945972
}
946973

947-
func (c *chain) signPayload(payload []byte) (*SignedEnvelope, error) {
974+
func (c *chain) SignPayload(payload []byte) (*SignedEnvelope, error) {
948975
//Get user info
949976
user, err := c.clientContext.GetUserContext("")
950977
if err != nil {
@@ -961,7 +988,7 @@ func (c *chain) signPayload(payload []byte) (*SignedEnvelope, error) {
961988
}
962989

963990
//broadcastEnvelope will send the given envelope to each orderer
964-
func (c *chain) broadcastEnvelope(envelope *SignedEnvelope) ([]*TransactionResponse, error) {
991+
func (c *chain) BroadcastEnvelope(envelope *SignedEnvelope) ([]*TransactionResponse, error) {
965992
// Check if orderers are defined
966993
if c.orderers == nil || len(c.orderers) == 0 {
967994
return nil, fmt.Errorf("orderers not set")

fabric-client/events/eventhub.go

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@ type EventHub interface {
3939
SetPeerAddr(peerURL string, certificate string, serverHostOverride string)
4040
IsConnected() bool
4141
Connect() error
42-
RegisterChaincodeEvent(ccid string, eventname string, callback func(*pb.ChaincodeEvent)) *ChainCodeCBE
42+
RegisterChaincodeEvent(ccid string, eventname string, callback func(*ChaincodeEvent)) *ChainCodeCBE
4343
UnregisterChaincodeEvent(cbe *ChainCodeCBE)
4444
RegisterTxEvent(txID string, callback func(string, error))
4545
UnregisterTxEvent(txID string)
4646
}
4747

48+
// The EventHubExt interface allows extensions of the SDK to add functionality to EventHub overloads.
49+
type EventHubExt interface {
50+
SetInterests(block bool, rejection bool)
51+
AddChaincodeInterest(ChaincodeID string, EventName string)
52+
}
53+
4854
type eventHub struct {
4955
// Protects chaincodeRegistrants, blockRegistrants and txRegistrants
5056
mtx sync.RWMutex
@@ -68,6 +74,14 @@ type eventHub struct {
6874
interestedEvents []*pb.Interest
6975
}
7076

77+
// ChaincodeEvent contains the current event data for the event handler
78+
type ChaincodeEvent struct {
79+
ChaincodeId string
80+
TxId string
81+
EventName string
82+
Payload []byte
83+
}
84+
7185
// ChainCodeCBE ...
7286
/**
7387
* The ChainCodeCBE is used internal to the EventHub to hold chaincode
@@ -79,7 +93,7 @@ type ChainCodeCBE struct {
7993
// event name regex filter
8094
EventNameFilter string
8195
// callback function to invoke on successful filter match
82-
CallbackFunc func(*pb.ChaincodeEvent)
96+
CallbackFunc func(*ChaincodeEvent)
8397
}
8498

8599
// NewEventHub ...
@@ -88,15 +102,40 @@ func NewEventHub() EventHub {
88102
blockRegistrants := make([]func(*common.Block), 0)
89103
txRegistrants := make(map[string]func(string, error))
90104

105+
eventHub := &eventHub{chaincodeRegistrants: chaincodeRegistrants, blockRegistrants: blockRegistrants, txRegistrants: txRegistrants, interestedEvents: nil}
91106
// default interested events
92-
// TODO: set interestedEvents based on handler registration
93-
interestedEvents := []*pb.Interest{{EventType: pb.EventType_BLOCK}}
94-
95-
eventHub := &eventHub{chaincodeRegistrants: chaincodeRegistrants, blockRegistrants: blockRegistrants, txRegistrants: txRegistrants, interestedEvents: interestedEvents}
107+
eventHub.SetInterests(true, true)
96108

97109
return eventHub
98110
}
99111

112+
// SetInterests clears all interests and sets the interests for BLOCK and REJECTION type of events.
113+
func (eventHub *eventHub) SetInterests(block bool, rejection bool) {
114+
eventHub.mtx.Lock()
115+
defer eventHub.mtx.Unlock()
116+
117+
eventHub.interestedEvents = nil
118+
if block {
119+
eventHub.interestedEvents = append(eventHub.interestedEvents, &pb.Interest{EventType: pb.EventType_BLOCK})
120+
}
121+
if rejection {
122+
eventHub.interestedEvents = append(eventHub.interestedEvents, &pb.Interest{EventType: pb.EventType_REJECTION})
123+
}
124+
}
125+
126+
// AddChaincodeInterest adds interest for specific CHAINCODE events.
127+
func (eventHub *eventHub) AddChaincodeInterest(ChaincodeID string, EventName string) {
128+
eventHub.interestedEvents = append(eventHub.interestedEvents, &pb.Interest{
129+
EventType: pb.EventType_CHAINCODE,
130+
RegInfo: &pb.Interest_ChaincodeRegInfo{
131+
ChaincodeRegInfo: &pb.ChaincodeReg{
132+
ChaincodeId: ChaincodeID,
133+
EventName: EventName,
134+
},
135+
},
136+
})
137+
}
138+
100139
// SetPeerAddr ...
101140
/**
102141
* Set peer url for event source<p>
@@ -149,6 +188,11 @@ func (eventHub *eventHub) Connect() error {
149188
return nil
150189
}
151190

191+
//SetInterestedEvents set events that client is interested in
192+
func (eventHub *eventHub) SetInterestedEvents(events []*pb.Interest) {
193+
eventHub.interestedEvents = events
194+
}
195+
152196
//GetInterestedEvents implements consumer.EventAdapter interface for registering interested events
153197
func (eventHub *eventHub) GetInterestedEvents() ([]*pb.Interest, error) {
154198
return eventHub.interestedEvents, nil
@@ -180,7 +224,12 @@ func (eventHub *eventHub) Recv(msg *pb.Event) (bool, error) {
180224
if v.EventNameFilter == ccEvent.ChaincodeEvent.EventName {
181225
callback := v.CallbackFunc
182226
if callback != nil {
183-
callback(ccEvent.ChaincodeEvent)
227+
callback(&ChaincodeEvent{
228+
ChaincodeId: ccEvent.ChaincodeEvent.ChaincodeId,
229+
TxId: ccEvent.ChaincodeEvent.TxId,
230+
EventName: ccEvent.ChaincodeEvent.EventName,
231+
Payload: ccEvent.ChaincodeEvent.Payload,
232+
})
184233
}
185234
}
186235
}
@@ -217,14 +266,12 @@ func (eventHub *eventHub) Disconnected(err error) {
217266
* @returns {object} ChainCodeCBE object that should be treated as an opaque
218267
* handle used to unregister (see unregisterChaincodeEvent)
219268
*/
220-
func (eventHub *eventHub) RegisterChaincodeEvent(ccid string, eventname string, callback func(*pb.ChaincodeEvent)) *ChainCodeCBE {
221-
if !eventHub.connected {
222-
return nil
223-
}
224-
269+
func (eventHub *eventHub) RegisterChaincodeEvent(ccid string, eventname string, callback func(*ChaincodeEvent)) *ChainCodeCBE {
225270
eventHub.mtx.Lock()
226271
defer eventHub.mtx.Unlock()
227272

273+
eventHub.AddChaincodeInterest(ccid, eventname)
274+
228275
cbe := ChainCodeCBE{CCID: ccid, EventNameFilter: eventname, CallbackFunc: callback}
229276
cbeArray := eventHub.chaincodeRegistrants[ccid]
230277
if cbeArray == nil && len(cbeArray) <= 0 {
@@ -245,10 +292,6 @@ func (eventHub *eventHub) RegisterChaincodeEvent(ccid string, eventname string,
245292
* registerChaincodeEvent.
246293
*/
247294
func (eventHub *eventHub) UnregisterChaincodeEvent(cbe *ChainCodeCBE) {
248-
if !eventHub.connected {
249-
return
250-
}
251-
252295
eventHub.mtx.Lock()
253296
defer eventHub.mtx.Unlock()
254297

0 commit comments

Comments
 (0)