Skip to content

Commit c9848ed

Browse files
authored
Fix missing delete blocks (bluesky-social#3033)
* send relevant blocks on commit * use relevantBlocks in pds * test * fix compiler errors * send both new & relevant blocks * build branch * changsets * no build branch
1 parent 588baae commit c9848ed

File tree

12 files changed

+147
-14
lines changed

12 files changed

+147
-14
lines changed

.changeset/lovely-ghosts-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@atproto/pds": patch
3+
---
4+
5+
Ensure we emit all relevant proof blocks for commit ops

.changeset/twenty-worms-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@atproto/repo": minor
3+
---
4+
5+
Add relevant proof blocks to commit data

.github/workflows/build-and-push-pds-ghcr.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ on:
33
push:
44
branches:
55
- main
6-
- msieben/micro-optimizations
76
env:
87
REGISTRY: ghcr.io
98
USERNAME: ${{ github.actor }}

packages/pds/src/actor-store/repo/transactor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ export class RepoTransactor extends RepoReader {
128128

129129
// find blocks that are relevant to ops but not included in diff
130130
// (for instance a record that was moved but cid stayed the same)
131-
const newRecordBlocks = commit.newBlocks.getMany(newRecordCids)
131+
const newRecordBlocks = commit.relevantBlocks.getMany(newRecordCids)
132132
if (newRecordBlocks.missing.length > 0) {
133133
const missingBlocks = await this.storage.getBlocks(
134134
newRecordBlocks.missing,
135135
)
136-
commit.newBlocks.addMap(missingBlocks.blocks)
136+
commit.relevantBlocks.addMap(missingBlocks.blocks)
137137
}
138138
return commit
139139
}

packages/pds/src/api/com/atproto/server/activateAccount.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export default function (server: Server, ctx: AppContext) {
5050
since: null,
5151
prev: null,
5252
newBlocks: blocks.blocks,
53+
relevantBlocks: blocks.blocks,
5354
removedCids: new CidSet(),
5455
}
5556
})

packages/pds/src/scripts/rebuild-repo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const rebuildRepo = async (ctx: AppContext, args: string[]) => {
7373
since: null,
7474
prev: null,
7575
newBlocks,
76+
relevantBlocks: newBlocks,
7677
removedCids: toDelete,
7778
}
7879
})

packages/pds/src/sequencer/events.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ export const formatSeqCommit = async (
2222
const blobs = new CidSet()
2323
let carSlice: Uint8Array
2424

25+
const blocksToSend = new BlockMap()
26+
blocksToSend.addMap(commitData.newBlocks)
27+
blocksToSend.addMap(commitData.relevantBlocks)
28+
2529
// max 200 ops or 1MB of data
26-
if (writes.length > 200 || commitData.newBlocks.byteSize > 1000000) {
30+
if (writes.length > 200 || blocksToSend.byteSize > 1000000) {
2731
tooBig = true
2832
const justRoot = new BlockMap()
29-
const rootBlock = commitData.newBlocks.get(commitData.cid)
33+
const rootBlock = blocksToSend.get(commitData.cid)
3034
if (rootBlock) {
3135
justRoot.set(commitData.cid, rootBlock)
3236
}
@@ -46,7 +50,7 @@ export const formatSeqCommit = async (
4650
}
4751
ops.push({ action: w.action, path, cid })
4852
}
49-
carSlice = await blocksToCarFile(commitData.cid, commitData.newBlocks)
53+
carSlice = await blocksToCarFile(commitData.cid, blocksToSend)
5054
}
5155

5256
const evt: CommitEvt = {

packages/repo/src/mst/mst.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,20 @@ export class MST {
756756
return cids
757757
}
758758

759+
async addBlocksForPath(key: string, blocks: BlockMap) {
760+
const serialized = await this.serialize()
761+
blocks.set(serialized.cid, serialized.bytes)
762+
const index = await this.findGtOrEqualLeafIndex(key)
763+
const found = await this.atIndex(index)
764+
if (found && found.isLeaf() && found.key === key) {
765+
return
766+
}
767+
const prev = await this.atIndex(index - 1)
768+
if (prev && prev.isTree()) {
769+
await prev.addBlocksForPath(key, blocks)
770+
}
771+
}
772+
759773
// Matching Leaf interface
760774
// -------------------
761775

packages/repo/src/repo.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CID } from 'multiformats/cid'
2-
import { TID } from '@atproto/common'
2+
import { dataToCborBlock, TID } from '@atproto/common'
33
import * as crypto from '@atproto/crypto'
4+
import { lexToIpld } from '@atproto/lexicon'
45
import {
56
Commit,
67
CommitData,
@@ -69,6 +70,7 @@ export class Repo extends ReadableRepo {
6970
since: null,
7071
prev: null,
7172
newBlocks,
73+
relevantBlocks: newBlocks,
7274
removedCids: diff.removedCids,
7375
}
7476
}
@@ -140,11 +142,22 @@ export class Repo extends ReadableRepo {
140142
const newBlocks = diff.newMstBlocks
141143
const removedCids = diff.removedCids
142144

145+
const relevantBlocks = new BlockMap()
146+
await Promise.all(
147+
writes.map((op) =>
148+
data.addBlocksForPath(
149+
util.formatDataKey(op.collection, op.rkey),
150+
relevantBlocks,
151+
),
152+
),
153+
)
154+
143155
const addedLeaves = leaves.getMany(diff.newLeafCids.toList())
144156
if (addedLeaves.missing.length > 0) {
145157
throw new Error(`Missing leaf blocks: ${addedLeaves.missing}`)
146158
}
147159
newBlocks.addMap(addedLeaves.blocks)
160+
relevantBlocks.addMap(addedLeaves.blocks)
148161

149162
const rev = TID.nextStr(this.commit.rev)
150163
const commit = await util.signCommit(
@@ -157,21 +170,20 @@ export class Repo extends ReadableRepo {
157170
},
158171
keypair,
159172
)
160-
const commitCid = await newBlocks.add(commit)
161-
162-
// ensure the commit cid actually changed
163-
if (commitCid.equals(this.cid)) {
164-
newBlocks.delete(commitCid)
165-
} else {
173+
const commitBlock = await dataToCborBlock(lexToIpld(commit))
174+
if (!commitBlock.cid.equals(this.cid)) {
175+
newBlocks.set(commitBlock.cid, commitBlock.bytes)
176+
relevantBlocks.set(commitBlock.cid, commitBlock.bytes)
166177
removedCids.add(this.cid)
167178
}
168179

169180
return {
170-
cid: commitCid,
181+
cid: commitBlock.cid,
171182
rev,
172183
since: this.commit.rev,
173184
prev: this.cid,
174185
newBlocks,
186+
relevantBlocks,
175187
removedCids,
176188
}
177189
}
@@ -208,6 +220,7 @@ export class Repo extends ReadableRepo {
208220
since: null,
209221
prev: null,
210222
newBlocks,
223+
relevantBlocks: newBlocks,
211224
removedCids: new CidSet([this.cid]),
212225
}
213226
}

packages/repo/src/sync/consumer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const verifyDiff = async (
9393
prev: repo?.cid ?? null,
9494
since: repo?.commit.rev ?? null,
9595
newBlocks,
96+
relevantBlocks: newBlocks,
9697
removedCids,
9798
},
9899
}

0 commit comments

Comments
 (0)