Skip to content

Commit

Permalink
fix: handle potential concurrent system contracts insertion (#378)
Browse files Browse the repository at this point in the history
If indexer finds a contract deployment event for a system contract at
the same time as system contracts service inserts it - there will be a
collision. It can very likely happen especially after these events are
added on the blockchain side for initial deployments of system
contracts.
  • Loading branch information
Romsters authored Jan 22, 2025
1 parent 2f55299 commit 24aecc3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions packages/worker/src/contract/systemContract.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ describe("SystemContractService", () => {
SystemContractService.getSystemContracts().map((contract) => mock<Address>({ address: contract.address }))
);
await systemContractService.addSystemContracts();
expect(addressRepositoryMock.add).toBeCalledTimes(0);
expect(addressRepositoryMock.upsert).toBeCalledTimes(0);
});

it("adds all system contracts if none of them exist in the DB", async () => {
(addressRepositoryMock.find as jest.Mock).mockResolvedValue([]);
await systemContractService.addSystemContracts();
expect(addressRepositoryMock.add).toBeCalledTimes(systemContracts.length);
expect(addressRepositoryMock.upsert).toBeCalledTimes(systemContracts.length);
for (const systemContract of systemContracts) {
expect(addressRepositoryMock.add).toBeCalledWith({
expect(addressRepositoryMock.upsert).toBeCalledWith({
address: systemContract.address,
bytecode: `${systemContract.address}-code`,
});
Expand All @@ -69,10 +69,10 @@ describe("SystemContractService", () => {
existingContractAddresses.map((existingContractAddress) => mock<Address>({ address: existingContractAddress }))
);
await systemContractService.addSystemContracts();
expect(addressRepositoryMock.add).toBeCalledTimes(systemContracts.length - existingContractAddresses.length);
expect(addressRepositoryMock.upsert).toBeCalledTimes(systemContracts.length - existingContractAddresses.length);
for (const systemContract of systemContracts) {
if (!existingContractAddresses.includes(systemContract.address)) {
expect(addressRepositoryMock.add).toBeCalledWith({
expect(addressRepositoryMock.upsert).toBeCalledWith({
address: systemContract.address,
bytecode: `${systemContract.address}-code`,
});
Expand All @@ -93,10 +93,10 @@ describe("SystemContractService", () => {
return `${address}-code`;
});
await systemContractService.addSystemContracts();
expect(addressRepositoryMock.add).toBeCalledTimes(systemContracts.length - notDeployedSystemContracts.length);
expect(addressRepositoryMock.upsert).toBeCalledTimes(systemContracts.length - notDeployedSystemContracts.length);
for (const systemContract of systemContracts) {
if (!notDeployedSystemContracts.includes(systemContract.address)) {
expect(addressRepositoryMock.add).toBeCalledWith({
expect(addressRepositoryMock.upsert).toBeCalledWith({
address: systemContract.address,
bytecode: `${systemContract.address}-code`,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/src/contract/systemContract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class SystemContractService {
const bytecode = await this.blockchainService.getCode(contract.address);
// some contract might not exist on the environment yet
if (bytecode !== "0x") {
await this.addressRepository.add({
await this.addressRepository.upsert({
address: contract.address,
bytecode,
});
Expand Down

0 comments on commit 24aecc3

Please sign in to comment.