Skip to content

Commit

Permalink
feat(chain): Exposed genesis config + runtime config and genesis reco…
Browse files Browse the repository at this point in the history
…rds via RPC

Resolves #2007 and #2025

We needed to make Near config query-able through node RPC. Specifically,
one of our clients wanted to know how many blocks remain until an
account is going to be evicted due to rent. This information can be
derived from the account balance and the config.

In this commit, two new RPC endpoints are exposed:
EXPERIMENTAL_genesis_config and EXPERIMENTAL_genesis_records. Learn more
in PR #2109.

# Test plan

Added tests to query the endpoints with a happy path and also invalid
parameters.
  • Loading branch information
frol authored Feb 28, 2020
1 parent 68e3b72 commit 4713f5f
Show file tree
Hide file tree
Showing 53 changed files with 842 additions and 577 deletions.
67 changes: 54 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,12 @@ impl ChainGenesis {
}
}

impl From<GenesisConfig> for ChainGenesis {
fn from(genesis_config: GenesisConfig) -> Self {
impl<T> From<T> for ChainGenesis
where
T: AsRef<GenesisConfig>,
{
fn from(genesis_config: T) -> Self {
let genesis_config = genesis_config.as_ref();
ChainGenesis::new(
genesis_config.genesis_time,
genesis_config.gas_limit,
Expand Down
26 changes: 14 additions & 12 deletions chain/client/tests/challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use std::sync::Arc;
use borsh::BorshSerialize;
use reed_solomon_erasure::galois_8::ReedSolomon;

use near::config::{GenesisConfigExt, FISHERMEN_THRESHOLD};
use near::config::{GenesisExt, FISHERMEN_THRESHOLD};
use near::NightshadeRuntime;
use near_chain::chain::BlockEconomicsConfig;
use near_chain::validate::validate_challenge;
use near_chain::{
Block, ChainGenesis, ChainStoreAccess, DoomslugThresholdMode, Error, ErrorKind, Provenance,
RuntimeAdapter,
};
use near_chain_configs::GenesisConfig;
use near_chain_configs::Genesis;
use near_client::test_utils::{MockNetworkAdapter, TestEnv};
use near_client::Client;
use near_crypto::{InMemorySigner, KeyType, Signer};
Expand Down Expand Up @@ -417,11 +417,11 @@ fn challenge(
#[test]
fn test_verify_chunk_invalid_state_challenge() {
let store1 = create_test_store();
let genesis_config = GenesisConfig::test(vec!["test0", "test1"], 1);
let genesis = Genesis::test(vec!["test0", "test1"], 1);
let runtimes: Vec<Arc<dyn RuntimeAdapter>> = vec![Arc::new(near::NightshadeRuntime::new(
Path::new("."),
store1,
genesis_config,
Arc::new(genesis),
vec![],
vec![],
))];
Expand Down Expand Up @@ -683,13 +683,14 @@ fn test_block_challenge() {
#[test]
fn test_fishermen_challenge() {
init_test_logger();
let mut genesis_config = GenesisConfig::test(vec!["test0", "test1", "test2"], 1);
genesis_config.epoch_length = 5;
let mut genesis = Genesis::test(vec!["test0", "test1", "test2"], 1);
genesis.config.epoch_length = 5;
let genesis = Arc::new(genesis);
let create_runtime = || -> Arc<NightshadeRuntime> {
Arc::new(near::NightshadeRuntime::new(
Path::new("."),
create_test_store(),
genesis_config.clone(),
Arc::clone(&genesis),
vec![],
vec![],
))
Expand Down Expand Up @@ -742,21 +743,22 @@ fn test_fishermen_challenge() {
#[test]
fn test_challenge_in_different_epoch() {
init_test_logger();
let mut genesis_config = GenesisConfig::test(vec!["test0", "test1"], 2);
genesis_config.epoch_length = 2;
// genesis_config.validator_kickout_threshold = 10;
let mut genesis = Genesis::test(vec!["test0", "test1"], 2);
genesis.config.epoch_length = 2;
let genesis = Arc::new(genesis);
// genesis.config.validator_kickout_threshold = 10;
let network_adapter = Arc::new(MockNetworkAdapter::default());
let runtime1 = Arc::new(near::NightshadeRuntime::new(
Path::new("."),
create_test_store(),
genesis_config.clone(),
Arc::clone(&genesis),
vec![],
vec![],
));
let runtime2 = Arc::new(near::NightshadeRuntime::new(
Path::new("."),
create_test_store(),
genesis_config,
genesis,
vec![],
vec![],
));
Expand Down
5 changes: 3 additions & 2 deletions chain/jsonrpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ chrono = { version = "0.4.4", features = ["serde"] }
lazy_static = "1.4"
log = "0.4"
prometheus = "^0.7"
serde_derive = "1.0"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
validator = "0.10"
uuid = { version = "~0.8", features = ["v4"] }
borsh = "0.2.10"

near-chain-configs = { path = "../../core/chain-configs" }
near-crypto = { path = "../../core/crypto" }
near-primitives = { path = "../../core/primitives" }
near-store = { path = "../../core/store" }
Expand Down
4 changes: 2 additions & 2 deletions chain/jsonrpc/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ edition = "2018"
[dependencies]
actix-web = "2.0.0"
futures = "0.3"
serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde = "1.0"
uuid = { version = "~0.8", features = ["v4"] }

near-chain-configs = { path = "../../../core/chain-configs" }
near-primitives = { path = "../../../core/primitives" }
14 changes: 12 additions & 2 deletions chain/jsonrpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use serde::Deserialize;
use serde::Serialize;

use near_primitives::hash::CryptoHash;
use near_primitives::rpc::{BlockQueryInfo, RpcQueryRequest};
use near_primitives::rpc::{BlockQueryInfo, RpcGenesisRecordsRequest, RpcQueryRequest};
use near_primitives::types::{BlockId, MaybeBlockId, ShardId};
use near_primitives::views::{
BlockView, ChunkView, EpochValidatorInfo, FinalExecutionOutcomeView, GasPriceView,
QueryResponse, StateChangesView, StatusResponse,
GenesisRecordsView, QueryResponse, StateChangesView, StatusResponse,
};

use crate::message::{from_slice, Message, RpcError};
Expand Down Expand Up @@ -180,6 +180,8 @@ jsonrpc_client!(pub struct JsonRpcClient {
pub fn broadcast_tx_async(&mut self, tx: String) -> RpcRequest<String>;
pub fn broadcast_tx_commit(&mut self, tx: String) -> RpcRequest<FinalExecutionOutcomeView>;
pub fn status(&mut self) -> RpcRequest<StatusResponse>;
#[allow(non_snake_case)]
pub fn EXPERIMENTAL_genesis_config(&mut self) -> RpcRequest<serde_json::Value>;
pub fn health(&mut self) -> RpcRequest<()>;
pub fn tx(&mut self, hash: String, account_id: String) -> RpcRequest<FinalExecutionOutcomeView>;
pub fn chunk(&mut self, id: ChunkId) -> RpcRequest<ChunkView>;
Expand All @@ -189,6 +191,14 @@ jsonrpc_client!(pub struct JsonRpcClient {
});

impl JsonRpcClient {
#[allow(non_snake_case)]
pub fn EXPERIMENTAL_genesis_records(
&mut self,
request: RpcGenesisRecordsRequest,
) -> RpcRequest<GenesisRecordsView> {
call_method(&self.client, &self.server_addr, "EXPERIMENTAL_genesis_records", request)
}

/// This is a soft-deprecated method to do query RPC request with a path and data positional
/// parameters.
pub fn query_by_path(&mut self, path: String, data: String) -> RpcRequest<QueryResponse> {
Expand Down
Loading

0 comments on commit 4713f5f

Please sign in to comment.