Skip to content

Commit 4d5f05a

Browse files
authored
fix(freezeV2): fix estimateConsumeBandWidthSize (#5450)
1 parent 4ada90e commit 4d5f05a

4 files changed

Lines changed: 106 additions & 3 deletions

File tree

actuator/src/main/java/org/tron/core/actuator/DelegateResourceActuator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public boolean validate() throws ContractValidateException {
158158
long accountNetUsage = ownerCapsule.getNetUsage();
159159
if (null != this.getTx() && this.getTx().isTransactionCreate()) {
160160
accountNetUsage += TransactionUtil.estimateConsumeBandWidthSize(dynamicStore,
161-
ownerCapsule.getBalance());
161+
ownerCapsule.getFrozenV2BalanceForBandwidth());
162162
}
163163
long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double)
164164
(dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));

framework/src/main/java/org/tron/core/Wallet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ public long calcCanDelegatedBandWidthMaxSize(
867867

868868
long accountNetUsage = ownerCapsule.getNetUsage();
869869
accountNetUsage += TransactionUtil.estimateConsumeBandWidthSize(dynamicStore,
870-
ownerCapsule.getBalance());
870+
ownerCapsule.getFrozenV2BalanceForBandwidth());
871871

872872
long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double)
873873
(dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));

framework/src/test/java/org/tron/core/WalletTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,5 +1107,48 @@ public void testListNodes() {
11071107
GrpcAPI.NodeList nodeList = wallet.listNodes();
11081108
assertEquals(0, nodeList.getNodesList().size());
11091109
}
1110+
1111+
/**
1112+
* We calculate the size of the structure and conclude that
1113+
* delegate_balance = 1000_000L; => 277
1114+
* delegate_balance = 1000_000_000L; => 279
1115+
* delegate_balance = 1000_000_000_000L => 280
1116+
*
1117+
* We initialize account information as follows
1118+
* account balance = 1000_000_000_000L
1119+
* account frozen_balance = 1000_000_000L
1120+
*
1121+
* then estimateConsumeBandWidthSize cost 279
1122+
*
1123+
* so we have following result:
1124+
* TransactionUtil.estimateConsumeBandWidthSize(
1125+
* dynamicStore,ownerCapsule.getBalance()) ===> false
1126+
* TransactionUtil.estimateConsumeBandWidthSize(
1127+
* dynamicStore,ownerCapsule.getFrozenV2BalanceForBandwidth()) ===> true
1128+
*
1129+
* This test case is used to verify the above conclusions
1130+
*/
1131+
@Test
1132+
public void testGetCanDelegatedMaxSizeBandWidth123() {
1133+
long frozenBalance = 1000_000_000L;
1134+
AccountCapsule ownerCapsule =
1135+
dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
1136+
ownerCapsule.addFrozenBalanceForBandwidthV2(frozenBalance);
1137+
ownerCapsule.setNetUsage(0L);
1138+
ownerCapsule.setEnergyUsage(0L);
1139+
dbManager.getDynamicPropertiesStore().saveTotalNetWeight(0L);
1140+
dbManager.getDynamicPropertiesStore().saveTotalEnergyWeight(0L);
1141+
dbManager.getDynamicPropertiesStore().addTotalNetWeight(
1142+
frozenBalance / TRX_PRECISION + 43100000000L);
1143+
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
1144+
1145+
GrpcAPI.CanDelegatedMaxSizeResponseMessage message = wallet.getCanDelegatedMaxSize(
1146+
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)),
1147+
BANDWIDTH.getNumber());
1148+
Assert.assertEquals(frozenBalance / TRX_PRECISION - 279,
1149+
message.getMaxSize() / TRX_PRECISION);
1150+
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(DELEGATE_PERIOD / 3000);
1151+
}
1152+
11101153
}
11111154

framework/src/test/java/org/tron/core/actuator/DelegateResourceActuatorTest.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.google.protobuf.Any;
1717
import com.google.protobuf.ByteString;
1818
import lombok.extern.slf4j.Slf4j;
19+
import org.junit.Assert;
1920
import org.junit.Before;
2021
import org.junit.Test;
2122
import org.tron.common.BaseTest;
@@ -25,11 +26,13 @@
2526
import org.tron.core.capsule.AccountCapsule;
2627
import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule;
2728
import org.tron.core.capsule.DelegatedResourceCapsule;
29+
import org.tron.core.capsule.TransactionCapsule;
2830
import org.tron.core.capsule.TransactionResultCapsule;
2931
import org.tron.core.config.args.Args;
3032
import org.tron.core.exception.ContractExeException;
3133
import org.tron.core.exception.ContractValidateException;
3234
import org.tron.core.store.DynamicPropertiesStore;
35+
import org.tron.protos.Protocol;
3336
import org.tron.protos.Protocol.AccountType;
3437
import org.tron.protos.Protocol.Transaction.Result.code;
3538
import org.tron.protos.contract.AssetIssueContractOuterClass;
@@ -43,7 +46,7 @@ public class DelegateResourceActuatorTest extends BaseTest {
4346
private static final String RECEIVER_ADDRESS;
4447
private static final String OWNER_ADDRESS_INVALID = "aaaa";
4548
private static final String OWNER_ACCOUNT_INVALID;
46-
private static final long initBalance = 10_000_000_000L;
49+
private static final long initBalance = 1000_000_000_000L;
4750

4851
static {
4952
dbPath = "output_delegate_resource_test";
@@ -59,6 +62,7 @@ public class DelegateResourceActuatorTest extends BaseTest {
5962
*/
6063
@Before
6164
public void createAccountCapsule() {
65+
dbManager.getDynamicPropertiesStore().saveTotalNetWeight(0L);
6266
dbManager.getDynamicPropertiesStore().saveUnfreezeDelayDays(1L);
6367
dbManager.getDynamicPropertiesStore().saveAllowNewResourceModel(1L);
6468

@@ -828,4 +832,60 @@ private Any getErrorContract() {
828832
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))).build()
829833
);
830834
}
835+
836+
837+
/**
838+
* We calculate the size of the structure and conclude that
839+
* delegate_balance = 1000_000L; => 277
840+
* delegate_balance = 1000_000_000L; => 279
841+
* delegate_balance = 1000_000_000_000L => 280
842+
*
843+
* We initialize account information as follows
844+
* account balance = 1000_000_000_000L
845+
* account frozen_balance = 1000_000_000L
846+
*
847+
* then estimateConsumeBandWidthSize cost 279
848+
*
849+
* so we have following result:
850+
* TransactionUtil.estimateConsumeBandWidthSize(
851+
* dynamicStore,ownerCapsule.getBalance()) ===> false
852+
* TransactionUtil.estimateConsumeBandWidthSize(
853+
* dynamicStore,ownerCapsule.getFrozenV2BalanceForBandwidth()) ===> true
854+
*
855+
* This test case is used to verify the above conclusions
856+
*/
857+
@Test
858+
public void testDelegateResourceNoFreeze123() {
859+
long frozenBalance = 1000_000_000L;
860+
AccountCapsule ownerCapsule =
861+
dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
862+
ownerCapsule.addFrozenBalanceForBandwidthV2(frozenBalance);
863+
dbManager.getDynamicPropertiesStore().addTotalNetWeight(
864+
frozenBalance / TRX_PRECISION + 43100000000L);
865+
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
866+
867+
long delegateBalance = frozenBalance - 279 * TRX_PRECISION;
868+
//long delegateBalance = initBalance;
869+
DelegateResourceActuator actuator = new DelegateResourceActuator();
870+
TransactionCapsule transactionCapsule = new TransactionCapsule(
871+
getDelegateContractForBandwidth(OWNER_ADDRESS, RECEIVER_ADDRESS, delegateBalance),
872+
Protocol.Transaction.Contract.ContractType.DelegateResourceContract
873+
);
874+
transactionCapsule.setTransactionCreate(true);
875+
actuator.setChainBaseManager(dbManager.getChainBaseManager()).setAny(
876+
getDelegateContractForBandwidth(OWNER_ADDRESS, RECEIVER_ADDRESS, delegateBalance));
877+
actuator.setTx(transactionCapsule);
878+
879+
boolean bSuccess = true;
880+
try {
881+
actuator.validate();
882+
} catch (ContractValidateException e) {
883+
assertEquals(
884+
"delegateBalance must be less than or equal to available FreezeBandwidthV2 balance",
885+
e.getMessage());
886+
bSuccess = false;
887+
}
888+
Assert.assertEquals(true, bSuccess);
889+
}
890+
831891
}

0 commit comments

Comments
 (0)