Skip to content

Commit

Permalink
cleanup: Removing RuntimeConfigView -> RuntimeConfig (#8763)
Browse files Browse the repository at this point in the history
In #8426 this conversion became lossy due to the introduction of Compute Costs. This raised the question of whether this conversion is needed at all. I've found one place in the Indexer code that uses it and replaced it with a direct lookup for config from `RuntimeConfigStore`. As a part of this I've also simplified some code in the indexer.

This should fix the failing nayduck test #8967
  • Loading branch information
aborg-dev authored and nikurt committed Apr 25, 2023
1 parent 591a256 commit 6bc3dc9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 78 deletions.
18 changes: 14 additions & 4 deletions chain/indexer/src/streamer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::time::Duration;

use actix::Addr;
use async_recursion::async_recursion;
use near_indexer_primitives::types::ProtocolVersion;
use node_runtime::config::RuntimeConfig;
use rocksdb::DB;
use tokio::sync::mpsc;
use tokio::time;
Expand Down Expand Up @@ -78,6 +80,10 @@ async fn build_streamer_message(
let num_shards = protocol_config_view.num_block_producer_seats_per_shard.len()
as near_primitives::types::NumShards;

let runtime_config_store =
near_primitives::runtime::config_store::RuntimeConfigStore::new(None);
let runtime_config = runtime_config_store.get_config(protocol_config_view.protocol_version);

let mut shards_outcomes = fetch_outcomes(&client, block.header.hash).await?;
let mut state_changes = fetch_state_changes(
&client,
Expand Down Expand Up @@ -121,7 +127,8 @@ async fn build_streamer_message(

let chunk_local_receipts = convert_transactions_sir_into_local_receipts(
&client,
&protocol_config_view,
&runtime_config,
protocol_config_view.protocol_version,
indexer_transactions
.iter()
.filter(|tx| tx.transaction.signer_id == tx.transaction.receiver_id)
Expand Down Expand Up @@ -167,7 +174,8 @@ async fn build_streamer_message(

if let Some(receipt) = find_local_receipt_by_id_in_block(
&client,
&protocol_config_view,
&runtime_config,
protocol_config_view.protocol_version,
prev_block,
execution_outcome.id,
)
Expand Down Expand Up @@ -237,7 +245,8 @@ async fn build_streamer_message(
/// otherwise returns None
async fn find_local_receipt_by_id_in_block(
client: &Addr<near_client::ViewClientActor>,
protocol_config_view: &near_chain_configs::ProtocolConfigView,
runtime_config: &RuntimeConfig,
protocol_version: ProtocolVersion,
block: views::BlockView,
receipt_id: near_primitives::hash::CryptoHash,
) -> Result<Option<views::ReceiptView>, FailedToFetchData> {
Expand Down Expand Up @@ -266,7 +275,8 @@ async fn find_local_receipt_by_id_in_block(
let indexer_transaction = IndexerTransactionWithOutcome { transaction, outcome };
let local_receipts = convert_transactions_sir_into_local_receipts(
&client,
&protocol_config_view,
&runtime_config,
protocol_version,
vec![&indexer_transaction],
&block,
)
Expand Down
10 changes: 5 additions & 5 deletions chain/indexer/src/streamer/utils.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use actix::Addr;

use near_indexer_primitives::types::ProtocolVersion;
use near_indexer_primitives::IndexerTransactionWithOutcome;
use near_primitives::views;
use node_runtime::config::tx_cost;
use node_runtime::config::{tx_cost, RuntimeConfig};

use super::errors::FailedToFetchData;
use super::fetchers::fetch_block;

pub(crate) async fn convert_transactions_sir_into_local_receipts(
client: &Addr<near_client::ViewClientActor>,
protocol_config: &near_chain_configs::ProtocolConfigView,
runtime_config: &RuntimeConfig,
protocol_version: ProtocolVersion,
txs: Vec<&IndexerTransactionWithOutcome>,
block: &views::BlockView,
) -> Result<Vec<views::ReceiptView>, FailedToFetchData> {
Expand All @@ -19,8 +21,6 @@ pub(crate) async fn convert_transactions_sir_into_local_receipts(
let prev_block = fetch_block(&client, block.header.prev_hash).await?;
let prev_block_gas_price = prev_block.header.gas_price;

let runtime_config =
node_runtime::config::RuntimeConfig::from(protocol_config.runtime_config.clone());
let local_receipts: Vec<views::ReceiptView> =
txs.into_iter()
.map(|tx| {
Expand All @@ -44,7 +44,7 @@ pub(crate) async fn convert_transactions_sir_into_local_receipts(
},
prev_block_gas_price,
true,
protocol_config.protocol_version,
protocol_version,
);
views::ReceiptView {
predecessor_id: tx.transaction.signer_id.clone(),
Expand Down
66 changes: 3 additions & 63 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2427,57 +2427,6 @@ impl From<RuntimeConfig> for RuntimeConfigView {
}
}

// reverse direction: rosetta adapter uses this, also we use to test that all fields are present in view
impl From<RuntimeConfigView> for RuntimeConfig {
fn from(config: RuntimeConfigView) -> Self {
Self {
fees: near_primitives_core::runtime::fees::RuntimeFeesConfig {
storage_usage_config: near_primitives_core::runtime::fees::StorageUsageConfig {
storage_amount_per_byte: config.storage_amount_per_byte,
num_bytes_account: config
.transaction_costs
.storage_usage_config
.num_bytes_account,
num_extra_bytes_record: config
.transaction_costs
.storage_usage_config
.num_extra_bytes_record,
},
burnt_gas_reward: config.transaction_costs.burnt_gas_reward,
pessimistic_gas_price_inflation_ratio: config
.transaction_costs
.pessimistic_gas_price_inflation_ratio,
action_fees: enum_map::enum_map! {
ActionCosts::create_account => config.transaction_costs.action_creation_config.create_account_cost.clone(),
ActionCosts::delete_account => config.transaction_costs.action_creation_config.delete_account_cost.clone(),
ActionCosts::delegate => config.transaction_costs.action_creation_config.delegate_cost.clone(),
ActionCosts::deploy_contract_base => config.transaction_costs.action_creation_config.deploy_contract_cost.clone(),
ActionCosts::deploy_contract_byte => config.transaction_costs.action_creation_config.deploy_contract_cost_per_byte.clone(),
ActionCosts::function_call_base => config.transaction_costs.action_creation_config.function_call_cost.clone(),
ActionCosts::function_call_byte => config.transaction_costs.action_creation_config.function_call_cost_per_byte.clone(),
ActionCosts::transfer => config.transaction_costs.action_creation_config.transfer_cost.clone(),
ActionCosts::stake => config.transaction_costs.action_creation_config.stake_cost.clone(),
ActionCosts::add_full_access_key => config.transaction_costs.action_creation_config.add_key_cost.full_access_cost.clone(),
ActionCosts::add_function_call_key_base => config.transaction_costs.action_creation_config.add_key_cost.function_call_cost.clone(),
ActionCosts::add_function_call_key_byte => config.transaction_costs.action_creation_config.add_key_cost.function_call_cost_per_byte.clone(),
ActionCosts::delete_key => config.transaction_costs.action_creation_config.delete_key_cost.clone(),
ActionCosts::new_action_receipt => config.transaction_costs.action_receipt_creation_config.clone(),
ActionCosts::new_data_receipt_base => config.transaction_costs.data_receipt_creation_config.base_cost.clone(),
ActionCosts::new_data_receipt_byte => config.transaction_costs.data_receipt_creation_config.cost_per_byte.clone(),

},
},
wasm_config: VMConfig::from(config.wasm_config),
account_creation_config: crate::runtime::config::AccountCreationConfig {
min_allowed_top_level_account_length: config
.account_creation_config
.min_allowed_top_level_account_length,
registrar_account_id: config.account_creation_config.registrar_account_id,
},
}
}
}

#[derive(Clone, Debug, Hash, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct VMConfigView {
/// Costs for runtime externals
Expand Down Expand Up @@ -2835,8 +2784,6 @@ impl From<ExtCostsConfigView> for near_primitives_core::config::ExtCostsConfig {
mod tests {
#[cfg(not(feature = "nightly"))]
use super::ExecutionMetadataView;
use super::RuntimeConfigView;
use crate::runtime::config::RuntimeConfig;
#[cfg(not(feature = "nightly"))]
use crate::transaction::ExecutionMetadata;
#[cfg(not(feature = "nightly"))]
Expand All @@ -2847,21 +2794,14 @@ mod tests {
#[test]
#[cfg(not(feature = "nightly"))]
fn test_runtime_config_view() {
use crate::runtime::config::RuntimeConfig;
use crate::views::RuntimeConfigView;

let config = RuntimeConfig::test();
let view = RuntimeConfigView::from(config);
insta::assert_json_snapshot!(&view);
}

/// A `RuntimeConfigView` must contain all info to reconstruct a `RuntimeConfig`.
#[test]
fn test_runtime_config_view_is_complete() {
let config = RuntimeConfig::test();
let view = RuntimeConfigView::from(config.clone());
let reconstructed_config = RuntimeConfig::from(view);

assert_eq!(config, reconstructed_config);
}

/// `ExecutionMetadataView` with profile V1 displayed on the RPC should not change.
#[test]
#[cfg(not(feature = "nightly"))]
Expand Down
6 changes: 0 additions & 6 deletions integration-tests/src/tests/nearcore/rpc_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use near_primitives::types::{
};
use near_primitives::version::ProtocolVersion;
use near_primitives::views::{ExecutionOutcomeView, ExecutionStatusView, RuntimeConfigView};
use node_runtime::config::RuntimeConfig;
use std::time::Duration;

#[test]
Expand Down Expand Up @@ -258,11 +257,6 @@ fn test_protocol_config_rpc() {
serde_json::json!(config_response.config_view.runtime_config),
serde_json::json!(RuntimeConfigView::from(latest_runtime_config.as_ref().clone()))
);
// compare struct used by runtime
assert_eq!(
RuntimeConfig::from(config_response.config_view.runtime_config),
latest_runtime_config.as_ref().clone()
);
System::current().stop();
});
}
Expand Down

0 comments on commit 6bc3dc9

Please sign in to comment.