Skip to content

Commit 1429b33

Browse files
committed
transfer token when suicide
1 parent 053cd05 commit 1429b33

9 files changed

Lines changed: 83 additions & 36 deletions

File tree

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package org.tron.common.runtime.utils;
22

33
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Iterator;
6+
import java.util.Map;
47
import org.tron.common.storage.Deposit;
58
import org.tron.core.Wallet;
69
import org.tron.core.actuator.TransferActuator;
710
import org.tron.core.actuator.TransferAssetActuator;
11+
import org.tron.core.capsule.AccountCapsule;
12+
import org.tron.core.config.args.Account;
813
import org.tron.core.exception.ContractValidateException;
14+
import org.tron.protos.Protocol;
915

1016
public class MUtil {
1117
private MUtil() {}
@@ -20,6 +26,19 @@ public static void transfer(Deposit deposit, byte[] fromAddress, byte[] toAddres
2026
deposit.addBalance(fromAddress, -amount);
2127
}
2228

29+
public static void transferAllToken(Deposit deposit, byte[] fromAddress, byte[] toAddress) {
30+
AccountCapsule fromAccountCap = deposit.getAccount(fromAddress);
31+
Protocol.Account.Builder fromBuilder = fromAccountCap.getInstance().toBuilder();
32+
AccountCapsule toAccountCap = deposit.getAccount(toAddress);
33+
Protocol.Account.Builder toBuilder = toAccountCap.getInstance().toBuilder();
34+
fromAccountCap.getAssetMap().forEach((tokenId, amount) ->
35+
toBuilder.putAsset(tokenId,toBuilder.getAssetMap().getOrDefault(tokenId, 0L) + amount));
36+
fromAccountCap.getAssetMap().forEach((tokenId, amount) ->
37+
fromBuilder.putAsset(tokenId,0L));
38+
deposit.putAccountValue(fromAddress,new AccountCapsule(fromBuilder.build()));
39+
deposit.putAccountValue(toAddress, new AccountCapsule(toBuilder.build()));
40+
}
41+
2342
public static void transferToken(Deposit deposit, byte[] fromAddress, byte[] toAddress, String tokenId, long amount)
2443
throws ContractValidateException {
2544
if (0 == amount) {
@@ -40,14 +59,4 @@ public static byte[] convertToTronAddress(byte[] address) {
4059
}
4160
return address;
4261
}
43-
44-
public static byte[] removeZeroes(byte[] data) {
45-
int i;
46-
for(i = 0; i < data.length; i++) {
47-
if(data[i] != '\0') {
48-
break;
49-
}
50-
}
51-
return Arrays.copyOfRange(data, i, data.length);
52-
}
5362
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,9 @@ public BlockCapsule getBlock(byte[] blockHash) {
282282
return this.deposit.getBlock(blockHash);
283283
}
284284

285+
@Override
286+
public byte[] getBlackHoleAddress() {
287+
return deposit.getBlackHoleAddress();
288+
}
289+
285290
}

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import com.google.common.primitives.Longs;
2424
import java.util.Arrays;
25+
import java.util.HashMap;
26+
import java.util.Map;
2527
import lombok.Getter;
2628
import lombok.extern.slf4j.Slf4j;
2729
import org.apache.commons.lang3.ArrayUtils;
@@ -42,7 +44,7 @@ public class InternalTransaction {
4244
/* the amount of trx to transfer (calculated as sun) */
4345
private long value;
4446
private long tokenValue;
45-
private String tokenId;
47+
private Map<String, Long> tokenInfo =new HashMap<>();
4648

4749
/* the address of the destination account (for message)
4850
* In creation transaction the receive address is - 0 */
@@ -104,8 +106,7 @@ public InternalTransaction(Transaction trx, InternalTransaction.TrxType trxType
104106
this.note = "create";
105107
this.value = contract.getNewContract().getCallValue();
106108
this.data = contract.getNewContract().getBytecode().toByteArray();
107-
this.tokenValue = contract.getCallTokenValue();
108-
this.tokenId = contract.getTokenId();
109+
this.tokenInfo.put(contract.getTokenId(), contract.getCallTokenValue());
109110
} else if (trxType == TrxType.TRX_CONTRACT_CALL_TYPE) {
110111
TriggerSmartContract contract = ContractCapsule.getTriggerContractFromTransaction(trx);
111112
if (contract == null) {
@@ -117,8 +118,7 @@ public InternalTransaction(Transaction trx, InternalTransaction.TrxType trxType
117118
this.note = "call";
118119
this.value = contract.getCallValue();
119120
this.data = contract.getData().toByteArray();
120-
this.tokenValue = contract.getCallTokenValue();
121-
this.tokenId = contract.getTokenId();
121+
this.tokenInfo.put(contract.getTokenId(), contract.getCallTokenValue());
122122
} else {
123123
// do nothing, just for running byte code
124124
}
@@ -130,7 +130,7 @@ public InternalTransaction(Transaction trx, InternalTransaction.TrxType trxType
130130
*/
131131

132132
public InternalTransaction(byte[] parentHash, int deep, int index,
133-
byte[] sendAddress, byte[] transferToAddress, long value, byte[] data, String note, long nonce, String tokenId) {
133+
byte[] sendAddress, byte[] transferToAddress, long value, byte[] data, String note, long nonce, Map<String, Long> tokenInfo) {
134134
this.parentHash = parentHash.clone();
135135
this.deep = deep;
136136
this.index = index;
@@ -149,8 +149,9 @@ public InternalTransaction(byte[] parentHash, int deep, int index,
149149
this.hash = getHash();
150150
// in a contract call contract case, only one value should be used. trx or a token. can't be both. We should avoid using
151151
// tokenValue in this case.
152-
this.tokenValue = this.value;
153-
this.tokenId = tokenId;
152+
if (tokenInfo != null) {
153+
this.tokenInfo.putAll(tokenInfo);
154+
}
154155
}
155156

156157
public Transaction getTransaction() {
@@ -180,11 +181,8 @@ public String getNote() {
180181
return note;
181182
}
182183

183-
public String getTokenId() {
184-
if (tokenId == null) {
185-
return "";
186-
}
187-
return tokenId;
184+
public Map<String, Long> getTokenInfo() {
185+
return tokenInfo;
188186
}
189187

190188
public byte[] getSender() {

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
import static org.apache.commons.lang3.ArrayUtils.nullToEmpty;
2727
import static org.tron.common.runtime.utils.MUtil.convertToTronAddress;
2828
import static org.tron.common.runtime.utils.MUtil.transfer;
29+
import static org.tron.common.runtime.utils.MUtil.transferAllToken;
2930
import static org.tron.common.utils.BIUtil.isPositive;
3031
import static org.tron.common.utils.BIUtil.toBI;
32+
import static org.tron.common.utils.ByteUtil.stripLeadingZeroes;
3133

3234
import com.google.protobuf.ByteString;
3335
import java.io.ByteArrayOutputStream;
3436
import java.math.BigInteger;
3537
import java.util.Arrays;
3638
import java.util.BitSet;
39+
import java.util.HashMap;
3740
import java.util.Map;
3841
import java.util.NavigableSet;
3942
import java.util.Objects;
@@ -194,12 +197,12 @@ public int getCallDeep() {
194197
*/
195198
private InternalTransaction addInternalTx(DataWord energyLimit, byte[] senderAddress,
196199
byte[] transferAddress,
197-
long value, byte[] data, String note, long nonce, String tokenId) {
200+
long value, byte[] data, String note, long nonce, Map<String, Long> tokenInfo) {
198201

199202
InternalTransaction addedInternalTx = null;
200203
if (internalTransaction != null) {
201204
addedInternalTx = getResult().addInternalTransaction(internalTransaction.getHash(), getCallDeep(),
202-
senderAddress, transferAddress, value, data, note, nonce, tokenId);
205+
senderAddress, transferAddress, value, data, note, nonce, tokenInfo);
203206
}
204207

205208
return addedInternalTx;
@@ -411,14 +414,24 @@ public void suicide(DataWord obtainerAddress) {
411414
}
412415

413416
increaseNonce();
414-
addInternalTx(null, owner, obtainer, balance, null, "suicide", nonce, null);
417+
418+
addInternalTx(null, owner, obtainer, balance, null, "suicide", nonce,
419+
getContractState().getAccount(owner).getAssetMap());
415420

416421
if (FastByteComparisons.compareTo(owner, 0, 20, obtainer, 0, 20) == 0) {
417422
// if owner == obtainer just zeroing account according to Yellow Paper
418423
getContractState().addBalance(owner, -balance);
424+
byte[] blackHoleAddress = getContractState().getBlackHoleAddress();
425+
if (VMConfig.getEnergyLimitHardFork()) {
426+
getContractState().addBalance(blackHoleAddress, balance);
427+
transferAllToken(getContractState(),owner,blackHoleAddress);
428+
}
419429
} else {
420430
try {
421431
transfer(getContractState(), owner, obtainer, balance);
432+
if (VMConfig.getEnergyLimitHardFork()) {
433+
transferAllToken(getContractState(), owner, obtainer);
434+
}
422435
} catch (ContractValidateException e) {
423436
throw new BytecodeExecutionException("transfer failure");
424437
}
@@ -671,9 +684,13 @@ public void callToAddress(MessageCall msg) {
671684

672685
// CREATE CALL INTERNAL TRANSACTION
673686
increaseNonce();
687+
HashMap<String,Long> tokenInfo = new HashMap<>();
688+
if (msg.getTokenId()!= null) {
689+
tokenInfo.put(new String(stripLeadingZeroes(msg.getTokenId().getData())),endowment);
690+
}
674691
InternalTransaction internalTx = addInternalTx(null, senderAddress, contextAddress,
675-
endowment, data, "call", nonce , msg.getTokenId() == null ? null: new String(MUtil.removeZeroes(msg.getTokenId().getData())));
676-
692+
endowment, data, "call", nonce ,
693+
msg.getTokenId() == null ? null: tokenInfo);
677694
ProgramResult callResult = null;
678695
if (isNotEmpty(programCode)) {
679696
long vmStartInUs = System.nanoTime() / 1000;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Arrays;
99
import java.util.HashSet;
1010
import java.util.List;
11+
import java.util.Map;
1112
import java.util.Set;
1213
import org.tron.common.runtime.vm.CallCreate;
1314
import org.tron.common.runtime.vm.DataWord;
@@ -159,9 +160,9 @@ public List<InternalTransaction> getInternalTransactions() {
159160
}
160161

161162
public InternalTransaction addInternalTransaction(byte[] parentHash, int deep,
162-
byte[] senderAddress, byte[] transferAddress, long value, byte[] data, String note, long nonce, String tokenId) {
163+
byte[] senderAddress, byte[] transferAddress, long value, byte[] data, String note, long nonce, Map<String, Long> token) {
163164
InternalTransaction transaction = new InternalTransaction(parentHash, deep,
164-
size(internalTransactions), senderAddress, transferAddress, value, data, note, nonce, tokenId);
165+
size(internalTransactions), senderAddress, transferAddress, value, data, note, nonce, token);
165166
getInternalTransactions().add(transaction);
166167
return transaction;
167168
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,6 @@ public interface Deposit {
104104

105105
BlockCapsule getBlock(byte[] blockHash);
106106

107+
byte[] getBlackHoleAddress();
108+
107109
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.tron.common.runtime.vm.DataWord;
1313
import org.tron.common.runtime.vm.program.Storage;
1414
import org.tron.common.utils.ByteArray;
15+
import org.tron.common.utils.ByteUtil;
1516
import org.tron.common.utils.StringUtil;
1617
import org.tron.core.capsule.AccountCapsule;
1718
import org.tron.core.capsule.AssetIssueCapsule;
@@ -163,6 +164,13 @@ public synchronized AccountCapsule getAccount(byte[] address) {
163164
return accountCapsule;
164165
}
165166

167+
@Override
168+
public byte[] getBlackHoleAddress() {
169+
// using dbManager directly, black hole address should not be changed
170+
// when executing smart contract.
171+
return getAccountStore().getBlackhole().getAddress().toByteArray();
172+
}
173+
166174
@Override
167175
public WitnessCapsule getWitness(byte[] address) {
168176
Key key = new Key(address);
@@ -318,7 +326,7 @@ public synchronized Storage getStorage(byte[] address) {
318326

319327
@Override
320328
public synchronized AssetIssueCapsule getAssetIssue(byte[] tokenId) {
321-
byte[] tokenIdWithoutLeadingZero =MUtil.removeZeroes(tokenId);
329+
byte[] tokenIdWithoutLeadingZero =ByteUtil.stripLeadingZeroes(tokenId);
322330
Key key = Key.create(tokenIdWithoutLeadingZero);
323331
if (assetIssueCache.containsKey(key)) {
324332
return assetIssueCache.get(key).getAssetIssue();
@@ -378,7 +386,7 @@ public synchronized long getBalance(byte[] address) {
378386

379387
@Override
380388
public synchronized long addTokenBalance(byte[] address, byte[] tokenId, long value) {
381-
byte[] tokenIdWithoutLeadingZero = MUtil.removeZeroes(tokenId);
389+
byte[] tokenIdWithoutLeadingZero = ByteUtil.stripLeadingZeroes(tokenId);
382390
AccountCapsule accountCapsule = getAccount(address);
383391
if (accountCapsule == null) {
384392
accountCapsule = createAccount(address, AccountType.Normal);
@@ -431,7 +439,7 @@ public synchronized long addBalance(byte[] address, long value) {
431439
@Override
432440
public synchronized long getTokenBalance(byte[] address, byte[] tokenId){
433441
AccountCapsule accountCapsule = getAccount(address);
434-
return accountCapsule.getAssetMap().getOrDefault(new String(MUtil.removeZeroes(tokenId)), new Long(0));
442+
return accountCapsule.getAssetMap().getOrDefault(new String(ByteUtil.stripLeadingZeroes(tokenId)), new Long(0));
435443
}
436444

437445
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.tron.common.runtime.utils.MUtil;
2525
import org.tron.common.storage.Deposit;
2626
import org.tron.common.utils.ByteArray;
27+
import org.tron.common.utils.ByteUtil;
2728
import org.tron.core.Wallet;
2829
import org.tron.core.capsule.AccountCapsule;
2930
import org.tron.core.capsule.TransactionResultCapsule;
@@ -186,7 +187,7 @@ public static boolean validateForSmartContract(Deposit deposit, byte[] ownerAddr
186187
}
187188

188189
long fee = 0;
189-
byte[] tokenIdWithoutLeadingZero = MUtil.removeZeroes(tokenId);
190+
byte[] tokenIdWithoutLeadingZero = ByteUtil.stripLeadingZeroes(tokenId);
190191

191192
if (!Wallet.addressValid(ownerAddress)) {
192193
throw new ContractValidateException("Invalid ownerAddress");

‎src/main/java/org/tron/core/capsule/TransactionInfoCapsule.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,17 @@ public static TransactionInfoCapsule buildInstance(TransactionCapsule trxCap, Bl
198198
//TODO: "for loop" below in future for multiple token case, we only have one for now.
199199
Protocol.InternalTransaction.CallValueInfo.Builder callValueInfoBuilder =
200200
Protocol.InternalTransaction.CallValueInfo.newBuilder();
201-
callValueInfoBuilder.setCallValue(internalTransaction.getValue());
202201
// trx will not be set token name
203-
callValueInfoBuilder.setTokenId(internalTransaction.getTokenId());
202+
callValueInfoBuilder.setCallValue(internalTransaction.getValue());
204203
// Just one transferBuilder for now.
205204
internalTrxBuilder.addCallValueInfo(callValueInfoBuilder);
205+
internalTransaction.getTokenInfo().forEach((tokenId, amount) -> {
206+
Protocol.InternalTransaction.CallValueInfo.Builder tokenInfoBuilder =
207+
Protocol.InternalTransaction.CallValueInfo.newBuilder();
208+
tokenInfoBuilder.setTokenId(tokenId);
209+
tokenInfoBuilder.setCallValue(amount);
210+
internalTrxBuilder.addCallValueInfo(tokenInfoBuilder);
211+
});
206212
// Token for loop end here
207213
internalTrxBuilder.setNote(ByteString.copyFrom(internalTransaction.getNote().getBytes()));
208214
internalTrxBuilder.setRejected(internalTransaction.isRejected());

0 commit comments

Comments
 (0)