Skip to content

Commit 95a6d8b

Browse files
committed
[all resource export] add subcommand for export
1 parent cf76032 commit 95a6d8b

File tree

3 files changed

+98
-39
lines changed

3 files changed

+98
-39
lines changed

cmd/resource-code-exporter/src/export.rs

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
// Copyright (c) The Starcoin Core Contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use clap::Parser;
54
use starcoin_crypto::HashValue;
65
use starcoin_statedb::{ChainStateDB, ChainStateReader};
76
use starcoin_storage::{
87
db_storage::DBStorage, storage::StorageInstance, BlockStore, Storage, StorageVersion,
98
};
10-
use std::{
11-
convert::TryInto,
12-
fmt::Debug,
13-
io::Write,
14-
path::{Path, PathBuf},
15-
sync::Arc,
16-
};
9+
use std::{fmt::Debug, io::Write, path::Path, sync::Arc};
1710

1811
/// Export resources and code from storage for a specific block
1912
pub fn export(db: &str, output: &Path, block_id: HashValue) -> anyhow::Result<()> {
@@ -85,24 +78,6 @@ pub fn export_from_statedb<W: Write>(
8578
Ok(())
8679
}
8780

88-
#[derive(Debug, Clone, Parser)]
89-
#[clap(
90-
name = "resource-code-exporter",
91-
about = "onchain resource and code exporter"
92-
)]
93-
pub struct ExporterOptions {
94-
#[clap(long, short = 'o', parse(from_os_str))]
95-
/// output file, like accounts.csv
96-
pub output: PathBuf,
97-
#[clap(long, short = 'i', parse(from_os_str))]
98-
/// starcoin node db path. like ~/.starcoin/barnard/starcoindb/db/starcoindb
99-
pub db_path: PathBuf,
100-
101-
#[clap(long)]
102-
/// block id which snapshot at.
103-
pub block_id: HashValue,
104-
}
105-
10681
#[cfg(test)]
10782
mod test {
10883
use super::*;

cmd/resource-code-exporter/src/import.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
use starcoin_crypto::HashValue;
22
use starcoin_statedb::{ChainStateDB, ChainStateReader, ChainStateWriter};
33
use starcoin_storage::{db_storage::DBStorage, storage::StorageInstance, Storage, StorageVersion};
4-
use starcoin_types::account_address::AccountAddress;
5-
use starcoin_types::state_set::{AccountStateSet, ChainStateSet};
6-
use std::path::Path;
4+
use starcoin_types::{
5+
account_address::AccountAddress,
6+
state_set::{AccountStateSet, ChainStateSet},
7+
};
8+
use std::path::{Path, PathBuf};
79
use std::sync::Arc;
810

11+
pub fn import_from_db_path(db_path: &PathBuf, csv_path: &PathBuf) -> anyhow::Result<()> {
12+
let db_storage = DBStorage::open_with_cfs(
13+
&db_path,
14+
StorageVersion::current_version()
15+
.get_column_family_names()
16+
.to_vec(),
17+
false,
18+
Default::default(),
19+
None,
20+
)?;
21+
let storage = Storage::new(StorageInstance::new_db_instance(db_storage))?;
22+
let storage = Arc::new(storage);
23+
let statedb = ChainStateDB::new(storage.clone(), None);
24+
import(&statedb, csv_path)
25+
}
26+
927
/// Import resources and code from CSV file to a new statedb
1028
pub fn import(statedb: &ChainStateDB, csv_path: &Path) -> anyhow::Result<()> {
1129
// Read CSV file
@@ -50,7 +68,9 @@ pub fn import(statedb: &ChainStateDB, csv_path: &Path) -> anyhow::Result<()> {
5068
mod test {
5169
use super::*;
5270
use crate::export::export_from_statedb;
71+
use starcoin_storage::db_storage::DBStorage;
5372
use std::fs::create_dir_all;
73+
use std::sync::Arc;
5474
use tempfile::TempDir;
5575
use test_helper::executor::prepare_genesis;
5676

cmd/resource-code-exporter/src/main.rs

+74-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,81 @@
44
mod export;
55
mod import;
66

7-
use crate::export::ExporterOptions;
8-
use clap::Parser;
7+
use clap::{Parser, Subcommand};
8+
use std::path::PathBuf;
9+
use starcoin_storage::{db_storage::DBStorage, storage::StorageInstance, Storage, StorageVersion};
10+
use starcoin_statedb::ChainStateDB;
11+
use std::sync::Arc;
12+
13+
#[derive(Parser)]
14+
#[clap(name = "resource-code-exporter", about = "Export and import state data")]
15+
struct Cli {
16+
#[clap(subcommand)]
17+
command: Commands,
18+
}
19+
20+
#[derive(Subcommand)]
21+
enum Commands {
22+
/// Export state data to CSV file
23+
Export {
24+
#[clap(long, short = 'o', parse(from_os_str))]
25+
/// Output file path, e.g. accounts.csv
26+
output: PathBuf,
27+
28+
#[clap(long, short = 'i', parse(from_os_str))]
29+
/// Starcoin node db path, e.g. ~/.starcoin/barnard/starcoindb/db/starcoindb
30+
db_path: PathBuf,
31+
32+
#[clap(long)]
33+
/// Block id to export state at
34+
block_id: starcoin_crypto::HashValue,
35+
},
36+
/// Import state data from CSV file
37+
Import {
38+
#[clap(long, short = 'i', parse(from_os_str))]
39+
/// Input CSV file path
40+
input: PathBuf,
41+
42+
#[clap(long, short = 'o', parse(from_os_str))]
43+
/// Output database path
44+
db_path: PathBuf,
45+
},
46+
}
947

1048
fn main() -> anyhow::Result<()> {
11-
let option: ExporterOptions = ExporterOptions::parse();
12-
let output = option.output.as_path();
13-
let block_id = option.block_id;
14-
export::export(
15-
option.db_path.display().to_string().as_str(),
16-
output,
17-
block_id,
18-
)?;
49+
let cli = Cli::parse();
50+
51+
match cli.command {
52+
Commands::Export {
53+
output,
54+
db_path,
55+
block_id,
56+
} => {
57+
export::export(
58+
db_path.display().to_string().as_str(),
59+
&output,
60+
block_id,
61+
)?;
62+
}
63+
Commands::Import { input, db_path } => {
64+
// Create new statedb for import
65+
let db_storage = DBStorage::open_with_cfs(
66+
&db_path,
67+
StorageVersion::current_version()
68+
.get_column_family_names()
69+
.to_vec(),
70+
false,
71+
Default::default(),
72+
None,
73+
)?;
74+
let storage = Storage::new(StorageInstance::new_db_instance(db_storage))?;
75+
let storage = Arc::new(storage);
76+
let statedb = ChainStateDB::new(storage.clone(), None);
77+
78+
// Import data
79+
import::import(&statedb, &input)?;
80+
}
81+
}
82+
1983
Ok(())
2084
}

0 commit comments

Comments
 (0)