99import org .junit .Assert ;
1010import org .junit .BeforeClass ;
1111import org .junit .Test ;
12+ import org .tron .common .application .TronApplicationContext ;
1213import org .tron .common .utils .ByteArray ;
1314import org .tron .common .utils .FileUtil ;
1415import org .tron .core .Constant ;
16+ import org .tron .core .Wallet ;
17+ import org .tron .core .config .DefaultConfig ;
1518import org .tron .core .config .args .Args ;
19+ import org .tron .core .db .Manager ;
20+ import org .tron .protos .Contract ;
1621import org .tron .protos .Protocol .AccountType ;
1722import org .tron .protos .Protocol .Vote ;
1823
1924public class AccountCapsuleTest {
2025
21- private static String dbPath = "output_accountCapsule_test" ;
26+ private static final String dbPath = "output_accountCapsule_test" ;
27+ private static final Manager dbManager ;
28+ private static final TronApplicationContext context ;
29+ private static final String OWNER_ADDRESS ;
30+ private static final String ASSET_NAME = "trx" ;
31+ private static final long TOTAL_SUPPLY = 10000L ;
32+ private static final int TRX_NUM = 10 ;
33+ private static final int NUM = 1 ;
34+ private static final long START_TIME = 1 ;
35+ private static final long END_TIME = 2 ;
36+ private static final int VOTE_SCORE = 2 ;
37+ private static final String DESCRIPTION = "TRX" ;
38+ private static final String URL = "https://tron.network" ;
39+
40+
2241 static AccountCapsule accountCapsuleTest ;
2342 static AccountCapsule accountCapsule ;
2443
44+ static {
45+ Args .setParam (new String []{"-d" , dbPath , "-w" }, Constant .TEST_CONF );
46+ context = new TronApplicationContext (DefaultConfig .class );
47+ dbManager = context .getBean (Manager .class );
48+
49+ OWNER_ADDRESS = Wallet .getAddressPreFixString () + "a06a17a49648a8ad32055c06f60fa14ae46df94cc1" ;
50+ }
51+
52+
2553 @ BeforeClass
2654 public static void init () {
27- Args .setParam (new String []{"-d" , dbPath , "-w" },
28- Constant .TEST_CONF );
2955 ByteString accountName = ByteString .copyFrom (AccountCapsuleTest .randomBytes (16 ));
3056 ByteString address = ByteString .copyFrom (AccountCapsuleTest .randomBytes (32 ));
3157 AccountType accountType = AccountType .forNumber (1 );
@@ -110,4 +136,172 @@ public static byte[] randomBytes(int length) {
110136 new Random ().nextBytes (result );
111137 return result ;
112138 }
139+
140+ /**
141+ * SameTokenName close, test assert amountV2 function
142+ */
143+ @ Test
144+ public void sameTokenNameCloseAssertAmountV2test () {
145+ dbManager .getDynamicPropertiesStore ().saveAllowSameTokenName (0 );
146+ long id = dbManager .getDynamicPropertiesStore ().getTokenIdNum () + 1 ;
147+ dbManager .getDynamicPropertiesStore ().saveTokenIdNum (id );
148+
149+ Contract .AssetIssueContract assetIssueContract =
150+ Contract .AssetIssueContract .newBuilder ()
151+ .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
152+ .setName (ByteString .copyFrom (ByteArray .fromString (ASSET_NAME )))
153+ .setId (Long .toString (id ))
154+ .setTotalSupply (TOTAL_SUPPLY )
155+ .setTrxNum (TRX_NUM )
156+ .setNum (NUM )
157+ .setStartTime (START_TIME )
158+ .setEndTime (END_TIME )
159+ .setVoteScore (VOTE_SCORE )
160+ .setDescription (ByteString .copyFrom (ByteArray .fromString (DESCRIPTION )))
161+ .setUrl (ByteString .copyFrom (ByteArray .fromString (URL )))
162+ .build ();
163+ AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule (assetIssueContract );
164+ dbManager .getAssetIssueStore ().put (assetIssueCapsule .createDbKey (), assetIssueCapsule );
165+
166+ Contract .AssetIssueContract assetIssueContract2 =
167+ Contract .AssetIssueContract .newBuilder ()
168+ .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
169+ .setName (ByteString .copyFrom (ByteArray .fromString ("abc" )))
170+ .setId (Long .toString (id +1 ))
171+ .setTotalSupply (TOTAL_SUPPLY )
172+ .setTrxNum (TRX_NUM )
173+ .setNum (NUM )
174+ .setStartTime (START_TIME )
175+ .setEndTime (END_TIME )
176+ .setVoteScore (VOTE_SCORE )
177+ .setDescription (ByteString .copyFrom (ByteArray .fromString (DESCRIPTION )))
178+ .setUrl (ByteString .copyFrom (ByteArray .fromString (URL )))
179+ .build ();
180+ AssetIssueCapsule assetIssueCapsule2 = new AssetIssueCapsule (assetIssueContract2 );
181+ dbManager .getAssetIssueStore ().put (assetIssueCapsule2 .createDbKey (), assetIssueCapsule2 );
182+
183+ AccountCapsule accountCapsule =
184+ new AccountCapsule (
185+ ByteString .copyFromUtf8 ("owner" ),
186+ ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )),
187+ AccountType .Normal ,
188+ 10000 );
189+ accountCapsule .addAsset (ByteArray .fromString (ASSET_NAME ),1000L );
190+ dbManager .getAccountStore ().put (accountCapsule .getAddress ().toByteArray (), accountCapsule );
191+
192+
193+ accountCapsule .addAssetV2 (ByteArray .fromString (String .valueOf (id )), 1000L );
194+ Assert .assertEquals (accountCapsule .getAssetMap ().get (ASSET_NAME ).longValue (), 1000L );
195+ Assert .assertEquals (accountCapsule .getAssetMapV2 ().get (String .valueOf (id )).longValue (),
196+ 1000L );
197+
198+ //assetBalanceEnoughV2
199+ Assert .assertTrue (accountCapsule .assetBalanceEnoughV2 (ByteArray .fromString (ASSET_NAME ),
200+ 999 , dbManager ));
201+ Assert .assertFalse (accountCapsule .assetBalanceEnoughV2 (ByteArray .fromString (ASSET_NAME ),
202+ 1001 , dbManager ));
203+
204+ //reduceAssetAmountV2
205+ Assert .assertTrue (accountCapsule .reduceAssetAmountV2 (ByteArray .fromString (ASSET_NAME ),
206+ 999 , dbManager ));
207+ Assert .assertFalse (accountCapsule .reduceAssetAmountV2 (ByteArray .fromString (ASSET_NAME ),
208+ 0 , dbManager ));
209+ Assert .assertFalse (accountCapsule .reduceAssetAmountV2 (ByteArray .fromString (ASSET_NAME ),
210+ 1001 , dbManager ));
211+ Assert .assertFalse (accountCapsule .reduceAssetAmountV2 (ByteArray .fromString ("abc" ),
212+ 1001 , dbManager ));
213+
214+ //addAssetAmountV2
215+ Assert .assertTrue (accountCapsule .addAssetAmountV2 (ByteArray .fromString (ASSET_NAME ),
216+ 500 , dbManager ));
217+ // 1000-999 +500
218+ Assert .assertEquals (accountCapsule .getAssetMap ().get (ASSET_NAME ).longValue (), 501L );
219+ Assert .assertTrue (accountCapsule .addAssetAmountV2 (ByteArray .fromString ("abc" ),
220+ 500 , dbManager ));
221+ Assert .assertEquals (accountCapsule .getAssetMap ().get ("abc" ).longValue (), 500L );
222+ }
223+
224+ /**
225+ * SameTokenName open, test assert amountV2 function
226+ */
227+ @ Test
228+ public void sameTokenNameOpenAssertAmountV2test () {
229+ dbManager .getDynamicPropertiesStore ().saveAllowSameTokenName (1 );
230+ long id = dbManager .getDynamicPropertiesStore ().getTokenIdNum () + 1 ;
231+ dbManager .getDynamicPropertiesStore ().saveTokenIdNum (id );
232+
233+ Contract .AssetIssueContract assetIssueContract =
234+ Contract .AssetIssueContract .newBuilder ()
235+ .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
236+ .setName (ByteString .copyFrom (ByteArray .fromString (ASSET_NAME )))
237+ .setId (Long .toString (id ))
238+ .setTotalSupply (TOTAL_SUPPLY )
239+ .setTrxNum (TRX_NUM )
240+ .setNum (NUM )
241+ .setStartTime (START_TIME )
242+ .setEndTime (END_TIME )
243+ .setVoteScore (VOTE_SCORE )
244+ .setDescription (ByteString .copyFrom (ByteArray .fromString (DESCRIPTION )))
245+ .setUrl (ByteString .copyFrom (ByteArray .fromString (URL )))
246+ .build ();
247+ AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule (assetIssueContract );
248+ dbManager .getAssetIssueV2Store ().put (assetIssueCapsule .createDbV2Key (), assetIssueCapsule );
249+
250+ Contract .AssetIssueContract assetIssueContract2 =
251+ Contract .AssetIssueContract .newBuilder ()
252+ .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
253+ .setName (ByteString .copyFrom (ByteArray .fromString ("abc" )))
254+ .setId (Long .toString (id +1 ))
255+ .setTotalSupply (TOTAL_SUPPLY )
256+ .setTrxNum (TRX_NUM )
257+ .setNum (NUM )
258+ .setStartTime (START_TIME )
259+ .setEndTime (END_TIME )
260+ .setVoteScore (VOTE_SCORE )
261+ .setDescription (ByteString .copyFrom (ByteArray .fromString (DESCRIPTION )))
262+ .setUrl (ByteString .copyFrom (ByteArray .fromString (URL )))
263+ .build ();
264+ AssetIssueCapsule assetIssueCapsule2 = new AssetIssueCapsule (assetIssueContract2 );
265+ dbManager .getAssetIssueV2Store ().put (assetIssueCapsule2 .createDbV2Key (), assetIssueCapsule2 );
266+
267+ AccountCapsule accountCapsule =
268+ new AccountCapsule (
269+ ByteString .copyFromUtf8 ("owner" ),
270+ ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )),
271+ AccountType .Normal ,
272+ 10000 );
273+ accountCapsule .addAssetV2 (ByteArray .fromString (String .valueOf (id )),1000L );
274+ dbManager .getAccountStore ().put (accountCapsule .getAddress ().toByteArray (), accountCapsule );
275+ Assert .assertEquals (accountCapsule .getAssetMapV2 ().get (String .valueOf (id )).longValue (),
276+ 1000L );
277+
278+ //assetBalanceEnoughV2
279+ Assert .assertTrue (accountCapsule .assetBalanceEnoughV2 (ByteArray .fromString (String .valueOf (id )),
280+ 999 , dbManager ));
281+
282+ Assert .assertFalse (accountCapsule .assetBalanceEnoughV2 (ByteArray .fromString (String .valueOf (id )),
283+ 1001 , dbManager ));
284+
285+ //reduceAssetAmountV2
286+ Assert .assertTrue (accountCapsule .reduceAssetAmountV2 (ByteArray .fromString (String .valueOf (id )),
287+ 999 , dbManager ));
288+ Assert .assertFalse (accountCapsule .reduceAssetAmountV2 (ByteArray .fromString (String .valueOf (id )),
289+ 0 , dbManager ));
290+ Assert .assertFalse (accountCapsule .reduceAssetAmountV2 (ByteArray .fromString (String .valueOf (id )),
291+ 1001 , dbManager ));
292+ // abc
293+ Assert .assertFalse (accountCapsule .reduceAssetAmountV2 (ByteArray .fromString (String .valueOf (id +1 )),
294+ 1001 , dbManager ));
295+
296+ //addAssetAmountV2
297+ Assert .assertTrue (accountCapsule .addAssetAmountV2 (ByteArray .fromString (String .valueOf (id )),
298+ 500 , dbManager ));
299+ // 1000-999 +500
300+ Assert .assertEquals (accountCapsule .getAssetMapV2 ().get (String .valueOf (id )).longValue (), 501L );
301+ //abc
302+ Assert .assertTrue (accountCapsule .addAssetAmountV2 (ByteArray .fromString (String .valueOf (id +1 )),
303+ 500 , dbManager ));
304+ Assert .assertEquals (accountCapsule .getAssetMapV2 ().get (String .valueOf (id +1 )).longValue (), 500L );
305+ }
306+
113307}
0 commit comments