Skip to content

Commit

Permalink
Merge pull request #183 from cherry1603/develop
Browse files Browse the repository at this point in the history
fix rpc
  • Loading branch information
LucasMLK authored Jan 8, 2023
2 parents da48b84 + c5f01fc commit 9df4200
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/main/java/io/xdag/rpc/modules/xdag/XdagModuleChainBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static io.xdag.utils.BasicUtils.amount2xdag;
import static io.xdag.utils.BasicUtils.hash2Address;
import static io.xdag.utils.BasicUtils.pubAddress2Hash;
import static io.xdag.utils.PubkeyAddressUtils.checkAddress;
import static io.xdag.utils.PubkeyAddressUtils.toBase58;
import static io.xdag.utils.XdagTime.xdagTimestampToMs;

Expand Down Expand Up @@ -153,17 +154,21 @@ public String getMaxXferBalance() {

public BlockResultDTO getBlockDTOByHash(String hash) {
Bytes32 blockHash;
if (StringUtils.length(hash) == 32) {
blockHash = address2Hash(hash);
if (checkAddress(hash)) {
return transferAccountToBlockResultDTO(hash);
} else {
blockHash = BasicUtils.getHash(hash);
}
Block block = blockchain.getBlockByHash(blockHash, true);
if (block == null) {
block = blockchain.getBlockByHash(blockHash, false);
return transferBlockInfoToBlockResultDTO(block);
if (StringUtils.length(hash) == 32) {
blockHash = address2Hash(hash);
} else {
blockHash = BasicUtils.getHash(hash);
}
Block block = blockchain.getBlockByHash(blockHash, true);
if (block == null) {
block = blockchain.getBlockByHash(blockHash, false);
return transferBlockInfoToBlockResultDTO(block);
}
return transferBlockToBlockResultDTO(block);
}
return transferBlockToBlockResultDTO(block);
}

private BlockResultDTO transferBlockToBriefBlockResultDTO(Block block) {
Expand Down Expand Up @@ -209,6 +214,28 @@ private BlockResultDTO transferBlockInfoToBlockResultDTO(Block block) {
return BlockResultDTOBuilder.build();
}

private BlockResultDTO transferAccountToBlockResultDTO(String address) {
UInt64 balance = kernel.getAddressStore().getBalanceByAddress(Hash2byte(pubAddress2Hash(address).mutableCopy()));

BlockResultDTO.BlockResultDTOBuilder BlockResultDTOBuilder = BlockResultDTO.builder();
BlockResultDTOBuilder.address(address)
.hash(null)
.balance(String.format("%.9f", amount2xdag(balance)))
.type("Wallet")
.blockTime(xdagTimestampToMs(kernel.getConfig().getSnapshotSpec().getSnapshotTime()))
.timeStamp(kernel.getConfig().getSnapshotSpec().getSnapshotTime())
// .flags(Integer.toHexString(block.getInfo().getFlags()))
// .diff(toQuantityJsonHex(block.getInfo().getDifficulty()))
// .remark(block.getInfo().getRemark() == null ? "" : new String(block.getInfo().getRemark(),
// StandardCharsets.UTF_8).trim())
.state("Accepted")
// .type(getType(block))
// .refs(getLinks(block))
// .height(block.getInfo().getHeight())
.transactions(getTxHistory(address));
return BlockResultDTOBuilder.build();
}

private BlockResultDTO transferBlockToBlockResultDTO(Block block) {
if (null == block) {
return null;
Expand Down

0 comments on commit 9df4200

Please sign in to comment.