1use crate::{
21 codec::{Codec, Decode, DecodeWithMemTracking, Encode, MaxEncodedLen},
22 generic::{self, UncheckedExtrinsic},
23 scale_info::TypeInfo,
24 traits::{self, BlakeTwo256, Dispatchable, OpaqueKeys},
25 DispatchResultWithInfo, KeyTypeId,
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
110 fn all() -> Vec<Self> {
111 ALL_KEYS.with(|l| l.borrow().clone())
112 }
113
114 fn generate_pair(_: Option<Vec<u8>>) -> Self {
115 use rand::RngCore;
116 UintAuthorityId(rand::thread_rng().next_u64())
117 }
118
119 fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature> {
120 Some(TestSignature(self.0, msg.as_ref().to_vec()))
121 }
122
123 fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
124 traits::Verify::verify(signature, msg.as_ref(), &self.0)
125 }
126
127 fn to_raw_vec(&self) -> Vec<u8> {
128 AsRef::<[u8]>::as_ref(self).to_vec()
129 }
130}
131
132impl OpaqueKeys for UintAuthorityId {
133 type KeyTypeIdProviders = ();
134
135 fn key_ids() -> &'static [KeyTypeId] {
136 &[key_types::DUMMY]
137 }
138
139 fn get_raw(&self, _: KeyTypeId) -> &[u8] {
140 self.as_ref()
141 }
142
143 fn get<T: Decode>(&self, _: KeyTypeId) -> Option<T> {
144 self.using_encoded(|mut x| T::decode(&mut x)).ok()
145 }
146}
147
148impl traits::IdentifyAccount for UintAuthorityId {
149 type AccountId = u64;
150
151 fn into_account(self) -> Self::AccountId {
152 self.0
153 }
154}
155
156impl traits::Verify for UintAuthorityId {
157 type Signer = Self;
158
159 fn verify<L: traits::Lazy<[u8]>>(
160 &self,
161 _msg: L,
162 signer: &<Self::Signer as traits::IdentifyAccount>::AccountId,
163 ) -> bool {
164 self.0 == *signer
165 }
166}
167
168#[derive(
170 Eq,
171 PartialEq,
172 Clone,
173 Debug,
174 Hash,
175 Serialize,
176 Deserialize,
177 Encode,
178 Decode,
179 DecodeWithMemTracking,
180 TypeInfo,
181)]
182pub struct TestSignature(pub u64, pub Vec<u8>);
183
184impl traits::Verify for TestSignature {
185 type Signer = UintAuthorityId;
186
187 fn verify<L: traits::Lazy<[u8]>>(&self, mut msg: L, signer: &u64) -> bool {
188 signer == &self.0 && msg.get() == &self.1[..]
189 }
190}
191
192pub type DigestItem = generic::DigestItem;
194
195pub type Digest = generic::Digest;
197
198pub type Header = generic::Header<u64, BlakeTwo256>;
200
201impl Header {
202 pub fn new_from_number(number: <Self as traits::Header>::Number) -> Self {
204 Self {
205 number,
206 extrinsics_root: Default::default(),
207 state_root: Default::default(),
208 parent_hash: Default::default(),
209 digest: Default::default(),
210 }
211 }
212}
213
214#[derive(
216 PartialEq, Eq, Clone, Serialize, Debug, Encode, Decode, DecodeWithMemTracking, TypeInfo,
217)]
218pub struct Block<Xt> {
219 pub header: Header,
221 pub extrinsics: Vec<Xt>,
223}
224
225impl<Xt> traits::HeaderProvider for Block<Xt> {
226 type HeaderT = Header;
227}
228
229impl<
230 Xt: 'static
231 + Codec
232 + DecodeWithMemTracking
233 + Sized
234 + Send
235 + Sync
236 + Serialize
237 + Clone
238 + Eq
239 + Debug
240 + traits::ExtrinsicLike,
241 > traits::Block for Block<Xt>
242{
243 type Extrinsic = Xt;
244 type Header = Header;
245 type Hash = <Header as traits::Header>::Hash;
246
247 fn header(&self) -> &Self::Header {
248 &self.header
249 }
250 fn extrinsics(&self) -> &[Self::Extrinsic] {
251 &self.extrinsics[..]
252 }
253 fn deconstruct(self) -> (Self::Header, Vec<Self::Extrinsic>) {
254 (self.header, self.extrinsics)
255 }
256 fn new(header: Self::Header, extrinsics: Vec<Self::Extrinsic>) -> Self {
257 Block { header, extrinsics }
258 }
259 fn encode_from(header: &Self::Header, extrinsics: &[Self::Extrinsic]) -> Vec<u8> {
260 (header, extrinsics).encode()
261 }
262}
263
264impl<'a, Xt> Deserialize<'a> for Block<Xt>
265where
266 Block<Xt>: Decode,
267{
268 fn deserialize<D: Deserializer<'a>>(de: D) -> Result<Self, D::Error> {
269 let r = <Vec<u8>>::deserialize(de)?;
270 Decode::decode(&mut &r[..])
271 .map_err(|e| DeError::custom(format!("Invalid value passed into decode: {}", e)))
272 }
273}
274
275pub type TestXt<Call, Extra> = UncheckedExtrinsic<u64, Call, (), Extra>;
277
278#[derive(PartialEq, Eq, Debug, Clone, Encode, Decode, DecodeWithMemTracking, TypeInfo)]
280pub struct MockCallU64(pub u64);
281
282impl Dispatchable for MockCallU64 {
283 type RuntimeOrigin = u64;
284 type Config = ();
285 type Info = ();
286 type PostInfo = ();
287 fn dispatch(self, _origin: Self::RuntimeOrigin) -> DispatchResultWithInfo<Self::PostInfo> {
288 Ok(())
289 }
290}
291
292impl From<u64> for MockCallU64 {
293 fn from(value: u64) -> Self {
294 Self(value)
295 }
296}