Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Fix formulate reorg and event workers (#374)
Browse files Browse the repository at this point in the history
* Test multiple chunks in formulate reorg

* Fix broken StorageLock...

The StorageLocks all have a default deadline, they don't just remain locked.
For some reason (yet to debug), the worker calls are taking a long time / timing out, and due to the broken lock additional worker processes are piling up.

* Dont give up formulating a reorg, even if we are missing local data

* Back to StorageLock with increased deadline

Each WASM worker runs in its own thread and does not have shared memory, so the previous fix only worked for native.

Setting to 120s deadline should work most of the time, and when it doesn't it's not the end of the world to ignore a few irrelevant blocks, just inefficient if it happens too often.
The other side effect is that the storage lock persists across restarts, which is the downside of making it too long.

* Rewrite formulate reorg to process forward/reverse in lockstep

Also fix the handling for traversing before the first block, but err out if the worker is otherwise missing data.

* Bump version
  • Loading branch information
jflatow committed Jun 22, 2021
1 parent 46eebcc commit c321984
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 285 deletions.
2 changes: 1 addition & 1 deletion chains/build_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!env node
#!/usr/bin/env node
const child_process = require('child_process');
const fs = require('fs').promises;
const path = require('path');
Expand Down
63 changes: 32 additions & 31 deletions chains/testnet/chain-spec-raw.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion chains/testnet/chain-spec.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions ethereum-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hex_buffer_serde::{ConstHex, ConstHexForm};
use sp_runtime::offchain::{http, Duration};
use sp_runtime_interface::pass_by::PassByCodec;

use our_std::{debug, error, info, warn, Deserialize, RuntimeDebug, Serialize};
use our_std::{error, info, trace, warn, Deserialize, RuntimeDebug, Serialize};
use types_derive::{type_alias, Types};

pub mod events;
Expand Down Expand Up @@ -182,7 +182,7 @@ pub fn send_rpc(
"id":1
})
.to_string();
debug!("RPC: {}", &data);
trace!("RPC: {}", &data);

let request = http::Request::post(server, vec![data]);

Expand All @@ -209,7 +209,7 @@ pub fn send_rpc(
warn!("No UTF8 body");
EthereumClientError::InvalidUTF8
})?;
debug!("RPC Response: {}", body_str.clone());
trace!("RPC Response: {}", body_str.clone());

Ok(String::from(body_str))
}
Expand All @@ -221,13 +221,13 @@ pub fn get_block(
) -> Result<EthereumBlock, EthereumClientError> {
let block_str = encode_block_hex(block_num);
let block_obj = get_block_object(server, &block_str)?;
debug!("eth_starport_address: {:X?}", &eth_starport_address[..]);
trace!("eth_starport_address: {:X?}", &eth_starport_address[..]);
let get_logs_params = vec![serde_json::json!({
"address": format!("0x{}", ::hex::encode(&eth_starport_address[..])),
"fromBlock": &block_str,
"toBlock": &block_str,
})];
debug!("get_logs_params: {:?}", get_logs_params.clone());
trace!("get_logs_params: {:?}", get_logs_params.clone());
let get_logs_response_str: String = send_rpc(server, "eth_getLogs".into(), get_logs_params)?;
let get_logs_response = deserialize_get_logs_response(&get_logs_response_str)?;
let event_objects = get_logs_response
Expand Down Expand Up @@ -282,7 +282,7 @@ pub fn get_block_object(server: &str, block_num: &str) -> Result<BlockObject, Et
pub fn get_latest_block_number(server: &str) -> Result<u64, EthereumClientError> {
let response_str: String = send_rpc(server, "eth_blockNumber".into(), vec![])?;
let response = deserialize_block_number_response(&response_str)?;
debug!("eth_blockNumber response: {:?}", response.result.clone());
trace!("eth_blockNumber response: {:?}", response.result.clone());
parse_u64(Some(response.result.ok_or(EthereumClientError::NoResult)?))
.ok_or(EthereumClientError::JsonParseError)
}
Expand Down
1 change: 0 additions & 1 deletion our-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub use sp_std::rc;
pub use sp_std::result;
pub use sp_std::slice;
pub use sp_std::str;
pub use sp_std::sync;
pub use sp_std::vec;

pub mod collections {
Expand Down
42 changes: 2 additions & 40 deletions our-std/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,44 @@ pub use log;
// std or no_std environment. Just use log!("My Log: {}", 5); and
// things should just magically work.

#[cfg(feature = "std")]
#[macro_export]
macro_rules! log {
($($arg:tt)*) => {{
$crate::log::log::info!($($arg)*);
}}
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! log {
($($arg:tt)*) => {{
sp_runtime::print($crate::alloc::format!($($arg)*).as_str());
}}
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {{
$crate::log::log::debug!($($arg)*);
}}
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! debug {
macro_rules! trace {
($($arg:tt)*) => {{
$crate::log!($($arg)*);
$crate::log::log::trace!($($arg)*);
}}
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {{
$crate::log::log::info!($($arg)*);
}}
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {{
$crate::log!($($arg)*);
}}
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {{
$crate::log::log::warn!($($arg)*);
}}
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {{
$crate::log!($($arg)*);
}}
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {{
$crate::log::log::error!($($arg)*);
}}
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {{
$crate::log!($($arg)*);
}}
}
1 change: 1 addition & 0 deletions our-std/with_std.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub use serde::{Deserialize, Serialize};
pub use std::fmt::Debug as Debuggable;
pub use std::sync;
pub use Debug as RuntimeDebug;
1 change: 1 addition & 0 deletions our-std/without_std.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use core::fmt::Debug as Debuggable;
pub use core::sync;
pub use serde::{Deserialize, Serialize};
#[cfg(not(feature = "runtime-debug"))]
pub use sp_runtime::RuntimeDebug;
Expand Down
13 changes: 9 additions & 4 deletions pallets/cash/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::{
SignersSet, Timestamp, ValidatorKeys,
},
AssetBalances, AssetsWithNonZeroBalance, CashIndex, CashPrincipals, CashYield, Config, Event,
GlobalCashIndex, IngressionQueue, LastProcessedBlock, Pallet, Starports, SupportedAssets,
TotalBorrowAssets, TotalCashPrincipal, TotalSupplyAssets, Validators,
FirstBlock, GlobalCashIndex, IngressionQueue, LastProcessedBlock, Pallet, Starports,
SupportedAssets, TotalBorrowAssets, TotalCashPrincipal, TotalSupplyAssets, Validators,
};

use codec::Decode;
Expand Down Expand Up @@ -71,9 +71,14 @@ pub fn get_event_queue<T: Config>(chain_id: ChainId) -> Result<ChainBlockEvents,
Ok(IngressionQueue::get(chain_id).unwrap_or(ChainBlockEvents::empty(chain_id)?))
}

/// Return the last processed block for the underlying chain, or the initial one for the starport.
/// Return the last processed block for the underlying chain.
pub fn get_first_block<T: Config>(chain_id: ChainId) -> Result<ChainBlock, Reason> {
FirstBlock::get(chain_id).ok_or(Reason::MissingBlock)
}

/// Return the last processed block for the underlying chain.
pub fn get_last_block<T: Config>(chain_id: ChainId) -> Result<ChainBlock, Reason> {
LastProcessedBlock::get(chain_id).ok_or(Reason::MissingLastBlock)
LastProcessedBlock::get(chain_id).ok_or(Reason::MissingBlock)
}

/// Return the current total borrow and total supply balances for the asset.
Expand Down
12 changes: 2 additions & 10 deletions pallets/cash/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,9 @@ fn fetch_eth_blocks(
mod tests {
use crate::events::*;
use crate::tests::*;
use sp_core::offchain::testing;
use sp_core::offchain::{OffchainDbExt, OffchainWorkerExt};

#[test]
fn test_fetch_chain_blocks_eth_returns_proper_blocks() -> Result<(), Reason> {
// XXX should this use new_test_ext_with_http_calls?
let blocks_to_return = vec![
ethereum_client::EthereumBlock {
hash: [1u8; 32],
Expand All @@ -116,13 +113,8 @@ mod tests {
let fetch_to = blocks_to_return[blocks_to_return.len() - 1].number + 1;
const STARPORT_ADDR: [u8; 20] = [1; 20];

let (offchain, offchain_state) = testing::TestOffchainExt::new();
let mut t = sp_io::TestExternalities::default();

t.register_extension(OffchainDbExt::new(offchain.clone()));
t.register_extension(OffchainWorkerExt::new(offchain));

gen_mock_responses(offchain_state, blocks_to_return.clone(), STARPORT_ADDR);
let calls = gen_mock_calls(&blocks_to_return, STARPORT_ADDR);
let (mut t, _, _) = new_test_ext_with_http_calls(calls);

t.execute_with(|| {
let fetched_blocks = fetch_eth_blocks(fetch_from, fetch_to, &STARPORT_ADDR).unwrap();
Expand Down
Loading

0 comments on commit c321984

Please sign in to comment.