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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ src/gen

# ignore slf4j test config
src/test/resources/log4j2-test.xml

CMakeFiles
CMakeFiles/
/CMakeFiles/
CMakeLists.txt.user
CMakeCache.txt
Makefile
install_manifest.txt
cmake_install.cmake
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## [v2.2.x](https://github.com/semuxproject/semux-core/tree/develop) (2020-xx-xx) (WIP)

This release adds support for EIP-665 precompiled contract.

NOTE: A softfork `EIP665_PRECOMPILED_UPGRADE` is introduced.



## [v2.1.x](https://github.com/semuxproject/semux-core/tree/v2.1.1) (2019-09-10)

This release tries to fix several issues about the voting precompiled contracts. All nodes
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.semux</groupId>
<artifactId>semux</artifactId>
<version>2.1.1</version>
<version>2.2.0</version>
<packaging>jar</packaging>
<description>Semux is an experimental high-performance blockchain platform that powers decentralized application.</description>

Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/semux/api/http/HttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import io.netty.handler.codec.http.HttpChunkedInput;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.QueryStringDecoder;
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/semux/api/util/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.semux.Kernel;
import org.semux.Network;
import org.semux.core.Amount;
import org.semux.core.Fork;
import org.semux.core.Transaction;
import org.semux.core.TransactionType;
import org.semux.crypto.CryptoException;
Expand Down Expand Up @@ -218,7 +219,7 @@ public TransactionBuilder withGasPrice(String gasPrice) {
return this;
}

public Transaction buildUnsigned() {
public Transaction buildUnsigned(byte[] from) {
Network network = (this.network != null) ? this.network : kernel.getConfig().network();

TransactionType type = this.type;
Expand Down Expand Up @@ -293,14 +294,19 @@ public Transaction buildUnsigned() {
}
}

return new Transaction(network, type, to, value, fee, nonce, timestamp, data, gas, gasPrice);
return new Transaction(network, type, to, from, value, fee, nonce, timestamp, data, gas, gasPrice, kernel.getBlockchain().isForkActivated(Fork.ED25519_CONTRACT));
}

public Transaction buildSigned() {
if (account == null) {
throw new IllegalArgumentException("The sender is not specified");
}

return buildUnsigned().sign(account);
return buildUnsigned(account.toAddress()).sign(account);
}

public byte[] To ()
{
return to;
}
}
9 changes: 6 additions & 3 deletions src/main/java/org/semux/api/v2/SemuxApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.semux.core.Block;
import org.semux.core.Blockchain;
import org.semux.core.BlockchainImpl;
import org.semux.core.Fork;
import org.semux.core.PendingManager;
import org.semux.core.SyncManager;
import org.semux.core.Transaction;
Expand Down Expand Up @@ -167,7 +168,9 @@ public Response composeRawTransaction(String network, String type, String to, St
.withData(data)
.withGas(gas)
.withGasPrice(gasPrice);
Transaction transaction = transactionBuilder.buildUnsigned();

/*We can use TO address instead FROM address, only if transaction will be used for ComposeRawTransactionResponse*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this? i dont think using the to address works, as it'd result in a different hash.

Transaction transaction = transactionBuilder.buildUnsigned(transactionBuilder.To());

ComposeRawTransactionResponse resp = new ComposeRawTransactionResponse();
resp.setResult(Hex.encode0x(transaction.getEncoded()));
Expand Down Expand Up @@ -662,7 +665,7 @@ public Response signRawTransaction(String raw, String address) {
return badRequest("Address doesn't belong to this wallet.");
}

Transaction tx = Transaction.fromEncoded(txBytes).sign(signerKey);
Transaction tx = Transaction.fromEncoded(txBytes, signerKey.toAddress(), kernel.getBlockchain().isForkActivated(Fork.ED25519_CONTRACT)).sign(signerKey);

SignRawTransactionResponse resp = new SignRawTransactionResponse();
resp.setResult(Hex.encode0x(tx.toBytes()));
Expand Down Expand Up @@ -914,7 +917,7 @@ private TransactionResultType doLocalTransaction(TransactionType type, String to
SemuxBlock block = kernel.createEmptyBlock();
BlockStore blockStore = new SemuxBlockStore(chain);
TransactionExecutor exec = new TransactionExecutor(config, blockStore, chain.isVMEnabled(),
chain.isVotingPrecompiledUpgraded());
chain.isVotingPrecompiledUpgraded(), chain.isEd25519ContractEnabled());
TransactionResult result = exec.execute(tx, asTrack, dsTrack, block, 0);

byte[] contractAddress = type.equals(TransactionType.CREATE)
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/org/semux/config/AbstractConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import static org.semux.Network.MAINNET;
import static org.semux.Network.TESTNET;
import static org.semux.core.Amount.ZERO;
import static org.semux.core.Fork.UNIFORM_DISTRIBUTION;
import static org.semux.core.Fork.VIRTUAL_MACHINE;
import static org.semux.core.Fork.VOTING_PRECOMPILED_UPGRADE;
import static org.semux.core.Fork.*;
import static org.semux.core.Unit.MILLI_SEM;
import static org.semux.core.Unit.SEM;

Expand Down Expand Up @@ -156,6 +154,7 @@ public abstract class AbstractConfig implements Config, ChainSpec {
protected boolean forkUniformDistributionEnabled = false;
protected boolean forkVirtualMachineEnabled = false;
protected boolean forkVotingPrecompiledUpgradeEnabled = false;
protected boolean forkEd25519ContractEnabled = false;

@Override
public ChainSpec spec() {
Expand Down Expand Up @@ -253,7 +252,7 @@ public String getPrimaryValidator(List<String> validators, long height, int view
return validators.get(getUniformDistPrimaryValidatorNumber(validators.size(), height, view));
} else {
byte[] key = Bytes.merge(Bytes.of(height), Bytes.of(view));
return validators.get((Hash.h256(key)[0] & 0xff) % validators.size());
return validators.get((Hash.h256_s(key, null)[0] & 0xff) % validators.size());
}
}

Expand Down Expand Up @@ -295,15 +294,18 @@ public Spec vmSpec() {
periods[MAINNET.id()][UNIFORM_DISTRIBUTION.id()] = new long[] { 200_001L, 400_000L };
periods[MAINNET.id()][VIRTUAL_MACHINE.id()] = new long[] { 1_500_001L, 1_700_000L };
periods[MAINNET.id()][VOTING_PRECOMPILED_UPGRADE.id()] = new long[] { 1_600_001L, 1_800_000L };
periods[MAINNET.id()][ED25519_CONTRACT.id()] = new long[] { 2_800_804L, 3_000_000L };

periods[TESTNET.id()][UNIFORM_DISTRIBUTION.id()] = new long[] { 1L, 200_000L };
periods[TESTNET.id()][VIRTUAL_MACHINE.id()] = new long[] { 1L, 200_000L };
periods[TESTNET.id()][VOTING_PRECOMPILED_UPGRADE.id()] = new long[] { 150_001L, 350_000L };
periods[MAINNET.id()][ED25519_CONTRACT.id()] = new long[] { 1_000_000L, 1_500_000L };

// as soon as possible
periods[DEVNET.id()][UNIFORM_DISTRIBUTION.id()] = new long[] { 1L, 200_000L };
periods[DEVNET.id()][VIRTUAL_MACHINE.id()] = new long[] { 1L, 200_000L };
periods[DEVNET.id()][VOTING_PRECOMPILED_UPGRADE.id()] = new long[] { 1, 200_000L };
periods[DEVNET.id()][ED25519_CONTRACT.id()] = new long[] { 1, 200_000L };
}

@Override
Expand Down Expand Up @@ -590,6 +592,11 @@ public boolean forkVotingPrecompiledUpgradeEnabled() {
return forkVotingPrecompiledUpgradeEnabled;
}

@Override
public boolean forkEd25519ContractEnabled() {
return forkEd25519ContractEnabled;
}

protected void init() {
File f = getFile();
if (!f.exists()) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/semux/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ public interface Config {
*/
boolean forkVotingPrecompiledUpgradeEnabled();

/**
* Returns whether ED25519_CONTRACT fork is enabled.
*
* @return
*/
boolean forkEd25519ContractEnabled();

// =========================
// Checkpoints
// =========================
Expand All @@ -462,4 +469,5 @@ public interface Config {
* [block height]
*/
Map<Fork, Long> manuallyActivatedForks();

}
2 changes: 1 addition & 1 deletion src/main/java/org/semux/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Constants {
/**
* Version of this client.
*/
public static final String CLIENT_VERSION = "2.1.1";
public static final String CLIENT_VERSION = "2.2.0";

/**
* Algorithm name for the 256-bit hash.
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/semux/config/DevnetConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public DevnetConfig(String dataDir) {
this.forkUniformDistributionEnabled = true;
this.forkVirtualMachineEnabled = true;
this.forkVotingPrecompiledUpgradeEnabled = true;
this.forkEd25519ContractEnabled = true;

// set fast blocks
bftNewHeightTimeout = 1000L;
Expand All @@ -48,6 +49,7 @@ public Map<Fork, Long> manuallyActivatedForks() {
forks.put(Fork.UNIFORM_DISTRIBUTION, 1l);
forks.put(Fork.VIRTUAL_MACHINE, 1l);
forks.put(Fork.VOTING_PRECOMPILED_UPGRADE, 1l);
forks.put(Fork.ED25519_CONTRACT, 1l);

return forks;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/semux/config/MainnetConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public MainnetConfig(String dataDir) {
this.forkUniformDistributionEnabled = true;
this.forkVirtualMachineEnabled = true;
this.forkVotingPrecompiledUpgradeEnabled = true;
this.forkEd25519ContractEnabled = false; // enable this when we are ready to go
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/semux/config/TestnetConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public TestnetConfig(String dataDir) {
this.forkUniformDistributionEnabled = true;
this.forkVirtualMachineEnabled = true;
this.forkVotingPrecompiledUpgradeEnabled = true;
this.forkEd25519ContractEnabled = true;
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/semux/consensus/SemuxBft.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.semux.core.Block;
import org.semux.core.BlockHeader;
import org.semux.core.Blockchain;
import org.semux.core.Fork;
import org.semux.core.PendingManager;
import org.semux.core.SyncManager;
import org.semux.core.Transaction;
Expand Down Expand Up @@ -766,7 +767,7 @@ protected Block proposeBlock() {
final List<TransactionResult> includedResults = new ArrayList<>();

TransactionExecutor exec = new TransactionExecutor(config, blockStore, chain.isVMEnabled(),
chain.isVotingPrecompiledUpgraded());
chain.isVotingPrecompiledUpgraded(), chain.isEd25519ContractEnabled());
SemuxBlock semuxBlock = new SemuxBlock(tempHeader, config.spec().maxBlockGasLimit());

// only propose gas used up to configured block gas limit
Expand Down Expand Up @@ -841,7 +842,7 @@ protected boolean validateBlockProposal(BlockHeader header, List<Transaction> tr

// [2] check transactions
List<Transaction> unvalidatedTransactions = getUnvalidatedTransactions(transactions);
if (!block.validateTransactions(header, unvalidatedTransactions, transactions, config.network())) {
if (!block.validateTransactions(header, unvalidatedTransactions, transactions, config.network(), kernel.getBlockchain().isForkActivated(Fork.ED25519_CONTRACT))) {
logger.warn("Invalid transactions");
return false;
}
Expand All @@ -852,7 +853,7 @@ protected boolean validateBlockProposal(BlockHeader header, List<Transaction> tr

// [3] evaluate transactions
TransactionExecutor transactionExecutor = new TransactionExecutor(config, blockStore, chain.isVMEnabled(),
chain.isVotingPrecompiledUpgraded());
chain.isVotingPrecompiledUpgraded(), chain.isEd25519ContractEnabled());
List<TransactionResult> results = transactionExecutor.execute(transactions, asTrack, dsTrack,
new SemuxBlock(header, config.spec().maxBlockGasLimit()), 0);
if (!block.validateResults(header, results)) {
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/semux/core/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ public boolean validateHeader(BlockHeader header, BlockHeader parentHeader) {
* @param header
* @param transactions
* @param network
* @param isFixTxHash
* @return
*/
public boolean validateTransactions(BlockHeader header, List<Transaction> transactions, Network network) {
return validateTransactions(header, transactions, transactions, network);
public boolean validateTransactions(BlockHeader header, List<Transaction> transactions, Network network, boolean isFixTxHash) {
return validateTransactions(header, transactions, transactions, network, isFixTxHash);
}

/**
Expand All @@ -169,18 +170,19 @@ public boolean validateTransactions(BlockHeader header, List<Transaction> transa
* all transactions within the block
* @param network
* network
* @param isFixTxHash
* @return
*/
public boolean validateTransactions(BlockHeader header, Collection<Transaction> unvalidatedTransactions,
List<Transaction> allTransactions, Network network) {
List<Transaction> allTransactions, Network network, boolean isFixTxHash) {

// validate transactions
if (!Key.isVerifyBatchSupported() || unvalidatedTransactions.size() < 3) {
if (!unvalidatedTransactions.parallelStream().allMatch(tx -> tx.validate(network))) {
if (!unvalidatedTransactions.parallelStream().allMatch(tx -> tx.validate_verify_sign(network, isFixTxHash))) {
return false;
}
} else {
if (!unvalidatedTransactions.parallelStream().allMatch(tx -> tx.validate(network, false))) {
if (!unvalidatedTransactions.parallelStream().allMatch(tx -> tx.validate_no_verify_sign(network, isFixTxHash))) {
return false;
}

Expand Down Expand Up @@ -460,6 +462,7 @@ public byte[] getEncodedVotes() {
* Serialized transaction results
* @param v
* Serialized votes
* @param isFixTxHash
* @return
*/
public static Block fromComponents(byte[] h, byte[] t, byte[] r, byte[] v) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/semux/core/BlockHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public BlockHeader(long number, byte[] coinbase, byte[] prevHash, long timestamp
enc.writeBytes(stateRoot);
enc.writeBytes(data);
this.encoded = enc.toBytes();
this.hash = Hash.h256(encoded);
this.hash = Hash.h256_s(encoded, null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this need changing? the block header algo should be unchanged, else we'd need to use forkflag.

}

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ public boolean validate() {
&& stateRoot != null && Arrays.equals(Bytes.EMPTY_HASH, stateRoot) // RESERVED FOR VM
&& data != null && data.length <= BlockHeaderData.MAX_SIZE
&& encoded != null
&& Arrays.equals(Hash.h256(encoded), hash);
&& Arrays.equals(Hash.h256_s(encoded, null), hash);
}

public byte[] getHash() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/semux/core/Blockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,6 @@ public interface Blockchain {
boolean isVMEnabled();

boolean isVotingPrecompiledUpgraded();

boolean isEd25519ContractEnabled();
}
Loading