use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class BlockMaintenanceThread method checkGenesisBlock.
public void checkGenesisBlock() throws Exception {
Block genesisBlock = NulsContext.getInstance().getGenesisBlock();
ValidateResult result = genesisBlock.verify();
if (result.isFailed()) {
throw new NulsRuntimeException(ErrorCode.DATA_ERROR, result.getMessage());
}
Block localGenesisBlock = this.blockService.getGengsisBlock();
if (null == localGenesisBlock) {
for (Transaction tx : genesisBlock.getTxs()) {
ledgerService.approvalTx(tx);
}
this.blockService.saveBlock(genesisBlock);
return;
}
localGenesisBlock.verify();
String logicHash = genesisBlock.getHeader().getHash().getDigestHex();
String localHash = localGenesisBlock.getHeader().getHash().getDigestHex();
if (!logicHash.equals(localHash)) {
throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
}
}
use of io.nuls.core.chain.entity.Transaction 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;
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class ConsensusMeetingRunner method packing.
private void packing(PocMeetingMember self) throws NulsException, IOException {
Block bestBlock = context.getBestBlock();
List<Transaction> txList = txCacheManager.getTxList();
txList.sort(TxTimeComparator.getInstance());
BlockData bd = new BlockData();
bd.setHeight(bestBlock.getHeader().getHeight() + 1);
bd.setPreHash(bestBlock.getHeader().getHash());
BlockRoundData roundData = new BlockRoundData();
roundData.setRoundIndex(consensusManager.getCurrentRound().getIndex());
roundData.setConsensusMemberCount(consensusManager.getCurrentRound().getMemberCount());
roundData.setPackingIndexOfRound(self.getIndexOfRound());
roundData.setRoundStartTime(consensusManager.getCurrentRound().getStartTime());
bd.setRoundData(roundData);
List<Integer> outTxList = new ArrayList<>();
List<NulsDigestData> outHashList = new ArrayList<>();
List<NulsDigestData> hashList = new ArrayList<>();
long totalSize = 0L;
for (int i = 0; i < txList.size(); i++) {
if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
break;
}
Transaction tx = txList.get(i);
totalSize += tx.size();
if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
break;
}
outHashList.add(tx.getHash());
ValidateResult result = tx.verify();
if (result.isFailed()) {
Log.error(result.getMessage());
outTxList.add(i);
continue;
}
try {
ledgerService.approvalTx(tx);
} catch (Exception e) {
Log.error(e);
outTxList.add(i);
continue;
}
confirmingTxCacheManager.putTx(tx);
}
txCacheManager.removeTx(hashList);
for (int i = outTxList.size() - 1; i >= 0; i--) {
txList.remove(i);
}
txCacheManager.removeTx(outHashList);
if (totalSize < PocConsensusConstant.MAX_BLOCK_SIZE) {
addOrphanTx(txList, totalSize, self);
}
addConsensusTx(bestBlock, txList, self);
bd.setTxList(txList);
Log.info("txCount:" + txList.size());
Block newBlock = ConsensusTool.createBlock(bd, consensusManager.getConsensusStatusInfo().getAccount());
ValidateResult result = newBlock.verify();
if (result.isFailed()) {
Log.warn("packing block error:" + result.getMessage());
for (Transaction tx : newBlock.getTxs()) {
if (tx.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
continue;
}
ledgerService.rollbackTx(tx);
}
return;
}
confirmingTxCacheManager.putTx(newBlock.getTxs().get(0));
blockManager.addBlock(newBlock, false, null);
BlockHeaderEvent event = new BlockHeaderEvent();
event.setEventBody(newBlock.getHeader());
eventBroadcaster.broadcastAndCache(event, false);
PackedBlockNotice notice = new PackedBlockNotice();
notice.setEventBody(newBlock.getHeader());
eventBroadcaster.publishToLocal(notice);
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class BlockStorageService method txListGrouping.
private Map<Long, List<Transaction>> txListGrouping(List<Transaction> txList) {
Map<Long, List<Transaction>> map = new HashMap<>();
for (Transaction tx : txList) {
List<Transaction> list = map.get(tx.getBlockHeight());
if (null == list) {
list = new ArrayList<>();
}
list.add(tx);
map.put(tx.getBlockHeight(), list);
}
return map;
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class BlockStorageService method getBlock.
public Block getBlock(long height) throws Exception {
Block block = blockCacheManager.getBlock(height);
if (null != block && block.getTxs().size() == block.getHeader().getTxCount()) {
return block;
}
BlockHeader header = getBlockHeader(height);
if (null == header) {
return null;
}
List<Transaction> txList = null;
try {
txList = ledgerService.getTxList(height);
} catch (Exception e) {
Log.error(e);
}
if (header.getTxCount() != txList.size()) {
Log.warn("block has wrong tx size!");
}
return fillBlock(header, txList);
}
Aggregations