Skip to content

Commit

Permalink
Fix root block missing in too big seq commit (bluesky-social#2894)
Browse files Browse the repository at this point in the history
* Fix root block missing in too big seq commit

* Update packages/pds/src/sequencer/events.ts

* fix indentation

---------

Co-authored-by: Daniel Holmgren <[email protected]>
  • Loading branch information
MarshalX and dholms authored Oct 29, 2024
1 parent c36456f commit de19439
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/pds/src/sequencer/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const formatSeqCommit = async (
if (writes.length > 200 || commitData.newBlocks.byteSize > 1000000) {
tooBig = true
const justRoot = new BlockMap()
justRoot.add(commitData.newBlocks.get(commitData.cid))
const rootBlock = commitData.newBlocks.get(commitData.cid)
if (rootBlock) {
justRoot.set(commitData.cid, rootBlock)
}
carSlice = await blocksToCarFile(commitData.cid, justRoot)
} else {
tooBig = false
Expand Down
48 changes: 46 additions & 2 deletions packages/pds/tests/sequencer.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { TestNetworkNoAppView, SeedClient } from '@atproto/dev-env'
import { randomStr } from '@atproto/crypto'
import { cborEncode, readFromGenerator, wait } from '@atproto/common'
import { Sequencer, SeqEvt } from '../src/sequencer'
import {
cborDecode,
cborEncode,
readFromGenerator,
wait,
} from '@atproto/common'
import { Sequencer, SeqEvt, formatSeqCommit } from '../src/sequencer'
import { sequencer, repoPrepare } from '../../pds'
import Outbox from '../src/sequencer/outbox'
import userSeed from './seeds/users'
import { ids } from '../src/lexicon/lexicons'
import { readCarWithRoot } from '@atproto/repo'

describe('sequencer', () => {
let network: TestNetworkNoAppView
Expand Down Expand Up @@ -207,4 +215,40 @@ describe('sequencer', () => {
}
lastSeen = results[0].at(-1)?.seq ?? lastSeen
})

it('root block must be returned in tooBig seq commit', async () => {
// Create good records to exceed the event limit (the current limit is 200 events)
// it creates events completely locally, so it doesn't need to be in the network
const eventsToCreate = 250
const createPostRecord = () =>
repoPrepare.prepareCreate({
did: sc.dids.alice,
collection: ids.AppBskyFeedPost,
record: { text: 'valid', createdAt: new Date().toISOString() },
})
const writesPromises = Array.from(
{ length: eventsToCreate },
createPostRecord,
)
const writes = await Promise.all(writesPromises)
// just format commit without processing writes
const writeCommit = await network.pds.ctx.actorStore.transact(
sc.dids.alice,
(store) => store.repo.formatCommit(writes),
)

const repoSeqInsert = await formatSeqCommit(
sc.dids.alice,
writeCommit,
writes,
)

const evt = cborDecode<sequencer.CommitEvt>(repoSeqInsert.event)
expect(evt.tooBig).toBe(true)

const car = await readCarWithRoot(evt.blocks)
expect(car.root.toString()).toBe(writeCommit.cid.toString())
// in the case of tooBig, the blocks must contain the root block only
expect(car.blocks.size).toBe(1)
})
})

0 comments on commit de19439

Please sign in to comment.