Search in sources :

Example 1 with BlockRoundData

use of io.nuls.consensus.entity.block.BlockRoundData in project nuls by nuls-io.

the class ConsensusMeetingRunner method calcRound.

private PocMeetingRound calcRound() {
    PocMeetingRound round = new PocMeetingRound(this.consensusManager.getCurrentRound());
    Block bestBlock = context.getBestBlock();
    BlockRoundData lastRoundData;
    try {
        lastRoundData = new BlockRoundData(bestBlock.getHeader().getExtend());
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    if (round.getPreviousRound() == null || round.getPreviousRound().getIndex() <= lastRoundData.getRoundIndex()) {
        while (true) {
            if (lastRoundData.getPackingIndexOfRound() == lastRoundData.getConsensusMemberCount() || lastRoundData.getRoundEndTime() <= TimeService.currentTimeMillis()) {
                break;
            }
            try {
                bestBlock = context.getBestBlock();
                lastRoundData = new BlockRoundData(bestBlock.getHeader().getExtend());
            } catch (NulsException e) {
                Log.error(e);
            }
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                Log.error(e);
            }
        }
        PocMeetingRound preRound = new PocMeetingRound(null);
        preRound.setIndex(lastRoundData.getRoundIndex());
        preRound.setStartTime(lastRoundData.getRoundStartTime());
        preRound.setMemberCount(lastRoundData.getConsensusMemberCount());
        round.setPreviousRound(preRound);
    }
    round.setStartTime(round.getPreviousRound().getEndTime());
    round.setIndex(lastRoundData.getRoundIndex() + 1);
    return round;
}
Also used : NulsException(io.nuls.core.exception.NulsException) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) Block(io.nuls.core.chain.entity.Block) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 2 with BlockRoundData

use of io.nuls.consensus.entity.block.BlockRoundData in project nuls by nuls-io.

the class HeaderContinuityValidator method validate.

@Override
public ValidateResult validate(BlockHeader header) {
    ValidateResult result = ValidateResult.getSuccessResult();
    boolean failed = false;
    do {
        if (header.getHeight() == 0) {
            failed = !header.getPreHash().equals(NulsDigestData.EMPTY_HASH);
            break;
        }
        BlockHeader preHeader = null;
        try {
            preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getHeight() - 1);
        } catch (NulsException e) {
            // todo
            e.printStackTrace();
        }
        if (null == preHeader) {
            break;
        }
        failed = !preHeader.getHash().equals(header.getPreHash());
        if (failed) {
            break;
        }
        BlockRoundData roundData = new BlockRoundData();
        try {
            roundData.parse(header.getExtend());
        } catch (NulsException e) {
            Log.error(e);
        }
        long shouldTime = roundData.getRoundStartTime() + roundData.getPackingIndexOfRound() * PocConsensusConstant.BLOCK_TIME_INTERVAL_SECOND * 1000;
        // todo 3 seconds error
        long difference = header.getTime() - shouldTime;
        failed = difference > 3000 || difference < -3000;
        if (failed) {
            break;
        }
    } while (false);
    if (failed) {
        result = ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return result;
}
Also used : NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 3 with BlockRoundData

use of io.nuls.consensus.entity.block.BlockRoundData in project nuls by nuls-io.

the class HeaderPackerValidator method validate.

@Override
public ValidateResult validate(BlockHeader header) {
    BlockHeader preHeader = null;
    try {
        preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getPreHash());
    } catch (NulsException e) {
    // todo
    }
    PocMeetingRound currentRound = consensusManager.getCurrentRound();
    if (header.getHeight() == 0) {
        return ValidateResult.getSuccessResult();
    }
    if (preHeader == null) {
        return ValidateResult.getSuccessResult();
    }
    BlockRoundData roundData = null;
    try {
        roundData = new BlockRoundData(preHeader.getExtend());
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(e.getMessage());
    }
    if (null == currentRound) {
        return ValidateResult.getSuccessResult();
    }
    if (currentRound.getIndex() == roundData.getRoundIndex() && currentRound.getMemberCount() > (roundData.getPackingIndexOfRound() + 1)) {
        if (currentRound.indexOf(header.getPackingAddress()) <= roundData.getPackingIndexOfRound()) {
            return ValidateResult.getFailedResult(ERROR_MESSAGE);
        }
    }
    byte[] packingHash160 = AccountTool.getHash160ByAddress(header.getPackingAddress());
    byte[] hash160InScriptSig = header.getScriptSig().getSignerHash160();
    if (!Arrays.equals(packingHash160, hash160InScriptSig)) {
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    BlockRoundData nowRoundData = null;
    try {
        nowRoundData = new BlockRoundData(header.getExtend());
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    if (!isAdjacent(roundData, nowRoundData)) {
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return ValidateResult.getSuccessResult();
}
Also used : NulsException(io.nuls.core.exception.NulsException) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 4 with BlockRoundData

use of io.nuls.consensus.entity.block.BlockRoundData in project nuls by nuls-io.

the class GenesisBlock method fillHeader.

private void fillHeader(Map<String, Object> jsonMap) {
    Integer height = (Integer) jsonMap.get(CONFIG_FILED_HEIGHT);
    AssertUtil.canNotEmpty(height, ErrorCode.CONFIG_ERROR);
    BlockHeader header = new BlockHeader();
    this.setHeader(header);
    header.setHeight(height);
    header.setTime(blockTime);
    header.setPreHash(NulsDigestData.EMPTY_HASH);
    header.setTxCount(this.getTxs().size());
    List<NulsDigestData> txHashList = new ArrayList<>();
    for (Transaction tx : this.getTxs()) {
        txHashList.add(tx.getHash());
    }
    header.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
    BlockRoundData data = new BlockRoundData();
    data.setRoundIndex(1);
    data.setRoundStartTime(header.getTime());
    data.setConsensusMemberCount(1);
    data.setPackingIndexOfRound(1);
    try {
        header.setExtend(data.serialize());
    } catch (IOException e) {
        Log.error(e);
    }
    header.setPackingAddress(address);
    header.setHash(NulsDigestData.calcDigestData(header));
    P2PKHScriptSig p2PKHScriptSig = new P2PKHScriptSig();
    NulsSignData signData = this.signature(header.getHash().getDigestBytes());
    p2PKHScriptSig.setSignData(signData);
    p2PKHScriptSig.setPublicKey(getGenesisPubkey());
    header.setScriptSig(p2PKHScriptSig);
}
Also used : BigInteger(java.math.BigInteger) P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with BlockRoundData

use of io.nuls.consensus.entity.block.BlockRoundData in project nuls by nuls-io.

the class ConsensusMeetingRunner method yellowPunishTx.

private void yellowPunishTx(Block bestBlock, List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
    BlockRoundData lastBlockRoundData = new BlockRoundData();
    try {
        lastBlockRoundData.parse(bestBlock.getHeader().getExtend());
    } catch (NulsException e) {
        Log.error(e);
    }
    // continuous blocks in the same round
    boolean ok = (self.getRoundIndex() == lastBlockRoundData.getRoundIndex()) && (self.getIndexOfRound() == (1 + lastBlockRoundData.getPackingIndexOfRound()));
    // continuous blocks between two rounds
    ok = ok || (self.getRoundIndex() == (lastBlockRoundData.getRoundIndex() + 1) && self.getIndexOfRound() == 1 && lastBlockRoundData.getPackingIndexOfRound() == lastBlockRoundData.getConsensusMemberCount());
    // two rounds
    ok = ok || (self.getRoundIndex() - 1) > lastBlockRoundData.getRoundIndex();
    if (ok) {
        return;
    }
    List<Address> addressList = new ArrayList<>();
    PocMeetingRound round = consensusManager.getCurrentRound();
    long roundIndex = lastBlockRoundData.getRoundIndex();
    int packingIndex = 0;
    if (lastBlockRoundData.getPackingIndexOfRound() == lastBlockRoundData.getConsensusMemberCount()) {
        packingIndex = 1;
    } else {
        packingIndex = lastBlockRoundData.getPackingIndexOfRound() + 1;
    }
    while (true) {
        PocMeetingRound tempRound;
        if (roundIndex == self.getRoundIndex()) {
            tempRound = round;
        } else if (roundIndex == (self.getRoundIndex() - 1)) {
            tempRound = round.getPreviousRound();
        } else {
            break;
        }
        if (tempRound.getIndex() > round.getIndex()) {
            break;
        }
        if (tempRound.getIndex() == round.getIndex() && packingIndex >= self.getIndexOfRound()) {
            break;
        }
        if (packingIndex >= tempRound.getMemberCount()) {
            roundIndex++;
            packingIndex = 1;
            continue;
        }
        PocMeetingMember member;
        try {
            member = tempRound.getMember(packingIndex);
            if (null == member) {
                break;
            }
        } catch (Exception e) {
            break;
        }
        packingIndex++;
        addressList.add(Address.fromHashs(member.getAgentAddress()));
    }
    if (addressList.isEmpty()) {
        return;
    }
    YellowPunishTransaction punishTx = new YellowPunishTransaction();
    YellowPunishData data = new YellowPunishData();
    data.setAddressList(addressList);
    data.setHeight(bestBlock.getHeader().getHeight() + 1);
    punishTx.setTxData(data);
    punishTx.setTime(TimeService.currentTimeMillis());
    punishTx.setFee(Na.ZERO);
    punishTx.setHash(NulsDigestData.calcDigestData(punishTx));
    punishTx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(punishTx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
    txList.add(punishTx);
}
Also used : Address(io.nuls.account.entity.Address) YellowPunishTransaction(io.nuls.consensus.entity.tx.YellowPunishTransaction) YellowPunishData(io.nuls.consensus.entity.YellowPunishData) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) PocMeetingMember(io.nuls.consensus.entity.meeting.PocMeetingMember) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData)

Aggregations

BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)8 NulsException (io.nuls.core.exception.NulsException)7 IOException (java.io.IOException)4 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)3 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)3 ValidateResult (io.nuls.core.validate.ValidateResult)3 YellowPunishTransaction (io.nuls.consensus.entity.tx.YellowPunishTransaction)2 Block (io.nuls.core.chain.entity.Block)2 BlockHeader (io.nuls.core.chain.entity.BlockHeader)2 Transaction (io.nuls.core.chain.entity.Transaction)2 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)2 Address (io.nuls.account.entity.Address)1 YellowPunishData (io.nuls.consensus.entity.YellowPunishData)1 BlockData (io.nuls.consensus.entity.block.BlockData)1 PocMeetingMember (io.nuls.consensus.entity.meeting.PocMeetingMember)1 RedPunishTransaction (io.nuls.consensus.entity.tx.RedPunishTransaction)1 BlockHeaderEvent (io.nuls.consensus.event.BlockHeaderEvent)1 PackedBlockNotice (io.nuls.consensus.event.notice.PackedBlockNotice)1 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)1 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)1