Skip to content

Commit

Permalink
feat: deleting instances/classes + extended test
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Sep 25, 2024
1 parent 09a60af commit 8e40d33
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 89 deletions.
10 changes: 10 additions & 0 deletions yarn-project/archiver/src/archiver/archiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ describe('Archiver', () => {
// Should also see the block number be reduced
latestBlockNum = await archiver.getBlockNumber();
expect(latestBlockNum).toEqual(numL2BlocksInTest - 1);

const txHash = blocks[1].body.txEffects[0].txHash;
expect(await archiver.getTxEffect(txHash)).resolves.toBeUndefined;
expect(await archiver.getBlock(2)).resolves.toBeUndefined;

[LogType.NOTEENCRYPTED, LogType.ENCRYPTED, LogType.UNENCRYPTED].forEach(async t => {
expect(await archiver.getLogs(2, 1, t)).toEqual([]);
});

// The random blocks don't include contract instances nor classes we we cannot look for those here.
}, 10_000);

// logs should be created in order of how archiver syncs.
Expand Down
9 changes: 6 additions & 3 deletions yarn-project/archiver/src/archiver/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ export class Archiver implements ArchiveSource {
};

// This is an edge case that we only hit if there are no proposed blocks.
// If we have 0 blocks locally and there are no blocks onchain there is nothing to do.
const noBlocks = localPendingBlockNumber === 0n && pendingBlockNumber === 0n;
if (noBlocks) {
await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
Expand Down Expand Up @@ -358,6 +359,7 @@ export class Archiver implements ArchiveSource {

if (retrievedBlocks.length === 0) {
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
// See further details in earlier comments.
this.log.verbose(`Retrieved no new blocks from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
return;
}
Expand Down Expand Up @@ -590,7 +592,7 @@ class ArchiverStoreHelper
if (operation == Operation.Store) {
return await this.store.addContractClasses(contractClasses, blockNum);
} else if (operation == Operation.Delete) {
// return await this.store.deleteContractClasses(contractClasses, blockNum);
return await this.store.deleteContractClasses(contractClasses, blockNum);
}
}
return true;
Expand All @@ -609,7 +611,7 @@ class ArchiverStoreHelper
if (operation == Operation.Store) {
return await this.store.addContractInstances(contractInstances, blockNum);
} else if (operation == Operation.Delete) {
// return await this.store.deleteContractInstances(contractInstances, blockNum);
return await this.store.deleteContractInstances(contractInstances, blockNum);
}
}
return true;
Expand Down Expand Up @@ -696,7 +698,8 @@ class ArchiverStoreHelper
throw new Error(`Can only remove from the tip`);
}

const blocks = await this.getBlocks(from - blocksToUnwind, blocksToUnwind);
// from - blocksToUnwind = the new head, so + 1 for what we need to remove
const blocks = await this.getBlocks(from - blocksToUnwind + 1, blocksToUnwind);

return [
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export class BlockStore {
*/
unwindBlocks(from: number, blocksToUnwind: number) {
return this.db.transaction(() => {
// We should only allow deleting the very last ye, otherwise we can really get some messy shit.
const last = this.getSynchedL2BlockNumber();
if (from != last) {
throw new Error(`Can only remove from the tip`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ export class MemoryArchiverStore implements ArchiverDataStore {
logType: TLogType,
): Promise<L2BlockL2Logs<FromLogType<TLogType>>[]> {
if (from < INITIAL_L2_BLOCK_NUM || limit < 1) {
throw new Error(`Invalid limit: ${limit}`);
return Promise.resolve([]);
}

if (from > this.l2Blocks.length) {
throw new Error(`"from" cannot be in the future`);
return Promise.resolve([]);
}

const logMap = (() => {
Expand Down
Loading

0 comments on commit 8e40d33

Please sign in to comment.