-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore receipts from failed transactions in message_receipts_proof
#2478
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
484f447
Ignore failed TX receipts and add a test.
AurelienFT 4dcd294
Fix clippy.
AurelienFT ad1b926
Update CHANGELOG
AurelienFT 451dfa5
Update tests to match the new behavior.
AurelienFT 0db9967
Remove receipts function and change test to use public function
AurelienFT 6966c0b
Merge branch 'master' into fix_message_receipts_proofs
AurelienFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,9 +115,6 @@ pub trait MessageProofData { | |
/// Get the block. | ||
fn block(&self, id: &BlockHeight) -> StorageResult<CompressedBlock>; | ||
|
||
/// Return all receipts in the given transaction. | ||
fn receipts(&self, transaction_id: &TxId) -> StorageResult<Vec<Receipt>>; | ||
|
||
/// Get the status of a transaction. | ||
fn transaction_status( | ||
&self, | ||
|
@@ -138,10 +135,6 @@ impl MessageProofData for ReadView { | |
self.block(id) | ||
} | ||
|
||
fn receipts(&self, transaction_id: &TxId) -> StorageResult<Vec<Receipt>> { | ||
self.receipts(transaction_id) | ||
} | ||
|
||
fn transaction_status( | ||
&self, | ||
transaction_id: &TxId, | ||
|
@@ -165,34 +158,28 @@ pub fn message_proof<T: MessageProofData + ?Sized>( | |
desired_nonce: Nonce, | ||
commit_block_height: BlockHeight, | ||
) -> StorageResult<MessageProof> { | ||
// Check if the receipts for this transaction actually contain this nonce or exit. | ||
let (sender, recipient, nonce, amount, data) = database | ||
.receipts(&transaction_id)? | ||
.into_iter() | ||
.find_map(|r| match r { | ||
Receipt::MessageOut { | ||
sender, | ||
recipient, | ||
nonce, | ||
amount, | ||
data, | ||
.. | ||
} if r.nonce() == Some(&desired_nonce) => { | ||
Some((sender, recipient, nonce, amount, data)) | ||
} | ||
_ => None, | ||
}) | ||
.ok_or::<StorageError>( | ||
anyhow::anyhow!("Desired `nonce` missing in transaction receipts").into(), | ||
)?; | ||
|
||
let Some(data) = data else { | ||
return Err(anyhow::anyhow!("Output message doesn't contain any `data`").into()) | ||
}; | ||
|
||
// Get the block id from the transaction status if it's ready. | ||
let message_block_height = match database.transaction_status(&transaction_id) { | ||
Ok(TransactionStatus::Success { block_height, .. }) => block_height, | ||
let (message_block_height, (sender, recipient, nonce, amount, data)) = match database.transaction_status(&transaction_id) { | ||
Ok(TransactionStatus::Success { block_height, receipts, .. }) => ( | ||
block_height, | ||
receipts.into_iter() | ||
.find_map(|r| match r { | ||
Receipt::MessageOut { | ||
sender, | ||
recipient, | ||
nonce, | ||
amount, | ||
data, | ||
.. | ||
} if r.nonce() == Some(&desired_nonce) => { | ||
Some((sender, recipient, nonce, amount, data)) | ||
} | ||
_ => None, | ||
}) | ||
.ok_or::<StorageError>( | ||
anyhow::anyhow!("Desired `nonce` missing in transaction receipts").into(), | ||
)? | ||
), | ||
Ok(TransactionStatus::Submitted { .. }) => { | ||
return Err(anyhow::anyhow!( | ||
"Unable to obtain the message block height. The transaction has not been processed yet" | ||
|
@@ -219,6 +206,10 @@ pub fn message_proof<T: MessageProofData + ?Sized>( | |
} | ||
}; | ||
|
||
let Some(data) = data else { | ||
return Err(anyhow::anyhow!("Output message doesn't contain any `data`").into()) | ||
}; | ||
|
||
// Get the message fuel block header. | ||
let (message_block_header, message_block_txs) = | ||
match database.block(&message_block_height) { | ||
|
@@ -278,7 +269,11 @@ fn message_receipts_proof<T: MessageProofData + ?Sized>( | |
// Get the message receipts from the block. | ||
let leaves: Vec<Vec<Receipt>> = message_block_txs | ||
.iter() | ||
.map(|id| database.receipts(id)) | ||
.filter_map(|id| match database.transaction_status(id) { | ||
Ok(TransactionStatus::Success { receipts, .. }) => Some(Ok(receipts)), | ||
Ok(_) => None, | ||
Err(err) => Some(Err(err)), | ||
}) | ||
.try_collect()?; | ||
let leaves = leaves.into_iter() | ||
// Flatten the receipts after filtering on output messages | ||
|
@@ -337,3 +332,173 @@ pub fn message_status( | |
Ok(MessageStatus::not_found()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
|
||
use fuel_core_storage::not_found; | ||
use fuel_core_types::{ | ||
blockchain::block::CompressedBlock, | ||
entities::relayer::message::MerkleProof, | ||
fuel_tx::{ | ||
Address, | ||
Bytes32, | ||
Receipt, | ||
TxId, | ||
}, | ||
fuel_types::{ | ||
BlockHeight, | ||
Nonce, | ||
}, | ||
services::txpool::TransactionStatus, | ||
tai64::Tai64, | ||
}; | ||
|
||
use super::{ | ||
message_proof, | ||
MessageProofData, | ||
}; | ||
|
||
pub struct FakeDB { | ||
pub blocks: HashMap<BlockHeight, CompressedBlock>, | ||
pub transaction_statuses: HashMap<TxId, TransactionStatus>, | ||
pub receipts: HashMap<TxId, Vec<Receipt>>, | ||
} | ||
|
||
impl FakeDB { | ||
fn new() -> Self { | ||
Self { | ||
blocks: HashMap::new(), | ||
transaction_statuses: HashMap::new(), | ||
receipts: HashMap::new(), | ||
} | ||
} | ||
|
||
fn insert_block(&mut self, block_height: BlockHeight, block: CompressedBlock) { | ||
self.blocks.insert(block_height, block); | ||
} | ||
|
||
fn insert_transaction_status( | ||
&mut self, | ||
transaction_id: TxId, | ||
status: TransactionStatus, | ||
) { | ||
self.transaction_statuses.insert(transaction_id, status); | ||
} | ||
|
||
fn insert_receipts(&mut self, transaction_id: TxId, receipts: Vec<Receipt>) { | ||
self.receipts.insert(transaction_id, receipts); | ||
} | ||
} | ||
|
||
impl MessageProofData for FakeDB { | ||
fn block(&self, id: &BlockHeight) -> fuel_core_storage::Result<CompressedBlock> { | ||
self.blocks.get(id).cloned().ok_or(not_found!("Block")) | ||
} | ||
|
||
fn transaction_status( | ||
&self, | ||
transaction_id: &TxId, | ||
) -> fuel_core_storage::Result<TransactionStatus> { | ||
self.transaction_statuses | ||
.get(transaction_id) | ||
.cloned() | ||
.ok_or(not_found!("Transaction status")) | ||
} | ||
|
||
fn block_history_proof( | ||
&self, | ||
_message_block_height: &BlockHeight, | ||
_commit_block_height: &BlockHeight, | ||
) -> fuel_core_storage::Result<MerkleProof> { | ||
// Unused in current tests | ||
Ok(MerkleProof::default()) | ||
} | ||
} | ||
|
||
// Test will try to get the message receipt proof with a block with only valid transactions | ||
// Then add an invalid transaction and check if the proof is still the same (meaning the invalid transaction was ignored) | ||
#[test] | ||
fn test_message_proof_ignore_failed() { | ||
// Create a fake database | ||
let mut database = FakeDB::new(); | ||
|
||
// Given | ||
// Create a block with a valid transaction and receipts | ||
let mut block = CompressedBlock::default(); | ||
let block_height: BlockHeight = BlockHeight::new(1); | ||
block.header_mut().set_block_height(block_height); | ||
let valid_tx_id = Bytes32::new([1; 32]); | ||
let mut valid_tx_receipts = vec![]; | ||
for i in 0..100 { | ||
AurelienFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
valid_tx_receipts.push(Receipt::MessageOut { | ||
sender: Address::default(), | ||
recipient: Address::default(), | ||
amount: 0, | ||
nonce: 0.into(), | ||
len: 32, | ||
digest: Bytes32::default(), | ||
data: Some(vec![i; 32]), | ||
}); | ||
} | ||
block.transactions_mut().push(valid_tx_id); | ||
database.insert_block(block_height, block.clone()); | ||
database.insert_transaction_status( | ||
valid_tx_id, | ||
TransactionStatus::Success { | ||
time: Tai64::UNIX_EPOCH, | ||
block_height, | ||
receipts: valid_tx_receipts.clone(), | ||
total_fee: 0, | ||
total_gas: 0, | ||
result: None, | ||
}, | ||
); | ||
database.insert_receipts(valid_tx_id, valid_tx_receipts.clone()); | ||
|
||
// Get the message proof with the valid transaction | ||
let message_proof_valid_tx = | ||
message_proof(&database, valid_tx_id, Nonce::default(), block_height) | ||
.unwrap(); | ||
|
||
// Add an invalid transaction with receipts to the block | ||
let invalid_tx_id = Bytes32::new([2; 32]); | ||
block.transactions_mut().push(invalid_tx_id); | ||
database.insert_block(block_height, block.clone()); | ||
let mut invalid_tx_receipts = vec![]; | ||
for i in 0..100 { | ||
invalid_tx_receipts.push(Receipt::MessageOut { | ||
sender: Address::default(), | ||
recipient: Address::default(), | ||
amount: 0, | ||
nonce: 0.into(), | ||
len: 33, | ||
digest: Bytes32::default(), | ||
data: Some(vec![i; 33]), | ||
}); | ||
} | ||
database.insert_transaction_status( | ||
invalid_tx_id, | ||
TransactionStatus::Failed { | ||
time: Tai64::UNIX_EPOCH, | ||
block_height, | ||
result: None, | ||
total_fee: 0, | ||
total_gas: 0, | ||
receipts: invalid_tx_receipts.clone(), | ||
}, | ||
); | ||
database.insert_receipts(invalid_tx_id, invalid_tx_receipts.clone()); | ||
|
||
// When | ||
// Get the message proof with the same message id | ||
let message_proof_invalid_tx = | ||
message_proof(&database, valid_tx_id, Nonce::default(), block_height) | ||
.unwrap(); | ||
|
||
// Then | ||
// The proof should be the same because the invalid transaction was ignored | ||
assert_eq!(message_proof_valid_tx, message_proof_invalid_tx); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested it without the fix, and it was failing here, yeah? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "yes" :) |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we also need to update another place with
database.receipts
function call. And removereceipts
function at all=)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done