diff --git a/Cargo.toml b/Cargo.toml index 9c59624e..604e6613 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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' } diff --git a/database/src/base/rpc_server.rs b/database/src/base/rpc_server.rs index 1dc710c4..11cc7d9b 100644 --- a/database/src/base/rpc_server.rs +++ b/database/src/base/rpc_server.rs @@ -132,11 +132,4 @@ pub trait ReaderDbManager { block_height: near_primitives::types::BlockHeight, method_name: &str, ) -> anyhow::Result; - - /// 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; } diff --git a/database/src/base/state_indexer.rs b/database/src/base/state_indexer.rs index 6c19e9ba..576bf6ca 100644 --- a/database/src/base/state_indexer.rs +++ b/database/src/base/state_indexer.rs @@ -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<()>; diff --git a/database/src/postgres/models.rs b/database/src/postgres/models.rs index df2e6e1e..aed5b949 100644 --- a/database/src/postgres/models.rs +++ b/database/src/postgres/models.rs @@ -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, @@ -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, - 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 { - 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) - } -} diff --git a/database/src/postgres/rpc_server.rs b/database/src/postgres/rpc_server.rs index 21ce11bc..a9a66096 100644 --- a/database/src/postgres/rpc_server.rs +++ b/database/src/postgres/rpc_server.rs @@ -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 { - 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) - } } diff --git a/database/src/postgres/state_indexer.rs b/database/src/postgres/state_indexer.rs index b5b71c22..6ae90229 100644 --- a/database/src/postgres/state_indexer.rs +++ b/database/src/postgres/state_indexer.rs @@ -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(()) - } } diff --git a/database/src/scylladb/rpc_server.rs b/database/src/scylladb/rpc_server.rs index b9f8e4f8..709d1c3b 100644 --- a/database/src/scylladb/rpc_server.rs +++ b/database/src/scylladb/rpc_server.rs @@ -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, } @@ -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 = ?", @@ -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 { - 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, diff --git a/database/src/scylladb/state_indexer.rs b/database/src/scylladb/state_indexer.rs index 1892f286..1db88cff 100644 --- a/database/src/scylladb/state_indexer.rs +++ b/database/src/scylladb/state_indexer.rs @@ -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, @@ -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( @@ -751,7 +731,9 @@ 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, @@ -759,57 +741,11 @@ impl crate::StateIndexerDbManager for ScyllaDBManager { 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(()) - } } diff --git a/epoch-indexer/src/lib.rs b/epoch-indexer/src/lib.rs index 7e4bd92a..50d9b747 100644 --- a/epoch-indexer/src/lib.rs +++ b/epoch-indexer/src/lib.rs @@ -29,69 +29,16 @@ pub async fn get_epoch_validators( } } -/// util to fetch protocol config by epoch_start_height -/// try_another_blocks - special flag to try another blocks if we have error -/// in case of error we try to fetch protocol config for next block -pub async fn get_protocol_config( - epoch_start_height: u64, - client: &near_jsonrpc_client::JsonRpcClient, - try_another_blocks: bool, -) -> anyhow::Result { - let mut attempt_counter = 0; - let mut block_height = epoch_start_height; - loop { - let params = - near_jsonrpc_client::methods::EXPERIMENTAL_protocol_config::RpcProtocolConfigRequest { - block_reference: near_primitives::types::BlockReference::BlockId( - near_indexer_primitives::types::BlockId::Height(block_height), - ), - }; - match client.call(params).await { - Ok(response) => return Ok(response), - Err(e) => { - attempt_counter += 1; - if try_another_blocks { - block_height += 1; - } - tracing::info!( - "Attempt: {}. Epoch_start_height {}. Error fetching protocol config: {:?}", - attempt_counter, - block_height, - e - ); - if attempt_counter > 20 { - anyhow::bail!( - "Failed to fetch epoch protocol config for epoch_start_height: {}", - epoch_start_height - ) - } - } - } - } -} pub async fn get_epoch_info_by_id( epoch_id: CryptoHash, rpc_client: &near_jsonrpc_client::JsonRpcClient, ) -> anyhow::Result { let validators_info = get_epoch_validators(epoch_id, rpc_client).await?; - let protocol_config = - match get_protocol_config(validators_info.epoch_start_height, rpc_client, false).await { - Ok(protocol_config) => protocol_config, - Err(e) => { - tracing::warn!( - "Error fetching protocol config: {:?}. Try with another blocks", - e - ); - get_protocol_config(validators_info.epoch_start_height, rpc_client, true).await? - } - }; Ok(readnode_primitives::IndexedEpochInfo { epoch_id, epoch_height: validators_info.epoch_height, epoch_start_height: validators_info.epoch_start_height, - epoch_end_height: None, validators_info, - protocol_config, }) } @@ -187,28 +134,11 @@ pub async fn get_next_epoch( ) } -pub async fn update_epoch_end_height( - db_manager: &(impl database::StateIndexerDbManager + Sync + Send + 'static), - epoch_id: Option, - epoch_end_block_hash: CryptoHash, -) -> anyhow::Result<()> { - if let Some(epoch_id) = epoch_id { - tracing::info!( - "Update epoch_end_height: epoch_id: {:?}, epoch_end_height_hash: {}", - epoch_id, - epoch_end_block_hash - ); - db_manager - .update_epoch_end_height(epoch_id, epoch_end_block_hash) - .await?; - } - Ok(()) -} - pub async fn save_epoch_info( epoch: &readnode_primitives::IndexedEpochInfo, db_manager: &(impl database::StateIndexerDbManager + Sync + Send + 'static), handled_epoch_height: Option, + epoch_end_block_hash: CryptoHash, ) -> anyhow::Result<()> { let epoch_height = if let Some(epoch_height) = handled_epoch_height { epoch_height @@ -216,21 +146,15 @@ pub async fn save_epoch_info( epoch.epoch_height }; - let save_validators_future = db_manager.add_validators( - epoch.epoch_id, - epoch_height, - epoch.epoch_start_height, - &epoch.validators_info, - ); - - let save_protocol_config_future = db_manager.add_protocol_config( - epoch.epoch_id, - epoch_height, - epoch.epoch_start_height, - &epoch.protocol_config, - ); - - futures::try_join!(save_validators_future, save_protocol_config_future)?; + db_manager + .add_validators( + epoch.epoch_id, + epoch_height, + epoch.epoch_start_height, + &epoch.validators_info, + epoch_end_block_hash, + ) + .await?; tracing::info!( "Save epoch info: epoch_id: {:?}, epoch_height: {:?}, epoch_start_height: {}", diff --git a/epoch-indexer/src/main.rs b/epoch-indexer/src/main.rs index 52c8e022..62782b54 100644 --- a/epoch-indexer/src/main.rs +++ b/epoch-indexer/src/main.rs @@ -27,19 +27,15 @@ async fn index_epochs( } }; - if let Err(e) = - epoch_indexer::save_epoch_info(&epoch_info.epoch_info, &db_manager, None).await - { - tracing::warn!("Error saving epoch info: {:?}", e); - } - if let Err(e) = epoch_indexer::update_epoch_end_height( + if let Err(e) = epoch_indexer::save_epoch_info( + &epoch.epoch_info, &db_manager, - epoch_info.previous_epoch_id, + None, epoch_info.next_epoch_id, ) .await { - tracing::warn!("Error update epoch_end_height: {:?}", e); + tracing::warn!("Error saving epoch info: {:?}", e); } db_manager .update_meta(indexer_id, epoch.epoch_info.epoch_start_height) @@ -96,7 +92,6 @@ async fn main() -> anyhow::Result<()> { .await? } }; - epoch_indexer::save_epoch_info(&epoch.epoch_info, &db_manager, None).await?; index_epochs( &s3_client, diff --git a/near-state-indexer/src/main.rs b/near-state-indexer/src/main.rs index 7c97166e..1ab618e6 100644 --- a/near-state-indexer/src/main.rs +++ b/near-state-indexer/src/main.rs @@ -125,34 +125,18 @@ async fn handle_epoch( Ok(()) } else { // If epoch changed, we need to save epoch info and update epoch_end_height - let validators_info_future = utils::fetch_epoch_validators_info(stats_epoch_id, client); - let protocol_config_future = utils::fetch_epoch_protocol_config(client); - - let (validators_info, protocol_config) = - futures::try_join!(validators_info_future, protocol_config_future)?; - - let save_validators_future = db_manager.add_validators( - stats_epoch_id, - stats_current_epoch_height, - validators_info.epoch_start_height, - &validators_info, - ); - - let save_protocol_config_future = db_manager.add_protocol_config( - stats_epoch_id, - stats_current_epoch_height, - validators_info.epoch_start_height, - &protocol_config, - ); - - let update_epoch_end_height_future = - db_manager.update_epoch_end_height(stats_epoch_id, next_epoch_id); - - futures::try_join!( - save_validators_future, - save_protocol_config_future, - update_epoch_end_height_future - )?; + let validators_info = + utils::fetch_epoch_validators_info(stats_epoch_id, client).await?; + + db_manager + .add_validators( + stats_epoch_id, + stats_current_epoch_height, + validators_info.epoch_start_height, + &validators_info, + next_epoch_id, + ) + .await?; Ok(()) } diff --git a/near-state-indexer/src/utils.rs b/near-state-indexer/src/utils.rs index 8336a03e..a74a4af8 100644 --- a/near-state-indexer/src/utils.rs +++ b/near-state-indexer/src/utils.rs @@ -19,19 +19,6 @@ pub(crate) async fn fetch_epoch_validators_info( .await??) } -pub(crate) async fn fetch_epoch_protocol_config( - client: &actix::Addr, -) -> anyhow::Result { - Ok(client - .send( - near_client::GetProtocolConfig(near_primitives::types::BlockReference::Finality( - near_primitives::types::Finality::Final, - )) - .with_span_context(), - ) - .await??) -} - /// Fetches the status to retrieve `latest_block_height` to determine if we need to fetch /// entire block or we already fetched this block. pub(crate) async fn fetch_latest_block( diff --git a/readnode-primitives/src/lib.rs b/readnode-primitives/src/lib.rs index aba339d1..f803e154 100644 --- a/readnode-primitives/src/lib.rs +++ b/readnode-primitives/src/lib.rs @@ -203,9 +203,7 @@ pub struct IndexedEpochInfo { pub epoch_id: CryptoHash, pub epoch_height: u64, pub epoch_start_height: u64, - pub epoch_end_height: Option, pub validators_info: views::EpochValidatorInfo, - pub protocol_config: near_chain_configs::ProtocolConfigView, } #[derive(Debug)] diff --git a/rpc-server/src/modules/blocks/mod.rs b/rpc-server/src/modules/blocks/mod.rs index c2019427..5dee3e5c 100644 --- a/rpc-server/src/modules/blocks/mod.rs +++ b/rpc-server/src/modules/blocks/mod.rs @@ -182,12 +182,6 @@ pub struct CurrentValidatorInfo { pub validators: near_primitives::views::EpochValidatorInfo, } -// Struct to store the protocol config for current epoch. -#[derive(Debug, Clone)] -pub struct CurrentEpochConfig { - pub epoch_config: near_primitives::epoch_manager::EpochConfig, -} - // Struct to store the optimistic changes in the block. #[derive(Debug, Clone)] pub struct OptimisticChanges { @@ -291,7 +285,6 @@ pub struct BlocksInfoByFinality { pub optimistic_block: futures_locks::RwLock, pub optimistic_changes: futures_locks::RwLock, pub current_validators: futures_locks::RwLock, - pub current_epoch_config: futures_locks::RwLock, } impl BlocksInfoByFinality { @@ -302,12 +295,10 @@ impl BlocksInfoByFinality { let final_block_future = crate::utils::get_final_block(near_rpc_client, false); let optimistic_block_future = crate::utils::get_final_block(near_rpc_client, true); let validators_future = crate::utils::get_current_validators(near_rpc_client); - let epoch_config_future = crate::utils::get_current_epoch_config(near_rpc_client); - let (final_block, optimistic_block, validators, epoch_config) = tokio::try_join!( + let (final_block, optimistic_block, validators) = tokio::try_join!( final_block_future, optimistic_block_future, validators_future, - epoch_config_future ) .map_err(|err| { tracing::error!("Error to fetch final block info: {:?}", err); @@ -328,7 +319,6 @@ impl BlocksInfoByFinality { ), optimistic_changes: futures_locks::RwLock::new(OptimisticChanges::new()), current_validators: futures_locks::RwLock::new(CurrentValidatorInfo { validators }), - current_epoch_config: futures_locks::RwLock::new(CurrentEpochConfig { epoch_config }), } } @@ -370,8 +360,6 @@ impl BlocksInfoByFinality { ) -> anyhow::Result<()> { self.current_validators.write().await.validators = crate::utils::get_current_validators(near_rpc_client).await?; - self.current_epoch_config.write().await.epoch_config = - crate::utils::get_current_epoch_config(near_rpc_client).await?; Ok(()) } @@ -462,9 +450,4 @@ impl BlocksInfoByFinality { pub async fn validators(&self) -> near_primitives::views::EpochValidatorInfo { self.current_validators.read().await.validators.clone() } - - // Return current epoch config - pub async fn epoch_config(&self) -> near_primitives::epoch_manager::EpochConfig { - self.current_epoch_config.read().await.epoch_config.clone() - } } diff --git a/rpc-server/src/modules/network/methods.rs b/rpc-server/src/modules/network/methods.rs index 01ce5127..029bc171 100644 --- a/rpc-server/src/modules/network/methods.rs +++ b/rpc-server/src/modules/network/methods.rs @@ -1,5 +1,6 @@ use jsonrpc_v2::{Data, Params}; use near_jsonrpc::RpcRequest; +use near_primitives::epoch_manager::{AllEpochConfig, EpochConfig}; use crate::config::ServerContext; use crate::errors::RPCError; @@ -324,71 +325,67 @@ async fn protocol_config_call( error_message: err.to_string(), } })?; + // Stores runtime config for each protocol version + // Create store of runtime configs for the given chain id. + // + // For mainnet and other chains except testnet we don't need to override runtime config for + // first protocol versions. + // For testnet, runtime config for genesis block was (incorrectly) different, that's why we + // need to override it specifically to preserve compatibility. + let store = near_parameters::RuntimeConfigStore::for_chain_id( + &data.genesis_info.genesis_config.chain_id, + ); + let runtime_config = store.get_config(block.latest_protocol_version); - let protocol_config = if data - .blocks_info_by_finality - .final_cache_block() - .await - .epoch_id - == block.epoch_id - { - let store = near_parameters::RuntimeConfigStore::for_chain_id( - &data.genesis_info.genesis_config.chain_id, - ); - let runtime_config = store.get_config(block.latest_protocol_version); + // get default epoch config for genesis config + let default_epoch_config = EpochConfig::from(&data.genesis_info.genesis_config); + // AllEpochConfig manages protocol configs that might be changing throughout epochs (hence EpochConfig). + // The main function in AllEpochConfig is ::for_protocol_version which takes a protocol version + // and returns the EpochConfig that should be used for this protocol version. + let all_epoch_config = AllEpochConfig::new( + true, + default_epoch_config, + &data.genesis_info.genesis_config.chain_id, + ); + let epoch_config = all_epoch_config.for_protocol_version(block.latest_protocol_version); - // Looks strange, but we follow the same logic as in nearcore - // nearcore/src/runtime/mod.rs:1203 - let mut genesis_config = data.genesis_info.genesis_config.clone(); - genesis_config.protocol_version = block.latest_protocol_version; + // Looks strange, but we follow the same logic as in nearcore + // nearcore/src/runtime/mod.rs:1203 + let mut genesis_config = data.genesis_info.genesis_config.clone(); + genesis_config.protocol_version = block.latest_protocol_version; - let epoch_config = data.blocks_info_by_finality.epoch_config().await; - genesis_config.epoch_length = epoch_config.epoch_length; - genesis_config.num_block_producer_seats = epoch_config.num_block_producer_seats; - genesis_config.num_block_producer_seats_per_shard = - epoch_config.num_block_producer_seats_per_shard; - genesis_config.avg_hidden_validator_seats_per_shard = - epoch_config.avg_hidden_validator_seats_per_shard; - genesis_config.block_producer_kickout_threshold = - epoch_config.block_producer_kickout_threshold; - genesis_config.chunk_producer_kickout_threshold = - epoch_config.chunk_producer_kickout_threshold; - genesis_config.max_kickout_stake_perc = epoch_config.validator_max_kickout_stake_perc; - genesis_config.online_min_threshold = epoch_config.online_min_threshold; - genesis_config.online_max_threshold = epoch_config.online_max_threshold; - genesis_config.fishermen_threshold = epoch_config.fishermen_threshold; - genesis_config.minimum_stake_divisor = epoch_config.minimum_stake_divisor; - genesis_config.protocol_upgrade_stake_threshold = - epoch_config.protocol_upgrade_stake_threshold; - genesis_config.shard_layout = epoch_config.shard_layout; - genesis_config.num_chunk_only_producer_seats = epoch_config - .validator_selection_config - .num_chunk_only_producer_seats; - genesis_config.minimum_validators_per_shard = epoch_config - .validator_selection_config - .minimum_validators_per_shard; - genesis_config.minimum_stake_ratio = - epoch_config.validator_selection_config.minimum_stake_ratio; + genesis_config.epoch_length = epoch_config.epoch_length; + genesis_config.num_block_producer_seats = epoch_config.num_block_producer_seats; + genesis_config.num_block_producer_seats_per_shard = + epoch_config.num_block_producer_seats_per_shard; + genesis_config.avg_hidden_validator_seats_per_shard = + epoch_config.avg_hidden_validator_seats_per_shard; + genesis_config.block_producer_kickout_threshold = epoch_config.block_producer_kickout_threshold; + genesis_config.chunk_producer_kickout_threshold = epoch_config.chunk_producer_kickout_threshold; + genesis_config.max_kickout_stake_perc = epoch_config.validator_max_kickout_stake_perc; + genesis_config.online_min_threshold = epoch_config.online_min_threshold; + genesis_config.online_max_threshold = epoch_config.online_max_threshold; + genesis_config.fishermen_threshold = epoch_config.fishermen_threshold; + genesis_config.minimum_stake_divisor = epoch_config.minimum_stake_divisor; + genesis_config.protocol_upgrade_stake_threshold = epoch_config.protocol_upgrade_stake_threshold; + genesis_config.shard_layout = epoch_config.shard_layout; + genesis_config.num_chunk_only_producer_seats = epoch_config + .validator_selection_config + .num_chunk_only_producer_seats; + genesis_config.minimum_validators_per_shard = epoch_config + .validator_selection_config + .minimum_validators_per_shard; + genesis_config.minimum_stake_ratio = + epoch_config.validator_selection_config.minimum_stake_ratio; - let protocol_config = near_chain_configs::ProtocolConfig { - genesis_config, - runtime_config: near_parameters::RuntimeConfig { - fees: runtime_config.fees.clone(), - wasm_config: runtime_config.wasm_config.clone(), - account_creation_config: runtime_config.account_creation_config.clone(), - storage_proof_size_soft_limit: runtime_config.storage_proof_size_soft_limit, - }, - }; - protocol_config.into() - } else { - data.db_manager - .get_protocol_config_by_epoch_id(block.epoch_id, "EXPERIMENTAL_protocol_config") - .await - .map_err(|err| { - near_jsonrpc::primitives::types::config::RpcProtocolConfigError::InternalError { - error_message: err.to_string(), - } - })? + let protocol_config = near_chain_configs::ProtocolConfig { + genesis_config, + runtime_config: near_parameters::RuntimeConfig { + fees: runtime_config.fees.clone(), + wasm_config: runtime_config.wasm_config.clone(), + account_creation_config: runtime_config.account_creation_config.clone(), + storage_proof_size_soft_limit: runtime_config.storage_proof_size_soft_limit, + }, }; - Ok(protocol_config) + Ok(protocol_config.into()) } diff --git a/rpc-server/src/modules/network/mod.rs b/rpc-server/src/modules/network/mod.rs index 6e9b48a6..93d46dac 100644 --- a/rpc-server/src/modules/network/mod.rs +++ b/rpc-server/src/modules/network/mod.rs @@ -1,32 +1 @@ pub mod methods; - -pub async fn epoch_config_from_protocol_config_view( - protocol_config_view: near_chain_configs::ProtocolConfigView, -) -> near_primitives::epoch_manager::EpochConfig { - near_primitives::epoch_manager::EpochConfig { - epoch_length: protocol_config_view.epoch_length, - num_block_producer_seats: protocol_config_view.num_block_producer_seats, - num_block_producer_seats_per_shard: protocol_config_view - .num_block_producer_seats_per_shard - .clone(), - avg_hidden_validator_seats_per_shard: protocol_config_view - .avg_hidden_validator_seats_per_shard - .clone(), - block_producer_kickout_threshold: protocol_config_view.block_producer_kickout_threshold, - chunk_producer_kickout_threshold: protocol_config_view.chunk_producer_kickout_threshold, - fishermen_threshold: protocol_config_view.fishermen_threshold, - online_min_threshold: protocol_config_view.online_min_threshold, - online_max_threshold: protocol_config_view.online_max_threshold, - protocol_upgrade_stake_threshold: protocol_config_view.protocol_upgrade_stake_threshold, - minimum_stake_divisor: protocol_config_view.minimum_stake_divisor, - shard_layout: protocol_config_view.shard_layout.clone(), - validator_selection_config: near_primitives::epoch_manager::ValidatorSelectionConfig { - num_chunk_only_producer_seats: protocol_config_view.num_chunk_only_producer_seats, - minimum_validators_per_shard: protocol_config_view.minimum_validators_per_shard, - minimum_stake_ratio: protocol_config_view.minimum_stake_ratio, - shuffle_shard_assignment_for_chunk_producers: protocol_config_view - .shuffle_shard_assignment_for_chunk_producers, - }, - validator_max_kickout_stake_perc: protocol_config_view.max_kickout_stake_perc, - } -} diff --git a/rpc-server/src/utils.rs b/rpc-server/src/utils.rs index 3a0259a8..3886d10c 100644 --- a/rpc-server/src/utils.rs +++ b/rpc-server/src/utils.rs @@ -1,5 +1,4 @@ use crate::modules::blocks::{BlockInfo, BlocksInfoByFinality, CacheBlock}; -use crate::modules::network::epoch_config_from_protocol_config_view; #[cfg(feature = "shadow_data_consistency")] use assert_json_diff::{assert_json_matches_no_panic, CompareMode, Config, NumericMode}; use futures::StreamExt; @@ -147,19 +146,6 @@ pub async fn get_current_validators( Ok(near_rpc_client.call(params, None).await?) } -pub async fn get_current_epoch_config( - near_rpc_client: &JsonRpcClient, -) -> anyhow::Result { - let params = - near_jsonrpc_client::methods::EXPERIMENTAL_protocol_config::RpcProtocolConfigRequest { - block_reference: near_primitives::types::BlockReference::Finality( - near_primitives::types::Finality::Final, - ), - }; - let protocol_config_view = near_rpc_client.call(params, None).await?; - Ok(epoch_config_from_protocol_config_view(protocol_config_view).await) -} - async fn handle_streamer_message( streamer_message: near_indexer_primitives::StreamerMessage, blocks_cache: std::sync::Arc>, diff --git a/state-indexer/src/main.rs b/state-indexer/src/main.rs index 7745d566..67f168f3 100644 --- a/state-indexer/src/main.rs +++ b/state-indexer/src/main.rs @@ -103,8 +103,8 @@ async fn handle_epoch( } else { // If epoch changed, we need to save epoch info and update epoch_end_height let epoch_info = epoch_indexer::get_epoch_info_by_id(stats_epoch_id, rpc_client).await?; - epoch_indexer::save_epoch_info(&epoch_info, db_manager, Some(stats_current_epoch_height)).await?; - epoch_indexer::update_epoch_end_height(db_manager, Some(stats_epoch_id), next_epoch_id).await?; + epoch_indexer::save_epoch_info(&epoch_info, db_manager, Some(stats_current_epoch_height), next_epoch_id) + .await?; Ok(()) } } else {