Skip to content

Commit

Permalink
feat(toolkit ): compare opt for account map field
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed Oct 12, 2024
1 parent 4f69f35 commit 96fa6e9
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions plugins/src/main/java/org/tron/plugins/DbCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.tron.plugins.utils.db.DBInterface;
import org.tron.plugins.utils.db.DBIterator;
import org.tron.plugins.utils.db.DbTool;
import org.tron.protos.Protocol;
import picocli.CommandLine;

@Slf4j(topic = "compare")
Expand All @@ -33,6 +34,8 @@ public class DbCompare implements Callable<Integer> {
@CommandLine.Option(names = {"-h", "--help"}, help = true, description = "display a help message")
private boolean help;

private static final String ACCOUNT_VOTE_SUFFIX = "-account-vote";

@Override
public Integer call() throws Exception {
if (help) {
Expand Down Expand Up @@ -98,7 +101,7 @@ private boolean compare() throws RocksDBException, IOException {
byte[] baseKey = baseIterator.getKey();
byte[] dstKey = dstIterator.getKey();
byte[] dstValue = dstIterator.getValue();
if (Arrays.equals(baseKey, dstKey) && !Arrays.equals(baseValue, dstValue)) {
if (Arrays.equals(baseKey, dstKey) && !compareValue(baseKey, baseValue, dstValue)) {
spec.commandLine().getOut().format("%s\t%s\t%s.",
ByteArray.toHexString(baseKey),
ByteArray.toHexString(baseValue), ByteArray.toHexString(dstValue)).println();
Expand All @@ -109,15 +112,15 @@ private boolean compare() throws RocksDBException, IOException {
if (!Arrays.equals(baseKey, dstKey)) {
byte[] dstValueTmp = base.get(dstKey);
byte[] baseValueTmp = dst.get(baseKey);
if (!Arrays.equals(baseValue, baseValueTmp)) {
if (!compareValue(baseKey, baseValue, baseValueTmp)) {
spec.commandLine().getOut().format("%s\t%s\t%s.",
ByteArray.toHexString(baseKey),
ByteArray.toHexString(baseValue), ByteArray.toHexString(baseValueTmp)).println();
logger.info("{}\t{}\t{}",
ByteArray.toHexString(baseKey),
ByteArray.toHexString(baseValue), ByteArray.toHexString(baseValueTmp));
}
if (!Arrays.equals(dstValueTmp, dstValue)) {
if (!compareValue(dstKey, dstValueTmp, dstValue)) {
spec.commandLine().getOut().format("%s\t%s\t%s.",
ByteArray.toHexString(dstKey),
ByteArray.toHexString(dstValueTmp), ByteArray.toHexString(dstValue)).println();
Expand All @@ -132,7 +135,7 @@ private boolean compare() throws RocksDBException, IOException {
byte[] key = baseIterator.getKey();
byte[] baseValue = baseIterator.getValue();
byte[] destValue = dst.get(key);
if (!Arrays.equals(baseValue, destValue)) {
if (!compareValue(key, baseValue, destValue)) {
spec.commandLine().getOut().format("%s\t%s\t%s.",
ByteArray.toHexString(key),
ByteArray.toHexString(baseValue), ByteArray.toHexString(destValue)).println();
Expand All @@ -145,7 +148,7 @@ private boolean compare() throws RocksDBException, IOException {
byte[] key = dstIterator.getKey();
byte[] destValue = dstIterator.getValue();
byte[] baseValue = base.get(key);
if (!Arrays.equals(baseValue, destValue)) {
if (!compareValue(key, baseValue, destValue)) {
spec.commandLine().getOut().format("%s\t%s\t%s.",
ByteArray.toHexString(key),
ByteArray.toHexString(baseValue), ByteArray.toHexString(destValue)).println();
Expand All @@ -159,5 +162,23 @@ private boolean compare() throws RocksDBException, IOException {
spec.commandLine().getOut().println("compare " + this.name + " end");
return true;
}

private boolean compareValue(byte[] key, byte[] base, byte[] exp) {
if ("account".equals(name)
|| ("delegation".equals(name) && new String(key).endsWith(ACCOUNT_VOTE_SUFFIX))) {
Protocol.Account baseAccount = ByteArray.toAccount(base);
Protocol.Account expAccount = ByteArray.toAccount(exp);
// remove assetOptimized // TODO why?
if (baseAccount.getAssetOptimized() || expAccount.getAssetOptimized()) {
baseAccount = baseAccount.toBuilder().clearAsset().clearAssetV2()
.setAssetOptimized(true).build();
expAccount = expAccount.toBuilder().clearAsset().clearAssetV2()
.setAssetOptimized(true).build();
}
return baseAccount.equals(expAccount);
} else {
return Arrays.equals(base, exp);
}
}
}
}

0 comments on commit 96fa6e9

Please sign in to comment.