Skip to content

Commit 4d35a90

Browse files
liferooterGleb Smirnov
andauthored
feat(ethexe): improve database migrations (#5188)
Co-authored-by: Gleb Smirnov <gleb.s@gear-tech.io>
1 parent e7a8757 commit 4d35a90

File tree

21 files changed

+644
-387
lines changed

21 files changed

+644
-387
lines changed

Cargo.lock

Lines changed: 8 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ members = [
111111
"ethexe/*",
112112
"ethexe/runtime/common",
113113
"ethexe/service/utils",
114-
"ethexe/db/init",
115114
]
116115

117116
[workspace.dependencies]

ethexe/cli/src/commands/check.rs

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@
1616
// You should have received a copy of the GNU General Public License
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

19+
use crate::params::{MergeParams, Params};
1920
use anyhow::{Context, Result, anyhow, ensure};
2021
use clap::Parser;
2122
use ethexe_common::{
2223
Announce, HashOf, SimpleBlockData,
2324
db::{AnnounceStorageRO, DBGlobals, GlobalsStorageRO, OnChainStorageRO},
25+
gear::CANONICAL_QUARANTINE,
2426
};
2527
use ethexe_db::{
26-
Database, RawDatabase, RocksDatabase,
28+
Database, InitConfig, RawDatabase, RocksDatabase,
2729
iterator::{BlockNode, DatabaseIterator},
2830
verifier::IntegrityVerifier,
2931
visitor::{self},
3032
};
31-
use ethexe_processor::{Processor, ProcessorConfig};
33+
use ethexe_processor::{DEFAULT_CHUNK_SIZE, Processor, ProcessorConfig};
3234
use indicatif::{ProgressBar, ProgressStyle};
3335
use std::{collections::HashSet, path::PathBuf};
3436

@@ -38,15 +40,13 @@ const PROGRESS_BAR_TEMPLATE: &str = "{spinner:.green} [{elapsed_precise}] [{wide
3840
/// Run checks on ethexe database, see more in [`super::Command::Check`].
3941
#[derive(Debug, Parser)]
4042
pub struct CheckCommand {
41-
/// Path to database directory (including router addr subdirectory).
42-
#[arg(long)]
43-
pub db: PathBuf,
44-
45-
#[arg(long, default_value = "2")]
46-
pub chunk_size: usize,
43+
/// CLI parameters to be merged with file ones before execution.
44+
#[clap(flatten)]
45+
pub params: Params,
4746

48-
#[arg(long, default_value = "4")]
49-
pub canonical_quarantine: u8,
47+
/// Override database location.
48+
#[arg(long)]
49+
pub db: Option<PathBuf>,
5050

5151
/// Perform computations of announces, by default from start announce to latest computed announce.
5252
#[arg(long, alias = "compute")]
@@ -56,12 +56,21 @@ pub struct CheckCommand {
5656
#[arg(long, alias = "integrity")]
5757
pub integrity_check: bool,
5858

59+
/// Perform migrations before checking the database.
60+
#[arg(long)]
61+
pub migrate: bool,
62+
5963
/// Enable logging verbosity (debug level by default), disables progress bar.
6064
#[arg(short, long)]
6165
pub verbose: bool,
6266
}
6367

6468
impl CheckCommand {
69+
pub fn with_params(mut self, params: Params) -> Self {
70+
self.params = self.params.merge(params);
71+
self
72+
}
73+
6574
/// Execute the command.
6675
pub fn exec(self) -> Result<()> {
6776
tokio::runtime::Builder::new_multi_thread()
@@ -80,17 +89,48 @@ impl CheckCommand {
8089
self.integrity_check = true;
8190
}
8291

83-
let rocks_db = RocksDatabase::open(self.db).context("failed to open rocks database")?;
84-
let db = Database::try_from_raw(RawDatabase::from_one(&rocks_db))?;
92+
let rocks_db = RocksDatabase::open(
93+
self.db
94+
.or_else(|| self.params.node.as_ref().map(|node| node.db_dir()))
95+
.context("missing database path")?,
96+
)
97+
.context("failed to open rocks database")?;
98+
let raw_db = RawDatabase::from_one(&rocks_db);
99+
100+
let db = if self.migrate {
101+
let ethereum_config = self
102+
.params
103+
.ethereum
104+
.context("missing Ethereum-related configuration")?
105+
.into_config()?;
106+
107+
ethexe_db::initialize_db(
108+
InitConfig {
109+
ethereum_rpc: ethereum_config.rpc.clone(),
110+
router_address: ethereum_config.router_address,
111+
slot_duration_secs: ethereum_config.block_time.as_secs(),
112+
},
113+
raw_db.overlaid(),
114+
)
115+
.await?
116+
} else {
117+
Database::try_from_raw(raw_db)?
118+
};
85119

86120
let globals = db.globals().clone();
87121

122+
let node_params = self.params.node.unwrap_or_default();
88123
let checker = Checker {
89124
db,
90125
globals,
91126
progress_bar: !self.verbose,
92-
chunk_size: self.chunk_size,
93-
canonical_quarantine: self.canonical_quarantine,
127+
chunk_size: node_params
128+
.chunk_processing_threads
129+
.unwrap_or(DEFAULT_CHUNK_SIZE)
130+
.get(),
131+
canonical_quarantine: node_params
132+
.canonical_quarantine
133+
.unwrap_or(CANONICAL_QUARANTINE),
94134
};
95135

96136
if self.integrity_check {

ethexe/cli/src/commands/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub enum Command {
3636
/// Keystore manipulations.
3737
Key(KeyCommand),
3838
/// Run the node.
39-
Run(Box<RunCommand>),
39+
Run(RunCommand),
4040
/// Submit a transaction.
4141
Tx(TxCommand),
4242
/// Check ethexe database for integrity and/or computation correctness.
@@ -50,9 +50,9 @@ impl Command {
5050
fn with_file_params(self, file_params: Params) -> Self {
5151
match self {
5252
Self::Key(key_cmd) => Self::Key(key_cmd.with_params(file_params)),
53-
Self::Run(run_cmd) => Self::Run(Box::new(run_cmd.with_params(file_params))),
53+
Self::Run(run_cmd) => Self::Run(run_cmd.with_params(file_params)),
5454
Self::Tx(tx_cmd) => Self::Tx(tx_cmd.with_params(file_params)),
55-
Self::Check(check_cmd) => Self::Check(check_cmd),
55+
Self::Check(check_cmd) => Self::Check(check_cmd.with_params(file_params)),
5656
}
5757
}
5858

ethexe/common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ gear-workspace-hack.workspace = true
3838
gprimitives = { workspace = true, features = ["ethexe"] }
3939
serde_json.workspace = true
4040
indoc.workspace = true
41-
scale-info = { workspace = true, features = ["docs"] }
41+
hex.workspace = true
4242

4343
[features]
4444
default = ["std"]

ethexe/db/Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ repository.workspace = true
99

1010
[dependencies]
1111
ethexe-common = { workspace = true, features = ["std"] }
12+
ethexe-ethereum.workspace = true
1213
ethexe-runtime-common = { workspace = true, features = ["std"] }
1314
gear-core = { workspace = true, features = ["std"] }
1415
gprimitives = { workspace = true, features = ["std"] }
16+
gsigner.workspace = true
1517

18+
alloy.workspace = true
1619
anyhow.workspace = true
1720
dashmap.workspace = true
21+
log.workspace = true
1822
parity-scale-codec.workspace = true
1923
tracing.workspace = true
2024
paste.workspace = true
2125
derive_more.workspace = true
26+
scale-info = { workspace = true, features = ["derive"] }
2227
auto_impl.workspace = true
2328
gear-workspace-hack.workspace = true
2429
delegate.workspace = true
@@ -38,7 +43,11 @@ version = "0.21"
3843
scopeguard.workspace = true
3944
tempfile.workspace = true
4045
ethexe-common = { workspace = true, features = ["mock"] }
46+
indoc.workspace = true
47+
scale-info = { workspace = true, features = ["docs"] }
48+
sha3.workspace = true
49+
hex.workspace = true
4150

4251
[features]
4352
default = ["mock"]
44-
mock = ["ethexe-common/mock"]
53+
mock = ["ethexe-common/mock"]

ethexe/db/init/Cargo.toml

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)