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;
}
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;
}
Aggregations