Search in sources :

Example 61 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class UtxoLedgerTransactionStorageServiceImpl method getTx.

@Override
public Transaction getTx(NulsDigestData hash) {
    if (hash == null) {
        return null;
    }
    byte[] hashBytes = new byte[0];
    try {
        hashBytes = hash.serialize();
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    Transaction tx = dbService.getModel(LedgerStorageConstant.DB_NAME_LEDGER_TX, hashBytes, Transaction.class);
    if (tx != null) {
        tx.setHash(hash);
    }
    return tx;
}
Also used : NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException)

Example 62 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class RedPunishTxProcessor method onCommit.

@Override
public Result onCommit(RedPunishTransaction tx, Object secondaryData) {
    RedPunishData punishData = tx.getTxData();
    BlockHeader header = (BlockHeader) secondaryData;
    BlockExtendsData roundData = new BlockExtendsData(header.getExtend());
    PunishLogPo punishLogPo = new PunishLogPo();
    punishLogPo.setAddress(punishData.getAddress());
    punishLogPo.setHeight(tx.getBlockHeight());
    punishLogPo.setRoundIndex(roundData.getRoundIndex());
    punishLogPo.setTime(tx.getTime());
    punishLogPo.setType(PunishType.RED.getCode());
    punishLogPo.setEvidence(punishData.getEvidence());
    punishLogPo.setReasonCode(punishData.getReasonCode());
    List<AgentPo> agentList = agentStorageService.getList();
    AgentPo agent = null;
    for (AgentPo agentPo : agentList) {
        if (agentPo.getDelHeight() > 0) {
            continue;
        }
        if (Arrays.equals(agentPo.getAgentAddress(), punishLogPo.getAddress())) {
            agent = agentPo;
            break;
        }
    }
    if (null == agent) {
        Log.error("There is no agent can be punished.");
        return Result.getSuccess();
    }
    List<DepositPo> depositPoList = depositStorageService.getList();
    List<DepositPo> updatedList = new ArrayList<>();
    for (DepositPo po : depositPoList) {
        if (po.getDelHeight() >= 0) {
            continue;
        }
        if (!po.getAgentHash().equals(agent.getHash())) {
            continue;
        }
        po.setDelHeight(tx.getBlockHeight());
        boolean b = depositStorageService.save(po);
        if (!b) {
            for (DepositPo po2 : updatedList) {
                po2.setDelHeight(-1);
                this.depositStorageService.save(po2);
            }
            return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.UPDATE_DEPOSIT_FAILED);
        }
        updatedList.add(po);
    }
    boolean success = storageService.save(punishLogPo);
    if (!success) {
        for (DepositPo po2 : updatedList) {
            po2.setDelHeight(-1);
            this.depositStorageService.save(po2);
        }
        throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
    }
    AgentPo agentPo = agent;
    agentPo.setDelHeight(tx.getBlockHeight());
    success = agentStorageService.save(agentPo);
    if (!success) {
        for (DepositPo po2 : updatedList) {
            po2.setDelHeight(-1);
            this.depositStorageService.save(po2);
        }
        this.storageService.delete(punishLogPo.getKey());
        return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
    }
    return Result.getSuccess();
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 63 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class BaseChainTest method initChain.

protected void initChain() {
    chain = new Chain();
    // new a block header
    BlockHeader blockHeader = new BlockHeader();
    blockHeader.setHeight(0);
    blockHeader.setPreHash(NulsDigestData.calcDigestData("00000000000".getBytes()));
    blockHeader.setTime(1L);
    blockHeader.setTxCount(0);
    // 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);
    // chain.setEndBlockHeader(blockHeader);
    // // add the block into chain
    // chain.getBlockList().add(block);
    // chain.setStartBlockHeader(blockHeader);
    // chain.setEndBlockHeader(blockHeader);
    chain.addBlock(block);
    // init some agent
    List<Agent> agentList = new ArrayList<>();
    Transaction<Agent> agentTx = new CreateAgentTransaction();
    Agent agent = new Agent();
    agent.setPackingAddress(AddressTool.getAddress(ecKey.getPubKey()));
    agent.setAgentAddress(AddressTool.getAddress(new ECKey().getPubKey()));
    agent.setRewardAddress(AddressTool.getAddress(ecKey.getPubKey()));
    agent.setTime(System.currentTimeMillis());
    agent.setDeposit(Na.NA.multiply(20000));
    agent.setCommissionRate(0.3d);
    agent.setBlockHeight(blockHeader.getHeight());
    agentTx.setTxData(agent);
    agentTx.setTime(agent.getTime());
    agentTx.setBlockHeight(blockHeader.getHeight());
    NulsSignData signData = signDigest(agentTx.getHash().getDigestBytes(), ecKey);
    agentTx.setTransactionSignature(signData.getSignBytes());
    agentTx.getTxData().setTxHash(agentTx.getHash());
    // add the agent tx into agent list
    agentList.add(agentTx.getTxData());
    // set the agent list into chain
    chain.setAgentList(agentList);
    // new a deposit
    Deposit deposit = new Deposit();
    deposit.setAddress(AddressTool.getAddress(ecKey.getPubKey()));
    deposit.setAgentHash(agentTx.getHash());
    deposit.setTime(System.currentTimeMillis());
    deposit.setDeposit(Na.NA.multiply(200000));
    deposit.setBlockHeight(blockHeader.getHeight());
    DepositTransaction depositTx = new DepositTransaction();
    depositTx.setTime(deposit.getTime());
    depositTx.setTxData(deposit);
    depositTx.setBlockHeight(blockHeader.getHeight());
    List<Deposit> depositList = new ArrayList<>();
    depositList.add(depositTx.getTxData());
    chain.setDepositList(depositList);
    chain.setYellowPunishList(new ArrayList<>());
    chain.setRedPunishList(new ArrayList<>());
    chainContainer = new ChainContainer(chain);
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) ChainContainer(io.nuls.consensus.poc.container.ChainContainer) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) ECKey(io.nuls.core.tools.crypto.ECKey) IOException(java.io.IOException) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)

Example 64 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class BaseChainTest method newBlock.

protected Block newBlock(Block preBlock) {
    assertNotNull(preBlock);
    assertNotNull(preBlock.getHeader());
    BlockHeader blockHeader = new BlockHeader();
    blockHeader.setHeight(preBlock.getHeader().getHeight() + 1);
    blockHeader.setPreHash(preBlock.getHeader().getHash());
    blockHeader.setTxCount(1);
    MeetingRound round = chainContainer.initRound();
    BlockExtendsData nextRoundData = new BlockExtendsData();
    nextRoundData.setRoundIndex(round.getIndex() + 1);
    nextRoundData.setRoundStartTime(round.getEndTime());
    MeetingRound currentRound = chainContainer.getRoundManager().getNextRound(nextRoundData, false);
    MeetingMember member = currentRound.getMember(AddressTool.getAddress(ecKey.getPubKey()));
    blockHeader.setTime(member.getPackEndTime());
    // add a round data
    BlockRoundData roundData = new BlockRoundData(preBlock.getHeader().getExtend());
    roundData.setConsensusMemberCount(currentRound.getMemberCount());
    roundData.setPackingIndexOfRound(member.getPackingIndexOfRound());
    roundData.setRoundIndex(currentRound.getIndex());
    roundData.setRoundStartTime(currentRound.getStartTime());
    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);
    txs.add(new TestTransaction());
    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) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)

Example 65 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class ConsensusTool method createBlock.

public static Block createBlock(BlockData blockData, Account account) throws NulsException {
    if (null == account) {
        throw new NulsRuntimeException(AccountErrorCode.ACCOUNT_NOT_EXIST);
    }
    // 账户不能加密,否则抛错
    if (account.isEncrypted()) {
        throw new NulsRuntimeException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED);
    }
    Block block = new Block();
    block.setTxs(blockData.getTxList());
    BlockHeader header = new BlockHeader();
    block.setHeader(header);
    try {
        // block.getHeader().setExtend(ArraysTool.concatenate(blockData.getExtendsData().serialize(),new byte[]{0,1,0,1,1}));
        block.getHeader().setExtend(blockData.getExtendsData().serialize());
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    header.setHeight(blockData.getHeight());
    header.setTime(blockData.getTime());
    header.setPreHash(blockData.getPreHash());
    header.setTxCount(blockData.getTxList().size());
    List<NulsDigestData> txHashList = new ArrayList<>();
    for (int i = 0; i < blockData.getTxList().size(); i++) {
        Transaction tx = blockData.getTxList().get(i);
        tx.setBlockHeight(header.getHeight());
        txHashList.add(tx.getHash());
    }
    header.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
    header.setHash(NulsDigestData.calcDigestData(block.getHeader()));
    BlockSignature scriptSig = new BlockSignature();
    NulsSignData signData = accountService.signDigest(header.getHash().getDigestBytes(), account.getEcKey());
    scriptSig.setSignData(signData);
    scriptSig.setPublicKey(account.getPubKey());
    header.setBlockSignature(scriptSig);
    return block;
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) YellowPunishTransaction(io.nuls.consensus.poc.protocol.tx.YellowPunishTransaction) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) ContractTransaction(io.nuls.contract.entity.tx.ContractTransaction) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) BlockSignature(io.nuls.kernel.script.BlockSignature) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) SmallBlock(io.nuls.protocol.model.SmallBlock) IOException(java.io.IOException)

Aggregations

NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)68 IOException (java.io.IOException)35 NulsException (io.nuls.kernel.exception.NulsException)26 ArrayList (java.util.ArrayList)21 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)10 Result (io.nuls.kernel.model.Result)9 Account (io.nuls.account.model.Account)8 MultiSigAccount (io.nuls.account.model.MultiSigAccount)8 Entry (io.nuls.db.model.Entry)8 Agent (io.nuls.consensus.poc.protocol.entity.Agent)7 VarInt (io.nuls.kernel.utils.VarInt)7 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)5 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)5 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)5 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)5 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)5 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4