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

Handl the partially linked Bytecode #61

Merged
merged 3 commits into from
Dec 9, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 2.0.0-alpha.18

* Handle the partially linked Bytecode in the Truffle Adapter.

## Version 2.0.0-alpha.17

* Separated the logic of the Reporter and moved transaction-related functions to the TransactionRunner class.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/hardhat-migrate",
"version": "2.0.0-alpha.17",
"version": "2.0.0-alpha.18",
"description": "Automatic deployment and verification of smart contracts",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
36 changes: 35 additions & 1 deletion src/deployer/Linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Linker {

public static async tryLinkBytecode(contractName: string, bytecode: string, libraries: Libraries): Promise<string> {
const artifact: ArtifactExtended = this._mustGetContractArtifact(contractName);
const neededLibraries = artifact.neededLibraries;
const neededLibraries = this._cleanNeededLibraries(bytecode, artifact, artifact.neededLibraries);

let linksToApply: Map<string, Link> = new Map();
for (const [linkedLibraryName, linkedLibraryAddress] of Object.entries(libraries)) {
Expand Down Expand Up @@ -143,9 +143,43 @@ export class Linker {
}
}

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

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

for (const { start, length } of linkReferences) {
if (!this._isLinkedLibrary(bytecode, start, length)) {
actuallyNeededLibs.set(`${sourceName}:${libName}`, { sourceName, libName });
}
}
}

return [...actuallyNeededLibs.values()];
}

/**
* The address of the linked library can be extracted like this: bytecode.slice(prefixLength + 2, suffixStart + 2)
*/
private static _isLinkedLibrary(bytecode: string, start: number, length: number): boolean {
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);

return `${prefix}${suffix}` !== "__$$__";
}

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 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}`;
}
}