Skip to content

Commit

Permalink
vm: Move contract bytecode hash verification from dusk-core
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Dec 22, 2024
1 parent c38dcd5 commit 27a067e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dusk-bytes = { workspace = true }
piecrust = { workspace = true }
lru = { workspace = true }
blake2b_simd = { workspace = true }
blake3 = { workspace = true }
dusk-poseidon = { workspace = true }
rkyv = { workspace = true, features = ["size_32"] }

Expand Down
13 changes: 11 additions & 2 deletions vm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use blake2b_simd::Params;
use dusk_core::abi::{ContractError, ContractId, CONTRACT_ID_BYTES};
use dusk_core::transfer::{Transaction, TRANSFER_CONTRACT};
use dusk_core::transfer::{
data::ContractBytecode, Transaction, TRANSFER_CONTRACT,
};
use piecrust::{CallReceipt, Error, Session};

/// Executes a transaction, returning the receipt of the call and the gas spent.
Expand Down Expand Up @@ -138,7 +140,7 @@ fn contract_deploy(
let min_gas_limit = receipt.gas_spent + deploy_charge;
if gas_left < min_gas_limit {
receipt.data = Err(ContractError::OutOfGas);
} else if !deploy.bytecode.verify_hash() {
} else if !verify_bytecode_hash(&deploy.bytecode) {
receipt.data = Err(ContractError::Panic(
"failed bytecode hash check".into(),
))
Expand Down Expand Up @@ -167,6 +169,13 @@ fn contract_deploy(
}
}

// Verifies that the stored contract bytecode hash is correct.
fn verify_bytecode_hash(bytecode: &ContractBytecode) -> bool {
let computed: [u8; 32] = blake3::hash(bytecode.bytes.as_slice()).into();

bytecode.hash == computed
}

/// Generate a [`ContractId`] address from:
/// - slice of bytes,
/// - nonce
Expand Down

0 comments on commit 27a067e

Please sign in to comment.