|
|
@@ -16,7 +16,7 @@
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
*/
|
|
|
|
|
|
-use std::collections::HashMap;
|
|
|
+use std::{collections::HashMap, sync::Arc};
|
|
|
|
|
|
use darkfi_sdk::{
|
|
|
blockchain::{block_version, compute_fee},
|
|
|
@@ -30,6 +30,7 @@ use darkfi_sdk::{
|
|
|
};
|
|
|
use darkfi_serial::{deserialize_async, serialize_async, AsyncDecodable, AsyncEncodable};
|
|
|
use num_bigint::BigUint;
|
|
|
+use parking_lot::Mutex;
|
|
|
use sled_overlay::SledDbOverlayStateDiff;
|
|
|
use smol::io::Cursor;
|
|
|
use tracing::{debug, error, warn};
|
|
|
@@ -40,7 +41,7 @@ use crate::{
|
|
|
Blockchain, BlockchainOverlayPtr, HeaderHash,
|
|
|
},
|
|
|
error::TxVerifyFailed,
|
|
|
- runtime::vm_runtime::Runtime,
|
|
|
+ runtime::vm_runtime::{Runtime, TxLocalState},
|
|
|
tx::{Transaction, MAX_TX_CALLS, MIN_TX_CALLS},
|
|
|
validator::{
|
|
|
consensus::{Consensus, Fork, Proposal, BLOCK_GAS_LIMIT},
|
|
|
@@ -447,9 +448,13 @@ pub async fn verify_producer_transaction(
|
|
|
debug!(target: "validator::verification::verify_producer_transaction", "Instantiating WASM runtime");
|
|
|
let wasm = overlay.lock().unwrap().contracts.get(call.data.contract_id)?;
|
|
|
|
|
|
+ // Create tx-local state. This lives through the entire tx.
|
|
|
+ let tx_local_state = Arc::new(Mutex::new(TxLocalState::new()));
|
|
|
+
|
|
|
let mut runtime = Runtime::new(
|
|
|
&wasm,
|
|
|
overlay.clone(),
|
|
|
+ tx_local_state,
|
|
|
call.data.contract_id,
|
|
|
verifying_block_height,
|
|
|
block_target,
|
|
|
@@ -574,9 +579,13 @@ pub async fn apply_producer_transaction(
|
|
|
let call = &tx.calls[0];
|
|
|
let wasm = overlay.lock().unwrap().contracts.get(call.data.contract_id)?;
|
|
|
|
|
|
+ // Create tx-local state. This lives through the entire tx.
|
|
|
+ let tx_local_state = Arc::new(Mutex::new(TxLocalState::new()));
|
|
|
+
|
|
|
let mut runtime = Runtime::new(
|
|
|
&wasm,
|
|
|
overlay.clone(),
|
|
|
+ tx_local_state,
|
|
|
call.data.contract_id,
|
|
|
verifying_block_height,
|
|
|
block_target,
|
|
|
@@ -705,6 +714,10 @@ pub async fn verify_transaction(
|
|
|
// calculate their verification cost.
|
|
|
let mut circuits_to_verify = vec![];
|
|
|
|
|
|
+ // Create the transaction-local state instance
|
|
|
+ // This state exists only during the single transaction verification.
|
|
|
+ let tx_local_state = Arc::new(Mutex::new(TxLocalState::new()));
|
|
|
+
|
|
|
// Iterate over all calls to get the metadata
|
|
|
for (idx, call) in tx.calls.iter().enumerate() {
|
|
|
debug!(target: "validator::verification::verify_transaction", "Executing contract call {idx}");
|
|
|
@@ -726,9 +739,11 @@ pub async fn verify_transaction(
|
|
|
|
|
|
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(),
|
|
|
+ tx_local_state.clone(),
|
|
|
call.data.contract_id,
|
|
|
verifying_block_height,
|
|
|
block_target,
|
|
|
@@ -813,6 +828,7 @@ pub async fn verify_transaction(
|
|
|
let mut deploy_runtime = Runtime::new(
|
|
|
&deploy_params.wasm_bincode,
|
|
|
overlay.clone(),
|
|
|
+ tx_local_state.clone(),
|
|
|
deploy_cid,
|
|
|
verifying_block_height,
|
|
|
block_target,
|
|
|
@@ -943,15 +959,20 @@ pub async fn apply_transaction(
|
|
|
let mut payload = vec![];
|
|
|
tx.calls.encode_async(&mut payload).await?;
|
|
|
|
|
|
+ // Create tx-local state
|
|
|
+ let tx_local_state = Arc::new(Mutex::new(TxLocalState::new()));
|
|
|
+
|
|
|
// Iterate over all calls to get the metadata
|
|
|
for (idx, call) in tx.calls.iter().enumerate() {
|
|
|
debug!(target: "validator::verification::apply_transaction", "Executing contract call {idx}");
|
|
|
|
|
|
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(),
|
|
|
+ tx_local_state.clone(),
|
|
|
call.data.contract_id,
|
|
|
verifying_block_height,
|
|
|
block_target,
|
|
|
@@ -987,6 +1008,7 @@ pub async fn apply_transaction(
|
|
|
let mut deploy_runtime = Runtime::new(
|
|
|
&deploy_params.wasm_bincode,
|
|
|
overlay.clone(),
|
|
|
+ tx_local_state.clone(),
|
|
|
deploy_cid,
|
|
|
verifying_block_height,
|
|
|
block_target,
|