Skip to content

Commit

Permalink
flexible parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Mar 18, 2020
1 parent 695120b commit f284efb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 19 additions & 6 deletions frame/chainrelay/eth/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ decl_module! {
}

fn remove_trascation_and_uncle(r: &mut Vec<u8>) {
let mut tail = r.split_off(1362);
let mut pr = 1350;
for i in 1350..1380 {
if r[i] == 91u8 {
pr = i;
break;
}
}
let mut tail = r.split_off(pr - 16);
if tail[tail.len() - 103 - 68 * 2 - 1 - 1] == 93u8 {
tail = tail.split_off(tail.len() - 103 - 68 * 2 - 1);
tail.split_off(tail.len() - 15 - 68 * 2 - 1)
Expand All @@ -128,6 +135,7 @@ fn json_request(raw_url: &Vec<u8>, api_type: EthScanAPI) -> Result<JsonValue> {
let mut response = pending.wait().map_err(|_| "Error in waiting http response back")?;

let mut retry_time = 0;
let mut r: Vec<u8>;
loop {
if response.code != 200 {
if retry_time == 3 {
Expand All @@ -141,15 +149,16 @@ fn json_request(raw_url: &Vec<u8>, api_type: EthScanAPI) -> Result<JsonValue> {
.map_err(|_| "Error in waiting http response back")?;
retry_time += 1;
debug::info!("[eth-offchain] retry {} times", retry_time);
// TODO: figure out how to sleep in no-std
} else {
break;
r = response.body().collect::<Vec<u8>>();
if r[0] == 123u8 {
break;
}
}
// TODO: figure out how to sleep in no-std here
}

let mut r = response.body().collect::<Vec<u8>>();
// TODO: make this trace
debug::info!("[eth-offchain] response: {:?}", core::str::from_utf8(&r));
debug::trace!("[eth-offchain] response: {:?}", core::str::from_utf8(&r));

let json_val: JsonValue = match api_type {
EthScanAPI::GetBlockByNumber => {
Expand All @@ -159,6 +168,10 @@ fn json_request(raw_url: &Vec<u8>, api_type: EthScanAPI) -> Result<JsonValue> {
}
remove_trascation_and_uncle(&mut r);

debug::info!(
"[eth-offchain] [ant] response: {:?}",
core::str::from_utf8(&r[33..r.len() - 1])
);
// get the result part
simple_json::parse_json(
&core::str::from_utf8(&r[33..r.len() - 1]).map_err(|_| "result part cannot convert to string")?,
Expand Down
7 changes: 7 additions & 0 deletions frame/chainrelay/eth/offchain/src/tests.rs

Large diffs are not rendered by default.

0 comments on commit f284efb

Please sign in to comment.