Skip to content

Commit a458075

Browse files
authored
Merge pull request tronprotocol#2267 from tronprotocol/hotfix/new_storage_create2_1
create2 new storage-key
2 parents 9a4f9f8 + dfff2c7 commit a458075

File tree

5 files changed

+18
-22
lines changed

5 files changed

+18
-22
lines changed

src/main/java/org/tron/common/runtime/RuntimeImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public long getTotalEnergyLimitWithFixRatio(AccountCapsule creator, AccountCapsu
315315
public long getTotalEnergyLimit(AccountCapsule creator, AccountCapsule caller,
316316
TriggerSmartContract contract, long feeLimit, long callValue)
317317
throws ContractValidateException {
318-
if (VMConfig.allowTvmConstantinople() && Objects.isNull(creator)) {
318+
if (Objects.isNull(creator) && VMConfig.allowTvmConstantinople()) {
319319
return getAccountEnergyLimitWithFixRatio(caller, feeLimit, callValue);
320320
}
321321
// according to version

src/main/java/org/tron/common/runtime/vm/program/Program.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ public void createContract(DataWord value, DataWord memStart, DataWord memSize)
453453
createContractImpl(value, programCode, newAddress, false);
454454
}
455455

456-
private void createContractImpl(DataWord value, byte[] programCode, byte[] newAddress
457-
, boolean isCreate2) {
456+
private void createContractImpl(DataWord value, byte[] programCode, byte[] newAddress,
457+
boolean isCreate2) {
458458
byte[] senderAddress = convertToTronAddress(this.getContractAddress().getLast20Bytes());
459459

460460
if (logger.isDebugEnabled()) {

src/main/java/org/tron/common/runtime/vm/program/Storage.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.tron.common.crypto.Hash;
99
import org.tron.common.runtime.vm.DataWord;
1010
import org.tron.common.utils.ByteUtil;
11-
import org.tron.core.capsule.ContractCapsule;
1211
import org.tron.core.capsule.StorageRowCapsule;
1312
import org.tron.core.db.StorageRowStore;
1413

@@ -21,26 +20,25 @@ public class Storage {
2120
@Getter
2221
private final Map<DataWord, StorageRowCapsule> rowCache = new HashMap<>();
2322

23+
@Getter
24+
private byte[] address;
25+
2426
private static final int PREFIX_BYTES = 16;
2527

2628
public Storage(byte[] address, StorageRowStore store) {
2729
addrHash = addrHash(address);
30+
this.address = address;
2831
this.store = store;
2932
}
3033

31-
public Storage(byte[] address, StorageRowStore store, ContractCapsule contract) {
32-
byte[] trxHash;
33-
if (contract == null) {
34-
trxHash = new byte[0];
35-
} else {
36-
trxHash = contract.getTrxHash();
37-
}
38-
addrHash = addrHash(address, trxHash);
39-
this.store = store;
34+
public void generateAddrHash(byte[] trxId) {
35+
// update addreHash for create2
36+
addrHash = addrHash(address, trxId);
4037
}
4138

4239
public Storage(Storage storage) {
4340
this.addrHash = storage.addrHash.clone();
41+
this.address = storage.getAddress().clone();
4442
this.store = storage.store;
4543
storage.getRowCache().forEach((DataWord rowKey, StorageRowCapsule row) -> {
4644
StorageRowCapsule newRow = new StorageRowCapsule(row);
@@ -90,7 +88,6 @@ private static byte[] addrHash(byte[] address, byte[] trxHash) {
9088
return Hash.sha3(ByteUtil.merge(address, trxHash));
9189
}
9290

93-
9491
public void commit() {
9592
rowCache.forEach((DataWord rowKey, StorageRowCapsule row) -> {
9693
if (row.isDirty()) {

src/main/java/org/tron/common/storage/DepositImpl.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,22 +329,21 @@ public synchronized Storage getStorage(byte[] address) {
329329
if (storageCache.containsKey(key)) {
330330
return storageCache.get(key);
331331
}
332-
333332
Storage storage;
334333
if (this.parent != null) {
335334
Storage parentStorage = parent.getStorage(address);
336335
if (VMConfig.getEnergyLimitHardFork()) {
336+
// deep copy
337337
storage = new Storage(parentStorage);
338338
} else {
339339
storage = parentStorage;
340340
}
341341
} else {
342-
if (VMConfig.allowTvmConstantinople()) {
343-
ContractCapsule contract = getContract(address);
344-
storage = new Storage(address, dbManager.getStorageRowStore(), contract);
345-
} else {
346-
storage = new Storage(address, dbManager.getStorageRowStore());
347-
}
342+
storage = new Storage(address, dbManager.getStorageRowStore());
343+
}
344+
ContractCapsule contract = getContract(address);
345+
if (contract != null && !ByteUtil.isNullOrZeroArray(contract.getTrxHash())) {
346+
storage.generateAddrHash(contract.getTrxHash());
348347
}
349348
return storage;
350349
}

src/main/java/org/tron/core/capsule/ReceiptCapsule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void payEnergyBill(Manager manager, AccountCapsule origin, AccountCapsule
106106
return;
107107
}
108108

109-
if (VMConfig.allowTvmConstantinople() && Objects.isNull(origin)) {
109+
if (Objects.isNull(origin) && VMConfig.allowTvmConstantinople()) {
110110
payEnergyBill(manager, caller, receipt.getEnergyUsageTotal(), energyProcessor, now);
111111
return;
112112
}

0 commit comments

Comments
 (0)