use of io.nuls.consensus.entity.member.Deposit in project nuls by nuls-io.
the class PocConsensusServiceImpl method joinTheConsensus.
private Transaction joinTheConsensus(Account account, String password, long amount, String agentHash) throws IOException, NulsException {
AssertUtil.canNotEmpty(account);
AssertUtil.canNotEmpty(password);
if (amount < PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT.getValue()) {
throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
}
AssertUtil.canNotEmpty(agentHash);
TransactionEvent event = new TransactionEvent();
Consensus<Deposit> ca = new ConsensusDepositImpl();
ca.setAddress(account.getAddress().toString());
Deposit deposit = new Deposit();
deposit.setAgentHash(agentHash);
deposit.setDeposit(Na.valueOf(amount));
deposit.setStartTime(TimeService.currentTimeMillis());
ca.setExtend(deposit);
CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_JOIN_CONSENSUS));
data.setTotalNa(deposit.getDeposit());
data.addFrom(account.getAddress().toString());
Coin coin = new Coin();
coin.setUnlockHeight(0);
coin.setUnlockTime(0);
coin.setNa(deposit.getDeposit());
data.addTo(account.getAddress().toString(), coin);
PocJoinConsensusTransaction tx = null;
try {
tx = new PocJoinConsensusTransaction(data, password);
} catch (NulsException e) {
throw new NulsRuntimeException(e);
}
tx.setTime(TimeService.currentTimeMillis());
tx.setTxData(ca);
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
tx.verifyWithException();
event.setEventBody(tx);
List<String> nodeList = eventBroadcaster.broadcastAndCache(event, true);
if (null == nodeList || nodeList.isEmpty()) {
throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
}
return tx;
}
use of io.nuls.consensus.entity.member.Deposit in project nuls by nuls-io.
the class ExitConsensusTxService method onApproval.
@Override
public void onApproval(PocExitConsensusTransaction tx) throws NulsException {
Transaction joinTx = ledgerService.getTx(tx.getTxData());
if (joinTx == null) {
joinTx = ConfirmingTxCacheManager.getInstance().getTx(tx.getTxData());
}
if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
manager.changeAgentStatusByHash(raTx.getTxData().getHexHash(), ConsensusStatusEnum.NOT_IN);
manager.changeDepositStatusByAgentHash(raTx.getTxData().getHexHash(), ConsensusStatusEnum.NOT_IN);
this.ledgerService.unlockTxApprove(tx.getTxData().getDigestHex());
return;
}
PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
Consensus<Deposit> cd = pjcTx.getTxData();
manager.changeDepositStatus(cd.getHexHash(), ConsensusStatusEnum.NOT_IN);
this.ledgerService.unlockTxApprove(tx.getTxData().getDigestHex());
}
use of io.nuls.consensus.entity.member.Deposit in project nuls by nuls-io.
the class ExitConsensusTxService method onCommit.
@Override
public void onCommit(PocExitConsensusTransaction tx) throws NulsException {
Transaction joinTx = ledgerService.getTx(tx.getTxData());
if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
manager.delAgent(raTx.getTxData().getHexHash());
manager.delDepositByAgentHash(raTx.getTxData().getHexHash());
this.ledgerService.unlockTxSave(tx.getTxData().getDigestHex());
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("agentHash", raTx.getTxData().getHexHash());
List<DepositPo> poList = depositDataService.getList(paramsMap);
for (DepositPo po : poList) {
this.ledgerService.unlockTxSave(po.getTxHash());
}
this.agentDataService.delete(raTx.getTxData().getHexHash());
this.depositDataService.deleteByAgentHash(raTx.getTxData().getHexHash());
return;
}
PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
Consensus<Deposit> cd = pjcTx.getTxData();
manager.delDeposit(cd.getHexHash());
this.depositDataService.delete(cd.getHexHash());
this.ledgerService.unlockTxSave(tx.getTxData().getDigestHex());
}
use of io.nuls.consensus.entity.member.Deposit in project nuls by nuls-io.
the class DepositAmountValidator method validate.
@Override
public ValidateResult validate(PocJoinConsensusTransaction data) {
Na limit = PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
Na max = PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
List<Consensus<Deposit>> list = consensusCacheManager.getCachedDepositListByAgentHash(data.getTxData().getExtend().getAgentHash());
if (list == null) {
return ValidateResult.getSuccessResult();
}
Na total = Na.ZERO;
for (Consensus<Deposit> cd : list) {
total = total.add(cd.getExtend().getDeposit());
}
if (limit.isGreaterThan(data.getTxData().getExtend().getDeposit())) {
return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_NOT_ENOUGH);
}
if (max.isLessThan(total)) {
return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_TOO_MUCH);
}
if (!data.getTxData().getExtend().getDeposit().equals(data.getCoinData().getTotalNa())) {
return ValidateResult.getFailedResult(SeverityLevelEnum.FLAGRANT_FOUL, ErrorCode.DEPOSIT_ERROR);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.consensus.entity.member.Deposit in project nuls by nuls-io.
the class ConsensusMeetingRunner method calcReward.
private List<ConsensusReward> calcReward(List<Transaction> txList, PocMeetingMember self) {
List<ConsensusReward> rewardList = new ArrayList<>();
Consensus<Agent> ca = self.getAgentConsensus();
if (ca.getExtend().getSeed()) {
long totalFee = 0;
for (Transaction tx : txList) {
totalFee += tx.getFee().getValue();
}
if (totalFee == 0L) {
return rewardList;
}
double caReward = totalFee;
ConsensusReward agentReword = new ConsensusReward();
agentReword.setAddress(ca.getAddress());
agentReword.setReward(Na.valueOf((long) caReward));
rewardList.add(agentReword);
return rewardList;
}
long totalFee = 0;
for (Transaction tx : txList) {
totalFee += tx.getFee().getValue();
}
double total = totalFee + DoubleUtils.mul(this.consensusManager.getCurrentRound().getMemberCount(), PocConsensusConstant.BLOCK_REWARD.getValue());
ConsensusReward agentReword = new ConsensusReward();
agentReword.setAddress(ca.getAddress());
double caReward = DoubleUtils.mul(total, DoubleUtils.div(ca.getExtend().getDeposit().getValue(), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()));
caReward = caReward + DoubleUtils.mul(total, DoubleUtils.mul(DoubleUtils.div((this.consensusManager.getCurrentRound().getTotalDeposit().getValue() - ca.getExtend().getDeposit().getValue()), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()), DoubleUtils.round(ca.getExtend().getCommissionRate() / 100, 2)));
agentReword.setReward(Na.valueOf((long) caReward));
Map<String, ConsensusReward> rewardMap = new HashMap<>();
rewardMap.put(ca.getAddress(), agentReword);
double delegateCommissionRate = DoubleUtils.div((100 - ca.getExtend().getCommissionRate()), 100, 2);
for (Consensus<Deposit> cd : self.getDelegateList()) {
double reward = DoubleUtils.mul(DoubleUtils.mul(total, delegateCommissionRate), DoubleUtils.div(cd.getExtend().getDeposit().getValue(), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()));
ConsensusReward delegateReword = rewardMap.get(cd.getAddress());
if (null == delegateReword) {
delegateReword = new ConsensusReward();
delegateReword.setReward(Na.ZERO);
}
delegateReword.setAddress(cd.getAddress());
delegateReword.setReward(delegateReword.getReward().add(Na.valueOf((long) reward)));
rewardMap.put(cd.getAddress(), delegateReword);
}
rewardList.addAll(rewardMap.values());
return rewardList;
}
Aggregations