Search in sources :

Example 1 with BlockSignature

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;
}
Also used : LevelDbModuleBootstrap(io.nuls.db.module.impl.LevelDbModuleBootstrap) BlockSignature(io.nuls.kernel.script.BlockSignature) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeaderStorageService(io.nuls.protocol.storage.service.BlockHeaderStorageService) BlockHeaderPo(io.nuls.protocol.storage.po.BlockHeaderPo) MicroKernelBootstrap(io.nuls.kernel.MicroKernelBootstrap) Before(org.junit.Before)

Example 2 with BlockSignature

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;
}
Also used : LevelDbModuleBootstrap(io.nuls.db.module.impl.LevelDbModuleBootstrap) BlockSignature(io.nuls.kernel.script.BlockSignature) UtxoLedgerModuleBootstrap(io.nuls.ledger.module.impl.UtxoLedgerModuleBootstrap) ArrayList(java.util.ArrayList) BlockService(io.nuls.protocol.service.BlockService) Block(io.nuls.kernel.model.Block) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeader(io.nuls.kernel.model.BlockHeader) MicroKernelBootstrap(io.nuls.kernel.MicroKernelBootstrap) Before(org.junit.Before)

Example 3 with BlockSignature

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;
}
Also used : BlockSignature(io.nuls.kernel.script.BlockSignature) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) BlockRoundData(io.nuls.consensus.poc.model.BlockRoundData)

Example 4 with BlockSignature

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));
}
Also used : BlockSignature(io.nuls.kernel.script.BlockSignature) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.kernel.model.NulsDigestData) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with BlockSignature

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);
}
Also used : BigInteger(java.math.BigInteger) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) BlockSignature(io.nuls.kernel.script.BlockSignature) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) 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