Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions yarn-project/archiver/src/store/contract_class_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ describe('ContractClassStore', () => {
await expect(contractClassStore.getContractClass(contractClass.id)).resolves.toBeUndefined();
});

it('throws if the same contract class is added again', async () => {
it('is a no-op if the same contract class is added again at a later block', async () => {
await expect(
contractClassStore.addContractClasses([await withCommitment(contractClass)], BlockNumber(blockNum + 1)),
).rejects.toThrow(/already exists/);
).resolves.toBe(true);
// Original l2BlockNumber is preserved, so a later delete at blockNum + 1 should not remove it.
await contractClassStore.deleteContractClasses([contractClass], BlockNumber(blockNum + 1));
await expect(contractClassStore.getContractClass(contractClass.id)).resolves.toMatchObject(contractClass);
});

it('returns contract class if deleted at a later block number', async () => {
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/archiver/src/store/contract_class_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ export class ContractClassStore {
): Promise<void> {
await this.db.transactionAsync(async () => {
const key = contractClass.id.toString();
if (await this.#contractClasses.hasAsync(key)) {
throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
}
await this.#contractClasses.set(
// Class id is content-derived (artifactHash, privateFunctionsRoot, publicBytecodeCommitment),
// so a duplicate add carries identical data. Keep the original entry's l2BlockNumber so that
// a later rollback at a higher block leaves the class registered at its earliest block.
await this.#contractClasses.setIfNotExists(
key,
serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
);
await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
await this.#bytecodeCommitments.setIfNotExists(key, bytecodeCommitment.toBuffer());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ describe('ContractInstanceStore', () => {
).resolves.toBeUndefined();
});

it('throws when adding the same contract instance twice', async () => {
await expect(contractInstanceStore.addContractInstances([contractInstance], BlockNumber(2))).rejects.toThrow(
/already exists/,
);
it('is a no-op when adding the same contract instance twice', async () => {
await expect(contractInstanceStore.addContractInstances([contractInstance], BlockNumber(2))).resolves.toBe(true);
await expect(
contractInstanceStore.getContractInstance(contractInstance.address, timestamp),
).resolves.toMatchObject(contractInstance);
});
});

Expand Down
15 changes: 9 additions & 6 deletions yarn-project/archiver/src/store/contract_instance_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ export class ContractInstanceStore {
addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
return this.db.transactionAsync(async () => {
const key = contractInstance.address.toString();
if (await this.#contractInstances.hasAsync(key)) {
throw new Error(
`Contract instance at ${key} already exists (deployed at block ${await this.#contractInstancePublishedAt.getAsync(key)}), cannot add again at block ${blockNumber}`,
);
// Instance address is content-derived from instance fields, so a duplicate add carries
// identical data. Keep the original entry's blockNumber so that a later rollback at a higher
// block leaves the instance registered at its earliest block.
const inserted = await this.#contractInstances.setIfNotExists(
key,
new SerializableContractInstance(contractInstance).toBuffer(),
);
if (inserted) {
await this.#contractInstancePublishedAt.set(key, blockNumber);
}
await this.#contractInstances.set(key, new SerializableContractInstance(contractInstance).toBuffer());
await this.#contractInstancePublishedAt.set(key, blockNumber);
});
}

Expand Down
Loading