Skip to content

Commit

Permalink
feat: implement KeyProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed Jun 21, 2024
1 parent 0a1a26e commit 2e30984
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
24 changes: 12 additions & 12 deletions coordinator/src/indexer_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use near_primitives::types::AccountId;
use registry_types::{Rule, StartBlock};

use crate::redis::KeyProvider;

#[derive(Debug, Clone, PartialEq)]
pub struct IndexerConfig {
pub account_id: AccountId,
Expand All @@ -13,6 +15,16 @@ pub struct IndexerConfig {
pub created_at_block_height: u64,
}

impl KeyProvider for IndexerConfig {
fn account_id(&self) -> String {
self.account_id.to_string()
}

fn function_name(&self) -> String {
self.function_name.clone()
}
}

#[cfg(test)]
impl Default for IndexerConfig {
fn default() -> Self {
Expand All @@ -37,18 +49,6 @@ impl IndexerConfig {
format!("{}/{}", self.account_id, self.function_name)
}

pub fn get_redis_stream_key(&self) -> String {
format!("{}:block_stream", self.get_full_name())
}

pub fn get_last_published_block_key(&self) -> String {
format!("{}:last_published_block", self.get_full_name())
}

pub fn get_state_key(&self) -> String {
format!("{}:state", self.get_full_name())
}

pub fn get_registry_version(&self) -> u64 {
self.updated_at_block_height
.unwrap_or(self.created_at_block_height)
Expand Down
17 changes: 7 additions & 10 deletions coordinator/src/indexer_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Context;
use near_primitives::types::AccountId;

use crate::indexer_config::IndexerConfig;
use crate::redis::RedisClient;
use crate::redis::{KeyProvider, RedisClient};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub enum ProvisionedState {
Expand Down Expand Up @@ -32,16 +32,13 @@ pub struct IndexerState {
pub provisioned_state: ProvisionedState,
}

// FIX `IndexerConfig` does not exist after an Indexer is deleted, and we need a way to
// construct the state key without it. But, this isn't ideal as we now have two places which
// define this key - we need to consolidate these somehow.
impl IndexerState {
pub fn get_state_key(&self) -> String {
format!("{}/{}:state", self.account_id, self.function_name)
impl KeyProvider for IndexerState {
fn account_id(&self) -> String {
self.account_id.to_string()
}

pub fn get_redis_stream_key(&self) -> String {
format!("{}/{}:block_stream", self.account_id, self.function_name)
fn function_name(&self) -> String {
self.function_name.clone()
}
}

Expand Down Expand Up @@ -300,7 +297,7 @@ mod tests {
))
});
redis_client
.expect_set_indexer_state()
.expect_set_indexer_state::<IndexerConfig>()
.with(
predicate::always(),
predicate::eq("{\"account_id\":\"morgs.near\",\"function_name\":\"test\",\"block_stream_synced_at\":123,\"enabled\":false,\"provisioned_state\":\"Provisioned\"}".to_string()),
Expand Down
4 changes: 3 additions & 1 deletion coordinator/src/synchroniser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,9 @@ mod test {
let last_published_block = 1;

let mut redis_client = RedisClient::default();
redis_client.expect_clear_block_stream().never();
redis_client
.expect_clear_block_stream::<IndexerConfig>()
.never();
redis_client
.expect_get_last_published_block()
.with(eq(config.clone()))
Expand Down

0 comments on commit 2e30984

Please sign in to comment.