Skip to content

Commit f3e8140

Browse files
feat: improve node stability and execution efficiency (#7007)
* feat(vm): add optimized storage layout and alias write checks * feat(api): separate outbound JSON parsing * perf(vm): cache alias check at Storage construction --------- Co-authored-by: halibobo1205 <halibobo1205@gmail.com>
1 parent d5c3d1d commit f3e8140

36 files changed

Lines changed: 1343 additions & 94 deletions

‎actuator/src/main/java/org/tron/core/actuator/VMActuator.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ public void execute(Object object) throws ContractExeException {
299299
result.setRuntimeError(result.getException().getMessage());
300300
}
301301
logger.info("runtime result is :{}", result.getException().getMessage());
302+
} finally {
303+
if (isConstantCall) {
304+
VMConfig.clearLocalSnapshot();
305+
}
302306
}
303307
//use program returned fill context
304308
context.setProgramResult(result);

‎actuator/src/main/java/org/tron/core/utils/ProposalUtil.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
941941
}
942942
break;
943943
}
944+
case ALLOW_OPTIMIZE_TVM_STORAGE: {
945+
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_2_3)) {
946+
throw new ContractValidateException(
947+
"Bad chain parameter id [ALLOW_OPTIMIZE_TVM_STORAGE]");
948+
}
949+
if (dynamicPropertiesStore.getAllowOptimizeTvmStorage() == 1) {
950+
throw new ContractValidateException(
951+
"[ALLOW_OPTIMIZE_TVM_STORAGE] has been valid, no need to propose again");
952+
}
953+
if (value != 1) {
954+
throw new ContractValidateException(
955+
"This value[ALLOW_OPTIMIZE_TVM_STORAGE] is only allowed to be 1");
956+
}
957+
break;
958+
}
944959
default:
945960
break;
946961
}
@@ -1029,7 +1044,8 @@ public enum ProposalType { // current value, value range
10291044
ALLOW_TVM_PRAGUE(95), // 0, 1
10301045
ALLOW_TVM_OSAKA(96), // 0, 1
10311046
ALLOW_HARDEN_RESOURCE_CALCULATION(97), // 0, 1
1032-
ALLOW_HARDEN_EXCHANGE_CALCULATION(98); // 0, 1
1047+
ALLOW_HARDEN_EXCHANGE_CALCULATION(98), // 0, 1
1048+
ALLOW_OPTIMIZE_TVM_STORAGE(99); // 0, 1
10331049
private long code;
10341050

10351051
ProposalType(long code) {

‎actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,13 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
982982
}
983983

984984
PairingCheck check = PairingCheck.create();
985+
long deadlineNs = pairingDeadlineNs();
985986

986987
// iterating over all pairs
987988
for (int offset = 0; offset < data.length; offset += PAIR_SIZE) {
989+
if (deadlineNs < System.nanoTime()) {
990+
throw Program.Exception.notEnoughTime("precompiled contract pair");
991+
}
988992

989993
Pair<BN128G1, BN128G2> pair = decodePair(data, offset);
990994

@@ -996,12 +1000,23 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
9961000
check.addPair(pair.getLeft(), pair.getRight());
9971001
}
9981002

999-
check.run();
1003+
if (!check.run(deadlineNs)) {
1004+
throw Program.Exception.notEnoughTime("precompiled contract pair");
1005+
}
10001006
int result = check.result();
10011007

10021008
return Pair.of(true, new DataWord(result).getData());
10031009
}
10041010

1011+
/** Same skip as {@link Program#checkCPUTimeLimit}: debug and solidity nodes do not enforce it. */
1012+
private long pairingDeadlineNs() {
1013+
if (CommonParameter.getInstance().isDebug()
1014+
|| CommonParameter.getInstance().isSolidityNode()) {
1015+
return Long.MAX_VALUE;
1016+
}
1017+
return getVmShouldEndInUs() * VMConstant.ONE_THOUSAND;
1018+
}
1019+
10051020
private Pair<BN128G1, BN128G2> decodePair(byte[] in, int offset) {
10061021

10071022
byte[] x = parseWord(in, offset, 0);

‎actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static void load(StoreFactory storeFactory, boolean isolate) {
5050
snapshot.allowTvmSelfdestructRestriction = ds.getAllowTvmSelfdestructRestriction() == 1;
5151
snapshot.allowTvmOsaka = ds.getAllowTvmOsaka() == 1;
5252
snapshot.allowHardenResourceCalculation = ds.getAllowHardenResourceCalculation() == 1;
53+
snapshot.allowOptimizeTvmStorage = ds.getAllowOptimizeTvmStorage() == 1;
5354
if (isolate) {
5455
VMConfig.setLocalSnapshot(snapshot);
5556
} else {

‎actuator/src/main/java/org/tron/core/vm/program/Storage.java‎

Lines changed: 143 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,27 @@
99
import org.tron.common.crypto.Hash;
1010
import org.tron.common.runtime.vm.DataWord;
1111
import org.tron.common.utils.ByteUtil;
12+
import org.tron.common.utils.ForkController;
1213
import org.tron.core.capsule.StorageRowCapsule;
14+
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
1315
import org.tron.core.store.StorageRowStore;
1416

1517
public 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
}

‎actuator/src/main/java/org/tron/core/vm/repository/RepositoryImpl.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,8 @@ public Storage getStorage(byte[] address) {
739739
storage = parentStorage;
740740
}
741741
} else {
742-
storage = new Storage(address, getStorageRowStore());
742+
storage = new Storage(address, getStorageRowStore(),
743+
VMConfig.allowOptimizeTvmStorage());
743744
}
744745
ContractCapsule contract = getContract(address);
745746
if (contract != null) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.tron.core.vm;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.assertThrows;
6+
7+
import java.math.BigInteger;
8+
import org.apache.commons.lang3.tuple.Pair;
9+
import org.junit.Test;
10+
import org.tron.core.vm.program.Program.OutOfTimeException;
11+
12+
public class Bn128PairingTimeoutForkTest {
13+
14+
@Test
15+
public void expiredDeadlineTimesOut() {
16+
PrecompiledContracts.BN128Pairing pairing = pairing(0);
17+
assertThrows(OutOfTimeException.class, () -> pairing.execute(onePair()));
18+
}
19+
20+
@Test
21+
public void futureDeadlineReturnsPairingResult() {
22+
PrecompiledContracts.BN128Pairing pairing = pairing(Long.MAX_VALUE / 1000);
23+
Pair<Boolean, byte[]> out = pairing.execute(onePair());
24+
assertTrue(out.getLeft());
25+
assertEquals(32, out.getRight().length);
26+
}
27+
28+
private static PrecompiledContracts.BN128Pairing pairing(long vmShouldEndInUs) {
29+
PrecompiledContracts.BN128Pairing pairing = new PrecompiledContracts.BN128Pairing();
30+
pairing.setVmShouldEndInUs(vmShouldEndInUs);
31+
return pairing;
32+
}
33+
34+
private static byte[] onePair() {
35+
byte[] input = new byte[192];
36+
write(input, 0, BigInteger.ONE);
37+
write(input, 32, BigInteger.valueOf(2));
38+
write(input, 64, new BigInteger(
39+
"11559732032986387107991004021392285783925812861821192530917403151452391805634"));
40+
write(input, 96, new BigInteger(
41+
"10857046999023057135944570762232829481370756359578518086990519993285655852781"));
42+
write(input, 128, new BigInteger(
43+
"4082367875863433681332203403145435568316851327593401208105741076214120093531"));
44+
write(input, 160, new BigInteger(
45+
"8495653923123431417604973247489272438418190587263600148770280649306958101930"));
46+
return input;
47+
}
48+
49+
private static void write(byte[] dest, int offset, BigInteger value) {
50+
byte[] raw = value.toByteArray();
51+
int src = raw.length > 32 ? raw.length - 32 : 0;
52+
System.arraycopy(raw, src, dest, offset + 32 - (raw.length - src), raw.length - src);
53+
}
54+
}

0 commit comments

Comments
 (0)