Skip to content

Commit 2bd308b

Browse files
authored
Tests for parallel executor (#2967)
## Linked Issues/PRs Resolve #2951 ## Description All tests needed to ensure the correct behavior of the parallel-executor ## Checklist - [ ] Breaking changes are clearly marked as such in the PR description and changelog - [ ] New behavior is reflected in tests - [ ] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [ ] I have reviewed the code myself - [ ] I have created follow-up issues caused by this PR and linked them here
1 parent eee60af commit 2bd308b

File tree

9 files changed

+970
-7
lines changed

9 files changed

+970
-7
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/services/parallel-executor/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ description = "Fuel Block Parallel Executor"
1414
wasm-executor = ["fuel-core-upgradable-executor/wasm-executor"]
1515

1616
[dependencies]
17+
fuel-core-executor = { workspace = true, features = ["std"] }
1718
fuel-core-storage = { workspace = true, features = ["std"] }
1819
fuel-core-types = { workspace = true, features = ["std"] }
1920
fuel-core-upgradable-executor = { workspace = true, features = ["std"] }
20-
tokio = { workspace = true, features = ["rt-multi-thread"] }
21+
futures = { workspace = true, features = ["std"] }
22+
tokio = { workspace = true, features = ["rt-multi-thread", "sync", "macros"] }
23+
24+
[dev-dependencies]
25+
anyhow = { workspace = true }
26+
fuel-core-storage = { workspace = true, features = ["test-helpers"] }
27+
fuel-core-types = { workspace = true, features = ["test-helpers", "serde"] }
28+
rand = { workspace = true }

crates/services/parallel-executor/src/executor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::config::Config;
1+
use crate::{
2+
config::Config,
3+
ports::TransactionsSource,
4+
};
25
use fuel_core_storage::transactional::Changes;
36
use fuel_core_types::{
47
blockchain::block::Block,
@@ -14,10 +17,7 @@ use fuel_core_types::{
1417
},
1518
},
1619
};
17-
use fuel_core_upgradable_executor::{
18-
executor::Executor as UpgradableExecutor,
19-
native_executor::ports::TransactionsSource,
20-
};
20+
use fuel_core_upgradable_executor::executor::Executor as UpgradableExecutor;
2121
use std::{
2222
num::NonZeroUsize,
2323
sync::{
@@ -77,7 +77,7 @@ impl<S, R> Executor<S, R> {
7777

7878
impl<S, R> Executor<S, R> {
7979
/// Produces the block and returns the result of the execution without committing the changes.
80-
pub fn produce_without_commit_with_source<TxSource>(
80+
pub async fn produce_without_commit_with_source<TxSource>(
8181
&self,
8282
_components: Components<TxSource>,
8383
) -> ExecutorResult<Uncommitted<ExecutionResult, Changes>>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
pub mod config;
22
pub mod executor;
3+
pub mod once_transaction_source;
4+
pub mod ports;
5+
6+
#[cfg(test)]
7+
mod tests;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::sync::Mutex;
2+
3+
use fuel_core_executor::ports::{
4+
MaybeCheckedTransaction,
5+
TransactionsSource as ExecutorTransactionsSource,
6+
};
7+
use fuel_core_types::fuel_vm::checked_transaction::CheckedTransaction;
8+
9+
use crate::ports::{
10+
TransactionFiltered,
11+
TransactionSourceExecutableTransactions,
12+
TransactionsSource,
13+
};
14+
15+
pub struct OnceTransactionsSource {
16+
transactions: Mutex<Vec<CheckedTransaction>>,
17+
consensus_parameters_version: u32,
18+
}
19+
20+
impl OnceTransactionsSource {
21+
pub fn new(
22+
transactions: Vec<CheckedTransaction>,
23+
consensus_parameters_version: u32,
24+
) -> Self {
25+
Self {
26+
transactions: Mutex::new(transactions),
27+
consensus_parameters_version,
28+
}
29+
}
30+
}
31+
32+
impl ExecutorTransactionsSource for OnceTransactionsSource {
33+
fn next(
34+
&self,
35+
_gas_limit: u64,
36+
transactions_limit: u16,
37+
_block_transaction_size_limit: u32,
38+
) -> Vec<fuel_core_executor::ports::MaybeCheckedTransaction> {
39+
let mut transactions = self.transactions.lock().expect("Mutex poisoned");
40+
// Avoid panicking if we request more transactions than there are in the vector
41+
let transactions_limit = (transactions_limit as usize).min(transactions.len());
42+
transactions
43+
.drain(..transactions_limit)
44+
.map(|tx| {
45+
MaybeCheckedTransaction::CheckedTransaction(
46+
tx,
47+
self.consensus_parameters_version,
48+
)
49+
})
50+
.collect()
51+
}
52+
}
53+
54+
impl TransactionsSource for OnceTransactionsSource {
55+
fn get_executable_transactions(
56+
&mut self,
57+
_gas_limit: u64,
58+
tx_count_limit: u16,
59+
_block_transaction_size_limit: u32,
60+
filter: crate::ports::Filter,
61+
) -> TransactionSourceExecutableTransactions {
62+
let mut transactions = self.transactions.lock().expect("Mutex poisoned");
63+
// Avoid panicking if we request more transactions than there are in the vector
64+
let transactions_limit = (tx_count_limit as usize).min(transactions.len());
65+
let txs = transactions.drain(..transactions_limit).collect();
66+
TransactionSourceExecutableTransactions {
67+
transactions: txs,
68+
filtered: TransactionFiltered::NotFiltered,
69+
filter,
70+
}
71+
}
72+
fn get_new_transactions_notifier(&mut self) -> tokio::sync::Notify {
73+
// This is a one-time source, so we don't need to notify about new transactions
74+
tokio::sync::Notify::new()
75+
}
76+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::collections::HashSet;
2+
3+
use fuel_core_storage::Result as StorageResult;
4+
use fuel_core_types::{
5+
blockchain::primitives::DaBlockHeight,
6+
entities::coins::coin::CompressedCoin,
7+
fuel_tx::{
8+
ConsensusParameters,
9+
ContractId,
10+
UtxoId,
11+
},
12+
fuel_types::BlockHeight,
13+
fuel_vm::checked_transaction::CheckedTransaction,
14+
};
15+
16+
#[derive(Debug, Clone, PartialEq, Eq)]
17+
pub enum TransactionFiltered {
18+
/// Some transactions were filtered out and so could be fetched in the future
19+
Filtered,
20+
/// No transactions were filtered out
21+
NotFiltered,
22+
}
23+
24+
#[derive(Debug, Clone, PartialEq, Eq)]
25+
pub struct Filter {
26+
/// The set of contract IDs to filter out
27+
pub excluded_contract_ids: HashSet<ContractId>,
28+
}
29+
30+
impl Filter {
31+
pub fn new(excluded_contract_ids: HashSet<ContractId>) -> Self {
32+
Self {
33+
excluded_contract_ids,
34+
}
35+
}
36+
}
37+
38+
pub struct TransactionSourceExecutableTransactions {
39+
/// The transactions that can be executed
40+
pub transactions: Vec<CheckedTransaction>,
41+
/// Indicates whether some transactions were filtered out based on the filter
42+
pub filtered: TransactionFiltered,
43+
/// The filter used to fetch these transactions
44+
pub filter: Filter,
45+
}
46+
47+
pub trait TransactionsSource {
48+
/// Returns the a batch of transactions to satisfy the given parameters
49+
fn get_executable_transactions(
50+
&mut self,
51+
gas_limit: u64,
52+
tx_count_limit: u16,
53+
block_transaction_size_limit: u32,
54+
filter: Filter,
55+
) -> TransactionSourceExecutableTransactions;
56+
57+
/// Returns a notification receiver for new transactions
58+
fn get_new_transactions_notifier(&mut self) -> tokio::sync::Notify;
59+
}
60+
61+
pub trait Storage {
62+
/// Get a coin by a UTXO
63+
fn get_coin(&self, utxo: &UtxoId) -> StorageResult<Option<CompressedCoin>>;
64+
65+
/// Get the DA block height based on provided height
66+
fn get_da_height_by_l2_height(
67+
&self,
68+
block_height: &BlockHeight,
69+
) -> StorageResult<Option<DaBlockHeight>>;
70+
71+
/// Get consensus parameters based on a version
72+
fn get_consensus_parameters(
73+
&self,
74+
consensus_parameters_version: u32,
75+
) -> StorageResult<ConsensusParameters>;
76+
}

0 commit comments

Comments
 (0)