runtime_api.rs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. use frame_support::inherent::{CheckInherentsResult, InherentData};
  2. use frame_support::traits::{KeyOwnerProofSystem, Randomness};
  3. use frame_support::unsigned::{TransactionSource, TransactionValidity};
  4. use pallet_contracts_rpc_runtime_api::ContractExecResult;
  5. use pallet_grandpa::fg_primitives;
  6. use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
  7. use sp_api::impl_runtime_apis;
  8. use sp_core::crypto::KeyTypeId;
  9. use sp_core::OpaqueMetadata;
  10. use sp_runtime::traits::{BlakeTwo256, Block as BlockT, NumberFor};
  11. use sp_runtime::{generic, ApplyExtrinsicResult};
  12. use sp_std::vec::Vec;
  13. use crate::constants::PRIMARY_PROBABILITY;
  14. use crate::{
  15. AccountId, AuthorityDiscoveryId, Balance, BlockNumber, EpochDuration, GrandpaAuthorityList,
  16. GrandpaId, Hash, Index, RuntimeVersion, Signature, VERSION,
  17. };
  18. use crate::{
  19. AllModules, AuthorityDiscovery, Babe, Call, Contracts, Grandpa, Historical, InherentDataExt,
  20. RandomnessCollectiveFlip, Runtime, SessionKeys, System, TransactionPayment,
  21. };
  22. /// The SignedExtension to the basic transaction logic.
  23. pub type SignedExtra = (
  24. system::CheckSpecVersion<Runtime>,
  25. system::CheckTxVersion<Runtime>,
  26. system::CheckGenesis<Runtime>,
  27. system::CheckEra<Runtime>,
  28. system::CheckNonce<Runtime>,
  29. system::CheckWeight<Runtime>,
  30. pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
  31. pallet_grandpa::ValidateEquivocationReport<Runtime>,
  32. );
  33. /// Digest item type.
  34. pub type DigestItem = generic::DigestItem<Hash>;
  35. /// Block header type as expected by this runtime.
  36. pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
  37. /// Block type as expected by this runtime.
  38. pub type Block = generic::Block<Header, UncheckedExtrinsic>;
  39. /// A Block signed with a Justification
  40. pub type SignedBlock = generic::SignedBlock<Block>;
  41. /// BlockId type as expected by this runtime.
  42. pub type BlockId = generic::BlockId<Block>;
  43. /// Unchecked extrinsic type as expected by this runtime.
  44. pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<AccountId, Call, Signature, SignedExtra>;
  45. /// Executive: handles dispatch to the various modules.
  46. pub type Executive =
  47. frame_executive::Executive<Runtime, Block, system::ChainContext<Runtime>, Runtime, AllModules>;
  48. /// Export of the private const generated within the macro.
  49. pub const EXPORTED_RUNTIME_API_VERSIONS: sp_version::ApisVec = RUNTIME_API_VERSIONS;
  50. impl_runtime_apis! {
  51. impl sp_api::Core<Block> for Runtime {
  52. fn version() -> RuntimeVersion {
  53. VERSION
  54. }
  55. fn execute_block(block: Block) {
  56. Executive::execute_block(block)
  57. }
  58. fn initialize_block(header: &<Block as BlockT>::Header) {
  59. Executive::initialize_block(header)
  60. }
  61. }
  62. impl sp_api::Metadata<Block> for Runtime {
  63. fn metadata() -> OpaqueMetadata {
  64. Runtime::metadata().into()
  65. }
  66. }
  67. impl sp_block_builder::BlockBuilder<Block> for Runtime {
  68. fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {
  69. Executive::apply_extrinsic(extrinsic)
  70. }
  71. fn finalize_block() -> <Block as BlockT>::Header {
  72. Executive::finalize_block()
  73. }
  74. fn inherent_extrinsics(data: InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
  75. data.create_extrinsics()
  76. }
  77. fn check_inherents(block: Block, data: InherentData) -> CheckInherentsResult {
  78. data.check_extrinsics(&block)
  79. }
  80. fn random_seed() -> <Block as BlockT>::Hash {
  81. RandomnessCollectiveFlip::random_seed()
  82. }
  83. }
  84. impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
  85. fn validate_transaction(
  86. source: TransactionSource,
  87. tx: <Block as BlockT>::Extrinsic,
  88. ) -> TransactionValidity {
  89. Executive::validate_transaction(source, tx)
  90. }
  91. }
  92. impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
  93. fn offchain_worker(header: &<Block as BlockT>::Header) {
  94. Executive::offchain_worker(header)
  95. }
  96. }
  97. impl fg_primitives::GrandpaApi<Block> for Runtime {
  98. fn grandpa_authorities() -> GrandpaAuthorityList {
  99. Grandpa::grandpa_authorities()
  100. }
  101. fn submit_report_equivocation_extrinsic(
  102. equivocation_proof: fg_primitives::EquivocationProof<
  103. <Block as BlockT>::Hash,
  104. NumberFor<Block>,
  105. >,
  106. key_owner_proof: fg_primitives::OpaqueKeyOwnershipProof,
  107. ) -> Option<()> {
  108. let key_owner_proof = key_owner_proof.decode()?;
  109. Grandpa::submit_report_equivocation_extrinsic(
  110. equivocation_proof,
  111. key_owner_proof,
  112. )
  113. }
  114. fn generate_key_ownership_proof(
  115. _set_id: fg_primitives::SetId,
  116. authority_id: GrandpaId,
  117. ) -> Option<fg_primitives::OpaqueKeyOwnershipProof> {
  118. use codec::Encode;
  119. Historical::prove((fg_primitives::KEY_TYPE, authority_id))
  120. .map(|p| p.encode())
  121. .map(fg_primitives::OpaqueKeyOwnershipProof::new)
  122. }
  123. }
  124. impl sp_consensus_babe::BabeApi<Block> for Runtime {
  125. fn configuration() -> sp_consensus_babe::BabeGenesisConfiguration {
  126. // The choice of `c` parameter (where `1 - c` represents the
  127. // probability of a slot being empty), is done in accordance to the
  128. // slot duration and expected target block time, for safely
  129. // resisting network delays of maximum two seconds.
  130. // <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
  131. sp_consensus_babe::BabeGenesisConfiguration {
  132. slot_duration: Babe::slot_duration(),
  133. epoch_length: EpochDuration::get(),
  134. c: PRIMARY_PROBABILITY,
  135. genesis_authorities: Babe::authorities(),
  136. randomness: Babe::randomness(),
  137. allowed_slots: sp_consensus_babe::AllowedSlots::PrimaryAndSecondaryPlainSlots,
  138. }
  139. }
  140. fn current_epoch_start() -> sp_consensus_babe::SlotNumber {
  141. Babe::current_epoch_start()
  142. }
  143. }
  144. impl sp_authority_discovery::AuthorityDiscoveryApi<Block> for Runtime {
  145. fn authorities() -> Vec<AuthorityDiscoveryId> {
  146. AuthorityDiscovery::authorities()
  147. }
  148. }
  149. impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {
  150. fn account_nonce(account: AccountId) -> Index {
  151. System::account_nonce(account)
  152. }
  153. }
  154. impl pallet_contracts_rpc_runtime_api::ContractsApi<Block, AccountId, Balance, BlockNumber>
  155. for Runtime
  156. {
  157. fn call(
  158. origin: AccountId,
  159. dest: AccountId,
  160. value: Balance,
  161. gas_limit: u64,
  162. input_data: Vec<u8>,
  163. ) -> ContractExecResult {
  164. let exec_result =
  165. Contracts::bare_call(origin, dest, value, gas_limit, input_data);
  166. match exec_result {
  167. Ok(v) => ContractExecResult::Success {
  168. status: v.status,
  169. data: v.data,
  170. },
  171. Err(_) => ContractExecResult::Error,
  172. }
  173. }
  174. fn get_storage(
  175. address: AccountId,
  176. key: [u8; 32],
  177. ) -> pallet_contracts_primitives::GetStorageResult {
  178. Contracts::get_storage(address, key)
  179. }
  180. fn rent_projection(
  181. address: AccountId,
  182. ) -> pallet_contracts_primitives::RentProjectionResult<BlockNumber> {
  183. Contracts::rent_projection(address)
  184. }
  185. }
  186. impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<
  187. Block,
  188. Balance,
  189. UncheckedExtrinsic,
  190. > for Runtime {
  191. fn query_info(uxt: UncheckedExtrinsic, len: u32) -> RuntimeDispatchInfo<Balance> {
  192. TransactionPayment::query_info(uxt, len)
  193. }
  194. }
  195. impl sp_session::SessionKeys<Block> for Runtime {
  196. fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
  197. SessionKeys::generate(seed)
  198. }
  199. fn decode_session_keys(
  200. encoded: Vec<u8>,
  201. ) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {
  202. SessionKeys::decode_into_raw_public_keys(&encoded)
  203. }
  204. }
  205. }