Skip to content

Commit

Permalink
Reject negative block numbers, standardise logging
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Nov 20, 2018
1 parent a969e2e commit 651fab7
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions modApiServer/src/org/aion/api/server/rpc/ApiWeb3Aion.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public class ApiWeb3Aion extends ApiAion {

private boolean isSeedMode;

private final long BEST_PENDING_BLOCK = -1L;

private final LoadingCache<Integer, ChainHeadView> CachedRecentEntities;
private final LoadingCache<String, MinerStatsView> MinerStats;

Expand Down Expand Up @@ -571,7 +573,7 @@ public RpcMsg eth_getBlockTransactionCountByNumber(Object _params) {
}

// pending transactions
if (bn < 0) {
if (bn == BEST_PENDING_BLOCK) {
long pendingTxCount = this.ac.getAionHub().getPendingState().getPendingTxSize();
return new RpcMsg(TypeConverter.toJsonHex(pendingTxCount));
}
Expand Down Expand Up @@ -789,7 +791,7 @@ public RpcMsg eth_call(Object _params) {

Long bn = parseBnOrId(bnOrId);
if (bn == null) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid block id provided.");
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid block number.");
}

AionBlock b = getBlockByBN(bn);
Expand Down Expand Up @@ -887,7 +889,7 @@ public RpcMsg eth_getBlockByNumber(Object _params) {
Long bn = this.parseBnOrId(_bnOrId);

if (bn == null) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid block id provided.");
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid block number.");
}

AionBlock nb = getBlockByBN(bn);
Expand Down Expand Up @@ -989,7 +991,7 @@ public RpcMsg eth_getTransactionByBlockNumberAndIndex(Object _params) {

Long bn = parseBnOrId(_bnOrId);
if (bn == null) {
return null;
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid block number.");
}

AionBlock b = this.getBlockByBN(bn);
Expand Down Expand Up @@ -1100,6 +1102,11 @@ private FltrLg createFilter(ArgFltr rf) {
return null;
}

if (bnTo != BEST_PENDING_BLOCK && (bnFrom > bnTo)) {
LOG.debug("jsonrpc - eth_newFilter(): from block is after to block");
return null;
}

AionBlock fromBlock = this.getBlockByBN(bnFrom);
AionBlock toBlock = this.getBlockByBN(bnTo);

Expand Down Expand Up @@ -1338,7 +1345,7 @@ public RpcMsg debug_getBlocksByNumber(Object _params) {
Long bn = parseBnOrId(_bnOrId);

if (bn == null || bn < 0) {
return null;
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid block number.");
}

List<Map.Entry<AionBlock, Map.Entry<BigInteger, Boolean>>> blocks =
Expand Down Expand Up @@ -2823,12 +2830,12 @@ public RpcMsg stratum_getMinerStats(Object _params) {
// comment out until resolved
private IRepository getRepoByJsonBlockId(String _bnOrId) {
Long bn = parseBnOrId(_bnOrId);
// if you passed in an invalid bnOrId, pending or it's an error

if (bn == null) {
return null;
}

if (bn < 0) return pendingState.getRepository();
if (bn == BEST_PENDING_BLOCK) return pendingState.getRepository();

AionBlock b = this.ac.getBlockchain().getBlockByNumber(bn);
if (b == null) {
Expand All @@ -2838,14 +2845,16 @@ private IRepository getRepoByJsonBlockId(String _bnOrId) {
return ac.getRepository().getSnapshotTo(b.getStateRoot());
}

private AionBlock getBlockByBN(Long bn) {
if (bn < 0) {
private AionBlock getBlockByBN(long bn) {
if (bn == BEST_PENDING_BLOCK) {
return pendingState.getBestBlock();
} else {
return this.ac.getBlockchain().getBlockByNumber(bn);
}
}

// Note: If return is null, caller sometimes assumes no blockNumber was passed in

private Long parseBnOrId(String _bnOrId) {
if (_bnOrId == null) {
return null;
Expand All @@ -2857,12 +2866,21 @@ private Long parseBnOrId(String _bnOrId) {
} else if ("latest".equalsIgnoreCase(_bnOrId)) {
return getBestBlock().getNumber();
} else if ("pending".equalsIgnoreCase(_bnOrId)) {
return -1L;
return BEST_PENDING_BLOCK;
} else {

Long ret;

if (_bnOrId.startsWith("0x")) {
return TypeConverter.StringHexToBigInteger(_bnOrId).longValue();
ret = TypeConverter.StringHexToBigInteger(_bnOrId).longValue();
} else {
ret = Long.parseLong(_bnOrId);
}
if (ret < 0) {
LOG.debug("block number cannot be negative" + _bnOrId);
return null;
} else {
return Long.parseLong(_bnOrId);
return ret;
}
}
} catch (Exception e) {
Expand Down

0 comments on commit 651fab7

Please sign in to comment.