Skip to content

Commit

Permalink
Rename sequence number to bundle id
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Dec 12, 2024
1 parent 43e19f2 commit 93876fa
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,6 @@ fn update_da_record_data__da_block_lowers_da_gas_price() {
let da_cost_per_byte = 40;
let l2_block_height = 11;
let original_known_total_cost = 150;
// let unrecorded_blocks = vec![BlockBytes {
// height: 11,
// block_bytes: 3000,
// }];
let mut unrecorded_blocks: BTreeMap<_, _> = [(11, 3000)].into_iter().collect();
let da_p_component = 2;
let guessed_cost: u64 = unrecorded_blocks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
common::{
fuel_core_storage_adapter::storage::{
BundleIdTable,
GasPriceColumn,
GasPriceMetadata,
SequenceNumberTable,
},
updater_metadata::UpdaterMetadata,
utils::{
Expand All @@ -14,9 +14,9 @@ use crate::{
},
ports::{
GasPriceServiceAtomicStorage,
GetDaSequenceNumber,
GetDaBundleId,
GetMetadataStorage,
SetDaSequenceNumber,
SetDaBundleId,
SetMetadataStorage,
},
};
Expand Down Expand Up @@ -99,28 +99,25 @@ where
}
}

impl<Storage> GetDaSequenceNumber for Storage
impl<Storage> GetDaBundleId for Storage
where
Storage: Send + Sync,
Storage: StorageInspect<SequenceNumberTable, Error = StorageError>,
Storage: StorageInspect<BundleIdTable, Error = StorageError>,
{
fn get_sequence_number(
&self,
block_height: &BlockHeight,
) -> GasPriceResult<Option<u32>> {
let sequence_number = self
.storage::<SequenceNumberTable>()
fn get_bundle_id(&self, block_height: &BlockHeight) -> GasPriceResult<Option<u32>> {
let bundle_id = self
.storage::<BundleIdTable>()
.get(block_height)
.map_err(|err| GasPriceError::CouldNotFetchDARecord(err.into()))?
.map(|no| *no);
Ok(sequence_number)
Ok(bundle_id)
}
}

impl<Storage> GasPriceServiceAtomicStorage for Storage
where
Storage: 'static,
Storage: GetMetadataStorage + GetDaSequenceNumber,
Storage: GetMetadataStorage + GetDaBundleId,
Storage: KeyValueInspect<Column = GasPriceColumn> + Modifiable + Send + Sync,
{
type Transaction<'a> = StorageTransaction<&'a mut Storage> where Self: 'a;
Expand All @@ -138,18 +135,18 @@ where
}
}

impl<Storage> SetDaSequenceNumber for Storage
impl<Storage> SetDaBundleId for Storage
where
Storage: Send + Sync,
Storage: StorageMutate<SequenceNumberTable, Error = StorageError>,
Storage: StorageMutate<BundleIdTable, Error = StorageError>,
{
fn set_sequence_number(
fn set_bundle_id(
&mut self,
block_height: &BlockHeight,
sequence_number: u32,
bundle_id: u32,
) -> GasPriceResult<()> {
self.storage_as_mut::<SequenceNumberTable>()
.insert(block_height, &sequence_number)
self.storage_as_mut::<BundleIdTable>()
.insert(block_height, &bundle_id)
.map_err(|err| GasPriceError::CouldNotFetchDARecord(err.into()))?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum GasPriceColumn {
Metadata = 0,
State = 1,
UnrecordedBlocks = 2,
SequenceNumber = 3,
BundleId = 3,
}

impl GasPriceColumn {
Expand Down Expand Up @@ -93,23 +93,23 @@ impl TableWithBlueprint for UnrecordedBlocksTable {
}
}

pub struct SequenceNumberTable;
pub struct BundleIdTable;

/// The sequence number or bundle id of the posted blocks.
type BundleId = u32;

impl Mappable for SequenceNumberTable {
impl Mappable for BundleIdTable {
type Key = Self::OwnedKey;
type OwnedKey = BlockHeight;
type Value = Self::OwnedValue;
type OwnedValue = BundleId;
}

impl TableWithBlueprint for SequenceNumberTable {
impl TableWithBlueprint for BundleIdTable {
type Blueprint = Plain<Primitive<4>, Postcard>;
type Column = GasPriceColumn;

fn column() -> Self::Column {
GasPriceColumn::SequenceNumber
GasPriceColumn::BundleId
}
}
19 changes: 8 additions & 11 deletions crates/services/gas_price_service/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,26 @@ pub trait GetMetadataStorage: Send + Sync {
-> Result<Option<UpdaterMetadata>>;
}

pub trait SetDaSequenceNumber: Send + Sync {
fn set_sequence_number(
&mut self,
block_height: &BlockHeight,
sequence_number: u32,
) -> Result<()>;
pub trait SetDaBundleId: Send + Sync {
fn set_bundle_id(&mut self, block_height: &BlockHeight, bundle_id: u32)
-> Result<()>;
}

pub trait GetDaSequenceNumber: Send + Sync {
fn get_sequence_number(&self, block_height: &BlockHeight) -> Result<Option<u32>>;
pub trait GetDaBundleId: Send + Sync {
fn get_bundle_id(&self, block_height: &BlockHeight) -> Result<Option<u32>>;
}

pub trait GasPriceServiceAtomicStorage
where
Self: 'static,
Self: Send + Sync,
Self: GetMetadataStorage + GetDaSequenceNumber,
Self: GetMetadataStorage + GetDaBundleId,
{
type Transaction<'a>: AsUnrecordedBlocks
+ SetMetadataStorage
+ GetMetadataStorage
+ SetDaSequenceNumber
+ GetDaSequenceNumber
+ SetDaBundleId
+ GetDaBundleId
where
Self: 'a;

Expand Down
4 changes: 2 additions & 2 deletions crates/services/gas_price_service/src/v1/da_source_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod service;

#[derive(Debug, Default, Clone, Eq, Hash, PartialEq)]
pub struct DaBlockCosts {
pub bundle_sequence_number: u32,
pub bundle_id: u32,
pub l2_blocks: Vec<u32>,
pub bundle_size_bytes: u32,
pub blob_cost_wei: u128,
Expand All @@ -32,7 +32,7 @@ mod tests {
async fn run__when_da_block_cost_source_gives_value_shared_state_is_updated() {
// given
let expected_da_cost = DaBlockCosts {
bundle_sequence_number: 1,
bundle_id: 1,
l2_blocks: (0..10).collect(),
bundle_size_bytes: 1024 * 128,
blob_cost_wei: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct BlockCommitterDaBlockCosts<BlockCommitter> {
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq)]
pub struct RawDaBlockCosts {
/// Sequence number (Monotonically increasing nonce)
pub sequence_number: u32,
pub bundle_id: u32,
/// The range of blocks that the costs apply to
pub blocks_heights: Vec<u32>,
/// The DA block height of the last transaction for the range of blocks
Expand All @@ -54,7 +54,7 @@ pub struct RawDaBlockCosts {
impl From<&RawDaBlockCosts> for DaBlockCosts {
fn from(raw_da_block_costs: &RawDaBlockCosts) -> Self {
DaBlockCosts {
bundle_sequence_number: raw_da_block_costs.sequence_number,
bundle_id: raw_da_block_costs.bundle_id,
l2_blocks: raw_da_block_costs
.blocks_heights
.clone()
Expand Down Expand Up @@ -83,9 +83,9 @@ where
{
async fn request_da_block_cost(&mut self) -> DaBlockCostsResult<DaBlockCosts> {
let raw_da_block_costs = match self.last_raw_da_block_costs {
Some(ref last_value) => self
.client
.get_costs_by_seqno(last_value.sequence_number + 1),
Some(ref last_value) => {
self.client.get_costs_by_seqno(last_value.bundle_id + 1)
}
_ => self.client.get_latest_costs(),
}
.await?;
Expand Down Expand Up @@ -117,9 +117,8 @@ where
self.last_raw_da_block_costs = Some(raw_da_block_costs.clone());
Ok(da_block_costs)
}
async fn set_last_value(&mut self, sequence_number: u32) -> DaBlockCostsResult<()> {
self.last_raw_da_block_costs =
self.client.get_costs_by_seqno(sequence_number).await?;
async fn set_last_value(&mut self, bundle_id: u32) -> DaBlockCostsResult<()> {
self.last_raw_da_block_costs = self.client.get_costs_by_seqno(bundle_id).await?;
Ok(())
}
}
Expand Down Expand Up @@ -207,7 +206,7 @@ mod tests {
// arbitrary logic to generate a new value
let mut value = self.value.clone();
if let Some(value) = &mut value {
value.sequence_number = seq_no;
value.bundle_id = seq_no;
value.blocks_heights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.to_vec()
.iter()
Expand All @@ -230,7 +229,7 @@ mod tests {

fn test_da_block_costs() -> RawDaBlockCosts {
RawDaBlockCosts {
sequence_number: 1,
bundle_id: 1,
blocks_heights: (0..10).collect(),
da_block_height: 1u64.into(),
total_cost: 1,
Expand Down Expand Up @@ -305,7 +304,7 @@ mod tests {
// arbitrary logic to generate a new value
let mut value = self.value.clone();
if let Some(value) = &mut value {
value.sequence_number = seq_no;
value.bundle_id = seq_no;
value.blocks_heights =
value.blocks_heights.iter().map(|x| x + seq_no).collect();
value.da_block_height = value.da_block_height + 1u64.into();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl DaBlockCostsSource for DummyDaBlockCosts {
}
}

async fn set_last_value(&mut self, sequence_number: u32) -> DaBlockCostsResult<()> {
async fn set_last_value(&mut self, _bundle_id: u32) -> DaBlockCostsResult<()> {
unimplemented!("This is a dummy implementation");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
#[async_trait::async_trait]
pub trait DaBlockCostsSource: Send + Sync {
async fn request_da_block_cost(&mut self) -> Result<DaBlockCosts>;
async fn set_last_value(&mut self, sequence_number: u32) -> Result<()>;
async fn set_last_value(&mut self, bundle_id: u32) -> Result<()>;
}

#[async_trait::async_trait]
Expand Down
30 changes: 15 additions & 15 deletions crates/services/gas_price_service/src/v1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::{
},
ports::{
GasPriceServiceAtomicStorage,
GetDaSequenceNumber,
GetDaBundleId,
GetMetadataStorage,
SetDaSequenceNumber,
SetDaBundleId,
SetMetadataStorage,
},
v0::metadata::V0Metadata,
Expand Down Expand Up @@ -157,8 +157,8 @@ where
let capacity = Self::validate_block_gas_capacity(block_gas_capacity)?;
let mut storage_tx = self.storage_tx_provider.begin_transaction()?;
let prev_height = height.saturating_sub(1);
let mut sequence_number = storage_tx
.get_sequence_number(&BlockHeight::from(prev_height))
let mut bundle_id = storage_tx
.get_bundle_id(&BlockHeight::from(prev_height))
.map_err(|err| anyhow!(err))?;

for da_block_costs in &self.da_block_costs_buffer {
Expand All @@ -169,12 +169,12 @@ where
da_block_costs.blob_cost_wei,
&mut storage_tx.as_unrecorded_blocks(),
)?;
sequence_number = Some(da_block_costs.bundle_sequence_number);
bundle_id = Some(da_block_costs.bundle_id);
}

if let Some(sequence_number) = sequence_number {
if let Some(bundle_id) = bundle_id {
storage_tx
.set_sequence_number(&BlockHeight::from(height), sequence_number)
.set_bundle_id(&BlockHeight::from(height), bundle_id)
.map_err(|err| anyhow!(err))?;
}

Expand Down Expand Up @@ -356,9 +356,9 @@ mod tests {
use crate::{
common::{
fuel_core_storage_adapter::storage::{
BundleIdTable,
GasPriceColumn,
GasPriceColumn::UnrecordedBlocks,
SequenceNumberTable,
UnrecordedBlocksTable,
},
gas_price_algorithm::SharedGasPriceAlgo,
Expand Down Expand Up @@ -554,7 +554,7 @@ mod tests {
let da_source = DaSourceService::new(
DummyDaBlockCosts::new(
Ok(DaBlockCosts {
bundle_sequence_number: 1,
bundle_id: 1,
l2_blocks: (1..2).collect(),
blob_cost_wei: 9000,
bundle_size_bytes: 3000,
Expand Down Expand Up @@ -617,9 +617,9 @@ mod tests {
}

#[tokio::test]
async fn run__responses_from_da_service_update_sequence_number_in_storage() {
async fn run__responses_from_da_service_update_bundle_id_in_storage() {
// given
let sequence_number = 1234;
let bundle_id = 1234;
let block_height = 2;
let l2_block_2 = BlockInfo::Block {
height: block_height,
Expand Down Expand Up @@ -654,7 +654,7 @@ mod tests {
let da_source = DaSourceService::new(
DummyDaBlockCosts::new(
Ok(DaBlockCosts {
bundle_sequence_number: sequence_number,
bundle_id,
l2_blocks: (1..2).collect(),
blob_cost_wei: 9000,
bundle_size_bytes: 3000,
Expand Down Expand Up @@ -693,13 +693,13 @@ mod tests {
tokio::time::sleep(Duration::from_millis(100)).await;

// then
let latest_sequence_number = service
let latest_bundle_id = service
.storage_tx_provider
.storage::<SequenceNumberTable>()
.storage::<BundleIdTable>()
.get(&BlockHeight::from(block_height))
.unwrap()
.unwrap();
assert_eq!(*latest_sequence_number, sequence_number);
assert_eq!(*latest_bundle_id, bundle_id);

service.shutdown().await.unwrap();
}
Expand Down
Loading

0 comments on commit 93876fa

Please sign in to comment.