1#![cfg_attr(not(feature = "std"), no_std)]
23
24#[cfg(feature = "std")]
26include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
27
28extern crate alloc;
29
30use frame_support::dispatch::PerDispatchClass;
31use frame_support::genesis_builder_helper::{build_state, get_preset};
32use frame_support::pallet_prelude::*;
33use frame_support::{derive_impl, parameter_types};
34use frame_system::limits::WeightsPerClass;
35use frame_system::pallet_prelude::*;
36use pallet_executive::Executive;
37use sp_api::impl_runtime_apis;
38use sp_core::{ConstU32, OpaqueMetadata};
39use sp_inherents::{CheckInherentsResult, InherentData};
40use sp_runtime::traits::Get;
41use sp_runtime::transaction_validity::{TransactionSource, TransactionValidity};
42use sp_runtime::{ApplyExtrinsicResult, ExtrinsicInclusionMode};
43use sp_std::vec;
44use sp_std::vec::Vec;
45#[cfg(feature = "std")]
46use sp_version::NativeVersion;
47use sp_version::{RuntimeVersion, runtime_version};
48
49const BITCOIN_BASE_BLOCK_WEIGHT: u64 = 80 * 4 + 4;
51
52const BITCOIN_MAX_WEIGHT: u64 = 4_000_000;
55
56#[runtime_version]
57pub const VERSION: RuntimeVersion = RuntimeVersion {
58 spec_name: alloc::borrow::Cow::Borrowed("subcoin"),
59 impl_name: alloc::borrow::Cow::Borrowed("subcoin"),
60 authoring_version: 0,
61 spec_version: 0,
62 impl_version: 0,
63 apis: RUNTIME_API_VERSIONS,
64 transaction_version: 0,
65 system_version: 1,
66};
67
68#[cfg(feature = "std")]
70pub fn native_version() -> NativeVersion {
71 NativeVersion {
72 runtime_version: VERSION,
73 can_author_with: Default::default(),
74 }
75}
76
77parameter_types! {
78 pub const Version: RuntimeVersion = VERSION;
79}
80
81#[frame_support::runtime]
82mod runtime {
83 #[runtime::runtime]
85 #[runtime::derive(RuntimeCall, RuntimeEvent, RuntimeError, RuntimeOrigin, RuntimeTask)]
87 pub struct Runtime;
88
89 #[runtime::pallet_index(0)]
90 pub type System = frame_system::Pallet<Runtime>;
91
92 #[runtime::pallet_index(1)]
93 pub type Bitcoin = pallet_bitcoin::Pallet<Runtime>;
94}
95
96#[derive_impl(frame_system::config_preludes::SolochainDefaultConfig as frame_system::DefaultConfig)]
97impl frame_system::Config for Runtime {
98 type Block = Block;
99 type Version = Version;
100 type BlockHashCount = ConstU32<1024>;
101 type BlockWeights = BlockWeights;
102}
103
104pub struct BlockWeights;
106
107impl Get<frame_system::limits::BlockWeights> for BlockWeights {
108 fn get() -> frame_system::limits::BlockWeights {
109 frame_system::limits::BlockWeights {
110 base_block: Weight::from_parts(BITCOIN_BASE_BLOCK_WEIGHT, 0u64),
111 max_block: Weight::from_parts(BITCOIN_MAX_WEIGHT, u64::MAX),
112 per_class: PerDispatchClass::new(|class| {
113 let initial = if class == DispatchClass::Mandatory {
114 None
115 } else {
116 Some(Weight::zero())
117 };
118 WeightsPerClass {
119 base_extrinsic: Weight::zero(),
120 max_extrinsic: None,
121 max_total: initial,
122 reserved: initial,
123 }
124 }),
125 }
126 }
127}
128
129impl pallet_bitcoin::Config for Runtime {
130 type WeightInfo = pallet_bitcoin::BitcoinTransactionWeight;
131}
132
133type SignedExtra = (
134 frame_system::CheckNonZeroSender<Runtime>,
135 frame_system::CheckSpecVersion<Runtime>,
136 frame_system::CheckGenesis<Runtime>,
137);
138type Signature = crate::types_common::Signature;
139type Block = crate::types_common::BlockOf<Runtime, SignedExtra>;
140pub type Address = sp_runtime::MultiAddress<interface::AccountId, ()>;
142pub type Header = HeaderFor<Runtime>;
143pub type UncheckedExtrinsic =
144 sp_runtime::generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
145
146type RuntimeExecutive =
147 Executive<Runtime, Block, frame_system::ChainContext<Runtime>, Runtime, AllPalletsWithSystem>;
148
149impl_runtime_apis! {
150 impl sp_api::Core<Block> for Runtime {
151 fn version() -> RuntimeVersion {
152 VERSION
153 }
154
155 fn execute_block(block: <Block as sp_runtime::traits::Block>::LazyBlock) {
156 RuntimeExecutive::execute_block(block)
157 }
158
159 fn initialize_block(header: &Header) -> ExtrinsicInclusionMode {
160 RuntimeExecutive::initialize_block(header)
161 }
162 }
163
164 impl sp_api::Metadata<Block> for Runtime {
165 fn metadata() -> OpaqueMetadata {
166 OpaqueMetadata::new(Runtime::metadata().into())
167 }
168
169 fn metadata_at_version(version: u32) -> Option<OpaqueMetadata> {
170 Runtime::metadata_at_version(version)
171 }
172
173 fn metadata_versions() -> Vec<u32> {
174 Runtime::metadata_versions()
175 }
176 }
177
178 impl sp_block_builder::BlockBuilder<Block> for Runtime {
180 fn apply_extrinsic(extrinsic: ExtrinsicFor<Runtime>) -> ApplyExtrinsicResult {
181 RuntimeExecutive::apply_extrinsic(extrinsic)
182 }
183
184 fn finalize_block() -> HeaderFor<Runtime> {
185 unimplemented!("BlockBuilder::finalize_block() is useless in Subcoin")
186 }
187
188 fn inherent_extrinsics(data: InherentData) -> Vec<ExtrinsicFor<Runtime>> {
189 data.create_extrinsics()
190 }
191
192 fn check_inherents(
193 _block: <Block as sp_runtime::traits::Block>::LazyBlock,
194 _data: InherentData,
195 ) -> CheckInherentsResult {
196 CheckInherentsResult::new()
198 }
199 }
200
201 impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
202 fn validate_transaction(
203 source: TransactionSource,
204 tx: ExtrinsicFor<Runtime>,
205 block_hash: <Runtime as frame_system::Config>::Hash,
206 ) -> TransactionValidity {
207 RuntimeExecutive::validate_transaction(source, tx, block_hash)
208 }
209 }
210
211 impl frame_system_rpc_runtime_api::AccountNonceApi<Block, interface::AccountId, interface::Nonce> for Runtime {
213 fn account_nonce(account: interface::AccountId) -> interface::Nonce {
214 System::account_nonce(account)
215 }
216 }
217
218 impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
219 fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
220 build_state::<RuntimeGenesisConfig>(config)
221 }
222
223 fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
224 get_preset::<RuntimeGenesisConfig>(id, |_| None)
225 }
226
227 fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
228 vec![]
229 }
230 }
231
232 impl subcoin_runtime_primitives::SubcoinApi<Block> for Runtime {
233 fn execute_block_without_state_root_check(block: <Block as sp_runtime::traits::Block>::LazyBlock) {
234 RuntimeExecutive::execute_block_without_state_root_check(block)
235 }
236
237 fn finalize_block_without_checks(header: HeaderFor<Runtime>) {
238 RuntimeExecutive::finalize_block_without_checks(header);
239 }
240
241 fn coins_count() -> u64 {
242 Bitcoin::coins_count()
243 }
244
245 fn get_utxos(outpoints: Vec<subcoin_runtime_primitives::OutPoint>) -> Vec<Option<subcoin_runtime_primitives::Coin>> {
246 Bitcoin::get_utxos(outpoints)
247 }
248 }
249}
250
251mod types_common {
264 use frame_system::Config as SysConfig;
265 use sp_runtime::{OpaqueExtrinsic, generic, traits};
266
267 pub type Signature = sp_runtime::MultiSignature;
269
270 pub type AccountId =
272 <<Signature as traits::Verify>::Signer as traits::IdentifyAccount>::AccountId;
273
274 pub type BlockNumber = u32;
276
277 type HeaderInner = generic::Header<BlockNumber, traits::BlakeTwo256>;
279
280 type ExtrinsicInner<T, Extra, AccountIndex = ()> = generic::UncheckedExtrinsic<
283 sp_runtime::MultiAddress<AccountId, AccountIndex>,
284 <T as SysConfig>::RuntimeCall,
285 Signature,
286 Extra,
287 >;
288
289 pub type BlockOf<T, Extra = ()> = generic::Block<HeaderInner, ExtrinsicInner<T, Extra>>;
296
297 pub type OpaqueBlock = generic::Block<HeaderInner, OpaqueExtrinsic>;
302}
303
304pub mod interface {
310 use super::Runtime;
311
312 pub type Block = super::Block;
313 pub use crate::types_common::OpaqueBlock;
314 pub type AccountId = <Runtime as frame_system::Config>::AccountId;
315 pub type Nonce = <Runtime as frame_system::Config>::Nonce;
316 pub type Hash = <Runtime as frame_system::Config>::Hash;
317 pub type Balance = u128;
318}