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);
}
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);
}
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);
}
Aggregations