1616import com .google .protobuf .Any ;
1717import com .google .protobuf .ByteString ;
1818import lombok .extern .slf4j .Slf4j ;
19+ import org .junit .Assert ;
1920import org .junit .Before ;
2021import org .junit .Test ;
2122import org .tron .common .BaseTest ;
2526import org .tron .core .capsule .AccountCapsule ;
2627import org .tron .core .capsule .DelegatedResourceAccountIndexCapsule ;
2728import org .tron .core .capsule .DelegatedResourceCapsule ;
29+ import org .tron .core .capsule .TransactionCapsule ;
2830import org .tron .core .capsule .TransactionResultCapsule ;
2931import org .tron .core .config .args .Args ;
3032import org .tron .core .exception .ContractExeException ;
3133import org .tron .core .exception .ContractValidateException ;
3234import org .tron .core .store .DynamicPropertiesStore ;
35+ import org .tron .protos .Protocol ;
3336import org .tron .protos .Protocol .AccountType ;
3437import org .tron .protos .Protocol .Transaction .Result .code ;
3538import 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