Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle potential concurrent system contracts insertion #378

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
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
Loading