Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: gracefully handle unknown transaction variants #1499

Open
wants to merge 18 commits into
base: 0.67.0-dev2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async-trait = { version = "0.1.74", default-features = false }
bech32 = "0.9.1"
bytes = { version = "1.5.0", default-features = false }
chrono = "0.4.31"
cynic = { version = "2.2", default-features = false }
cynic = { version = "3.1.0", default-features = false }
elliptic-curve = { version = "0.13.8", default-features = false }
eth-keystore = "0.5.0"
flate2 = { version = "1.0", default-features = false }
Expand Down Expand Up @@ -113,3 +113,12 @@ fuels-macros = { version = "0.66.10", path = "./packages/fuels-macros", default-
fuels-programs = { version = "0.66.10", path = "./packages/fuels-programs", default-features = false }
fuels-test-helpers = { version = "0.66.10", path = "./packages/fuels-test-helpers", default-features = false }
versions-replacer = { version = "0.66.10", path = "./scripts/versions-replacer", default-features = false }

[patch.crates-io]
fuel-core = { git = "https://github.com/FuelLabs/fuel-core", branch = "hal3e/unknown-variants", package = "fuel-core"}
fuel-core-chain-config = { git = "https://github.com/FuelLabs/fuel-core", branch = "hal3e/unknown-variants", package = "fuel-core-chain-config"}
fuel-core-client = { git = "https://github.com/FuelLabs/fuel-core", branch = "hal3e/unknown-variants", package = "fuel-core-client"} # TODO: remove this @hal3e
fuel-core-poa = { git = "https://github.com/FuelLabs/fuel-core", branch = "hal3e/unknown-variants", package = "fuel-core-poa"}
fuel-core-services = { git = "https://github.com/FuelLabs/fuel-core", branch = "hal3e/unknown-variants", package = "fuel-core-services"}
fuel-core-types = { git = "https://github.com/FuelLabs/fuel-core", branch = "hal3e/unknown-variants", package = "fuel-core-types"}
fuel-core-wasm-executor = { git = "https://github.com/FuelLabs/fuel-core", branch = "hal3e/unknown-variants", package = "fuel-core-wasm-executor"}
3 changes: 1 addition & 2 deletions e2e/tests/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,7 @@ async fn predicate_transfer_to_base_layer() -> Result<()> {
let proof = predicate
.try_provider()?
.get_message_proof(&tx_id, &msg_nonce, None, Some(2))
.await?
.expect("failed to retrieve message proof");
.await?;

assert_eq!(proof.amount, amount);
assert_eq!(proof.recipient, base_layer_address);
Expand Down
3 changes: 1 addition & 2 deletions examples/wallets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ mod tests {
let proof = wallet
.try_provider()?
.get_message_proof(&tx_id, &msg_id, None, Some(2))
.await?
.expect("failed to retrieve message proof");
.await?;

// Verify the amount and recipient
assert_eq!(proof.amount, amount);
Expand Down
4 changes: 2 additions & 2 deletions packages/fuels-accounts/src/accounts_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ pub fn available_base_assets_and_amount(
amount, asset_id, ..
}) if asset_id == base_asset_id => {
sum += amount;
Some(resource.id())
resource.id()
}
CoinType::Message(message) => {
sum += message.amount;
Some(resource.id())
resource.id()
}
_ => None,
},
Expand Down
14 changes: 8 additions & 6 deletions packages/fuels-accounts/src/impersonated_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use std::fmt::Debug;

use async_trait::async_trait;
use fuel_crypto::{Message, Signature};
use fuels_core::traits::Signer;
use fuels_core::types::transaction_builders::TransactionBuilder;
use fuels_core::types::{bech32::Bech32Address, errors::Result};
use fuels_core::types::{coin_type_id::CoinTypeId, input::Input, AssetId};
use fuels_core::{
traits::Signer,
types::{
bech32::Bech32Address, coin_type_id::CoinTypeId, errors::Result, input::Input,
transaction_builders::TransactionBuilder, AssetId,
},
};

use crate::accounts_utils::try_provider_error;
use crate::{provider::Provider, Account, ViewOnlyAccount};
use crate::{accounts_utils::try_provider_error, provider::Provider, Account, ViewOnlyAccount};

/// A `ImpersonatedAccount` simulates ownership of assets held by an account with a given address.
/// `ImpersonatedAccount` will only succeed in unlocking assets if the the network is setup with
Expand Down
16 changes: 7 additions & 9 deletions packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ impl Provider {
.await?
.into_iter()
.flatten()
.map(CoinType::try_from)
.collect::<Result<Vec<CoinType>>>()?;
.map(CoinType::from)
.collect();

Ok(res)
}
Expand Down Expand Up @@ -696,19 +696,17 @@ impl Provider {
nonce: &Nonce,
commit_block_id: Option<&Bytes32>,
commit_block_height: Option<u32>,
) -> Result<Option<MessageProof>> {
let proof = self
.client
) -> Result<MessageProof> {
self.client
.message_proof(
tx_id,
nonce,
commit_block_id.map(Into::into),
commit_block_height.map(Into::into),
)
.await?
.map(Into::into);

Ok(proof)
.await
.map(Into::into)
.map_err(Into::into)
}

pub async fn is_user_account(&self, address: impl Into<Bytes32>) -> Result<bool> {
Expand Down
4 changes: 2 additions & 2 deletions packages/fuels-accounts/src/provider/retryable_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl RetryableClient {
nonce: &Nonce,
commit_block_id: Option<&BlockId>,
commit_block_height: Option<BlockHeight>,
) -> RequestResult<Option<MessageProof>> {
) -> RequestResult<MessageProof> {
self.wrap(|| {
self.client
.message_proof(transaction_id, nonce, commit_block_id, commit_block_height)
Expand Down Expand Up @@ -350,7 +350,7 @@ mod custom_queries {
use fuel_core_client::client::schema::blob::BlobIdFragment;
use fuel_core_client::client::schema::schema;
use fuel_core_client::client::schema::{
contract::{ContractByIdArgs, ContractIdFragment},
contract::{ContractByIdArgsFields, ContractIdFragment},
tx::TransactionIdFragment,
BlobId, ContractId, TransactionId,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-accounts/src/schema/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ type Query {
"""
owner: Address, first: Int, after: String, last: Int, before: String
): MessageConnection!
messageProof(transactionId: TransactionId!, nonce: Nonce!, commitBlockId: BlockId, commitBlockHeight: U32): MessageProof
messageProof(transactionId: TransactionId!, nonce: Nonce!, commitBlockId: BlockId, commitBlockHeight: U32): MessageProof!
messageStatus(nonce: Nonce!): MessageStatus!
relayedTransactionStatus(
"""
Expand Down
22 changes: 17 additions & 5 deletions packages/fuels-core/src/types/transaction_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ macro_rules! impl_tx_builder_trait {
.inputs()
.iter()
.filter_map(|input| match input {
Input::ResourceSigned { resource } => Some(resource.owner()),
Input::ResourceSigned { resource } => resource.owner(),
_ => None,
})
.unique()
Expand Down Expand Up @@ -1305,7 +1305,7 @@ fn resolve_fuel_inputs(
resource,
code,
data,
} => Ok(resolve_predicate_resource(resource, code, data)),
} => resolve_predicate_resource(resource, code, data),
Input::Contract {
utxo_id,
balance_root,
Expand Down Expand Up @@ -1357,13 +1357,25 @@ fn resolve_signed_resource(
create_coin_message_input(message, num_witnesses + *witness_idx_offset as u16)
})
}
CoinType::Unknown => Err(error_transaction!(
Builder,
"can not resolve `CoinType::Unknown`"
)),
}
}

fn resolve_predicate_resource(resource: CoinType, code: Vec<u8>, data: Vec<u8>) -> FuelInput {
fn resolve_predicate_resource(
resource: CoinType,
code: Vec<u8>,
data: Vec<u8>,
) -> Result<FuelInput> {
match resource {
CoinType::Coin(coin) => create_coin_predicate(coin.asset_id, coin, code, data),
CoinType::Message(message) => create_coin_message_predicate(message, code, data),
CoinType::Coin(coin) => Ok(create_coin_predicate(coin.asset_id, coin, code, data)),
CoinType::Message(message) => Ok(create_coin_message_predicate(message, code, data)),
CoinType::Unknown => Err(error_transaction!(
Builder,
"can not resolve `CoinType::Unknown`"
)),
}
}

Expand Down
36 changes: 17 additions & 19 deletions packages/fuels-core/src/types/wrappers/coin_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,56 @@
use fuel_core_client::client::types::CoinType as ClientCoinType;

use crate::types::{
bech32::Bech32Address,
coin::Coin,
coin_type_id::CoinTypeId,
errors::{error, Error},
message::Message,
AssetId,
bech32::Bech32Address, coin::Coin, coin_type_id::CoinTypeId, message::Message, AssetId,
};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CoinType {
Coin(Coin),
Message(Message),
Unknown,
}

impl TryFrom<ClientCoinType> for CoinType {
type Error = Error;

fn try_from(client_resource: ClientCoinType) -> Result<Self, Self::Error> {
impl From<ClientCoinType> for CoinType {
fn from(client_resource: ClientCoinType) -> Self {
match client_resource {
ClientCoinType::Coin(coin) => Ok(CoinType::Coin(coin.into())),
ClientCoinType::MessageCoin(message) => Ok(CoinType::Message(message.into())),
ClientCoinType::Unknown => Err(error!(Other, "unknown `ClientCoinType`")),
ClientCoinType::Coin(coin) => CoinType::Coin(coin.into()),
ClientCoinType::MessageCoin(message) => CoinType::Message(message.into()),
ClientCoinType::Unknown => CoinType::Unknown,
}
}
}

impl CoinType {
pub fn id(&self) -> CoinTypeId {
pub fn id(&self) -> Option<CoinTypeId> {
match self {
CoinType::Coin(coin) => CoinTypeId::UtxoId(coin.utxo_id),
CoinType::Message(message) => CoinTypeId::Nonce(message.nonce),
CoinType::Coin(coin) => Some(CoinTypeId::UtxoId(coin.utxo_id)),
CoinType::Message(message) => Some(CoinTypeId::Nonce(message.nonce)),
CoinType::Unknown => None,
}
}

pub fn amount(&self) -> u64 {
match self {
CoinType::Coin(coin) => coin.amount,
CoinType::Message(message) => message.amount,
CoinType::Unknown => 0,
}
}

pub fn coin_asset_id(&self) -> Option<AssetId> {
match self {
CoinType::Coin(coin) => Some(coin.asset_id),
CoinType::Message(_) => None,
CoinType::Unknown => None,
}
}

pub fn owner(&self) -> &Bech32Address {
pub fn owner(&self) -> Option<&Bech32Address> {
match self {
CoinType::Coin(coin) => &coin.owner,
CoinType::Message(message) => &message.recipient,
CoinType::Coin(coin) => Some(&coin.owner),
CoinType::Message(message) => Some(&message.recipient),
CoinType::Unknown => None,
}
}
}
19 changes: 11 additions & 8 deletions packages/fuels-core/src/types/wrappers/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub enum TransactionType {
Upload(UploadTransaction),
Upgrade(UpgradeTransaction),
Blob(BlobTransaction),
Unknown,
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
Expand Down Expand Up @@ -284,15 +285,17 @@ pub trait Transaction:
) -> Result<Signature>;
}

impl From<TransactionType> for FuelTransaction {
fn from(value: TransactionType) -> Self {
impl TryFrom<TransactionType> for FuelTransaction {
type Error = Error;
fn try_from(value: TransactionType) -> Result<Self> {
match value {
TransactionType::Script(tx) => tx.into(),
TransactionType::Create(tx) => tx.into(),
TransactionType::Mint(tx) => tx.into(),
TransactionType::Upload(tx) => tx.into(),
TransactionType::Upgrade(tx) => tx.into(),
TransactionType::Blob(tx) => tx.into(),
TransactionType::Script(tx) => Ok(tx.into()),
TransactionType::Create(tx) => Ok(tx.into()),
TransactionType::Mint(tx) => Ok(tx.into()),
TransactionType::Upload(tx) => Ok(tx.into()),
TransactionType::Upgrade(tx) => Ok(tx.into()),
TransactionType::Blob(tx) => Ok(tx.into()),
TransactionType::Unknown => Err(error_transaction!(Other, "`Unknown` transaction")),
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions packages/fuels-core/src/types/wrappers/transaction_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
use chrono::{DateTime, Utc};
use fuel_core_client::client::types::{
TransactionResponse as ClientTransactionResponse, TransactionStatus as ClientTransactionStatus,
TransactionType as ClientTxType,
};
use fuel_tx::Transaction;
use fuel_types::BlockHeight;

use crate::types::{
transaction::{CreateTransaction, ScriptTransaction, TransactionType},
tx_status::TxStatus,
};
use crate::types::{transaction::TransactionType, tx_status::TxStatus};

#[derive(Debug, Clone)]
pub struct TransactionResponse {
Expand Down Expand Up @@ -39,12 +37,13 @@ impl From<ClientTransactionResponse> for TransactionResponse {
};

let transaction = match client_response.transaction {
Transaction::Script(tx) => TransactionType::Script(ScriptTransaction::from(tx)),
Transaction::Create(tx) => TransactionType::Create(CreateTransaction::from(tx)),
Transaction::Mint(tx) => TransactionType::Mint(tx.into()),
Transaction::Upgrade(tx) => TransactionType::Upgrade(tx.into()),
Transaction::Upload(tx) => TransactionType::Upload(tx.into()),
Transaction::Blob(tx) => TransactionType::Blob(tx.into()),
ClientTxType::Known(Transaction::Script(tx)) => TransactionType::Script(tx.into()),
ClientTxType::Known(Transaction::Create(tx)) => TransactionType::Create(tx.into()),
ClientTxType::Known(Transaction::Mint(tx)) => TransactionType::Mint(tx.into()),
ClientTxType::Known(Transaction::Upgrade(tx)) => TransactionType::Upgrade(tx.into()),
ClientTxType::Known(Transaction::Upload(tx)) => TransactionType::Upload(tx.into()),
ClientTxType::Known(Transaction::Blob(tx)) => TransactionType::Blob(tx.into()),
ClientTxType::Unknown => TransactionType::Unknown,
};

Self {
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-test-helpers/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ mod tests {
assert_eq!(&coin.owner, wallet.address())
}
CoinType::Message(_) => panic!("resources contained messages"),
CoinType::Unknown => panic!("resources contained unknown coins"),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/fuels-test-helpers/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ impl FuelService {
database_type: node_config.database_type.into(),
#[cfg(feature = "rocksdb")]
state_rewind_policy: Default::default(),
#[cfg(feature = "rocksdb")]
max_fds: 512,
};
ServiceConfig {
graphql_config: GraphQLConfig {
Expand Down
Loading