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 a36d11f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
28 changes: 19 additions & 9 deletions frame/chainrelay/eth/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ decl_module! {
) -> dispatch::DispatchResult {
let offchain_worker = ensure_none(origin)?;

debug::info!("[eth-offchain] try to update block: {}", eth_header.number);
T::EthRelay::verify_header(&eth_header)?;
T::EthRelay::maybe_store_header(&eth_header)?;
debug::info!("[eth-offchain] updated height: {}", eth_header.number);

// TODO: brode case eth-relay event
Self::deposit_event(RawEvent::OffchainRelayHeader(None));
Expand All @@ -101,7 +103,14 @@ decl_module! {
}

fn remove_trascation_and_uncle(r: &mut Vec<u8>) {
let mut tail = r.split_off(1362);
let mut pr = 1330;
for i in 1330..1394 {
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 +137,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,24 +151,24 @@ 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 => {
if r.len() < 1362 {
debug::warn!("[eth-offchain][warn] response: {:?}", core::str::from_utf8(&r));
debug::warn!("[eth-offchain] response: {:?}", core::str::from_utf8(&r));
return Err("unexpected api response");
}
remove_trascation_and_uncle(&mut r);

// 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 Expand Up @@ -270,7 +280,7 @@ impl<T: Trait> Module<T> {
.parse::<u64>()
.map_err(|_| "fetch current block height error: parsing to u64 error")?;

debug::info!("[eth-offchain] current block height: {:?}", current_block_height);
debug::trace!("[eth-offchain] current block height: {:?}", current_block_height);

// TODO: check current header and pass this run

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 a36d11f

Please sign in to comment.