Skip to content

Commit

Permalink
Added RPC feature to modify page size.
Browse files Browse the repository at this point in the history
  • Loading branch information
Junze888 committed Aug 10, 2023
1 parent 2365460 commit c26ccfe
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 56 deletions.
42 changes: 22 additions & 20 deletions docs/XDAGJ_RPC.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/io/xdag/core/Blockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public interface Blockchain {
// 注册监听器
void registerListener(Listener listener);

List<TxHistory> getBlockTxHistoryByAddress(Bytes32 addressHashlow, int page, Object... timeRange);
List<TxHistory> getBlockTxHistoryByAddress(Bytes32 addressHashlow, int page, Object... parameters);

XdagExtStats getXdagExtStats();
}
6 changes: 3 additions & 3 deletions src/main/java/io/xdag/core/BlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,18 +494,18 @@ public void onNewTxHistory(Bytes32 addressHashlow, Bytes32 txHashlow, XdagField.
}
}

public List<TxHistory> getBlockTxHistoryByAddress(Bytes32 addressHashlow, int page, Object... timeRange) {
public List<TxHistory> getBlockTxHistoryByAddress(Bytes32 addressHashlow, int page, Object... parameters) {
List<TxHistory> txHistory = Lists.newArrayList();
if (txHistoryStore != null) {
if (checkAddress(BasicUtils.hash2PubAddress(addressHashlow))) {
try {
txHistory.addAll(txHistoryStore.listTxHistoryByAddress(BasicUtils.hash2PubAddress(addressHashlow), page, timeRange));
txHistory.addAll(txHistoryStore.listTxHistoryByAddress(BasicUtils.hash2PubAddress(addressHashlow), page, parameters));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
} else {
try {
txHistory.addAll(txHistoryStore.listTxHistoryByAddress(BasicUtils.hash2Address(addressHashlow), page, timeRange));
txHistory.addAll(txHistoryStore.listTxHistoryByAddress(BasicUtils.hash2Address(addressHashlow), page, parameters));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/xdag/db/TransactionHistoryStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface TransactionHistoryStore {

boolean saveTxHistory(TxHistory txHistory);
boolean batchSaveTxHistory(TxHistory txHistory,int... cacheNum);
List<TxHistory> listTxHistoryByAddress(String address, int page, Object... timeRange);
List<TxHistory> listTxHistoryByAddress(String address, int page, Object... parameters);

int getTxHistoryCount(String address);

Expand Down
42 changes: 31 additions & 11 deletions src/main/java/io/xdag/db/mysql/TransactionHistoryStoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class TransactionHistoryStoreImpl implements TransactionHistoryStore {
private static final int BLOCK_ADDRESS_FLAG = 0;
private static final int WALLET_ADDRESS_FLAG = 1;

private static final int PAGE_SIZE = 100;
private static final int Default_PAGE_SIZE = 100;
private Connection connBatch = null;
private PreparedStatement pstmtBatch = null;
private int count = 0;
Expand Down Expand Up @@ -155,23 +155,43 @@ public boolean batchSaveTxHistory(TxHistory txHistory, int... cacheNum) {


@Override
public List<TxHistory> listTxHistoryByAddress(String address, int page, Object... timeRange) {
public List<TxHistory> listTxHistoryByAddress(String address, int page, Object... parameters) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<TxHistory> txHistoryList = Lists.newArrayList();
int totalcount = 0;
int PAGE_SIZE = Default_PAGE_SIZE;
long start = new Date(0).getTime();
long end = System.currentTimeMillis();
if (timeRange.length != 0) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
start = sdf.parse(timeRange[0].toString()).getTime();
end = sdf.parse(timeRange[1].toString()).getTime();
} catch (ParseException e) {
start = Long.parseLong(timeRange[0].toString());
end = Long.parseLong(timeRange[1].toString());
}
switch (parameters.length) {
case 0: break;
case 1:
int pageSize = Integer.parseInt(parameters[0].toString());
PAGE_SIZE = (pageSize > 0 && pageSize <= 500) ? pageSize : PAGE_SIZE;
break;
case 2:
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
start = sdf.parse(parameters[0].toString()).getTime();
end = sdf.parse(parameters[1].toString()).getTime();
} catch (ParseException e) {
start = Long.parseLong(parameters[0].toString());
end = Long.parseLong(parameters[1].toString());
}
break;
case 3:
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
start = sdf.parse(parameters[0].toString()).getTime();
end = sdf.parse(parameters[1].toString()).getTime();
} catch (ParseException e) {
start = Long.parseLong(parameters[0].toString());
end = Long.parseLong(parameters[1].toString());
}
int page_size = Integer.parseInt(parameters[2].toString());
PAGE_SIZE = (page_size > 0 && page_size <= 500) ? page_size : PAGE_SIZE;
break;
}
try {
conn = DruidUtils.getConnection();
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/xdag/rpc/Web3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,21 @@ public Object xdag_getBlocksByNumber(String bnOrId) {

@Override
public BlockResultDTO xdag_getBlockByHash(String blockHash, int page, String startTime, String endTime) {
return web3XdagModule.xdag_getBlockByHash(blockHash, page, startTime,endTime);
return web3XdagModule.xdag_getBlockByHash(blockHash, page, startTime, endTime);
}

public BlockResultDTO xdag_getBlockByHash(String blockHash, int page) {
return web3XdagModule.xdag_getBlockByHash(blockHash, page);
}

public BlockResultDTO xdag_getBlockByHash(String blockHash, int page, String startTime, String endTime, int pageSize) {
return web3XdagModule.xdag_getBlockByHash(blockHash, page, startTime, endTime, pageSize);
}

public BlockResultDTO xdag_getBlockByHash(String blockHash, int page, int pageSize) {
return web3XdagModule.xdag_getBlockByHash(blockHash, page, pageSize);
}

@Override
public StatusDTO xdag_getStatus() throws Exception {
return web3XdagModule.xdag_getStatus();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/xdag/rpc/modules/xdag/Web3XdagModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ default BlockResultDTO xdag_getBlockByHash(String blockHash, int page) {
return getXdagModule().getBlockByHash(blockHash, page);
}

default BlockResultDTO xdag_getBlockByHash(String blockHash, int page, String startTime, String endTime, int pageSize) {
return getXdagModule().getBlockByHash(blockHash, page, startTime, endTime, pageSize);
}

default BlockResultDTO xdag_getBlockByHash(String blockHash, int page, int pageSize) {
return getXdagModule().getBlockByHash(blockHash, page, pageSize);
}

StatusDTO xdag_getStatus() throws Exception;

Object xdag_netType() throws Exception;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/xdag/rpc/modules/xdag/XdagModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public String getCoinBase() {
}

@Override
public BlockResultDTO getBlockByHash(String hash, int page,Object... timeRange ) {
return xdagModuleChain.getBlockByHash(hash, page, timeRange);
public BlockResultDTO getBlockByHash(String hash, int page,Object... parameters ) {
return xdagModuleChain.getBlockByHash(hash, page, parameters);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface XdagModuleChain {

String getCoinBase();

BlockResultDTO getBlockByHash(String hash, int page, Object... timeRange);
BlockResultDTO getBlockByHash(String hash, int page, Object... parameters);

BlockResultDTO getBlockByNumber(String bnOrId, int page);

Expand Down
32 changes: 16 additions & 16 deletions src/main/java/io/xdag/rpc/modules/xdag/XdagModuleChainBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public String getCoinBase() {
}

@Override
public BlockResultDTO getBlockByHash(String hash, int page, Object... timeRange) {
return getBlockDTOByHash(hash, page, timeRange);
public BlockResultDTO getBlockByHash(String hash, int page, Object... parameters) {
return getBlockDTOByHash(hash, page, parameters);
}

@Override
Expand Down Expand Up @@ -147,10 +147,10 @@ public String getMaxXferBalance() {
return Commands.getBalanceMaxXfer(kernel);
}

public BlockResultDTO getBlockDTOByHash(String hash, int page, Object... timeRange) {
public BlockResultDTO getBlockDTOByHash(String hash, int page, Object... parameters) {
Bytes32 blockHash;
if (checkAddress(hash)) {
return transferAccountToBlockResultDTO(hash, page, timeRange);
return transferAccountToBlockResultDTO(hash, page, parameters);
} else {
if (StringUtils.length(hash) == 32) {
blockHash = address2Hash(hash);
Expand All @@ -160,9 +160,9 @@ public BlockResultDTO getBlockDTOByHash(String hash, int page, Object... timeRan
Block block = blockchain.getBlockByHash(blockHash, true);
if (block == null) {
block = blockchain.getBlockByHash(blockHash, false);
return transferBlockInfoToBlockResultDTO(block, page, timeRange);
return transferBlockInfoToBlockResultDTO(block, page, parameters);
}
return transferBlockToBlockResultDTO(block, page, timeRange);
return transferBlockToBlockResultDTO(block, page, parameters);
}
}

Expand All @@ -186,7 +186,7 @@ private BlockResultDTO transferBlockToBriefBlockResultDTO(Block block) {
return BlockResultDTOBuilder.build();
}

private BlockResultDTO transferBlockInfoToBlockResultDTO(Block block, int page, Object... timeRange) {
private BlockResultDTO transferBlockInfoToBlockResultDTO(Block block, int page, Object... parameters) {
if (null == block) {
return null;
}
Expand All @@ -206,14 +206,14 @@ private BlockResultDTO transferBlockInfoToBlockResultDTO(Block block, int page,
// .refs(getLinks(block))
// .height(block.getInfo().getHeight())
if (page != 0){
BlockResultDTOBuilder.transactions(getTxLinks(block, page, timeRange))
BlockResultDTOBuilder.transactions(getTxLinks(block, page, parameters))
.totalPage(totalPage);
}
totalPage = 1;
return BlockResultDTOBuilder.build();
}

private BlockResultDTO transferAccountToBlockResultDTO(String address, int page, Object... timeRange) {
private BlockResultDTO transferAccountToBlockResultDTO(String address, int page, Object... parameters) {
XAmount balance = kernel.getAddressStore().getBalanceByAddress(hash2byte(pubAddress2Hash(address).mutableCopy()));

BlockResultDTO.BlockResultDTOBuilder BlockResultDTOBuilder = BlockResultDTO.builder();
Expand All @@ -225,14 +225,14 @@ private BlockResultDTO transferAccountToBlockResultDTO(String address, int page,
.timeStamp(kernel.getConfig().getSnapshotSpec().getSnapshotTime())
.state("Accepted");
if (page != 0){
BlockResultDTOBuilder.transactions(getTxHistory(address, page, timeRange))
BlockResultDTOBuilder.transactions(getTxHistory(address, page, parameters))
.totalPage(totalPage);
}
totalPage = 1;
return BlockResultDTOBuilder.build();
}

private BlockResultDTO transferBlockToBlockResultDTO(Block block, int page, Object... timeRange) {
private BlockResultDTO transferBlockToBlockResultDTO(Block block, int page, Object... parameters) {
if (null == block) {
return null;
}
Expand All @@ -251,7 +251,7 @@ private BlockResultDTO transferBlockToBlockResultDTO(Block block, int page, Obje
.refs(getLinks(block))
.height(block.getInfo().getHeight());
if (page != 0) {
BlockResultDTOBuilder.transactions(getTxLinks(block, page, timeRange))
BlockResultDTOBuilder.transactions(getTxLinks(block, page, parameters))
.totalPage(totalPage);
}
totalPage = 1;
Expand Down Expand Up @@ -296,8 +296,8 @@ private List<Link> getLinks(Block block) {
return links;
}

private List<TxLink> getTxLinks(Block block, int page, Object timeRange) {
List<TxHistory> txHistories = blockchain.getBlockTxHistoryByAddress(block.getHashLow(), page, timeRange);
private List<TxLink> getTxLinks(Block block, int page, Object... parameters) {
List<TxHistory> txHistories = blockchain.getBlockTxHistoryByAddress(block.getHashLow(), page, parameters);
List<TxLink> txLinks = Lists.newArrayList();
// 1. earning info
if (getStateByFlags(block.getInfo().getFlags()).equals(MAIN.getDesc()) && block.getInfo().getHeight() > kernel.getConfig().getSnapshotSpec().getSnapshotHeight()) {
Expand Down Expand Up @@ -333,8 +333,8 @@ private List<TxLink> getTxLinks(Block block, int page, Object timeRange) {
return txLinks;
}

private List<TxLink> getTxHistory(String address, int page, Object... timeRange) {
List<TxHistory> txHistories = blockchain.getBlockTxHistoryByAddress(pubAddress2Hash(address), page, timeRange);
private List<TxLink> getTxHistory(String address, int page, Object... parameters) {
List<TxHistory> txHistories = blockchain.getBlockTxHistoryByAddress(pubAddress2Hash(address), page, parameters);
List<TxLink> txLinks = Lists.newArrayList();
for (TxHistory txHistory : txHistories) {
Block b = blockchain.getBlockByHash(txHistory.getAddress().getAddress(), false);
Expand Down

0 comments on commit c26ccfe

Please sign in to comment.