Skip to content

Commit dc1a016

Browse files
authored
Merge branch 'dev' into feature/rust-fmt
2 parents 9a58864 + 956db01 commit dc1a016

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

pallets/offchain-worker/src/lib.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub mod pallet {
131131
/// The on_finalize trigger the query result aggregation.
132132
/// The argument block_number has big impact on the weight.
133133
fn on_finalize(block_number: T::BlockNumber) {
134-
log::info!("ocw on_finalize.{:?}.", block_number);
134+
log::info!("ocw on_finalize {:?}.", block_number);
135135

136136
let query_session_length: usize = T::QuerySessionLength::get() as usize;
137137
let index_in_session = TryInto::<usize>::try_into(block_number)
@@ -150,14 +150,18 @@ pub mod pallet {
150150
/// TODO block N offchain_worker will be called after block N+1 finalize
151151
/// Trigger by offchain framework in each block
152152
fn offchain_worker(block_number: T::BlockNumber) {
153+
log::info!("ocw hook function called on block {:?}.", block_number);
154+
153155
let query_session_length: usize = T::QuerySessionLength::get() as usize;
154156

155157
let index_in_session = TryInto::<usize>::try_into(block_number)
156158
.map_or(query_session_length, |bn| bn % query_session_length);
157159

158160
// Start query at second block of a session
159161
if index_in_session == 1 {
160-
Self::start(block_number);
162+
// TODO make use of the returned value of start,
163+
// and adjust the logics of OCW accordingly
164+
let _ = Self::start(block_number);
161165
}
162166
}
163167
}
@@ -185,6 +189,10 @@ pub mod pallet {
185189
InvalidAccountIndex,
186190
/// Offchain worker index overflow
187191
OffchainWorkerIndexOverflow,
192+
/// Token Server no response
193+
TokenServerNoResponse,
194+
/// Storage retrieval error
195+
InvalidStorageRetrieval,
188196
}
189197

190198
#[pallet::pallet]
@@ -410,25 +418,31 @@ pub mod pallet {
410418
}
411419

412420
// Start new round of offchain worker
413-
fn start(block_number: T::BlockNumber) {
421+
fn start(block_number: T::BlockNumber) -> Result<(), Error<T>> {
414422
let local_token = StorageValueRef::persistent(b"offchain-worker::token");
415423

416424
match local_token.get::<urls::TokenInfo>() {
417425
Ok(Some(token)) => {
426+
log::info!("API keys found! Start to query from sources.");
418427
Self::query(block_number, &token);
428+
Ok(())
419429
},
420-
_ => {
430+
Ok(None) => {
431+
log::info!("No API keys stored! Request keys from local server.");
421432
// Get token from local server
422-
let _ = urls::get_token();
433+
urls::get_token().map_err(|_| Error::<T>::TokenServerNoResponse )
423434
},
424-
};
435+
Err(_) => {
436+
Err(Error::<T>::InvalidStorageRetrieval)
437+
},
438+
}
425439
}
426440

427441
/// Aggregate query result and then record on chain
428442
/// ---------------------
429443
/// Algorithm description as following:
430444
/// 1. collect all query result from `CommitAccountBalance`
431-
/// 2. select the most frequence result as final, then store them on-chain
445+
/// 2. select the most frequent result as final, then store them on-chain
432446
/// 3. store the successful commit according to off-chain worker account
433447
/// 4. reward the off-chain worker based on its correct query and submit
434448
/// 5. update the Eth and BTC balances on-chain

pallets/offchain-worker/src/urls.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,12 @@ pub fn send_get_token() -> Result<Vec<u8>, &'static str> {
250250
}
251251

252252
// Get the API tokens from local server
253-
pub fn get_token() {
254-
match send_get_token() {
255-
Ok(json_result) => match core::str::from_utf8(&json_result) {
256-
Ok(balance) => parse_store_tokens(balance),
257-
Err(_) => {},
258-
},
259-
Err(_) => {},
260-
}
253+
pub fn get_token() -> Result<(), &'static str> {
254+
let json_result = send_get_token()?;
255+
match core::str::from_utf8(&json_result) {
256+
Ok(balance) => parse_store_tokens(balance),
257+
Err(_) => Err("Error occurred while converting from raw bytes to string"),
258+
}
261259
}
262260

263261
#[allow(dead_code)]
@@ -333,15 +331,19 @@ pub fn parse_infura_balances(price_str: &str) -> Option<Vec<u128>> {
333331
}
334332

335333
// Parse the token from local server
336-
pub fn parse_store_tokens(resp_str: &str) {
337-
let token_info: Result<TokenInfo, _> = serde_json::from_str(&resp_str);
338-
339-
match token_info {
340-
Ok(info) => {
341-
let s_info = StorageValueRef::persistent(b"offchain-worker::token");
342-
s_info.set(&info);
343-
log::info!("Token info get from local server is {:?}.", &info);
344-
},
345-
Err(_) => {},
346-
}
334+
pub fn parse_store_tokens(resp_str: &str) -> Result<(), &'static str> {
335+
let token_info: Result<TokenInfo, _> = serde_json::from_str(&resp_str);
336+
337+
match token_info {
338+
Ok(info) => {
339+
let s_info = StorageValueRef::persistent(b"offchain-worker::token");
340+
s_info.set(&info);
341+
log::info!("Token info get from local server is {:?}.", &info);
342+
Ok(())
343+
},
344+
Err(_) => {
345+
log::info!("Error occurred while requesting API keys.");
346+
Err("Error occurred while parsing json string")
347+
},
348+
}
347349
}

0 commit comments

Comments
 (0)