Skip to content

Commit

Permalink
fix(verify-bytecode): json print message with correct verification ty…
Browse files Browse the repository at this point in the history
…pe (#8402)

* fix(verify-bytecode): json print message with correct verification type

* replace `input` of creation tx with local creation code before runtime match.
  • Loading branch information
yash-atreya committed Jul 16, 2024
1 parent ea7817c commit 3cbe211
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions crates/verify/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl VerifyBytecodeArgs {
// Get creation tx hash
let creation_data = etherscan.contract_creation_data(self.address).await?;

trace!(creation_tx_hash = ?creation_data.transaction_hash);
let mut transaction = provider
.get_transaction_by_hash(creation_data.transaction_hash)
.await
Expand Down Expand Up @@ -237,6 +238,7 @@ impl VerifyBytecodeArgs {
};

// Append constructor args to the local_bytecode
trace!(%constructor_args);
let mut local_bytecode_vec = local_bytecode.to_vec();
local_bytecode_vec.extend_from_slice(&constructor_args);

Expand All @@ -259,6 +261,21 @@ impl VerifyBytecodeArgs {
&config,
);

// If the creation code does not match, the runtime also won't match. Hence return.
if !did_match {
self.print_result(
(did_match, with_status),
BytecodeType::Runtime,
&mut json_results,
etherscan_metadata,
&config,
);
if self.json {
println!("{}", serde_json::to_string(&json_results)?);
}
return Ok(());
}

// Get contract creation block
let simulation_block = match self.block {
Some(BlockId::Number(BlockNumberOrTag::Number(block))) => block,
Expand Down Expand Up @@ -308,6 +325,20 @@ impl VerifyBytecodeArgs {
env.block.gas_limit = U256::from(block.header.gas_limit);
}

// Replace the `input` with local creation code in the creation tx.
if let Some(to) = transaction.to {
if to == DEFAULT_CREATE2_DEPLOYER {
let mut input = transaction.input[..32].to_vec(); // Salt
input.extend_from_slice(&local_bytecode_vec);
transaction.input = Bytes::from(input);

// Deploy default CREATE2 deployer
executor.deploy_create2_deployer()?;
}
} else {
transaction.input = Bytes::from(local_bytecode_vec);
}

configure_tx_env(&mut env, &transaction);

let env_with_handler =
Expand Down Expand Up @@ -350,9 +381,9 @@ impl VerifyBytecodeArgs {
let onchain_runtime_code =
provider.get_code_at(self.address).block_id(BlockId::number(simulation_block)).await?;

// Compare the runtime bytecode with the locally built bytecode
// Compare the onchain runtime bytecode with the runtime code from the fork.
let (did_match, with_status) = try_match(
fork_runtime_code.bytecode(),
&fork_runtime_code.original_bytes(),
&onchain_runtime_code,
&constructor_args,
&verification_type,
Expand Down Expand Up @@ -491,7 +522,7 @@ impl VerifyBytecodeArgs {
let json_res = JsonResult {
bytecode_type,
matched: false,
verification_type: self.verification_type,
verification_type: res.1.unwrap(),
message: Some(format!(
"{bytecode_type:?} code did not match - this may be due to varying compiler settings"
)),
Expand Down Expand Up @@ -573,7 +604,7 @@ fn try_match(
Ok((true, Some(VerificationType::Full)))
} else {
try_partial_match(local_bytecode, bytecode, constructor_args, is_runtime, has_metadata)
.map(|matched| (matched, matched.then_some(VerificationType::Partial)))
.map(|matched| (matched, Some(VerificationType::Partial)))
}
}

Expand Down

0 comments on commit 3cbe211

Please sign in to comment.