99import org .tron .common .crypto .Hash ;
1010import org .tron .common .runtime .vm .DataWord ;
1111import org .tron .common .utils .ByteUtil ;
12+ import org .tron .common .utils .ForkController ;
1213import org .tron .core .capsule .StorageRowCapsule ;
14+ import org .tron .core .config .Parameter .ForkBlockVersionEnum ;
1315import org .tron .core .store .StorageRowStore ;
1416
1517public class Storage {
1618
1719 private static final int PREFIX_BYTES = 16 ;
20+ private static final int WORD_BYTES = 32 ;
21+
22+ private enum ReadKind {
23+ NEW , OLD , EMPTY
24+ }
25+
1826 @ Getter
1927 private final Map <DataWord , StorageRowCapsule > rowCache = new HashMap <>();
28+ private final Map <DataWord , DataWord > oldRowKeyOwners = new HashMap <>();
29+ private final Map <DataWord , ReadKind > readKinds = new HashMap <>();
30+ private final boolean optimizeTvmStorage ;
31+ private final boolean aliasCheckEnabled ;
32+
2033 @ Getter
2134 private byte [] addrHash ;
2235 @ Getter
@@ -26,21 +39,29 @@ public class Storage {
2639 @ Setter
2740 private int contractVersion ;
2841
29- public Storage (byte [] address , StorageRowStore store ) {
42+ public Storage (byte [] address , StorageRowStore store , boolean optimizeTvmStorage ) {
3043 addrHash = addrHash (address );
3144 this .address = address ;
3245 this .store = store ;
46+ this .optimizeTvmStorage = optimizeTvmStorage ;
47+ this .aliasCheckEnabled = ForkController .instance ().pass (ForkBlockVersionEnum .VERSION_4_8_2_3 );
3348 }
3449
3550 public Storage (Storage storage ) {
3651 this .addrHash = storage .addrHash .clone ();
3752 this .address = storage .getAddress ().clone ();
3853 this .store = storage .store ;
3954 this .contractVersion = storage .contractVersion ;
40- storage .getRowCache ().forEach ((DataWord rowKey , StorageRowCapsule row ) -> {
55+ this .optimizeTvmStorage = storage .optimizeTvmStorage ;
56+ this .aliasCheckEnabled = storage .aliasCheckEnabled ;
57+ storage .getRowCache ().forEach ((DataWord key , StorageRowCapsule row ) -> {
4158 StorageRowCapsule newRow = new StorageRowCapsule (row );
42- this .rowCache .put (rowKey .clone (), newRow );
59+ this .rowCache .put (key .clone (), newRow );
4360 });
61+ storage .oldRowKeyOwners .forEach ((DataWord legacy , DataWord owner ) ->
62+ this .oldRowKeyOwners .put (legacy .clone (), owner .clone ()));
63+ storage .readKinds .forEach ((DataWord key , ReadKind kind ) ->
64+ this .readKinds .put (key .clone (), kind ));
4465 }
4566
4667 private byte [] compose (byte [] key , byte [] addrHash ) {
@@ -53,6 +74,18 @@ private byte[] compose(byte[] key, byte[] addrHash) {
5374 return result ;
5475 }
5576
77+ private byte [] getOldRowKey (DataWord key ) {
78+ return compose (key .getData (), addrHash );
79+ }
80+
81+ private byte [] getNewRowKey (DataWord key ) {
82+ byte [] result = new byte [PREFIX_BYTES + WORD_BYTES ];
83+ arraycopy (addrHash , 0 , result , 0 , PREFIX_BYTES );
84+ arraycopy (Hash .sha3 (ByteUtil .merge (addrHash , key .getData ())), 0 , result , PREFIX_BYTES ,
85+ WORD_BYTES );
86+ return result ;
87+ }
88+
5689 // 32 bytes
5790 private static byte [] addrHash (byte [] address ) {
5891 return Hash .sha3 (address );
@@ -71,6 +104,12 @@ public void generateAddrHash(byte[] trxId) {
71104 }
72105
73106 public DataWord getValue (DataWord key ) {
107+ if (optimizeTvmStorage ) {
108+ return getOptimized (key );
109+ }
110+ if (aliasCheckEnabled ) {
111+ checkAlias (key );
112+ }
74113 if (rowCache .containsKey (key )) {
75114 return new DataWord (rowCache .get (key ).getValue ());
76115 } else {
@@ -84,17 +123,88 @@ public DataWord getValue(DataWord key) {
84123 }
85124
86125 public void put (DataWord key , DataWord value ) {
126+ if (!optimizeTvmStorage && aliasCheckEnabled ) {
127+ checkAlias (key );
128+ }
87129 if (rowCache .containsKey (key )) {
88130 rowCache .get (key ).setValue (value .getData ());
89131 } else {
90- byte [] rowKey = compose (key . getData (), addrHash );
132+ byte [] rowKey = optimizeTvmStorage ? getNewRowKey (key ) : getOldRowKey ( key );
91133 StorageRowCapsule row = new StorageRowCapsule (rowKey , value .getData ());
92134 rowCache .put (key , row );
93135 }
136+ if (optimizeTvmStorage ) {
137+ ownOldKey (key );
138+ }
139+ }
140+
141+ private DataWord getOptimized (DataWord key ) {
142+ if (rowCache .containsKey (key )) {
143+ return new DataWord (rowCache .get (key ).getValue ());
144+ }
145+ if (readKinds .get (key ) == ReadKind .EMPTY
146+ || readKinds .get (key ) == ReadKind .NEW ) {
147+ return null ;
148+ }
149+
150+ byte [] newRowKey = getNewRowKey (key );
151+ StorageRowCapsule newRow = store .get (newRowKey );
152+ if (newRow != null && newRow .getValue () != null ) {
153+ readKinds .put (key .clone (), ReadKind .NEW );
154+ if (DataWord .isZero (newRow .getValue ())) {
155+ return null ;
156+ }
157+ return cacheRead (key , newRowKey , newRow .getValue ());
158+ }
159+
160+ if (oldKeyTaken (key )) {
161+ return null ;
162+ }
163+
164+ byte [] oldRowKey = getOldRowKey (key );
165+ StorageRowCapsule oldRow = store .get (oldRowKey );
166+ if (oldRow != null && oldRow .getValue () != null ) {
167+ readKinds .put (key .clone (), ReadKind .OLD );
168+ return cacheRead (key , oldRowKey , oldRow .getValue ());
169+ }
170+
171+ readKinds .put (key .clone (), ReadKind .EMPTY );
172+ return null ;
173+ }
174+
175+ private DataWord cacheRead (DataWord key , byte [] rowKey , byte [] value ) {
176+ StorageRowCapsule row = new StorageRowCapsule (value .clone ());
177+ row .setRowKey (rowKey );
178+ rowCache .put (key , row );
179+ ownOldKey (key );
180+ return new DataWord (row .getValue ());
181+ }
182+
183+ private void checkAlias (DataWord key ) {
184+ DataWord oldRowKey = new DataWord (getOldRowKey (key ));
185+ DataWord owner = oldRowKeyOwners .get (oldRowKey );
186+ if (owner == null ) {
187+ oldRowKeyOwners .put (oldRowKey , key .clone ());
188+ } else if (!owner .equals (key )) {
189+ throw new Program .OutOfTimeException ("CPU timeout for storage check" );
190+ }
191+ }
192+
193+ private void ownOldKey (DataWord key ) {
194+ oldRowKeyOwners .putIfAbsent (new DataWord (getOldRowKey (key )), key .clone ());
195+ }
196+
197+ private boolean oldKeyTaken (DataWord key ) {
198+ DataWord owner = oldRowKeyOwners .get (new DataWord (getOldRowKey (key )));
199+ return owner != null && !owner .equals (key );
94200 }
95201
96202 public void commit () {
97- rowCache .forEach ((DataWord rowKey , StorageRowCapsule row ) -> {
203+ if (optimizeTvmStorage ) {
204+ commitOptimized ();
205+ return ;
206+ }
207+ rowCache .forEach ((DataWord key , StorageRowCapsule row ) -> {
98208 if (row .isDirty ()) {
99209 if (new DataWord (row .getValue ()).isZero ()) {
100210 this .store .delete (row .getRowKey ());
@@ -104,4 +214,32 @@ public void commit() {
104214 }
105215 });
106216 }
217+
218+ private void commitOptimized () {
219+ rowCache .forEach ((DataWord key , StorageRowCapsule row ) -> {
220+ if (!row .isDirty ()) {
221+ return ;
222+ }
223+ putNew (key , row .getValue ());
224+ ReadKind kind = readKinds .get (key );
225+ if (kind == null || kind == ReadKind .OLD ) {
226+ store .delete (getOldRowKey (key ));
227+ }
228+ });
229+ readKinds .forEach ((DataWord key , ReadKind kind ) -> {
230+ StorageRowCapsule row = rowCache .get (key );
231+ if (row != null && row .isDirty ()) {
232+ return ;
233+ }
234+ if (kind == ReadKind .OLD ) {
235+ putNew (key , row .getValue ());
236+ store .delete (getOldRowKey (key ));
237+ }
238+ });
239+ }
240+
241+ private void putNew (DataWord key , byte [] value ) {
242+ store .put (getNewRowKey (key ), new StorageRowCapsule (value ));
243+ }
244+
107245}
0 commit comments