Skip to content

Commit d0fb82d

Browse files
authored
Merge pull request #1379 from hyperledger/backports
Correctly parse blockchain subscription name when delivering events
2 parents 43481d4 + 01f4f51 commit d0fb82d

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

internal/blockchain/common/common.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,24 @@ func buildBatchPin(ctx context.Context, event *blockchain.Event, params *BatchPi
245245
}
246246

247247
func GetNamespaceFromSubName(subName string) string {
248-
var parts = strings.Split(subName, "-")
249248
// Subscription names post version 1.1 are in the format `ff-sub-<namespace>-<listener ID>`
250-
if len(parts) != 4 {
251-
// Assume older subscription and return empty string
252-
return ""
249+
// Priot to that they had the format `ff-sub-<listener ID>`
250+
251+
// Strip the "ff-sub-" prefix from the beginning of the name
252+
withoutPrefix := strings.TrimPrefix(subName, "ff-sub-")
253+
if len(withoutPrefix) < len(subName) {
254+
// Strip the listener ID from the end of the name
255+
const UUIDLength = 36
256+
if len(withoutPrefix) > UUIDLength {
257+
uuidSplit := len(withoutPrefix) - UUIDLength - 1
258+
namespace := withoutPrefix[:uuidSplit]
259+
listenerID := withoutPrefix[uuidSplit:]
260+
if strings.HasPrefix(listenerID, "-") {
261+
return namespace
262+
}
263+
}
253264
}
254-
return parts[2]
265+
return ""
255266
}
256267

257268
func (s *subscriptions) AddSubscription(ctx context.Context, namespace *core.Namespace, version int, subID string, extra interface{}) {

internal/blockchain/common/common_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,15 @@ func TestBuildBatchPinErrors(t *testing.T) {
194194
}
195195

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

200+
ns = GetNamespaceFromSubName("ff-sub-03071072-079b-4047-b192-a07186fc9db8")
201+
assert.Equal(t, "", ns)
202+
203+
ns = GetNamespaceFromSubName("ff-sub-ns1-123")
204+
assert.Equal(t, "", ns)
205+
200206
ns = GetNamespaceFromSubName("BAD")
201207
assert.Equal(t, "", ns)
202208
}

internal/blockchain/ethereum/ethereum_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ func TestHandleMessageContractEventNoNamespaceHandlers(t *testing.T) {
21292129

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

21352135
e.SetHandler("ns2", em)

internal/blockchain/fabric/fabric_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ func TestHandleMessageContractEventNoNamespacedHandlers(t *testing.T) {
17711771

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

17771777
e.streams = newTestStreamManager(e.client, e.signer)

0 commit comments

Comments
 (0)