55import com .google .protobuf .Any ;
66import com .google .protobuf .ByteString ;
77import java .io .File ;
8+ import java .util .ArrayList ;
9+ import java .util .List ;
810import lombok .extern .slf4j .Slf4j ;
911import org .junit .AfterClass ;
1012import org .junit .Assert ;
1719import org .tron .core .Constant ;
1820import org .tron .core .Wallet ;
1921import org .tron .core .capsule .AccountCapsule ;
22+ import org .tron .core .capsule .TransactionCapsule ;
2023import org .tron .core .capsule .TransactionResultCapsule ;
2124import org .tron .core .config .DefaultConfig ;
2225import org .tron .core .config .args .Args ;
@@ -139,42 +142,46 @@ private Any getContract(
139142 .build ());
140143 }
141144
142- // TODO: use Key.equals to check result?
143- private void checkResult (
144- String ownerAddress , String permissionName , String keyAddress , long keyWeight ) {
145- boolean checked = false ;
146- AccountCapsule owner = dbManager .getAccountStore ().get (ByteArray .fromHexString (ownerAddress ));
147- Permission permission = owner .getPermissionByName (permissionName );
148- if (permission != null ) {
149- for (Key key : permission .getKeysList ()) {
150- if (key .getAddress ().equals (ByteString .copyFrom (ByteArray .fromHexString (keyAddress )))) {
151- if (!checked ) {
152- checked = true ;
153- } else {
154- Assert .assertFalse (checked );
155- }
156- Assert .assertEquals (key .getWeight (), keyWeight );
157- }
158- }
159- }
145+ @ Test
146+ public void successFistAddPermissionKey () {
147+ PermissionAddKeyActuator actuator = new PermissionAddKeyActuator (getContract (), dbManager );
148+ TransactionResultCapsule ret = new TransactionResultCapsule ();
160149
161- Assert . assertTrue ( checked ) ;
162- }
150+ String ownerAddress = OWNER_ADDRESS ;
151+ byte [] owner_name_array = ByteArray . fromHexString ( ownerAddress );
163152
164- private void processAndCheckResult (
165- PermissionAddKeyActuator actuator ,
166- TransactionResultCapsule ret ,
167- String ownerAddress ,
168- String permissionName ,
169- String keyAddress ,
170- long keyWeight ) {
171153 try {
172154 actuator .validate ();
173155 actuator .execute (ret );
174156 Assert .assertEquals (ret .getInstance ().getRet (), code .SUCESS );
175157
176- checkResult (ownerAddress , permissionName , keyAddress , keyWeight );
177-
158+ AccountCapsule owner = dbManager .getAccountStore ().get (owner_name_array );
159+
160+ List <Permission > expectedPermissions = new ArrayList <>();
161+ Permission ownerPermission =
162+ TransactionCapsule .getDefaultPermission (ByteString .copyFrom (owner_name_array ), "owner" );
163+ Permission activePermission =
164+ Permission .newBuilder ()
165+ .setName ("active" )
166+ .setThreshold (1 )
167+ .setParent ("owner" )
168+ .addKeys (
169+ Key .newBuilder ()
170+ .setAddress (ByteString .copyFrom (ByteArray .fromHexString (ownerAddress )))
171+ .setWeight (1 )
172+ .build ())
173+ .addKeys (
174+ Key .newBuilder ()
175+ .setAddress (ByteString .copyFrom (ByteArray .fromHexString (KEY_ADDRESS )))
176+ .setWeight (KEY_WEIGHT )
177+ .build ())
178+ .build ();
179+ expectedPermissions .add (ownerPermission );
180+ expectedPermissions .add (activePermission );
181+
182+ List <Permission > ownerPermissions = owner .getPermissions ();
183+
184+ Assert .assertEquals (expectedPermissions , ownerPermissions );
178185 Assert .assertTrue (true );
179186 } catch (ContractValidateException e ) {
180187 Assert .assertFalse (e instanceof ContractValidateException );
@@ -183,18 +190,9 @@ private void processAndCheckResult(
183190 }
184191 }
185192
186- @ Test
187- public void successFistAddPermissionKey () {
188- PermissionAddKeyActuator actuator = new PermissionAddKeyActuator (getContract (), dbManager );
189- TransactionResultCapsule ret = new TransactionResultCapsule ();
190-
191- processAndCheckResult (actuator , ret , OWNER_ADDRESS , PERMISSION_NAME , KEY_ADDRESS , KEY_WEIGHT );
192- }
193-
194193 @ Test
195194 public void successSecondAddPermissionKey () {
196195 // init
197- String permissionName = "owner" ;
198196 long keyWeight = 2 ;
199197
200198 byte [] owner_name_array = ByteArray .fromHexString (OWNER_ADDRESS );
@@ -204,11 +202,60 @@ public void successSecondAddPermissionKey() {
204202
205203 PermissionAddKeyActuator actuator =
206204 new PermissionAddKeyActuator (
207- getContract (OWNER_ADDRESS , SECOND_KEY_ADDRESS , keyWeight , permissionName ), dbManager );
205+ getContract (OWNER_ADDRESS , SECOND_KEY_ADDRESS , keyWeight , "owner" ), dbManager );
208206 TransactionResultCapsule ret = new TransactionResultCapsule ();
209207
210- processAndCheckResult (
211- actuator , ret , OWNER_ADDRESS , permissionName , SECOND_KEY_ADDRESS , keyWeight );
208+ try {
209+ actuator .validate ();
210+ actuator .execute (ret );
211+ Assert .assertEquals (ret .getInstance ().getRet (), code .SUCESS );
212+
213+ AccountCapsule owner = dbManager .getAccountStore ().get (owner_name_array );
214+
215+ List <Permission > expectedPermissions = new ArrayList <>();
216+ Permission ownerPermission =
217+ Permission .newBuilder ()
218+ .setName ("owner" )
219+ .setThreshold (1 )
220+ .addKeys (
221+ Key .newBuilder ()
222+ .setAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
223+ .setWeight (1 )
224+ .build ())
225+ .addKeys (
226+ Key .newBuilder ()
227+ .setAddress (ByteString .copyFrom (ByteArray .fromHexString (SECOND_KEY_ADDRESS )))
228+ .setWeight (keyWeight )
229+ .build ())
230+ .build ();
231+ Permission activePermission =
232+ Permission .newBuilder ()
233+ .setName ("active" )
234+ .setThreshold (1 )
235+ .setParent ("owner" )
236+ .addKeys (
237+ Key .newBuilder ()
238+ .setAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
239+ .setWeight (1 )
240+ .build ())
241+ .addKeys (
242+ Key .newBuilder ()
243+ .setAddress (ByteString .copyFrom (ByteArray .fromHexString (KEY_ADDRESS )))
244+ .setWeight (KEY_WEIGHT )
245+ .build ())
246+ .build ();
247+ expectedPermissions .add (ownerPermission );
248+ expectedPermissions .add (activePermission );
249+
250+ List <Permission > ownerPermissions = owner .getPermissions ();
251+
252+ Assert .assertEquals (expectedPermissions , ownerPermissions );
253+ Assert .assertTrue (true );
254+ } catch (ContractValidateException e ) {
255+ Assert .assertFalse (e instanceof ContractValidateException );
256+ } catch (ContractExeException e ) {
257+ Assert .assertFalse (e instanceof ContractExeException );
258+ }
212259 }
213260
214261 @ Test
@@ -220,7 +267,48 @@ public void addOwnerSelfKey() {
220267 getContract (OWNER_ADDRESS , OWNER_ADDRESS , keyWeight , permissionName ), dbManager );
221268 TransactionResultCapsule ret = new TransactionResultCapsule ();
222269
223- processAndCheckResult (actuator , ret , OWNER_ADDRESS , permissionName , OWNER_ADDRESS , keyWeight );
270+ try {
271+ actuator .validate ();
272+ actuator .execute (ret );
273+ Assert .assertEquals (ret .getInstance ().getRet (), code .SUCESS );
274+
275+ AccountCapsule owner =
276+ dbManager .getAccountStore ().get (ByteArray .fromHexString (OWNER_ADDRESS ));
277+
278+ List <Permission > expectedPermissions = new ArrayList <>();
279+ Permission ownerPermission =
280+ Permission .newBuilder ()
281+ .setName ("owner" )
282+ .setThreshold (1 )
283+ .addKeys (
284+ Key .newBuilder ()
285+ .setAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
286+ .setWeight (keyWeight )
287+ .build ())
288+ .build ();
289+ Permission activePermission =
290+ Permission .newBuilder ()
291+ .setName ("active" )
292+ .setThreshold (1 )
293+ .setParent ("owner" )
294+ .addKeys (
295+ Key .newBuilder ()
296+ .setAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
297+ .setWeight (1 )
298+ .build ())
299+ .build ();
300+ expectedPermissions .add (ownerPermission );
301+ expectedPermissions .add (activePermission );
302+
303+ List <Permission > ownerPermissions = owner .getPermissions ();
304+
305+ Assert .assertEquals (expectedPermissions , ownerPermissions );
306+ Assert .assertTrue (true );
307+ } catch (ContractValidateException e ) {
308+ Assert .assertFalse (e instanceof ContractValidateException );
309+ } catch (ContractExeException e ) {
310+ Assert .assertFalse (e instanceof ContractExeException );
311+ }
224312 }
225313
226314 @ Test
0 commit comments