Search in sources :

Example 6 with StakingBlock

use of org.aion.zero.impl.types.StakingBlock in project aion by aionnetwork.

the class RPCMethods method serializeBlockDetails.

private BlockDetails serializeBlockDetails(Block block) {
    if (block == null) {
        // occurs if the requested block does not exist in the db
        return null;
    } else {
        final BigInteger blkReward = // get the block reward
        chainHolder.calculateReward(block);
        final BigInteger totalDiff = chainHolder.getTotalDifficultyByHash(// get the total difficulty
        block.getHash());
        List<AionTxInfo> txInfoList = new ArrayList<>();
        logger.debug("Retrieving transactions for block: {}", "0x" + ByteUtil.toHexString(block.getHash()));
        for (AionTransaction transaction : block.getTransactionsList()) {
            AionTxInfo txInfo = chainHolder.getTransactionInfo(transaction.getTransactionHash());
            txInfoList.add(txInfo);
        }
        Block previousBlock = // get the parent block
        chainHolder.getBlockByHash(block.getParentHash());
        final Long previousTimestamp;
        if (previousBlock == null) {
            previousTimestamp = null;
        } else {
            previousTimestamp = previousBlock.getTimestamp();
        // block time
        }
        if (block.getHeader().getSealType().equals(// return a block based on the seal type
        Seal.PROOF_OF_WORK))
            return new BlockDetails(block.getNumber(), ByteArray.wrap(block.getHash()), ByteArray.wrap(block.getParentHash()), ByteArray.wrap(block.getLogBloom()), ByteArray.wrap(block.getTxTrieRoot()), ByteArray.wrap(block.getStateRoot()), ByteArray.wrap(block.getReceiptsRoot()), ByteUtil.bytesToBigInteger(block.getDifficulty()), totalDiff, block.getCoinbase(), block.getTimestamp(), block.getNrgConsumed(), block.getNrgLimit(), block.getNrgConsumed(), block.getNrgLimit(), block.getHeader().getSealType().getSealId(), block.isMainChain(), ByteArray.wrap(block.getHeader().getExtraData()), block.size(), block.getTransactionsList().size(), ByteArray.wrap(block.getTxTrieRoot()), blkReward, serializeTxDetails(txInfoList, block), ByteArray.wrap(((MiningBlock) block).getNonce()), ByteArray.wrap(((MiningBlock) block).getHeader().getSolution()), null, null, null, previousBlock == null ? null : Math.toIntExact((block.getTimestamp() - previousTimestamp)));
        else
            return new BlockDetails(block.getNumber(), ByteArray.wrap(block.getHash()), ByteArray.wrap(block.getParentHash()), ByteArray.wrap(block.getLogBloom()), ByteArray.wrap(block.getTxTrieRoot()), ByteArray.wrap(block.getStateRoot()), ByteArray.wrap(block.getReceiptsRoot()), ByteUtil.bytesToBigInteger(block.getDifficulty()), totalDiff, block.getCoinbase(), block.getTimestamp(), block.getNrgConsumed(), block.getNrgLimit(), block.getNrgConsumed(), block.getNrgLimit(), block.getHeader().getSealType().getSealId(), block.isMainChain(), ByteArray.wrap(block.getHeader().getExtraData()), block.size(), block.getTransactionsList().size(), ByteArray.wrap(block.getTxTrieRoot()), blkReward, serializeTxDetails(txInfoList, block), null, null, ByteArray.wrap(((StakingBlock) block).getHeader().getSeedOrProof()), ByteArray.wrap(((StakingBlock) block).getHeader().getSignature()), ByteArray.wrap(((StakingBlock) block).getHeader().getSigningPublicKey()), previousBlock == null ? null : Math.toIntExact((block.getTimestamp() - previousTimestamp)));
    }
}
Also used : BlockDetails(org.aion.rpc.types.RPCTypes.BlockDetails) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) StakingBlock(org.aion.zero.impl.types.StakingBlock) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) StakingBlock(org.aion.zero.impl.types.StakingBlock)

Example 7 with StakingBlock

use of org.aion.zero.impl.types.StakingBlock in project aion by aionnetwork.

the class Blk method AionBlockOnlyToJson.

@SuppressWarnings("Duplicates")
public static JSONObject AionBlockOnlyToJson(Block block) {
    if (block == null) {
        return null;
    }
    JSONObject obj = new JSONObject();
    obj.put("number", block.getHeader().getNumber());
    obj.put("hash", StringUtils.toJsonHex(block.getHeader().getHash()));
    obj.put("parentHash", StringUtils.toJsonHex(block.getHeader().getParentHash()));
    obj.put("logsBloom", StringUtils.toJsonHex(block.getLogBloom()));
    obj.put("transactionsRoot", StringUtils.toJsonHex(block.getTxTrieRoot()));
    obj.put("stateRoot", StringUtils.toJsonHex(block.getStateRoot()));
    obj.put("receiptsRoot", StringUtils.toJsonHex(block.getReceiptsRoot() == null ? new byte[0] : block.getReceiptsRoot()));
    obj.put("difficulty", StringUtils.toJsonHex(block.getHeader().getDifficulty()));
    obj.put("totalDifficulty", StringUtils.toJsonHex(block.getTotalDifficulty()));
    obj.put("timestamp", StringUtils.toJsonHex(block.getHeader().getTimestamp()));
    obj.put("gasUsed", StringUtils.toJsonHex(block.getHeader().getEnergyConsumed()));
    obj.put("gasLimit", StringUtils.toJsonHex(block.getHeader().getEnergyLimit()));
    obj.put("nrgUsed", StringUtils.toJsonHex(block.getHeader().getEnergyConsumed()));
    obj.put("nrgLimit", StringUtils.toJsonHex(block.getHeader().getEnergyLimit()));
    obj.put("numTransactions", block.getTransactionsList().size());
    obj.put("extraData", StringUtils.toJsonHex(block.getExtraData()));
    obj.put("miner", StringUtils.toJsonHex(block.getCoinbase().toString()));
    obj.put("sealType", StringUtils.toJsonHex(block.getHeader().getSealType().getSealId()));
    obj.put("mainChain", block.isMainChain() ? "true" : "false");
    if (block.getHeader().getSealType() == Seal.PROOF_OF_WORK) {
        MiningBlock miningBlock = (MiningBlock) block;
        obj.put("nonce", StringUtils.toJsonHex(miningBlock.getHeader().getNonce()));
        obj.put("solution", StringUtils.toJsonHex(miningBlock.getHeader().getSolution()));
        obj.put("size", miningBlock.size());
    } else if (block.getHeader().getSealType() == Seal.PROOF_OF_STAKE) {
        StakingBlock stakingBlock = (StakingBlock) block;
        byte[] seedOrProof = stakingBlock.getHeader().getSeedOrProof();
        if (seedOrProof.length == StakingBlockHeader.SEED_LENGTH) {
            obj.put("seed", StringUtils.toJsonHex(seedOrProof));
        } else if (seedOrProof.length == StakingBlockHeader.PROOF_LENGTH) {
            obj.put("proof", StringUtils.toJsonHex(seedOrProof));
        } else {
            obj.put("seedOrProofError", StringUtils.toJsonHex(seedOrProof));
        }
        obj.put("signature", StringUtils.toJsonHex(stakingBlock.getHeader().getSignature()));
        obj.put("publicKey", StringUtils.toJsonHex(stakingBlock.getHeader().getSigningPublicKey()));
        obj.put("size", stakingBlock.size());
    } else {
        throw new IllegalStateException("Invalid block seal type!");
    }
    return obj;
}
Also used : JSONObject(org.json.JSONObject) MiningBlock(org.aion.zero.impl.types.MiningBlock) StakingBlock(org.aion.zero.impl.types.StakingBlock)

Example 8 with StakingBlock

use of org.aion.zero.impl.types.StakingBlock in project aion by aionnetwork.

the class Blk method AionBlockOnlyToJson.

public static JSONObject AionBlockOnlyToJson(Block genericBlock, BigInteger totalDifficulty) {
    if (genericBlock == null) {
        return null;
    }
    JSONObject obj = new JSONObject();
    obj.put("number", genericBlock.getNumber());
    obj.put("hash", StringUtils.toJsonHex(genericBlock.getHash()));
    obj.put("parentHash", StringUtils.toJsonHex(genericBlock.getParentHash()));
    obj.put("logsBloom", StringUtils.toJsonHex(genericBlock.getLogBloom()));
    obj.put("transactionsRoot", StringUtils.toJsonHex(genericBlock.getTxTrieRoot()));
    obj.put("stateRoot", StringUtils.toJsonHex(genericBlock.getStateRoot()));
    obj.put("receiptsRoot", StringUtils.toJsonHex(genericBlock.getReceiptsRoot() == null ? new byte[0] : genericBlock.getReceiptsRoot()));
    obj.put("difficulty", StringUtils.toJsonHex(genericBlock.getDifficulty()));
    obj.put("totalDifficulty", StringUtils.toJsonHex(totalDifficulty));
    obj.put("miner", StringUtils.toJsonHex(genericBlock.getCoinbase().toString()));
    obj.put("timestamp", StringUtils.toJsonHex(genericBlock.getTimestamp()));
    obj.put("gasUsed", StringUtils.toJsonHex(genericBlock.getHeader().getEnergyConsumed()));
    obj.put("gasLimit", StringUtils.toJsonHex(genericBlock.getHeader().getEnergyLimit()));
    obj.put("nrgUsed", StringUtils.toJsonHex(genericBlock.getHeader().getEnergyConsumed()));
    obj.put("nrgLimit", StringUtils.toJsonHex(genericBlock.getHeader().getEnergyLimit()));
    obj.put("sealType", StringUtils.toJsonHex(genericBlock.getHeader().getSealType().getSealId()));
    obj.put("mainChain", genericBlock.isMainChain() ? "true" : "false");
    obj.put("extraData", StringUtils.toJsonHex(genericBlock.getExtraData()));
    obj.put("size", genericBlock.size());
    obj.put("numTransactions", genericBlock.getTransactionsList().size());
    if (genericBlock.getHeader().getSealType() == Seal.PROOF_OF_WORK) {
        MiningBlock block = (MiningBlock) genericBlock;
        obj.put("nonce", StringUtils.toJsonHex(block.getHeader().getNonce()));
        obj.put("solution", StringUtils.toJsonHex(block.getHeader().getSolution()));
        obj.put("size", block.size());
    } else if (genericBlock.getHeader().getSealType() == Seal.PROOF_OF_STAKE) {
        StakingBlock block = (StakingBlock) genericBlock;
        byte[] seedOrProof = block.getHeader().getSeedOrProof();
        if (seedOrProof.length == StakingBlockHeader.SEED_LENGTH) {
            obj.put("seed", StringUtils.toJsonHex(seedOrProof));
        } else if (seedOrProof.length == StakingBlockHeader.PROOF_LENGTH) {
            obj.put("proof", StringUtils.toJsonHex(seedOrProof));
        } else {
            obj.put("seedOrProofError", StringUtils.toJsonHex(seedOrProof));
        }
        obj.put("signature", StringUtils.toJsonHex(block.getHeader().getSignature()));
        obj.put("publicKey", StringUtils.toJsonHex(block.getHeader().getSigningPublicKey()));
        obj.put("size", block.size());
    } else {
        throw new IllegalStateException("Invalid block seal type!");
    }
    return obj;
}
Also used : JSONObject(org.json.JSONObject) MiningBlock(org.aion.zero.impl.types.MiningBlock) StakingBlock(org.aion.zero.impl.types.StakingBlock)

Example 9 with StakingBlock

use of org.aion.zero.impl.types.StakingBlock in project aion by aionnetwork.

the class Blk method AionBlockToJson.

public static Object AionBlockToJson(Block block, boolean fullTransaction) {
    if (block == null) {
        return null;
    }
    JSONObject obj = new JSONObject();
    obj.put("number", block.getHeader().getNumber());
    obj.put("hash", StringUtils.toJsonHex(block.getHeader().getHash()));
    obj.put("parentHash", StringUtils.toJsonHex(block.getHeader().getParentHash()));
    obj.put("logsBloom", StringUtils.toJsonHex(block.getLogBloom()));
    obj.put("transactionsRoot", StringUtils.toJsonHex(block.getTxTrieRoot()));
    obj.put("stateRoot", StringUtils.toJsonHex(block.getStateRoot()));
    obj.put("receiptsRoot", StringUtils.toJsonHex(block.getReceiptsRoot() == null ? new byte[0] : block.getReceiptsRoot()));
    obj.put("difficulty", StringUtils.toJsonHex(block.getHeader().getDifficulty()));
    obj.put("totalDifficulty", StringUtils.toJsonHex(block.getTotalDifficulty()));
    obj.put("timestamp", StringUtils.toJsonHex(block.getHeader().getTimestamp()));
    obj.put("miner", StringUtils.toJsonHex(block.getCoinbase().toString()));
    obj.put("gasUsed", StringUtils.toJsonHex(block.getHeader().getEnergyConsumed()));
    obj.put("gasLimit", StringUtils.toJsonHex(block.getHeader().getEnergyLimit()));
    obj.put("nrgUsed", StringUtils.toJsonHex(block.getHeader().getEnergyConsumed()));
    obj.put("nrgLimit", StringUtils.toJsonHex(block.getHeader().getEnergyLimit()));
    obj.put("extraData", StringUtils.toJsonHex(block.getHeader().getExtraData()));
    obj.put("sealType", StringUtils.toJsonHex(block.getHeader().getSealType().getSealId()));
    obj.put("mainChain", block.isMainChain() ? "true" : "false");
    if (block.getHeader().getSealType() == Seal.PROOF_OF_WORK) {
        MiningBlock miningBlock = (MiningBlock) block;
        obj.put("nonce", StringUtils.toJsonHex(miningBlock.getNonce()));
        obj.put("solution", StringUtils.toJsonHex(miningBlock.getHeader().getSolution()));
        obj.put("size", new NumericalValue(miningBlock.size()).toHexString());
    } else if (block.getHeader().getSealType() == Seal.PROOF_OF_STAKE) {
        StakingBlock stakingBlock = (StakingBlock) block;
        byte[] seedOrProof = stakingBlock.getHeader().getSeedOrProof();
        if (seedOrProof.length == StakingBlockHeader.SEED_LENGTH) {
            obj.put("seed", StringUtils.toJsonHex(seedOrProof));
        } else if (seedOrProof.length == StakingBlockHeader.PROOF_LENGTH) {
            obj.put("proof", StringUtils.toJsonHex(seedOrProof));
        } else {
            obj.put("seedOrProofError", StringUtils.toJsonHex(seedOrProof));
        }
        obj.put("signature", StringUtils.toJsonHex(stakingBlock.getHeader().getSignature()));
        obj.put("publicKey", StringUtils.toJsonHex(stakingBlock.getHeader().getSigningPublicKey()));
        obj.put("size", new NumericalValue(stakingBlock.size()).toHexString());
    } else {
        throw new IllegalStateException("Invalid block seal type!");
    }
    JSONArray jsonTxs = new JSONArray();
    List<AionTransaction> txs = block.getTransactionsList();
    for (int i = 0; i < txs.size(); i++) {
        AionTransaction tx = txs.get(i);
        if (fullTransaction) {
            JSONObject jsonTx = new JSONObject();
            AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
            jsonTx.put("contractAddress", (contractAddress != null) ? StringUtils.toJsonHex(contractAddress.toString()) : null);
            jsonTx.put("hash", StringUtils.toJsonHex(tx.getTransactionHash()));
            jsonTx.put("transactionIndex", i);
            jsonTx.put("value", StringUtils.toJsonHex(tx.getValue()));
            jsonTx.put("nrg", tx.getEnergyLimit());
            jsonTx.put("nrgPrice", StringUtils.toJsonHex(tx.getEnergyPrice()));
            jsonTx.put("gas", tx.getEnergyLimit());
            jsonTx.put("gasPrice", StringUtils.toJsonHex(tx.getEnergyPrice()));
            jsonTx.put("nonce", ByteUtil.byteArrayToLong(tx.getNonce()));
            jsonTx.put("from", StringUtils.toJsonHex(tx.getSenderAddress().toString()));
            jsonTx.put("to", StringUtils.toJsonHex(tx.getDestinationAddress() == null ? EMPTY_BYTE_ARRAY : tx.getDestinationAddress().toByteArray()));
            jsonTx.put("timestamp", tx.getTimeStampBI());
            jsonTx.put("input", StringUtils.toJsonHex(tx.getData()));
            jsonTx.put("blockNumber", block.getHeader().getNumber());
            jsonTxs.put(jsonTx);
        } else {
            jsonTxs.put(StringUtils.toJsonHex(tx.getTransactionHash()));
        }
    }
    obj.put("transactions", jsonTxs);
    return obj;
}
Also used : AionAddress(org.aion.types.AionAddress) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) StakingBlock(org.aion.zero.impl.types.StakingBlock)

Example 10 with StakingBlock

use of org.aion.zero.impl.types.StakingBlock in project aion by aionnetwork.

the class RpcImpl method submitsignature.

@Override
public boolean submitsignature(byte[] signature, byte[] sealhash) throws NullReturnRpcException {
    if (!ac.getAionHub().getBlockchain().isUnityForkEnabledAtNextBlock()) {
        throw new NullReturnRpcException("unity fork is not enabled");
    }
    if (signature == null) {
        throw new NullReturnRpcException("the giving signature is null");
    }
    if (sealhash == null) {
        throw new NullReturnRpcException("the giving sealhash is null");
    }
    if (signature.length != StakingBlockHeader.SIG_LENGTH) {
        throw new NullReturnRpcException("the giving signature length is incorrect");
    }
    if (sealhash.length != StakingBlockHeader.HASH_BYTE_SIZE) {
        throw new NullReturnRpcException("the giving sealhash length is incorrect");
    }
    StakingBlock block = (StakingBlock) ac.getBlockchain().getCachingStakingBlockTemplate(sealhash);
    if (block == null) {
        return false;
    }
    // Cannot submit a future block to the kernel
    if (block.getTimestamp() > TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + 1) {
        return false;
    }
    block.seal(signature, block.getHeader().getSigningPublicKey());
    ImportResult result = ac.addNewBlock(block);
    boolean sealed = (result == ImportResult.IMPORTED_BEST || result == ImportResult.IMPORTED_NOT_BEST);
    if (sealed) {
        AionLoggerFactory.getLogger(LogEnum.CONS.toString()).info("Staking block sealed <num={}, hash={}, diff={}, tx={}>", block.getNumber(), block.getShortHash(), block.getDifficultyBI(), block.getTransactionsList().size());
    }
    return sealed;
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) NullReturnRpcException(org.aion.api.server.rpc2.autogen.errors.NullReturnRpcException) StakingBlock(org.aion.zero.impl.types.StakingBlock)

Aggregations

StakingBlock (org.aion.zero.impl.types.StakingBlock)18 MiningBlock (org.aion.zero.impl.types.MiningBlock)10 Block (org.aion.zero.impl.types.Block)7 AionAddress (org.aion.types.AionAddress)6 BigInteger (java.math.BigInteger)4 AionTransaction (org.aion.base.AionTransaction)4 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)4 GenesisStakingBlock (org.aion.zero.impl.types.GenesisStakingBlock)4 RetValidPreBlock (org.aion.zero.impl.types.RetValidPreBlock)4 BlockDetailsValidator.isValidBlock (org.aion.zero.impl.valid.BlockDetailsValidator.isValidBlock)4 JSONObject (org.json.JSONObject)4 ArrayList (java.util.ArrayList)3 AionBlockchainImpl (org.aion.zero.impl.blockchain.AionBlockchainImpl)3 AccountState (org.aion.base.AccountState)2 ImportResult (org.aion.zero.impl.core.ImportResult)2 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)2 StakingBlockHeader (org.aion.zero.impl.types.StakingBlockHeader)2 JSONArray (org.json.JSONArray)2 ByteString (com.google.protobuf.ByteString)1 ArrayDeque (java.util.ArrayDeque)1