@@ -59,37 +59,35 @@ pub fn create_distance_inherent_data_provider<B, C, Backend>(
59
59
parent : B :: Hash ,
60
60
distance_dir : PathBuf ,
61
61
owner_keys : & [ sp_core:: sr25519:: Public ] ,
62
- ) -> Result < sp_distance:: InherentDataProvider < IdtyIndex > , sc_client_api :: blockchain :: Error >
62
+ ) -> sp_distance:: InherentDataProvider < IdtyIndex >
63
63
where
64
64
B : BlockT ,
65
65
C : ProvideUncles < B > + StorageProvider < B , Backend > ,
66
66
Backend : sc_client_api:: Backend < B > ,
67
67
IdtyIndex : Decode + Encode + PartialEq + TypeInfo ,
68
68
{
69
- // Retrieve the period_index from storage. If storage is inaccessible or the data is corrupted,
70
- // return the appropriate error.
69
+ // Retrieve the period_index from storage.
71
70
let period_index = client
72
71
. storage (
73
72
parent,
74
73
& StorageKey (
75
74
frame_support:: storage:: storage_prefix ( b"Distance" , b"CurrentPeriodIndex" ) . to_vec ( ) ,
76
75
) ,
77
- ) ?
78
- . map_or_else (
79
- || {
80
- Err ( sc_client_api:: blockchain:: Error :: Storage (
81
- "CurrentPeriodIndex value not found" . to_string ( ) ,
82
- ) )
83
- } ,
84
- |raw| {
85
- u32:: decode ( & mut & raw . 0 [ ..] )
86
- . map_err ( |e| sc_client_api:: blockchain:: Error :: from_state ( Box :: new ( e) ) )
87
- } ,
88
- ) ?;
76
+ )
77
+ . ok ( )
78
+ . flatten ( )
79
+ . and_then ( |raw| u32:: decode ( & mut & raw . 0 [ ..] ) . ok ( ) ) ;
80
+
81
+ // Return early if the storage is inaccessible or the data is corrupted.
82
+ let period_index = match period_index {
83
+ Some ( index) => index,
84
+ None => {
85
+ log:: error!( "🧙 [distance inherent] PeriodIndex decoding failed." ) ;
86
+ return sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( None ) ;
87
+ }
88
+ } ;
89
89
90
90
// Retrieve the published_results from storage.
91
- // Return an error if the storage is inaccessible.
92
- // If accessible, continue execution. If None, it means there are no published_results at this block.
93
91
let published_results = client
94
92
. storage (
95
93
parent,
@@ -105,47 +103,61 @@ where
105
103
)
106
104
. to_vec ( ) ,
107
105
) ,
108
- ) ?
106
+ )
107
+ . ok ( )
108
+ . flatten ( )
109
109
. and_then ( |raw| {
110
110
pallet_distance:: EvaluationPool :: < AccountId32 , IdtyIndex > :: decode ( & mut & raw . 0 [ ..] ) . ok ( )
111
111
} ) ;
112
112
113
- // Have we already published a result for this period?
114
- if let Some ( results) = published_results {
115
- // Find the account associated with the BABE key that is in our owner keys.
116
- let mut local_account = None ;
117
- for key in owner_keys {
118
- // Session::KeyOwner is StorageMap<_, Twox64Concat, (KeyTypeId, Vec<u8>), AccountId32, OptionQuery>
119
- // Slices (variable length) and array (fixed length) are encoded differently, so the `.as_slice()` is needed
120
- let item_key = ( sp_runtime:: KeyTypeId ( * b"babe" ) , key. 0 . as_slice ( ) ) . encode ( ) ;
121
- let mut storage_key =
122
- frame_support:: storage:: storage_prefix ( b"Session" , b"KeyOwner" ) . to_vec ( ) ;
123
- storage_key. extend_from_slice ( & sp_core:: twox_64 ( & item_key) ) ;
124
- storage_key. extend_from_slice ( & item_key) ;
113
+ // Return early if the storage is inaccessible or the data is corrupted.
114
+ let published_results = match published_results {
115
+ Some ( published_results) => published_results,
116
+ None => {
117
+ log:: info!( "🧙 [distance inherent] No published result at this block." ) ;
118
+ return sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( None ) ;
119
+ }
120
+ } ;
125
121
126
- if let Some ( raw_data) = client. storage ( parent, & StorageKey ( storage_key) ) ? {
127
- if let Ok ( key_owner) = AccountId32 :: decode ( & mut & raw_data. 0 [ ..] ) {
128
- local_account = Some ( key_owner) ;
129
- break ;
130
- } else {
131
- log:: warn!( "🧙 [distance oracle] Cannot decode key owner value" ) ;
132
- }
122
+ // Find the account associated with the BABE key that is in our owner keys.
123
+ let mut local_account = None ;
124
+ for key in owner_keys {
125
+ // Session::KeyOwner is StorageMap<_, Twox64Concat, (KeyTypeId, Vec<u8>), AccountId32, OptionQuery>
126
+ // Slices (variable length) and array (fixed length) are encoded differently, so the `.as_slice()` is needed
127
+ let item_key = ( sp_runtime:: KeyTypeId ( * b"babe" ) , key. 0 . as_slice ( ) ) . encode ( ) ;
128
+ let mut storage_key =
129
+ frame_support:: storage:: storage_prefix ( b"Session" , b"KeyOwner" ) . to_vec ( ) ;
130
+ storage_key. extend_from_slice ( & sp_core:: twox_64 ( & item_key) ) ;
131
+ storage_key. extend_from_slice ( & item_key) ;
132
+
133
+ if let Some ( raw_data) = client
134
+ . storage ( parent, & StorageKey ( storage_key) )
135
+ . ok ( )
136
+ . flatten ( )
137
+ {
138
+ if let Ok ( key_owner) = AccountId32 :: decode ( & mut & raw_data. 0 [ ..] ) {
139
+ local_account = Some ( key_owner) ;
140
+ break ;
141
+ } else {
142
+ log:: warn!( "🧙 [distance inherent] Cannot decode key owner value" ) ;
133
143
}
134
144
}
135
- if let Some ( local_account) = local_account {
136
- if results. evaluators . contains ( & local_account) {
137
- log:: debug!( "🧙 [distance oracle] Already published a result for this period" ) ;
138
- return Ok ( sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( None ) ) ;
139
- }
140
- } else {
141
- log:: error!( "🧙 [distance oracle] Cannot find our BABE owner key" ) ;
142
- return Ok ( sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( None ) ) ;
145
+ }
146
+
147
+ // Have we already published a result for this period?
148
+ if let Some ( local_account) = local_account {
149
+ if published_results. evaluators . contains ( & local_account) {
150
+ log:: debug!( "🧙 [distance inherent] Already published a result for this period" ) ;
151
+ return sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( None ) ;
143
152
}
153
+ } else {
154
+ log:: error!( "🧙 [distance inherent] Cannot find our BABE owner key" ) ;
155
+ return sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( None ) ;
144
156
}
145
157
146
158
// Read evaluation result from file, if it exists
147
159
log:: debug!(
148
- "🧙 [distance oracle ] Reading evaluation result from file {:?}" ,
160
+ "🧙 [distance inherent ] Reading evaluation result from file {:?}" ,
149
161
distance_dir. clone( ) . join( period_index. to_string( ) )
150
162
) ;
151
163
let evaluation_result = match std:: fs:: read (
@@ -155,20 +167,20 @@ where
155
167
Err ( e) => {
156
168
match e. kind ( ) {
157
169
std:: io:: ErrorKind :: NotFound => {
158
- log:: debug!( "🧙 [distance oracle ] Evaluation result file not found. Please ensure that the oracle version matches {}" , VERSION_PREFIX ) ;
170
+ log:: debug!( "🧙 [distance inherent ] Evaluation result file not found. Please ensure that the oracle version matches {}" , VERSION_PREFIX ) ;
159
171
}
160
172
_ => {
161
173
log:: error!(
162
- "🧙 [distance oracle ] Cannot read distance evaluation result file: {e:?}"
174
+ "🧙 [distance inherent ] Cannot read distance evaluation result file: {e:?}"
163
175
) ;
164
176
}
165
177
}
166
- return Ok ( sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( None ) ) ;
178
+ return sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( None ) ;
167
179
}
168
180
} ;
169
181
170
- log:: info!( "🧙 [distance oracle ] Providing evaluation result" ) ;
171
- Ok ( sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( Some (
182
+ log:: info!( "🧙 [distance inherent ] Providing evaluation result" ) ;
183
+ sp_distance:: InherentDataProvider :: < IdtyIndex > :: new ( Some (
172
184
sp_distance:: ComputationResult :: decode ( & mut evaluation_result. as_slice ( ) ) . unwrap ( ) ,
173
- ) ) )
185
+ ) )
174
186
}
0 commit comments