11package org .tron .core .store ;
22
3+ import com .google .protobuf .ByteString ;
4+ import java .util .ArrayList ;
5+ import java .util .Comparator ;
6+ import java .util .List ;
7+ import java .util .stream .Collectors ;
38import org .apache .commons .lang3 .ArrayUtils ;
49import org .springframework .beans .factory .annotation .Autowired ;
510import org .springframework .beans .factory .annotation .Value ;
1116public class DelegatedResourceAccountIndexStore extends
1217 TronStoreWithRevoking <DelegatedResourceAccountIndexCapsule > {
1318
19+ private static final byte [] FROM_PREFIX = {0x01 };
20+ private static final byte [] TO_PREFIX = {0x02 };
21+
1422 @ Autowired
1523 public DelegatedResourceAccountIndexStore (@ Value ("DelegatedResourceAccountIndex" ) String dbName ) {
1624 super (dbName );
@@ -23,4 +31,85 @@ public DelegatedResourceAccountIndexCapsule get(byte[] key) {
2331 return ArrayUtils .isEmpty (value ) ? null : new DelegatedResourceAccountIndexCapsule (value );
2432 }
2533
34+ private byte [] createKey (byte [] prefix , byte [] address1 , byte [] address2 ) {
35+ byte [] key = new byte [prefix .length + address1 .length + address2 .length ];
36+ System .arraycopy (prefix , 0 , key , 0 , prefix .length );
37+ System .arraycopy (address1 , 0 , key , prefix .length , address1 .length );
38+ System .arraycopy (address2 , 0 , key , prefix .length + address1 .length , address2 .length );
39+ return key ;
40+ }
41+
42+ public void convert (byte [] address ) {
43+ DelegatedResourceAccountIndexCapsule indexCapsule = this .get (address );
44+ if (indexCapsule == null ) {
45+ // convert complete or have no delegate
46+ return ;
47+ }
48+ // convert old data
49+ List <ByteString > toList = indexCapsule .getToAccountsList ();
50+ for (int i = 0 ; i < toList .size (); i ++) {
51+ // use index as the timestamp, just to keep index in order
52+ this .delegate (address , toList .get (i ).toByteArray (), i + 1L );
53+ }
54+
55+ List <ByteString > fromList = indexCapsule .getFromAccountsList ();
56+ for (int i = 0 ; i < fromList .size (); i ++) {
57+ // use index as the timestamp, just to keep index in order
58+ this .delegate (fromList .get (i ).toByteArray (), address , i + 1L );
59+ }
60+ this .delete (address );
61+ }
62+
63+ public void delegate (byte [] from , byte [] to , long time ) {
64+ byte [] fromKey = createKey (FROM_PREFIX , from , to );
65+ DelegatedResourceAccountIndexCapsule toIndexCapsule =
66+ new DelegatedResourceAccountIndexCapsule (ByteString .copyFrom (to ));
67+ toIndexCapsule .setTimestamp (time );
68+ this .put (fromKey , toIndexCapsule );
69+
70+ byte [] toKey = createKey (TO_PREFIX , to , from );
71+ DelegatedResourceAccountIndexCapsule fromIndexCapsule =
72+ new DelegatedResourceAccountIndexCapsule (ByteString .copyFrom (from ));
73+ fromIndexCapsule .setTimestamp (time );
74+ this .put (toKey , fromIndexCapsule );
75+ }
76+
77+ public void unDelegate (byte [] from , byte [] to ) {
78+ byte [] fromKey = createKey (FROM_PREFIX , from , to );
79+ this .delete (fromKey );
80+ byte [] toKey = createKey (TO_PREFIX , to , from );
81+ this .delete (toKey );
82+ }
83+
84+ public DelegatedResourceAccountIndexCapsule getIndex (byte [] address ) {
85+ DelegatedResourceAccountIndexCapsule indexCapsule = get (address );
86+ if (indexCapsule != null ) {
87+ return indexCapsule ;
88+ }
89+
90+ DelegatedResourceAccountIndexCapsule tmpIndexCapsule =
91+ new DelegatedResourceAccountIndexCapsule (ByteString .copyFrom (address ));
92+ byte [] key = new byte [FROM_PREFIX .length + address .length ];
93+
94+ System .arraycopy (FROM_PREFIX , 0 , key , 0 , FROM_PREFIX .length );
95+ System .arraycopy (address , 0 , key , FROM_PREFIX .length , address .length );
96+ List <DelegatedResourceAccountIndexCapsule > tmpToList =
97+ new ArrayList <>(this .prefixQuery (key ).values ());
98+
99+ tmpToList .sort (Comparator .comparing (DelegatedResourceAccountIndexCapsule ::getTimestamp ));
100+ List <ByteString > list = tmpToList .stream ()
101+ .map (DelegatedResourceAccountIndexCapsule ::getAccount ).collect (Collectors .toList ());
102+ tmpIndexCapsule .setAllToAccounts (list );
103+
104+ System .arraycopy (TO_PREFIX , 0 , key , 0 , TO_PREFIX .length );
105+ System .arraycopy (address , 0 , key , TO_PREFIX .length , address .length );
106+ List <DelegatedResourceAccountIndexCapsule > tmpFromList =
107+ new ArrayList <>(this .prefixQuery (key ).values ());
108+ tmpFromList .sort (Comparator .comparing (DelegatedResourceAccountIndexCapsule ::getTimestamp ));
109+ list = tmpFromList .stream ().map (DelegatedResourceAccountIndexCapsule ::getAccount ).collect (
110+ Collectors .toList ());
111+ tmpIndexCapsule .setAllFromAccounts (list );
112+ return tmpIndexCapsule ;
113+ }
114+
26115}
0 commit comments