Skip to content
Closed
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
1 change: 1 addition & 0 deletions services/server/src/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ module.exports = {
// verify-deprecated endpoint used in services/database/scripts.mjs. Used when recreating the DB with deprecated chains that don't have an RPC.
verifyDeprecated: false,
upgradeContract: false,
throwIfAlreadyVerified: true, // If true, will not throw an error if contract already verified.
};
29 changes: 16 additions & 13 deletions services/server/src/server/apiv2/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,23 @@ export async function checkIfAlreadyVerified(
res: Response,
next: NextFunction,
) {
const { address, chainId } = req.params;
const services = req.app.get("services") as Services;
const contract = await services.storage.performServiceOperation(
"getContract",
[chainId, address],
);

if (
contract.runtimeMatch === "exact_match" &&
contract.creationMatch === "exact_match"
) {
throw new AlreadyVerifiedError(
`Contract ${address} on chain ${chainId} is already verified with runtimeMatch and creationMatch both being exact matches.`,
const throwIfAlreadyVerified = req.app.get("chainRepository") as boolean;
if (!throwIfAlreadyVerified) {
const { address, chainId } = req.params;
const services = req.app.get("services") as Services;
const contract = await services.storage.performServiceOperation(
"getContract",
[chainId, address],
);

if (
contract.runtimeMatch === "exact_match" &&
contract.creationMatch === "exact_match"
) {
throw new AlreadyVerifiedError(
`Contract ${address} on chain ${chainId} is already verified with runtimeMatch and creationMatch both being exact matches.`,
);
}
}

next();
Expand Down
1 change: 1 addition & 0 deletions services/server/src/server/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ const server = new Server(
},
schema: process.env.ALLIANCE_POSTGRES_SCHEMA as string,
},
throwIfAlreadyVerified: config.get("throwIfAlreadyVerified"),
},
);

Expand Down
1 change: 1 addition & 0 deletions services/server/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export class Server {
this.app.set("verifyDeprecated", options.verifyDeprecated);
this.app.set("upgradeContract", options.upgradeContract);
this.app.set("services", this.services);
this.app.set("throwIfAlreadyVerified", storageServiceOptions.throwIfAlreadyVerified ?? true);

this.app.use(
bodyParser.urlencoded({
Expand Down
29 changes: 17 additions & 12 deletions services/server/src/server/services/StorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ export interface StorageServiceOptions {
sourcifyDatabaseServiceOptions?: DatabaseOptions;
allianceDatabaseServiceOptions?: DatabaseOptions;
s3RepositoryServiceOptions?: S3RepositoryServiceOptions;
throwIfAlreadyVerified?: boolean;
}

export class StorageService {
enabledServices: EnabledServices;
throwIfAlreadyVerified: boolean = true;

rwServices: { [key in RWStorageIdentifiers]: RWStorageService } = {} as {
[key in RWStorageIdentifiers]: RWStorageService;
Expand All @@ -142,7 +144,7 @@ export class StorageService {

constructor(options: StorageServiceOptions) {
this.enabledServices = options.enabledServices;

this.throwIfAlreadyVerified = options.throwIfAlreadyVerified ?? true;
const enabledServicesArray = [
this.enabledServices.read,
...this.enabledServices.writeOrWarn,
Expand Down Expand Up @@ -330,17 +332,20 @@ export class StorageService {
existingMatch.length > 0 &&
!isBetterVerification(verification, existingMatch[0])
) {
logger.info("Partial match already exists", {
chain: verification.chainId,
address: verification.address,
newRuntimeMatch: verification.status.runtimeMatch,
newCreationMatch: verification.status.creationMatch,
existingRuntimeMatch: existingMatch[0].runtimeMatch,
existingCreationMatch: existingMatch[0].creationMatch,
});
throw new ConflictError(
`The contract ${verification.address} on chainId ${verification.chainId} is already partially verified. The provided new source code also yielded a partial match and will not be stored unless it's a full match`,
);
if (this.throwIfAlreadyVerified) {
logger.info("Partial match already exists", {
chain: verification.chainId,
address: verification.address,
newRuntimeMatch: verification.status.runtimeMatch,
newCreationMatch: verification.status.creationMatch,
existingRuntimeMatch: existingMatch[0].runtimeMatch,
existingCreationMatch: existingMatch[0].creationMatch,
});
throw new ConflictError(
`The contract ${verification.address} on chainId ${verification.chainId} is already partially verified. The provided new source code also yielded a partial match and will not be stored unless it's a full match`,
);
}
return;
}

// Initialize an array to hold active service promises
Expand Down