use of org.aion.zero.impl.blockchain.AionBlockchainImpl in project aion by aionnetwork.
the class DevCLI method printBlockDetails.
public static Cli.ReturnType printBlockDetails(long nbBlock) {
// ensure mining is disabled
CfgAion localCfg = CfgAion.inst();
localCfg.dbFromXML();
localCfg.getConsensus().setMining(false);
AionLoggerFactory.initAll(Map.of(LogEnum.GEN, LogLevel.INFO));
final Logger log = AionLoggerFactory.getLogger(LogEnum.GEN.name());
// get the current blockchain
AionBlockchainImpl blockchain = new AionBlockchainImpl(localCfg, null, false);
try {
List<Block> blocks = blockchain.getRepository().getAllChainBlockByNumber(nbBlock, log);
if (blocks == null || blocks.isEmpty()) {
log.error("Cannot find the block with given block height.");
return Cli.ReturnType.ERROR;
}
for (Block b : blocks) {
log.info(b.toString());
}
// Now print the transaction state. Only for the mainchain.
// TODO: the worldstate can not read the data after the stateRoot has been setup, need to fix the issue first then the tooling can print the states between the block.
Block mainChainBlock = blockchain.getBlockByNumber(nbBlock);
if (mainChainBlock == null) {
log.error("Cannot find the main chain block with given block height.");
return Cli.ReturnType.ERROR;
}
Block parentBlock = blockchain.getBlockByHash(mainChainBlock.getParentHash());
if (parentBlock == null) {
log.error("Cannot find the parent block with given block height.");
return Cli.ReturnType.ERROR;
}
blockchain.setBestBlock(parentBlock);
Pair<AionBlockSummary, RepositoryCache> result = blockchain.tryImportWithoutFlush(mainChainBlock);
log.info("Import result: " + (result == null ? ImportResult.INVALID_BLOCK : ImportResult.IMPORTED_BEST));
if (result != null) {
log.info("Block summary:\n" + result.getLeft() + "\n");
log.info("RepoCacheDetails:\n" + result.getRight());
}
return Cli.ReturnType.EXIT;
} catch (Exception e) {
log.error("Error encountered while attempting to retrieve the block data.", e);
return Cli.ReturnType.ERROR;
} finally {
blockchain.close();
}
}
use of org.aion.zero.impl.blockchain.AionBlockchainImpl in project aion by aionnetwork.
the class ApiWeb3Aion method ops_getBlock.
public RpcMsg ops_getBlock(Object _params) {
String _bnOrHash;
boolean _fullTx;
if (_params instanceof JSONArray) {
_bnOrHash = ((JSONArray) _params).get(0) + "";
_fullTx = ((JSONArray) _params).optBoolean(1, false);
} else if (_params instanceof JSONObject) {
_bnOrHash = ((JSONObject) _params).get("block") + "";
_fullTx = ((JSONObject) _params).optBoolean("fullTransaction", false);
} else {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
Block block = null;
Long bn = this.parseBnOrId(_bnOrHash);
AionBlockchainImpl blockchainImpl = (AionBlockchainImpl) this.ac.getBlockchain();
// user passed a Long block number
if (bn != null) {
if (bn >= 0) {
block = blockchainImpl.getBlockByNumber(bn);
if (block == null) {
return new RpcMsg(JSONObject.NULL);
}
} else {
return new RpcMsg(JSONObject.NULL);
}
}
// see if the user passed in a hash
if (block == null) {
block = blockchainImpl.getBlockByHash(ByteUtil.hexStringToBytes(_bnOrHash));
if (block == null) {
return new RpcMsg(JSONObject.NULL);
}
}
Block mainBlock = blockchainImpl.getBlockByNumber(block.getNumber());
if (mainBlock == null) {
return new RpcMsg(JSONObject.NULL);
}
if (!Arrays.equals(block.getHash(), mainBlock.getHash())) {
return new RpcMsg(JSONObject.NULL);
}
// ok so now we have a mainchain block
BigInteger blkReward = blockchainImpl.calculateBlockRewards(block.getHeader().getNumber());
BigInteger totalDiff = this.ac.getAionHub().getTotalDifficultyForHash(block.getHash());
JSONObject blk = new JSONObject();
blk.put("timestampVal", block.getTimestamp());
blk.put("blockNumber", block.getNumber());
blk.put("numTransactions", block.getTransactionsList().size());
blk.put("blockHash", StringUtils.toJsonHex(block.getHash()));
blk.put("parentHash", StringUtils.toJsonHex(block.getParentHash()));
blk.put("minerAddress", StringUtils.toJsonHex(block.getCoinbase().toByteArray()));
blk.put("receiptTxRoot", StringUtils.toJsonHex(block.getReceiptsRoot()));
blk.put("txTrieRoot", StringUtils.toJsonHex(block.getTxTrieRoot()));
blk.put("stateRoot", StringUtils.toJsonHex(block.getStateRoot()));
blk.put("difficulty", StringUtils.toJsonHex(block.getDifficulty()));
blk.put("totalDifficulty", totalDiff.toString(16));
blk.put("blockReward", blkReward);
blk.put("nrgConsumed", block.getNrgConsumed());
blk.put("nrgLimit", block.getNrgLimit());
blk.put("size", block.size());
blk.put("bloom", StringUtils.toJsonHex(block.getLogBloom()));
blk.put("extraData", StringUtils.toJsonHex(block.getExtraData()));
blk.put("sealType", StringUtils.toJsonHex(block.getHeader().getSealType().getSealId()));
if (isPowBlock(block)) {
MiningBlock powBlock = (MiningBlock) block;
blk.put("nonce", StringUtils.toJsonHex(powBlock.getNonce()));
blk.put("solution", StringUtils.toJsonHex(powBlock.getHeader().getSolution()));
} else if (isPosBlock(block)) {
StakingBlock posBlock = (StakingBlock) block;
byte[] seedOrProof = posBlock.getHeader().getSeedOrProof();
if (seedOrProof.length == StakingBlockHeader.SEED_LENGTH) {
blk.put("seed", StringUtils.toJsonHex(seedOrProof));
} else if (seedOrProof.length == StakingBlockHeader.PROOF_LENGTH) {
blk.put("proof", StringUtils.toJsonHex(seedOrProof));
} else {
blk.put("seedOrProofError", StringUtils.toJsonHex(seedOrProof));
}
blk.put("publicKey", StringUtils.toJsonHex(posBlock.getHeader().getSigningPublicKey()));
blk.put("signature", StringUtils.toJsonHex(posBlock.getHeader().getSignature()));
}
JSONObject result = new JSONObject();
result.put("blk", blk);
if (_fullTx) {
JSONArray txn = new JSONArray();
for (AionTransaction tx : block.getTransactionsList()) {
// transactionHash, fromAddr, toAddr, value, timestampVal, blockNumber, blockHash
JSONArray t = new JSONArray();
t.put(StringUtils.toJsonHex(tx.getTransactionHash()));
t.put(StringUtils.toJsonHex(tx.getSenderAddress().toByteArray()));
t.put(StringUtils.toJsonHex(tx.getDestinationAddress() == null ? EMPTY_BYTE_ARRAY : tx.getDestinationAddress().toByteArray()));
t.put(StringUtils.toJsonHex(tx.getValue()));
t.put(block.getTimestamp());
t.put(block.getNumber());
txn.put(t);
}
result.put("txn", txn);
}
return new RpcMsg(result);
}
use of org.aion.zero.impl.blockchain.AionBlockchainImpl in project aion by aionnetwork.
the class MiningRPCImplTest method testTimestampInSubmitSolution.
@Test
public void testTimestampInSubmitSolution() {
MiningBlock blockWithRightTimestamp = new MiningBlock(new byte[MiningBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[MiningBlockHeader.BLOOM_BYTE_SIZE], new byte[MiningBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000), new byte[0], new byte[1], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), equihashSolution.toBytes(), 0, 0);
AionImpl aionImpl = mock(AionImpl.class);
AionBlockchainImpl chainImpl = mock(AionBlockchainImpl.class);
ChainHolder chainHolder = spy(new AionChainHolder(aionImpl, accountManager));
doReturn(true).when(chainHolder).isUnityForkEnabled();
doReturn(chainImpl).when(aionImpl).getBlockchain();
doReturn(blockWithRightTimestamp).when(chainImpl).getCachingMiningBlockTemplate(blockThatCanBeSealed.toBytes());
doReturn(true).when(chainHolder).addNewBlock(blockWithRightTimestamp);
doCallRealMethod().when(chainHolder).submitBlock(nonce.toBytes(), equihashSolution.toBytes(), blockThatCanBeSealed.toBytes());
rpcMethods = new RPCMethods(chainHolder);
String method = "submitBlock";
Request request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatCanBeSealed)));
SubmissionResult submissionResult = execute(request1, SubmissionResultConverter::decode);
assertNotNull(submissionResult);
assertTrue(submissionResult.result);
// Now we test current timestamp + 1 (for testing the clock drift)
MiningBlock blockWithRightTimestamp1 = new MiningBlock(new byte[MiningBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[MiningBlockHeader.BLOOM_BYTE_SIZE], new byte[MiningBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000) + 1, new byte[0], new byte[1], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), equihashSolution.toBytes(), 0, 0);
doReturn(blockWithRightTimestamp1).when(chainImpl).getCachingMiningBlockTemplate(blockThatCanBeSealed.toBytes());
doReturn(true).when(chainHolder).addNewBlock(blockWithRightTimestamp1);
request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatCanBeSealed)));
submissionResult = execute(request1, SubmissionResultConverter::decode);
assertNotNull(submissionResult);
assertTrue(submissionResult.result);
// Now we test the future block timestamp (timestamp + 2)
MiningBlock blockWithFutureTimestamp = new MiningBlock(new byte[MiningBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[MiningBlockHeader.BLOOM_BYTE_SIZE], new byte[MiningBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000) + 2, new byte[0], new byte[1], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new byte[MiningBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), equihashSolution.toBytes(), 0, 0);
doReturn(blockWithFutureTimestamp).when(chainImpl).getCachingMiningBlockTemplate(blockThatCanBeSealed.toBytes());
doReturn(true).when(chainHolder).addNewBlock(blockWithFutureTimestamp);
request1 = buildRequest(method, SubmitBlockParamsConverter.encode(new SubmitBlockParams(nonce, equihashSolution, blockThatCanBeSealed)));
submissionResult = execute(request1, SubmissionResultConverter::decode);
assertNotNull(submissionResult);
assertFalse(submissionResult.result);
}
use of org.aion.zero.impl.blockchain.AionBlockchainImpl in project aion by aionnetwork.
the class AionChainHolder method calculateReward.
@Override
public BigInteger calculateReward(Block block) {
Objects.requireNonNull(block);
if (chain.getAionHub().isForkSignatureSwapActive(block.getNumber())) {
boolean isMiningBlock = block.getHeader().getSealType().equals(Seal.PROOF_OF_WORK);
IRewardsCalculator calculator = ((AionBlockchainImpl) chain.getBlockchain()).getChainConfiguration().getRewardsCalculatorAfterSignatureSchemeSwap(isMiningBlock);
if (isMiningBlock) {
Block parent = chain.getBlockchain().getBlockByHash(block.getParentHash());
Objects.requireNonNull(parent);
return calculator.calculateReward(block.getTimestamp() - parent.getTimestamp());
} else {
return calculator.calculateReward(block.getNumber());
}
} else {
return ((AionBlockchainImpl) chain.getBlockchain()).getChainConfiguration().getRewardsCalculatorBeforeSignatureSchemeSwap(chain.getAionHub().isForkUnityActive(block.getNumber())).calculateReward(block.getNumber());
}
}
use of org.aion.zero.impl.blockchain.AionBlockchainImpl in project aion by aionnetwork.
the class StakingRPCImplTest method testTimeStampInSubmitSignature.
@Test
public void testTimeStampInSubmitSignature() {
StakingBlock blockWithRightTimestamp = new StakingBlock(new byte[StakingBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[StakingBlockHeader.BLOOM_BYTE_SIZE], new byte[StakingBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000), new byte[0], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), 0, 0, DEFAULT_SIGNATURE, validSeed.toBytes(), validSigningPublicKey.toBytes());
byte[] validSealHash = blockWithRightTimestamp.getHeader().getMineHash();
AionImpl aionImpl = mock(AionImpl.class);
AionBlockchainImpl chainImpl = mock(AionBlockchainImpl.class);
chainHolder = spy(new AionChainHolder(aionImpl, accountManager));
doReturn(true).when(chainHolder).isUnityForkEnabled();
doReturn(chainImpl).when(aionImpl).getBlockchain();
doReturn(blockWithRightTimestamp).when(chainImpl).getCachingStakingBlockTemplate(validSealHash);
doReturn(true).when(chainHolder).addNewBlock(blockWithRightTimestamp);
doCallRealMethod().when(chainHolder).submitSignature(validSignature.toBytes(), validSealHash);
rpcMethods = new RPCMethods(chainHolder);
String method = "submitsignature";
assertTrue(execute(new Request(1, method, SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(validSignature, ByteArray.wrap(validSealHash))), VersionType.Version2), BoolConverter::decode));
// Now we test current timestamp + 1 (for testing the clock drift)
StakingBlock blockWithRightTimestamp1 = new StakingBlock(new byte[StakingBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[StakingBlockHeader.BLOOM_BYTE_SIZE], new byte[StakingBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000 + 1), new byte[0], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), 0, 0, DEFAULT_SIGNATURE, validSeed.toBytes(), validSigningPublicKey.toBytes());
validSealHash = blockWithRightTimestamp1.getHeader().getMineHash();
doReturn(blockWithRightTimestamp1).when(chainImpl).getCachingStakingBlockTemplate(validSealHash);
doReturn(true).when(chainHolder).addNewBlock(blockWithRightTimestamp1);
assertTrue(execute(new Request(1, method, SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(validSignature, ByteArray.wrap(validSealHash))), VersionType.Version2), BoolConverter::decode));
// Now we test the future block timestamp (timestamp + 2)
StakingBlock futureBlock = new StakingBlock(new byte[StakingBlockHeader.HASH_BYTE_SIZE], AddressUtils.ZERO_ADDRESS, new byte[StakingBlockHeader.BLOOM_BYTE_SIZE], new byte[StakingBlockHeader.MAX_DIFFICULTY_LENGTH], 0, (System.currentTimeMillis() / 1000 + 2), new byte[0], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new byte[StakingBlockHeader.HASH_BYTE_SIZE], new ArrayList<>(), 0, 0, DEFAULT_SIGNATURE, validSeed.toBytes(), validSigningPublicKey.toBytes());
validSealHash = futureBlock.getHeader().getMineHash();
doReturn(futureBlock).when(chainImpl).getCachingStakingBlockTemplate(validSealHash);
doReturn(true).when(chainHolder).addNewBlock(futureBlock);
assertTrue(execute(new Request(1, method, SubmitSignatureParamsConverter.encode(new SubmitSignatureParams(validSignature, ByteArray.wrap(validSealHash))), VersionType.Version2), BoolConverter::decode));
}
Aggregations