From 9187c66d3518063aad464521c4b1e292c775eea1 Mon Sep 17 00:00:00 2001 From: xgreenx Date: Mon, 18 Mar 2024 12:02:23 +0100 Subject: [PATCH 1/3] Added tests to verify that the network operates with a custom chain id and base asset id --- crates/fuel-core/src/service/config.rs | 6 + tests/tests/chain.rs | 158 ++++++++++++++++++++++++- 2 files changed, 160 insertions(+), 4 deletions(-) diff --git a/crates/fuel-core/src/service/config.rs b/crates/fuel-core/src/service/config.rs index f81713bc582..a1d6f397671 100644 --- a/crates/fuel-core/src/service/config.rs +++ b/crates/fuel-core/src/service/config.rs @@ -144,6 +144,12 @@ impl Config { self.txpool.chain_config = self.chain_config.clone(); } + if self.block_importer.chain_id != self.chain_config.consensus_parameters.chain_id + { + tracing::warn!("The `ChainConfig` of `BlockImporter` was inconsistent"); + self.block_importer.chain_id = self.chain_config.consensus_parameters.chain_id + } + if self.txpool.utxo_validation != self.utxo_validation { tracing::warn!("The `utxo_validation` of `TxPool` was inconsistent"); self.txpool.utxo_validation = self.utxo_validation; diff --git a/tests/tests/chain.rs b/tests/tests/chain.rs index a8573f973c4..a7add04a87d 100644 --- a/tests/tests/chain.rs +++ b/tests/tests/chain.rs @@ -1,8 +1,34 @@ -use fuel_core::service::{ - Config, - FuelService, +use fuel_core::{ + chain_config::{ + CoinConfig, + StateConfig, + StateReader, + }, + service::{ + Config, + FuelService, + }, }; -use fuel_core_client::client::FuelClient; +use fuel_core_client::client::{ + types::{ + primitives::{ + AssetId, + UtxoId, + }, + TransactionStatus, + }, + FuelClient, +}; +use fuel_core_types::{ + fuel_crypto::SecretKey, + fuel_tx::{ + Input, + Output, + TransactionBuilder, + }, + fuel_types::ChainId, +}; +use rand::SeedableRng; #[tokio::test] async fn chain_info() { @@ -24,3 +50,127 @@ async fn chain_info() { chain_info.consensus_parameters.gas_costs ); } + +#[tokio::test] +async fn network_operates_with_non_zero_chain_id() { + let mut rng = rand::rngs::StdRng::seed_from_u64(0xBAADF00D); + let secret = SecretKey::random(&mut rng); + let amount = 10000; + let owner = Input::owner(&secret.public_key()); + let utxo_id = UtxoId::new([1; 32].into(), 0); + + let state_config = StateConfig { + coins: vec![CoinConfig { + tx_id: *utxo_id.tx_id(), + output_index: utxo_id.output_index(), + owner, + amount, + asset_id: AssetId::BASE, + ..Default::default() + }], + ..Default::default() + }; + let mut node_config = Config { + debug: true, + utxo_validation: true, + static_gas_price: 1, + state_reader: StateReader::in_memory(state_config), + ..Config::local_node() + }; + + // Given + let chain_id = ChainId::new(0xDEAD); + node_config.chain_config.consensus_parameters.chain_id = chain_id; + let srv = FuelService::new_node(node_config.clone()).await.unwrap(); + let client = FuelClient::from(srv.bound_address); + let script = TransactionBuilder::script(vec![], vec![]) + .with_chain_id(chain_id) + .max_fee_limit(amount) + .add_unsigned_coin_input( + secret, + utxo_id, + amount, + AssetId::BASE, + Default::default(), + ) + .add_output(Output::Change { + to: owner, + amount, + asset_id: AssetId::BASE, + }) + .finalize_as_transaction(); + + // When + let result = client + .submit_and_await_commit(&script) + .await + .expect("transaction should insert"); + + // Then + assert!(matches!(result, TransactionStatus::Success { .. })) +} + +#[tokio::test] +async fn network_operates_with_non_zero_base_asset_id() { + let mut rng = rand::rngs::StdRng::seed_from_u64(0xBAADF00D); + let secret = SecretKey::random(&mut rng); + let amount = 10000; + let owner = Input::owner(&secret.public_key()); + let utxo_id = UtxoId::new([1; 32].into(), 0); + + // Given + let new_base_asset_id = AssetId::new([6; 32]); + + let state_config = StateConfig { + coins: vec![CoinConfig { + tx_id: *utxo_id.tx_id(), + output_index: utxo_id.output_index(), + owner, + amount, + asset_id: new_base_asset_id, + ..Default::default() + }], + ..Default::default() + }; + let mut node_config = Config { + debug: true, + utxo_validation: true, + static_gas_price: 1, + state_reader: StateReader::in_memory(state_config), + ..Config::local_node() + }; + + node_config.chain_config.consensus_parameters.base_asset_id = new_base_asset_id; + let srv = FuelService::new_node(node_config.clone()).await.unwrap(); + let client = FuelClient::from(srv.bound_address); + let script = TransactionBuilder::script(vec![], vec![]) + .max_fee_limit(amount) + .add_unsigned_coin_input( + secret, + utxo_id, + amount, + new_base_asset_id, + Default::default(), + ) + .add_output(Output::Change { + to: owner, + amount, + asset_id: new_base_asset_id, + }) + .finalize_as_transaction(); + + // When + let result = client + .submit_and_await_commit(&script) + .await + .expect("transaction should insert"); + + // Then + let expected_fee = 1; + assert!(matches!(result, TransactionStatus::Success { .. })); + let balance = client + .balance(&owner, Some(&new_base_asset_id)) + .await + .expect("Should fetch hte balance"); + assert_eq!(balance, amount - expected_fee); +} From 6b420ce07f41ac72f3d97cda9d09cef70e54d501 Mon Sep 17 00:00:00 2001 From: xgreenx Date: Mon, 18 Mar 2024 12:04:49 +0100 Subject: [PATCH 2/3] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09a85806560..5144d932d88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Description of the upcoming release here. ### Added +- [#1760](https://github.com/FuelLabs/fuel-core/pull/1760): Added tests to verify that the network operates with a custom chain id and base asset id. - [#1752](https://github.com/FuelLabs/fuel-core/pull/1752): Add `ProducerGasPrice` trait that the `Producer` depends on to get the gas price for the block. - [#1747](https://github.com/FuelLabs/fuel-core/pull/1747): The DA block height is now included in the genesis state. - [#1740](https://github.com/FuelLabs/fuel-core/pull/1740): Remove optional fields from genesis configs From 2ee550275cce47bbb25c162858834148d8252db9 Mon Sep 17 00:00:00 2001 From: Brandon Vrooman Date: Mon, 18 Mar 2024 14:55:34 -0400 Subject: [PATCH 3/3] Small spelling fix --- tests/tests/chain.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests/chain.rs b/tests/tests/chain.rs index a7add04a87d..db610e9ac02 100644 --- a/tests/tests/chain.rs +++ b/tests/tests/chain.rs @@ -171,6 +171,6 @@ async fn network_operates_with_non_zero_base_asset_id() { let balance = client .balance(&owner, Some(&new_base_asset_id)) .await - .expect("Should fetch hte balance"); + .expect("Should fetch the balance"); assert_eq!(balance, amount - expected_fee); }