Skip to content

Commit ac26865

Browse files
HristoStaykovreo101
authored andcommitted
chore(sequencer): Renaming round buffer to ring buffer + provider.get_latest_values only available for test
1 parent fa4ccc3 commit ac26865

File tree

6 files changed

+55
-60
lines changed

6 files changed

+55
-60
lines changed

apps/sequencer/src/aggregate_batch_consensus_processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub async fn aggregation_batch_consensus_loop(
6161
let providers = sequencer_state.providers.read().await;
6262
let mut provider = providers.get(net.as_str()).unwrap().lock().await;
6363
let ids_vec: Vec<_> = t.updated_feeds_ids.iter().copied().collect();
64-
warn!("Tiemed out batch {t:?} while collectiong reporters' signatures for net {net}. Decreasing the round buffer indices for feed_ids: {ids_vec:?}");
64+
warn!("Tiemed out batch {t:?} while collectiong reporters' signatures for net {net}. Decreasing the ring buffer indices for feed_ids: {ids_vec:?}");
6565
decrement_feed_rb_indices(&ids_vec, net.as_str(), &mut provider).await
6666
}
6767

apps/sequencer/src/providers/eth_send_utils.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
sequencer_state::SequencerState,
3030
};
3131
use blocksense_feeds_processing::adfs_gen_calldata::{
32-
adfs_serialize_updates, get_neighbour_feed_ids, RoundBufferIndices,
32+
adfs_serialize_updates, get_neighbour_feed_ids, RingBufferIndices,
3333
};
3434
use blocksense_metrics::{
3535
dec_metric, inc_metric, inc_vec_metric,
@@ -559,7 +559,7 @@ pub async fn eth_batch_send_to_contract(
559559

560560
// If the nonce in the contract increased and the next state root hash is not as we expect,
561561
// another sequencer was able to post updates for the current block height before this one.
562-
// We need to take this into account and reread the round counters of the feeds.
562+
// We need to take this into account and reread the ring buffer indices of the feeds.
563563
info!("Updates to contract already posted, network {net}, block_height {block_height}, latest_nonce {latest_nonce}, previous_nonce {nonce}, merkle_root in contract {prev_calldata_merkle_tree_root:?}");
564564
try_to_sync(
565565
net.as_str(),
@@ -836,7 +836,7 @@ pub async fn eth_batch_send_to_contract(
836836
provider.calldata_merkle_tree_frontier = next_calldata_merkle_tree;
837837
provider.merkle_root_in_contract = None;
838838
debug!("Successfully updated contract in network `{net}` block height {block_height} Merkle root {root:?}");
839-
} // TODO: Reread round counters + latest state hash from contract
839+
} // TODO: Reread ring buffer indices + latest state hash from contract
840840
drop(provider);
841841
debug!("Released a read/write lock on provider state in network `{net}` block height {block_height}");
842842

@@ -1391,14 +1391,14 @@ pub async fn eth_batch_send_to_all_contracts(
13911391
async fn log_rb_indices(
13921392
prefix: &str,
13931393
updated_feeds: &Vec<EncodedFeedId>,
1394-
rb_indices: &mut RoundBufferIndices,
1394+
rb_indices: &mut RingBufferIndices,
13951395
net: &str,
13961396
) {
13971397
let mut debug_string =
13981398
format!("{prefix} for net = {net} and updated_feeds = {updated_feeds:?} ");
13991399
for feed in updated_feeds {
1400-
let round_index = rb_indices.get(feed).unwrap_or(&0);
1401-
debug_string.push_str(format!("{feed} = {round_index}; ").as_str());
1400+
let ring_buffer_index = rb_indices.get(feed).unwrap_or(&0);
1401+
debug_string.push_str(format!("{feed} = {ring_buffer_index}; ").as_str());
14021402
}
14031403
debug!(debug_string);
14041404
}
@@ -1417,8 +1417,8 @@ pub async fn increment_feeds_rb_indices(
14171417
.await;
14181418

14191419
for feed in updated_feeds {
1420-
let round_buffer_index = provider.rb_indices.entry(*feed).or_insert(0);
1421-
*round_buffer_index += 1;
1420+
let ring_buffer_index = provider.rb_indices.entry(*feed).or_insert(0);
1421+
*ring_buffer_index += 1;
14221422
}
14231423

14241424
log_rb_indices(
@@ -1429,8 +1429,8 @@ pub async fn increment_feeds_rb_indices(
14291429
)
14301430
.await;
14311431
}
1432-
// Since we update the round buffer index when we post the tx and before we
1433-
// receive its receipt if the tx fails we need to decrease the round indices.
1432+
// Since we update the ring buffer index when we post the tx and before we
1433+
// receive its receipt if the tx fails we need to decrease the ring buffer indices.
14341434
pub async fn decrement_feed_rb_indices(
14351435
updated_feeds: &Vec<EncodedFeedId>,
14361436
net: &str,
@@ -1445,9 +1445,9 @@ pub async fn decrement_feed_rb_indices(
14451445
.await;
14461446

14471447
for feed in updated_feeds {
1448-
let round_buffer_index = provider.rb_indices.entry(*feed).or_insert(0);
1449-
if *round_buffer_index > 0 {
1450-
*round_buffer_index -= 1;
1448+
let ring_buffer_index = provider.rb_indices.entry(*feed).or_insert(0);
1449+
if *ring_buffer_index > 0 {
1450+
*ring_buffer_index -= 1;
14511451
}
14521452
}
14531453

@@ -1575,7 +1575,7 @@ mod tests {
15751575
) {
15761576
let key1 = EncodedFeedId::new(0x1F as FeedId, 0);
15771577
let key2 = EncodedFeedId::new(0x0FFF as FeedId, 0);
1578-
let mut rb_indices = RoundBufferIndices::new();
1578+
let mut rb_indices = RingBufferIndices::new();
15791579
rb_indices.insert(key1, 7);
15801580
rb_indices.insert(key2, 8);
15811581
let mut strides_and_decimals = HashMap::new();

apps/sequencer/src/providers/provider.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use alloy::{
1414
use alloy_primitives::{keccak256, B256, U256};
1515
use alloy_u256_literal::u256;
1616
use blocksense_feeds_processing::adfs_gen_calldata::{
17-
calc_row_index, RoundBufferIndices, MAX_HISTORY_ELEMENTS_PER_FEED,
17+
calc_row_index, RingBufferIndices, MAX_HISTORY_ELEMENTS_PER_FEED,
1818
NUM_FEED_IDS_IN_RB_INDEX_RECORD,
1919
};
2020
use blocksense_utils::{EncodedFeedId, FeedId};
@@ -32,7 +32,7 @@ use blocksense_data_feeds::feeds_processing::{
3232
use blocksense_feed_registry::registry::FeedAggregateHistory;
3333
use blocksense_feed_registry::types::FeedType;
3434
use blocksense_metrics::{metrics::ProviderMetrics, process_provider_getter};
35-
use eyre::{eyre, Result};
35+
use eyre::{bail, eyre, Result};
3636
use paste::paste;
3737
use ringbuf::traits::{Consumer, Observer};
3838
use serde::{Deserialize, Serialize};
@@ -123,7 +123,7 @@ pub struct RpcProvider {
123123
pub feeds_variants: HashMap<EncodedFeedId, FeedVariant>,
124124
pub contracts: Vec<Contract>,
125125
pub rpc_url: Url,
126-
pub rb_indices: RoundBufferIndices,
126+
pub rb_indices: RingBufferIndices,
127127
num_tx_in_progress: u32,
128128
pub inflight: InflightObservations,
129129
}
@@ -241,18 +241,18 @@ async fn load_data_from_chain(
241241
let res = provider.load_rb_indices_from_chain(&feeds_config).await;
242242
match res {
243243
Ok(mut rb_indices) => {
244-
info!("Loaded round buffer indices from chain {network} = {rb_indices:?}");
244+
info!("Loaded ring buffer indices from chain {network} = {rb_indices:?}");
245245
for (_id, counter) in rb_indices.iter_mut() {
246246
*counter = (*counter + 1) % MAX_HISTORY_ELEMENTS_PER_FEED;
247247
}
248248
provider.rb_indices = rb_indices;
249249
}
250250
Err(err) => {
251-
error!("Error when loading round buffer indices for {network} = {err}");
251+
error!("Error when loading ring buffer indices for {network} = {err}");
252252
}
253253
}
254254
} else {
255-
warn!("Skipping loading round buffer indices from chain {network}");
255+
warn!("Skipping loading ring buffer indices from chain {network}");
256256
}
257257
}
258258

@@ -356,7 +356,7 @@ impl RpcProvider {
356356
feeds_variants,
357357
contracts,
358358
rpc_url,
359-
rb_indices: RoundBufferIndices::new(),
359+
rb_indices: RingBufferIndices::new(),
360360
num_tx_in_progress: 0,
361361
inflight: InflightObservations::new(),
362362
}
@@ -574,6 +574,7 @@ impl RpcProvider {
574574
r
575575
}
576576

577+
#[cfg(test)]
577578
pub async fn get_latest_values(
578579
&self,
579580
encoded_feed_ids: &[EncodedFeedId],
@@ -584,20 +585,16 @@ impl RpcProvider {
584585
let mut variants: HashMap<EncodedFeedId, FeedVariant> = HashMap::new();
585586
for encoded_feed_id in encoded_feed_ids.iter() {
586587
let Some(variant) = self.feeds_variants.get(encoded_feed_id) else {
587-
return Err(eyre!(
588-
"Unknown variant and number of digits for feed with encoded_feed_id = {encoded_feed_id}"
589-
));
588+
bail!("Unknown variant and number of digits for feed with encoded_feed_id = {encoded_feed_id}");
590589
};
591590
variants.insert(*encoded_feed_id, variant.clone());
592591
}
593592

594593
for encoded_feed_id in encoded_feed_ids {
595594
let Some(feed_variant) = variants.get(encoded_feed_id) else {
596-
return Err(eyre!(
597-
"Unknown variant and number of digits for feed with id (logical error) = {encoded_feed_id}"
598-
));
595+
bail!("Unknown variant and number of digits for feed with id (logical error) = {encoded_feed_id}");
599596
};
600-
// abi.encodePacked(bytes1(0x82), stride, uint120(id))
597+
// abi.encodePacked(bytes1(0x83), stride, uint120(id))
601598
let calldata = DynSolValue::Tuple(vec![
602599
DynSolValue::Uint(U256::from(0x83_u8), 8),
603600
DynSolValue::Uint(U256::from(feed_variant.stride), 8),
@@ -697,9 +694,7 @@ impl RpcProvider {
697694
Ok(res) => res,
698695
Err(e) => {
699696
warn!("Timed out on get_tx_retry_params while deploying contract {contract_name} in network `{network}`: {e}!");
700-
return Err(eyre!(
701-
"failed to get_tx_retry_params for network `{network}"
702-
));
697+
bail!("failed to get_tx_retry_params for network `{network}");
703698
}
704699
};
705700

@@ -1638,7 +1633,7 @@ mod tests {
16381633

16391634
let prov = sequencer_state.providers.read().await;
16401635
let p = prov.get(network).unwrap();
1641-
let round = p
1636+
let rb_index = p
16421637
.lock()
16431638
.await
16441639
.get_latest_rb_index(&encoded_feed_id)
@@ -1654,10 +1649,10 @@ mod tests {
16541649
.as_ref()
16551650
.expect("Expected correct value in contract for feed id {feed_id}");
16561651

1657-
// Assert that the value of the round counter in the contract is as expected.
1652+
// Assert that the value of the rb_index counter in the contract is as expected.
16581653
// Note: The sequencer tracks the index of the *next* slot to write,
16591654
// while the contract stores the index of the *last* written value.
1660-
assert_eq!(round.index, wrapped_val);
1655+
assert_eq!(rb_index.index, wrapped_val);
16611656

16621657
assert_eq!(val.value, new_update);
16631658

apps/sequencer/src/providers/reorg_tracking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl ReorgTracker {
398398
};
399399
};
400400

401-
// 3) If we detected divergence, resync round-buffer indices from chain
401+
// 3) If we detected divergence, resync ring buffer indices from chain
402402
if need_resync_indices {
403403
let mut provider = provider_mutex.lock().await;
404404
if let Some(contract) = provider.get_latest_contract() {

libs/feeds_processing/src/adfs_gen_calldata.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use once_cell::sync::Lazy;
1414
pub const MAX_HISTORY_ELEMENTS_PER_FEED: u64 = 8192;
1515
pub const NUM_FEED_IDS_IN_RB_INDEX_RECORD: u128 = 16;
1616

17-
pub type RoundBufferIndices = HashMap<EncodedFeedId, u64>; // for each key (feed_id) we store its round buffer index
17+
pub type RingBufferIndices = HashMap<EncodedFeedId, u64>; // for each key (feed_id) we store its ring buffer index
1818

1919
static STRIDES_SIZES: Lazy<HashMap<u8, u32>> = Lazy::new(|| {
2020
let mut map = HashMap::new(); // TODO: confirm the correct values for the strides we will support
@@ -67,9 +67,9 @@ pub fn calc_row_index(feed_id: FeedId, stride: Stride) -> alloy_primitives::Uint
6767
pub async fn adfs_serialize_updates(
6868
net: &str,
6969
feed_updates: &BatchedAggregatesToSend,
70-
rb_indices: Option<&RoundBufferIndices>,
70+
rb_indices: Option<&RingBufferIndices>,
7171
strides_and_decimals: HashMap<EncodedFeedId, FeedStrideAndDecimals>,
72-
feeds_rb_indexes: &mut HashMap<EncodedFeedId, u64>, /* The round buffer indices table for the relevant feeds. If the rb_indices are provided,
72+
feeds_rb_indexes: &mut HashMap<EncodedFeedId, u64>, /* The ring buffer indices table for the relevant feeds. If the rb_indices are provided,
7373
this map will be filled with the update count for each feed from it. If the
7474
rb_indices is None, feeds_rb_indexes will be used as the source of the updates
7575
count. */
@@ -179,8 +179,8 @@ pub async fn adfs_serialize_updates(
179179
result.append(&mut result_bytes);
180180
}
181181

182-
// In case feed_metrics is none, the feeds_rb_indexes contains all the round indices needed for serialization.
183-
// We use them to populate feeds_info map based on which the round indices will be serialized
182+
// In case feed_metrics is none, the feeds_rb_indexes contains all the ring buffer indices needed for serialization.
183+
// We use them to populate feeds_info map based on which the ring buffer indices will be serialized
184184
if rb_indices.is_none() {
185185
for (feed_id, rb_index) in feeds_rb_indexes.iter() {
186186
if let Some(strides_and_decimals) = strides_and_decimals.get(feed_id) {
@@ -306,7 +306,7 @@ pub mod tests {
306306
config_init: HashMap<EncodedFeedId, FeedStrideAndDecimals>,
307307
) -> (
308308
BatchedAggregatesToSend,
309-
RoundBufferIndices,
309+
RingBufferIndices,
310310
HashMap<EncodedFeedId, FeedStrideAndDecimals>,
311311
) {
312312
let updates = BatchedAggregatesToSend {
@@ -317,9 +317,9 @@ pub mod tests {
317317
.collect(),
318318
};
319319

320-
let mut rb_indices = RoundBufferIndices::new();
321-
for (feed_id, round) in rb_indices_init.iter() {
322-
rb_indices.insert(*feed_id, *round);
320+
let mut rb_indices = RingBufferIndices::new();
321+
for (feed_id, rb_index) in rb_indices_init.iter() {
322+
rb_indices.insert(*feed_id, *rb_index);
323323
}
324324

325325
(updates, rb_indices, config_init)
@@ -340,7 +340,7 @@ pub mod tests {
340340
(encoded_feed_id_for_stride_zero(4), "4890"),
341341
(encoded_feed_id_for_stride_zero(5), "5abc"),
342342
];
343-
let round_counters_init = vec![
343+
let rb_indices_init = vec![
344344
(encoded_feed_id_for_stride_zero(1), 6),
345345
(encoded_feed_id_for_stride_zero(2), 5),
346346
(encoded_feed_id_for_stride_zero(3), 4),
@@ -350,7 +350,7 @@ pub mod tests {
350350

351351
let config_init = default_config();
352352
let (updates, rb_indices, config) =
353-
setup_updates_rb_indices_and_config(&updates_init, &round_counters_init, config_init);
353+
setup_updates_rb_indices_and_config(&updates_init, &rb_indices_init, config_init);
354354

355355
let expected_result = "000000050102400c0107123432676435730002400501022456000260040102367800028003010248900002a00201025abc010000000000000500040003000200000000000000000000000000000000000000000e80000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000";
356356

@@ -394,7 +394,7 @@ pub mod tests {
394394
(encoded_feed_id_for_stride_zero(4), "4890"),
395395
(encoded_feed_id_for_stride_zero(5), "5abc"),
396396
];
397-
let round_counters_init = vec![
397+
let rb_indices_init = vec![
398398
(encoded_feed_id_for_stride_zero(1), 6),
399399
(encoded_feed_id_for_stride_zero(2), 5),
400400
(encoded_feed_id_for_stride_zero(3), 4),
@@ -404,7 +404,7 @@ pub mod tests {
404404

405405
let config_init = default_config();
406406
let (updates, mut rb_indices, config) =
407-
setup_updates_rb_indices_and_config(&updates_init, &round_counters_init, config_init);
407+
setup_updates_rb_indices_and_config(&updates_init, &rb_indices_init, config_init);
408408
rb_indices.insert(encoded_feed_id_for_stride_zero(6), 5);
409409

410410
let expected_result = "000000050102400c0107123432676435730002400501022456000260040102367800028003010248900002a00201025abc010000000000000500040003000200040000000000000000000000000000000000000e80000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000";
@@ -449,7 +449,7 @@ pub mod tests {
449449
(encoded_feed_id_for_stride_zero(4), "4890"),
450450
(encoded_feed_id_for_stride_zero(5), "5abc"),
451451
];
452-
let round_counters_init = vec![
452+
let rb_indices_init = vec![
453453
(encoded_feed_id_for_stride_zero(1), 6),
454454
(encoded_feed_id_for_stride_zero(2), 5),
455455
(encoded_feed_id_for_stride_zero(3), 4),
@@ -458,13 +458,13 @@ pub mod tests {
458458
];
459459

460460
let config_init = default_config();
461-
let (updates, mut round_counters, config) =
462-
setup_updates_rb_indices_and_config(&updates_init, &round_counters_init, config_init);
463-
round_counters.insert(encoded_feed_id_for_stride_zero(6), 5);
461+
let (updates, mut rb_indices, config) =
462+
setup_updates_rb_indices_and_config(&updates_init, &rb_indices_init, config_init);
463+
rb_indices.insert(encoded_feed_id_for_stride_zero(6), 5);
464464

465465
let expected_result = "000000050102400c0107123432676435730002400501022456000260040102367800028328010248900002a00201025abc010000000000000500040328000200040000000000000000000000000000000000000e80000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000";
466466

467-
let mut feeds_rounds = HashMap::new();
467+
let mut batch_rb_indices = HashMap::new();
468468

469469
// Call as it will be in the sequencer
470470
assert_eq!(
@@ -473,20 +473,20 @@ pub mod tests {
473473
adfs_serialize_updates(
474474
net,
475475
&updates,
476-
Some(&round_counters),
476+
Some(&rb_indices),
477477
config.clone(),
478-
&mut feeds_rounds,
478+
&mut batch_rb_indices,
479479
)
480480
.await
481481
.unwrap()
482482
)
483483
);
484484

485-
// Call as it will be in the reporter (feeds_rounds provided by the sequencer)
485+
// Call as it will be in the reporter (rb_indices provided by the sequencer)
486486
assert_eq!(
487487
expected_result,
488488
hex::encode(
489-
adfs_serialize_updates(net, &updates, None, config, &mut feeds_rounds,)
489+
adfs_serialize_updates(net, &updates, None, config, &mut rb_indices,)
490490
.await
491491
.unwrap()
492492
)

libs/feeds_processing/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ pub async fn validate(
422422
pub mod tests {
423423
use blocksense_utils::FeedId;
424424

425-
use crate::adfs_gen_calldata::RoundBufferIndices;
425+
use crate::adfs_gen_calldata::RingBufferIndices;
426426
use std::collections::HashSet;
427427

428428
use super::*;
@@ -507,7 +507,7 @@ pub mod tests {
507507
end_slot_timestamp: 1677654323,
508508
},
509509
];
510-
let mut feeds_rb_indices: RoundBufferIndices = HashMap::new();
510+
let mut feeds_rb_indices: RingBufferIndices = HashMap::new();
511511
feeds_rb_indices.insert(EncodedFeedId::new(1, 0), 1000);
512512
feeds_rb_indices.insert(EncodedFeedId::new(5, 0), 2000);
513513
feeds_rb_indices.insert(EncodedFeedId::new(11, 0), 3000);

0 commit comments

Comments
 (0)