diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 5a1cd977a1..77de5ac9f6 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -215,22 +215,16 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> { &self, range: RangeInclusive, ) -> std::result::Result>, TransactionError> { - Ok(self - .tx + self.tx .cursor_read::()? .walk_range(BlockNumberAddress::range(range))? - .collect::, _>>()? - .into_iter() // fold all storages and save its old state so we can remove it from HashedStorage // it is needed as it is dup table. - .fold( - BTreeMap::new(), - |mut accounts: BTreeMap>, - (BlockNumberAddress((_, address)), storage_entry)| { - accounts.entry(address).or_default().insert(storage_entry.key); - accounts - }, - )) + .try_fold(BTreeMap::new(), |mut accounts: BTreeMap>, entry| { + let (BlockNumberAddress((_, address)), storage_entry) = entry?; + accounts.entry(address).or_default().insert(storage_entry.key); + Ok(accounts) + }) } /// Get plainstate storages @@ -313,17 +307,14 @@ impl<'this, TX: DbTx<'this>> DatabaseProvider<'this, TX> { &self, range: RangeInclusive, ) -> std::result::Result, TransactionError> { - Ok(self - .tx - .cursor_read::()? - .walk_range(range)? - .collect::, _>>()? - .into_iter() - // fold all account to one set of changed accounts - .fold(BTreeSet::new(), |mut accounts: BTreeSet
, (_, account_before)| { + self.tx.cursor_read::()?.walk_range(range)?.try_fold( + BTreeSet::new(), + |mut accounts: BTreeSet
, entry| { + let (_, account_before) = entry?; accounts.insert(account_before.address); - accounts - })) + Ok(accounts) + }, + ) } /// Get plainstate account from iterator @@ -1099,8 +1090,7 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> { id: StageId, checkpoint: StageCheckpoint, ) -> std::result::Result<(), DatabaseError> { - self.tx.put::(id.to_string(), checkpoint)?; - Ok(()) + self.tx.put::(id.to_string(), checkpoint) } /// Get stage checkpoint progress. @@ -1117,8 +1107,7 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> { id: StageId, checkpoint: Vec, ) -> std::result::Result<(), DatabaseError> { - self.tx.put::(id.to_string(), checkpoint)?; - Ok(()) + self.tx.put::(id.to_string(), checkpoint) } /// Get lastest block number. @@ -1452,18 +1441,15 @@ impl<'this, TX: DbTx<'this>> AccountExtProvider for DatabaseProvider<'this, TX> &self, range: impl RangeBounds, ) -> Result> { - Ok(self - .tx + self.tx .cursor_read::()? .walk_range(range)? - .collect::, _>>()? - .into_iter() - // fold all account to one set of changed accounts - .fold(BTreeSet::new(), |mut accounts: BTreeSet
, (_, account_before)| { - accounts.insert(account_before.address); - accounts - })) + .map(|entry| { + entry.map(|(_, account_before)| account_before.address).map_err(Into::into) + }) + .collect() } + fn basic_accounts( &self, iter: impl IntoIterator, @@ -1647,9 +1633,9 @@ impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX &self, tx_hash: TxHash, ) -> Result> { + let mut transaction_cursor = self.tx.cursor_read::()?; if let Some(transaction_id) = self.transaction_id(tx_hash)? { if let Some(transaction) = self.transaction_by_id(transaction_id)? { - let mut transaction_cursor = self.tx.cursor_read::()?; if let Some(block_number) = transaction_cursor.seek(transaction_id).map(|b| b.map(|(_, bn)| bn))? { @@ -1689,13 +1675,13 @@ impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX &self, id: BlockHashOrNumber, ) -> Result>> { + let mut tx_cursor = self.tx.cursor_read::()?; if let Some(block_number) = self.convert_hash_or_number(id)? { if let Some(body) = self.block_body_indices(block_number)? { let tx_range = body.tx_num_range(); return if tx_range.is_empty() { Ok(Some(Vec::new())) } else { - let mut tx_cursor = self.tx.cursor_read::()?; let transactions = tx_cursor .walk_range(tx_range)? .map(|result| result.map(|(_, tx)| tx.into())) @@ -1711,14 +1697,14 @@ impl<'this, TX: DbTx<'this>> TransactionsProvider for DatabaseProvider<'this, TX &self, range: impl RangeBounds, ) -> Result>> { - let mut results = Vec::default(); + let mut results = Vec::new(); let mut body_cursor = self.tx.cursor_read::()?; let mut tx_cursor = self.tx.cursor_read::()?; for entry in body_cursor.walk_range(range)? { let (_, body) = entry?; let tx_num_range = body.tx_num_range(); if tx_num_range.is_empty() { - results.push(Vec::default()); + results.push(Vec::new()); } else { results.push( tx_cursor @@ -1787,13 +1773,9 @@ impl<'this, TX: DbTx<'this>> WithdrawalsProvider for DatabaseProvider<'this, TX> } fn latest_withdrawal(&self) -> Result> { - let latest_block_withdrawal = self.tx.cursor_read::()?.last(); - latest_block_withdrawal - .map(|block_withdrawal_pair| { - block_withdrawal_pair - .and_then(|(_, block_withdrawal)| block_withdrawal.withdrawals.last().cloned()) - }) - .map_err(Into::into) + let latest_block_withdrawal = self.tx.cursor_read::()?.last()?; + Ok(latest_block_withdrawal + .and_then(|(_, mut block_withdrawal)| block_withdrawal.withdrawals.pop())) } }