Search in sources :

Example 6 with BlockSignature

use of io.nuls.kernel.script.BlockSignature in project nuls by nuls-io.

the class BaseChainTest method newBlock.

protected Block newBlock(Block preBlock) {
    assertNotNull(preBlock);
    assertNotNull(preBlock.getHeader());
    BlockHeader blockHeader = new BlockHeader();
    blockHeader.setHeight(preBlock.getHeader().getHeight() + 1);
    blockHeader.setPreHash(preBlock.getHeader().getHash());
    blockHeader.setTxCount(1);
    MeetingRound round = chainContainer.initRound();
    BlockExtendsData nextRoundData = new BlockExtendsData();
    nextRoundData.setRoundIndex(round.getIndex() + 1);
    nextRoundData.setRoundStartTime(round.getEndTime());
    MeetingRound currentRound = chainContainer.getRoundManager().getNextRound(nextRoundData, false);
    MeetingMember member = currentRound.getMember(AddressTool.getAddress(ecKey.getPubKey()));
    blockHeader.setTime(member.getPackEndTime());
    // add a round data
    BlockRoundData roundData = new BlockRoundData(preBlock.getHeader().getExtend());
    roundData.setConsensusMemberCount(currentRound.getMemberCount());
    roundData.setPackingIndexOfRound(member.getPackingIndexOfRound());
    roundData.setRoundIndex(currentRound.getIndex());
    roundData.setRoundStartTime(currentRound.getStartTime());
    try {
        blockHeader.setExtend(roundData.serialize());
    } catch (IOException e) {
        throw new NulsRuntimeException(e);
    }
    // new a block of height 0
    Block block = new Block();
    block.setHeader(blockHeader);
    List<Transaction> txs = new ArrayList<>();
    block.setTxs(txs);
    txs.add(new TestTransaction());
    List<NulsDigestData> txHashList = block.getTxHashList();
    blockHeader.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
    NulsSignData signData = signDigest(blockHeader.getHash().getDigestBytes(), ecKey);
    BlockSignature sig = new BlockSignature();
    sig.setSignData(signData);
    sig.setPublicKey(ecKey.getPubKey());
    blockHeader.setBlockSignature(sig);
    return block;
}
Also used : BlockSignature(io.nuls.kernel.script.BlockSignature) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)

Example 7 with BlockSignature

use of io.nuls.kernel.script.BlockSignature in project nuls by nuls-io.

the class ConsensusTool method createBlock.

public static Block createBlock(BlockData blockData, Account account) throws NulsException {
    if (null == account) {
        throw new NulsRuntimeException(AccountErrorCode.ACCOUNT_NOT_EXIST);
    }
    // 账户不能加密,否则抛错
    if (account.isEncrypted()) {
        throw new NulsRuntimeException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED);
    }
    Block block = new Block();
    block.setTxs(blockData.getTxList());
    BlockHeader header = new BlockHeader();
    block.setHeader(header);
    try {
        // block.getHeader().setExtend(ArraysTool.concatenate(blockData.getExtendsData().serialize(),new byte[]{0,1,0,1,1}));
        block.getHeader().setExtend(blockData.getExtendsData().serialize());
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    header.setHeight(blockData.getHeight());
    header.setTime(blockData.getTime());
    header.setPreHash(blockData.getPreHash());
    header.setTxCount(blockData.getTxList().size());
    List<NulsDigestData> txHashList = new ArrayList<>();
    for (int i = 0; i < blockData.getTxList().size(); i++) {
        Transaction tx = blockData.getTxList().get(i);
        tx.setBlockHeight(header.getHeight());
        txHashList.add(tx.getHash());
    }
    header.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
    header.setHash(NulsDigestData.calcDigestData(block.getHeader()));
    BlockSignature scriptSig = new BlockSignature();
    NulsSignData signData = accountService.signDigest(header.getHash().getDigestBytes(), account.getEcKey());
    scriptSig.setSignData(signData);
    scriptSig.setPublicKey(account.getPubKey());
    header.setBlockSignature(scriptSig);
    return block;
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) YellowPunishTransaction(io.nuls.consensus.poc.protocol.tx.YellowPunishTransaction) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) ContractTransaction(io.nuls.contract.entity.tx.ContractTransaction) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) BlockSignature(io.nuls.kernel.script.BlockSignature) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) SmallBlock(io.nuls.protocol.model.SmallBlock) IOException(java.io.IOException)

Aggregations

BlockSignature (io.nuls.kernel.script.BlockSignature)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)4 NulsDigestData (io.nuls.kernel.model.NulsDigestData)3 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)2 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)2 LevelDbModuleBootstrap (io.nuls.db.module.impl.LevelDbModuleBootstrap)2 MicroKernelBootstrap (io.nuls.kernel.MicroKernelBootstrap)2 CoinBaseTransaction (io.nuls.protocol.model.tx.CoinBaseTransaction)2 Before (org.junit.Before)2 BlockExtendsData (io.nuls.consensus.poc.model.BlockExtendsData)1 BlockRoundData (io.nuls.consensus.poc.model.BlockRoundData)1 YellowPunishTransaction (io.nuls.consensus.poc.protocol.tx.YellowPunishTransaction)1 ContractTransaction (io.nuls.contract.entity.tx.ContractTransaction)1 NulsException (io.nuls.kernel.exception.NulsException)1 Block (io.nuls.kernel.model.Block)1 BlockHeader (io.nuls.kernel.model.BlockHeader)1 UtxoLedgerModuleBootstrap (io.nuls.ledger.module.impl.UtxoLedgerModuleBootstrap)1 SmallBlock (io.nuls.protocol.model.SmallBlock)1