Skip to content

Commit

Permalink
feat(toolkit): show db root error to console (#5748)
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 authored Mar 7, 2024
1 parent 074e0d6 commit eaf2c34
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
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

0 comments on commit eaf2c34

Please sign in to comment.