Skip to content

Commit

Permalink
Handled the partially linked Bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrylR committed Dec 9, 2023
1 parent b75b231 commit 609c7e1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/deployer/Linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class Linker {
});
}

linksToApply = this._fillLinksToApply(bytecode, artifact, neededLibraries);

if (linksToApply.size < neededLibraries.length) {
const separatelyDeployedLibraries = await this._findMissingLibraries(
neededLibraries.filter((lib) => !linksToApply.has(`${lib.sourceName}:${lib.libName}`)),
Expand Down Expand Up @@ -143,10 +145,55 @@ export class Linker {
}
}

private static _fillLinksToApply(
bytecode: string,
artifact: Artifact,
libraries: NeededLibrary[],
): Map<string, Link> {
const linksToApplyFilled: Map<string, Link> = new Map();

for (const { sourceName, libName } of libraries) {
const linkReferences = artifact.linkReferences[sourceName][libName];

for (const { start, length } of linkReferences) {
const [isLinkedLibrary, address] = this._getLinkedLibrary(bytecode, start, length);

if (isLinkedLibrary) {
linksToApplyFilled.set(`${sourceName}:${libName}`, {
sourceName: sourceName,
libraryName: libName,
address,
});
}
}
}

return linksToApplyFilled;
}

private static _getLinkedLibrary(bytecode: string, start: number, length: number): [boolean, string] {
const prefixLength = start * 2;
const prefix = bytecode.slice(prefixLength + 2, prefixLength + 5);

const suffixStart = (start + length) * 2;
const suffix = bytecode.slice(suffixStart - 1, suffixStart + 2);

const address = bytecode.slice(prefixLength + 2, suffixStart + 2);

return [`${prefix}${suffix}` !== "__$$__", `0x${address}`];
}

private static _linkBytecode(bytecode: string, artifact: Artifact, libraries: Link[]): string {
for (const { sourceName, libraryName, address } of libraries) {
const linkReferences = artifact.linkReferences[sourceName][libraryName];

for (const { start, length } of linkReferences) {
const [isLinkedLibrary] = this._getLinkedLibrary(bytecode, start, length);

if (isLinkedLibrary) {
continue;
}

const prefixLength = 2 + start * 2;
const prefix = bytecode.slice(0, prefixLength);

Expand Down
18 changes: 15 additions & 3 deletions src/deployer/adapters/TruffleAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ export class TruffleAdapter extends Adapter {
}

public getRawBytecode(instance: TruffleContract): string {
return bytecodeToString(instance.bytecode);
try {
return bytecodeToString(instance.binary);
} catch {
return bytecodeToString(instance.bytecode);
}
}

public getContractName<A, I>(instance: Instance<A, I>, parameters: OverridesAndName): string {
Expand All @@ -80,8 +84,8 @@ export class TruffleAdapter extends Adapter {
try {
return ArtifactProcessor.tryGetContractName(this.getRawBytecode(instance));
} catch {
if ((instance as any).contractName) {
return (instance as any).contractName;
if ((instance as any)._hArtifact) {
return this._getFullyQualifiedName(instance as any) || (instance as any).contractName;
}

return UNKNOWN_CONTRACT_NAME;
Expand Down Expand Up @@ -238,4 +242,12 @@ export class TruffleAdapter extends Adapter {

return UNKNOWN_TRANSACTION_NAME;
}

private _getFullyQualifiedName(instance: any): string | undefined {
if (!instance._hArtifact.sourceName || !instance._hArtifact.contractName) {
return undefined;
}

return `${instance._hArtifact.sourceName}:${instance._hArtifact.contractName}`;
}
}

0 comments on commit 609c7e1

Please sign in to comment.