use of io.nuls.kernel.script.BlockSignature in project nuls by nuls-io.
the class BlockHeaderStorageServiceImplTest method init.
@Before
public void init() {
MicroKernelBootstrap mk = MicroKernelBootstrap.getInstance();
mk.init();
mk.start();
LevelDbModuleBootstrap bootstrap = new LevelDbModuleBootstrap();
bootstrap.init();
bootstrap.start();
service = NulsContext.getServiceBean(BlockHeaderStorageService.class);
BlockHeaderPo po = new BlockHeaderPo();
po.setHash(NulsDigestData.calcDigestData("hashhash".getBytes()));
po.setHeight(1286L);
po.setExtend("extends".getBytes());
po.setMerkleHash(NulsDigestData.calcDigestData("merkleHash".getBytes()));
po.setPreHash(NulsDigestData.calcDigestData("prehash".getBytes()));
try {
po.setPackingAddress("address".getBytes());
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
po.setScriptSign(new BlockSignature());
po.setTime(12345678901L);
po.setTxCount(3);
List<NulsDigestData> txHashList = new ArrayList<>();
txHashList.add(NulsDigestData.calcDigestData("first-tx-hash".getBytes()));
txHashList.add(NulsDigestData.calcDigestData("second-tx-hash".getBytes()));
txHashList.add(NulsDigestData.calcDigestData("third-tx-hash".getBytes()));
po.setTxHashList(txHashList);
this.entity = po;
}
use of io.nuls.kernel.script.BlockSignature in project nuls by nuls-io.
the class BlockServiceImplTest method init.
@Before
public void init() {
MicroKernelBootstrap mk = MicroKernelBootstrap.getInstance();
mk.init();
mk.start();
LevelDbModuleBootstrap bootstrap = new LevelDbModuleBootstrap();
bootstrap.init();
bootstrap.start();
UtxoLedgerModuleBootstrap ledgerModuleBootstrap = new UtxoLedgerModuleBootstrap();
ledgerModuleBootstrap.init();
ledgerModuleBootstrap.start();
service = NulsContext.getServiceBean(BlockService.class);
Block block = new Block();
BlockHeader blockHeader = new BlockHeader();
blockHeader.setHash(NulsDigestData.calcDigestData("hashhash".getBytes()));
blockHeader.setHeight(1286L);
blockHeader.setExtend("extends".getBytes());
blockHeader.setMerkleHash(NulsDigestData.calcDigestData("merkleHash".getBytes()));
blockHeader.setPreHash(NulsDigestData.calcDigestData("prehash".getBytes()));
try {
blockHeader.setPackingAddress("address".getBytes());
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
blockHeader.setBlockSignature(new BlockSignature());
blockHeader.setTime(12345678901L);
blockHeader.setTxCount(3);
List<NulsDigestData> txHashList = new ArrayList<>();
txHashList.add(NulsDigestData.calcDigestData("first-tx-hash".getBytes()));
txHashList.add(NulsDigestData.calcDigestData("second-tx-hash".getBytes()));
txHashList.add(NulsDigestData.calcDigestData("third-tx-hash".getBytes()));
// block.setTxHashList(txHashList);
// this.model = blockHeader;
}
use of io.nuls.kernel.script.BlockSignature in project nuls by nuls-io.
the class BaseTest method createBlock.
protected Block createBlock() {
// new a block header
BlockHeader blockHeader = new BlockHeader();
blockHeader.setHeight(0);
blockHeader.setPreHash(NulsDigestData.calcDigestData("00000000000".getBytes()));
blockHeader.setTime(1L);
blockHeader.setTxCount(1);
blockHeader.setMerkleHash(NulsDigestData.calcDigestData(new byte[20]));
// add a round data
BlockRoundData roundData = new BlockRoundData();
roundData.setConsensusMemberCount(1);
roundData.setPackingIndexOfRound(1);
roundData.setRoundIndex(1);
roundData.setRoundStartTime(1L);
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);
Transaction tx = new TestTransaction();
txs.add(tx);
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 BlockHeaderPoTest method serializeAndParse.
/**
* 验证区块头实体的序列化和反序列化的正确性
* Verify the correctness of serialization and deserialization of block header entities.
*/
@Test
public void serializeAndParse() {
BlockHeaderPo po = new BlockHeaderPo();
po.setHeight(1286L);
po.setExtend("extends".getBytes());
po.setMerkleHash(NulsDigestData.calcDigestData("merkleHash".getBytes()));
try {
po.setPackingAddress("address".getBytes());
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
po.setScriptSign(new BlockSignature());
po.setTime(12345678901L);
po.setTxCount(3);
List<NulsDigestData> txHashList = new ArrayList<>();
txHashList.add(NulsDigestData.calcDigestData("first-tx-hash".getBytes()));
txHashList.add(NulsDigestData.calcDigestData("second-tx-hash".getBytes()));
txHashList.add(NulsDigestData.calcDigestData("third-tx-hash".getBytes()));
po.setTxHashList(txHashList);
byte[] bytes = new byte[0];
try {
bytes = po.serialize();
} catch (IOException e) {
Log.error(e);
}
BlockHeaderPo newPo = new BlockHeaderPo();
try {
newPo.parse(bytes, 0);
} catch (NulsException e) {
Log.error(e);
}
assertNull(newPo.getHash());
assertEquals(po.getHeight(), newPo.getHeight());
assertEquals(po.getPreHash(), newPo.getPreHash());
assertEquals(po.getMerkleHash(), newPo.getMerkleHash());
assertTrue(Arrays.equals(po.getExtend(), newPo.getExtend()));
assertTrue(Arrays.equals(po.getPackingAddress(), newPo.getPackingAddress()));
assertTrue(Arrays.equals(po.getScriptSign().getPublicKey(), newPo.getScriptSign().getPublicKey()));
assertEquals(po.getScriptSign().getSignData(), newPo.getScriptSign().getSignData());
assertEquals(po.getTime(), newPo.getTime());
assertEquals(po.getTxCount(), newPo.getTxCount());
assertEquals(po.getTxHashList().get(0), newPo.getTxHashList().get(0));
assertEquals(po.getTxHashList().get(1), newPo.getTxHashList().get(1));
assertEquals(po.getTxHashList().get(2), newPo.getTxHashList().get(2));
}
use of io.nuls.kernel.script.BlockSignature in project nuls by nuls-io.
the class GenesisBlock method fillHeader.
private void fillHeader(Map<String, Object> jsonMap) throws NulsException {
Integer height = (Integer) jsonMap.get(CONFIG_FILED_HEIGHT);
AssertUtil.canNotEmpty(height, KernelErrorCode.CONFIG_ERROR.getMsg());
BlockHeader header = new BlockHeader();
this.setHeader(header);
header.setHeight(height);
header.setTime(blockTime);
header.setPreHash(NulsDigestData.calcDigestData(new byte[35]));
header.setTxCount(this.getTxs().size());
List<NulsDigestData> txHashList = new ArrayList<>();
for (Transaction tx : this.getTxs()) {
txHashList.add(tx.getHash());
}
header.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
BlockExtendsData data = new BlockExtendsData();
data.setRoundIndex(1);
data.setRoundStartTime(header.getTime() - ProtocolConstant.BLOCK_TIME_INTERVAL_SECOND * 1000);
data.setConsensusMemberCount(1);
data.setPackingIndexOfRound(1);
try {
header.setExtend(data.serialize());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
header.setHash(NulsDigestData.calcDigestData(header));
BlockSignature p2PKHScriptSig = new BlockSignature();
NulsSignData signData = this.signature(header.getHash().getDigestBytes());
p2PKHScriptSig.setSignData(signData);
p2PKHScriptSig.setPublicKey(getGenesisPubkey());
header.setBlockSignature(p2PKHScriptSig);
}
Aggregations