Skip to content

Commit a97a6f2

Browse files
authored
rpc: add rpc v2 chainSpec to polkadot (#2859)
The [chainSpec RPC API from the v2 spec](https://paritytech.github.io/json-rpc-interface-spec/api/chainSpec.html) was only added to substrate-node and should be added to polkadot as well /cc @lexnv
1 parent 8232749 commit a97a6f2

16 files changed

Lines changed: 45 additions & 23 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/sdk/src/reference_docs/runtime_vs_smart_contract.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,21 @@
3232
//!
3333
//! ## Comparative Table
3434
//!
35-
//! | Aspect | Runtime | Smart Contracts |
35+
//! | Aspect | Runtime
36+
//! | Smart Contracts |
3637
//! |-----------------------|-------------------------------------------------------------------------|----------------------------------------------------------------------|
37-
//! | **Design Philosophy** | Core logic of a blockchain, allowing broad and deep customization. | Designed for DApps deployed on the blockchain runtime.|
38-
//! | **Development Complexity** | Requires in-depth knowledge of Rust and Substrate. Suitable for complex blockchain architectures. | Easier to develop with knowledge of Smart Contract languages like Solidity or [ink!](https://use.ink/). |
39-
//! | **Upgradeability and Flexibility** | Offers comprehensive upgradeability with migration logic and on-chain governance, allowing modifications to the entire blockchain logic without hard forks. | Less flexible in upgrade migrations but offers more straightforward deployment and iteration. |
40-
//! | **Performance and Efficiency** | More efficient, optimized for specific needs of the blockchain. | Can be less efficient due to its generic nature (e.g. the overhead of a virtual machine). |
41-
//! | **Security Considerations** | Security flaws can affect the entire blockchain. | Security risks usually localized to the individual contract. |
42-
//! | **Weighing and Metering** | Operations can be weighed, allowing for precise benchmarking. | Execution is metered, allowing for measurement of resource consumption. |
38+
//! | **Design Philosophy** | Core logic of a blockchain, allowing broad and deep customization.
39+
//! | Designed for DApps deployed on the blockchain runtime.| | **Development Complexity** | Requires in-depth knowledge of Rust and Substrate. Suitable for complex blockchain architectures. | Easier to develop with knowledge of Smart Contract languages like Solidity or [ink!](https://use.ink/). |
40+
//! | **Upgradeability and Flexibility** | Offers comprehensive upgradeability with migration logic
41+
//! and on-chain governance, allowing modifications to the entire blockchain logic without hard
42+
//! forks. | Less flexible in upgrade migrations but offers more straightforward deployment and
43+
//! iteration. | | **Performance and Efficiency** | More efficient, optimized for specific needs of
44+
//! the blockchain. | Can be less efficient due to its generic nature (e.g. the overhead of a
45+
//! virtual machine). | | **Security Considerations** | Security flaws can affect the entire
46+
//! blockchain. | Security risks usually localized to the individual
47+
//! contract. | | **Weighing and Metering** | Operations can be weighed, allowing for precise
48+
//! benchmarking. | Execution is metered, allowing for measurement of resource
49+
//! consumption. |
4350
//!
4451
//! We will now explore these differences in more detail.
4552
//!

polkadot/node/core/candidate-validation/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,21 +773,21 @@ trait ValidationBackend {
773773
if num_death_retries_left > 0 {
774774
num_death_retries_left -= 1;
775775
} else {
776-
break;
776+
break
777777
},
778778

779779
Err(ValidationError::PossiblyInvalid(PossiblyInvalidError::JobError(_))) =>
780780
if num_job_error_retries_left > 0 {
781781
num_job_error_retries_left -= 1;
782782
} else {
783-
break;
783+
break
784784
},
785785

786786
Err(ValidationError::Internal(_)) =>
787787
if num_internal_retries_left > 0 {
788788
num_internal_retries_left -= 1;
789789
} else {
790-
break;
790+
break
791791
},
792792

793793
Ok(_) | Err(ValidationError::Invalid(_) | ValidationError::Preparation(_)) => break,

polkadot/node/core/candidate-validation/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ fn candidate_validation_retry_on_error_helper(
726726
ExecutorParams::default(),
727727
exec_kind,
728728
&Default::default(),
729-
));
729+
))
730730
}
731731

732732
#[test]

polkadot/rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ sp-consensus = { path = "../../substrate/primitives/consensus/common" }
2121
sp-consensus-babe = { path = "../../substrate/primitives/consensus/babe" }
2222
sc-chain-spec = { path = "../../substrate/client/chain-spec" }
2323
sc-rpc = { path = "../../substrate/client/rpc" }
24+
sc-rpc-spec-v2 = { path = "../../substrate/client/rpc-spec-v2" }
2425
sc-consensus-babe = { path = "../../substrate/client/consensus/babe" }
2526
sc-consensus-babe-rpc = { path = "../../substrate/client/consensus/babe/rpc" }
2627
sc-consensus-beefy = { path = "../../substrate/client/consensus/beefy" }

polkadot/rpc/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ where
121121
use sc_consensus_babe_rpc::{Babe, BabeApiServer};
122122
use sc_consensus_beefy_rpc::{Beefy, BeefyApiServer};
123123
use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer};
124+
use sc_rpc_spec_v2::chain_spec::{ChainSpec, ChainSpecApiServer};
124125
use sc_sync_state_rpc::{SyncState, SyncStateApiServer};
125126
use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer};
126127

@@ -134,6 +135,11 @@ where
134135
finality_provider,
135136
} = grandpa;
136137

138+
let chain_name = chain_spec.name().to_string();
139+
let genesis_hash = client.hash(0).ok().flatten().expect("Genesis block exists; qed");
140+
let properties = chain_spec.properties();
141+
142+
io.merge(ChainSpec::new(chain_name, genesis_hash, properties).into_rpc())?;
137143
io.merge(StateMigration::new(client.clone(), backend.clone(), deny_unsafe).into_rpc())?;
138144
io.merge(System::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?;
139145
io.merge(TransactionPayment::new(client.clone()).into_rpc())?;

substrate/bin/node-template/node/src/rpc.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,12 @@ where
5353
// to call into the runtime.
5454
// `module.merge(YourRpcTrait::into_rpc(YourRpcStruct::new(ReferenceToClient, ...)))?;`
5555

56+
// You probably want to enable the `rpc v2 chainSpec` API as well
57+
//
58+
// let chain_name = chain_spec.name().to_string();
59+
// let genesis_hash = client.block_hash(0).ok().flatten().expect("Genesis block exists; qed");
60+
// let properties = chain_spec.properties();
61+
// module.merge(ChainSpec::new(chain_name, genesis_hash, properties).into_rpc())?;
62+
5663
Ok(module)
5764
}

substrate/client/consensus/beefy/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ where
148148
// The block is imported as part of some chain sync.
149149
// The voter doesn't need to process it now.
150150
// It will be detected and processed as part of the voter state init.
151-
return Ok(inner_import_result);
151+
return Ok(inner_import_result)
152152
},
153153
}
154154

substrate/client/consensus/beefy/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ where
398398
header =
399399
wait_for_parent_header(backend.blockchain(), header, HEADER_SYNC_DELAY).await?;
400400
}
401-
return Ok(state);
401+
return Ok(state)
402402
}
403403

404404
// No valid voter-state persisted, re-initialize from pallet genesis.

substrate/client/network/src/protocol/notifications/behaviour.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ impl Notifications {
10371037
peerset_rejected,
10381038
incoming_index,
10391039
};
1040-
return self.report_reject(index).map_or((), |_| ());
1040+
return self.report_reject(index).map_or((), |_| ())
10411041
}
10421042

10431043
trace!(

0 commit comments

Comments
 (0)