Skip to content

Commit

Permalink
FabConnect does not / can not set transactionIndex and eventIndex
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <[email protected]>
  • Loading branch information
peterbroadhurst authored and nguyer committed Jul 12, 2023
1 parent eda9463 commit c27557e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
11 changes: 8 additions & 3 deletions internal/blockchain/fabric/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,15 @@ func (f *Fabric) parseBlockchainEvent(ctx context.Context, msgJSON fftypes.JSONO
return nil // move on
}

// Fabric events are dispatched by the underlying fabric client to FabConnect with just the block number
// and the transaction hash. The index of the transaction in the block, or the index of the action within
// the transaction are not available. So we cannot generate an alphanumerically sortable string
// into the protocol ID. Instead we can only do this (which is according to Fabric rules assured to be
// unique, as Fabric only allows one event per transaction):
sTransactionHash := msgJSON.GetString("transactionId")
blockNumber := msgJSON.GetInt64("blockNumber")
transactionIndex := msgJSON.GetInt64("transactionIndex")
eventIndex := msgJSON.GetInt64("eventIndex")
protocolID := fmt.Sprintf("%.12d/%s", blockNumber, sTransactionHash)

name := msgJSON.GetString("eventName")
timestamp := msgJSON.GetInt64("timestamp")
chaincode := msgJSON.GetString("chaincodeId")
Expand All @@ -317,7 +322,7 @@ func (f *Fabric) parseBlockchainEvent(ctx context.Context, msgJSON fftypes.JSONO
BlockchainTXID: sTransactionHash,
Source: f.Name(),
Name: name,
ProtocolID: fmt.Sprintf("%.12d/%.6d/%.6d", blockNumber, transactionIndex, eventIndex),
ProtocolID: protocolID,
Output: *payload,
Info: msgJSON,
Timestamp: fftypes.UnixTime(timestamp),
Expand Down
44 changes: 13 additions & 31 deletions internal/blockchain/fabric/fabric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1620,8 +1620,6 @@ func TestHandleMessageContractEventOldSubscription(t *testing.T) {
"chaincodeId": "basic",
"blockNumber": 10,
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d",
"transactionIndex": 20,
"eventIndex": 30,
"eventName": "AssetCreated",
"payload": "eyJBcHByYWlzZWRWYWx1ZSI6MTAsIkNvbG9yIjoicmVkIiwiSUQiOiIxMjM0IiwiT3duZXIiOiJtZSIsIlNpemUiOjN9",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320"
Expand All @@ -1645,7 +1643,7 @@ func TestHandleMessageContractEventOldSubscription(t *testing.T) {

em.On("BlockchainEvent", mock.MatchedBy(func(e *blockchain.EventWithSubscription) bool {
assert.Equal(t, "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d", e.BlockchainTXID)
assert.Equal(t, "000000000010/000020/000030", e.Event.ProtocolID)
assert.Contains(t, []string{"000000000010/4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d", "000000000010/4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746f"}, e.Event.ProtocolID)
return true
})).Return(nil)

Expand All @@ -1669,13 +1667,11 @@ func TestHandleMessageContractEventOldSubscription(t *testing.T) {
assert.Equal(t, outputs, ev.Event.Output)

info := fftypes.JSONObject{
"blockNumber": float64(10),
"chaincodeId": "basic",
"eventName": "AssetCreated",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320",
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d",
"transactionIndex": float64(20),
"eventIndex": float64(30),
"blockNumber": float64(10),
"chaincodeId": "basic",
"eventName": "AssetCreated",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320",
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d",
}
assert.Equal(t, info, ev.Event.Info)

Expand All @@ -1689,8 +1685,6 @@ func TestHandleMessageContractEventNamespacedHandlers(t *testing.T) {
"chaincodeId": "basic",
"blockNumber": 10,
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d",
"transactionIndex": 20,
"eventIndex": 30,
"eventName": "AssetCreated",
"payload": "eyJBcHByYWlzZWRWYWx1ZSI6MTAsIkNvbG9yIjoicmVkIiwiSUQiOiIxMjM0IiwiT3duZXIiOiJtZSIsIlNpemUiOjN9",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320"
Expand All @@ -1699,8 +1693,6 @@ func TestHandleMessageContractEventNamespacedHandlers(t *testing.T) {
"chaincodeId": "basic",
"blockNumber": 10,
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746f",
"transactionIndex": 20,
"eventIndex": 30,
"eventName": "AssetCreated",
"payload": "eyJBcHByYWlzZWRWYWx1ZSI6MTAsIkNvbG9yIjoicmVkIiwiSUQiOiIxMjM0IiwiT3duZXIiOiJtZSIsIlNpemUiOjN9",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320"
Expand All @@ -1723,7 +1715,7 @@ func TestHandleMessageContractEventNamespacedHandlers(t *testing.T) {
e.SetHandler("ns1", em)

em.On("BlockchainEvent", mock.MatchedBy(func(e *blockchain.EventWithSubscription) bool {
assert.Equal(t, "000000000010/000020/000030", e.Event.ProtocolID)
assert.Contains(t, []string{"000000000010/4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d", "000000000010/4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746f"}, e.Event.ProtocolID)
return true
})).Return(nil)

Expand All @@ -1747,13 +1739,11 @@ func TestHandleMessageContractEventNamespacedHandlers(t *testing.T) {
assert.Equal(t, outputs, ev.Event.Output)

info := fftypes.JSONObject{
"blockNumber": float64(10),
"chaincodeId": "basic",
"eventName": "AssetCreated",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320",
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d",
"transactionIndex": float64(20),
"eventIndex": float64(30),
"blockNumber": float64(10),
"chaincodeId": "basic",
"eventName": "AssetCreated",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320",
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d",
}
assert.Equal(t, info, ev.Event.Info)

Expand All @@ -1767,8 +1757,6 @@ func TestHandleMessageContractEventNoNamespacedHandlers(t *testing.T) {
"chaincodeId": "basic",
"blockNumber": 10,
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d",
"transactionIndex": 20,
"eventIndex": 30,
"eventName": "AssetCreated",
"payload": "eyJBcHByYWlzZWRWYWx1ZSI6MTAsIkNvbG9yIjoicmVkIiwiSUQiOiIxMjM0IiwiT3duZXIiOiJtZSIsIlNpemUiOjN9",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320"
Expand All @@ -1792,7 +1780,7 @@ func TestHandleMessageContractEventNoNamespacedHandlers(t *testing.T) {

em.On("BlockchainEvent", mock.MatchedBy(func(e *blockchain.EventWithSubscription) bool {
assert.Equal(t, "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d", e.BlockchainTXID)
assert.Equal(t, "000000000010/000020/000030", e.Event.ProtocolID)
assert.Equal(t, "000000000010/4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d", e.Event.ProtocolID)
return true
})).Return(nil)

Expand All @@ -1811,8 +1799,6 @@ func TestHandleMessageContractEventNoPayload(t *testing.T) {
"chaincodeId": "basic",
"blockNumber": 10,
"transactionId": "4763a0c50e3bba7cef1a7ba35dd3f9f3426bb04d0156f326e84ec99387c4746d",
"transactionIndex": 20,
"eventIndex": 30,
"eventName": "AssetCreated",
"subId": "sb-cb37cc07-e873-4f58-44ab-55add6bba320"
}
Expand Down Expand Up @@ -2367,8 +2353,6 @@ func TestHandleNetworkAction(t *testing.T) {
"chaincodeId": "firefly",
"blockNumber": 91,
"transactionId": "ce79343000e851a0c742f63a733ce19a5f8b9ce1c719b6cecd14f01bcf81fff2",
"transactionIndex": 2,
"eventIndex": 50,
"eventName": "BatchPin",
"payload": "eyJzaWduZXIiOiJ1MHZnd3U5czAwLXg1MDk6OkNOPXVzZXIyLE9VPWNsaWVudDo6Q049ZmFicmljLWNhLXNlcnZlciIsInRpbWVzdGFtcCI6eyJzZWNvbmRzIjoxNjMwMDMxNjY3LCJuYW5vcyI6NzkxNDk5MDAwfSwibmFtZXNwYWNlIjoiZmlyZWZseTp0ZXJtaW5hdGUiLCJ1dWlkcyI6IjB4MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsImJhdGNoSGFzaCI6IjB4MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsInBheWxvYWRSZWYiOiIiLCJjb250ZXh0cyI6W119",
"subId": "sb-0910f6a8-7bd6-4ced-453e-2db68149ce8e"
Expand Down Expand Up @@ -2411,8 +2395,6 @@ func TestHandleNetworkActionFail(t *testing.T) {
"chaincodeId": "firefly",
"blockNumber": 91,
"transactionId": "ce79343000e851a0c742f63a733ce19a5f8b9ce1c719b6cecd14f01bcf81fff2",
"transactionIndex": 2,
"eventIndex": 50,
"eventName": "BatchPin",
"payload": "eyJzaWduZXIiOiJ1MHZnd3U5czAwLXg1MDk6OkNOPXVzZXIyLE9VPWNsaWVudDo6Q049ZmFicmljLWNhLXNlcnZlciIsInRpbWVzdGFtcCI6eyJzZWNvbmRzIjoxNjMwMDMxNjY3LCJuYW5vcyI6NzkxNDk5MDAwfSwibmFtZXNwYWNlIjoiZmlyZWZseTp0ZXJtaW5hdGUiLCJ1dWlkcyI6IjB4MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsImJhdGNoSGFzaCI6IjB4MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsInBheWxvYWRSZWYiOiIiLCJjb250ZXh0cyI6W119",
"subId": "sb-0910f6a8-7bd6-4ced-453e-2db68149ce8e"
Expand Down

0 comments on commit c27557e

Please sign in to comment.