Skip to content

Commit

Permalink
Introduce ‘max_gas_burnt_view’ client config and command line flag
Browse files Browse the repository at this point in the history
The ‘max_gas_burnt_view’ affects the RPCs only and therefore doesn’t
need to be part of the consensus.  In other words, all nodes could
have this value customised rather than using an agreed upon value
from genesis configuration.

Introduce a ‘max_gas_burnt_view’ field to ClientConfig and
a corresponding flag to ‘init’ and ‘run’ subcommands.  When present,
it will override the limit used by the node ignoring whatever is
stored in genesis configuration.

Fixes: near#3080
  • Loading branch information
mina86 committed Jun 25, 2021
1 parent 9c10e80 commit 027fb57
Show file tree
Hide file tree
Showing 26 changed files with 266 additions and 43 deletions.
1 change: 1 addition & 0 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,7 @@ mod test {
vec![],
vec![],
None,
None,
)) as Arc<dyn RuntimeAdapter>
})
.collect()
Expand Down
4 changes: 4 additions & 0 deletions chain/client/tests/challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ fn test_verify_chunk_invalid_state_challenge() {
vec![],
vec![],
None,
None,
))];
let mut env = TestEnv::new_with_runtime(ChainGenesis::test(), 1, 1, runtimes);
let signer = InMemorySigner::from_seed("test0", KeyType::ED25519, "test0");
Expand Down Expand Up @@ -570,6 +571,7 @@ fn test_fishermen_challenge() {
vec![],
vec![],
None,
None,
))
};
let runtime1 = create_runtime();
Expand Down Expand Up @@ -632,6 +634,7 @@ fn test_challenge_in_different_epoch() {
vec![],
vec![],
None,
None,
));
let runtime2 = Arc::new(nearcore::NightshadeRuntime::new(
Path::new("."),
Expand All @@ -640,6 +643,7 @@ fn test_challenge_in_different_epoch() {
vec![],
vec![],
None,
None,
));
let runtimes: Vec<Arc<dyn RuntimeAdapter>> = vec![runtime1, runtime2];
let networks = vec![network_adapter.clone(), network_adapter.clone()];
Expand Down
3 changes: 3 additions & 0 deletions chain/client/tests/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub fn create_nightshade_runtimes(genesis: &Genesis, n: usize) -> Vec<Arc<dyn Ru
vec![],
vec![],
None,
None,
)) as Arc<dyn RuntimeAdapter>
})
.collect()
Expand Down Expand Up @@ -1804,6 +1805,7 @@ fn test_incorrect_validator_key_produce_block() {
vec![],
vec![],
None,
None,
));
let signer = Arc::new(InMemoryValidatorSigner::from_seed("test0", KeyType::ED25519, "seed"));
let mut config = ClientConfig::test(true, 10, 20, 2, false, true);
Expand Down Expand Up @@ -2939,6 +2941,7 @@ mod protocol_feature_restore_receipts_after_fix_tests {
vec![],
vec![],
None,
None,
);
// TODO #4305: get directly from NightshadeRuntime
let migration_data = load_migration_data(&genesis.config.chain_id);
Expand Down
1 change: 1 addition & 0 deletions chain/client/tests/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn test_setup() -> (TestEnv, InMemorySigner) {
vec![],
vec![],
None,
None,
)) as Arc<dyn RuntimeAdapter>],
);
let signer = InMemorySigner::from_seed("test0", KeyType::ED25519, "test0");
Expand Down
4 changes: 4 additions & 0 deletions chain/indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use self::streamer::{
StreamerMessage,
};
pub use near_primitives;
use near_primitives::types::Gas;

/// Config wrapper to simplify signature and usage of `nearcore::init_configs`
/// function by making args more explicit via struct
Expand All @@ -39,6 +40,8 @@ pub struct InitConfigArgs {
pub download: bool,
/// Specify a custom download URL for the genesis-file.
pub download_genesis_url: Option<String>,
/// Specify a custom max_gas_burnt_view limit.
pub max_gas_burnt_view: Option<Gas>,
}

/// Enum to define a mode of syncing for NEAR Indexer
Expand Down Expand Up @@ -135,5 +138,6 @@ pub fn indexer_init_configs(dir: &std::path::PathBuf, params: InitConfigArgs) {
params.genesis.as_deref(),
params.download,
params.download_genesis_url.as_deref(),
params.max_gas_burnt_view,
)
}
7 changes: 6 additions & 1 deletion core/chain-configs/src/client_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::Duration;

use serde::{Deserialize, Serialize};

use near_primitives::types::{AccountId, BlockHeightDelta, NumBlocks, NumSeats, ShardId};
use near_primitives::types::{AccountId, BlockHeightDelta, Gas, NumBlocks, NumSeats, ShardId};
use near_primitives::version::Version;

pub const TEST_STATE_SYNC_TIMEOUT: u64 = 5;
Expand Down Expand Up @@ -97,6 +97,10 @@ pub struct ClientConfig {
pub view_client_throttle_period: Duration,
/// Upper bound of the byte size of contract state that is still viewable. None is no limit
pub trie_viewer_state_size_limit: Option<u64>,
/// Max burnt gas per view method. If present, overrides value stored in
/// genesis file. The value only affects the RPCs without influencing the
/// protocol thus changing it per-node doesn’t affect the blockchain.
pub max_gas_burnt_view: Option<Gas>,
}

impl ClientConfig {
Expand Down Expand Up @@ -154,6 +158,7 @@ impl ClientConfig {
epoch_sync_enabled,
view_client_throttle_period: Duration::from_secs(1),
trie_viewer_state_size_limit: None,
max_gas_burnt_view: None,
}
}
}
1 change: 1 addition & 0 deletions genesis-tools/genesis-populate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl GenesisBuilder {
vec![],
vec![],
None,
None,
);
Self {
home_dir: home_dir.to_path_buf(),
Expand Down
13 changes: 10 additions & 3 deletions nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ pub struct Config {
pub view_client_throttle_period: Duration,
#[serde(default = "default_trie_viewer_state_size_limit")]
pub trie_viewer_state_size_limit: Option<u64>,
/// If set, overrides value in genesis configuration.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_gas_burnt_view: Option<Gas>,
}

impl Default for Config {
Expand All @@ -456,6 +459,7 @@ impl Default for Config {
view_client_threads: default_view_client_threads(),
view_client_throttle_period: default_view_client_throttle_period(),
trie_viewer_state_size_limit: default_trie_viewer_state_size_limit(),
max_gas_burnt_view: None,
}
}
}
Expand Down Expand Up @@ -642,6 +646,7 @@ impl NearConfig {
epoch_sync_enabled: config.epoch_sync_enabled,
view_client_throttle_period: config.view_client_throttle_period,
trie_viewer_state_size_limit: config.trie_viewer_state_size_limit,
max_gas_burnt_view: config.max_gas_burnt_view,
},
network_config: NetworkConfig {
public_key: network_key_pair.public_key,
Expand Down Expand Up @@ -787,6 +792,7 @@ pub fn init_configs(
genesis: Option<&str>,
download: bool,
download_genesis_url: Option<&str>,
max_gas_burnt_view: Option<Gas>,
) {
fs::create_dir_all(dir).expect("Failed to create directory");
// Check if config already exists in home dir.
Expand All @@ -798,12 +804,15 @@ pub fn init_configs(
let chain_id = chain_id
.and_then(|c| if c.is_empty() { None } else { Some(c.to_string()) })
.unwrap_or_else(random_chain_id);
let mut config = Config::default();
if max_gas_burnt_view.is_some() {
config.max_gas_burnt_view = max_gas_burnt_view;
}
match chain_id.as_ref() {
"mainnet" => {
if test_seed.is_some() {
panic!("Test seed is not supported for MainNet");
}
let mut config = Config::default();
config.telemetry.endpoints.push(MAINNET_TELEMETRY_URL.to_string());
config.write_to_file(&dir.join(CONFIG_FILENAME));

Expand All @@ -824,7 +833,6 @@ pub fn init_configs(
if test_seed.is_some() {
panic!("Test seed is not supported for official TestNet");
}
let mut config = Config::default();
config.telemetry.endpoints.push(NETWORK_TELEMETRY_URL.replace("{}", &chain_id));
config.write_to_file(&dir.join(CONFIG_FILENAME));

Expand Down Expand Up @@ -858,7 +866,6 @@ pub fn init_configs(
}
_ => {
// Create new configuration, key files and genesis for one validator.
let mut config = Config::default();
config.network.skip_sync_wait = true;
if fast {
config.consensus.min_block_production_delay =
Expand Down
1 change: 1 addition & 0 deletions nearcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub fn start_with_config(
config.client_config.tracked_accounts.clone(),
config.client_config.tracked_shards.clone(),
config.client_config.trie_viewer_state_size_limit,
config.client_config.max_gas_burnt_view,
));

let telemetry = TelemetryActor::new(config.telemetry_config.clone()).start();
Expand Down
3 changes: 3 additions & 0 deletions nearcore/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub fn migrate_12_to_13(path: &String, near_config: &NearConfig) {
near_config.client_config.tracked_accounts.clone(),
near_config.client_config.tracked_shards.clone(),
None,
None,
);
let mut store_update = store.store_update();
store_update.delete_all(DBCol::ColTransactionResult);
Expand Down Expand Up @@ -221,6 +222,7 @@ pub fn migrate_19_to_20(path: &String, near_config: &NearConfig) {
near_config.client_config.tracked_accounts.clone(),
near_config.client_config.tracked_shards.clone(),
None,
None,
);
let shard_id = 0;
// This is hardcoded for mainnet specifically. Blocks with lower heights have been checked.
Expand Down Expand Up @@ -287,6 +289,7 @@ pub fn migrate_22_to_23(path: &String, near_config: &NearConfig) {
near_config.client_config.tracked_accounts.clone(),
near_config.client_config.tracked_shards.clone(),
None,
None,
);
let shard_id = 0;
// This is hardcoded for mainnet specifically. Blocks with lower heights have been checked.
Expand Down
46 changes: 31 additions & 15 deletions nearcore/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ impl EpochInfoProvider for SafeEpochManager {
/// TODO: this possibly should be merged with the runtime cargo or at least reconciled on the interfaces.
pub struct NightshadeRuntime {
genesis_config: GenesisConfig,
genesis_runtime_config: Arc<RuntimeConfig>,
/// Runtime configuration. Note that it may be slightly different than
/// `genesis_config.runtime_config`. Consider `max_gas_burnt_view` value
/// which may be configured per node.
runtime_config: Arc<RuntimeConfig>,

store: Arc<Store>,
tries: ShardTries,
Expand All @@ -142,11 +145,18 @@ impl NightshadeRuntime {
initial_tracking_accounts: Vec<AccountId>,
initial_tracking_shards: Vec<ShardId>,
trie_viewer_state_size_limit: Option<u64>,
max_gas_burnt_view: Option<Gas>,
) -> Self {
let runtime = Runtime::new();
let trie_viewer = TrieViewer::new_with_state_size_limit(trie_viewer_state_size_limit);
let trie_viewer = TrieViewer::new(trie_viewer_state_size_limit, max_gas_burnt_view);
let genesis_config = genesis.config.clone();
let genesis_runtime_config = Arc::new(genesis_config.runtime_config.clone());
let runtime_config = Arc::new({
let mut cfg = genesis_config.runtime_config.clone();
if let Some(gas) = max_gas_burnt_view {
cfg.wasm_config.limit_config.max_gas_burnt_view = gas;
}
cfg
});
let num_shards = genesis.config.num_block_producer_seats_per_shard.len() as NumShards;
let initial_epoch_config = EpochConfig::from(&genesis_config);
let reward_calculator = RewardCalculator::new(&genesis_config);
Expand Down Expand Up @@ -175,7 +185,7 @@ impl NightshadeRuntime {
);
NightshadeRuntime {
genesis_config,
genesis_runtime_config,
runtime_config,
store,
tries,
runtime,
Expand Down Expand Up @@ -420,7 +430,7 @@ impl NightshadeRuntime {
random_seed,
current_protocol_version,
config: RuntimeConfig::from_protocol_version(
&self.genesis_runtime_config,
&self.runtime_config,
current_protocol_version,
),
cache: Some(Arc::new(StoreCompiledContractCache { store: self.store.clone() })),
Expand Down Expand Up @@ -552,10 +562,8 @@ impl RuntimeAdapter for NightshadeRuntime {
verify_signature: bool,
current_protocol_version: ProtocolVersion,
) -> Result<Option<InvalidTxError>, Error> {
let runtime_config = RuntimeConfig::from_protocol_version(
&self.genesis_runtime_config,
current_protocol_version,
);
let runtime_config =
RuntimeConfig::from_protocol_version(&self.runtime_config, current_protocol_version);

if let Some(state_root) = state_root {
let shard_id = self.account_id_to_shard_id(&transaction.transaction.signer_id);
Expand Down Expand Up @@ -624,10 +632,8 @@ impl RuntimeAdapter for NightshadeRuntime {
let mut transactions = vec![];
let mut num_checked_transactions = 0;

let runtime_config = RuntimeConfig::from_protocol_version(
&self.genesis_runtime_config,
current_protocol_version,
);
let runtime_config =
RuntimeConfig::from_protocol_version(&self.runtime_config, current_protocol_version);

while total_gas_burnt < transactions_gas_limit {
if let Some(iter) = pool_iterator.next() {
Expand Down Expand Up @@ -1507,8 +1513,17 @@ impl RuntimeAdapter for NightshadeRuntime {
config.protocol_version = protocol_version;
// Currently only runtime config is changed through protocol upgrades.
let runtime_config =
RuntimeConfig::from_protocol_version(&self.genesis_runtime_config, protocol_version);
config.runtime_config = (*runtime_config).clone();
RuntimeConfig::from_protocol_version(&self.runtime_config, protocol_version);
// If we were initialised with a custom max_gas_burnt_view (see new
// function), bring back the value from genesis configuration. Since
// max_gas_burnt_view never changes as a result of protocol upgrade, we
// can be sure that this value will be correct.
config.runtime_config = {
let mut cfg = (*runtime_config).clone();
cfg.wasm_config.limit_config.max_gas_burnt_view =
config.runtime_config.wasm_config.limit_config.max_gas_burnt_view;
cfg
};
Ok(config)
}

Expand Down Expand Up @@ -1758,6 +1773,7 @@ mod test {
initial_tracked_accounts,
initial_tracked_shards,
None,
None,
);
let (_store, state_roots) = runtime.genesis_state();
let genesis_hash = hash(&vec![0]);
Expand Down
1 change: 1 addition & 0 deletions nearcore/tests/economics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn setup_env(f: &mut dyn FnMut(&mut Genesis) -> ()) -> (TestEnv, FeeHelper) {
vec![],
vec![],
None,
None,
))];
let env = TestEnv::new_with_runtime(ChainGenesis::from(&genesis), 1, 1, runtimes);
(env, fee_helper)
Expand Down
15 changes: 14 additions & 1 deletion neard/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{DEFAULT_HOME, NEARD_VERSION, NEARD_VERSION_STRING, PROTOCOL_VERSION};
use clap::{AppSettings, Clap};
use near_primitives::types::{NumSeats, NumShards};
use near_primitives::types::{Gas, NumSeats, NumShards};
use nearcore::get_store_path;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -123,6 +123,10 @@ pub(super) struct InitCmd {
/// Specify private key generated from seed (TESTING ONLY).
#[clap(long)]
test_seed: Option<String>,
/// Customize max_gas_burnt_view runtime limit. If not specified, value
/// from genesis configuration will be taken.
#[clap(long)]
max_gas_burnt_view: Option<Gas>,
}

impl InitCmd {
Expand All @@ -145,6 +149,7 @@ impl InitCmd {
self.genesis.as_deref(),
self.download_genesis,
self.download_genesis_url.as_deref(),
self.max_gas_burnt_view,
);
}
}
Expand Down Expand Up @@ -179,6 +184,11 @@ pub(super) struct RunCmd {
/// Customize telemetry url.
#[clap(long)]
telemetry_url: Option<String>,
/// Customize max_gas_burnt_view runtime limit. If not specified, either
/// value given at ‘init’ (i.e. present in config.json) or one from genesis
/// configuration will be taken.
#[clap(long)]
max_gas_burnt_view: Option<Gas>,
}

impl RunCmd {
Expand Down Expand Up @@ -220,6 +230,9 @@ impl RunCmd {
if self.archive {
near_config.client_config.archive = true;
}
if self.max_gas_burnt_view.is_some() {
near_config.client_config.max_gas_burnt_view = self.max_gas_burnt_view;
}

#[cfg(feature = "sandbox")]
{
Expand Down
Loading

0 comments on commit 027fb57

Please sign in to comment.