-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
308 lines (257 loc) · 9.89 KB
/
lib.rs
File metadata and controls
308 lines (257 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//! The runtime contains the core logic of the ledger run by the Griffin node.
#![cfg_attr(not(feature = "std"), no_std)]
// Make the WASM binary available.
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
extern crate alloc;
pub mod genesis;
use alloc::{string::ToString, vec, vec::Vec};
use griffin_core::genesis::config_builder::GenesisConfig;
use griffin_core::types::{Address, AssetName, Input, PolicyId};
use griffin_core::utxo_set::TransparentUtxoSet;
pub use opaque::SessionKeys;
use parity_scale_codec::{Decode, Encode};
use polkadot_sdk_frame::runtime::apis;
use scale_info::TypeInfo;
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_inherents::InherentData;
use sp_runtime::{
impl_opaque_keys,
traits::Block as BlockT,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, BoundToRuntimeAppPublic,
};
use demo_authorities as Authorities;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::{runtime_version, RuntimeVersion};
/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
/// to even the core data structures.
pub mod opaque {
use super::*;
use authority_selection_inherents::MaybeFromCandidateKeys;
use sp_core::{ed25519, sr25519};
// This part is necessary for generating session keys in the runtime
impl_opaque_keys! {
pub struct SessionKeys {
pub aura: AuraAppPublic,
pub grandpa: GrandpaAppPublic,
}
}
impl From<(sr25519::Public, ed25519::Public)> for SessionKeys {
fn from((aura, grandpa): (sr25519::Public, ed25519::Public)) -> Self {
Self {
aura: aura.into(),
grandpa: grandpa.into(),
}
}
}
// Typically these are not implemented manually, but rather for the pallet associated with the
// keys. Here we are not using the pallets, and these implementations are trivial, so we just
// re-write them.
pub struct AuraAppPublic;
impl BoundToRuntimeAppPublic for AuraAppPublic {
type Public = AuraId;
}
pub struct GrandpaAppPublic;
impl BoundToRuntimeAppPublic for GrandpaAppPublic {
type Public = sp_consensus_grandpa::AuthorityId;
}
pub const CROSS_CHAIN: KeyTypeId = KeyTypeId(*b"crch");
pub struct CrossChainRuntimeAppPublic;
pub mod cross_chain_app {
use super::CROSS_CHAIN;
use parity_scale_codec::MaxEncodedLen;
use sidechain_domain::SidechainPublicKey;
use sp_core::crypto::AccountId32;
use sp_runtime::app_crypto::{app_crypto, ecdsa};
use sp_runtime::traits::IdentifyAccount;
use sp_runtime::MultiSigner;
use sp_std::vec::Vec;
app_crypto!(ecdsa, CROSS_CHAIN);
impl MaxEncodedLen for Signature {
fn max_encoded_len() -> usize {
ecdsa::Signature::max_encoded_len()
}
}
impl From<Signature> for Vec<u8> {
fn from(value: Signature) -> Self {
value.into_inner().0.to_vec()
}
}
impl From<Public> for AccountId32 {
fn from(value: Public) -> Self {
MultiSigner::from(ecdsa::Public::from(value)).into_account()
}
}
impl From<Public> for Vec<u8> {
fn from(value: Public) -> Self {
value.into_inner().0.to_vec()
}
}
impl TryFrom<SidechainPublicKey> for Public {
type Error = SidechainPublicKey;
fn try_from(pubkey: SidechainPublicKey) -> Result<Self, Self::Error> {
let cross_chain_public_key =
Public::try_from(pubkey.0.as_slice()).map_err(|_| pubkey)?;
Ok(cross_chain_public_key)
}
}
}
impl MaybeFromCandidateKeys for SessionKeys {}
impl_opaque_keys! {
pub struct CrossChainKey {
pub account: CrossChainPublic,
}
}
}
pub type CrossChainPublic = opaque::cross_chain_app::Public;
/// The runtime version.
#[runtime_version]
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: alloc::borrow::Cow::Borrowed("griffin-solochain-runtime"),
impl_name: alloc::borrow::Cow::Borrowed("griffin-solochain-runtime"),
authoring_version: 1,
spec_version: 0,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
system_version: 1,
};
/// The version information used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
NativeVersion {
runtime_version: VERSION,
can_author_with: Default::default(),
}
}
pub type Transaction = griffin_core::types::Transaction;
pub type Block = griffin_core::types::Block;
pub type Executive = griffin_core::Executive;
pub type Output = griffin_core::types::Output;
/// The main struct in this module.
#[derive(Encode, Decode, PartialEq, Eq, Clone, TypeInfo)]
pub struct Runtime;
impl_runtime_apis! {
impl griffin_core::utxo_set::TransparentUtxoSetApi<Block> for Runtime {
fn peek_utxo(input: &Input) -> Option<Output> {
TransparentUtxoSet::peek_utxo(input)
}
fn peek_utxo_by_address(addr: &Address) -> Vec<Output> {
TransparentUtxoSet::peek_utxos_from_address(addr)
}
fn peek_utxo_with_asset(asset_name: &AssetName, asset_policy: &PolicyId) -> Vec<Output> {
TransparentUtxoSet::peek_utxos_with_asset(asset_name, asset_policy)
}
}
// https://substrate.dev/rustdocs/master/sp_api/trait.Core.html
impl apis::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
VERSION
}
fn execute_block(block: Block) {
Executive::execute_block(block)
}
fn initialize_block(header: &<Block as BlockT>::Header) -> sp_runtime::ExtrinsicInclusionMode {
Executive::open_block(header)
}
}
impl apis::Metadata<Block> for Runtime {
fn metadata() -> OpaqueMetadata {
OpaqueMetadata::new(Vec::new())
}
fn metadata_at_version(_version: u32) -> Option<OpaqueMetadata> {
None
}
fn metadata_versions() -> alloc::vec::Vec<u32> {
Default::default()
}
}
impl apis::BlockBuilder<Block> for Runtime {
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {
Executive::apply_extrinsic(extrinsic)
}
fn finalize_block() -> <Block as BlockT>::Header {
Executive::close_block()
}
fn inherent_extrinsics(_data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
Vec::new()
}
fn check_inherents(
_block: Block,
_data: InherentData
) -> sp_inherents::CheckInherentsResult {
sp_inherents::CheckInherentsResult::new()
}
}
impl apis::TaggedTransactionQueue<Block> for Runtime {
fn validate_transaction(
source: TransactionSource,
tx: <Block as BlockT>::Extrinsic,
block_hash: <Block as BlockT>::Hash,
) -> TransactionValidity {
Executive::validate_transaction(source, tx, block_hash)
}
}
impl apis::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
opaque::SessionKeys::generate(seed)
}
fn decode_session_keys(
encoded: Vec<u8>,
) -> Option<Vec<(Vec<u8>, sp_core::crypto::KeyTypeId)>> {
opaque::SessionKeys::decode_into_raw_public_keys(&encoded)
}
}
impl apis::AuraApi<Block, AuraId> for Runtime {
fn slot_duration() -> sp_consensus_aura::SlotDuration {
sp_consensus_aura::SlotDuration::from_millis(griffin_core::Executive::slot_length() as u64)
}
fn authorities() -> Vec<AuraId> {
Authorities::aura_authorities()
}
}
impl apis::GrandpaApi<Block> for Runtime {
fn grandpa_authorities() -> sp_consensus_grandpa::AuthorityList {
Authorities::grandpa_authorities()
}
fn current_set_id() -> sp_consensus_grandpa::SetId {
0u64
}
fn submit_report_equivocation_unsigned_extrinsic(
_equivocation_proof: sp_consensus_grandpa::EquivocationProof<
<Block as BlockT>::Hash,
sp_runtime::traits::NumberFor<Block>,
>,
_key_owner_proof: sp_consensus_grandpa::OpaqueKeyOwnershipProof,
) -> Option<()> {
None
}
fn generate_key_ownership_proof(
_set_id: sp_consensus_grandpa::SetId,
_authority_id: sp_consensus_grandpa::AuthorityId,
) -> Option<sp_consensus_grandpa::OpaqueKeyOwnershipProof> {
None
}
}
impl apis::GenesisBuilder<Block> for Runtime {
fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
let genesis_config = serde_json::from_slice::<GenesisConfig>(config.as_slice())
.map_err(|_| "The input JSON is not a valid genesis configuration.")?;
griffin_core::genesis::GriffinGenesisConfigBuilder::build(genesis_config)
}
fn get_preset(_id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
let genesis_config : &GenesisConfig = &genesis::get_genesis_config("".to_string());
Some(serde_json::to_vec(genesis_config)
.expect("Genesis configuration is valid."))
}
fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
vec![]
}
}
}