1use crate::{
21 codec::{Codec, Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen},
22 generic::{self, LazyBlock, UncheckedExtrinsic},
23 scale_info::TypeInfo,
24 traits::{self, BlakeTwo256, Dispatchable, LazyExtrinsic, OpaqueKeys},
25 DispatchResultWithInfo, KeyTypeId, OpaqueExtrinsic,
26};
27use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize};
28use sp_core::crypto::{key_types, ByteArray, CryptoType, Dummy};
29pub use sp_core::{sr25519, H256};
30use std::{cell::RefCell, fmt::Debug};
31
32#[derive(
39 Default,
40 PartialEq,
41 Eq,
42 Clone,
43 Encode,
44 Decode,
45 DecodeWithMemTracking,
46 Debug,
47 Hash,
48 Serialize,
49 Deserialize,
50 PartialOrd,
51 Ord,
52 MaxEncodedLen,
53 TypeInfo,
54)]
55pub struct UintAuthorityId(pub u64);
56
57impl From<u64> for UintAuthorityId {
58 fn from(id: u64) -> Self {
59 UintAuthorityId(id)
60 }
61}
62
63impl From<UintAuthorityId> for u64 {
64 fn from(id: UintAuthorityId) -> u64 {
65 id.0
66 }
67}
68
69impl UintAuthorityId {
70 pub fn to_public_key<T: ByteArray>(&self) -> T {
72 let mut bytes = [0u8; 32];
73 bytes[0..8].copy_from_slice(&self.0.to_le_bytes());
74 T::from_slice(&bytes).unwrap()
75 }
76
77 pub fn set_all_keys<T: Into<UintAuthorityId>>(keys: impl IntoIterator<Item = T>) {
79 ALL_KEYS.with(|l| *l.borrow_mut() = keys.into_iter().map(Into::into).collect())
80 }
81}
82
83impl CryptoType for UintAuthorityId {
84 type Pair = Dummy;
85}
86
87impl AsRef<[u8]> for UintAuthorityId {
88 fn as_ref(&self) -> &[u8] {
89 unsafe {
92 std::slice::from_raw_parts(
93 &self.0 as *const u64 as *const _,
94 std::mem::size_of::<u64>(),
95 )
96 }
97 }
98}
99
100thread_local! {
101 static ALL_KEYS: RefCell<Vec<UintAuthorityId>> = RefCell::new(vec![]);
103}
104
105impl sp_application_crypto::RuntimeAppPublic for UintAuthorityId {
106 const ID: KeyTypeId = key_types::DUMMY;
107
108 type Signature = TestSignature;
109 type ProofOfPossession = TestSignature;
110
111 fn all() -> Vec<Self> {
112 ALL_KEYS.with(|l| l.borrow().clone())
113 }
114
115 fn generate_pair(_: Option<Vec<u8>>) -> Self {
116 use rand::RngCore;
117 UintAuthorityId(rand::thread_rng().next_u64())
118 }
119
120 fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature> {
121 Some(TestSignature(self.0, msg.as_ref().to_vec()))
122 }
123
124 fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
125 traits::Verify::verify(signature, msg.as_ref(), &self.0)
126 }
127
128 fn generate_proof_of_possession(&mut self, owner: &[u8]) -> Option<Self::Signature> {
129 Some(TestSignature(self.0, owner.to_vec()))
130 }
131
132 fn verify_proof_of_possession(&self, owner: &[u8], pop: &Self::Signature) -> bool {
133 traits::Verify::verify(pop, owner, &self.0)
134 }
135
136 fn to_raw_vec(&self) -> Vec<u8> {
137 AsRef::<[u8]>::as_ref(self).to_vec()
138 }
139}
140
141impl OpaqueKeys for UintAuthorityId {
142 type KeyTypeIdProviders = ();
143
144 fn key_ids() -> &'static [KeyTypeId] {
145 &[key_types::DUMMY]
146 }
147
148 fn get_raw(&self, _: KeyTypeId) -> &[u8] {
149 self.as_ref()
150 }
151
152 fn get<T: Decode>(&self, _: KeyTypeId) -> Option<T> {
153 self.using_encoded(|mut x| T::decode(&mut x)).ok()
154 }
155
156 fn ownership_proof_is_valid(&self, _: &[u8], _: &[u8]) -> bool {
157 true
158 }
159}
160
161impl traits::IdentifyAccount for UintAuthorityId {
162 type AccountId = u64;
163
164 fn into_account(self) -> Self::AccountId {
165 self.0
166 }
167}
168
169impl traits::Verify for UintAuthorityId {
170 type Signer = Self;
171
172 fn verify<L: traits::Lazy<[u8]>>(
173 &self,
174 _msg: L,
175 signer: &<Self::Signer as traits::IdentifyAccount>::AccountId,
176 ) -> bool {
177 self.0 == *signer
178 }
179}
180
181#[derive(
183 Eq,
184 PartialEq,
185 Clone,
186 Debug,
187 Hash,
188 Serialize,
189 Deserialize,
190 Encode,
191 Decode,
192 DecodeWithMemTracking,
193 TypeInfo,
194)]
195pub struct TestSignature(pub u64, pub Vec<u8>);
196
197impl traits::Verify for TestSignature {
198 type Signer = UintAuthorityId;
199
200 fn verify<L: traits::Lazy<[u8]>>(&self, mut msg: L, signer: &u64) -> bool {
201 signer == &self.0 && msg.get() == &self.1[..]
202 }
203}
204
205pub type DigestItem = generic::DigestItem;
207
208pub type Digest = generic::Digest;
210
211pub type Header = generic::Header<u64, BlakeTwo256>;
213
214impl Header {
215 pub fn new_from_number(number: <Self as traits::Header>::Number) -> Self {
217 Self {
218 number,
219 extrinsics_root: Default::default(),
220 state_root: Default::default(),
221 parent_hash: Default::default(),
222 digest: Default::default(),
223 }
224 }
225}
226
227#[derive(
229 PartialEq, Eq, Clone, Serialize, Debug, Encode, Decode, DecodeWithMemTracking, TypeInfo,
230)]
231pub struct Block<Xt> {
232 pub header: Header,
234 pub extrinsics: Vec<Xt>,
236}
237
238impl<Xt> traits::HeaderProvider for Block<Xt> {
239 type HeaderT = Header;
240}
241
242impl<Xt: Into<OpaqueExtrinsic>> From<Block<Xt>> for LazyBlock<Header, Xt> {
243 fn from(block: Block<Xt>) -> Self {
244 LazyBlock::new(block.header, block.extrinsics)
245 }
246}
247
248impl<Xt> EncodeLike<LazyBlock<Header, Xt>> for Block<Xt>
249where
250 Block<Xt>: Encode,
251 LazyBlock<Header, Xt>: Encode,
252{
253}
254
255impl<Xt> EncodeLike<Block<Xt>> for LazyBlock<Header, Xt>
256where
257 Block<Xt>: Encode,
258 LazyBlock<Header, Xt>: Encode,
259{
260}
261
262impl<
263 Xt: 'static
264 + Codec
265 + DecodeWithMemTracking
266 + Sized
267 + Send
268 + Sync
269 + Serialize
270 + Clone
271 + Eq
272 + Debug
273 + traits::ExtrinsicLike
274 + Into<OpaqueExtrinsic>
275 + LazyExtrinsic,
276 > traits::Block for Block<Xt>
277{
278 type Extrinsic = Xt;
279 type Header = Header;
280 type Hash = <Header as traits::Header>::Hash;
281 type LazyBlock = LazyBlock<Header, Xt>;
282
283 fn header(&self) -> &Self::Header {
284 &self.header
285 }
286 fn extrinsics(&self) -> &[Self::Extrinsic] {
287 &self.extrinsics[..]
288 }
289 fn deconstruct(self) -> (Self::Header, Vec<Self::Extrinsic>) {
290 (self.header, self.extrinsics)
291 }
292 fn new(header: Self::Header, extrinsics: Vec<Self::Extrinsic>) -> Self {
293 Block { header, extrinsics }
294 }
295}
296
297impl<'a, Xt> Deserialize<'a> for Block<Xt>
298where
299 Block<Xt>: Decode,
300{
301 fn deserialize<D: Deserializer<'a>>(de: D) -> Result<Self, D::Error> {
302 let r = <Vec<u8>>::deserialize(de)?;
303 Decode::decode(&mut &r[..])
304 .map_err(|e| DeError::custom(format!("Invalid value passed into decode: {}", e)))
305 }
306}
307
308pub type TestXt<Call, Extra> = UncheckedExtrinsic<u64, Call, (), Extra>;
310
311#[derive(PartialEq, Eq, Debug, Clone, Encode, Decode, DecodeWithMemTracking, TypeInfo)]
313pub struct MockCallU64(pub u64);
314
315impl Dispatchable for MockCallU64 {
316 type RuntimeOrigin = u64;
317 type Config = ();
318 type Info = ();
319 type PostInfo = ();
320 fn dispatch(self, _origin: Self::RuntimeOrigin) -> DispatchResultWithInfo<Self::PostInfo> {
321 Ok(())
322 }
323}
324
325impl From<u64> for MockCallU64 {
326 fn from(value: u64) -> Self {
327 Self(value)
328 }
329}