Skip to content

Commit

Permalink
Avoid possible truncation of higher bits
Browse files Browse the repository at this point in the history
  • Loading branch information
xgreenx committed Oct 30, 2023
1 parent 0ecc88b commit b80ea61
Show file tree
Hide file tree
Showing 58 changed files with 134 additions and 153 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion benches/src/bin/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn main() {
let mut reader = readers.next().unwrap();
while let Err(TryRecvError::Empty) = rx.try_recv() {
match reader.read_line(&mut line) {
Ok(amount) if amount == 0 => {
Ok(0) => {
reader = match readers.next() {
Some(r) => r,
None => break,
Expand Down
2 changes: 1 addition & 1 deletion benches/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn provision_import_test(
) {
let shared_notify = Arc::new(Notify::new());
let params = Config {
header_batch_size,
header_batch_size: header_batch_size as usize,
block_stream_buffer_size,
};
let p2p = Arc::new(PressurePeerToPeer::new(
Expand Down
2 changes: 1 addition & 1 deletion bin/fuel-core/src/cli/run/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl From<SyncArgs> for fuel_core::sync::Config {
fn from(value: SyncArgs) -> Self {
Self {
block_stream_buffer_size: value.block_stream_buffer_size,
header_batch_size: value.header_batch_size,
header_batch_size: value.header_batch_size as usize,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/chain-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![deny(clippy::cast_possible_truncation)]
#![deny(unused_crate_dependencies)]
#![deny(warnings)]

Expand Down
2 changes: 1 addition & 1 deletion crates/client/src/client/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct PaginationRequest<T> {
/// The cursor returned from a previous query to indicate an offset
pub cursor: Option<T>,
/// The number of results to take
pub results: usize,
pub results: i32,
/// The direction of the query (e.g. asc, desc order).
pub direction: PageDirection,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/client/src/client/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,14 @@ impl<T: Into<String>> From<PaginationRequest<T>> for ConnectionArgs {
PageDirection::Forward => Self {
after: req.cursor.map(Into::into),
before: None,
first: Some(req.results as i32),
first: Some(req.results),
last: None,
},
PageDirection::Backward => Self {
after: None,
before: req.cursor.map(Into::into),
first: None,
last: Some(req.results as i32),
last: Some(req.results),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/client/src/client/schema/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ impl From<(Address, PaginationRequest<String>)> for BalancesConnectionArgs {
filter: BalanceFilterInput { owner: r.0 },
after: r.1.cursor,
before: None,
first: Some(r.1.results as i32),
first: Some(r.1.results),
last: None,
},
PageDirection::Backward => BalancesConnectionArgs {
filter: BalanceFilterInput { owner: r.0 },
after: None,
before: r.1.cursor,
first: None,
last: Some(r.1.results as i32),
last: Some(r.1.results),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/client/src/client/schema/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl From<(Address, AssetId, PaginationRequest<String>)> for CoinsConnectionArgs
},
after: r.2.cursor,
before: None,
first: Some(r.2.results as i32),
first: Some(r.2.results),
last: None,
},
PageDirection::Backward => CoinsConnectionArgs {
Expand All @@ -74,7 +74,7 @@ impl From<(Address, AssetId, PaginationRequest<String>)> for CoinsConnectionArgs
after: None,
before: r.2.cursor,
first: None,
last: Some(r.2.results as i32),
last: Some(r.2.results),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/client/src/client/schema/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ impl From<(ContractId, PaginationRequest<String>)> for ContractBalancesConnectio
filter: ContractBalanceFilterInput { contract: r.0 },
after: r.1.cursor,
before: None,
first: Some(r.1.results as i32),
first: Some(r.1.results),
last: None,
},
PageDirection::Backward => ContractBalancesConnectionArgs {
filter: ContractBalanceFilterInput { contract: r.0 },
after: None,
before: r.1.cursor,
first: None,
last: Some(r.1.results as i32),
last: Some(r.1.results),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/client/src/client/schema/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ impl From<(Option<Address>, PaginationRequest<String>)> for OwnedMessagesConnect
owner: r.0,
after: r.1.cursor,
before: None,
first: Some(r.1.results as i32),
first: Some(r.1.results),
last: None,
},
PageDirection::Backward => OwnedMessagesConnectionArgs {
owner: r.0,
after: None,
before: r.1.cursor,
first: None,
last: Some(r.1.results as i32),
last: Some(r.1.results),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/client/src/client/schema/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ impl From<(Address, PaginationRequest<String>)> for TransactionsByOwnerConnectio
owner: r.0,
after: r.1.cursor,
before: None,
first: Some(r.1.results as i32),
first: Some(r.1.results),
last: None,
},
PageDirection::Backward => TransactionsByOwnerConnectionArgs {
owner: r.0,
after: None,
before: r.1.cursor,
first: None,
last: Some(r.1.results as i32),
last: Some(r.1.results),
},
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![deny(clippy::cast_possible_truncation)]
#![deny(unused_crate_dependencies)]
#![deny(warnings)]
pub mod client;
Expand Down
1 change: 1 addition & 0 deletions crates/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! defined here are used by services but are flexible enough to customize the
//! logic when the `Database` is known.
#![deny(clippy::cast_possible_truncation)]
#![deny(missing_docs)]
#![deny(unused_crate_dependencies)]
#![deny(warnings)]
Expand Down
10 changes: 5 additions & 5 deletions crates/fuel-core/src/coins_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ mod tests {
// Query some targets, including higher than the owner's balance
for target in 0..20 {
let coins = query(
&[AssetSpendTarget::new(asset_id, target, u64::MAX)],
&[AssetSpendTarget::new(asset_id, target, usize::MAX)],
&owner,
base_asset_id,
&db.service_database(),
Expand Down Expand Up @@ -440,8 +440,8 @@ mod tests {
) {
let coins = query(
&[
AssetSpendTarget::new(asset_ids[0], 3, u64::MAX),
AssetSpendTarget::new(asset_ids[1], 6, u64::MAX),
AssetSpendTarget::new(asset_ids[0], 3, usize::MAX),
AssetSpendTarget::new(asset_ids[1], 6, usize::MAX),
],
&owner,
base_asset_id,
Expand Down Expand Up @@ -514,7 +514,7 @@ mod tests {
// Query some amounts, including higher than the owner's balance
for amount in 0..20 {
let coins = query(
vec![AssetSpendTarget::new(asset_id, amount, u64::MAX)],
vec![AssetSpendTarget::new(asset_id, amount, usize::MAX)],
owner,
asset_ids,
base_asset_id,
Expand Down Expand Up @@ -706,7 +706,7 @@ mod tests {
// Query some amounts, including higher than the owner's balance
for amount in 0..20 {
let coins = query(
vec![AssetSpendTarget::new(asset_id, amount, u64::MAX)],
vec![AssetSpendTarget::new(asset_id, amount, usize::MAX)],
excluded_ids.clone(),
);

Expand Down
14 changes: 7 additions & 7 deletions crates/fuel-core/src/database/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,20 +371,20 @@ mod tests {
assert!(matches!(err, fuel_core_storage::Error::NotFound(_, _)));
}

const TEST_BLOCKS_COUNT: usize = 10;
const TEST_BLOCKS_COUNT: u32 = 10;

fn insert_test_ascending_blocks(
database: &mut Database,
genesis_height: BlockHeight,
) {
let start = genesis_height.as_usize();
let start = *genesis_height;
// Generate 10 blocks with ascending heights
let blocks = (start..start + TEST_BLOCKS_COUNT)
.map(|height| {
let header = PartialBlockHeader {
application: Default::default(),
consensus: ConsensusHeader::<Empty> {
height: BlockHeight::from(height as u32),
height: BlockHeight::from(height),
..Default::default()
},
};
Expand Down Expand Up @@ -431,8 +431,8 @@ mod tests {
for r in l..TEST_BLOCKS_COUNT {
let proof = database
.block_history_proof(
&BlockHeight::from(genesis_height + l as u32),
&BlockHeight::from(genesis_height + r as u32),
&BlockHeight::from(genesis_height + l),
&BlockHeight::from(genesis_height + r),
)
.expect("Should return the merkle proof");
assert_eq!(proof.proof_index, l as u64);
Expand All @@ -447,8 +447,8 @@ mod tests {
insert_test_ascending_blocks(&mut database, BlockHeight::from(0));

let result = database.block_history_proof(
&BlockHeight::from(TEST_BLOCKS_COUNT as u32),
&BlockHeight::from(TEST_BLOCKS_COUNT as u32 - 1),
&BlockHeight::from(TEST_BLOCKS_COUNT),
&BlockHeight::from(TEST_BLOCKS_COUNT - 1),
);
assert!(result.is_err());
}
Expand Down
5 changes: 3 additions & 2 deletions crates/fuel-core/src/database/vm_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ impl InterpreterStorage for VmDatabase {
Some(ContractsStateKey::new(contract_id, start_key)),
Some(IterDirection::Forward),
);
let range = range as usize;
let range = usize::try_from(range)
.expect("Corresponding PR in `fuel-vm` https://github.com/FuelLabs/fuel-vm/pull/619 will use `usize`");

let mut expected_key = U256::from_big_endian(start_key.as_ref());
let mut results = vec![];
Expand Down Expand Up @@ -609,7 +610,7 @@ mod tests {
.is_some();

// check stored data
let results: Vec<_> = (0..(remove_count as usize))
let results: Vec<_> = (0..remove_count)
.filter_map(|i| {
let current_key = U256::from_big_endian(&start_key) + i;
let current_key = u256_to_bytes32(current_key);
Expand Down
11 changes: 8 additions & 3 deletions crates/fuel-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ where
backtrace.contract(),
backtrace.registers(),
backtrace.call_stack(),
hex::encode(&backtrace.memory()[..backtrace.registers()[RegId::SP] as usize]), // print stack
hex::encode(&backtrace.memory()[..usize::try_from(backtrace.registers()[RegId::SP]).expect("`$sp` can't be more than memory - `usize`")]), // print stack
);
}
}
Expand All @@ -1516,7 +1516,9 @@ where
outputs: &[Output],
) -> ExecutorResult<()> {
for (output_index, output) in outputs.iter().enumerate() {
let utxo_id = UtxoId::new(*tx_id, output_index as u8);
let index = u8::try_from(output_index)
.expect("Transaction can have only up to `u8::MAX` outputs");
let utxo_id = UtxoId::new(*tx_id, index);
match output {
Output::Coin {
amount,
Expand Down Expand Up @@ -1639,6 +1641,8 @@ where
let block_height = *block.header().height();
let inputs;
let outputs;
let tx_idx =
u16::try_from(tx_idx).map_err(|_| ExecutorError::TooManyTransactions)?;
let tx_id = tx.id(&self.config.consensus_parameters.chain_id);
match tx {
Transaction::Script(tx) => {
Expand All @@ -1656,7 +1660,7 @@ where
inputs,
outputs,
&tx_id,
tx_idx as u16,
tx_idx,
block_db_transaction.deref_mut(),
)?;
}
Expand Down Expand Up @@ -1771,6 +1775,7 @@ impl Fee for CreateCheckedMetadata {
}
}

#[allow(clippy::cast_possible_truncation)]
#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![deny(clippy::cast_possible_truncation)]
#![deny(unused_crate_dependencies)]
#![deny(warnings)]

Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-core/src/query/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl BalanceQueryData for Database {
) -> StorageResult<AddressBalance> {
let amount = AssetQuery::new(
&owner,
&AssetSpendTarget::new(asset_id, u64::MAX, u64::MAX),
&AssetSpendTarget::new(asset_id, u64::MAX, usize::MAX),
&base_asset_id,
None,
self,
Expand Down
8 changes: 2 additions & 6 deletions crates/fuel-core/src/query/balance/asset_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ pub struct AssetSpendTarget {
}

impl AssetSpendTarget {
pub fn new(id: AssetId, target: u64, max: u64) -> Self {
Self {
id,
target,
max: max as usize,
}
pub fn new(id: AssetId, target: u64, max: usize) -> Self {
Self { id, target, max }
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/fuel-core/src/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl BlockMutation {
&self,
ctx: &Context<'_>,
start_timestamp: Option<Tai64Timestamp>,
blocks_to_produce: U64,
blocks_to_produce: U32,
) -> async_graphql::Result<U32> {
let query: &Database = ctx.data_unchecked();
let consensus_module = ctx.data_unchecked::<ConsensusModule>();
Expand All @@ -302,9 +302,9 @@ impl BlockMutation {
}

let start_time = start_timestamp.map(|timestamp| timestamp.0);
let blocks_to_produce: u64 = blocks_to_produce.into();
let blocks_to_produce: u32 = blocks_to_produce.into();
consensus_module
.manually_produce_blocks(start_time, blocks_to_produce as u32)
.manually_produce_blocks(start_time, blocks_to_produce)
.await?;

query
Expand Down
6 changes: 2 additions & 4 deletions crates/fuel-core/src/schema/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub struct SpendQueryElementInput {
/// Target amount for the query.
amount: U64,
/// The maximum number of currencies for selection.
max: Option<U64>,
max: Option<U32>,
}

#[derive(async_graphql::InputObject)]
Expand Down Expand Up @@ -226,9 +226,7 @@ impl CoinQuery {
AssetSpendTarget::new(
e.asset_id.0,
e.amount.0,
e.max
.map(|max| max.0)
.unwrap_or(config.consensus_parameters.tx_params().max_inputs),
e.max.map(|max| max.0 as usize).unwrap_or(usize::MAX),
)
})
.collect_vec();
Expand Down
Loading

0 comments on commit b80ea61

Please sign in to comment.