Skip to content

Commit

Permalink
Update for new StorageError approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal committed Sep 21, 2023
1 parent 375579c commit 7851aec
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 317 deletions.
583 changes: 302 additions & 281 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,5 @@ tempfile = "3.4"
tikv-jemallocator = "0.5"


[patch.crates-io]
fuel-vm = { git = "https://github.com/FuelLabs/fuel-vm", branch = "dento/nostd"}
2 changes: 1 addition & 1 deletion benches/benches/block_target_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn run(
.gas_limit(TARGET_BLOCK_GAS_LIMIT - BASE)
.gas_price(1)
.add_unsigned_coin_input(
rng.gen(),
SecretKey::random(&mut rng),
rng.gen(),
u64::MAX,
AssetId::BASE,
Expand Down
2 changes: 1 addition & 1 deletion benches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl TryFrom<VmBench> for VmBenchPrepared {
let PrepareCall { ra, rb, rc, rd } = p;

vm.prepare_call(ra, rb, rc, rd)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
for instruction in post_call {
vm.instruction(instruction).unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion crates/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl From<TryFromSliceError> for Error {

impl From<Error> for ExecutorError {
fn from(e: Error) -> Self {
ExecutorError::StorageError(Box::new(StorageError::from(e)))
ExecutorError::StorageError(anyhow::anyhow!(StorageError::from(e)))
}
}

Expand Down
25 changes: 16 additions & 9 deletions crates/fuel-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ use fuel_core_types::{
state::StateTransition,
Backtrace as FuelBacktrace,
Interpreter,
InterpreterError,
},
services::{
block_producer::Components,
Expand Down Expand Up @@ -843,7 +844,7 @@ where
let vm_result: StateTransition<_> = vm
.transact(checked_tx.clone())
.map_err(|error| ExecutorError::VmExecution {
error,
error: InterpreterError::Storage(anyhow::anyhow!(error)),
transaction_id: tx_id,
})?
.into();
Expand Down Expand Up @@ -2387,7 +2388,7 @@ mod tests {
let gas_price = 1;
let tx = TransactionBuilder::script(vec![], vec![])
.add_unsigned_coin_input(
rng.gen(),
SecretKey::random(&mut rng),
rng.gen(),
rng.gen(),
rng.gen(),
Expand Down Expand Up @@ -3692,7 +3693,13 @@ mod tests {
/// Helper to build transactions and a message in it for some of the message tests
fn make_tx_and_message(rng: &mut StdRng, da_height: u64) -> (Transaction, Message) {
let tx = TransactionBuilder::script(vec![], vec![])
.add_unsigned_message_input(rng.gen(), rng.gen(), rng.gen(), 1000, vec![])
.add_unsigned_message_input(
SecretKey::random(rng),
rng.gen(),
rng.gen(),
1000,
vec![],
)
.finalize();

let message = message_from_input(&tx.inputs()[0], da_height);
Expand Down Expand Up @@ -3758,9 +3765,9 @@ mod tests {

let tx = TransactionBuilder::script(vec![], vec![])
// Add `Input::MessageCoin`
.add_unsigned_message_input(rng.gen(), rng.gen(), rng.gen(), amount, vec![])
.add_unsigned_message_input(SecretKey::random(&mut rng), rng.gen(), rng.gen(), amount, vec![])
// Add `Input::MessageData`
.add_unsigned_message_input(rng.gen(), rng.gen(), rng.gen(), amount, vec![0xff; 10])
.add_unsigned_message_input(SecretKey::random(&mut rng), rng.gen(), rng.gen(), amount, vec![0xff; 10])
.add_output(Output::change(to, amount + amount, AssetId::BASE))
.finalize();
let tx_id = tx.id(&ChainId::default());
Expand Down Expand Up @@ -3821,9 +3828,9 @@ mod tests {
let script = vec![op::ret(1)].into_iter().collect();
let tx = TransactionBuilder::script(script, vec![])
// Add `Input::MessageCoin`
.add_unsigned_message_input(rng.gen(), rng.gen(), rng.gen(), amount, vec![])
.add_unsigned_message_input(SecretKey::random(&mut rng), rng.gen(), rng.gen(), amount, vec![])
// Add `Input::MessageData`
.add_unsigned_message_input(rng.gen(), rng.gen(), rng.gen(), amount, vec![0xff; 10])
.add_unsigned_message_input(SecretKey::random(&mut rng), rng.gen(), rng.gen(), amount, vec![0xff; 10])
.add_output(Output::change(to, amount + amount, AssetId::BASE))
.finalize();
let tx_id = tx.id(&ChainId::default());
Expand Down Expand Up @@ -4072,7 +4079,7 @@ mod tests {
let tx = TransactionBuilder::script(script.into_iter().collect(), vec![])
.gas_limit(10000)
.add_unsigned_coin_input(
rng.gen(),
SecretKey::random(&mut rng),
rng.gen(),
1000,
base_asset_id,
Expand Down Expand Up @@ -4150,7 +4157,7 @@ mod tests {
let tx = TransactionBuilder::script(script.into_iter().collect(), vec![])
.gas_limit(10000)
.add_unsigned_coin_input(
rng.gen(),
SecretKey::random(&mut rng),
rng.gen(),
1000,
base_asset_id,
Expand Down
14 changes: 3 additions & 11 deletions crates/fuel-core/src/schema/dap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,19 @@ impl ConcreteStorage {
let mut vm = Interpreter::with_storage(vm_database, (&self.params).into());
vm.transact(checked_tx)?;
self.vm.insert(id.clone(), vm).ok_or_else(|| {
InterpreterError::Io(io::Error::new(
io::ErrorKind::NotFound,
"The VM instance was not found",
))
io::Error::new(io::ErrorKind::NotFound, "The VM instance was not found")
})?;
self.db.insert(id.clone(), storage);
Ok(())
}

pub fn exec(&mut self, id: &ID, op: Instruction) -> Result<(), InterpreterError> {
pub fn exec(&mut self, id: &ID, op: Instruction) -> anyhow::Result<()> {
self.vm
.get_mut(id)
.map(|vm| vm.instruction(op))
.transpose()?
.map(|_| ())
.ok_or_else(|| {
InterpreterError::Io(io::Error::new(
io::ErrorKind::NotFound,
"The VM instance was not found",
))
})
.ok_or_else(|| anyhow::anyhow!("The VM instance was not found"))
}

fn vm_database(storage: &DatabaseTransaction) -> anyhow::Result<VmDatabase> {
Expand Down
7 changes: 5 additions & 2 deletions crates/services/txpool/src/transaction_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ mod tests {
Output,
TransactionBuilder,
},
fuel_vm::checked_transaction::builder::TransactionBuilderExt,
fuel_vm::{
checked_transaction::builder::TransactionBuilderExt,
SecretKey,
},
};
use itertools::Itertools;
use std::sync::Arc;
Expand Down Expand Up @@ -85,7 +88,7 @@ mod tests {
.gas_price(tx_gas.price)
.gas_limit(tx_gas.limit)
.add_unsigned_coin_input(
rng.gen(),
SecretKey::random(&mut rng),
rng.gen(),
1_000_000,
Default::default(),
Expand Down
14 changes: 13 additions & 1 deletion crates/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@ impl From<Error> for std::io::Error {

impl From<Error> for ExecutorError {
fn from(e: Error) -> Self {
ExecutorError::StorageError(Box::new(e))
ExecutorError::StorageError(anyhow::anyhow!(e))
}
}

impl From<Error> for fuel_vm_private::prelude::InterpreterError<Error> {
fn from(e: Error) -> Self {
fuel_vm_private::prelude::InterpreterError::Storage(e)
}
}

impl From<Error> for fuel_vm_private::prelude::RuntimeError<Error> {
fn from(e: Error) -> Self {
fuel_vm_private::prelude::RuntimeError::Storage(e)
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/types/src/services/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,14 @@ pub enum Error {
// TODO: Replace with `fuel_core_storage::Error` when execution error will live in the
// `fuel-core-executor`.
#[error("got error during work with storage {0}")]
StorageError(Box<dyn StdError + Send + Sync>),
StorageError(anyhow::Error),
#[error("got error during work with relayer {0}")]
RelayerError(Box<dyn StdError + Send + Sync>),
#[error("Transaction({transaction_id:#x}) execution error: {error:?}")]
VmExecution {
error: InterpreterError,
// TODO: Replace with `fuel_core_storage::Error` when execution error will live in the
// `fuel-core-executor`.
error: InterpreterError<anyhow::Error>,
transaction_id: Bytes32,
},
#[error(transparent)]
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ mod coin {
let mut rng = StdRng::seed_from_u64(1234);
let asset_id_a: AssetId = rng.gen();
let asset_id_b: AssetId = rng.gen();
let secret_key: SecretKey = rng.gen();
let secret_key: SecretKey = SecretKey::random(&mut rng);
let pk = secret_key.public_key();
let owner = Input::owner(&pk);
let context = setup(owner, asset_id_a, asset_id_b).await;
Expand Down Expand Up @@ -344,7 +344,7 @@ mod message_coin {
async fn excludes_spent_coins() {
let mut rng = StdRng::seed_from_u64(1234);

let secret_key: SecretKey = rng.gen();
let secret_key: SecretKey = SecretKey::random(&mut rng);
let pk = secret_key.public_key();
let owner = Input::owner(&pk);
let (base_asset_id, context) = setup(owner).await;
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/messages/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::{
async fn can_submit_genesis_message() {
let mut rng = StdRng::seed_from_u64(1234);

let secret_key: SecretKey = rng.gen();
let secret_key: SecretKey = SecretKey::random(&mut rng);
let pk = secret_key.public_key();

let msg1 = MessageConfig {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async fn messages_are_spendable_after_relayer_is_synced() {
let contract_address = relayer_config.eth_v2_listening_contracts[0];

// setup a real spendable message
let secret_key: SecretKey = rng.gen();
let secret_key: SecretKey = SecretKey::random(&mut rng);
let pk = secret_key.public_key();
let recipient = Input::owner(&pk);
let sender = Address::zeroed();
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/trigger_integration/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async fn poa_interval_produces_nonempty_blocks_at_correct_rate() {
vec![],
)
.add_unsigned_coin_input(
rng.gen(),
SecretKey::random(&mut rng),
rng.gen(),
rng.gen(),
rng.gen(),
Expand Down
6 changes: 3 additions & 3 deletions tests/tests/tx/tx_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn tx_pointer_set_from_genesis_for_coin_and_contract_inputs() {
// setup genesis coin
let coin_tx_pointer = TxPointer::new(starting_block, rng.gen());
let coin_utxo_id: UtxoId = rng.gen();
let secret_key: SecretKey = rng.gen();
let secret_key: SecretKey = SecretKey::random(&mut rng);
let owner = Input::owner(&secret_key.public_key());
let amount = 1000;

Expand Down Expand Up @@ -116,7 +116,7 @@ async fn tx_pointer_set_from_previous_block() {

// setup genesis coin
let coin_utxo_id: UtxoId = rng.gen();
let secret_key: SecretKey = rng.gen();
let secret_key: SecretKey = SecretKey::random(&mut rng);
let owner = Input::owner(&secret_key.public_key());
let amount = 1000;

Expand Down Expand Up @@ -191,7 +191,7 @@ async fn tx_pointer_unset_when_utxo_validation_disabled() {
test_builder.starting_block = Some(block_height.into());
test_builder.utxo_validation = false;

let secret_key: SecretKey = rng.gen();
let secret_key: SecretKey = SecretKey::random(&mut rng);
let script = script_tx(secret_key, 1000, Default::default(), contract_id, rng.gen());

let TestContext {
Expand Down

0 comments on commit 7851aec

Please sign in to comment.