Skip to content

Commit

Permalink
Merge pull request #1 from MidasCore/dev-0.2.0
Browse files Browse the repository at this point in the history
Mcashchain v0.2.0
  • Loading branch information
nghiand authored Nov 6, 2019
2 parents 4021bac + 141bc5f commit 42d8e35
Show file tree
Hide file tree
Showing 160 changed files with 6,898 additions and 6,317 deletions.
3 changes: 3 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ if [[ ${DB} == "remove" ]]; then
rm -rf ${OUTPUT_DIRECTORY}
echo "remove db success"
elif [[ ${DB} == "backup" ]]; then
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
tar -czf ${OUTPUT_DIRECTORY}-${current_time}.tar.gz ${OUTPUT_DIRECTORY}
rm -rf ${OUTPUT_DIRECTORY}
echo "backup db success"
fi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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") || type.startsWith("trcToken") || type.equals("token")) {
return Type.INT_NUMBER;
} else if (type.equals("bool")) {
return Type.BOOL;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/io/midasprotocol/common/overlay/message/Message.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package io.midasprotocol.common.overlay.message;

import com.google.protobuf.CodedInputStream;
import io.midasprotocol.common.utils.Sha256Hash;
import io.midasprotocol.core.db.Manager;
import io.midasprotocol.core.exception.P2pException;
import io.midasprotocol.core.net.message.MessageTypes;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Setter;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.Arrays;

import static io.midasprotocol.core.exception.P2pException.TypeEnum.PROTOBUF_ERROR;

public abstract class Message {

protected static final Logger logger = LoggerFactory.getLogger("Message");

protected byte[] data;
protected byte type;
@Setter
private static Manager manager;

public Message() {
}
Expand Down Expand Up @@ -68,4 +78,33 @@ public boolean equals(Object o) {
Message message = (Message) o;
return Arrays.equals(data, message.data);
}

public static void compareBytes(byte[] src, byte[] dest) throws P2pException {
if (src.length != dest.length) {
throw new P2pException(PROTOBUF_ERROR, PROTOBUF_ERROR.getDesc());
}
}

private static final Field field = ReflectionUtils
.findField(CodedInputStream.class, "shouldDiscardUnknownFields");

static {
ReflectionUtils.makeAccessible(field);
}

public static CodedInputStream getCodedInputStream(byte[] data) {
CodedInputStream codedInputStream = CodedInputStream.newInstance(data);
if (isFilter()) {
ReflectionUtils.setField(field, codedInputStream, true);
}
return codedInputStream;
}

public void temp() {
}

public static boolean isFilter() {
return manager.getDynamicPropertiesStore().getAllowProtoFilter() == 1;
}

}
4 changes: 1 addition & 3 deletions src/main/java/io/midasprotocol/common/runtime/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

public interface Runtime {

boolean isCallConstant() throws ContractValidateException;

void execute() throws ContractValidateException, ContractExeException, VMIllegalException;

void go();
Expand All @@ -23,5 +21,5 @@ public interface Runtime {

String getRuntimeError();

void setEnableEventLinstener(boolean enableEventLinstener);
void setEnableEventListener(boolean enableEventListener);
}
71 changes: 21 additions & 50 deletions src/main/java/io/midasprotocol/common/runtime/RuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import io.midasprotocol.protos.Protocol;
import io.midasprotocol.protos.Protocol.Block;
import io.midasprotocol.protos.Protocol.SmartContract;
import io.midasprotocol.protos.Protocol.SmartContract.ABI;
import io.midasprotocol.protos.Protocol.Transaction;
import io.midasprotocol.protos.Protocol.Transaction.Contract.ContractType;
import io.midasprotocol.protos.Protocol.Transaction.Result.ContractResult;
Expand Down Expand Up @@ -83,10 +82,13 @@ public class RuntimeImpl implements Runtime {

//tx trace
private TransactionTrace trace;
private boolean isStaticCall;

@Getter
@Setter
private boolean isStaticCall = false;

@Setter
private boolean enableEventLinstener;
private boolean enableEventListener;

private LogInfoTriggerParser logInfoTriggerParser;

Expand Down Expand Up @@ -439,8 +441,7 @@ private void create()
this.blockCap);
byte[] txId = new TransactionCapsule(trx).getTransactionId().getBytes();
this.program.setRootTransactionId(txId);
this.program.setRootCallConstant(isCallConstant());
if (enableEventLinstener &&
if (enableEventListener &&
(EventPluginLoader.getInstance().isContractEventTriggerEnable()
|| EventPluginLoader.getInstance().isContractLogTriggerEnable())
&& isCheckTransaction()) {
Expand All @@ -459,8 +460,9 @@ && isCheckTransaction()) {

deposit.createContract(contractAddress, new ContractCapsule(newSmartContract));
byte[] code = newSmartContract.getBytecode().toByteArray();
deposit.saveCode(contractAddress, ProgramPrecompile.getCode(code));

if (!VMConfig.allowVmConstantinople()) {
deposit.saveCode(contractAddress, ProgramPrecompile.getCode(code));
}
// transfer from callerAddress to contractAddress according to callValue
if (callValue > 0) {
transfer(this.deposit, callerAddress, contractAddress, callValue);
Expand Down Expand Up @@ -533,8 +535,7 @@ private void call()
}
AccountCapsule caller = this.deposit.getAccount(callerAddress);
long energyLimit;
if (isCallConstant(contractAddress)) {
isStaticCall = true;
if (isStaticCall) {
energyLimit = Constant.ENERGY_LIMIT_IN_CONSTANT_TX;
} else {
AccountCapsule creator = this.deposit
Expand All @@ -560,9 +561,8 @@ private void call()
this.blockCap);
byte[] txId = new TransactionCapsule(trx).getTransactionId().getBytes();
this.program.setRootTransactionId(txId);
this.program.setRootCallConstant(isCallConstant());

if (enableEventLinstener &&
if (enableEventListener &&
(EventPluginLoader.getInstance().isContractEventTriggerEnable()
|| EventPluginLoader.getInstance().isContractLogTriggerEnable())
&& isCheckTransaction()) {
Expand Down Expand Up @@ -603,7 +603,7 @@ public void go() {
vm.play(program);
result = program.getResult();

if (isCallConstant()) {
if (isStaticCall) {
long callValue = TransactionCapsule.getCallValue(trx.getRawData().getContract(0));
long callTokenValue = TransactionCapsule
.getCallTokenValue(trx.getRawData().getContract(0));
Expand All @@ -626,6 +626,9 @@ public void go() {
}
} else {
result.spendEnergy(saveCodeEnergy);
if (VMConfig.allowVmConstantinople()) {
deposit.saveCode(program.getContractAddress().getNoLeadZeroesData(), code);
}
}
}

Expand All @@ -636,7 +639,9 @@ public void go() {
result.rejectInternalTransactions();

if (result.getException() != null) {
program.spendAllEnergy();
if (!(result.getException() instanceof Program.TransferException)) {
program.spendAllEnergy();
}
runtimeError = result.getException().getMessage();
throw result.getException();
} else {
Expand Down Expand Up @@ -669,10 +674,10 @@ public void go() {
result.rejectInternalTransactions();
runtimeError = result.getException().getMessage();
logger.info("timeout: {}", result.getException().getMessage());
} catch (ContractValidateException e) {
logger.info("when check constant, {}", e.getMessage());
} catch (Throwable e) {
program.spendAllEnergy();
if (!(e instanceof Program.TransferException)) {
program.spendAllEnergy();
}
result = program.getResult();
result.rejectInternalTransactions();
if (Objects.isNull(result.getException())) {
Expand All @@ -687,40 +692,6 @@ public void go() {
trace.setBill(result.getEnergyUsed());
}

public boolean isCallConstant() throws ContractValidateException {

TriggerSmartContract triggerContractFromTransaction = ContractCapsule
.getTriggerContractFromTransaction(trx);
if (TrxType.TRX_CONTRACT_CALL_TYPE == trxType) {

ContractCapsule contract = deposit
.getContract(triggerContractFromTransaction.getContractAddress().toByteArray());
if (contract == null) {
logger.info("contract: {} is not in contract store", Wallet
.encodeBase58Check(triggerContractFromTransaction.getContractAddress().toByteArray()));
throw new ContractValidateException("contract: " + Wallet
.encodeBase58Check(triggerContractFromTransaction.getContractAddress().toByteArray())
+ " is not in contract store");
}
ABI abi = contract.getInstance().getAbi();
if (Wallet.isConstant(abi, triggerContractFromTransaction)) {
return true;
}
}
return false;
}

private boolean isCallConstant(byte[] address) throws ContractValidateException {

if (TrxType.TRX_CONTRACT_CALL_TYPE == trxType) {
ABI abi = deposit.getContract(address).getInstance().getAbi();
if (Wallet.isConstant(abi, ContractCapsule.getTriggerContractFromTransaction(trx))) {
return true;
}
}
return false;
}

public void finalization() {
if (StringUtils.isEmpty(runtimeError)) {
for (DataWord contract : result.getDeleteAccounts()) {
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/io/midasprotocol/common/runtime/config/VMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public class VMConfig {
// @Setter
// private static boolean ENERGY_LIMIT_HARD_FORK = false;
@Setter
private static boolean ALLOW_TVM_TRANSFER_M1 = false;
private static boolean ALLOW_VM_TRANSFER_M1 = false;
@Setter
private static boolean ALLOW_MULTI_SIGN = false;
@Setter
private static boolean ALLOW_VM_CONSTANTINOPLE = false;

// @Getter
// @Setter
Expand All @@ -61,16 +63,20 @@ public static void initAllowMultiSign(long allow) {
ALLOW_MULTI_SIGN = allow == 1;
}

public static void initAllowTvmTransferM1(long allow) {
ALLOW_TVM_TRANSFER_M1 = allow == 1;
public static void initAllowVmTransferM1(long allow) {
ALLOW_VM_TRANSFER_M1 = allow == 1;
}

// public static boolean getEnergyLimitHardFork() {
// return ENERGY_LIMIT_HARD_FORK;
// }
public static void initAllowVmConstantinople(long allow) {
ALLOW_VM_CONSTANTINOPLE = allow == 1;
}

public static boolean allowTvmTransferM1() {
return ALLOW_TVM_TRANSFER_M1;
return ALLOW_VM_TRANSFER_M1;
}

public static boolean allowVmConstantinople() {
return ALLOW_VM_CONSTANTINOPLE;
}

public static boolean allowMultiSign() {
Expand Down
Loading

0 comments on commit 42d8e35

Please sign in to comment.