Skip to content

Commit

Permalink
Merge branch 'staging' of github.com:ethereum/sourcify into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzdogan committed Mar 9, 2023
2 parents e8033ec + b64da92 commit 3c13af8
Show file tree
Hide file tree
Showing 14 changed files with 489 additions and 82 deletions.
1 change: 1 addition & 0 deletions packages/lib-sourcify/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface Match {
create2Args?: Create2Args;
libraryMap?: StringMap;
contextVariables?: ContextVariables;
creatorTxHash?: string;
}

export type Status =
Expand Down
1 change: 1 addition & 0 deletions packages/lib-sourcify/src/lib/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export async function matchWithCreationTx(
recompiledCreationBytecode
);
match.abiEncodedConstructorArguments = abiEncodedConstructorArguments;
match.creatorTxHash = creatorTxHash;
}
}
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/lib-sourcify/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { expect } from 'chai';
/**
* Function to deploy contracts from provider unlocked accounts
*
* @returns the address of the deployed contract
* @returns the address of the deployed contract and the creator tx hash
*/
// TODO: ABI type definition
export async function deployFromAbiAndBytecode(
Expand Down
7 changes: 5 additions & 2 deletions src/server/controllers/VerificationController-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export type ContractMeta = {
abiEncodedConstructorArguments?: string;
msgSender?: string;
};
creatorTxHash?: string;
status?: Status;
statusMessage?: string;
storageTimestamp?: Date;
Expand Down Expand Up @@ -387,7 +388,8 @@ export const verifyContractsInSession = async (
continue;
}

const { address, chainId, contract, contextVariables } = contractWrapper;
const { address, chainId, contract, contextVariables, creatorTxHash } =
contractWrapper;

// The session saves the CheckedContract as a simple object, so we need to reinstantiate it
const checkedContract = new CheckedContract(
Expand All @@ -410,7 +412,8 @@ export const verifyContractsInSession = async (
checkedContract,
chainId as string,
address as string,
contextVariables
contextVariables,
creatorTxHash
);
// Send to verification again with all source files.
if (match.status === "extra-file-input-bug") {
Expand Down
25 changes: 21 additions & 4 deletions src/server/controllers/VerificationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export default class VerificationController
contract,
req.body.chain,
req.addresses[0], // Due to the old API taking an array of addresses.
req.body.contextVariables
req.body.contextVariables,
req.body.creatorTxHash
);
// Send to verification again with all source files.
if (match.status === "extra-file-input-bug") {
Expand All @@ -144,7 +145,8 @@ export default class VerificationController
contractWithAllSources,
req.body.chain,
req.addresses[0], // Due to the old API taking an array of addresses.
req.body.contextVariables
req.body.contextVariables,
req.body.creatorTxHash
);
if (tempMatch.status === "perfect") {
await this.repositoryService.storeMatch(contract, tempMatch);
Expand Down Expand Up @@ -230,6 +232,7 @@ export default class VerificationController
contractWrapper.address = receivedContract.address;
contractWrapper.chainId = receivedContract.chainId;
contractWrapper.contextVariables = receivedContract.contextVariables;
contractWrapper.creatorTxHash = receivedContract.creatorTxHash;
if (isVerifiable(contractWrapper)) {
verifiable[id] = contractWrapper;
}
Expand Down Expand Up @@ -446,14 +449,19 @@ export default class VerificationController

const contract: CheckedContract = checkedContracts[0];

const result = await verifyCreate2(
const match = await verifyCreate2(
contract,
deployerAddress,
salt,
create2Address,
abiEncodedConstructorArguments
);
res.send({ result: [result] });

if (match.status) {
await this.repositoryService.storeMatch(contract, match);
}

res.send({ result: [match] });
};

private sessionVerifyCreate2 = async (
Expand Down Expand Up @@ -495,6 +503,10 @@ export default class VerificationController
contractWrapper.storageTimestamp = match.storageTimestamp;
contractWrapper.address = match.address;

if (match.status) {
await this.repositoryService.storeMatch(contract, match);
}

res.send(getSessionJSON(session));
};

Expand Down Expand Up @@ -563,6 +575,11 @@ export default class VerificationController
...req.body.contextVariables,
})
),
body("creatorTxHash")
.optional()
.custom(
(creatorTxHash, { req }) => (req.body.creatorTxHash = creatorTxHash)
),
this.safeHandler(this.legacyVerifyEndpoint)
);

Expand Down
100 changes: 32 additions & 68 deletions src/server/services/RepositoryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,22 @@ export default class RepositoryService implements IRepositoryService {
match.address,
contract.solidity
);
this.storeMetadata(

// Store metadata
this.storeJSON(
matchQuality,
match.chainId,
match.address,
"metadata.json",
contract.metadata
);

if (match.abiEncodedConstructorArguments) {
this.storeConstructorArgs(
this.storeTxt(
matchQuality,
match.chainId,
match.address,
"constructor-args.txt",
match.abiEncodedConstructorArguments
);
}
Expand All @@ -423,28 +427,41 @@ export default class RepositoryService implements IRepositoryService {
match.contextVariables &&
Object.keys(match.contextVariables).length > 0
) {
this.storeContextVariables(
this.storeJSON(
matchQuality,
match.chainId,
match.address,
"context-variables.json",
match.contextVariables
);
}

if (match.creatorTxHash) {
this.storeTxt(
matchQuality,
match.chainId,
match.address,
"creator-tx-hash.txt",
match.creatorTxHash
);
}

if (match.create2Args) {
this.storeCreate2Args(
this.storeJSON(
matchQuality,
match.chainId,
match.address,
"create2-args.json",
match.create2Args
);
}

if (match.libraryMap && Object.keys(match.libraryMap).length) {
this.storeLibraryMap(
this.storeJSON(
matchQuality,
match.chainId,
match.address,
"library-map.json",
match.libraryMap
);
}
Expand Down Expand Up @@ -527,93 +544,40 @@ export default class RepositoryService implements IRepositoryService {
}
}

private storeMetadata(
private storeJSON(
matchQuality: MatchQuality,
chainId: string,
address: string,
metadata: Metadata
fileName: string,
contentJSON: any
) {
this.save(
{
matchQuality,
chainId,
address,
fileName: "metadata.json",
},
JSON.stringify(metadata)
);
}

private storeConstructorArgs(
matchQuality: MatchQuality,
chainId: string,
address: string,
abiEncodedConstructorArguments: string
) {
this.save(
{
matchQuality,
chainId,
address,
source: false,
fileName: "constructor-args.txt",
},
abiEncodedConstructorArguments
);
}

private storeContextVariables(
matchQuality: MatchQuality,
chainId: string,
address: string,
contextVariables: ContextVariables
) {
this.save(
{
matchQuality,
chainId,
address,
source: false,
fileName: "context-variables.json",
},
JSON.stringify(contextVariables, undefined, 2)
);
}

private storeCreate2Args(
matchQuality: MatchQuality,
chainId: string,
address: string,
create2Args: Create2Args
) {
this.save(
{
matchQuality,
chainId,
address,
source: false,
fileName: "create2-args.json",
fileName,
},
JSON.stringify(create2Args)
JSON.stringify(contentJSON)
);
}

private storeLibraryMap(
private storeTxt(
matchQuality: MatchQuality,
chainId: string,
address: string,
libraryMap: StringMap
fileName: string,
content: string
) {
const indentationSpaces = 2;
this.save(
{
matchQuality,
chainId,
address,
source: false,
fileName: "library-map.json",
fileName,
},
JSON.stringify(libraryMap, null, indentationSpaces)
content
);
}

Expand Down
1 change: 1 addition & 0 deletions src/server/services/VerificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class VerificationService implements IVerificationService {
} catch (err) {
// Find the creator tx if it wasn't supplied and try verifying again with it.
if (
!creatorTxHash &&
err instanceof Error &&
err.message === "The deployed and recompiled bytecode don't match."
) {
Expand Down
52 changes: 52 additions & 0 deletions test/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,57 @@ async function deployFromAbiAndBytecode(web3, abi, bytecode, from, args) {
return contractResponse.options.address;
}

/**
* Creator tx hash is needed for tests. This function returns the tx hash in addition to the contract address.
*
* @returns The contract address and the tx hash
*/
async function deployFromAbiAndBytecodeForCreatorTxHash(
web3,
abi,
bytecode,
from,
args
) {
// Deploy contract
const contract = new web3.eth.Contract(abi);
const deployment = contract.deploy({
data: bytecode,
arguments: args || [],
});
const gas = await deployment.estimateGas({ from });

// If awaited, the send() Promise returns the contract instance.
// We also need the tx hash so we need two seperate event listeners.
const sendPromiEvent = deployment.send({
from,
gas,
});

const txHashPromise = new Promise((resolve, reject) => {
sendPromiEvent.on("transactionHash", (txHash) => {
resolve(txHash);
});
sendPromiEvent.on("error", (error) => {
reject(error);
});
});

const contractAddressPromise = new Promise((resolve, reject) => {
sendPromiEvent.on("receipt", (receipt) => {
if (!receipt.contractAddress) {
reject(new Error("No contract address in receipt"));
} else {
resolve(receipt.contractAddress);
}
});
sendPromiEvent.on("error", (error) => {
reject(error);
});
});

return Promise.all([contractAddressPromise, txHashPromise]);
}
/**
* Function to deploy contracts from an external account with private key
*/
Expand Down Expand Up @@ -86,6 +137,7 @@ async function callContractMethodWithTx(

module.exports = {
deployFromAbiAndBytecode,
deployFromAbiAndBytecodeForCreatorTxHash,
deployFromPrivateKey,
waitSecs,
callContractMethod,
Expand Down
Loading

0 comments on commit 3c13af8

Please sign in to comment.