Skip to content

Commit

Permalink
transfer transaction fee to voters
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-liu55 committed Nov 16, 2020
1 parent ee36a24 commit 75c4d3b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions chainbase/src/main/java/org/tron/core/store/DelegationStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down
75 changes: 68 additions & 7 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -2735,22 +2735,33 @@ public HashMap<String, Long> 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;
}
Expand All @@ -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<String, Long> queryRewardByCycle(byte[] address,
long beginCycle, long endCycle) {
HashMap<String, Long> rewardMap = new HashMap<>();
Expand All @@ -2794,12 +2850,17 @@ public HashMap<String, Long> 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;
}

Expand Down
28 changes: 14 additions & 14 deletions framework/src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 75c4d3b

Please sign in to comment.