Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VM: add conversion between SEM and ETH #183

Merged
merged 11 commits into from
Jul 3, 2019
9 changes: 5 additions & 4 deletions config/semux.properties
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ ui.fractionDigits = 9
#================

# Set the maximum gas this client can process in one block proposal
# for RC3 we raise default block gas limit to 20 million for tuning.
# This should be removed for mainnet!
vm.blockGasLimit = 20000000
txpool.blockGasLimit = 10000000

# The minimum price this client will accept for gas
vm.minGasPrice = 1
txpool.minGasPrice = 1

# The max transaction time drift in milliseconds
txpool.maxTransactionTimeDrift = 7200000
4 changes: 2 additions & 2 deletions src/main/java/org/semux/api/util/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public TransactionBuilder withValue(String value) {

public TransactionBuilder withFee(String fee, boolean optional) {
if (optional && (fee == null || fee.isEmpty())) {
this.fee = kernel.getConfig().minTransactionFee();
this.fee = kernel.getConfig().spec().minTransactionFee();
return this;
}

Expand Down Expand Up @@ -259,7 +259,7 @@ public Transaction buildUnsigned() {
// DELEGATE transaction has fixed receiver and value
if (type == TransactionType.DELEGATE) {
to = Bytes.EMPTY_ADDRESS;
value = kernel.getConfig().minDelegateBurnAmount();
value = kernel.getConfig().spec().minDelegateBurnAmount();
}
if (type == TransactionType.CREATE) {
to = Bytes.EMPTY_ADDRESS;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/semux/api/v2/SemuxApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,8 @@ public Response call(String from, String to, String value, String gasPrice, Stri
gasPrice, gas);

SemuxTransaction transaction = new SemuxTransaction(tx);
SemuxBlock block = new SemuxBlock(kernel.getBlockchain().getLatestBlock().getHeader(), 0);
SemuxBlock block = new SemuxBlock(kernel.getBlockchain().getLatestBlock().getHeader(),
kernel.getConfig().spec().maxBlockGasLimit());
Repository repository = new SemuxRepository(kernel.getBlockchain().getAccountState());
ProgramInvokeFactory invokeFactory = new ProgramInvokeFactoryImpl();
BlockStore blockStore = new SemuxBlockStore(kernel.getBlockchain());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/semux/api/v2/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public static PeerType peerType(Peer peer) {
public static TransactionLimitsType transactionLimitsType(Kernel kernel,
org.semux.core.TransactionType transactionType) {
return new TransactionLimitsType()
.maxTransactionDataSize(kernel.getConfig().maxTransactionDataSize(transactionType))
.minTransactionFee(encodeAmount(kernel.getConfig().minTransactionFee()))
.maxTransactionDataSize(kernel.getConfig().spec().maxTransactionDataSize(transactionType))
.minTransactionFee(encodeAmount(kernel.getConfig().spec().minTransactionFee()))
.minDelegateBurnAmount(encodeAmount(
transactionType.equals(DELEGATE) ? kernel.getConfig().minDelegateBurnAmount() : null));
transactionType.equals(DELEGATE) ? kernel.getConfig().spec().minDelegateBurnAmount() : null));
}

public static TransactionType transactionType(Transaction tx) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/semux/cli/SemuxCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static void main(String[] args) {
* Creates a new Semux CLI instance.
*/
public SemuxCli() {
SystemUtil.setLocale(getConfig().locale());
SystemUtil.setLocale(getConfig().uiLocale());

Option helpOption = Option.builder()
.longOpt(SemuxOption.HELP.toString())
Expand Down
195 changes: 96 additions & 99 deletions src/main/java/org/semux/config/AbstractConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,28 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractConfig implements Config {
public abstract class AbstractConfig implements Config, ChainSpec {

private static final Logger logger = LoggerFactory.getLogger(AbstractConfig.class);

private static final String CONFIG_FILE = "semux.properties";

// =========================
// Chain spec
// =========================
protected long maxBlockGasLimit = 10_000_000L; // 10m gas
protected int maxBlockTransactionsSize = 1024 * 1024;
protected Amount minTransactionFee = MILLI_SEM.of(5);
protected Amount minDelegateBurnAmount = SEM.of(1000);
protected long nonVMTransactionGasCost = 21_000L;

// =========================
// General
// =========================
protected File dataDir;
protected Network network;
protected short networkVersion;

protected int maxBlockTransactionsSize = 1024 * 1024;
protected Amount minTransactionFee = MILLI_SEM.of(5);
protected long maxTransactionTimeDrift = TimeUnit.HOURS.toMillis(2);
protected Amount minDelegateBurnAmount = SEM.of(1000);

// =========================
// P2P
// =========================
Expand Down Expand Up @@ -112,21 +116,19 @@ public abstract class AbstractConfig implements Config {
protected long bftPreCommitTimeout = 6000L;
protected long bftCommitTimeout = 3000L;
protected long bftFinalizeTimeout = 3000L;
protected long maxBlockTimeDrift = TimeUnit.SECONDS.toMillis(30);
protected long bftMaxBlockTimeDrift = TimeUnit.SECONDS.toMillis(30);

// =========================
// Virtual machine
// Transaction pool
// =========================
protected int vmMaxStackSize = 1024;
protected int vmInitHeapSize = 128;
protected int vmBlockGasLimit = 999_999;
protected int vmMaxBlockGasLimit = 9_999_999;
protected int vmMinGasPrice = 1;
protected int poolBlockGasLimit = 5_000_000;
protected int poolMinGasPrice = 10; // 10 NanoSEM = 10 Gwei
protected long poolMaxTransactionTimeDrift = TimeUnit.HOURS.toMillis(2);

// =========================
// UI
// =========================
protected Locale locale = Locale.getDefault();
protected Locale uiLocale = Locale.getDefault();
protected String uiUnit = "SEM";
protected int uiFractionDigits = 9;

Expand All @@ -136,13 +138,11 @@ public abstract class AbstractConfig implements Config {
protected boolean forkUniformDistributionEnabled = false;
protected boolean forkVirtualMachineEnabled = false;

/**
* Create an {@link AbstractConfig} instance.
*
* @param dataDir
* @param network
* @param networkVersion
*/
@Override
public ChainSpec spec() {
return this;
}

protected AbstractConfig(String dataDir, Network network, short networkVersion) {
this.dataDir = new File(dataDir);
this.network = network;
Expand All @@ -153,8 +153,51 @@ protected AbstractConfig(String dataDir, Network network, short networkVersion)
}

@Override
public File getFile() {
return new File(configDir(), CONFIG_FILE);
public long maxBlockGasLimit() {
return maxBlockGasLimit;
}

@Override
public int maxBlockTransactionsSize() {
return maxBlockTransactionsSize;
}

@Override
public int maxTransactionDataSize(TransactionType type) {
switch (type) {
case COINBASE:
case UNVOTE:
case VOTE:
return 0; // not required

case TRANSFER:
return 128; // for memo

case DELEGATE:
return 16; // for name

case CREATE:
case CALL:
return 512 * 1024; // for dapps

default:
throw new UnreachableException();
}
}

@Override
public Amount minTransactionFee() {
return minTransactionFee;
}

@Override
public Amount minDelegateBurnAmount() {
return minDelegateBurnAmount;
}

@Override
public long nonVMTransactionGasCost() {
return nonVMTransactionGasCost;
}

@Override
Expand Down Expand Up @@ -223,18 +266,8 @@ private int getUniformDistPrimaryValidatorNumber(int size, long height, long vie
}

@Override
public String getClientId() {
return String.format("%s/v%s-%s/%s/%s",
Constants.CLIENT_NAME,
Constants.CLIENT_VERSION,
SystemUtil.getImplementationVersion(),
SystemUtil.getOsName().toString(),
SystemUtil.getOsArch());
}

@Override
public CapabilitySet getClientCapabilities() {
return Constants.CLIENT_CAPABILITIES;
public File getFile() {
return new File(configDir(), CONFIG_FILE);
}

@Override
Expand Down Expand Up @@ -268,48 +301,18 @@ public short networkVersion() {
}

@Override
public int maxBlockTransactionsSize() {
return maxBlockTransactionsSize;
}

@Override
public int maxTransactionDataSize(TransactionType type) {
switch (type) {
case COINBASE:
return 0; // not required

case TRANSFER:
return 128; // for memo

case DELEGATE:
return 16; // for name

case UNVOTE:
case VOTE:
return 0; // not required

case CREATE:
case CALL:
return 64 * 1024; // for dapps

default:
throw new UnreachableException();
}
}

@Override
public Amount minTransactionFee() {
return minTransactionFee;
}

@Override
public long maxTransactionTimeDrift() {
return maxTransactionTimeDrift;
public String getClientId() {
return String.format("%s/v%s-%s/%s/%s",
Constants.CLIENT_NAME,
Constants.CLIENT_VERSION,
SystemUtil.getImplementationVersion(),
SystemUtil.getOsName().toString(),
SystemUtil.getOsArch());
}

@Override
public Amount minDelegateBurnAmount() {
return minDelegateBurnAmount;
public CapabilitySet getClientCapabilities() {
return Constants.CLIENT_CAPABILITIES;
}

@Override
Expand Down Expand Up @@ -468,38 +471,28 @@ public long bftFinalizeTimeout() {
}

@Override
public long maxBlockTimeDrift() {
return maxBlockTimeDrift;
public long bftMaxBlockTimeDrift() {
return bftMaxBlockTimeDrift;
}

@Override
public int vmMaxStackSize() {
return vmMaxStackSize;
public int poolBlockGasLimit() {
return poolBlockGasLimit;
}

@Override
public int vmInitialHeapSize() {
return vmInitHeapSize;
public int poolMinGasPrice() {
return poolMinGasPrice;
}

@Override
public int vmBlockGasLimit() {
return vmBlockGasLimit;
public long poolMaxTransactionTimeDrift() {
return poolMaxTransactionTimeDrift;
}

@Override
public int vmMaxBlockGasLimit() {
return vmMaxBlockGasLimit;
}

@Override
public int vmMinGasPrice() {
return vmMinGasPrice;
}

@Override
public Locale locale() {
return locale;
public Locale uiLocale() {
return uiLocale;
}

@Override
Expand Down Expand Up @@ -618,7 +611,7 @@ protected void init() {
// ui.locale must be in format of en_US ([language]_[country])
String[] localeComponents = props.getProperty(name).trim().split("_");
if (localeComponents.length == 2) {
locale = new Locale(localeComponents[0], localeComponents[1]);
uiLocale = new Locale(localeComponents[0], localeComponents[1]);
}
break;
}
Expand All @@ -630,12 +623,16 @@ protected void init() {
uiFractionDigits = Integer.parseInt(props.getProperty(name).trim());
break;
}
case "vm.blockGasLimit": {
vmBlockGasLimit = Integer.parseInt(props.getProperty(name).trim());
case "txpool.blockGasLimit": {
poolBlockGasLimit = Integer.parseInt(props.getProperty(name).trim());
break;
}
case "txpool.minGasPrice": {
poolMinGasPrice = Integer.parseInt(props.getProperty(name).trim());
break;
}
case "vm.minGasPrice": {
vmMinGasPrice = Integer.parseInt(props.getProperty(name).trim());
case "txpool.maxTransactionTimeDrift": {
poolMaxTransactionTimeDrift = Integer.parseInt(props.getProperty(name).trim());
break;
}
default:
Expand Down
Loading