Skip to content

Commit bf853ad

Browse files
taihaofuhuzhenyuan
authored andcommitted
add change address function to adapt tron address
1 parent fb2194b commit bf853ad

6 files changed

Lines changed: 44 additions & 32 deletions

File tree

‎src/main/java/org/tron/common/runtime/Runtime.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private void call() {
173173
this.program = new Program(null, code, programInvoke,
174174
new InternalTransaction(trx), config);
175175
}
176-
//transfer
176+
177177
}
178178

179179
/*
@@ -221,6 +221,13 @@ private void create() {
221221
deposit.createAccount(newContractAddress, Protocol.AccountType.Contract);
222222
deposit.createContract(newContractAddress, new ContractCapsule(trx));
223223
deposit.saveCode(newContractAddress, ProgramPrecompile.getCode(code));
224+
//
225+
// // transfer from callerAddress to contractAddress according to callValue
226+
// byte[] callerAddress = contract.getOwnerAddress().toByteArray();
227+
// byte[] callValue = contract.getCallValue().toByteArray();
228+
// long callValueLong = new BigInteger(Hex.toHexString(callValue),16).longValue();
229+
// this.deposit.addBalance(callerAddress, - callValueLong);
230+
// this.deposit.addBalance(newContractAddress, callValueLong);
224231
}
225232

226233
public void go() {

‎src/main/java/org/tron/common/runtime/utils/MUtil.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.spongycastle.util.encoders.Hex;
44
import org.tron.common.storage.Deposit;
5+
import org.tron.core.Constant;
56

67
/**
78
* @author Guo Yonggang
@@ -20,4 +21,15 @@ public static void burn(Deposit deposit, byte[] address, long amount) {
2021
if (deposit.getBalance(address) < amount) throw new RuntimeException("Not enough balance!");
2122
deposit.addBalance(address, -amount);
2223
}
24+
25+
public static byte[] convertToTronAddress(byte[] address){
26+
if (address.length == 20) {
27+
byte [] newAddress = new byte [21];
28+
byte[] temp = new byte[]{Constant.ADD_PRE_FIX_BYTE_MAINNET};
29+
System.arraycopy(temp, 0, newAddress, 0, temp.length);
30+
System.arraycopy(address, 0, newAddress, temp.length, address.length);
31+
address = newAddress;
32+
}
33+
return address;
34+
}
2335
}

‎src/main/java/org/tron/common/runtime/vm/VM.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ else if (oldValue != null && newValue.isZero()) {
222222
gasLeft.sub(new DataWord(dropCost));
223223
//adjustedCallGas = tronConfig.getCallGas(op, callGasWord, gasLeft);
224224
//adjustedCallGas = new DataWord(tronConfig.getDropCost().getCALL());
225+
//TODO: remove below and recover above statment when config is ready
226+
adjustedCallGas = new DataWord();
225227
dropCost += adjustedCallGas.longValueSafe();
226228
break;
227229
case CREATE:

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import static java.lang.StrictMath.min;
5454
import static java.lang.String.format;
5555
import static org.apache.commons.lang3.ArrayUtils.*;
56+
import static org.tron.common.runtime.utils.MUtil.convertToTronAddress;
5657
import static org.tron.common.runtime.utils.MUtil.transfer;
5758
import static org.tron.common.utils.BIUtil.isPositive;
5859
import static org.tron.common.utils.BIUtil.toBI;
@@ -359,8 +360,8 @@ public void allocateMemory(int offset, int size) {
359360

360361
public void suicide(DataWord obtainerAddress) {
361362

362-
byte[] owner = getOwnerAddress().getLast20Bytes();
363-
byte[] obtainer = obtainerAddress.getLast20Bytes();
363+
byte[] owner = convertToTronAddress(getOwnerAddress().getLast20Bytes());
364+
byte[] obtainer = convertToTronAddress(obtainerAddress.getLast20Bytes());
364365
long balance = getStorage().getBalance(owner);
365366

366367
if (logger.isInfoEnabled())
@@ -393,7 +394,7 @@ public void createContract(DataWord value, DataWord memStart, DataWord memSize)
393394
return;
394395
}
395396

396-
byte[] senderAddress = this.getOwnerAddress().getLast20Bytes();
397+
byte[] senderAddress = convertToTronAddress(this.getOwnerAddress().getLast20Bytes());
397398
long endowment = value.value().longValue();
398399
if (getStorage().getBalance(senderAddress) < endowment) {
399400
stackPushZero();
@@ -510,7 +511,7 @@ this, new DataWord(newAddress), getOwnerAddress(), value,
510511
refundGas(refundGas, "remain gas from the internal call");
511512
if (logger.isInfoEnabled()) {
512513
logger.info("The remaining gas is refunded, account: [{}], gas: [{}] ",
513-
Hex.toHexString(getOwnerAddress().getLast20Bytes()),
514+
Hex.toHexString(convertToTronAddress(getOwnerAddress().getLast20Bytes())),
514515
refundGas);
515516
}
516517
}
@@ -536,8 +537,8 @@ public void callToAddress(MessageCall msg) {
536537
byte[] data = memoryChunk(msg.getInDataOffs().intValue(), msg.getInDataSize().intValue());
537538

538539
// FETCH THE SAVED STORAGE
539-
byte[] codeAddress = msg.getCodeAddress().getLast20Bytes();
540-
byte[] senderAddress = getOwnerAddress().getLast20Bytes();
540+
byte[] codeAddress = convertToTronAddress(msg.getCodeAddress().getLast20Bytes());
541+
byte[] senderAddress = convertToTronAddress(getOwnerAddress().getLast20Bytes());
541542
byte[] contextAddress = msg.getType().callIsStateless() ? senderAddress : codeAddress;
542543

543544
if (logger.isInfoEnabled())
@@ -679,7 +680,7 @@ public void storageSave(DataWord word1, DataWord word2) {
679680
//storageSave(word1.getData(), word2.getData());
680681
DataWord keyWord = word1.clone();
681682
DataWord valWord = word2.clone();
682-
getStorage().addStorageValue(getOwnerAddress().getLast20Bytes(), keyWord, valWord);
683+
getStorage().addStorageValue(convertToTronAddress(getOwnerAddress().getLast20Bytes()), keyWord, valWord);
683684
}
684685

685686
/*
@@ -695,7 +696,7 @@ public byte[] getCode() {
695696
}
696697

697698
public byte[] getCodeAt(DataWord address) {
698-
byte[] code = invoke.getDeposit().getCode(address.getLast20Bytes());
699+
byte[] code = invoke.getDeposit().getCode(convertToTronAddress(address.getLast20Bytes()));
699700
return nullToEmpty(code);
700701
}
701702

@@ -725,7 +726,7 @@ public DataWord getBlockHash(int index) {
725726
}
726727

727728
public DataWord getBalance(DataWord address) {
728-
long balance = getStorage().getBalance(address.getLast20Bytes());
729+
long balance = getStorage().getBalance(convertToTronAddress(address.getLast20Bytes()));
729730
return new DataWord(balance);
730731
}
731732

@@ -780,7 +781,7 @@ public byte[] getReturnDataBufferData(DataWord off, DataWord size) {
780781
}
781782

782783
public DataWord storageLoad(DataWord key) {
783-
DataWord ret = getStorage().getStorageValue(getOwnerAddress().getLast20Bytes(), key.clone());
784+
DataWord ret = getStorage().getStorageValue(convertToTronAddress(getOwnerAddress().getLast20Bytes()), key.clone());
784785
return ret == null ? null : ret.clone();
785786
}
786787

@@ -1118,8 +1119,8 @@ public void callToPrecompiledAddress(MessageCall msg, PrecompiledContracts.Preco
11181119
// Repository track = getStorage().startTracking();
11191120
Deposit deposit = getStorage();
11201121

1121-
byte[] senderAddress = this.getOwnerAddress().getLast20Bytes();
1122-
byte[] codeAddress = msg.getCodeAddress().getLast20Bytes();
1122+
byte[] senderAddress = convertToTronAddress(this.getOwnerAddress().getLast20Bytes());
1123+
byte[] codeAddress = convertToTronAddress(msg.getCodeAddress().getLast20Bytes());
11231124
byte[] contextAddress = msg.getType().callIsStateless() ? senderAddress : codeAddress;
11241125

11251126

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.tron.common.storage;
22

3+
import static org.tron.common.runtime.utils.MUtil.convertToTronAddress;
4+
35
import com.google.protobuf.ByteString;
46
import io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException;
57
import java.math.BigInteger;
@@ -171,13 +173,7 @@ public synchronized StorageCapsule getStorage(byte[] address) {
171173

172174
@Override
173175
public synchronized void addStorageValue(byte[] address, DataWord key, DataWord value) {
174-
if (address.length == 20) {
175-
byte [] newAddress = new byte [21];
176-
byte[] temp = new byte[]{Constant.ADD_PRE_FIX_BYTE_MAINNET};
177-
System.arraycopy(temp, 0, newAddress, 0, temp.length);
178-
System.arraycopy(address, 0, newAddress, temp.length, address.length);
179-
address = newAddress;
180-
}
176+
address = convertToTronAddress(address);
181177
if (getAccount(address) == null) return;
182178
Key addressKey = Key.create(address);
183179
if (storageCache.containsKey(addressKey)) {
@@ -209,13 +205,7 @@ public synchronized void addStorageValue(byte[] address, DataWord key, DataWord
209205

210206
@Override
211207
public synchronized DataWord getStorageValue(byte[] address, DataWord key) {
212-
if (address.length == 20) {
213-
byte [] newAddress = new byte [21];
214-
byte[] temp = new byte[]{Constant.ADD_PRE_FIX_BYTE_MAINNET};
215-
System.arraycopy(temp, 0, newAddress, 0, temp.length);
216-
System.arraycopy(address, 0, newAddress, temp.length, address.length);
217-
address = newAddress;
218-
}
208+
address = convertToTronAddress(address);
219209
if (getAccount(address) == null) return null;
220210
Key addressKey = Key.create(address);
221211
if (storageCache.containsKey(addressKey)) {

‎src/main/java/org/tron/core/db/Manager.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -964,11 +964,11 @@ public boolean processTransaction(final TransactionCapsule trxCap, Protocol.Bloc
964964
}
965965

966966
transactionStore.put(trxCap.getTransactionId().getBytes(), trxCap);
967-
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
968-
transactionInfoCapsule.setId(trxCap.getTransactionId().getBytes());
969-
transactionInfoCapsule.setFee(runtime.getResult().getRet().getFee());
970-
transactionInfoCapsule.setContractResult(runtime.getResult().getHReturn());
971-
transactionHistoryStore.put(trxCap.getTransactionId().getBytes(), transactionInfoCapsule);
967+
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
968+
transactionInfoCapsule.setId(trxCap.getTransactionId().getBytes());
969+
transactionInfoCapsule.setFee(runtime.getResult().getRet().getFee());
970+
transactionInfoCapsule.setContractResult(runtime.getResult().getHReturn());
971+
transactionHistoryStore.put(trxCap.getTransactionId().getBytes(), transactionInfoCapsule);
972972

973973
return true;
974974
}

0 commit comments

Comments
 (0)