Skip to content

Commit

Permalink
new database crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurii Koba authored and Yurii Koba committed Jun 21, 2024
1 parent 2c23a32 commit c774bd6
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 326 deletions.
212 changes: 24 additions & 188 deletions database-new/src/base/state_indexer.rs
Original file line number Diff line number Diff line change
@@ -1,90 +1,5 @@
#[async_trait::async_trait]
pub trait StateIndexerDbManager {
async fn add_state_changes(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
key: &[u8],
value: &[u8],
) -> anyhow::Result<()>;

async fn delete_state_changes(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
key: &[u8],
) -> anyhow::Result<()>;

async fn add_access_key(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
public_key: &[u8],
access_key: &[u8],
) -> anyhow::Result<()>;

async fn delete_access_key(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
public_key: &[u8],
) -> anyhow::Result<()>;

async fn get_access_keys(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
) -> anyhow::Result<std::collections::HashMap<String, Vec<u8>>>;

async fn add_account_access_keys(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
public_key: &[u8],
access_key: Option<&[u8]>,
) -> anyhow::Result<()>;

async fn update_account_access_keys(
&self,
account_id: String,
block_height: u64,
account_keys: std::collections::HashMap<String, Vec<u8>>,
) -> anyhow::Result<()>;

async fn add_contract_code(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
code: &[u8],
) -> anyhow::Result<()>;

async fn delete_contract_code(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()>;

async fn add_account(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
account: Vec<u8>,
) -> anyhow::Result<()>;

async fn delete_account(
&self,
account_id: near_indexer_primitives::types::AccountId,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()>;

async fn add_block(
&self,
block_height: u64,
Expand All @@ -108,7 +23,9 @@ pub trait StateIndexerDbManager {
) -> anyhow::Result<u64>;

async fn update_meta(&self, indexer_id: &str, block_height: u64) -> anyhow::Result<()>;

async fn get_last_processed_block_height(&self, indexer_id: &str) -> anyhow::Result<u64>;

async fn add_validators(
&self,
epoch_id: near_indexer_primitives::CryptoHash,
Expand Down Expand Up @@ -148,112 +65,31 @@ pub trait StateIndexerDbManager {
Ok(())
}

async fn save_state_change(
async fn save_state_changes_data(
&self,
state_change: &near_primitives::views::StateChangeWithCauseView,
state_changes: Vec<near_primitives::views::StateChangeWithCauseView>,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()> {
match &state_change.value {
near_primitives::views::StateChangeValueView::DataUpdate {
account_id,
key,
value,
} => {
self.add_state_changes(
account_id.clone(),
block_height,
block_hash,
key.as_ref(),
value.as_ref(),
)
.await?
}
near_primitives::views::StateChangeValueView::DataDeletion { account_id, key } => {
self.delete_state_changes(
account_id.clone(),
block_height,
block_hash,
key.as_ref(),
)
.await?
}
near_primitives::views::StateChangeValueView::AccessKeyUpdate {
account_id,
public_key,
access_key,
} => {
let data_key = borsh::to_vec(&public_key)?;
let data_value = borsh::to_vec(&access_key)?;
let add_access_key_future = self.add_access_key(
account_id.clone(),
block_height,
block_hash,
&data_key,
&data_value,
);
) -> anyhow::Result<()>;

#[cfg(feature = "account_access_keys")]
{
let add_account_access_keys_future = self.add_account_access_keys(
account_id.clone(),
block_height,
&data_key,
Some(&data_value),
);
futures::try_join!(add_access_key_future, add_account_access_keys_future)?;
}
#[cfg(not(feature = "account_access_keys"))]
add_access_key_future.await?;
}
near_primitives::views::StateChangeValueView::AccessKeyDeletion {
account_id,
public_key,
} => {
let data_key = borsh::to_vec(&public_key)?;
let delete_access_key_future =
self.delete_access_key(account_id.clone(), block_height, block_hash, &data_key);
async fn save_state_changes_access_key(
&self,
state_changes: Vec<near_primitives::views::StateChangeWithCauseView>,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()>;

#[cfg(feature = "account_access_keys")]
{
let delete_account_access_keys_future = self.add_account_access_keys(
account_id.clone(),
block_height,
&data_key,
None,
);
futures::try_join!(
delete_access_key_future,
delete_account_access_keys_future
)?;
}
#[cfg(not(feature = "account_access_keys"))]
delete_access_key_future.await?;
}
near_primitives::views::StateChangeValueView::ContractCodeUpdate {
account_id,
code,
} => {
self.add_contract_code(account_id.clone(), block_height, block_hash, code.as_ref())
.await?
}
near_primitives::views::StateChangeValueView::ContractCodeDeletion { account_id } => {
self.delete_contract_code(account_id.clone(), block_height, block_hash)
.await?
}
near_primitives::views::StateChangeValueView::AccountUpdate {
account_id,
account,
} => {
let value = borsh::to_vec(&near_primitives::account::Account::from(account))?;
self.add_account(account_id.clone(), block_height, block_hash, value)
.await?
}
near_primitives::views::StateChangeValueView::AccountDeletion { account_id } => {
self.delete_account(account_id.clone(), block_height, block_hash)
.await?
}
}
Ok(())
}
async fn save_state_changes_contract(
&self,
state_changes: Vec<near_primitives::views::StateChangeWithCauseView>,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()>;

async fn save_state_changes_account(
&self,
state_changes: Vec<near_primitives::views::StateChangeWithCauseView>,
block_height: u64,
block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()>;
}
Loading

0 comments on commit c774bd6

Please sign in to comment.