use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiWeb3Aion method eth_newFilter.
/* -------------------------------------------------------------------------
* filters
*/
/* Web3 Filters Support
*
* NOTE: newFilter behaviour is ill-defined in the JSON-rpc spec for the following scenarios:
* (an explanation of how we resolved these ambiguities follows immediately after)
*
* newFilter is used to subscribe for filter on transaction logs for transactions with provided address and topics
*
* role of fromBlock, toBlock fields within context of newFilter, newBlockFilter, newPendingTransactionFilter
* (they seem only more pertinent for getLogs)
* how we resolve it: populate historical data (best-effort) in the filter response before "installing the filter"
* onus on the user to flush the filter of the historical data, before depending on it for up-to-date values.
* apart from loading historical data, fromBlock & toBlock are ignored when loading events on filter queue
*/
public String eth_newFilter(JSONObject _filterObj) {
ArgFltr rf = ArgFltr.fromJSON(_filterObj);
FltrLg filter = new FltrLg();
filter.setTopics(rf.topics);
filter.setContractAddress(rf.address);
Long bnFrom = parseBnOrId(rf.fromBlock);
Long bnTo = parseBnOrId(rf.toBlock);
if (bnFrom == null || bnTo == null || bnFrom == -1 || bnTo == -1) {
LOG.debug("jsonrpc - eth_newFilter(): from, to block parse failed");
return null;
}
final AionBlock fromBlock = this.ac.getBlockchain().getBlockByNumber(bnFrom);
AionBlock toBlock = this.ac.getBlockchain().getBlockByNumber(bnTo);
if (fromBlock != null) {
// need to add historical data
// this is our own policy: what to do in this case is not defined in the spec
//
// policy: add data from earliest to latest, until we can't fill the queue anymore
//
// caveat: filling up the events-queue with historical data will cause the following issue:
// the user will miss all events generated between the first poll and filter installation.
toBlock = toBlock == null ? getBestBlock() : toBlock;
for (long i = fromBlock.getNumber(); i <= toBlock.getNumber(); i++) {
if (filter.isFull())
break;
filter.onBlock(this.ac.getBlockchain().getBlockByNumber(i), this.ac.getAionHub().getBlockchain());
}
}
// "install" the filter after populating historical data;
// rationale: until the user gets the id back, the user should not expect the filter to be "installed" anyway.
long id = fltrIndex.getAndIncrement();
installedFilters.put(id, filter);
return TypeConverter.toJsonHex(id);
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiWeb3Aion method eth_getBlockTransactionCountByHash.
public Object eth_getBlockTransactionCountByHash(String hashString) {
byte[] hash = ByteUtil.hexStringToBytes(hashString);
AionBlock b = this.ac.getBlockchain().getBlockByHash(hash);
if (b == null)
return null;
long n = b.getTransactionsList().size();
return TypeConverter.toJsonHex(n);
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiWeb3Aion method stratum_submitblock.
public Object stratum_submitblock(Object nce, Object soln, Object hdrHash, Object ts) {
JSONObject obj = new JSONObject();
if (nce != null && soln != null && hdrHash != null && ts != null && !nce.equals(null) && !soln.equals(null) && !hdrHash.equals(null) && !ts.equals(null)) {
templateMapLock.writeLock().lock();
AionBlock bestBlock = templateMap.get(hdrHash + "");
if (bestBlock != null) {
IEvent ev = new EventConsensus(EventConsensus.CALLBACK.ON_SOLUTION);
ev.setFuncArgs(Collections.singletonList(new Solution(bestBlock, hexStringToBytes(nce + ""), hexStringToBytes(soln + ""), Long.parseLong(ts + "", 16))));
evtMgr.newEvent(ev);
LOG.info("block submitted via api <num={}, hash={}, diff={}, tx={}>", bestBlock.getNumber(), // LogUtil.toHexF8(newBlock.getHash()),
bestBlock.getShortHash(), bestBlock.getHeader().getDifficultyBI().toString(), bestBlock.getTransactionsList().size());
templateMap.clear();
}
templateMapLock.writeLock().unlock();
// TODO: Simplified response for now, need to provide better feedback to caller in next update
obj.put("result", true);
} else {
obj.put("message", "success");
obj.put("code", -1);
}
return obj;
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiWeb3Aion method eth_getTransactionByBlockNumberAndIndex.
public Object eth_getTransactionByBlockNumberAndIndex(String _bnOrId, String _index) {
Long bn = parseBnOrId(_bnOrId);
if (bn == null || bn < 0)
return null;
AionBlock b = this.ac.getBlockchain().getBlockByNumber(bn);
if (b == null)
return null;
List<AionTransaction> txs = b.getTransactionsList();
int idx = Integer.decode(_index);
if (idx >= txs.size())
return null;
return Tx.AionTransactionToJSON(txs.get(idx), b, idx);
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiWeb3Aion method debug_getBlocksByNumber.
/* -------------------------------------------------------------------------
* debug
*/
public Object debug_getBlocksByNumber(String _bnOrId, boolean _fullTransactions) {
Long bn = parseBnOrId(_bnOrId);
if (bn == null || bn < 0)
return null;
List<Map.Entry<AionBlock, Map.Entry<BigInteger, Boolean>>> blocks = ((AionBlockStore) this.ac.getAionHub().getBlockchain().getBlockStore()).getBlocksByNumber(bn);
if (blocks == null) {
LOG.debug("<get-block bn={} err=not-found>");
return null;
}
JSONArray response = new JSONArray();
for (Map.Entry<AionBlock, Map.Entry<BigInteger, Boolean>> block : blocks) {
JSONObject b = (JSONObject) Blk.AionBlockToJson(block.getKey(), block.getValue().getKey(), _fullTransactions);
b.put("mainchain", block.getValue().getValue());
response.put(b);
}
return response;
}
Aggregations