Add CLI command for updating current block

This commit is contained in:
silverpill 2022-06-20 20:26:43 +00:00
parent 3bad313d01
commit d8972a9286
6 changed files with 40 additions and 5 deletions

7
Cargo.lock generated
View file

@ -289,6 +289,12 @@ dependencies = [
"url 2.2.2",
]
[[package]]
name = "anyhow"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704"
[[package]]
name = "arrayref"
version = "0.3.6"
@ -1590,6 +1596,7 @@ dependencies = [
"actix-web",
"actix-web-httpauth",
"ammonia",
"anyhow",
"base64",
"chrono",
"clap",

View file

@ -19,6 +19,8 @@ actix-web-httpauth = "0.6.0"
actix-rt = "2.7.0"
# Used for HTML sanitization
ammonia = "3.2.0"
# Used for catching errors
anyhow = "1.0.58"
# Used for working with RSA keys, HTTP signatures and file uploads
base64 = "0.13.0"
# Used for working with dates

View file

@ -183,6 +183,12 @@ Generate ethereum address:
mitractl generate-ethereum-address
```
Update blockchain synchronization starting block:
```shell
mitractl update-current-block -n 2000000
```
## License
[AGPL-3.0](./LICENSE)

View file

@ -1,3 +1,4 @@
use anyhow::Error;
use chrono::{Duration, Utc};
use clap::Parser;
use uuid::Uuid;
@ -6,10 +7,11 @@ use mitra::activitypub::builders::delete_note::prepare_delete_note;
use mitra::activitypub::builders::delete_person::prepare_delete_person;
use mitra::activitypub::fetcher::fetchers::fetch_actor;
use mitra::activitypub::handlers::update_person::update_actor;
use mitra::config;
use mitra::config::{parse_config, Config};
use mitra::database::create_database_client;
use mitra::database::migrate::apply_migrations;
use mitra::ethereum::signatures::generate_ecdsa_key;
use mitra::ethereum::sync::save_current_block_number;
use mitra::ethereum::utils::key_to_ethereum_address;
use mitra::logger::configure_logger;
use mitra::models::attachments::queries::delete_unused_attachments;
@ -41,6 +43,7 @@ enum SubCommand {
DeletePost(DeletePost),
DeleteExtraneousPosts(DeleteExtraneousPosts),
DeleteUnusedAttachments(DeleteUnusedAttachments),
UpdateCurrentBlock(UpdateCurrentBlock),
}
/// Generate RSA private key
@ -98,13 +101,28 @@ struct DeleteExtraneousPosts {
dry_run: bool,
}
/// Delete attachments that doesn't belong to any post
/// Delete attachments that don't belong to any post
#[derive(Parser)]
struct DeleteUnusedAttachments {
#[clap(short)]
days: i64,
}
/// Update blockchain synchronization starting block
#[derive(Parser)]
struct UpdateCurrentBlock {
#[clap(short)]
number: u64,
}
impl UpdateCurrentBlock {
fn execute(&self, config: &Config) -> Result<(), Error> {
save_current_block_number(&config.storage_dir, self.number)?;
println!("current block updated");
Ok(())
}
}
#[tokio::main]
async fn main() {
let opts: Opts = Opts::parse();
@ -121,7 +139,7 @@ async fn main() {
},
subcmd => {
// Other commands require initialized app
let config = config::parse_config();
let config = parse_config();
configure_logger(config.log_level);
log::info!("config loaded from {}", config.config_path);
let db_config = config.database_url.parse().unwrap();
@ -198,6 +216,7 @@ async fn main() {
deletion_queue.process(&config).await;
println!("unused attachments deleted");
},
SubCommand::UpdateCurrentBlock(cmd) => cmd.execute(&config).unwrap(),
_ => panic!(),
};
},

View file

@ -7,5 +7,5 @@ pub mod identity;
pub mod nft;
pub mod signatures;
pub mod subscriptions;
mod sync;
pub mod sync;
pub mod utils;

View file

@ -8,7 +8,7 @@ use super::errors::EthereumError;
const BLOCK_NUMBER_FILE_NAME: &str = "current_block";
pub const CHAIN_REORG_MAX_DEPTH: u64 = 100;
fn save_current_block_number(
pub fn save_current_block_number(
storage_dir: &Path,
block_number: u64,
) -> Result<(), EthereumError> {
@ -41,6 +41,7 @@ pub async fn get_current_block_number(
let block_number = match read_current_block_number(storage_dir)? {
Some(block_number) => block_number,
None => {
// Save block number when connecting to the node for the first time
let block_number = web3.eth().block_number().await?.as_u64();
save_current_block_number(storage_dir, block_number)?;
block_number