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 Blobs table #2667

Merged
merged 4 commits into from
Feb 5, 2025
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 @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [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.
- [2667](https://github.com/FuelLabs/fuel-core/pull/2667): Populate `Blobs` table in global merkle root storage.

## [Version 0.41.5]

Expand Down
75 changes: 74 additions & 1 deletion crates/fraud_proofs/global_merkle_root/storage/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
column::Column,
Blobs,
Coins,
ConsensusParametersVersions,
ContractsLatestUtxo,
Expand All @@ -12,6 +13,7 @@ use alloc::{
vec,
vec::Vec,
};
use anyhow::anyhow;
use fuel_core_storage::{
kv_store::KeyValueInspect,
transactional::StorageTransaction,
Expand All @@ -32,11 +34,13 @@ use fuel_core_types::{
fuel_asm::Word,
fuel_tx::{
field::{
ChargeableBody,
InputContract,
Inputs,
OutputContract,
Outputs,
UpgradePurpose as _,
Witnesses,
},
input::{
self,
Expand All @@ -54,6 +58,8 @@ use fuel_core_types::{
output,
Address,
AssetId,
Blob,
BlobBody,
Input,
Output,
Transaction,
Expand Down Expand Up @@ -163,7 +169,9 @@ where

// TODO(#2583): Add the transaction to the `ProcessedTransactions` table.
// TODO(#2585): Insert uplodade bytecodes.
// TODO(#2586): Insert blobs.
if let Transaction::Blob(tx) = tx {
netrome marked this conversation as resolved.
Show resolved Hide resolved
self.process_blob_transaction(tx)?;
}
// TODO(#2587): Insert raw code for created contracts.

Ok(())
Expand Down Expand Up @@ -355,6 +363,25 @@ where

Ok(())
}

fn process_blob_transaction(&mut self, tx: &Blob) -> anyhow::Result<()> {
let BlobBody {
id: blob_id,
witness_index,
} = tx.body();

let blob = tx
.witnesses()
.get(usize::from(*witness_index))
// TODO(#2588): Proper error type
.ok_or_else(|| anyhow!("transaction should have blob payload"))?;

self.storage
.storage::<Blobs>()
.insert(blob_id, blob.as_ref())?;

Ok(())
}
}

pub trait TransactionInputs {
Expand Down Expand Up @@ -411,6 +438,8 @@ mod tests {
use fuel_core_types::{
fuel_crypto::Hasher,
fuel_tx::{
BlobId,
BlobIdExt,
Bytes32,
ConsensusParameters,
ContractId,
Expand Down Expand Up @@ -766,6 +795,50 @@ mod tests {
assert!(result_after_second_call.is_err());
}

#[test]
/// When encountering a blob transaction,
/// `process_transaction` should insert the
/// corresponding blob.
fn process_transaction__should_insert_blob() {
let mut rng = StdRng::seed_from_u64(1337);

// Given
let blob = vec![1, 3, 3, 7];
let blob_id = BlobId::compute(&blob);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when we already have a blob that already exists? should we throw an error or just overwrite it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the current behavior of the node (whether we allow duplicate blobs or not), but here we should only receive valid transactions. Since blobs are content-addressed it doesn't really matter. We could probably just ignore it, since we already have the blob stored. The behavior should only matter if we have hash collisions, which in practice doesn't happen afaik.

let body = BlobBody {
id: blob_id,
witness_index: 0,
};
let blob_tx = TransactionBuilder::blob(body)
.add_witness(Witness::from(blob.as_slice()))
.finalize_as_transaction();

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(rng.gen());
let tx_idx = rng.gen();

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

storage_tx.commit().unwrap();

let read_tx = storage.read_transaction();
let blob_in_storage = read_tx
.storage_as_ref::<Blobs>()
.get(&blob_id)
.unwrap()
.unwrap();

// Then
assert_eq!(blob_in_storage.0.as_slice(), blob.as_slice());
}

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