Search in sources :

Example 11 with DepositTransaction

use of io.nuls.consensus.poc.protocol.tx.DepositTransaction 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 12 with DepositTransaction

use of io.nuls.consensus.poc.protocol.tx.DepositTransaction in project nuls by nuls-io.

the class CancelDepositTxProcessor method onRollback.

@Override
public Result onRollback(CancelDepositTransaction tx, Object secondaryData) {
    DepositTransaction transaction = (DepositTransaction) ledgerService.getTx(tx.getTxData().getJoinTxHash());
    if (null == transaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
    }
    DepositPo po = depositStorageService.get(tx.getTxData().getJoinTxHash());
    if (null == po) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND);
    }
    if (po.getDelHeight() != tx.getBlockHeight()) {
        return Result.getFailed(PocConsensusErrorCode.DEPOSIT_NEVER_CANCELED);
    }
    po.setDelHeight(-1L);
    boolean b = depositStorageService.save(po);
    if (b) {
        return Result.getSuccess();
    }
    return Result.getFailed(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo)

Example 13 with DepositTransaction

use of io.nuls.consensus.poc.protocol.tx.DepositTransaction in project nuls by nuls-io.

the class CancelDepositTxProcessor method onCommit.

@Override
public Result onCommit(CancelDepositTransaction tx, Object secondaryData) {
    DepositTransaction transaction = (DepositTransaction) ledgerService.getTx(tx.getTxData().getJoinTxHash());
    if (null == transaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
    }
    DepositPo po = depositStorageService.get(tx.getTxData().getJoinTxHash());
    if (null == po) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND);
    }
    tx.getTxData().setAddress(po.getAddress());
    if (po.getDelHeight() > 0L) {
        return Result.getFailed(PocConsensusErrorCode.DEPOSIT_WAS_CANCELED);
    }
    po.setDelHeight(tx.getBlockHeight());
    boolean b = depositStorageService.save(po);
    if (b) {
        return Result.getSuccess();
    }
    return Result.getFailed(TransactionErrorCode.SAVE_TX_ERROR);
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo)

Aggregations

DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)13 CancelDepositTransaction (io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)9 MultiSigAccount (io.nuls.account.model.MultiSigAccount)7 CancelDeposit (io.nuls.consensus.poc.protocol.entity.CancelDeposit)7 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)7 Account (io.nuls.account.model.Account)6 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)5 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)5 VarInt (io.nuls.kernel.utils.VarInt)4 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)3 IOException (java.io.IOException)3 Agent (io.nuls.consensus.poc.protocol.entity.Agent)2 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)2 NulsException (io.nuls.kernel.exception.NulsException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ChainContainer (io.nuls.consensus.poc.container.ChainContainer)1 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)1 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)1 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)1 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)1