Skip to content

Commit dbb5e10

Browse files
committed
Match restored streams to verified channel head
1 parent f25c81d commit dbb5e10

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

sdn-server/internal/api/channels.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,9 @@ func (h *ChannelHandler) latestVerifiedDatasetShardPublication(schemaName string
11851185
}
11861186
return publication, true
11871187
}
1188+
if channelHead != "" {
1189+
return storage.DatasetShardPublication{}, false
1190+
}
11881191
return publications[len(publications)-1], true
11891192
}
11901193

sdn-server/internal/api/channels_test.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,65 @@ func TestChannelHandlerStreamsVerifiedDatasetPublicationShardFromDurableLedger(t
393393
t.Parallel()
394394

395395
store := newChannelTestStore(t)
396+
oldStreamBytes := bytes.Join([][]byte{
397+
nativeSizePrefixedFlatBufferFrame("$OMM", []byte{9, 9}),
398+
}, nil)
399+
oldShardHashBytes := sha256.Sum256(oldStreamBytes)
400+
oldShardHash := hex.EncodeToString(oldShardHashBytes[:])
401+
oldPublication := storage.DatasetShardPublication{
402+
SchemaName: "OMM.fbs",
403+
ProviderID: "space-data-network-02",
404+
SourceName: "celestrak-gp",
405+
BatchID: "batch-stream-old",
406+
QueryProfile: storage.DatasetPublicationQueryProfile,
407+
Offset: 0,
408+
Limit: 50000,
409+
RecordCount: 1,
410+
ByteCount: int64(len(oldStreamBytes)),
411+
ShardCID: "bafkstreamshard-old",
412+
IndexCID: "bafkstreamindex-old",
413+
ManifestCID: "bafkstreammanifest-old",
414+
PNMCID: "bafkstreampnm-old",
415+
ShardSHA256: oldShardHash,
416+
IndexSHA256: strings.Repeat("4", 64),
417+
QuerySHA256: strings.Repeat("3", 64),
418+
ResultSHA256: oldShardHash,
419+
PublishedAt: time.Unix(1_779_999_800, 0).UTC(),
420+
}
421+
if err := store.UpsertDatasetShardPublication(oldPublication); err != nil {
422+
t.Fatalf("UpsertDatasetShardPublication old failed: %v", err)
423+
}
424+
var (
425+
found bool
426+
err error
427+
)
428+
oldPublication, found, err = store.FindDatasetShardPublication(storage.DatasetShardPublicationQuery{
429+
SchemaName: oldPublication.SchemaName,
430+
ProviderID: oldPublication.ProviderID,
431+
SourceName: oldPublication.SourceName,
432+
BatchID: oldPublication.BatchID,
433+
QueryProfile: oldPublication.QueryProfile,
434+
Offset: oldPublication.Offset,
435+
Limit: oldPublication.Limit,
436+
RecordCount: oldPublication.RecordCount,
437+
})
438+
if err != nil {
439+
t.Fatalf("FindDatasetShardPublication old failed: %v", err)
440+
}
441+
if !found {
442+
t.Fatal("FindDatasetShardPublication old did not find stored publication")
443+
}
444+
oldShardPath, err := store.DatasetPublicationShardPath(oldPublication)
445+
if err != nil {
446+
t.Fatalf("DatasetPublicationShardPath old failed: %v", err)
447+
}
448+
if err := os.MkdirAll(filepath.Dir(oldShardPath), 0o755); err != nil {
449+
t.Fatalf("create old shard dir failed: %v", err)
450+
}
451+
if err := os.WriteFile(oldShardPath, oldStreamBytes, 0o644); err != nil {
452+
t.Fatalf("write old shard failed: %v", err)
453+
}
454+
396455
streamBytes := bytes.Join([][]byte{
397456
nativeSizePrefixedFlatBufferFrame("$OMM", []byte{1, 2, 3}),
398457
nativeSizePrefixedFlatBufferFrame("$OMM", []byte{4, 5, 6, 7}),
@@ -422,6 +481,22 @@ func TestChannelHandlerStreamsVerifiedDatasetPublicationShardFromDurableLedger(t
422481
if err := store.UpsertDatasetShardPublication(publication); err != nil {
423482
t.Fatalf("UpsertDatasetShardPublication failed: %v", err)
424483
}
484+
publication, found, err = store.FindDatasetShardPublication(storage.DatasetShardPublicationQuery{
485+
SchemaName: publication.SchemaName,
486+
ProviderID: publication.ProviderID,
487+
SourceName: publication.SourceName,
488+
BatchID: publication.BatchID,
489+
QueryProfile: publication.QueryProfile,
490+
Offset: publication.Offset,
491+
Limit: publication.Limit,
492+
RecordCount: publication.RecordCount,
493+
})
494+
if err != nil {
495+
t.Fatalf("FindDatasetShardPublication failed: %v", err)
496+
}
497+
if !found {
498+
t.Fatal("FindDatasetShardPublication did not find stored publication")
499+
}
425500
shardPath, err := store.DatasetPublicationShardPath(publication)
426501
if err != nil {
427502
t.Fatalf("DatasetPublicationShardPath failed: %v", err)
@@ -432,6 +507,45 @@ func TestChannelHandlerStreamsVerifiedDatasetPublicationShardFromDurableLedger(t
432507
if err := os.WriteFile(shardPath, streamBytes, 0o644); err != nil {
433508
t.Fatalf("write shard failed: %v", err)
434509
}
510+
for _, entry := range []storage.PinLedgerEntry{
511+
{
512+
CID: oldPublication.ShardCID,
513+
Role: "shard",
514+
RowCount: int64(oldPublication.RecordCount),
515+
ByteCount: oldPublication.ByteCount,
516+
Head: oldPublication.FeedHead,
517+
ByteHash: "sha256:" + oldShardHash,
518+
VerificationState: "verified",
519+
},
520+
{
521+
CID: oldPublication.ManifestCID,
522+
Role: "manifest",
523+
ByteCount: 512,
524+
Head: oldPublication.FeedHead,
525+
ByteHash: "sha256:manifest-old",
526+
VerificationState: "verified",
527+
},
528+
{
529+
CID: oldPublication.PNMCID,
530+
Role: "pnm",
531+
ByteCount: 256,
532+
Head: oldPublication.FeedHead,
533+
ByteHash: "sha256:pnm-old",
534+
VerificationState: "verified",
535+
},
536+
} {
537+
entry.SchemaName = oldPublication.SchemaName
538+
entry.ProviderPeerID = "16Uiu2HCelesTrakProvider"
539+
entry.ProviderPublicKey = "provider-public-key"
540+
entry.ProviderID = oldPublication.ProviderID
541+
entry.SourceName = oldPublication.SourceName
542+
entry.BatchID = oldPublication.BatchID
543+
entry.QueryProfile = oldPublication.QueryProfile
544+
entry.VerifiedAt = oldPublication.PublishedAt
545+
if err := store.UpsertPinLedgerEntry(entry); err != nil {
546+
t.Fatalf("UpsertPinLedgerEntry old %s failed: %v", entry.Role, err)
547+
}
548+
}
435549
for _, entry := range []storage.PinLedgerEntry{
436550
{
437551
CID: publication.ShardCID,
@@ -489,6 +603,47 @@ func TestChannelHandlerStreamsVerifiedDatasetPublicationShardFromDurableLedger(t
489603
}
490604
}
491605

606+
func TestChannelHandlerLatestVerifiedDatasetShardPublicationRequiresMatchingHead(t *testing.T) {
607+
t.Parallel()
608+
609+
store := newChannelTestStore(t)
610+
publication := storage.DatasetShardPublication{
611+
SchemaName: "OMM.fbs",
612+
ProviderID: "space-data-network-02",
613+
SourceName: "celestrak-gp",
614+
BatchID: "batch-old",
615+
QueryProfile: storage.DatasetPublicationQueryProfile,
616+
Offset: 0,
617+
Limit: 50000,
618+
RecordCount: 1,
619+
ByteCount: 128,
620+
ShardCID: "bafkoldshard",
621+
IndexCID: "bafkoldindex",
622+
ManifestCID: "bafkoldmanifest",
623+
PNMCID: "bafkoldpnm",
624+
ShardSHA256: strings.Repeat("1", 64),
625+
IndexSHA256: strings.Repeat("2", 64),
626+
QuerySHA256: strings.Repeat("3", 64),
627+
ResultSHA256: strings.Repeat("1", 64),
628+
PublishedAt: time.Unix(1_779_999_700, 0).UTC(),
629+
}
630+
if err := store.UpsertDatasetShardPublication(publication); err != nil {
631+
t.Fatalf("UpsertDatasetShardPublication failed: %v", err)
632+
}
633+
634+
handler := NewChannelHandler(store)
635+
_, ok := handler.latestVerifiedDatasetShardPublication("OMM.fbs", storage.LocalReplicaStats{
636+
ProviderID: publication.ProviderID,
637+
SourceName: publication.SourceName,
638+
BatchID: publication.BatchID,
639+
QueryProfile: publication.QueryProfile,
640+
PinnedRows: int64(publication.RecordCount),
641+
}, "newer-channel-head")
642+
if ok {
643+
t.Fatal("mismatched channel head returned a fallback publication")
644+
}
645+
}
646+
492647
func TestChannelHandlerIssuesPrivateGrantAndAuthorizesBoundaries(t *testing.T) {
493648
t.Parallel()
494649

0 commit comments

Comments
 (0)