Skip to content

Commit

Permalink
fix(freezeV2): fix estimateConsumeBandWidthSize (tronprotocol#5450)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlChaoCarl authored Aug 30, 2023
1 parent 4ada90e commit 4d5f05a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public boolean validate() throws ContractValidateException {
long accountNetUsage = ownerCapsule.getNetUsage();
if (null != this.getTx() && this.getTx().isTransactionCreate()) {
accountNetUsage += TransactionUtil.estimateConsumeBandWidthSize(dynamicStore,
ownerCapsule.getBalance());
ownerCapsule.getFrozenV2BalanceForBandwidth());
}
long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double)
(dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));
Expand Down
2 changes: 1 addition & 1 deletion framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ public long calcCanDelegatedBandWidthMaxSize(

long accountNetUsage = ownerCapsule.getNetUsage();
accountNetUsage += TransactionUtil.estimateConsumeBandWidthSize(dynamicStore,
ownerCapsule.getBalance());
ownerCapsule.getFrozenV2BalanceForBandwidth());

long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double)
(dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));
Expand Down
43 changes: 43 additions & 0 deletions framework/src/test/java/org/tron/core/WalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1107,5 +1107,48 @@ public void testListNodes() {
GrpcAPI.NodeList nodeList = wallet.listNodes();
assertEquals(0, nodeList.getNodesList().size());
}

/**
* We calculate the size of the structure and conclude that
* delegate_balance = 1000_000L; => 277
* delegate_balance = 1000_000_000L; => 279
* delegate_balance = 1000_000_000_000L => 280
*
* We initialize account information as follows
* account balance = 1000_000_000_000L
* account frozen_balance = 1000_000_000L
*
* then estimateConsumeBandWidthSize cost 279
*
* so we have following result:
* TransactionUtil.estimateConsumeBandWidthSize(
* dynamicStore,ownerCapsule.getBalance()) ===> false
* TransactionUtil.estimateConsumeBandWidthSize(
* dynamicStore,ownerCapsule.getFrozenV2BalanceForBandwidth()) ===> true
*
* This test case is used to verify the above conclusions
*/
@Test
public void testGetCanDelegatedMaxSizeBandWidth123() {
long frozenBalance = 1000_000_000L;
AccountCapsule ownerCapsule =
dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
ownerCapsule.addFrozenBalanceForBandwidthV2(frozenBalance);
ownerCapsule.setNetUsage(0L);
ownerCapsule.setEnergyUsage(0L);
dbManager.getDynamicPropertiesStore().saveTotalNetWeight(0L);
dbManager.getDynamicPropertiesStore().saveTotalEnergyWeight(0L);
dbManager.getDynamicPropertiesStore().addTotalNetWeight(
frozenBalance / TRX_PRECISION + 43100000000L);
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);

GrpcAPI.CanDelegatedMaxSizeResponseMessage message = wallet.getCanDelegatedMaxSize(
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)),
BANDWIDTH.getNumber());
Assert.assertEquals(frozenBalance / TRX_PRECISION - 279,
message.getMaxSize() / TRX_PRECISION);
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(DELEGATE_PERIOD / 3000);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.tron.common.BaseTest;
Expand All @@ -25,11 +26,13 @@
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule;
import org.tron.core.capsule.DelegatedResourceCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.config.args.Args;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.AccountType;
import org.tron.protos.Protocol.Transaction.Result.code;
import org.tron.protos.contract.AssetIssueContractOuterClass;
Expand All @@ -43,7 +46,7 @@ public class DelegateResourceActuatorTest extends BaseTest {
private static final String RECEIVER_ADDRESS;
private static final String OWNER_ADDRESS_INVALID = "aaaa";
private static final String OWNER_ACCOUNT_INVALID;
private static final long initBalance = 10_000_000_000L;
private static final long initBalance = 1000_000_000_000L;

static {
dbPath = "output_delegate_resource_test";
Expand All @@ -59,6 +62,7 @@ public class DelegateResourceActuatorTest extends BaseTest {
*/
@Before
public void createAccountCapsule() {
dbManager.getDynamicPropertiesStore().saveTotalNetWeight(0L);
dbManager.getDynamicPropertiesStore().saveUnfreezeDelayDays(1L);
dbManager.getDynamicPropertiesStore().saveAllowNewResourceModel(1L);

Expand Down Expand Up @@ -828,4 +832,60 @@ private Any getErrorContract() {
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))).build()
);
}


/**
* We calculate the size of the structure and conclude that
* delegate_balance = 1000_000L; => 277
* delegate_balance = 1000_000_000L; => 279
* delegate_balance = 1000_000_000_000L => 280
*
* We initialize account information as follows
* account balance = 1000_000_000_000L
* account frozen_balance = 1000_000_000L
*
* then estimateConsumeBandWidthSize cost 279
*
* so we have following result:
* TransactionUtil.estimateConsumeBandWidthSize(
* dynamicStore,ownerCapsule.getBalance()) ===> false
* TransactionUtil.estimateConsumeBandWidthSize(
* dynamicStore,ownerCapsule.getFrozenV2BalanceForBandwidth()) ===> true
*
* This test case is used to verify the above conclusions
*/
@Test
public void testDelegateResourceNoFreeze123() {
long frozenBalance = 1000_000_000L;
AccountCapsule ownerCapsule =
dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
ownerCapsule.addFrozenBalanceForBandwidthV2(frozenBalance);
dbManager.getDynamicPropertiesStore().addTotalNetWeight(
frozenBalance / TRX_PRECISION + 43100000000L);
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);

long delegateBalance = frozenBalance - 279 * TRX_PRECISION;
//long delegateBalance = initBalance;
DelegateResourceActuator actuator = new DelegateResourceActuator();
TransactionCapsule transactionCapsule = new TransactionCapsule(
getDelegateContractForBandwidth(OWNER_ADDRESS, RECEIVER_ADDRESS, delegateBalance),
Protocol.Transaction.Contract.ContractType.DelegateResourceContract
);
transactionCapsule.setTransactionCreate(true);
actuator.setChainBaseManager(dbManager.getChainBaseManager()).setAny(
getDelegateContractForBandwidth(OWNER_ADDRESS, RECEIVER_ADDRESS, delegateBalance));
actuator.setTx(transactionCapsule);

boolean bSuccess = true;
try {
actuator.validate();
} catch (ContractValidateException e) {
assertEquals(
"delegateBalance must be less than or equal to available FreezeBandwidthV2 balance",
e.getMessage());
bSuccess = false;
}
Assert.assertEquals(true, bSuccess);
}

}

0 comments on commit 4d5f05a

Please sign in to comment.