Skip to content

Commit

Permalink
Improvement to handle ptotocol config (#283)
Browse files Browse the repository at this point in the history
* Improvement to handle ptotocol config

* add comment

* fmt
  • Loading branch information
kobayurii committed Jun 24, 2024
1 parent a08cbf1 commit 6b5c846
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 501 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ near-parameters = { git = 'https://github.com/kobayurii/nearcore.git', branch =
near-vm-runner = { git = 'https://github.com/kobayurii/nearcore.git', branch = "1.40.0-fork", features = [
"wasmer0_vm",
"wasmer2_vm",
"near_vm",
"wasmtime_vm",
"near_vm",
] }

near-lake-framework = { git = 'https://github.com/kobayurii/near-lake-framework-rs.git', branch = '0.7.14' }
Expand Down
7 changes: 0 additions & 7 deletions database/src/base/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,4 @@ pub trait ReaderDbManager {
block_height: near_primitives::types::BlockHeight,
method_name: &str,
) -> anyhow::Result<readnode_primitives::EpochValidatorsInfo>;

/// Return protocol config by the given epoch id
async fn get_protocol_config_by_epoch_id(
&self,
epoch_id: near_primitives::hash::CryptoHash,
method_name: &str,
) -> anyhow::Result<near_chain_configs::ProtocolConfigView>;
}
13 changes: 0 additions & 13 deletions database/src/base/state_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,6 @@ pub trait StateIndexerDbManager {
epoch_height: u64,
epoch_start_height: u64,
validators_info: &near_primitives::views::EpochValidatorInfo,
) -> anyhow::Result<()>;

async fn add_protocol_config(
&self,
epoch_id: near_indexer_primitives::CryptoHash,
epoch_height: u64,
epoch_start_height: u64,
protocol_config: &near_chain_configs::ProtocolConfigView,
) -> anyhow::Result<()>;

async fn update_epoch_end_height(
&self,
epoch_id: near_indexer_primitives::CryptoHash,
epoch_end_block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()>;

Expand Down
63 changes: 0 additions & 63 deletions database/src/postgres/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,19 +696,6 @@ impl Validators {
Ok(())
}

pub async fn update_epoch_end_height(
mut conn: crate::postgres::PgAsyncConn,
epoch_id: near_indexer_primitives::CryptoHash,
epoch_end_height: bigdecimal::BigDecimal,
) -> anyhow::Result<()> {
diesel::update(validators::table)
.filter(validators::epoch_id.eq(epoch_id.to_string()))
.set(validators::epoch_end_height.eq(epoch_end_height))
.execute(&mut conn)
.await?;
Ok(())
}

pub async fn get_validators(
mut conn: crate::postgres::PgAsyncConn,
epoch_id: near_indexer_primitives::CryptoHash,
Expand All @@ -734,53 +721,3 @@ impl Validators {
Ok(response)
}
}

#[derive(Insertable, Queryable, Selectable)]
#[diesel(table_name = protocol_configs)]
pub struct ProtocolConfig {
pub epoch_id: String,
pub epoch_height: bigdecimal::BigDecimal,
pub epoch_start_height: bigdecimal::BigDecimal,
pub epoch_end_height: Option<bigdecimal::BigDecimal>,
pub protocol_config: serde_json::Value,
}

impl ProtocolConfig {
pub async fn insert_or_ignore(
&self,
mut conn: crate::postgres::PgAsyncConn,
) -> anyhow::Result<()> {
diesel::insert_into(protocol_configs::table)
.values(self)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
Ok(())
}

pub async fn update_epoch_end_height(
mut conn: crate::postgres::PgAsyncConn,
epoch_id: near_indexer_primitives::CryptoHash,
epoch_end_height: bigdecimal::BigDecimal,
) -> anyhow::Result<()> {
diesel::update(protocol_configs::table)
.filter(protocol_configs::epoch_id.eq(epoch_id.to_string()))
.set(protocol_configs::epoch_end_height.eq(epoch_end_height))
.execute(&mut conn)
.await?;
Ok(())
}

pub async fn get_protocol_config(
mut conn: crate::postgres::PgAsyncConn,
epoch_id: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<Self> {
let response = protocol_configs::table
.filter(protocol_configs::epoch_id.eq(epoch_id.to_string()))
.select(Self::as_select())
.first(&mut conn)
.await?;

Ok(response)
}
}
16 changes: 0 additions & 16 deletions database/src/postgres/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,4 @@ impl crate::ReaderDbManager for PostgresDBManager {
validators_info,
})
}

async fn get_protocol_config_by_epoch_id(
&self,
epoch_id: near_indexer_primitives::CryptoHash,
_method_name: &str,
) -> anyhow::Result<near_chain_configs::ProtocolConfigView> {
let protocol_config = crate::models::ProtocolConfig::get_protocol_config(
Self::get_connection(&self.pg_pool).await?,
epoch_id,
)
.await?;
let (protocol_config,) = serde_json::from_value::<(near_chain_configs::ProtocolConfigView,)>(
protocol_config.protocol_config,
)?;
Ok(protocol_config)
}
}
43 changes: 3 additions & 40 deletions database/src/postgres/state_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,55 +312,18 @@ impl crate::StateIndexerDbManager for PostgresDBManager {
epoch_height: u64,
epoch_start_height: u64,
validators_info: &near_primitives::views::EpochValidatorInfo,
epoch_end_block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()> {
let epoch_end_height = self.get_block_by_hash(epoch_end_block_hash).await?;
crate::models::Validators {
epoch_id: epoch_id.to_string(),
epoch_height: bigdecimal::BigDecimal::from(epoch_height),
epoch_start_height: bigdecimal::BigDecimal::from(epoch_start_height),
epoch_end_height: None,
epoch_end_height: Some(bigdecimal::BigDecimal::from(epoch_end_height)),
validators_info: serde_json::to_value(validators_info)?,
}
.insert_or_ignore(Self::get_connection(&self.pg_pool).await?)
.await?;
Ok(())
}

async fn add_protocol_config(
&self,
epoch_id: near_indexer_primitives::CryptoHash,
epoch_height: u64,
epoch_start_height: u64,
protocol_config: &near_chain_configs::ProtocolConfigView,
) -> anyhow::Result<()> {
crate::models::ProtocolConfig {
epoch_id: epoch_id.to_string(),
epoch_height: bigdecimal::BigDecimal::from(epoch_height),
epoch_start_height: bigdecimal::BigDecimal::from(epoch_start_height),
epoch_end_height: None,
protocol_config: serde_json::to_value(protocol_config)?,
}
.insert_or_ignore(Self::get_connection(&self.pg_pool).await?)
.await?;
Ok(())
}

async fn update_epoch_end_height(
&self,
epoch_id: near_indexer_primitives::CryptoHash,
epoch_end_block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()> {
let epoch_end_height = self.get_block_by_hash(epoch_end_block_hash).await?;
let validators_future = crate::models::Validators::update_epoch_end_height(
Self::get_connection(&self.pg_pool).await?,
epoch_id,
bigdecimal::BigDecimal::from(epoch_end_height),
);
let protocol_config_future = crate::models::ProtocolConfig::update_epoch_end_height(
Self::get_connection(&self.pg_pool).await?,
epoch_id,
bigdecimal::BigDecimal::from(epoch_end_height),
);
futures::future::try_join(validators_future, protocol_config_future).await?;
Ok(())
}
}
28 changes: 0 additions & 28 deletions database/src/scylladb/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub struct ScyllaDBManager {
get_indexing_transaction_receipts: PreparedStatement,
get_stored_at_block_height_and_shard_id_by_block_height: PreparedStatement,
get_validators_by_epoch_id: PreparedStatement,
get_protocol_config_by_epoch_id: PreparedStatement,
get_validators_by_end_block_height: PreparedStatement,
}

Expand Down Expand Up @@ -142,10 +141,6 @@ impl ScyllaStorageManager for ScyllaDBManager {
&scylla_db_session,
"SELECT epoch_height, validators_info FROM state_indexer.validators WHERE epoch_id = ?",
).await?,
get_protocol_config_by_epoch_id: Self::prepare_read_query(
&scylla_db_session,
"SELECT protocol_config FROM state_indexer.protocol_configs WHERE epoch_id = ?",
).await?,
get_validators_by_end_block_height: Self::prepare_read_query(
&scylla_db_session,
"SELECT epoch_id, epoch_height, validators_info FROM state_indexer.validators WHERE epoch_end_height = ?",
Expand Down Expand Up @@ -641,29 +636,6 @@ impl crate::ReaderDbManager for ScyllaDBManager {
})
}

async fn get_protocol_config_by_epoch_id(
&self,
epoch_id: near_primitives::hash::CryptoHash,
method_name: &str,
) -> anyhow::Result<near_chain_configs::ProtocolConfigView> {
crate::metrics::DATABASE_READ_QUERIES
.with_label_values(&[method_name, "state_indexer.protocol_configs"])
.inc();
let (protocol_config,) = Self::execute_prepared_query(
&self.scylla_session,
&self.get_protocol_config_by_epoch_id,
(epoch_id.to_string(),),
)
.await?
.single_row()?
.into_typed::<(String,)>()?;

let protocol_config: near_chain_configs::ProtocolConfigView =
serde_json::from_str(&protocol_config)?;

Ok(protocol_config)
}

async fn get_validators_by_end_block_height(
&self,
block_height: near_primitives::types::BlockHeight,
Expand Down
72 changes: 4 additions & 68 deletions database/src/scylladb/state_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ pub struct ScyllaDBManager {
add_chunk: PreparedStatement,

add_validators: PreparedStatement,
add_protocol_config: PreparedStatement,
update_validators_epoch_end_height: PreparedStatement,
update_protocol_config_epoch_end_height: PreparedStatement,

add_account_state: PreparedStatement,

Expand Down Expand Up @@ -372,24 +369,7 @@ impl ScyllaStorageManager for ScyllaDBManager {
&scylla_db_session,
"INSERT INTO state_indexer.validators
(epoch_id, epoch_height, epoch_start_height, epoch_end_height, validators_info)
VALUES (?, ?, ?, NULL, ?)",
)
.await?,
add_protocol_config: Self::prepare_write_query(
&scylla_db_session,
"INSERT INTO state_indexer.protocol_configs
(epoch_id, epoch_height, epoch_start_height, epoch_end_height, protocol_config)
VALUES (?, ?, ?, NULL, ?)",
)
.await?,
update_validators_epoch_end_height: Self::prepare_write_query(
&scylla_db_session,
"UPDATE state_indexer.validators SET epoch_end_height = ? WHERE epoch_id = ?",
)
.await?,
update_protocol_config_epoch_end_height: Self::prepare_write_query(
&scylla_db_session,
"UPDATE state_indexer.protocol_configs SET epoch_end_height = ? WHERE epoch_id = ?",
VALUES (?, ?, ?, ?, ?)",
)
.await?,
add_account_state: Self::prepare_write_query(
Expand Down Expand Up @@ -751,65 +731,21 @@ impl crate::StateIndexerDbManager for ScyllaDBManager {
epoch_height: u64,
epoch_start_height: u64,
validators_info: &near_primitives::views::EpochValidatorInfo,
epoch_end_block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()> {
let epoch_end_height = self.get_block_by_hash(epoch_end_block_hash).await?;
Self::execute_prepared_query(
&self.scylla_session,
&self.add_validators,
(
epoch_id.to_string(),
num_bigint::BigInt::from(epoch_height),
num_bigint::BigInt::from(epoch_start_height),
num_bigint::BigInt::from(epoch_end_height),
serde_json::to_string(validators_info)?,
),
)
.await?;
Ok(())
}

async fn add_protocol_config(
&self,
epoch_id: near_indexer_primitives::CryptoHash,
epoch_height: u64,
epoch_start_height: u64,
protocol_config: &near_chain_configs::ProtocolConfigView,
) -> anyhow::Result<()> {
Self::execute_prepared_query(
&self.scylla_session,
&self.add_protocol_config,
(
epoch_id.to_string(),
num_bigint::BigInt::from(epoch_height),
num_bigint::BigInt::from(epoch_start_height),
serde_json::to_string(protocol_config)?,
),
)
.await?;
Ok(())
}

async fn update_epoch_end_height(
&self,
epoch_id: near_indexer_primitives::CryptoHash,
epoch_end_block_hash: near_indexer_primitives::CryptoHash,
) -> anyhow::Result<()> {
let epoch_end_height = self.get_block_by_hash(epoch_end_block_hash).await?;
let validators_future = Self::execute_prepared_query(
&self.scylla_session,
&self.update_validators_epoch_end_height,
(
num_bigint::BigInt::from(epoch_end_height),
epoch_id.to_string(),
),
);
let protocol_config_future = Self::execute_prepared_query(
&self.scylla_session,
&self.update_protocol_config_epoch_end_height,
(
num_bigint::BigInt::from(epoch_end_height),
epoch_id.to_string(),
),
);
futures::future::try_join(validators_future, protocol_config_future).await?;
Ok(())
}
}
Loading

0 comments on commit 6b5c846

Please sign in to comment.