Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static String parseDataBytes(byte[] data, String typeStr, int index) {
private static Type basicType(String type) {
if (!Pattern.matches("^.*\\[\\d*\\]$", type)) {
// ignore not valide type such as "int92", "bytes33", these types will be compiled failed.
if (type.startsWith("int") || type.startsWith("uint") || type.startsWith("trcToken")) {
if ((type.startsWith("int") || type.startsWith("uint"))) {
return Type.INT_NUMBER;
} else if (type.equals("bool")) {
return Type.BOOL;
Expand Down
30 changes: 23 additions & 7 deletions src/main/java/org/tron/common/runtime/RuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

import com.google.protobuf.ByteString;
import java.math.BigInteger;
import java.util.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -20,7 +21,12 @@
import org.tron.common.logsfilter.EventPluginLoader;
import org.tron.common.logsfilter.trigger.ContractTrigger;
import org.tron.common.runtime.config.VMConfig;
import org.tron.common.runtime.vm.*;
import org.tron.common.runtime.vm.DataWord;
import org.tron.common.runtime.vm.EnergyCost;
import org.tron.common.runtime.vm.LogInfoTriggerParser;
import org.tron.common.runtime.vm.VM;
import org.tron.common.runtime.vm.VMConstant;
import org.tron.common.runtime.vm.VMUtils;
import org.tron.common.runtime.vm.program.InternalTransaction;
import org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType;
import org.tron.common.runtime.vm.program.InternalTransaction.TrxType;
Expand Down Expand Up @@ -88,6 +94,7 @@ public class RuntimeImpl implements Runtime {
@Setter
private boolean enableEventLinstener;


private LogInfoTriggerParser logInfoTriggerParser;

/**
Expand Down Expand Up @@ -443,7 +450,10 @@ private void create()
(EventPluginLoader.getInstance().isContractEventTriggerEnable()
|| EventPluginLoader.getInstance().isContractLogTriggerEnable())
&& isCheckTransaction()){
logInfoTriggerParser = new LogInfoTriggerParser(blockCap.getNum(), blockCap.getTimeStamp(), txId, callerAddress);

logInfoTriggerParser = new LogInfoTriggerParser(newSmartContract.getAbi(),
blockCap.getNum(), blockCap.getTimeStamp(), txId,
callerAddress, callerAddress, callerAddress, contractAddress);

}
} catch (Exception e) {
Expand Down Expand Up @@ -532,11 +542,15 @@ private void call()
}
AccountCapsule caller = this.deposit.getAccount(callerAddress);
long energyLimit;
byte[] creatorAddress = null;
byte[] originAddress = null;
if (isCallConstant(contractAddress)) {
isStaticCall = true;
energyLimit = Constant.ENERGY_LIMIT_IN_CONSTANT_TX;
} else {
AccountCapsule creator = this.deposit.getAccount(deployedContract.getInstance().getOriginAddress().toByteArray());
originAddress = deployedContract.getInstance().getOriginAddress().toByteArray();
AccountCapsule creator = this.deposit.getAccount(originAddress);
creatorAddress = creator.getAddress().toByteArray();
energyLimit = getTotalEnergyLimit(creator, caller, contract, feeLimit, callValue);
}
long maxCpuTimeOfOneTx = deposit.getDbManager().getDynamicPropertiesStore()
Expand Down Expand Up @@ -565,7 +579,9 @@ private void call()
|| EventPluginLoader.getInstance().isContractLogTriggerEnable())
&& isCheckTransaction()){

logInfoTriggerParser = new LogInfoTriggerParser(blockCap.getNum(), blockCap.getTimeStamp(), txId, callerAddress);
logInfoTriggerParser = new LogInfoTriggerParser(deployedContract.getInstance().getAbi(),
blockCap.getNum(), blockCap.getTimeStamp(), txId,
callerAddress, creatorAddress, originAddress, contractAddress);
}
}

Expand Down Expand Up @@ -644,7 +660,7 @@ public void go() {
deposit.commit();

if (logInfoTriggerParser != null){
List<ContractTrigger> triggers = logInfoTriggerParser.parseLogInfos(program.getResult().getLogInfoList(), this.deposit);
List<ContractTrigger> triggers = logInfoTriggerParser.parseLogInfos(program.getResult().getLogInfoList());
program.getResult().setTriggerList(triggers);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tron/common/runtime/vm/DataWord.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public byte[] getClonedData() {
if (data != null){
ret = new byte[DATAWORD_UNIT_SIZE];
int dataSize = Math.min(data.length, DATAWORD_UNIT_SIZE);
System.arraycopy(data, 0, ret, 0, dataSize);
System.arraycopy(data, 0, ret, DATAWORD_UNIT_SIZE - dataSize, dataSize);
}
return ret;
}
Expand Down
69 changes: 34 additions & 35 deletions src/main/java/org/tron/common/runtime/vm/LogInfoTriggerParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,51 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import jdk.nashorn.internal.objects.annotations.Setter;
import org.apache.commons.lang3.ArrayUtils;
import org.spongycastle.util.encoders.Hex;
import org.tron.common.crypto.Hash;
import org.tron.common.logsfilter.trigger.ContractLogTrigger;
import org.tron.common.logsfilter.trigger.ContractTrigger;
import org.tron.common.storage.Deposit;
import org.tron.core.Wallet;
import org.tron.protos.Protocol.SmartContract.ABI;

public class LogInfoTriggerParser {

private ABI abi;
private Long blockNum;
private Long blockTimestamp;
private String txId;
private String callerAddress;
private String creatorAddress;
private String originAddress;
private String contractAddress;


public LogInfoTriggerParser(Long blockNum,
public LogInfoTriggerParser(ABI abi,
Long blockNum,
Long blockTimestamp,
byte[] txId, byte[] originAddress) {
byte[] txId,
byte[] callerAddress,
byte[] creatorAddress,
byte[] originAddress,
byte[] contractAddress) {

this.abi = abi;
this.blockNum = blockNum;
this.blockTimestamp = blockTimestamp;
this.txId = ArrayUtils.isEmpty(txId) ? "" : Hex.toHexString(txId);
this.callerAddress =
ArrayUtils.isEmpty(callerAddress) ? "" : Wallet.encode58Check(callerAddress);
this.contractAddress =
ArrayUtils.isEmpty(contractAddress) ? "" : Wallet.encode58Check(contractAddress);
this.originAddress =
ArrayUtils.isEmpty(originAddress) ? "" : Wallet.encode58Check(originAddress);
this.creatorAddress =
ArrayUtils.isEmpty(creatorAddress) ? "" : Wallet.encode58Check(creatorAddress);

}

public List<ContractTrigger> parseLogInfos(List<LogInfo> logInfos, Deposit deposit) {
public List<ContractTrigger> parseLogInfos(List<LogInfo> logInfos) {

List<ContractTrigger> list = new LinkedList<>();
if (logInfos == null || logInfos.size() <= 0) {
Expand All @@ -44,43 +58,28 @@ public List<ContractTrigger> parseLogInfos(List<LogInfo> logInfos, Deposit depos
Map<String, ABI.Entry> fullMap = new HashMap<>();
Map<String, String> signMap = new HashMap<>();

for (LogInfo logInfo : logInfos) {

byte[] contractAddress = logInfo.getAddress();
String strContractAddr = ArrayUtils.isEmpty(contractAddress) ? "" : Wallet.encode58Check(contractAddress);
if (signMap.get(strContractAddr) == null) {
ABI abi = deposit.getContract(contractAddress).getInstance().getAbi();
signMap.put(strContractAddr, "1"); // mark as found.

// calculate the sha3 of the event signature first.
if (abi != null && abi.getEntrysCount() > 0) {
for (ABI.Entry entry : abi.getEntrysList()) {
if (entry.getType() != ABI.Entry.EntryType.Event || entry.getAnonymous()) {
continue;
}
String signature = getEntrySignature(entry);
String sha3 = Hex.toHexString(Hash.sha3(signature.getBytes()));
fullMap.put(strContractAddr + "_" + sha3, entry);
signMap.put(strContractAddr + "_" + sha3, signature);
}
// calculate the sha3 of the event signature first.
if (abi != null && abi.getEntrysCount() > 0) {
for (ABI.Entry entry : abi.getEntrysList()) {
if (entry.getType() != ABI.Entry.EntryType.Event || entry.getAnonymous()) {
continue;
}
String signature = getEntrySignature(entry);
String sha3 = Hex.toHexString(Hash.sha3(signature.getBytes()));
fullMap.put(sha3, entry);
signMap.put(sha3, signature);
}
}

int index = 1;
for (LogInfo logInfo : logInfos) {

byte[] contractAddress = logInfo.getAddress();
String strContractAddr = ArrayUtils.isEmpty(contractAddress) ? "" : Wallet.encode58Check(contractAddress);

List<DataWord> topics = logInfo.getTopics();
ABI.Entry entry = null;
String signature = "";
if (topics != null && topics.size() > 0 && !ArrayUtils.isEmpty(topics.get(0).getData())
&& fullMap.size() > 0) {
String firstTopic = topics.get(0).toString();
entry = fullMap.get(strContractAddr + "_" + firstTopic);
signature = signMap.get(strContractAddr + "_" + firstTopic);
entry = fullMap.get(firstTopic);
signature = signMap.get(firstTopic);
}

boolean isEvent = (entry != null);
Expand All @@ -98,10 +97,10 @@ public List<ContractTrigger> parseLogInfos(List<LogInfo> logInfos, Deposit depos
}
event.setUniqueId(txId + "_" + index);
event.setTransactionId(txId);
event.setContractAddress(strContractAddr);
event.setContractAddress(contractAddress);
event.setCallerAddress(callerAddress);
event.setOriginAddress(originAddress);
event.setCallerAddress("");
event.setCreatorAddress("");
event.setCreatorAddress(creatorAddress);
event.setBlockNumber(blockNum);
event.setTimeStamp(blockTimestamp);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public byte[] getSender() {
}

public byte[] getReceiveAddress() {
if (receiveAddress == null) {
if (sendAddress == null) {
return EMPTY_BYTE_ARRAY;
}
return receiveAddress.clone();
Expand Down