From 0a199ca8857ca86a66ff4dba6cf1e45da7100e4a Mon Sep 17 00:00:00 2001 From: Green Baneling Date: Thu, 27 Jul 2023 19:23:07 +0200 Subject: [PATCH] Fixed wrong encoding of the `Mint` and `Burn` receipts. (#1264) Fixed wrong encoding of the `Mint` and `Burn` receipts. Added an integration test to decode all receipts. --- crates/client/assets/schema.sdl | 4 + crates/client/src/client.rs | 13 +++ crates/client/src/client/schema/tx.rs | 8 +- .../client/schema/tx/transparent_receipt.rs | 10 +- crates/fuel-core/src/schema/tx.rs | 9 ++ crates/fuel-core/src/schema/tx/receipt.rs | 108 +++++++++++++++++- tests/tests/tx.rs | 10 ++ xtask/Cargo.toml | 2 +- 8 files changed, 153 insertions(+), 11 deletions(-) diff --git a/crates/client/assets/schema.sdl b/crates/client/assets/schema.sdl index c4cdd671b00..9c2c55c3a14 100644 --- a/crates/client/assets/schema.sdl +++ b/crates/client/assets/schema.sdl @@ -650,6 +650,10 @@ type Query { """ estimatePredicates(tx: HexString!): Transaction! """ + Returns all possible receipts for test purposes. + """ + allReceipts: [Receipt!]! + """ Returns true when the GraphQL API is serving requests. """ health: Boolean! diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 340fb15aed6..49bca0b47e9 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -648,6 +648,19 @@ impl FuelClient { Ok(receipts) } + #[cfg(feature = "test-helpers")] + pub async fn all_receipts(&self) -> io::Result> { + let query = schema::tx::AllReceipts::build(()); + let receipts = self.query(query).await?.all_receipts; + + let vec: Result, ConversionError> = receipts + .into_iter() + .map(TryInto::::try_into) + .collect(); + + Ok(vec?) + } + pub async fn produce_blocks( &self, blocks_to_produce: u64, diff --git a/crates/client/src/client/schema/tx.rs b/crates/client/src/client/schema/tx.rs index 2ce3cda6c6a..ec5d80f15f6 100644 --- a/crates/client/src/client/schema/tx.rs +++ b/crates/client/src/client/schema/tx.rs @@ -284,7 +284,7 @@ pub struct DryRunArg { )] pub struct DryRun { #[arguments(tx: $tx, utxoValidation: $utxo_validation)] - pub dry_run: Vec, + pub dry_run: Vec, } #[derive(cynic::QueryFragment, Debug)] @@ -309,6 +309,12 @@ pub struct SubmitAndAwaitSubscription { pub submit_and_await: TransactionStatus, } +#[derive(cynic::QueryFragment, Debug)] +#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Query")] +pub struct AllReceipts { + pub all_receipts: Vec, +} + #[cfg(test)] pub mod tests { use super::*; diff --git a/crates/client/src/client/schema/tx/transparent_receipt.rs b/crates/client/src/client/schema/tx/transparent_receipt.rs index 7fce914998a..80cef373412 100644 --- a/crates/client/src/client/schema/tx/transparent_receipt.rs +++ b/crates/client/src/client/schema/tx/transparent_receipt.rs @@ -343,10 +343,7 @@ impl TryFrom for fuel_tx::Receipt { .sub_id .ok_or_else(|| MissingField("sub_id".to_string()))? .into(), - contract_id: schema - .contract_id - .ok_or_else(|| MissingField("contract_id".to_string()))? - .into(), + contract_id: schema.contract.map(|id| id.id.into()).unwrap_or_default(), val: schema .val .ok_or_else(|| MissingField("val".to_string()))? @@ -365,10 +362,7 @@ impl TryFrom for fuel_tx::Receipt { .sub_id .ok_or_else(|| MissingField("sub_id".to_string()))? .into(), - contract_id: schema - .contract_id - .ok_or_else(|| MissingField("contract_id".to_string()))? - .into(), + contract_id: schema.contract.map(|id| id.id.into()).unwrap_or_default(), val: schema .val .ok_or_else(|| MissingField("val".to_string()))? diff --git a/crates/fuel-core/src/schema/tx.rs b/crates/fuel-core/src/schema/tx.rs index bf25e700e69..52c7927fc03 100644 --- a/crates/fuel-core/src/schema/tx.rs +++ b/crates/fuel-core/src/schema/tx.rs @@ -214,6 +214,15 @@ impl TxQuery { tx, )) } + + #[cfg(feature = "test-helpers")] + /// Returns all possible receipts for test purposes. + async fn all_receipts(&self) -> Vec { + receipt::all_receipts() + .into_iter() + .map(Into::into) + .collect() + } } #[derive(Default)] diff --git a/crates/fuel-core/src/schema/tx/receipt.rs b/crates/fuel-core/src/schema/tx/receipt.rs index 5e6b0306035..b46d3bb46c2 100644 --- a/crates/fuel-core/src/schema/tx/receipt.rs +++ b/crates/fuel-core/src/schema/tx/receipt.rs @@ -20,7 +20,7 @@ use fuel_core_types::{ fuel_tx, }; -#[derive(Copy, Clone, Debug, Display, Enum, Eq, PartialEq)] +#[derive(Copy, Clone, Debug, Display, Enum, Eq, PartialEq, strum_macros::EnumIter)] pub enum ReceiptType { Call, Return, @@ -158,3 +158,109 @@ impl From for Receipt { Receipt(receipt) } } + +#[cfg(feature = "test-helpers")] +pub fn all_receipts() -> Vec { + use strum::IntoEnumIterator; + + let mut receipts = vec![]; + for variant in ReceiptType::iter() { + let receipt = match variant { + ReceiptType::Call => fuel_tx::Receipt::call( + fuel_tx::ContractId::from([1u8; 32]), + fuel_tx::ContractId::from([2u8; 32]), + 3, + fuel_tx::AssetId::from([3u8; 32]), + 5, + 6, + 7, + 8, + 9, + ), + ReceiptType::Return => { + fuel_tx::Receipt::ret(fuel_tx::ContractId::from([1u8; 32]), 2, 3, 4) + } + ReceiptType::ReturnData => fuel_tx::Receipt::return_data( + fuel_tx::ContractId::from([1u8; 32]), + 2, + 3, + 4, + vec![5; 30], + ), + ReceiptType::Panic => fuel_tx::Receipt::panic( + fuel_tx::ContractId::from([1u8; 32]), + fuel_core_types::fuel_asm::PanicInstruction::error( + fuel_core_types::fuel_asm::PanicReason::ArithmeticOverflow, + 2, + ), + 3, + 4, + ), + ReceiptType::Revert => { + fuel_tx::Receipt::revert(fuel_tx::ContractId::from([1u8; 32]), 2, 3, 4) + } + ReceiptType::Log => fuel_tx::Receipt::log( + fuel_tx::ContractId::from([1u8; 32]), + 2, + 3, + 4, + 5, + 6, + 7, + ), + ReceiptType::LogData => fuel_tx::Receipt::log_data( + fuel_tx::ContractId::from([1u8; 32]), + 2, + 3, + 4, + 5, + 6, + vec![7; 30], + ), + ReceiptType::Transfer => fuel_tx::Receipt::transfer( + fuel_tx::ContractId::from([1u8; 32]), + fuel_tx::ContractId::from([2u8; 32]), + 3, + fuel_tx::AssetId::from([4u8; 32]), + 5, + 6, + ), + ReceiptType::TransferOut => fuel_tx::Receipt::transfer_out( + fuel_tx::ContractId::from([1u8; 32]), + fuel_tx::Address::from([2u8; 32]), + 3, + fuel_tx::AssetId::from([4u8; 32]), + 5, + 6, + ), + ReceiptType::ScriptResult => fuel_tx::Receipt::script_result( + fuel_tx::ScriptExecutionResult::Success, + 1, + ), + ReceiptType::MessageOut => fuel_tx::Receipt::message_out( + &Default::default(), + 1, + fuel_tx::Address::from([2u8; 32]), + fuel_tx::Address::from([3u8; 32]), + 4, + vec![5; 30], + ), + ReceiptType::Mint => fuel_tx::Receipt::mint( + fuel_tx::Bytes32::from([1u8; 32]), + fuel_tx::ContractId::from([2u8; 32]), + 3, + 4, + 5, + ), + ReceiptType::Burn => fuel_tx::Receipt::burn( + fuel_tx::Bytes32::from([1u8; 32]), + fuel_tx::ContractId::from([2u8; 32]), + 3, + 4, + 5, + ), + }; + receipts.push(receipt); + } + receipts +} diff --git a/tests/tests/tx.rs b/tests/tests/tx.rs index 543b45227d0..39f8b492e59 100644 --- a/tests/tests/tx.rs +++ b/tests/tests/tx.rs @@ -2,6 +2,7 @@ use crate::helpers::TestContext; use fuel_core::{ database::Database, executor::Executor, + schema::tx::receipt::all_receipts, service::{ adapters::MaybeRelayerAdapter, Config, @@ -210,6 +211,15 @@ async fn receipts() { assert!(receipts.is_some()); } +#[tokio::test] +async fn receipts_decoding() { + let srv = FuelService::new_node(Config::local_node()).await.unwrap(); + let client = FuelClient::from(srv.bound_address); + + let actual_receipts = client.all_receipts().await.unwrap(); + assert_eq!(actual_receipts, all_receipts()) +} + #[tokio::test] async fn get_transaction_by_id() { // setup test data in the node diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 11ab7905473..13fe6011c1a 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] clap = { workspace = true, features = ["env", "derive"] } -fuel-core = { path = "../crates/fuel-core", default-features = false, features = ["dap"] } +fuel-core = { path = "../crates/fuel-core", default-features = false, features = ["dap", "test-helpers"] } [features] default = ["fuel-core/default"]