Skip to content

Commit

Permalink
Merge pull request #129 from orogvany/fix_testnet
Browse files Browse the repository at this point in the history
[WIP] RC3 - Raise gas limits & fix client sync
  • Loading branch information
orogvany authored Feb 23, 2019
2 parents c341c9f + d5323aa commit 006e5bb
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 16 deletions.
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>1.5.1</version>
<version>1.5.2</version>
<packaging>jar</packaging>
<description>Semux is an experimental high-performance blockchain platform that powers decentralized application.</description>

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/semux/api/v2/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static BlockType blockType(Block block, Transaction coinbaseTransaction)
.resultsRoot(Hex.encode0x(block.getResultsRoot()))
.stateRoot(Hex.encode0x(block.getStateRoot()))
.data(Hex.encode0x(block.getData()))
.gasUsed(String.valueOf(block.getResults().stream()
.mapToLong(TransactionResult::getGasUsed)
.sum()))
.transactions(txs.stream()
.map(tx -> transactionType(block.getNumber(), tx))
.collect(Collectors.toList()));
Expand Down
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 @@ -41,7 +41,7 @@ public class Constants {
/**
* Version of this client.
*/
public static final String CLIENT_VERSION = "1.5.1";
public static final String CLIENT_VERSION = "1.5.2";

/**
* Capability of this client.
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/org/semux/consensus/SemuxBft.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ protected void onNewHeight(long newHeight) {
activeValidators = channelMgr.getActiveChannels(validators);

// Pick 2/3th active validator's height as sync target. The sync will not be
// started if there are less than 2 active validators.
// started if there are less than 2 active validators
OptionalLong target = activeValidators.stream()
.mapToLong(c -> c.getRemotePeer().getLatestBlockNumber() + 1)
.sorted()
Expand All @@ -489,7 +489,7 @@ protected void onNewView(Proof p) {
logger.trace("On new_view: {}", p);

if (p.getHeight() == height // at same height
&& p.getView() > view && state != State.COMMIT && state != State.FINALIZE) {// larger view
&& p.getView() == view + 1 && state != State.COMMIT && state != State.FINALIZE) {// larger view

// check proof-of-unlock
VoteSet vs = new VoteSet(VoteType.PRECOMMIT, p.getHeight(), p.getView() - 1, validators);
Expand All @@ -499,7 +499,7 @@ protected void onNewView(Proof p) {
}

// switch view
logger.debug("Switching view because of NEW_VIEW message");
logger.debug("Switching view because of NEW_VIEW message: {}", p.getView());
jumpToView(p.getView(), p, null);
}
}
Expand All @@ -509,7 +509,7 @@ protected void onProposal(Proposal p) {

if (p.getHeight() == height // at the same height
&& (p.getView() == view && proposal == null && (state == State.NEW_HEIGHT || state == State.PROPOSE) // expecting
|| p.getView() > view && state != State.COMMIT && state != State.FINALIZE) // larger view
|| p.getView() == view + 1 && state != State.COMMIT && state != State.FINALIZE) // larger view
&& isPrimary(p.getHeight(), p.getView(), Hex.encode(p.getSignature().getAddress()))) {

// check proof-of-unlock
Expand Down Expand Up @@ -681,6 +681,12 @@ protected void updateValidators() {
}
activeValidators = channelMgr.getActiveChannels(validators);
lastUpdate = TimeUtil.currentTimeMillis();

if (logger.isDebugEnabled()) {
logger.debug(
"Max validators = {}, Number of validators = {}, validators = {}, Number of active validators = {}, Active validators = {}",
maxValidators, validators.size(), String.join(",", validators), activeValidators.size());
}
}

/**
Expand Down Expand Up @@ -790,9 +796,10 @@ protected Block proposeBlock() {
for (PendingManager.PendingTransaction tx : pending) {
if (tx.transaction.getType() == TransactionType.CALL
|| tx.transaction.getType() == TransactionType.CREATE) {
long maxGasForTransaction = tx.transaction.getGas() + gasUsed;
long pendingGasForBlock = tx.transaction.getGas() + gasUsed;

if (tx.transaction.getGasPrice() >= config.vmMinGasPrice()
&& maxGasForTransaction < config.vmBlockGasLimit()) {
&& pendingGasForBlock < config.vmBlockGasLimit()) {
TransactionResult result = exec.execute(tx.transaction, as, ds, semuxBlock);
gasUsed += result.getGasUsed();

Expand Down Expand Up @@ -883,7 +890,7 @@ protected boolean validateBlock(BlockHeader header, List<Transaction> transactio
// against our own local limit, only
// when proposing
List<TransactionResult> results = exec.execute(transactions, as, ds,
new SemuxBlock(header, config.vmMaxBlockGasLimit()));
new SemuxBlock(header, Long.MAX_VALUE));
if (!Block.validateResults(header, results)) {
logger.warn("Invalid transactions");
return false;
Expand Down Expand Up @@ -928,6 +935,9 @@ protected List<Transaction> getUnvalidatedTransactions(List<Transaction> transac
* @param block
*/
protected void applyBlock(Block block) {

long t1 = TimeUtil.currentTimeMillis();

BlockHeader header = block.getHeader();
List<Transaction> transactions = block.getTransactions();
long number = header.getNumber();
Expand All @@ -946,7 +956,7 @@ protected void applyBlock(Block block) {

// [3] evaluate all transactions
List<TransactionResult> results = exec.execute(transactions, as, ds,
new SemuxBlock(block.getHeader(), config.vmMaxBlockGasLimit()));
new SemuxBlock(block.getHeader(), Long.MAX_VALUE));
if (!Block.validateResults(header, results)) {
logger.debug("Invalid transactions");
return;
Expand Down Expand Up @@ -977,6 +987,10 @@ protected void applyBlock(Block block) {
} finally {
lock.unlock();
}

long t2 = TimeUtil.currentTimeMillis();
logger.debug("Block apply: # txs = {}, time = {} ms", transactions.size(), t2 - t1);

}

public enum State {
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/org/semux/consensus/SemuxSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.commons.lang3.tuple.Pair;
import org.ethereum.vm.client.BlockStore;
import org.semux.Kernel;
import org.semux.Network;
import org.semux.config.Config;
import org.semux.config.Constants;
import org.semux.core.Amount;
Expand Down Expand Up @@ -513,6 +514,16 @@ protected boolean validateBlock(Block block, AccountState asSnapshot, DelegateSt
BlockHeader header = block.getHeader();
List<Transaction> transactions = block.getTransactions();

// a workaround to ensure testnet clients ignore bad block, can remove at later
// date
if (config.network() == Network.TESTNET) {
String badBlock = "0x1a472841464b9bea9e530d73a95e1213f29adef11a3661f3e98df87a9a230b7d";
String blockHash = Hex.encode0x(block.getHash());
if (badBlock.equals(blockHash)) {
return false;
}
}

// [1] check block header
Block latest = chain.getLatestBlock();
if (!Block.validateHeader(latest.getHeader(), header)) {
Expand Down Expand Up @@ -572,8 +583,16 @@ protected boolean validateBlock(Block block, AccountState asSnapshot, DelegateSt
}

protected boolean validateBlockVotes(Block block) {
Set<String> validators = new HashSet<>(chain.getValidators());
int twoThirds = (int) Math.ceil(validators.size() * 2.0 / 3.0);
int maxValidators = config.getNumberOfValidators(block.getNumber());

List<String> validatorList = chain.getValidators();

if (validatorList.size() > maxValidators) {
validatorList = validatorList.subList(0, maxValidators);
}
Set<String> validators = new HashSet<>(validatorList);

int twoThirds = (int) Math.round(validators.size() * 2.0 / 3.0);

Vote vote = new Vote(VoteType.PRECOMMIT, Vote.VALUE_APPROVE, block.getNumber(), block.getView(),
block.getHash());
Expand All @@ -590,7 +609,7 @@ protected boolean validateBlockVotes(Block block) {
if (block.getVotes().stream()
.map(sig -> new ByteArray(sig.getA()))
.collect(Collectors.toSet()).size() < twoThirds) {
logger.warn("Not enough votes, needs 2/3+");
logger.warn("Not enough votes, needs 2/3+ twoThirds = {}, block = {}", twoThirds, block);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/semux/consensus/VoteSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public VoteSet(VoteType type, long height, int view, List<String> validators) {
this.view = view;

this.validators = new HashSet<>(validators);
this.twoThirds = (int) Math.ceil(validators.size() * 2.0 / 3.0);
this.twoThirds = (int) Math.round(validators.size() * 2.0 / 3.0);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/semux/core/TransactionExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ public List<TransactionResult> execute(List<Transaction> txs, AccountState as, D
result.setCode(Code.SUCCESS);
} else {
executeVmTransaction(result, tx, as, block, gasUsedInBlock);
gasUsedInBlock += result.getGasUsed();
if (result.getCode().isAccepted()) {
gasUsedInBlock += result.getGasUsed();
}
}

break;
Expand Down Expand Up @@ -259,7 +261,7 @@ private void executeVmTransaction(TransactionResult result, Transaction tx, Acco

TransactionReceipt summary = executor.run();
if (summary == null) {
result.setCode(Code.FAILURE);
result.setCode(Code.INVALID_GAS);
result.setGasUsed(tx.getGas());
} else {
for (LogInfo log : summary.getLogs()) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/org/semux/api/swagger/v2.2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,11 @@
"type" : "string",
"pattern" : "^(0x)?[0-9a-fA-F]*$"
},
"gasUsed" : {
"type" : "string",
"format" : "int64",
"pattern" : "^\\d+$"
},
"transactions" : {
"type" : "array",
"items" : {
Expand Down

0 comments on commit 006e5bb

Please sign in to comment.