Skip to content

Commit

Permalink
add transaction fee pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-liu55 committed Nov 11, 2020
1 parent e8876bd commit af76742
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,15 @@ private void payEnergyBill(
}
account.setBalance(balance - energyFee);

//send to blackHole
Commons.adjustBalance(accountStore, accountStore.getBlackhole().getAddress().toByteArray(),
energyFee);
if (dynamicPropertiesStore.supportTransactionFeePool()) {
dynamicPropertiesStore
.saveTransactionFeePool(dynamicPropertiesStore.getTransactionFeePool() + energyFee);
} else {
//send to blackHole
Commons.adjustBalance(accountStore, accountStore.getBlackhole().getAddress().toByteArray(),
energyFee);
}

}

accountStore.put(account.getAddress().toByteArray(), account);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ protected boolean consumeFee(AccountCapsule accountCapsule, long fee) {
long latestOperationTime = dynamicPropertiesStore.getLatestBlockHeaderTimestamp();
accountCapsule.setLatestOperationTime(latestOperationTime);
Commons.adjustBalance(accountStore, accountCapsule, -fee);
Commons.adjustBalance(accountStore, accountStore.getBlackhole().createDbKey(), +fee);
if (dynamicPropertiesStore.supportTransactionFeePool()) {
dynamicPropertiesStore
.saveTransactionFeePool(dynamicPropertiesStore.getTransactionFeePool() + fee);
} else {
Commons.adjustBalance(accountStore, accountStore.getBlackhole().createDbKey(), +fee);
}

return true;
} catch (BalanceInsufficientException e) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ public void withdrawReward(byte[] address) {
beginCycle, endCycle, accountCapsule.getVotesList());
}


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


public long queryReward(byte[] address) {
if (!dynamicPropertiesStore.allowChangeDelegation()) {
return 0;
Expand Down
22 changes: 22 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 @@ -71,6 +71,28 @@ public long getVoteReward(long cycle, byte[] address) {
}
}


public void addTransactionFeeReward(long cycle, byte[] address, long value) {
byte[] key = buildRewardBlockKey(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(buildRewardBlockKey(cycle, address));
if (bytesCapsule == null) {
return 0L;
} else {
return ByteArray.toLong(bytesCapsule.getData());
}
}

public void addReward(long cycle, byte[] address, long value) {
byte[] key = buildRewardKey(cycle, address);
BytesCapsule bytesCapsule = get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
private static final byte[] MARKET_CANCEL_FEE = "MARKET_CANCEL_FEE".getBytes();
private static final byte[] MARKET_QUANTITY_LIMIT = "MARKET_QUANTITY_LIMIT".getBytes();

//todo,add proposal
private static final byte[] ALLOW_TRANSACTION_FEE_POOL = "ALLOW_TRANSACTION_FEE_POOL".getBytes();
private static final byte[] TRANSACTION_FEE_POOL = "TRANSACTION_FEE_POOL".getBytes();

@Autowired
private DynamicPropertiesStore(@Value("properties") String dbName) {
super(dbName);
Expand Down Expand Up @@ -1373,6 +1377,39 @@ public long getMarketQuantityLimit() {
() -> new IllegalArgumentException("not found MARKET_QUANTITY_LIMIT"));
}


public boolean supportTransactionFeePool() {
return getAllowTransactionFeePool() == 1L;
}

public void saveAllowTransactionFeePool(long limit) {
this.put(ALLOW_TRANSACTION_FEE_POOL,
new BytesCapsule(ByteArray.fromLong(limit)));
}

public long getAllowTransactionFeePool() {
return Optional.ofNullable(getUnchecked(ALLOW_TRANSACTION_FEE_POOL))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElseThrow(
() -> new IllegalArgumentException("not found ALLOW_TRANSACTION_FEE_POOL"));
}

public void saveTransactionFeePool(long value) {
this.put(TRANSACTION_FEE_POOL,
new BytesCapsule(ByteArray.fromLong(value)));
}

public long getTransactionFeePool() {
return Optional.ofNullable(getUnchecked(TRANSACTION_FEE_POOL))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElseThrow(
() -> new IllegalArgumentException("not found TRANSACTION_FEE_POOL"));
}



public void saveTotalTransactionCost(long value) {
this.put(TOTAL_TRANSACTION_COST,
new BytesCapsule(ByteArray.fromLong(value)));
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Constant {
public static final long TRANSACTION_MAX_BYTE_SIZE = 500 * 1_024L;
public static final long MAXIMUM_TIME_UNTIL_EXPIRATION = 24 * 60 * 60 * 1_000L; //one day
public static final long TRANSACTION_DEFAULT_EXPIRATION_TIME = 60 * 1_000L; //60 seconds
public static final long TRANSACTION_FEE_POOL_PERIOD = 60; //60 blocks
// config for smart contract
public static final long SUN_PER_ENERGY = 100; // 1 us = 100 SUN = 100 * 10^-6 TRX
public static final long ENERGY_LIMIT_IN_CONSTANT_TX = 3_000_000L; // ref: 1 us = 1 energy
Expand Down
11 changes: 11 additions & 0 deletions framework/src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1352,13 +1352,24 @@ 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.payBlockReward(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);
}


}

private void postSolidityLogContractTrigger(Long blockNum, Long lastSolidityNum) {
Expand Down

0 comments on commit af76742

Please sign in to comment.