Skip to content
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

fault_proving(global_roots): populate ProcessedTransactions table #2650

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- [2668](https://github.com/FuelLabs/fuel-core/pull/2668): Expose gas price service test helpers
- [2621](https://github.com/FuelLabs/fuel-core/pull/2598): Global merkle root storage updates process upgrade transactions.
- [2650](https://github.com/FuelLabs/fuel-core/pull/2650): Populate `ProcessedTransactions` table in global merkle root storage.

## [Version 0.41.5]

Expand Down
82 changes: 78 additions & 4 deletions crates/fraud_proofs/global_merkle_root/storage/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
ConsensusParametersVersions,
ContractsLatestUtxo,
Messages,
ProcessedTransactions,
StateTransitionBytecodeVersions,
};
use alloc::{
Expand Down Expand Up @@ -50,14 +51,13 @@ use fuel_core_types::{
MessageDataSigned,
},
},
output::{
self,
},
output,
Address,
AssetId,
Input,
Output,
Transaction,
TxId,
TxPointer,
UniqueIdentifier,
Upgrade,
Expand Down Expand Up @@ -139,7 +139,9 @@ where
tx_idx: u16,
tx: &Transaction,
) -> anyhow::Result<()> {
let tx_id = tx.id(&self.chain_id);
let inputs = tx.inputs();

for input in inputs.iter() {
self.process_input(input)?;
}
Expand All @@ -149,14 +151,16 @@ where
let output_index =
u16::try_from(output_index).map_err(|_| ExecutorError::TooManyOutputs)?;

let tx_id = tx.id(&self.chain_id);
let utxo_id = UtxoId::new(tx_id, output_index);
self.process_output(tx_pointer, utxo_id, &inputs, output)?;
}

if let Transaction::Upgrade(tx) = tx {
self.process_upgrade_transaction(tx)?;
}

self.store_processed_transaction(tx_id)?;

// TODO(#2583): Add the transaction to the `ProcessedTransactions` table.
netrome marked this conversation as resolved.
Show resolved Hide resolved
// TODO(#2585): Insert uplodade bytecodes.
// TODO(#2586): Insert blobs.
Expand Down Expand Up @@ -237,6 +241,19 @@ where
Ok(())
}

fn store_processed_transaction(&mut self, tx_id: TxId) -> anyhow::Result<()> {
let previous_tx = self
.storage
.storage_as_mut::<ProcessedTransactions>()
.replace(&tx_id, &())?;

if previous_tx.is_some() {
anyhow::bail!("duplicate transaction detected")
};

Ok(())
}

fn insert_coin_if_it_has_amount(
&mut self,
tx_pointer: TxPointer,
Expand Down Expand Up @@ -692,6 +709,63 @@ mod tests {
assert_eq!(consensus_parameters_after_upgrade, consensus_parameters);
}

#[test]
/// After processing a transaction,
/// it should be stored in the `ProcessedTransactions` table.
fn process_transaction__should_store_processed_transaction() {
netrome marked this conversation as resolved.
Show resolved Hide resolved
// Given
let mut storage: InMemoryStorage<Column> = InMemoryStorage::default();
let mut storage_tx = storage.write_transaction();
let mut storage_update_tx =
storage_tx.construct_update_merkleized_tables_transaction();

let block_height = BlockHeight::new(0);
let tx_idx = 0;
let tx = Transaction::default_test_tx();
let tx_id = tx.id(&storage_update_tx.chain_id);

// When
storage_update_tx
.process_transaction(block_height, tx_idx, &tx)
.unwrap();

storage_tx.commit().unwrap();

// Then
assert!(storage
.read_transaction()
.storage_as_ref::<ProcessedTransactions>()
.get(&tx_id)
.unwrap()
.is_some());
netrome marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
/// We get an error if we encounter the same transaction
/// twice in `process_transaction`.
fn process_transaction__should_error_on_duplicate_transaction() {
// Given
let mut storage: InMemoryStorage<Column> = InMemoryStorage::default();
let mut storage_tx = storage.write_transaction();
let mut storage_update_tx =
storage_tx.construct_update_merkleized_tables_transaction();

let block_height = BlockHeight::new(0);
let tx_idx = 0;
let tx = Transaction::default_test_tx();
netrome marked this conversation as resolved.
Show resolved Hide resolved

// When
storage_update_tx
.process_transaction(block_height, tx_idx, &tx)
.unwrap();

let result_after_second_call =
storage_update_tx.process_transaction(block_height, tx_idx, &tx);

// Then
assert!(result_after_second_call.is_err());
}

fn random_utxo_id(rng: &mut impl rand::RngCore) -> UtxoId {
let mut txid = TxId::default();
rng.fill_bytes(txid.as_mut());
Expand Down
Loading