Skip to content

Commit

Permalink
Merge pull request #235 from XDagger/hotfix/sync
Browse files Browse the repository at this point in the history
Hotfix/sync
  • Loading branch information
LucasMLK authored Jun 23, 2023
2 parents 207dd12 + 17efb89 commit f97ec34
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
30 changes: 12 additions & 18 deletions src/main/java/io/xdag/core/BlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@
import io.xdag.utils.WalletUtils;
import io.xdag.utils.XdagTime;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -768,11 +763,7 @@ private UInt64 applyBlock(Block block) {
if (link.getType() == XdagField.FieldType.XDAG_FIELD_IN) {
subtractAndAccept(ref,link.getAmount());
UInt64 allBalance = addressStore.getAllBalance();
try {
allBalance = allBalance.addExact(link.getAmount());
}catch (Exception e){
log.debug("apply allBalance error");
}
allBalance = allBalance.add(link.getAmount());
addressStore.updateAllBalance(allBalance);
} else {
addAndAccept(ref,link.getAmount());
Expand All @@ -796,6 +787,7 @@ private UInt64 applyBlock(Block block) {
// TODO: unapply block which in snapshot
public UInt64 unApplyBlock(Block block) {
List<Address> links = block.getLinks();
Collections.reverse(links);
if ((block.getInfo().flags & BI_APPLIED) != 0) {
UInt64 sum = UInt64.ZERO;
for (Address link : links) {
Expand Down Expand Up @@ -1495,7 +1487,9 @@ public void startCheckMain(long period) {
}

public void checkState() {
checkOrphan();
if (kernel.getXdagState() == XdagState.SDST || XdagState.STST == kernel.getXdagState() || XdagState.SYNC == kernel.getXdagState()) {
checkOrphan();
}
checkMain();
}

Expand Down Expand Up @@ -1561,7 +1555,7 @@ private void addAndAccept(Block block, UInt64 amount) {
try {
block.getInfo().setAmount(block.getInfo().getAmount().addExact(amount));
} catch (Exception e) {
// log.error(e.getMessage(), e);
log.error(e.getMessage(), e);
log.debug("balance {} amount {} block {}", oldAmount, amount, block.getHashLow().toHexString());
}
UInt64 finalAmount = blockStore.getBlockInfoByHash(block.getHashLow()).getInfo().getAmount();
Expand All @@ -1583,7 +1577,7 @@ private void subtractAndAccept(Block block,UInt64 amount){
try {
block.getInfo().setAmount(block.getInfo().getAmount().subtractExact(amount));
} catch (Exception e) {
// log.error(e.getMessage(), e);
log.error(e.getMessage(), e);
log.debug("balance {} amount {} block {}", oldAmount, amount, block.getHashLow().toHexString());
}
UInt64 finalAmount = blockStore.getBlockInfoByHash(block.getHashLow()).getInfo().getAmount();
Expand All @@ -1605,7 +1599,7 @@ private void subtractAmount(byte[] addressHash, UInt64 amount, Block block){
try {
addressStore.updateBalance(addressHash,balance.subtractExact(amount));
} catch (Exception e) {
// log.error(e.getMessage(), e);
log.error(e.getMessage(), e);
log.debug("balance {} amount {} addressHsh {} block {}", balance, amount, toBase58(addressHash), block.getHashLow());
}
UInt64 finalAmount = addressStore.getBalanceByAddress(addressHash);
Expand All @@ -1623,11 +1617,11 @@ private void addAmount(byte[] addressHash, UInt64 amount, Block block){
try {
addressStore.updateBalance(addressHash,balance.addExact(amount));
} catch (Exception e) {
// log.error(e.getMessage(), e);
log.error(e.getMessage(), e);
log.debug("balance {} amount {} addressHsh {} block {}", balance, amount, toBase58(addressHash), block.getHashLow());
}
UInt64 finalAmount = addressStore.getBalanceByAddress(addressHash);
log.debug("Balance checker —— Address:{} [old:{} add:{} fin:{}]",
log.warn("Balance checker —— Address:{} [old:{} add:{} fin:{}]",
WalletUtils.toBase58(addressHash),
BasicUtils.amount2xdag(balance),
BasicUtils.amount2xdag(amount),
Expand All @@ -1645,7 +1639,7 @@ private void acceptAmount(Block block, UInt64 amount) {
blockStore.saveBlockInfo(block.getInfo());
}
UInt64 finalAmount = blockStore.getBlockByHash(block.getHashLow(),false).getInfo().getAmount();
log.debug("Balance checker —— Block:{} [old:{} acc:{} fin:{}]",
log.warn("Balance checker —— Block:{} [old:{} acc:{} fin:{}]",
block.getHashLow().toHexString(),
BasicUtils.amount2xdag(oldAmount),
BasicUtils.amount2xdag(amount),
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/xdag/db/rocksdb/SnapshotStoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -254,7 +255,7 @@ public void saveAddress(BlockStore blockStore,AddressStore addressStore, List<Ke
}
}
addressStore.snapshotAddress(address,balance);
blockStore.saveTxHistory(BytesUtils.arrayToByte32(address), BytesUtils.arrayToByte32(address),
blockStore.saveTxHistory(BytesUtils.arrayToByte32(Arrays.copyOfRange(address,1,21)), BytesUtils.arrayToByte32(Arrays.copyOfRange(address,1,21)),
XdagField.FieldType.XDAG_FIELD_SNAPSHOT,UInt64.fromBytes(Bytes.wrap(balance)),
snapshotTime,0,"snapshot".getBytes());
}
Expand Down

0 comments on commit f97ec34

Please sign in to comment.