-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime.rs
241 lines (217 loc) · 8.17 KB
/
runtime.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
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
mod extension;
mod pallet_pink;
use crate::types::{AccountId, Balance, BlockNumber, Hash, Hashing, Nonce};
use frame_support::{
parameter_types,
traits::{ConstBool, ConstU32},
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
};
use log::info;
use pallet_contracts::{
migration::{v11, v12, v13, v14, v15, NoopMigration},
weights::SubstrateWeight,
Frame, Migration, Schedule,
};
use sp_runtime::{traits::IdentityLookup, Perbill};
pub use extension::get_side_effects;
pub use pink_capi::types::ExecSideEffects;
type Block = sp_runtime::generic::Block<
sp_runtime::generic::Header<BlockNumber, Hashing>,
frame_system::mocking::MockUncheckedExtrinsic<PinkRuntime>,
>;
pub type SystemEvents = Vec<frame_system::EventRecord<RuntimeEvent, Hash>>;
frame_support::construct_runtime! {
pub struct PinkRuntime {
System: frame_system,
Timestamp: pallet_timestamp,
Balances: pallet_balances,
Randomness: pallet_insecure_randomness_collective_flip,
Contracts: pallet_contracts,
Pink: pallet_pink,
}
}
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
parameter_types! {
pub const BlockHashCount: u32 = 250;
pub RuntimeBlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::with_sensible_defaults(
Weight::from_parts(2u64 * WEIGHT_REF_TIME_PER_SECOND, u64::MAX),
NORMAL_DISPATCH_RATIO,
);
pub const ExistentialDeposit: Balance = 1;
pub const MaxLocks: u32 = 50;
pub const MaxReserves: u32 = 50;
pub const MaxHolds: u32 = 10;
}
impl pallet_pink::Config for PinkRuntime {
type Currency = Balances;
}
impl pallet_balances::Config for PinkRuntime {
type Balance = Balance;
type DustRemoval = ();
type RuntimeEvent = RuntimeEvent;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = frame_system::Pallet<PinkRuntime>;
type WeightInfo = pallet_balances::weights::SubstrateWeight<PinkRuntime>;
type MaxLocks = MaxLocks;
type MaxReserves = MaxReserves;
type ReserveIdentifier = [u8; 8];
type FreezeIdentifier = RuntimeFreezeReason;
type MaxHolds = MaxHolds;
type MaxFreezes = ();
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
}
impl frame_system::Config for PinkRuntime {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = RuntimeBlockWeights;
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Nonce = Nonce;
type Hash = Hash;
type RuntimeCall = RuntimeCall;
type Hashing = Hashing;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
impl pallet_insecure_randomness_collective_flip::Config for PinkRuntime {}
parameter_types! {
pub const MinimumPeriod: u64 = 1;
}
impl pallet_timestamp::Config for PinkRuntime {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}
const MAX_CODE_LEN: u32 = 2 * 1024 * 1024;
parameter_types! {
pub DepositPerStorageByte: Balance = Pink::deposit_per_byte();
pub DepositPerStorageItem: Balance = Pink::deposit_per_item();
pub const DefaultDepositLimit: Balance = Balance::max_value();
pub const MaxCodeLen: u32 = MAX_CODE_LEN;
pub const MaxStorageKeyLen: u32 = 128;
pub const MaxDebugBufferLen: u32 = 128 * 1024;
pub DefaultSchedule: Schedule<PinkRuntime> = {
let mut schedule = Schedule::<PinkRuntime>::default();
const MB: u32 = 16; // 64KiB * 16
// Each concurrent query would create a VM instance to serve it. We couldn't
// allocate too much here.
schedule.limits.memory_pages = 4 * MB;
schedule.instruction_weights.base = 8000;
schedule.limits.runtime_memory = 2048 * 1024 * 1024; // For unittests
schedule.limits.payload_len = 1024 * 1024; // Max size for storage value
schedule
};
pub CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0);
}
impl pallet_contracts::Config for PinkRuntime {
type Time = Timestamp;
type Randomness = Randomness;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type CallFilter = frame_support::traits::Nothing;
type CallStack = [Frame<Self>; 5];
type WeightPrice = Pink;
type WeightInfo = SubstrateWeight<Self>;
type ChainExtension = extension::PinkExtension;
type Schedule = DefaultSchedule;
type DepositPerByte = DepositPerStorageByte;
type DepositPerItem = DepositPerStorageItem;
type DefaultDepositLimit = DefaultDepositLimit;
type AddressGenerator = Pink;
type MaxCodeLen = MaxCodeLen;
type MaxStorageKeyLen = MaxStorageKeyLen;
type UnsafeUnstableInterface = ConstBool<false>;
type MaxDebugBufferLen = MaxDebugBufferLen;
type Migrations = (
// Our on-chain runtime was started from polkadot-v0.9.41 but it already contains
// the changes handled by the v10::Migration. So we just use a NoopMigration here.
NoopMigration<10>,
v11::Migration<Self>,
v12::Migration<Self, Balances>,
v13::Migration<Self>,
v14::Migration<Self, Balances>,
v15::Migration<Self>,
);
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type MaxDelegateDependencies = ConstU32<32>;
type RuntimeHoldReason = RuntimeHoldReason;
type Debug = ();
type Environment = ();
type Xcm = ();
}
#[test]
fn detect_parameter_changes() {
use pallet_contracts::Config;
insta::assert_debug_snapshot!((
<PinkRuntime as frame_system::Config>::BlockWeights::get(),
<PinkRuntime as Config>::Schedule::get(),
<PinkRuntime as Config>::DefaultDepositLimit::get(),
<PinkRuntime as Config>::MaxCodeLen::get(),
<PinkRuntime as Config>::MaxStorageKeyLen::get(),
));
}
/// Call on_genesis for all pallets. This would put the each pallet's storage version into
/// the frame_support::StorageVersion.
pub fn on_genesis() {
<AllPalletsWithSystem as frame_support::traits::OnGenesis>::on_genesis();
}
/// Call on_runtime_upgrade for all pallets
pub fn on_runtime_upgrade() {
use frame_support::traits::OnRuntimeUpgrade;
type Migrations = (Migration<PinkRuntime>, AllPalletsWithSystem);
Migrations::on_runtime_upgrade();
info!("Runtime database migration done");
}
/// Call on_idle for each pallet.
pub fn on_idle(n: BlockNumber) {
<AllPalletsWithSystem as frame_support::traits::OnIdle<BlockNumber>>::on_idle(n, Weight::MAX);
}
#[test]
fn check_metadata() {
let (major, minor, _) = this_crate::version_tuple!();
let filename = format!("assets/metadata-{major}.{minor}.bin");
check_metadata_with_path(&filename).expect("metadata changed");
}
#[cfg(coverage)]
#[test]
fn check_metadata_for_coverage() {
let filename = format!("/tmp/pink-runtime-metadata.bin.cov");
assert!(check_metadata_with_path(&filename).is_err())
}
#[cfg(test)]
fn check_metadata_with_path(path: &str) -> Result<(), String> {
let storage = crate::storage::in_memory_backend::InMemoryStorage::default();
let context = pink_capi::v1::ocall::ExecContext {
block_number: 1,
..Default::default()
};
let metadata: Vec<u8> = storage
.execute_with(&context, || PinkRuntime::metadata().into())
.0;
let old_metadata = std::fs::read(path).unwrap_or_default();
if metadata != old_metadata {
let new_metadata_path = std::env::current_dir().unwrap().join(format!("{path}.new"));
std::fs::write(&new_metadata_path, metadata).unwrap();
return Err(format!(
"Pink runtime metadata changed. The new metadata is stored at \n {:?}",
new_metadata_path.display()
));
}
Ok(())
}