Search in sources :

Example 26 with BlockHeader

use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.

the class RoundManager method getNextRoundByExpectedRound.

private MeetingRound getNextRoundByExpectedRound(BlockExtendsData roundData) {
    BlockHeader startBlockHeader = chain.getEndBlockHeader();
    long roundIndex = roundData.getRoundIndex();
    long roundStartTime = roundData.getRoundStartTime();
    if (startBlockHeader.getHeight() != 0L) {
        // if(roundData.getConsensusMemberCount() == roundData.getPackingIndexOfRound()) {
        // roundIndex += 1;
        // roundStartTime = roundData.getRoundEndTime();
        // }
        startBlockHeader = getFirstBlockHeightOfPreRoundByRoundIndex(roundIndex);
    }
    return calculationRound(startBlockHeader, roundIndex, roundStartTime);
}
Also used : BlockHeader(io.nuls.kernel.model.BlockHeader)

Example 27 with BlockHeader

use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.

the class RoundManager method getBlockCountMap.

private Map<String, Integer> getBlockCountMap(long roundStart, long roundEnd) {
    List<BlockHeader> blockHeaderList = chain.getAllBlockHeaderList();
    Map<String, Integer> map = new HashMap<>();
    for (int i = blockHeaderList.size() - 1; i >= 0; i--) {
        BlockHeader blockHeader = blockHeaderList.get(i);
        BlockExtendsData roundData = new BlockExtendsData(blockHeader.getExtend());
        if (roundData.getRoundIndex() > roundEnd) {
            continue;
        }
        if (roundData.getRoundIndex() < roundStart) {
            break;
        }
        Integer count = map.get(blockHeader.getPackingAddressStr());
        if (null == count) {
            count = 0;
        }
        map.put(blockHeader.getPackingAddressStr(), count + 1);
    }
    return map;
}
Also used : BlockHeader(io.nuls.kernel.model.BlockHeader)

Example 28 with BlockHeader

use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.

the class Chain method rollbackBlock.

public BlockHeader rollbackBlock() {
    blockList.remove(blockList.size() - 1);
    BlockHeader header;
    if (blockHeaderList.size() == 1) {
        header = blockHeaderList.remove(blockHeaderList.size() - 1);
        this.startBlockHeader = null;
        this.endBlockHeader = null;
    } else {
        BlockHeader preHeader = blockHeaderList.get(blockHeaderList.size() - 2);
        header = blockHeaderList.remove(blockHeaderList.size() - 1);
        if (header.getPreHash().equals(preHeader.getHash())) {
            this.endBlockHeader = preHeader;
        } else {
            this.endBlockHeader = blockHeaderList.get(blockHeaderList.size() - 1);
        }
    }
    return header;
}
Also used : BlockHeader(io.nuls.kernel.model.BlockHeader)

Example 29 with BlockHeader

use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.

the class VMContext method getBlockHeader.

/**
 * @param height
 * @return
 * @throws NulsException
 * @throws IOException
 */
public BlockHeaderDto getBlockHeader(long height) throws NulsException, IOException {
    if (height < 0L) {
        return null;
    }
    Result<BlockHeader> blockHeaderResult = blockService.getBlockHeader(height);
    if (blockHeaderResult == null || blockHeaderResult.getData() == null) {
        return null;
    }
    BlockHeaderDto header = new BlockHeaderDto(blockHeaderResult.getData());
    return header;
}
Also used : BlockHeader(io.nuls.kernel.model.BlockHeader) BlockHeaderDto(io.nuls.contract.entity.BlockHeaderDto)

Example 30 with BlockHeader

use of io.nuls.kernel.model.BlockHeader in project nuls by nuls-io.

the class CreateContractTxProcessor method onCommit.

@Override
public Result onCommit(CreateContractTransaction tx, Object secondaryData) {
    ContractResult contractResult = tx.getContractResult();
    contractService.saveContractExecuteResult(tx.getHash(), contractResult);
    CreateContractData txData = tx.getTxData();
    byte[] contractAddress = txData.getContractAddress();
    byte[] sender = txData.getSender();
    String senderStr = AddressTool.getStringAddressByBytes(sender);
    String contractAddressStr = AddressTool.getStringAddressByBytes(contractAddress);
    // 移除未确认的创建合约交易
    contractTxService.removeLocalUnconfirmedCreateContractTransaction(senderStr, contractAddressStr, contractResult);
    // 执行失败的合约直接返回
    if (!contractResult.isSuccess()) {
        return Result.getSuccess();
    }
    NulsDigestData hash = tx.getHash();
    long blockHeight = tx.getBlockHeight();
    long bestBlockHeight = NulsContext.getInstance().getBestHeight();
    ContractAddressInfoPo info = new ContractAddressInfoPo();
    info.setContractAddress(contractAddress);
    info.setSender(sender);
    try {
        info.setCreateTxHash(hash.serialize());
    } catch (IOException e) {
        throw new NulsRuntimeException(e);
    }
    info.setCreateTime(tx.getTime());
    info.setBlockHeight(blockHeight);
    // byte[] stateRoot = contractResult.getStateRoot();
    boolean isNrc20Contract = contractResult.isNrc20();
    boolean acceptDirectTransfer = contractResult.isAcceptDirectTransfer();
    info.setAcceptDirectTransfer(acceptDirectTransfer);
    info.setNrc20(isNrc20Contract);
    // 获取 token tracker
    if (isNrc20Contract) {
        BlockHeader blockHeader = tx.getBlockHeader();
        byte[] newestStateRoot = blockHeader.getStateRoot();
        // NRC20 token 标准方法获取名称数据
        ProgramResult programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_NAME, null, null);
        if (programResult.isSuccess()) {
            String tokenName = programResult.getResult();
            info.setNrc20TokenName(tokenName);
        }
        programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_SYMBOL, null, null);
        if (programResult.isSuccess()) {
            String symbol = programResult.getResult();
            info.setNrc20TokenSymbol(symbol);
        }
        programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_DECIMALS, null, null);
        if (programResult.isSuccess()) {
            String decimals = programResult.getResult();
            if (StringUtils.isNotBlank(decimals)) {
                try {
                    info.setDecimals(new BigInteger(decimals).longValue());
                } catch (Exception e) {
                    Log.error("Get nrc20 decimals error.", e);
                // skip it
                }
            }
        }
        programResult = vmHelper.invokeViewMethod(newestStateRoot, bestBlockHeight, contractAddress, NRC20_METHOD_TOTAL_SUPPLY, null, null);
        if (programResult.isSuccess()) {
            String totalSupply = programResult.getResult();
            if (StringUtils.isNotBlank(totalSupply)) {
                try {
                    info.setTotalSupply(new BigInteger(totalSupply));
                } catch (Exception e) {
                    Log.error("Get nrc20 totalSupply error.", e);
                // skip it
                }
            }
        }
        // 刷新创建者的token余额
        vmHelper.refreshTokenBalance(newestStateRoot, info, senderStr, contractAddressStr);
        // 处理合约事件
        vmHelper.dealEvents(newestStateRoot, tx, contractResult, info);
    }
    Result result = contractAddressStorageService.saveContractAddress(contractAddress, info);
    return result;
}
Also used : ContractResult(io.nuls.contract.dto.ContractResult) ProgramResult(io.nuls.contract.vm.program.ProgramResult) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) ContractResult(io.nuls.contract.dto.ContractResult) ValidateResult(io.nuls.kernel.validate.ValidateResult) ProgramResult(io.nuls.contract.vm.program.ProgramResult) Result(io.nuls.kernel.model.Result) ContractAddressInfoPo(io.nuls.contract.storage.po.ContractAddressInfoPo) CreateContractData(io.nuls.contract.entity.txdata.CreateContractData) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BigInteger(java.math.BigInteger) BlockHeader(io.nuls.kernel.model.BlockHeader)

Aggregations

BlockHeader (io.nuls.kernel.model.BlockHeader)40 Block (io.nuls.kernel.model.Block)8 BlockExtendsData (io.nuls.consensus.poc.model.BlockExtendsData)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)7 NulsDigestData (io.nuls.kernel.model.NulsDigestData)7 ArrayList (java.util.ArrayList)7 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)5 Result (io.nuls.kernel.model.Result)5 ValidateResult (io.nuls.kernel.validate.ValidateResult)4 IOException (java.io.IOException)4 ChainContainer (io.nuls.consensus.poc.container.ChainContainer)3 Agent (io.nuls.consensus.poc.protocol.entity.Agent)3 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)3 ProtocolContainer (io.nuls.protocol.base.version.ProtocolContainer)3 Chain (io.nuls.consensus.poc.model.Chain)2 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)2 YellowPunishData (io.nuls.consensus.poc.protocol.entity.YellowPunishData)2 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)2 ContractResult (io.nuls.contract.dto.ContractResult)2 BlockHeaderDto (io.nuls.contract.entity.BlockHeaderDto)2