Search in sources :

Example 81 with MiningBlock

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

the class ApiWeb3Aion method stratum_submitblock.

public RpcMsg stratum_submitblock(Object _params) {
    Object nce;
    Object soln;
    Object hdrHash;
    if (_params instanceof JSONArray) {
        nce = ((JSONArray) _params).opt(0);
        soln = ((JSONArray) _params).opt(1);
        hdrHash = ((JSONArray) _params).opt(2);
    } else {
        return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
    }
    JSONObject obj = new JSONObject();
    if (!JSONObject.NULL.equals(nce) && !JSONObject.NULL.equals(soln) && !JSONObject.NULL.equals(hdrHash)) {
        // Grab copy of best block
        MiningBlock bestBlock = ac.getBlockchain().getCachingMiningBlockTemplate(hexStringToBytes((String) hdrHash));
        if (bestBlock != null && bestBlock.notSealed()) {
            try {
                bestBlock.seal(hexStringToBytes(nce + ""), hexStringToBytes(soln + ""));
            } catch (Exception e) {
                LOG.error("Finalize block failed!", e);
                obj.put("message", "failed: " + e.toString());
                obj.put("code", -1);
                return new RpcMsg(obj);
            }
            // Directly submit to chain for new due to delays using event, explore event
            // submission again
            ImportResult importResult = AionImpl.inst().addNewBlock(bestBlock);
            if (importResult.isSuccessful()) {
                LOG.info("mining block submitted via api <num={}, hash={}, diff={}, tx={}>", bestBlock.getNumber(), // LogUtil.toHexF8(newBlock.getHash()),
                bestBlock.getShortHash(), bestBlock.getHeader().getDifficultyBI().toString(), bestBlock.getTransactionsList().size());
            } else {
                LOG.info("Unable to submit mining block via api <num={}, hash={}, diff={}, tx={}>", bestBlock.getNumber(), // LogUtil.toHexF8(newBlock.getHash()),
                bestBlock.getShortHash(), bestBlock.getHeader().getDifficultyBI().toString(), bestBlock.getTransactionsList().size());
            }
        }
        // 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 new RpcMsg(obj);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) Hex.toHexString(org.aion.util.conversions.Hex.toHexString) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 82 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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 83 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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 84 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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 85 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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)

Aggregations

MiningBlock (org.aion.zero.impl.types.MiningBlock)185 Test (org.junit.Test)136 AionTransaction (org.aion.base.AionTransaction)128 ImportResult (org.aion.zero.impl.core.ImportResult)81 AionAddress (org.aion.types.AionAddress)79 BigInteger (java.math.BigInteger)64 Block (org.aion.zero.impl.types.Block)63 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)59 RepositoryCache (org.aion.base.db.RepositoryCache)41 AionTxReceipt (org.aion.base.AionTxReceipt)37 ArrayList (java.util.ArrayList)36 ECKey (org.aion.crypto.ECKey)27 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)27 AionTxExecSummary (org.aion.base.AionTxExecSummary)25 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)24 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 BlockContext (org.aion.zero.impl.types.BlockContext)18 DataWord (org.aion.util.types.DataWord)13 MockDB (org.aion.db.impl.mockdb.MockDB)12 StakingBlock (org.aion.zero.impl.types.StakingBlock)11