Skip to content

Commit

Permalink
test(reward): add test,remove unused code.
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed Jun 7, 2022
1 parent a6fd5b2 commit 82b79c0
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 49 deletions.
63 changes: 15 additions & 48 deletions chainbase/src/main/java/org/tron/core/service/MortgageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,28 +338,6 @@ public void adjustOracleAllowance(byte[] address, OracleRewardCapsule reward) {
accountStore.put(account.createDbKey(), account);
}


private OracleRewardCapsule computeOracleReward(long cycle, AccountCapsule accountCapsule) {
Dec balance = Dec.zeroDec();
Map<String, Dec> asset = new HashMap<>();
for (Vote vote : accountCapsule.getVotesList()) {
byte[] srAddress = vote.getVoteAddress().toByteArray();
DecOracleRewardCapsule totalReward = delegationStore.getOracleReward(cycle, srAddress);
long totalVote = delegationStore.getWitnessVote(cycle, srAddress);
if (totalVote == DelegationStore.REMARK || totalVote == 0) {
continue;
}
long userVote = vote.getVoteCount();
DecOracleRewardCapsule userReward = totalReward.mul(userVote).quo(totalVote);
balance = balance.add(userReward.getBalance());
userReward.getAsset().forEach((k, v) -> asset.merge(k, v, Dec::add));
logger.debug("computeOracleReward {} {} {} {},{},{},{}", cycle,
Hex.toHexString(accountCapsule.getAddress().toByteArray()), Hex.toHexString(srAddress),
userVote, totalVote, totalReward, userReward);
}
return new DecOracleRewardCapsule(balance, asset).truncateDecimal();
}

private OracleRewardCapsule computeOracleReward(long beginCycle, long endCycle,
AccountCapsule accountCapsule) {
OracleRewardCapsule oracleReward = new OracleRewardCapsule();
Expand All @@ -373,32 +351,21 @@ private OracleRewardCapsule computeOracleReward(long beginCycle, long endCycle,

long balance = 0;
Map<String, Long> asset = new HashMap<>();
// long newAlgorithmCycle = dynamicPropertiesStore.getNewRewardAlgorithmEffectiveCycle();
// if (beginCycle < newAlgorithmCycle) {
// long oldEndCycle = Math.min(endCycle, newAlgorithmCycle);
// for (long cycle = beginCycle; cycle < oldEndCycle; cycle++) {
// OracleRewardCapsule reward = computeOracleReward(cycle, accountCapsule);
// balance = LongMath.checkedAdd(balance, reward.getBalance());
// reward.getAsset().forEach((k, v) -> asset.merge(k, v, LongMath::checkedAdd));
// }
// beginCycle = oldEndCycle;
// }
// if (beginCycle < endCycle) {
for (Vote vote : accountCapsule.getVotesList()) {
byte[] srAddress = vote.getVoteAddress().toByteArray();
DecOracleRewardCapsule beginVi =
delegationStore.getWitnessOracleVi(beginCycle - 1, srAddress);
DecOracleRewardCapsule endVi = delegationStore.getWitnessOracleVi(endCycle - 1, srAddress);
DecOracleRewardCapsule deltaVi = endVi.sub(beginVi);
if (deltaVi.isZero()) {
continue;
}
long userVote = vote.getShares();// vote.getVoteCount();
OracleRewardCapsule userReward = deltaVi.mul(userVote).truncateDecimal();
balance = LongMath.checkedAdd(balance, userReward.getBalance());
userReward.getAsset().forEach((k, v) -> asset.merge(k, v, LongMath::checkedAdd));

for (Vote vote : accountCapsule.getVotesList()) {
byte[] srAddress = vote.getVoteAddress().toByteArray();
DecOracleRewardCapsule beginVi =
delegationStore.getWitnessOracleVi(beginCycle - 1, srAddress);
DecOracleRewardCapsule endVi = delegationStore.getWitnessOracleVi(endCycle - 1, srAddress);
DecOracleRewardCapsule deltaVi = endVi.sub(beginVi);
if (deltaVi.isZero()) {
continue;
}
// }
long userVote = vote.getShares();// vote.getVoteCount();
OracleRewardCapsule userReward = deltaVi.mul(userVote).truncateDecimal();
balance = LongMath.checkedAdd(balance, userReward.getBalance());
userReward.getAsset().forEach((k, v) -> asset.merge(k, v, LongMath::checkedAdd));
}
return new OracleRewardCapsule(balance, asset);

}
Expand Down Expand Up @@ -454,7 +421,7 @@ public void payOracleReward(byte[] witnessAddress, DecOracleRewardCapsule reward
adjustOracleAllowance(witnessAddress, witnessReward.truncateDecimal());
}

public boolean allowStableMarketOff () {
public boolean allowStableMarketOff() {
return !dynamicPropertiesStore.allowChangeDelegation()
|| !dynamicPropertiesStore.allowStableMarketOn();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@

import com.google.protobuf.ByteString;
import io.grpc.ManagedChannelBuilder;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.tron.api.GrpcAPI.BytesMessage;
import org.tron.api.GrpcAPI.TransactionExtention;
import org.tron.api.WalletGrpc;
import org.tron.api.WalletGrpc.WalletBlockingStub;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.entity.Dec;
import org.tron.common.utils.Pair;
import org.tron.core.Wallet;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.DecOracleRewardCapsule;
import org.tron.core.capsule.OracleRewardCapsule;
import org.tron.core.db.Manager;
import org.tron.core.service.MortgageService;
import org.tron.core.store.DelegationStore;
import org.tron.protos.Protocol.Vote;
import org.tron.protos.contract.StorageContract.UpdateBrokerageContract;

@Slf4j
Expand Down Expand Up @@ -49,6 +57,50 @@ public static void testGrpc() {
System.out.println("UpdateBrokerage: " + transactionExtention);
}


private Pair<OracleRewardCapsule,DecOracleRewardCapsule> computeOracleReward(
long cycle, AccountCapsule accountCapsule) {
Dec balance = Dec.zeroDec();
Map<String, Dec> asset = new HashMap<>();
for (Vote vote : accountCapsule.getVotesList()) {
byte[] srAddress = vote.getVoteAddress().toByteArray();
DecOracleRewardCapsule totalReward = manager.getDelegationStore()
.getOracleReward(cycle, srAddress);
long totalVote = manager.getDelegationStore().getWitnessVote(cycle, srAddress);
if (totalVote == DelegationStore.REMARK || totalVote == 0) {
continue;
}
long userVote = vote.getVoteCount();
DecOracleRewardCapsule userReward = totalReward.mul(userVote).quo(totalVote);
balance = balance.add(userReward.getBalance());
userReward.getAsset().forEach((k, v) -> asset.merge(k, v, Dec::add));
}
return new DecOracleRewardCapsule(balance, asset).truncateDecimalAndRemainder();
}

private void testOraclePay(int cycle) {

int brokerage = 20;
if (cycle == 0) {
brokerage = 10;
}

byte[] sr27 = decodeFromBase58Check("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD");
DecOracleRewardCapsule value = manager.getDelegationStore().getOracleReward(cycle, sr27);
Dec balance = Dec.newDec(66);
Map<String, Dec> asset = new HashMap<>();
asset.put("stable-01",Dec.newDec(100));
asset.put("stable-02",Dec.newDec(25));
asset.put("stable-03",Dec.newDec(33));
DecOracleRewardCapsule reward = new DecOracleRewardCapsule(balance, asset);
mortgageService.payOracleReward(sr27,new DecOracleRewardCapsule(balance, asset));
DecOracleRewardCapsule expect = value.add(reward.mul(Dec.newDecWithPrec(100 - brokerage, 2)));
value = manager.getDelegationStore().getOracleReward(cycle, sr27);
Assert.assertEquals(expect.getInstance(),value.getInstance());
manager.getDelegationStore().accumulateWitnessOracleVi(cycle, sr27,
manager.getWitnessStore().get(sr27).getTotalShares());
}

private void testPay(int cycle) {
double rate = 0.2;
if (cycle == 0) {
Expand Down Expand Up @@ -81,16 +133,34 @@ private void testWithdraw() {
//init
manager.getDynamicPropertiesStore().saveCurrentCycleNumber(1);
testPay(1);
testOraclePay(1);
manager.getDynamicPropertiesStore().saveCurrentCycleNumber(2);
testPay(2);
testOraclePay(2);
byte[] sr1 = decodeFromBase58Check("THKJYuUmMKKARNf7s2VT51g5uPY6KEqnat");
AccountCapsule accountCapsule = manager.getAccountStore().get(sr1);
byte[] sr27 = decodeFromBase58Check("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD");
accountCapsule.addVotes(ByteString.copyFrom(sr27), 10000000);
accountCapsule.addVotes(ByteString.copyFrom(sr27), 10000000, 10000000 * 1000_000L);
manager.getAccountStore().put(sr1, accountCapsule);
accountCapsule = manager.getAccountStore().get(sr1);
//
long allowance = accountCapsule.getAllowance();
OracleRewardCapsule oracleAllowance = new OracleRewardCapsule(
accountCapsule.getOracleAllowance());
long value = mortgageService.queryReward(sr1) - allowance;
OracleRewardCapsule retain = mortgageService.queryOracleReward(sr1)
.sub(oracleAllowance);
Pair<OracleRewardCapsule,DecOracleRewardCapsule> pair1 =
computeOracleReward(0, accountCapsule);
Pair<OracleRewardCapsule,DecOracleRewardCapsule> pair2 =
computeOracleReward(1, accountCapsule);
OracleRewardCapsule oracleReward1 = pair1.getKey();
DecOracleRewardCapsule remainder1 = pair1.getValue();
OracleRewardCapsule oracleReward2 = pair2.getKey();
DecOracleRewardCapsule remainder2 = pair2.getValue();
OracleRewardCapsule oracleReward = new DecOracleRewardCapsule(oracleReward1.add(oracleReward2))
.add(remainder1).add(remainder2).truncateDecimal();
Assert.assertEquals(retain.getInstance(), oracleReward.getInstance());
long reward1 = (long) ((double) manager.getDelegationStore().getReward(0, sr27) / 100000000
* 10000000);
long reward2 = (long) ((double) manager.getDelegationStore().getReward(1, sr27) / 100000000
Expand All @@ -101,19 +171,24 @@ private void testWithdraw() {
mortgageService.withdrawReward(sr1);
accountCapsule = manager.getAccountStore().get(sr1);
allowance = accountCapsule.getAllowance() - allowance;
oracleAllowance = new OracleRewardCapsule(accountCapsule.getOracleAllowance())
.sub(oracleAllowance);
System.out.println("withdrawReward:" + allowance);
Assert.assertEquals(reward, allowance);
Assert.assertEquals(oracleReward.getInstance(), oracleAllowance.getInstance());
}

public void test() {
manager.getDynamicPropertiesStore().saveChangeDelegation(1);
manager.getDynamicPropertiesStore().saveAllowStableMarketOn(1);
byte[] sr27 = decodeFromBase58Check("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD");
manager.getDelegationStore().setBrokerage(0, sr27, 10);
manager.getDelegationStore().setBrokerage(1, sr27, 20);
manager.getDelegationStore().setWitnessVote(0, sr27, 100000000);
manager.getDelegationStore().setWitnessVote(1, sr27, 100000000);
manager.getDelegationStore().setWitnessVote(2, sr27, 100000000);
testPay(0);
testOraclePay(0);
testWithdraw();
}
}

0 comments on commit 82b79c0

Please sign in to comment.