-
Notifications
You must be signed in to change notification settings - Fork 18
/
genesis.rs
195 lines (163 loc) · 6.96 KB
/
genesis.rs
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
//! Helper module to build a genesis configuration for the template runtime.
use super::{
kitties::{KittyData, Parent},
money::Coin,
OuterConstraintChecker, OuterConstraintCheckerInherentHooks, OuterVerifier, WASM_BINARY,
};
use hex_literal::hex;
use tuxedo_core::{
inherents::InherentInternal,
verifier::{Sr25519Signature, ThresholdMultiSignature, UpForGrabs},
};
/// Helper type for the ChainSpec.
pub type RuntimeGenesisConfig =
tuxedo_core::genesis::TuxedoGenesisConfig<OuterVerifier, OuterConstraintChecker>;
const SHAWN_PUB_KEY_BYTES: [u8; 32] =
hex!("d2bf4b844dfefd6772a8843e669f943408966a977e3ae2af1dd78e0f55f4df67");
const ANDREW_PUB_KEY_BYTES: [u8; 32] =
hex!("baa81e58b1b4d053c2e86d93045765036f9d265c7dfe8b9693bbc2c0f048d93a");
pub fn development_genesis_config() -> RuntimeGenesisConfig {
let signatories = vec![SHAWN_PUB_KEY_BYTES.into(), ANDREW_PUB_KEY_BYTES.into()];
// The inherents are computed using the appropriate method, and placed before the extrinsics.
let mut genesis_transactions = OuterConstraintCheckerInherentHooks::genesis_transactions();
genesis_transactions.extend([
// Money Transactions
Coin::<0>::mint(100, Sr25519Signature::new(SHAWN_PUB_KEY_BYTES)),
Coin::<0>::mint(100, ThresholdMultiSignature::new(1, signatories)),
// Kitty Transactions
KittyData::mint(Parent::mom(), b"mother", UpForGrabs),
KittyData::mint(Parent::dad(), b"father", UpForGrabs),
// TODO: Initial Transactions for Existence
]);
RuntimeGenesisConfig::new(
WASM_BINARY
.expect("Runtime WASM binary must exist.")
.to_vec(),
genesis_transactions,
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::OuterVerifier;
use parity_scale_codec::{Decode, Encode};
use sp_api::HashT;
use sp_core::testing::SR25519;
use sp_keystore::{testing::MemoryKeystore, Keystore, KeystoreExt};
use sp_runtime::{traits::BlakeTwo256, BuildStorage};
use std::sync::Arc;
use tuxedo_core::{
dynamic_typing::{DynamicallyTypedData, UtxoData},
inherents::InherentInternal,
types::{Output, OutputRef},
};
// other random account generated with subkey
const SHAWN_PHRASE: &str =
"news slush supreme milk chapter athlete soap sausage put clutch what kitten";
const ANDREW_PHRASE: &str =
"monkey happy total rib lumber scrap guide photo country online rose diet";
fn default_runtime_genesis_config() -> RuntimeGenesisConfig {
let keystore = MemoryKeystore::new();
let shawn_pub_key_bytes = keystore
.sr25519_generate_new(SR25519, Some(SHAWN_PHRASE))
.unwrap()
.0;
let andrew_pub_key_bytes = keystore
.sr25519_generate_new(SR25519, Some(ANDREW_PHRASE))
.unwrap()
.0;
let signatories = vec![shawn_pub_key_bytes.into(), andrew_pub_key_bytes.into()];
let mut genesis_transactions = OuterConstraintCheckerInherentHooks::genesis_transactions();
genesis_transactions.extend([
// Money Transactions
Coin::<0>::mint(100, Sr25519Signature::new(shawn_pub_key_bytes)),
Coin::<0>::mint(100, ThresholdMultiSignature::new(1, signatories)),
]);
RuntimeGenesisConfig::new(
WASM_BINARY
.expect("Runtime WASM binary must exist.")
.to_vec(),
genesis_transactions,
)
}
fn new_test_ext() -> sp_io::TestExternalities {
let keystore = MemoryKeystore::new();
let storage = default_runtime_genesis_config()
.build_storage()
.expect("System builds valid default genesis config");
let mut ext = sp_io::TestExternalities::from(storage);
ext.register_extension(KeystoreExt(Arc::new(keystore)));
ext
}
#[test]
fn genesis_utxo_money() {
new_test_ext().execute_with(|| {
let keystore = MemoryKeystore::new();
let shawn_pub_key = keystore
.sr25519_generate_new(SR25519, Some(SHAWN_PHRASE))
.unwrap();
// Grab genesis value from storage and assert it is correct
let genesis_utxo = Output {
verifier: OuterVerifier::Sr25519Signature(Sr25519Signature {
owner_pubkey: shawn_pub_key.into(),
}),
payload: DynamicallyTypedData {
data: 100u128.encode(),
type_id: <money::Coin<0> as UtxoData>::TYPE_ID,
},
};
let inherents_len = OuterConstraintCheckerInherentHooks::genesis_transactions().len();
let tx = default_runtime_genesis_config()
.get_transaction(inherents_len)
.unwrap()
.clone();
assert_eq!(tx.outputs.get(0), Some(&genesis_utxo));
let tx_hash = BlakeTwo256::hash_of(&tx.encode());
let output_ref = OutputRef {
tx_hash,
index: 0_u32,
};
let encoded_utxo =
sp_io::storage::get(&output_ref.encode()).expect("Retrieve Genesis UTXO");
let utxo = Output::decode(&mut &encoded_utxo[..]).expect("Can Decode UTXO correctly");
assert_eq!(utxo, genesis_utxo);
})
}
#[test]
fn genesis_utxo_money_multi_sig() {
new_test_ext().execute_with(|| {
let keystore = MemoryKeystore::new();
let shawn_pub_key = keystore
.sr25519_generate_new(SR25519, Some(SHAWN_PHRASE))
.unwrap();
let andrew_pub_key = keystore
.sr25519_generate_new(SR25519, Some(ANDREW_PHRASE))
.unwrap();
let genesis_multi_sig_utxo = Output {
verifier: OuterVerifier::ThresholdMultiSignature(ThresholdMultiSignature {
threshold: 1,
signatories: vec![shawn_pub_key.into(), andrew_pub_key.into()],
}),
payload: DynamicallyTypedData {
data: 100u128.encode(),
type_id: <money::Coin<0> as UtxoData>::TYPE_ID,
},
};
let inherents_len = OuterConstraintCheckerInherentHooks::genesis_transactions().len();
let tx = default_runtime_genesis_config()
.get_transaction(1 + inherents_len)
.unwrap()
.clone();
assert_eq!(tx.outputs.get(0), Some(&genesis_multi_sig_utxo));
let tx_hash = BlakeTwo256::hash_of(&tx.encode());
let output_ref = OutputRef {
tx_hash,
index: 0_u32,
};
let encoded_utxo =
sp_io::storage::get(&output_ref.encode()).expect("Retrieve Genesis MultiSig UTXO");
let utxo = Output::decode(&mut &encoded_utxo[..]).expect("Can Decode UTXO correctly");
assert_eq!(utxo, genesis_multi_sig_utxo);
})
}
}