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
use crate::requests::RequestStatus;
use codec::Decode;
use codec::Encode;
use frame_support::dispatch::GetStorageVersion;
use frame_support::sp_runtime::legacy::byte_sized_error::DispatchError as OldDispatchError;
use frame_support::sp_runtime::DispatchError;
use frame_support::sp_runtime::ModuleError;
use frame_support::traits::StorageVersion;
use frame_support::RuntimeDebug;
use crate::Config;
use crate::Pallet;
use crate::RequestStatuses;
#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug, scale_info::TypeInfo)]
pub enum OldRequestStatus {
Pending,
Frozen,
ApprovalsReady,
Failed(OldDispatchError),
Done,
Broken(OldDispatchError, OldDispatchError),
}
pub fn migrate<T: Config>() {
if Pallet::<T>::on_chain_storage_version() < StorageVersion::new(2) {
RequestStatuses::<T>::translate::<OldRequestStatus, _>(|_, _, status| {
let status = match status {
OldRequestStatus::Pending => RequestStatus::Pending,
OldRequestStatus::Frozen => RequestStatus::Frozen,
OldRequestStatus::ApprovalsReady => RequestStatus::ApprovalsReady,
OldRequestStatus::Failed(err) => RequestStatus::Failed(migrate_error(err)),
OldRequestStatus::Done => RequestStatus::Done,
OldRequestStatus::Broken(err1, err2) => {
RequestStatus::Broken(migrate_error(err1), migrate_error(err2))
}
};
Some(status)
});
StorageVersion::new(2).put::<Pallet<T>>()
}
}
pub fn migrate_error(err: OldDispatchError) -> DispatchError {
match err {
OldDispatchError::Other(s) => DispatchError::Other(s),
OldDispatchError::CannotLookup => DispatchError::CannotLookup,
OldDispatchError::BadOrigin => DispatchError::BadOrigin,
OldDispatchError::Module(err) => DispatchError::Module(ModuleError {
index: err.index,
error: [err.error, 0, 0, 0],
message: err.message,
}),
OldDispatchError::ConsumerRemaining => DispatchError::ConsumerRemaining,
OldDispatchError::NoProviders => DispatchError::NoProviders,
OldDispatchError::TooManyConsumers => DispatchError::TooManyConsumers,
OldDispatchError::Token(err) => DispatchError::Token(err),
OldDispatchError::Arithmetic(err) => DispatchError::Arithmetic(err),
}
}
#[cfg(test)]
mod tests {
use crate::migration::OldRequestStatus;
use crate::requests::RequestStatus;
use crate::tests::mock::ExtBuilder;
use crate::tests::mock::Runtime;
use crate::Pallet;
use crate::RequestStatuses;
use ethereum_types::H256;
use frame_support::sp_runtime::legacy::byte_sized_error::DispatchError as OldDispatchError;
use frame_support::sp_runtime::legacy::byte_sized_error::ModuleError as OldModuleError;
use frame_support::sp_runtime::DispatchError;
use frame_support::sp_runtime::ModuleError;
use frame_support::traits::GetStorageVersion;
#[test]
fn request_statuses_migration_works() {
let (mut ext, _state) = ExtBuilder::default().build();
ext.execute_with(|| {
assert_eq!(Pallet::<Runtime>::on_chain_storage_version(), 0);
let key = RequestStatuses::<Runtime>::hashed_key_for(0, H256::from_low_u64_be(1));
frame_support::storage::unhashed::put(&key, &OldRequestStatus::Done);
let key = RequestStatuses::<Runtime>::hashed_key_for(0, H256::from_low_u64_be(2));
frame_support::storage::unhashed::put(&key, &OldRequestStatus::ApprovalsReady);
let key = RequestStatuses::<Runtime>::hashed_key_for(0, H256::from_low_u64_be(3));
frame_support::storage::unhashed::put(
&key,
&OldRequestStatus::Failed(OldDispatchError::Module(OldModuleError {
index: 1,
error: 2,
message: Some("test"),
})),
);
let key = RequestStatuses::<Runtime>::hashed_key_for(0, H256::from_low_u64_be(4));
frame_support::storage::unhashed::put(
&key,
&OldRequestStatus::Broken(
OldDispatchError::Module(OldModuleError {
index: 3,
error: 4,
message: Some("test2"),
}),
OldDispatchError::Module(OldModuleError {
index: 5,
error: 6,
message: Some("test3"),
}),
),
);
super::migrate::<Runtime>();
assert_eq!(Pallet::<Runtime>::on_chain_storage_version(), 2);
assert_eq!(
RequestStatuses::<Runtime>::get(0, H256::from_low_u64_be(1)),
Some(RequestStatus::Done)
);
assert_eq!(
RequestStatuses::<Runtime>::get(0, H256::from_low_u64_be(2)),
Some(RequestStatus::ApprovalsReady)
);
assert_eq!(
RequestStatuses::<Runtime>::get(0, H256::from_low_u64_be(3)),
Some(RequestStatus::Failed(DispatchError::Module(ModuleError {
index: 1,
error: [2, 0, 0, 0],
message: Some("test"),
})))
);
assert_eq!(
RequestStatuses::<Runtime>::get(0, H256::from_low_u64_be(4)),
Some(RequestStatus::Broken(
DispatchError::Module(ModuleError {
index: 3,
error: [4, 0, 0, 0],
message: Some("test2"),
}),
DispatchError::Module(ModuleError {
index: 5,
error: [6, 0, 0, 0],
message: Some("test3"),
}),
)),
);
});
}
}