Read blockchain sync step and max reorg depth from config
This commit is contained in:
parent
672ff5f0ac
commit
3a28219405
7 changed files with 36 additions and 21 deletions
|
@ -19,6 +19,8 @@ blockchain:
|
||||||
api_url: 'http://127.0.0.1:8546'
|
api_url: 'http://127.0.0.1:8546'
|
||||||
explorer_url: null
|
explorer_url: null
|
||||||
signing_key: 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
|
signing_key: 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
|
||||||
|
chain_sync_step: 100
|
||||||
|
chain_reorg_max_depth: 0
|
||||||
|
|
||||||
ipfs_api_url: 'http://127.0.0.1:5001'
|
ipfs_api_url: 'http://127.0.0.1:5001'
|
||||||
ipfs_gateway_url: 'http://127.0.0.1:8001'
|
ipfs_gateway_url: 'http://127.0.0.1:8001'
|
||||||
|
|
|
@ -36,6 +36,8 @@ registrations_open: false
|
||||||
# api_url: 'http://127.0.0.1:8545'
|
# api_url: 'http://127.0.0.1:8545'
|
||||||
# explorer_url: null
|
# explorer_url: null
|
||||||
# signing_key: null
|
# signing_key: null
|
||||||
|
# chain_sync_step: 1000
|
||||||
|
# chain_reorg_max_depth: 10
|
||||||
|
|
||||||
# IPFS integration
|
# IPFS integration
|
||||||
#ipfs_api_url: 'http://127.0.0.1:5001'
|
#ipfs_api_url: 'http://127.0.0.1:5001'
|
||||||
|
|
|
@ -74,11 +74,9 @@ fn parse_env() -> EnvConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_log_level() -> LogLevel { LogLevel::Info }
|
fn default_chain_sync_step() -> u64 { 1000 }
|
||||||
|
|
||||||
fn default_login_message() -> String { "Do not sign this message on other sites!".to_string() }
|
fn default_chain_reorg_max_depth() -> u64 { 10 }
|
||||||
|
|
||||||
fn default_post_character_limit() -> usize { 2000 }
|
|
||||||
|
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct BlockchainConfig {
|
pub struct BlockchainConfig {
|
||||||
|
@ -93,6 +91,11 @@ pub struct BlockchainConfig {
|
||||||
pub explorer_url: Option<String>,
|
pub explorer_url: Option<String>,
|
||||||
// Instance private key
|
// Instance private key
|
||||||
pub signing_key: String,
|
pub signing_key: String,
|
||||||
|
|
||||||
|
#[serde(default = "default_chain_sync_step")]
|
||||||
|
pub chain_sync_step: u64,
|
||||||
|
#[serde(default = "default_chain_reorg_max_depth")]
|
||||||
|
pub chain_reorg_max_depth: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlockchainConfig {
|
impl BlockchainConfig {
|
||||||
|
@ -105,6 +108,12 @@ impl BlockchainConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_log_level() -> LogLevel { LogLevel::Info }
|
||||||
|
|
||||||
|
fn default_login_message() -> String { "Do not sign this message on other sites!".to_string() }
|
||||||
|
|
||||||
|
fn default_post_character_limit() -> usize { 2000 }
|
||||||
|
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
|
|
|
@ -175,10 +175,11 @@ pub async fn get_contracts(
|
||||||
};
|
};
|
||||||
|
|
||||||
let current_block = get_current_block_number(&web3, storage_dir).await?;
|
let current_block = get_current_block_number(&web3, storage_dir).await?;
|
||||||
log::info!("current block is {}", current_block);
|
|
||||||
let sync_state = SyncState::new(
|
let sync_state = SyncState::new(
|
||||||
current_block,
|
current_block,
|
||||||
sync_targets,
|
sync_targets,
|
||||||
|
config.chain_sync_step,
|
||||||
|
config.chain_reorg_max_depth,
|
||||||
storage_dir,
|
storage_dir,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ use crate::models::posts::queries::{
|
||||||
};
|
};
|
||||||
use super::errors::EthereumError;
|
use super::errors::EthereumError;
|
||||||
use super::signatures::{sign_contract_call, CallArgs, SignatureData};
|
use super::signatures::{sign_contract_call, CallArgs, SignatureData};
|
||||||
use super::sync::{save_current_block_number, SyncState};
|
use super::sync::SyncState;
|
||||||
use super::utils::parse_address;
|
use super::utils::parse_address;
|
||||||
|
|
||||||
const TOKEN_WAIT_TIME: i64 = 10; // in minutes
|
const TOKEN_WAIT_TIME: i64 = 10; // in minutes
|
||||||
|
@ -123,9 +123,7 @@ pub async fn process_nft_events(
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
if sync_state.update(&contract.address(), to_block) {
|
sync_state.update(&contract.address(), to_block)?;
|
||||||
save_current_block_number(&sync_state.storage_dir, sync_state.current_block)?;
|
|
||||||
};
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ use super::signatures::{
|
||||||
CallArgs,
|
CallArgs,
|
||||||
SignatureData,
|
SignatureData,
|
||||||
};
|
};
|
||||||
use super::sync::{save_current_block_number, SyncState};
|
use super::sync::SyncState;
|
||||||
use super::utils::{address_to_string, parse_address};
|
use super::utils::{address_to_string, parse_address};
|
||||||
|
|
||||||
const ETHEREUM: Currency = Currency::Ethereum;
|
const ETHEREUM: Currency = Currency::Ethereum;
|
||||||
|
@ -240,9 +240,7 @@ pub async fn check_subscriptions(
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
if sync_state.update(&contract.address(), to_block) {
|
sync_state.update(&contract.address(), to_block)?;
|
||||||
save_current_block_number(&sync_state.storage_dir, sync_state.current_block)?;
|
|
||||||
};
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,6 @@ use crate::utils::files::write_file;
|
||||||
use super::errors::EthereumError;
|
use super::errors::EthereumError;
|
||||||
|
|
||||||
const BLOCK_NUMBER_FILE_NAME: &str = "current_block";
|
const BLOCK_NUMBER_FILE_NAME: &str = "current_block";
|
||||||
const CHAIN_REORG_MAX_DEPTH: u64 = 100;
|
|
||||||
const CHAIN_SYNC_STEP: u64 = 1000;
|
|
||||||
|
|
||||||
pub fn save_current_block_number(
|
pub fn save_current_block_number(
|
||||||
storage_dir: &Path,
|
storage_dir: &Path,
|
||||||
|
@ -56,16 +54,21 @@ pub async fn get_current_block_number(
|
||||||
pub struct SyncState {
|
pub struct SyncState {
|
||||||
pub current_block: u64,
|
pub current_block: u64,
|
||||||
contracts: HashMap<Address, u64>,
|
contracts: HashMap<Address, u64>,
|
||||||
|
sync_step: u64,
|
||||||
|
reorg_max_depth: u64,
|
||||||
|
|
||||||
pub storage_dir: PathBuf,
|
storage_dir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SyncState {
|
impl SyncState {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
current_block: u64,
|
current_block: u64,
|
||||||
contracts: Vec<Address>,
|
contracts: Vec<Address>,
|
||||||
|
sync_step: u64,
|
||||||
|
reorg_max_depth: u64,
|
||||||
storage_dir: &Path,
|
storage_dir: &Path,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
log::info!("current block is {}", current_block);
|
||||||
let mut contract_map = HashMap::new();
|
let mut contract_map = HashMap::new();
|
||||||
for address in contracts {
|
for address in contracts {
|
||||||
contract_map.insert(address, current_block);
|
contract_map.insert(address, current_block);
|
||||||
|
@ -73,6 +76,8 @@ impl SyncState {
|
||||||
Self {
|
Self {
|
||||||
current_block,
|
current_block,
|
||||||
contracts: contract_map,
|
contracts: contract_map,
|
||||||
|
sync_step,
|
||||||
|
reorg_max_depth,
|
||||||
storage_dir: storage_dir.to_path_buf(),
|
storage_dir: storage_dir.to_path_buf(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,8 +85,8 @@ impl SyncState {
|
||||||
pub fn get_scan_range(&self, contract_address: &Address) -> (u64, u64) {
|
pub fn get_scan_range(&self, contract_address: &Address) -> (u64, u64) {
|
||||||
let current_block = self.contracts[contract_address];
|
let current_block = self.contracts[contract_address];
|
||||||
// Take reorgs into account
|
// Take reorgs into account
|
||||||
let safe_current_block = current_block.saturating_sub(CHAIN_REORG_MAX_DEPTH);
|
let safe_current_block = current_block.saturating_sub(self.reorg_max_depth);
|
||||||
(safe_current_block, safe_current_block + CHAIN_SYNC_STEP)
|
(safe_current_block, safe_current_block + self.sync_step)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_out_of_sync(&self, contract_address: &Address) -> bool {
|
pub fn is_out_of_sync(&self, contract_address: &Address) -> bool {
|
||||||
|
@ -97,15 +102,15 @@ impl SyncState {
|
||||||
&mut self,
|
&mut self,
|
||||||
contract_address: &Address,
|
contract_address: &Address,
|
||||||
block_number: u64,
|
block_number: u64,
|
||||||
) -> bool {
|
) -> Result<(), EthereumError> {
|
||||||
self.contracts.insert(*contract_address, block_number);
|
self.contracts.insert(*contract_address, block_number);
|
||||||
if let Some(min_value) = self.contracts.values().min().copied() {
|
if let Some(min_value) = self.contracts.values().min().copied() {
|
||||||
if min_value > self.current_block {
|
if min_value > self.current_block {
|
||||||
self.current_block = min_value;
|
self.current_block = min_value;
|
||||||
|
save_current_block_number(&self.storage_dir, self.current_block)?;
|
||||||
log::info!("synced to block {}", self.current_block);
|
log::info!("synced to block {}", self.current_block);
|
||||||
return true;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
false
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue