Skip to content

Commit

Permalink
Merge pull request #1379 from hyperledger/backports
Browse files Browse the repository at this point in the history
Correctly parse blockchain subscription name when delivering events
  • Loading branch information
nguyer committed Jul 27, 2023
2 parents 43481d4 + 01f4f51 commit d0fb82d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
21 changes: 16 additions & 5 deletions internal/blockchain/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,24 @@ func buildBatchPin(ctx context.Context, event *blockchain.Event, params *BatchPi
}

func GetNamespaceFromSubName(subName string) string {
var parts = strings.Split(subName, "-")
// Subscription names post version 1.1 are in the format `ff-sub-<namespace>-<listener ID>`
if len(parts) != 4 {
// Assume older subscription and return empty string
return ""
// Priot to that they had the format `ff-sub-<listener ID>`

// Strip the "ff-sub-" prefix from the beginning of the name
withoutPrefix := strings.TrimPrefix(subName, "ff-sub-")
if len(withoutPrefix) < len(subName) {
// Strip the listener ID from the end of the name
const UUIDLength = 36
if len(withoutPrefix) > UUIDLength {
uuidSplit := len(withoutPrefix) - UUIDLength - 1
namespace := withoutPrefix[:uuidSplit]
listenerID := withoutPrefix[uuidSplit:]
if strings.HasPrefix(listenerID, "-") {
return namespace
}
}
}
return parts[2]
return ""
}

func (s *subscriptions) AddSubscription(ctx context.Context, namespace *core.Namespace, version int, subID string, extra interface{}) {
Expand Down
8 changes: 7 additions & 1 deletion internal/blockchain/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,15 @@ func TestBuildBatchPinErrors(t *testing.T) {
}

func TestGetNamespaceFromSubName(t *testing.T) {
ns := GetNamespaceFromSubName("ff-sub-ns1-123")
ns := GetNamespaceFromSubName("ff-sub-ns1-03071072-079b-4047-b192-a07186fc9db8")
assert.Equal(t, "ns1", ns)

ns = GetNamespaceFromSubName("ff-sub-03071072-079b-4047-b192-a07186fc9db8")
assert.Equal(t, "", ns)

ns = GetNamespaceFromSubName("ff-sub-ns1-123")
assert.Equal(t, "", ns)

ns = GetNamespaceFromSubName("BAD")
assert.Equal(t, "", ns)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/blockchain/ethereum/ethereum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2129,7 +2129,7 @@ func TestHandleMessageContractEventNoNamespaceHandlers(t *testing.T) {

httpmock.RegisterResponder("GET", "http://localhost:12345/subscriptions/sub2",
httpmock.NewJsonResponderOrPanic(200, subscription{
ID: "sub2", Stream: "es12345", Name: "ff-sub-ns1-1132312312312",
ID: "sub2", Stream: "es12345", Name: "ff-sub-ns1-58113723-0cc3-411f-aa1b-948eca83b9cd",
}))

e.SetHandler("ns2", em)
Expand Down
2 changes: 1 addition & 1 deletion internal/blockchain/fabric/fabric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ func TestHandleMessageContractEventNoNamespacedHandlers(t *testing.T) {

httpmock.RegisterResponder("GET", "http://localhost:12345/subscriptions/sb-cb37cc07-e873-4f58-44ab-55add6bba320",
httpmock.NewJsonResponderOrPanic(200, subscription{
ID: "sb-cb37cc07-e873-4f58-44ab-55add6bba320", Stream: "es12345", Name: "ff-sub-ns1-11232312312",
ID: "sb-cb37cc07-e873-4f58-44ab-55add6bba320", Stream: "es12345", Name: "ff-sub-ns1-58113723-0cc3-411f-aa1b-948eca83b9cd",
}))

e.streams = newTestStreamManager(e.client, e.signer)
Expand Down

0 comments on commit d0fb82d

Please sign in to comment.