use std::collections::HashMap;
use darkfi_sdk::{
blockchain::block_version,
crypto::{schnorr::SchnorrPublic, ContractId, MerkleTree, PublicKey},
dark_tree::dark_forest_leaf_vec_integrity_check,
deploy::DeployParamsV1,
pasta::pallas,
};
use darkfi_serial::{deserialize_async, serialize_async, AsyncDecodable, AsyncEncodable};
use log::{debug, error, warn};
use num_bigint::BigUint;
use smol::io::Cursor;
use crate::{
blockchain::{
block_store::append_tx_to_merkle_tree, BlockInfo, Blockchain, BlockchainOverlayPtr,
HeaderHash,
},
error::TxVerifyFailed,
runtime::vm_runtime::Runtime,
tx::{Transaction, MAX_TX_CALLS, MIN_TX_CALLS},
validator::{
consensus::{Consensus, Fork, Proposal, GAS_LIMIT_UNPROPOSED_TXS},
fees::{circuit_gas_use, compute_fee, GasData, PALLAS_SCHNORR_SIGNATURE_FEE},
pow::PoWModule,
},
zk::VerifyingKey,
Error, Result,
};
pub async fn verify_genesis_block(
overlay: &BlockchainOverlayPtr,
block: &BlockInfo,
block_target: u32,
) -> Result<()> {
let block_hash = block.hash().as_string();
debug!(target: "validator::verification::verify_genesis_block", "Validating genesis block {}", block_hash);
if overlay.lock().unwrap().has_block(block)? {
return Err(Error::BlockAlreadyExists(block_hash))
}
if block.header.height != 0 {
return Err(Error::BlockIsInvalid(block_hash))
}
if block.header.version != block_version(block.header.height) {
return Err(Error::BlockIsInvalid(block_hash))
}
if block.txs.is_empty() {
return Err(Error::BlockContainsNoTransactions(block_hash))
}
let producer_tx = block.txs.last().unwrap();
if producer_tx != &Transaction::default() {
error!(target: "validator::verification::verify_genesis_block", "Genesis producer transaction is not default one");
return Err(TxVerifyFailed::ErroneousTxs(vec![producer_tx.clone()]).into())
}
let mut tree = MerkleTree::new(1);
let txs = &block.txs[..block.txs.len() - 1];
if let Err(e) =
verify_transactions(overlay, block.header.height, block_target, txs, &mut tree, false).await
{
warn!(
target: "validator::verification::verify_genesis_block",
"[VALIDATOR] Erroneous transactions found in set",
);
overlay.lock().unwrap().overlay.lock().unwrap().purge_new_trees()?;
return Err(e)
}
append_tx_to_merkle_tree(&mut tree, producer_tx);
if tree.root(0).unwrap() != block.header.root {
error!(target: "validator::verification::verify_genesis_block", "Genesis Merkle tree is invalid");
return Err(Error::BlockIsInvalid(block_hash))
}
overlay.lock().unwrap().add_block(block)?;
debug!(target: "validator::verification::verify_genesis_block", "Genesis block {} verified successfully", block_hash);
Ok(())
}
pub fn validate_block(block: &BlockInfo, previous: &BlockInfo, module: &PoWModule) -> Result<()> {
if block.header.version != block_version(block.header.height) {
return Err(Error::BlockIsInvalid(block.hash().as_string()))
}
if block.header.previous != previous.hash() {
return Err(Error::BlockIsInvalid(block.hash().as_string()))
}
if block.header.height != previous.header.height + 1 {
return Err(Error::BlockIsInvalid(block.hash().as_string()))
}
if !module.verify_timestamp_by_median(block.header.timestamp) {
return Err(Error::BlockIsInvalid(block.hash().as_string()))
}
module.verify_block_hash(block)?;
Ok(())
}
pub fn validate_blockchain(
blockchain: &Blockchain,
pow_target: u32,
pow_fixed_difficulty: Option<BigUint>,
) -> Result<()> {
let mut module = PoWModule::new(blockchain.clone(), pow_target, pow_fixed_difficulty, None)?;
let blocks = blockchain.blocks.get_all_order()?;
for (index, block) in blocks[1..].iter().enumerate() {
let full_blocks = blockchain.get_blocks_by_hash(&[blocks[index].1, block.1])?;
let full_block = &full_blocks[1];
validate_block(full_block, &full_blocks[0], &module)?;
module.append(full_block.header.timestamp, &module.next_difficulty()?);
}
Ok(())
}
pub async fn verify_block(
overlay: &BlockchainOverlayPtr,
module: &PoWModule,
block: &BlockInfo,
previous: &BlockInfo,
verify_fees: bool,
) -> Result<()> {
let block_hash = block.hash();
debug!(target: "validator::verification::verify_block", "Validating block {}", block_hash);
if overlay.lock().unwrap().has_block(block)? {
return Err(Error::BlockAlreadyExists(block_hash.as_string()))
}
validate_block(block, previous, module)?;
if block.txs.is_empty() {
return Err(Error::BlockContainsNoTransactions(block_hash.as_string()))
}
let mut tree = MerkleTree::new(1);
let txs = &block.txs[..block.txs.len() - 1];
let e = verify_transactions(
overlay,
block.header.height,
module.target,
txs,
&mut tree,
verify_fees,
)
.await;
if let Err(e) = e {
warn!(
target: "validator::verification::verify_block",
"[VALIDATOR] Erroneous transactions found in set",
);
overlay.lock().unwrap().overlay.lock().unwrap().purge_new_trees()?;
return Err(e)
}
let public_key = verify_producer_transaction(
overlay,
block.header.height,
module.target,
block.txs.last().unwrap(),
&mut tree,
)
.await?;
if tree.root(0).unwrap() != block.header.root {
error!(target: "validator::verification::verify_block", "Block Merkle tree root is invalid");
return Err(Error::BlockIsInvalid(block_hash.as_string()))
}
verify_producer_signature(block, &public_key)?;
overlay.lock().unwrap().add_block(block)?;
debug!(target: "validator::verification::verify_block", "Block {} verified successfully", block_hash);
Ok(())
}
pub async fn verify_checkpoint_block(
overlay: &BlockchainOverlayPtr,
block: &BlockInfo,
header: &HeaderHash,
block_target: u32,
) -> Result<()> {
let block_hash = block.hash();
debug!(target: "validator::verification::verify_checkpoint_block", "Validating block {}", block_hash);
if overlay.lock().unwrap().has_block(block)? {
return Err(Error::BlockAlreadyExists(block_hash.as_string()))
}
if block_hash != *header {
error!(target: "validator::verification::verify_checkpoint_block", "Block hash doesn't match the expected one");
return Err(Error::BlockIsInvalid(block_hash.as_string()))
}
if block.txs.is_empty() {
return Err(Error::BlockContainsNoTransactions(block_hash.as_string()))
}
let mut tree = MerkleTree::new(1);
let txs = &block.txs[..block.txs.len() - 1];
let e = apply_transactions(overlay, block.header.height, block_target, txs, &mut tree).await;
if let Err(e) = e {
warn!(
target: "validator::verification::verify_checkpoint_block",
"[VALIDATOR] Erroneous transactions found in set",
);
overlay.lock().unwrap().overlay.lock().unwrap().purge_new_trees()?;
return Err(e)
}
let public_key = apply_producer_transaction(
overlay,
block.header.height,
block_target,
block.txs.last().unwrap(),
&mut tree,
)
.await?;
if tree.root(0).unwrap() != block.header.root {
error!(target: "validator::verification::verify_checkpoint_block", "Block Merkle tree root is invalid");
return Err(Error::BlockIsInvalid(block_hash.as_string()))
}
verify_producer_signature(block, &public_key)?;
overlay.lock().unwrap().add_block(block)?;
debug!(target: "validator::verification::verify_checkpoint_block", "Block {} verified successfully", block_hash);
Ok(())
}
pub fn verify_producer_signature(block: &BlockInfo, public_key: &PublicKey) -> Result<()> {
if !public_key.verify(block.header.hash().inner(), &block.signature) {
warn!(target: "validator::verification::verify_producer_signature", "Proposer {} signature could not be verified", public_key);
return Err(Error::InvalidSignature)
}
Ok(())
}
pub async fn verify_producer_transaction(
overlay: &BlockchainOverlayPtr,
verifying_block_height: u32,
block_target: u32,
tx: &Transaction,
tree: &mut MerkleTree,
) -> Result<PublicKey> {
let tx_hash = tx.hash();
debug!(target: "validator::verification::verify_producer_transaction", "Validating producer transaction {}", tx_hash);
if !tx.is_pow_reward() {
return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
}
let call = &tx.calls[0];
let mut verifying_keys: HashMap<[u8; 32], HashMap<String, VerifyingKey>> = HashMap::new();
verifying_keys.insert(call.data.contract_id.to_bytes(), HashMap::new());
let mut zkp_table = vec![];
let mut sig_table = vec![];
debug!(target: "validator::verification::verify_producer_transaction", "Executing contract call");
let mut payload = vec![];
tx.calls.encode_async(&mut payload).await?; debug!(target: "validator::verification::verify_producer_transaction", "Instantiating WASM runtime");
let wasm = overlay.lock().unwrap().contracts.get(call.data.contract_id)?;
let mut runtime = Runtime::new(
&wasm,
overlay.clone(),
call.data.contract_id,
verifying_block_height,
block_target,
tx_hash,
0,
)?;
debug!(target: "validator::verification::verify_producer_transaction", "Executing \"metadata\" call");
let metadata = runtime.metadata(&payload)?;
let mut decoder = Cursor::new(&metadata);
let zkp_pub: Vec<(String, Vec<pallas::Base>)> =
AsyncDecodable::decode_async(&mut decoder).await?;
let sig_pub: Vec<PublicKey> = AsyncDecodable::decode_async(&mut decoder).await?;
if zkp_pub.len() != 1 || sig_pub.len() != 1 {
error!(target: "validator::verification::verify_producer_transaction", "Producer transaction contains multiple ZK proofs or signature public keys");
return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
}
debug!(target: "validator::verification::verify_producer_transaction", "Successfully executed \"metadata\" call");
debug!(target: "validator::verification::verify_producer_transaction", "Performing VerifyingKey lookups from the sled db");
for (zkas_ns, _) in &zkp_pub {
let inner_vk_map = verifying_keys.get_mut(&call.data.contract_id.to_bytes()).unwrap();
if inner_vk_map.contains_key(zkas_ns.as_str()) {
continue
}
let (_zkbin, vk) =
overlay.lock().unwrap().contracts.get_zkas(&call.data.contract_id, zkas_ns)?;
inner_vk_map.insert(zkas_ns.to_string(), vk);
}
zkp_table.push(zkp_pub);
let signature_public_key = *sig_pub.last().unwrap();
sig_table.push(sig_pub);
debug!(target: "validator::verification::verify_producer_transaction", "Executing \"exec\" call");
let state_update = runtime.exec(&payload)?;
debug!(target: "validator::verification::verify_producer_transaction", "Successfully executed \"exec\" call");
debug!(target: "validator::verification::verify_producer_transaction", "Executing \"apply\" call");
runtime.apply(&state_update)?;
debug!(target: "validator::verification::verify_producer_transaction", "Successfully executed \"apply\" call");
debug!(target: "validator::verification::verify_producer_transaction", "Verifying signatures for transaction {}", tx_hash);
if sig_table.len() != tx.signatures.len() {
error!(target: "validator::verification::verify_producer_transaction", "Incorrect number of signatures in tx {}", tx_hash);
return Err(TxVerifyFailed::MissingSignatures.into())
}
if let Err(e) = tx.verify_sigs(sig_table) {
error!(target: "validator::verification::verify_producer_transaction", "Signature verification for tx {} failed: {}", tx_hash, e);
return Err(TxVerifyFailed::InvalidSignature.into())
}
debug!(target: "validator::verification::verify_producer_transaction", "Signature verification successful");
debug!(target: "validator::verification::verify_producer_transaction", "Verifying ZK proofs for transaction {}", tx_hash);
if let Err(e) = tx.verify_zkps(&verifying_keys, zkp_table).await {
error!(target: "validator::verification::verify_producer_transaction", "ZK proof verification for tx {} failed: {}", tx_hash, e);
return Err(TxVerifyFailed::InvalidZkProof.into())
}
debug!(target: "validator::verification::verify_producer_transaction", "ZK proof verification successful");
append_tx_to_merkle_tree(tree, tx);
debug!(target: "validator::verification::verify_producer_transaction", "Producer transaction {} verified successfully", tx_hash);
Ok(signature_public_key)
}
async fn apply_producer_transaction(
overlay: &BlockchainOverlayPtr,
verifying_block_height: u32,
block_target: u32,
tx: &Transaction,
tree: &mut MerkleTree,
) -> Result<PublicKey> {
let tx_hash = tx.hash();
debug!(target: "validator::verification::apply_producer_transaction", "Applying producer transaction {}", tx_hash);
if !tx.is_single_call() {
return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
}
debug!(target: "validator::verification::apply_producer_transaction", "Executing contract call");
let mut payload = vec![];
tx.calls.encode_async(&mut payload).await?; debug!(target: "validator::verification::apply_producer_transaction", "Instantiating WASM runtime");
let call = &tx.calls[0];
let wasm = overlay.lock().unwrap().contracts.get(call.data.contract_id)?;
let mut runtime = Runtime::new(
&wasm,
overlay.clone(),
call.data.contract_id,
verifying_block_height,
block_target,
tx_hash,
0,
)?;
debug!(target: "validator::verification::apply_producer_transaction", "Executing \"metadata\" call");
let metadata = runtime.metadata(&payload)?;
let mut decoder = Cursor::new(&metadata);
let _: Vec<(String, Vec<pallas::Base>)> = AsyncDecodable::decode_async(&mut decoder).await?;
let sig_pub: Vec<PublicKey> = AsyncDecodable::decode_async(&mut decoder).await?;
if sig_pub.len() != 1 {
error!(target: "validator::verification::apply_producer_transaction", "Producer transaction contains multiple ZK proofs or signature public keys");
return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
}
let signature_public_key = *sig_pub.last().unwrap();
debug!(target: "validator::verification::apply_producer_transaction", "Executing \"exec\" call");
let state_update = runtime.exec(&payload)?;
debug!(target: "validator::verification::apply_producer_transaction", "Successfully executed \"exec\" call");
debug!(target: "validator::verification::apply_producer_transaction", "Executing \"apply\" call");
runtime.apply(&state_update)?;
debug!(target: "validator::verification::apply_producer_transaction", "Successfully executed \"apply\" call");
append_tx_to_merkle_tree(tree, tx);
debug!(target: "validator::verification::apply_producer_transaction", "Producer transaction {} executed successfully", tx_hash);
Ok(signature_public_key)
}
pub async fn verify_transaction(
overlay: &BlockchainOverlayPtr,
verifying_block_height: u32,
block_target: u32,
tx: &Transaction,
tree: &mut MerkleTree,
verifying_keys: &mut HashMap<[u8; 32], HashMap<String, VerifyingKey>>,
verify_fee: bool,
) -> Result<GasData> {
let tx_hash = tx.hash();
debug!(target: "validator::verification::verify_transaction", "Validating transaction {}", tx_hash);
let mut gas_data = GasData::default();
if verify_fee {
dark_forest_leaf_vec_integrity_check(
&tx.calls,
Some(MIN_TX_CALLS + 1),
Some(MAX_TX_CALLS),
)?;
} else {
dark_forest_leaf_vec_integrity_check(&tx.calls, Some(MIN_TX_CALLS), Some(MAX_TX_CALLS))?;
}
let mut zkp_table = vec![];
let mut sig_table = vec![];
let mut fee_call_idx = 0;
if verify_fee {
let mut found_fee = false;
for (call_idx, call) in tx.calls.iter().enumerate() {
if call.data.is_money_fee() {
found_fee = true;
fee_call_idx = call_idx;
break
}
}
if !found_fee {
error!(
target: "validator::verification::verify_transcation",
"[VALIDATOR] Transaction {} does not contain fee payment call", tx_hash,
);
return Err(TxVerifyFailed::InvalidFee.into())
}
}
let mut circuits_to_verify = vec![];
for (idx, call) in tx.calls.iter().enumerate() {
if call.data.is_money_pow_reward() {
error!(target: "validator::verification::verify_transaction", "Reward transaction detected");
return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
}
debug!(target: "validator::verification::verify_transaction", "Executing contract call {}", idx);
let mut payload = vec![];
tx.calls.encode_async(&mut payload).await?;
debug!(target: "validator::verification::verify_transaction", "Instantiating WASM runtime");
let wasm = overlay.lock().unwrap().contracts.get(call.data.contract_id)?;
let mut runtime = Runtime::new(
&wasm,
overlay.clone(),
call.data.contract_id,
verifying_block_height,
block_target,
tx_hash,
idx as u8,
)?;
debug!(target: "validator::verification::verify_transaction", "Executing \"metadata\" call");
let metadata = runtime.metadata(&payload)?;
let mut decoder = Cursor::new(&metadata);
let zkp_pub: Vec<(String, Vec<pallas::Base>)> =
AsyncDecodable::decode_async(&mut decoder).await?;
let sig_pub: Vec<PublicKey> = AsyncDecodable::decode_async(&mut decoder).await?;
if decoder.position() != metadata.len() as u64 {
error!(
target: "validator::verification::verify_transaction",
"[VALIDATOR] Failed decoding entire metadata buffer for {}:{}", tx_hash, idx,
);
return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
}
debug!(target: "validator::verification::verify_transaction", "Successfully executed \"metadata\" call");
debug!(target: "validator::verification::verify_transaction", "Performing VerifyingKey lookups from the sled db");
for (zkas_ns, _) in &zkp_pub {
let inner_vk_map = verifying_keys.get_mut(&call.data.contract_id.to_bytes()).unwrap();
if inner_vk_map.contains_key(zkas_ns.as_str()) {
continue
}
let (zkbin, vk) =
overlay.lock().unwrap().contracts.get_zkas(&call.data.contract_id, zkas_ns)?;
inner_vk_map.insert(zkas_ns.to_string(), vk);
circuits_to_verify.push(zkbin);
}
zkp_table.push(zkp_pub);
sig_table.push(sig_pub);
debug!(target: "validator::verification::verify_transaction", "Executing \"exec\" call");
let state_update = runtime.exec(&payload)?;
debug!(target: "validator::verification::verify_transaction", "Successfully executed \"exec\" call");
debug!(target: "validator::verification::verify_transaction", "Executing \"apply\" call");
runtime.apply(&state_update)?;
debug!(target: "validator::verification::verify_transaction", "Successfully executed \"apply\" call");
if call.data.is_deployment()
{
debug!(target: "validator::verification::verify_transaction", "Deploying new contract");
let deploy_params: DeployParamsV1 = deserialize_async(&call.data.data[1..]).await?;
let deploy_cid = ContractId::derive_public(deploy_params.public_key);
let mut deploy_runtime = Runtime::new(
&deploy_params.wasm_bincode,
overlay.clone(),
deploy_cid,
verifying_block_height,
block_target,
tx_hash,
idx as u8,
)?;
deploy_runtime.deploy(&deploy_params.ix)?;
let deploy_gas_used = deploy_runtime.gas_used();
debug!(target: "validator::verification::verify_transaction", "The gas used for deployment call {:?} of transaction {}: {}", call, tx_hash, deploy_gas_used);
gas_data.deployments += deploy_gas_used;
}
let wasm_gas_used = runtime.gas_used();
debug!(target: "validator::verification::verify_transaction", "The gas used for WASM call {:?} of transaction {}: {}", call, tx_hash, wasm_gas_used);
gas_data.wasm += wasm_gas_used;
}
gas_data.signatures = (PALLAS_SCHNORR_SIGNATURE_FEE * tx.signatures.len() as u64) +
serialize_async(tx).await.len() as u64;
debug!(target: "validator::verification::verify_transaction", "The gas used for signature of transaction {}: {}", tx_hash, gas_data.signatures);
for zkbin in circuits_to_verify.iter() {
let zk_circuit_gas_used = circuit_gas_use(zkbin);
debug!(target: "validator::verification::verify_transaction", "The gas used for ZK circuit in namespace {} of transaction {}: {}", zkbin.namespace, tx_hash, zk_circuit_gas_used);
gas_data.zk_circuits += zk_circuit_gas_used;
}
let total_gas_used = gas_data.total_gas_used();
if verify_fee {
let fee: u64 = match deserialize_async(&tx.calls[fee_call_idx].data.data[1..9]).await {
Ok(v) => v,
Err(e) => {
error!(
target: "validator::verification::verify_transaction",
"[VALIDATOR] Failed deserializing tx {} fee call: {}", tx_hash, e,
);
return Err(TxVerifyFailed::InvalidFee.into())
}
};
let required_fee = compute_fee(&total_gas_used);
if required_fee > fee {
error!(
target: "validator::verification::verify_transaction",
"[VALIDATOR] Transaction {} has insufficient fee. Required: {}, Paid: {}",
tx_hash, required_fee, fee,
);
return Err(TxVerifyFailed::InsufficientFee.into())
}
debug!(target: "validator::verification::verify_transaction", "The gas paid for transaction {}: {}", tx_hash, gas_data.paid);
gas_data.paid = fee;
}
debug!(target: "validator::verification::verify_transaction", "Verifying signatures for transaction {}", tx_hash);
if sig_table.len() != tx.signatures.len() {
error!(
target: "validator::verification::verify_transaction",
"[VALIDATOR] Incorrect number of signatures in tx {}", tx_hash,
);
return Err(TxVerifyFailed::MissingSignatures.into())
}
if let Err(e) = tx.verify_sigs(sig_table) {
error!(
target: "validator::verification::verify_transaction",
"[VALIDATOR] Signature verification for tx {} failed: {}", tx_hash, e,
);
return Err(TxVerifyFailed::InvalidSignature.into())
}
debug!(target: "validator::verification::verify_transaction", "Signature verification successful");
debug!(target: "validator::verification::verify_transaction", "Verifying ZK proofs for transaction {}", tx_hash);
if let Err(e) = tx.verify_zkps(verifying_keys, zkp_table).await {
error!(
target: "validator::verification::verify_transaction",
"[VALIDATOR] ZK proof verification for tx {} failed: {}", tx_hash, e,
);
return Err(TxVerifyFailed::InvalidZkProof.into())
}
debug!(target: "validator::verification::verify_transaction", "ZK proof verification successful");
append_tx_to_merkle_tree(tree, tx);
debug!(target: "validator::verification::verify_transaction", "The total gas used for transaction {}: {}", tx_hash, total_gas_used);
debug!(target: "validator::verification::verify_transaction", "Transaction {} verified successfully", tx_hash);
Ok(gas_data)
}
async fn apply_transaction(
overlay: &BlockchainOverlayPtr,
verifying_block_height: u32,
block_target: u32,
tx: &Transaction,
tree: &mut MerkleTree,
) -> Result<()> {
let tx_hash = tx.hash();
debug!(target: "validator::verification::apply_transaction", "Applying transaction {}", tx_hash);
for (idx, call) in tx.calls.iter().enumerate() {
debug!(target: "validator::verification::apply_transaction", "Executing contract call {}", idx);
let mut payload = vec![];
tx.calls.encode_async(&mut payload).await?;
debug!(target: "validator::verification::apply_transaction", "Instantiating WASM runtime");
let wasm = overlay.lock().unwrap().contracts.get(call.data.contract_id)?;
let mut runtime = Runtime::new(
&wasm,
overlay.clone(),
call.data.contract_id,
verifying_block_height,
block_target,
tx_hash,
idx as u8,
)?;
debug!(target: "validator::verification::apply_transaction", "Executing \"exec\" call");
let state_update = runtime.exec(&payload)?;
debug!(target: "validator::verification::apply_transaction", "Successfully executed \"exec\" call");
debug!(target: "validator::verification::apply_transaction", "Executing \"apply\" call");
runtime.apply(&state_update)?;
debug!(target: "validator::verification::apply_transaction", "Successfully executed \"apply\" call");
if call.data.is_deployment()
{
debug!(target: "validator::verification::apply_transaction", "Deploying new contract");
let deploy_params: DeployParamsV1 = deserialize_async(&call.data.data[1..]).await?;
let deploy_cid = ContractId::derive_public(deploy_params.public_key);
let mut deploy_runtime = Runtime::new(
&deploy_params.wasm_bincode,
overlay.clone(),
deploy_cid,
verifying_block_height,
block_target,
tx_hash,
idx as u8,
)?;
deploy_runtime.deploy(&deploy_params.ix)?;
}
}
append_tx_to_merkle_tree(tree, tx);
debug!(target: "validator::verification::apply_transaction", "Transaction {} applied successfully", tx_hash);
Ok(())
}
pub async fn verify_transactions(
overlay: &BlockchainOverlayPtr,
verifying_block_height: u32,
block_target: u32,
txs: &[Transaction],
tree: &mut MerkleTree,
verify_fees: bool,
) -> Result<(u64, u64)> {
debug!(target: "validator::verification::verify_transactions", "Verifying {} transactions", txs.len());
if txs.is_empty() {
return Ok((0, 0))
}
let mut erroneous_txs = vec![];
let mut total_gas_used = 0;
let mut total_gas_paid = 0;
let mut vks: HashMap<[u8; 32], HashMap<String, VerifyingKey>> = HashMap::new();
for tx in txs {
for call in &tx.calls {
vks.insert(call.data.contract_id.to_bytes(), HashMap::new());
}
}
for tx in txs {
overlay.lock().unwrap().checkpoint();
let gas_data = match verify_transaction(
overlay,
verifying_block_height,
block_target,
tx,
tree,
&mut vks,
verify_fees,
)
.await
{
Ok(gas_values) => gas_values,
Err(e) => {
warn!(target: "validator::verification::verify_transactions", "Transaction verification failed: {}", e);
erroneous_txs.push(tx.clone());
overlay.lock().unwrap().revert_to_checkpoint()?;
continue
}
};
let tx_gas_used = gas_data.total_gas_used();
let accumulated_gas_usage = total_gas_used + tx_gas_used;
if accumulated_gas_usage > GAS_LIMIT_UNPROPOSED_TXS {
warn!(target: "validator::verification::verify_transactions", "Transaction {} exceeds configured transaction gas limit: {} - {}", tx.hash(), accumulated_gas_usage, GAS_LIMIT_UNPROPOSED_TXS);
erroneous_txs.push(tx.clone());
overlay.lock().unwrap().revert_to_checkpoint()?;
break
}
total_gas_used += tx_gas_used;
total_gas_paid += gas_data.paid;
}
if !erroneous_txs.is_empty() {
return Err(TxVerifyFailed::ErroneousTxs(erroneous_txs).into())
}
Ok((total_gas_used, total_gas_paid))
}
async fn apply_transactions(
overlay: &BlockchainOverlayPtr,
verifying_block_height: u32,
block_target: u32,
txs: &[Transaction],
tree: &mut MerkleTree,
) -> Result<()> {
debug!(target: "validator::verification::apply_transactions", "Applying {} transactions", txs.len());
if txs.is_empty() {
return Ok(())
}
let mut erroneous_txs = vec![];
for tx in txs {
overlay.lock().unwrap().checkpoint();
if let Err(e) =
apply_transaction(overlay, verifying_block_height, block_target, tx, tree).await
{
warn!(target: "validator::verification::apply_transactions", "Transaction apply failed: {}", e);
erroneous_txs.push(tx.clone());
overlay.lock().unwrap().revert_to_checkpoint()?;
};
}
if !erroneous_txs.is_empty() {
return Err(TxVerifyFailed::ErroneousTxs(erroneous_txs).into())
}
Ok(())
}
pub async fn verify_proposal(
consensus: &Consensus,
proposal: &Proposal,
verify_fees: bool,
) -> Result<(Fork, Option<usize>)> {
let proposal_hash = proposal.block.hash();
if proposal.hash != proposal_hash {
warn!(
target: "validator::verification::verify_proposal", "Received proposal contains mismatched hashes: {} - {}",
proposal.hash, proposal_hash
);
return Err(Error::ProposalHashesMissmatchError)
}
let (fork, index) = consensus.find_extended_fork(proposal).await?;
let previous = fork.overlay.lock().unwrap().last_block()?;
if verify_block(&fork.overlay, &fork.module, &proposal.block, &previous, verify_fees)
.await
.is_err()
{
error!(target: "validator::verification::verify_proposal", "Erroneous proposal block found");
fork.overlay.lock().unwrap().overlay.lock().unwrap().purge_new_trees()?;
return Err(Error::BlockIsInvalid(proposal.hash.as_string()))
};
Ok((fork, index))
}
pub async fn verify_fork_proposal(
fork: &Fork,
proposal: &Proposal,
verify_fees: bool,
) -> Result<()> {
let proposal_hash = proposal.block.hash();
if proposal.hash != proposal_hash {
warn!(
target: "validator::verification::verify_fork_proposal", "Received proposal contains mismatched hashes: {} - {}",
proposal.hash, proposal_hash
);
return Err(Error::ProposalHashesMissmatchError)
}
let previous = fork.overlay.lock().unwrap().last_block()?;
if verify_block(&fork.overlay, &fork.module, &proposal.block, &previous, verify_fees)
.await
.is_err()
{
error!(target: "validator::verification::verify_fork_proposal", "Erroneous proposal block found");
fork.overlay.lock().unwrap().overlay.lock().unwrap().purge_new_trees()?;
return Err(Error::BlockIsInvalid(proposal.hash.as_string()))
};
Ok(())
}