Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Factor out repeat compilation check and put it in encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
haltman-at committed Nov 15, 2022
1 parent 26bf271 commit b756ba2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 29 deletions.
13 changes: 13 additions & 0 deletions packages/codec/lib/compilations/utils.ts
Expand Up @@ -688,3 +688,16 @@ export function infoToCompilations(
return shimArtifacts(projectInfo.artifacts, undefined, nonceString);
}
}

export function findRepeatCompilationId(
compilations: Compilation[]
): string | null {
for (let i = 0; i < compilations.length; i++) {
for (let j = i + 1; j < compilations.length; j++) {
if (compilations[i].id === compilations[j].id) {
return compilations[i].id;
}
}
}
return null;
}
13 changes: 13 additions & 0 deletions packages/codec/lib/errors.ts
Expand Up @@ -76,3 +76,16 @@ export class NoProjectInfoError extends Error {
this.name = "NoProjectInfoError";
}
}

/**
* This error indicates there was an attempt to add multiple compilations
* with the same ID, or a compilation whose ID was already in use.
*/
export class RepeatCompilationIdError extends Error {
public id: string;
constructor(id: string) {
super(`Compilation id ${id} already in use.`);
this.id = id;
this.name = "RepeatCompilationIdError";
}
}
7 changes: 6 additions & 1 deletion packages/codec/lib/index.ts
Expand Up @@ -77,7 +77,12 @@ export {
decodeReturndata,
decodeRevert
} from "./core";
export { DecodingError, StopDecodingError, NoProjectInfoError } from "./errors";
export {
DecodingError,
StopDecodingError,
NoProjectInfoError,
RepeatCompilationIdError
} from "./errors";

//now: what types should we export? (other than the ones from ./format)
//public-facing types for the interface
Expand Down
25 changes: 10 additions & 15 deletions packages/decoder/lib/decoders.ts
Expand Up @@ -41,8 +41,7 @@ import {
VariableNotFoundError,
MemberNotFoundError,
ArrayIndexOutOfBoundsError,
NoProviderError,
RepeatCompilationIdError
NoProviderError
} from "./errors";
import { Shims } from "@truffle/compile-common";
//sorry for the untyped import, but...
Expand Down Expand Up @@ -83,12 +82,10 @@ export class ProjectDecoder {
throw new NoProviderError();
}
//check for repeat compilation IDs
for (let i = 0; i < compilations.length; i++) {
for (let j = i + 1; j < compilations.length; j++) {
if (compilations[i].id === compilations[j].id) {
throw new RepeatCompilationIdError(compilations[i].id);
}
}
const repeatId =
Codec.Compilations.Utils.findRepeatCompilationId(compilations);
if (repeatId !== null) {
throw new Codec.RepeatCompilationIdError(repeatId);
}
this.providerAdapter = new ProviderAdapter(provider);
this.compilations = compilations;
Expand Down Expand Up @@ -166,15 +163,13 @@ export class ProjectDecoder {
existingIds.has(compilation.id)
);
if (conflictingCompilation !== undefined) {
throw new RepeatCompilationIdError(conflictingCompilation.id);
throw new Codec.RepeatCompilationIdError(conflictingCompilation.id);
}
//also: check for repeats among the ones we're adding
for (let i = 0; i < compilations.length; i++) {
for (let j = i + 1; j < compilations.length; j++) {
if (compilations[i].id === compilations[j].id) {
throw new RepeatCompilationIdError(compilations[i].id);
}
}
const repeatId =
Codec.Compilations.Utils.findRepeatCompilationId(compilations);
if (repeatId !== null) {
throw new Codec.RepeatCompilationIdError(repeatId);
}

//now: checks are over, start adding stuff
Expand Down
13 changes: 0 additions & 13 deletions packages/decoder/lib/errors.ts
Expand Up @@ -174,16 +174,3 @@ export class NoProviderError extends Error {
this.name = "NoProviderError";
}
}

/**
* This error indicates there was an attempt to add multiple compilations
* with the same ID, or a compilation whose ID was already in use.
*/
export class RepeatCompilationIdError extends Error {
public id: string;
constructor(id: string) {
super(`Compilation id ${id} already in use.`);
this.id = id;
this.name = "RepeatCompilationIdError";
}
}
8 changes: 8 additions & 0 deletions packages/encoder/lib/encoders.ts
Expand Up @@ -96,6 +96,14 @@ export class ProjectEncoder {
if (!info.compilations) {
throw new NoInternalInfoError();
}
//check for repeat compilation IDs
const repeatId = Codec.Compilations.Utils.findRepeatCompilationId(
info.compilations
);
if (repeatId !== null) {
throw new Codec.RepeatCompilationIdError(repeatId);
}
//since that's good, save it and continue
this.compilations = info.compilations;
({
definitions: this.referenceDeclarations,
Expand Down

0 comments on commit b756ba2

Please sign in to comment.