Skip to content

Commit 73d7536

Browse files
Merge pull request tronprotocol#2259 from tronprotocol/hotfix/new_storage_create2
create2 new storage-key
2 parents 33a6f21 + 1b8cea5 commit 73d7536

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import org.tron.protos.Protocol;
8080
import org.tron.protos.Protocol.AccountType;
8181
import org.tron.protos.Protocol.SmartContract;
82+
import org.tron.protos.Protocol.SmartContract.Builder;
8283

8384
/**
8485
* @author Roman Mandeleil
@@ -449,10 +450,11 @@ public void createContract(DataWord value, DataWord memStart, DataWord memSize)
449450
byte[] newAddress = Wallet
450451
.generateContractAddress(rootTransactionId, nonce);
451452

452-
createContractImpl(value, programCode, newAddress);
453+
createContractImpl(value, programCode, newAddress, false);
453454
}
454455

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

458460
if (logger.isDebugEnabled()) {
@@ -485,9 +487,14 @@ private void createContractImpl(DataWord value, byte[] programCode, byte[] newAd
485487
}
486488

487489
if (!contractAlreadyExists) {
488-
SmartContract newSmartContract = SmartContract.newBuilder()
489-
.setContractAddress(ByteString.copyFrom(newAddress)).setConsumeUserResourcePercent(100)
490-
.setOriginAddress(ByteString.copyFrom(senderAddress)).build();
490+
Builder builder = SmartContract.newBuilder();
491+
builder.setContractAddress(ByteString.copyFrom(newAddress))
492+
.setConsumeUserResourcePercent(100)
493+
.setOriginAddress(ByteString.copyFrom(senderAddress));
494+
if (isCreate2) {
495+
builder.setTrxHash(ByteString.copyFrom(rootTransactionId));
496+
}
497+
SmartContract newSmartContract = builder.build();
491498
deposit.createContract(newAddress, new ContractCapsule(newSmartContract));
492499
}
493500
} else {
@@ -1237,7 +1244,7 @@ public void createContract2(DataWord value, DataWord memStart, DataWord memSize,
12371244

12381245
byte[] contractAddress = Wallet
12391246
.generateContractAddress2(senderAddress, salt.getData(), programCode);
1240-
createContractImpl(value, programCode, contractAddress);
1247+
createContractImpl(value, programCode, contractAddress, true);
12411248
}
12421249

12431250
static class ByteCodeIterator {

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import java.util.HashMap;
66
import java.util.Map;
77
import lombok.Getter;
8-
import org.spongycastle.util.encoders.Hex;
98
import org.tron.common.crypto.Hash;
109
import org.tron.common.runtime.vm.DataWord;
10+
import org.tron.common.utils.ByteUtil;
11+
import org.tron.core.capsule.ContractCapsule;
1112
import org.tron.core.capsule.StorageRowCapsule;
1213
import org.tron.core.db.StorageRowStore;
1314

@@ -27,6 +28,17 @@ public Storage(byte[] address, StorageRowStore store) {
2728
this.store = store;
2829
}
2930

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;
40+
}
41+
3042
public Storage(Storage storage) {
3143
this.addrHash = storage.addrHash.clone();
3244
this.store = storage.store;
@@ -71,6 +83,14 @@ private static byte[] addrHash(byte[] address) {
7183
return Hash.sha3(address);
7284
}
7385

86+
private static byte[] addrHash(byte[] address, byte[] trxHash) {
87+
if (ByteUtil.isNullOrZeroArray(trxHash)) {
88+
return Hash.sha3(address);
89+
}
90+
return Hash.sha3(ByteUtil.merge(address, trxHash));
91+
}
92+
93+
7494
public void commit() {
7595
rowCache.forEach((DataWord rowKey, StorageRowCapsule row) -> {
7696
if (row.isDirty()) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,12 @@ public synchronized Storage getStorage(byte[] address) {
339339
storage = parentStorage;
340340
}
341341
} else {
342-
storage = new Storage(address, dbManager.getStorageRowStore());
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+
}
343348
}
344349
return storage;
345350
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ public long getOriginEnergyLimit() {
113113
public void clearABI() {
114114
this.smartContract = this.smartContract.toBuilder().setAbi(ABI.getDefaultInstance()).build();
115115
}
116+
117+
public byte[] getTrxHash() {
118+
return this.smartContract.getTrxHash().toByteArray();
119+
}
116120
}

src/main/protos/core/Tron.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ message SmartContract {
543543
string name = 7;
544544
int64 origin_energy_limit = 8;
545545
bytes code_hash = 9;
546+
bytes trx_hash = 10;
546547
}
547548

548549
message InternalTransaction {

0 commit comments

Comments
 (0)