Skip to content

Commit

Permalink
More doc-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Dec 27, 2024
1 parent 298c052 commit c318477
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 0 additions & 2 deletions gear-programs/historical-proxy/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub struct HistoricalProxyProgram(RefCell<state::ProxyState>);

#[sails_rs::program]
impl HistoricalProxyProgram {
// Program's constructor
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let exec_context = GStdExecContext::new();
Expand All @@ -28,7 +27,6 @@ impl HistoricalProxyProgram {
}))
}

// Exposed service
pub fn historical_proxy(&self) -> service::HistoricalProxyService<GStdExecContext> {
service::HistoricalProxyService::new(&self.0, GStdExecContext::new())
}
Expand Down
21 changes: 15 additions & 6 deletions gear-programs/historical-proxy/app/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
use super::error::ProxyError;
use super::{ActorId, Vec};

pub type Slot = u64;

/// State of the Historical Proxy service.
pub struct ProxyState {
pub admin: ActorId,
pub endpoints: EndpointList,
}

/// Mapping between endpoints and Ethereum slots they're active from.
///
/// ### Invariant
///
/// Endpoints are stored in ascending order, sorted by slot number.
#[derive(Default)]
pub struct EndpointList(Vec<(Slot, ActorId)>);

impl Default for EndpointList {
fn default() -> Self {
Self::new()
}
}

impl EndpointList {
pub fn new() -> Self {
Self(Vec::with_capacity(2))
}

/// Add new endpoint that will be active starting from `slot`(inclusive).
///
/// Panics if provided `slot` <= greatest already existing slot.
pub fn push(&mut self, slot: Slot, actor_id: ActorId) {
assert!(
self.0.is_empty() || self.0[self.0.len() - 1].0 < slot,
Expand All @@ -28,10 +33,14 @@ impl EndpointList {
self.0.push((slot, actor_id));
}

/// Get list of currently active endpoints. Returns `Vec<(Slot, ActorId)>`
/// where `ActorId` means endpoint address and `Slot` means Ethereum slot
/// this endpoint is active from(inclusive).
pub fn endpoints(&self) -> Vec<(Slot, ActorId)> {
self.0.clone()
}

/// Get endpoint for the specified slot. Will return error if endpoint is not found.
pub fn endpoint_for(&self, slot: Slot) -> Result<ActorId, ProxyError> {
match self.0.binary_search_by(|(s, _)| s.cmp(&slot)) {
Ok(i) => Ok(self.0[i].1),
Expand Down

0 comments on commit c318477

Please sign in to comment.