Skip to content

feat(toolkit): show db root error to console #5748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,14 @@ Execute move command.
java -jar Toolkit.jar db mv -c main_net_config.conf -d /data/tron/output-directory
```

## DB Root

DB root provides a helper which can compute merkle root for tiny db.

NOTE: large db may GC overhead limit exceeded.

### Available parameters:

- `<src>`: Source path for database. Default: output-directory/database
- `--db`: db name.
- `-h | --help`: provide the help info
38 changes: 30 additions & 8 deletions plugins/src/main/java/org/tron/plugins/DbRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,20 @@ public Integer call() throws Exception {
.errorText("Specify at least one exit database: --db dbName."));
return 404;
}
if (dbs.size() > 1) {
ProgressBar.wrap(dbs.stream(), "root task").parallel().forEach(this::calcMerkleRoot);
} else {
calcMerkleRoot(dbs.get(0));
List<Ret> task = ProgressBar.wrap(dbs.stream(), "root task").parallel()
.map(this::calcMerkleRoot).collect(Collectors.toList());
task.forEach(this::printInfo);
int code = (int) task.stream().filter(r -> r.code == 1).count();
if (code > 0) {
spec.commandLine().getErr().println(spec.commandLine().getColorScheme()
.errorText("There are some errors, please check toolkit.log for detail."));
}
spec.commandLine().getOut().println("root task done.");
return 0;
return code;
}

private void calcMerkleRoot(String name) {
private Ret calcMerkleRoot(String name) {
Ret info = new Ret();
try (DBInterface database = DbTool.getDB(this.db, name)) {
DBIterator iterator = database.iterator();
iterator.seekToFirst();
Expand All @@ -85,15 +89,33 @@ private void calcMerkleRoot(String name) {
.collect(Collectors.toCollection(ArrayList::new));
Sha256Hash root = MerkleRoot.root(ids);
logger.info("db: {},root: {}", database.getName(), root);
spec.commandLine().getOut().println(String.format("db: %s,root: %s",
database.getName(), root));
info.code = 0;
info.msg = String.format("db: %s,root: %s", database.getName(), root);
} catch (RocksDBException | IOException e) {
logger.error("calc db {} fail", name, e);
info.code = 1;
info.msg = String.format("db: %s,fail: %s",
name, e.getMessage());
}
return info;
}

private Sha256Hash getHash(Map.Entry<byte[], byte[]> entry) {
return Sha256Hash.of(true,
Bytes.concat(entry.getKey(), entry.getValue()));
}

private void printInfo(Ret ret) {
if (ret.code == 0) {
spec.commandLine().getOut().println(ret.msg);
} else {
spec.commandLine().getErr().println(spec.commandLine().getColorScheme()
.errorText(ret.msg));
}
}

private static class Ret {
private int code;
private String msg;
}
}
2 changes: 1 addition & 1 deletion plugins/src/test/java/org/tron/plugins/DbRootTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void testRoot() throws IOException, RocksDBException {
errorDb.put(("" + i).getBytes(), (ERROR_DB + "-" + i).getBytes());
}
args = new String[] {"db", "root", database.toString(), "--db", ERROR_DB};
Assert.assertEquals(0, cli.execute(args));
Assert.assertEquals(1, cli.execute(args));
}

}
Expand Down