Skip to content

Commit

Permalink
Simplify code extracting exactly one historic transaction
Browse files Browse the repository at this point in the history
Thanks to @ii-cruz for the suggestion.
  • Loading branch information
hrxi committed May 28, 2024
1 parent 7967642 commit 61b503d
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions rpc-server/src/dispatchers/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,20 @@ impl BlockchainInterface for BlockchainDispatcher {
let mut hist_txs = blockchain.history_store.get_hist_tx_by_hash(&hash, None);

// Unpack the transaction or raise an error.
let hist_tx = match (hist_txs.pop(), hist_txs.len()) {
(Some(hist_tx), 0) => hist_tx,
(Some(_), _) => return Err(Error::MultipleTransactionsFound(hash)),
(None, _) => return Err(Error::TransactionNotFound(hash)),
};
if hist_txs.len() > 1 {
return Err(Error::MultipleTransactionsFound(hash));
}
let hist_tx = hist_txs
.pop()
.ok_or_else(|| Error::TransactionNotFound(hash.clone()))?;

// Convert the historic transaction into a regular transaction. This will also convert
// reward inherents.
Ok(ExecutedTransaction::try_from_historic_transaction(
hist_tx,
Some(blockchain.block_number()),
)
.ok_or(Error::TransactionNotFound(hash))?
.ok_or_else(|| Error::TransactionNotFound(hash.clone()))?
.into())
} else {
Err(Error::NotSupportedForLightBlockchain)
Expand Down Expand Up @@ -340,11 +341,12 @@ impl BlockchainInterface for BlockchainDispatcher {
let mut hist_txs = blockchain.history_store.get_hist_tx_by_hash(&hash, None);

// Unpack the transaction or raise an error.
let hist_tx = match (hist_txs.pop(), hist_txs.len()) {
(Some(hist_tx), 0) => hist_tx,
(Some(_), _) => return Err(Error::MultipleTransactionsFound(hash)),
(None, _) => return Err(Error::TransactionNotFound(hash)),
};
if hist_txs.len() > 1 {
return Err(Error::MultipleTransactionsFound(hash));
}
let hist_tx = hist_txs
.pop()
.ok_or_else(|| Error::TransactionNotFound(hash.clone()))?;

// Convert the historic transaction into a regular transaction. This will also convert
// reward inherents.
Expand All @@ -353,7 +355,7 @@ impl BlockchainInterface for BlockchainDispatcher {
hist_tx,
Some(blockchain.block_number()),
)
.ok_or(Error::TransactionNotFound(hash))?,
.ok_or_else(|| Error::TransactionNotFound(hash.clone()))?,
)
}

Expand Down

0 comments on commit 61b503d

Please sign in to comment.