diff --git a/chainbase/src/main/java/org/tron/core/service/MortgageService.java b/chainbase/src/main/java/org/tron/core/service/MortgageService.java index f123104cf4b..f6875dbb68c 100644 --- a/chainbase/src/main/java/org/tron/core/service/MortgageService.java +++ b/chainbase/src/main/java/org/tron/core/service/MortgageService.java @@ -81,6 +81,13 @@ public void payBlockReward(byte[] witnessAddress, long value) { payReward(witnessAddress, value); } + public void payTransactionFeeReward(byte[] witnessAddress, long value) { + logger.debug("pay {} transaction fee reward {}", Hex.toHexString(witnessAddress), value); + long cycle = dynamicPropertiesStore.getCurrentCycleNumber(); + delegationStore.addTransactionFeeReward(cycle, witnessAddress, value); + payReward(witnessAddress, value); + } + private void payReward(byte[] witnessAddress, long value) { long cycle = dynamicPropertiesStore.getCurrentCycleNumber(); int brokerage = delegationStore.getBrokerage(cycle, witnessAddress); diff --git a/chainbase/src/main/java/org/tron/core/store/DelegationStore.java b/chainbase/src/main/java/org/tron/core/store/DelegationStore.java index 6f497e4fa29..6e7572808f2 100644 --- a/chainbase/src/main/java/org/tron/core/store/DelegationStore.java +++ b/chainbase/src/main/java/org/tron/core/store/DelegationStore.java @@ -50,6 +50,27 @@ public long getBlockReward(long cycle, byte[] address) { } } + public void addTransactionFeeReward(long cycle, byte[] address, long value) { + byte[] key = buildTransactionFeeKey(cycle, address); + BytesCapsule bytesCapsule = get(key); + + if (bytesCapsule == null) { + put(key, new BytesCapsule(ByteArray.fromLong(value))); + } else { + put(key, new BytesCapsule(ByteArray + .fromLong(ByteArray.toLong(bytesCapsule.getData()) + value))); + } + } + + public long getTransactionFeeReward(long cycle, byte[] address) { + BytesCapsule bytesCapsule = get(buildTransactionFeeKey(cycle, address)); + if (bytesCapsule == null) { + return 0L; + } else { + return ByteArray.toLong(bytesCapsule.getData()); + } + } + public void addVoteReward(long cycle, byte[] address, long value) { byte[] key = buildRewardVoteKey(cycle, address); BytesCapsule bytesCapsule = get(key); @@ -185,6 +206,10 @@ private byte[] buildRewardBlockKey(long cycle, byte[] address) { return (cycle + "-" + Hex.toHexString(address) + "-block").getBytes(); } + private byte[] buildTransactionFeeKey(long cycle, byte[] address) { + return (cycle + "-" + Hex.toHexString(address) + "-transaction-fee").getBytes(); + } + private byte[] buildRewardVoteKey(long cycle, byte[] address) { return (cycle + "-" + Hex.toHexString(address) + "-reward-vote").getBytes(); } diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index 62636a3eae9..f32a0fb2bc4 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -889,7 +889,7 @@ public Protocol.ChainParameters getChainParameters() { .setKey("getAllowTvmSolidity059") .setValue(chainBaseManager.getDynamicPropertiesStore().getAllowTvmSolidity059()) .build()); - + // ALLOW_TVM_ISTANBUL builder.addChainParameter( Protocol.ChainParameters.ChainParameter.newBuilder().setKey("getAllowTvmIstanbul") @@ -2735,22 +2735,33 @@ public HashMap queryPayByCycle(byte[] address, } long blockPayReward = 0; + long voteReward = 0; + long transactionFeeReward = 0; if (beginCycle <= endCycle) { for (long cycle = beginCycle; cycle <= endCycle; cycle++) { int brokerage = dbManager.getDelegationStore().getBrokerage(cycle, address); blockPayReward += dbManager.getDelegationStore().getBlockReward(cycle, address); if (brokerage == 100) { - reward += dbManager.getDelegationStore().getBlockReward(cycle, address) + dbManager - .getDelegationStore().getVoteReward(cycle, address); + if (chainBaseManager.getDynamicPropertiesStore().supportTransactionFeePool()) { + reward += dbManager.getDelegationStore().getBlockReward(cycle, address) + dbManager + .getDelegationStore().getVoteReward(cycle, address) + dbManager + .getDelegationStore().getTransactionFeeReward(cycle, address); + transactionFeeReward += dbManager.getDelegationStore().getTransactionFeeReward(cycle, address); + } else { + reward += dbManager.getDelegationStore().getBlockReward(cycle, address) + dbManager + .getDelegationStore().getVoteReward(cycle, address); + } } else { double brokerageRate = (double) brokerage / 100; reward += dbManager.getDelegationStore().getReward(cycle, address) / (1 - brokerageRate); } + voteReward += dbManager.getDelegationStore().getVoteReward(cycle, address); } } rewardMap.put("total", reward); rewardMap.put("produceBlock", blockPayReward); - rewardMap.put("vote", reward - blockPayReward); + rewardMap.put("vote", voteReward); + rewardMap.put("transactionFee", transactionFeeReward); return rewardMap; } @@ -2777,6 +2788,51 @@ public double percentageOfBlockReward(long beginCycle, long endCycle, byte[] add return (double) blockPayReward / (double) reward; } + + public double percentageOfVoteReward(long beginCycle, long endCycle, byte[] address) { + long reward = 0; + long voteReward = 0; + if (beginCycle <= endCycle) { + for (long cycle = beginCycle; cycle <= endCycle; cycle++) { + int brokerage = dbManager.getDelegationStore().getBrokerage(cycle, address); + if (brokerage == 100) { + continue; + } + + double brokerageRate = (double) brokerage / 100; + reward += dbManager.getDelegationStore().getReward(cycle, address) / (1 - brokerageRate); + voteReward += dbManager.getDelegationStore().getVoteReward(cycle, address); + } + } + + if (reward == 0 || voteReward == 0) { + return 0; + } + return (double) voteReward / (double) reward; + } + + public double percentageOfTransactionFee(long beginCycle, long endCycle, byte[] address) { + long reward = 0; + long transactionFeeReward = 0; + if (beginCycle <= endCycle) { + for (long cycle = beginCycle; cycle <= endCycle; cycle++) { + int brokerage = dbManager.getDelegationStore().getBrokerage(cycle, address); + if (brokerage == 100) { + continue; + } + + double brokerageRate = (double) brokerage / 100; + reward += dbManager.getDelegationStore().getReward(cycle, address) / (1 - brokerageRate); + transactionFeeReward += dbManager.getDelegationStore().getTransactionFeeReward(cycle, address); + } + } + + if (reward == 0 || transactionFeeReward == 0) { + return 0; + } + return (double) transactionFeeReward / (double) reward; + } + public HashMap queryRewardByCycle(byte[] address, long beginCycle, long endCycle) { HashMap rewardMap = new HashMap<>(); @@ -2794,12 +2850,17 @@ public HashMap queryRewardByCycle(byte[] address, bonus += dbManager.getDelegationStore().getReward(cycle, address); } } - double percentage = percentageOfBlockReward(beginCycle, endCycle, address); - Double blockBonus = new Double(bonus * percentage); + double percentageOfBlock = percentageOfBlockReward(beginCycle, endCycle, address); + double percentageOfVote = percentageOfVoteReward(beginCycle, endCycle, address); + double percentageOfTransactionFee = percentageOfTransactionFee(beginCycle, endCycle, address); + Double blockBonus = new Double(bonus * percentageOfBlock); + Double voteBonus = new Double(bonus * percentageOfVote); + Double transactionFeeBonus = new Double(bonus * percentageOfTransactionFee); rewardMap.put("total", bonus); rewardMap.put("produceBlock", blockBonus.longValue()); - rewardMap.put("vote", bonus - blockBonus.longValue()); + rewardMap.put("vote", voteBonus.longValue()); + rewardMap.put("transactionFee", transactionFeeBonus.longValue()); return rewardMap; } diff --git a/framework/src/main/java/org/tron/core/db/Manager.java b/framework/src/main/java/org/tron/core/db/Manager.java index f9de8e85833..ce9304dc854 100644 --- a/framework/src/main/java/org/tron/core/db/Manager.java +++ b/framework/src/main/java/org/tron/core/db/Manager.java @@ -1352,27 +1352,27 @@ private void payReward(BlockCapsule block) { mortgageService.payBlockReward(witnessCapsule.getAddress().toByteArray(), getDynamicPropertiesStore().getWitnessPayPerBlock()); mortgageService.payStandbyWitness(); + + if (chainBaseManager.getDynamicPropertiesStore().supportTransactionFeePool()) { + long transactionFeeReward = Math + .floorDiv(chainBaseManager.getDynamicPropertiesStore().getTransactionFeePool(), + Constant.TRANSACTION_FEE_POOL_PERIOD); + mortgageService.payTransactionFeeReward(witnessCapsule.getAddress().toByteArray(), + transactionFeeReward); + } } else { byte[] witness = block.getWitnessAddress().toByteArray(); AccountCapsule account = getAccountStore().get(witness); account.setAllowance(account.getAllowance() + chainBaseManager.getDynamicPropertiesStore().getWitnessPayPerBlock()); - getAccountStore().put(account.createDbKey(), account); - } - if (chainBaseManager.getDynamicPropertiesStore().supportTransactionFeePool()) { - byte[] witness = block.getWitnessAddress().toByteArray(); - AccountCapsule account = getAccountStore().get(witness); - - long transactionFeeReward = Math - .floorDiv(chainBaseManager.getDynamicPropertiesStore().getTransactionFeePool(), - Constant.TRANSACTION_FEE_POOL_PERIOD); - - chainBaseManager.getDynamicPropertiesStore().saveTransactionFeePool( - chainBaseManager.getDynamicPropertiesStore().getTransactionFeePool() - - transactionFeeReward); + if (chainBaseManager.getDynamicPropertiesStore().supportTransactionFeePool()) { + long transactionFeeReward = Math + .floorDiv(chainBaseManager.getDynamicPropertiesStore().getTransactionFeePool(), + Constant.TRANSACTION_FEE_POOL_PERIOD); + account.setAllowance(account.getAllowance() + transactionFeeReward); + } - account.setAllowance(account.getAllowance() + transactionFeeReward); getAccountStore().put(account.createDbKey(), account); } }