Skip to content

Commit

Permalink
fault_proving(global_roots): Populate Blobs table (#2667)
Browse files Browse the repository at this point in the history
  • Loading branch information
netrome authored Feb 5, 2025
1 parent c53a265 commit c4404d2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
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 {
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);
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

0 comments on commit c4404d2

Please sign in to comment.