Search in sources :

Example 6 with BlockHeaderPo

use of io.nuls.protocol.storage.po.BlockHeaderPo in project nuls by nuls-io.

the class BlockHeaderStorageServiceImpl method getBlockHeaderPo.

/**
 * 根据区块hash查询区块头数据
 * Query block header data according to block hash.
 *
 * @param hashBytes 区块头摘要/block hash
 * @return BlockHeaderPo 区块头数据
 */
private BlockHeaderPo getBlockHeaderPo(byte[] hashBytes) {
    byte[] bytes = dbService.get(ProtocolStorageConstant.DB_NAME_BLOCK_HEADER, hashBytes);
    if (null == bytes) {
        return null;
    }
    BlockHeaderPo po = new BlockHeaderPo();
    try {
        po.parse(bytes, 0);
    } catch (NulsException e) {
        Log.error(e);
    }
    NulsDigestData hash = new NulsDigestData();
    try {
        hash.parse(hashBytes, 0);
    } catch (NulsException e) {
        Log.error(e);
    }
    po.setHash(hash);
    return po;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeaderPo(io.nuls.protocol.storage.po.BlockHeaderPo)

Example 7 with BlockHeaderPo

use of io.nuls.protocol.storage.po.BlockHeaderPo in project nuls by nuls-io.

the class BlockHeaderStorageServiceImplTest method removeBlockHerader.

public void removeBlockHerader() {
    service.removeBlockHerader(entity);
    BlockHeaderPo po = this.service.getBlockHeaderPo(entity.getHash());
    assertNull(po);
}
Also used : BlockHeaderPo(io.nuls.protocol.storage.po.BlockHeaderPo)

Example 8 with BlockHeaderPo

use of io.nuls.protocol.storage.po.BlockHeaderPo in project nuls by nuls-io.

the class BlockHeaderStorageServiceImplTest method getBlockPo1.

public void getBlockPo1() {
    BlockHeaderPo po = this.service.getBlockHeaderPo(entity.getHash());
    this.testEquals(po, entity);
}
Also used : BlockHeaderPo(io.nuls.protocol.storage.po.BlockHeaderPo)

Example 9 with BlockHeaderPo

use of io.nuls.protocol.storage.po.BlockHeaderPo 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 10 with BlockHeaderPo

use of io.nuls.protocol.storage.po.BlockHeaderPo in project nuls by nuls-io.

the class BlockServiceImpl method rollbackBlock.

/**
 * 回滚区块
 * roll back the block to the store.
 *
 * @param block 完整区块/whole block
 * @return 操作结果/operating result
 * @throws NulsException 回滚区块有可能出现异常,请捕获后谨慎处理/There may be exceptions to the roll back block, please handle it carefully after capture.
 */
@Override
public Result rollbackBlock(Block block) throws NulsException {
    if (null == block) {
        return Result.getFailed(ProtocolErroeCode.BLOCK_IS_NULL);
    }
    boolean txsResult = this.rollbackTxList(block.getTxs(), block.getHeader(), true);
    if (!txsResult) {
        return Result.getFailed();
    }
    BlockHeaderPo po = new BlockHeaderPo();
    po.setHash(block.getHeader().getHash());
    po.setHeight(block.getHeader().getHeight());
    po.setPreHash(block.getHeader().getPreHash());
    Result result = this.blockHeaderStorageService.removeBlockHerader(po);
    if (result.isFailed()) {
        return result;
    }
    try {
        accountLedgerService.rollbackTransactions(block.getTxs());
        // 回滚合约相关交易
        contractService.rollbackTransactionList(block.getTxs());
    } catch (Exception e) {
        Log.warn("rollbackTransaction local tx failed", e);
    }
    return result;
}
Also used : BlockHeaderPo(io.nuls.protocol.storage.po.BlockHeaderPo) NulsException(io.nuls.kernel.exception.NulsException) ContractResult(io.nuls.contract.dto.ContractResult)

Aggregations

BlockHeaderPo (io.nuls.protocol.storage.po.BlockHeaderPo)15 SmallBlock (io.nuls.protocol.model.SmallBlock)6 NulsException (io.nuls.kernel.exception.NulsException)2 NulsDigestData (io.nuls.kernel.model.NulsDigestData)2 ArrayList (java.util.ArrayList)2 ContractResult (io.nuls.contract.dto.ContractResult)1 LevelDbModuleBootstrap (io.nuls.db.module.impl.LevelDbModuleBootstrap)1 MicroKernelBootstrap (io.nuls.kernel.MicroKernelBootstrap)1 BlockSignature (io.nuls.kernel.script.BlockSignature)1 VarInt (io.nuls.kernel.utils.VarInt)1 BlockHeaderStorageService (io.nuls.protocol.storage.service.BlockHeaderStorageService)1 IOException (java.io.IOException)1 Before (org.junit.Before)1