Skip to content

Commit

Permalink
Core: fix an issue in BlockchainPerformance
Browse files Browse the repository at this point in the history
  • Loading branch information
semux committed Jul 6, 2019
1 parent e7cdf8e commit 185986d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 43 deletions.
15 changes: 8 additions & 7 deletions src/main/java/org/semux/consensus/SemuxBft.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.semux.core.Transaction;
import org.semux.core.TransactionExecutor;
import org.semux.core.TransactionResult;
import org.semux.core.TransactionType;
import org.semux.core.state.AccountState;
import org.semux.core.state.DelegateState;
import org.semux.crypto.Hash;
Expand Down Expand Up @@ -772,7 +771,8 @@ protected Block proposeBlock() {
byte[] data = chain.constructBlockData();

// fetch pending transactions
final List<PendingManager.PendingTransaction> pending = pendingMgr.getPendingTransactions(config.poolBlockGasLimit());
final List<PendingManager.PendingTransaction> pending = pendingMgr
.getPendingTransactions(config.poolBlockGasLimit());
final List<Transaction> pendingTxs = new ArrayList<>();
final List<TransactionResult> pendingResults = new ArrayList<>();

Expand All @@ -792,7 +792,8 @@ protected Block proposeBlock() {
for (PendingManager.PendingTransaction pendingTx : pending) {
Transaction tx = pendingTx.transaction;
if (tx.isVMTransaction()) {
// Note: transactions that exceed the remaining block gas limit are ignored; however,
// Note: transactions that exceed the remaining block gas limit are ignored;
// however,
// this doesn't mean they're invalid because the gas usage will be less
// than gas limit.
if (tx.getGas() <= myRemainingBlockGas) {
Expand Down Expand Up @@ -854,7 +855,7 @@ protected boolean validateBlockProposal(Block block) {

// [1] check block header
Block latest = chain.getLatestBlock();
if (!block.validateHeader(latest.getHeader(), header)) {
if (!Block.validateHeader(header, latest.getHeader())) {
logger.warn("Invalid block header");
return false;
}
Expand All @@ -877,7 +878,7 @@ protected boolean validateBlockProposal(Block block) {
// [2] check transactions and results (skipped)
List<Transaction> unvalidatedTransactions = getUnvalidatedTransactions(transactions);

if (!block.validateTransactions(header, unvalidatedTransactions, transactions, config.network())) {
if (!Block.validateTransactions(header, unvalidatedTransactions, transactions, config.network())) {
logger.warn("Invalid block transactions");
return false;
}
Expand All @@ -899,7 +900,7 @@ protected boolean validateBlockProposal(Block block) {
new SemuxBlock(header, config.spec().maxBlockGasLimit()), chain);
block.setResults(results);

if (!block.validateResults(header, results)) {
if (!Block.validateResults(header, results)) {
logger.warn("Invalid transactions");
return false;
}
Expand Down Expand Up @@ -961,7 +962,7 @@ protected void applyBlock(Block block) {
// [3] evaluate all transactions
List<TransactionResult> results = exec.execute(transactions, as, ds,
new SemuxBlock(block.getHeader(), config.spec().maxBlockGasLimit()), chain);
if (!block.validateResults(header, results)) {
if (!Block.validateResults(header, results)) {
logger.warn("Invalid transactions");
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/semux/consensus/SemuxSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ protected boolean validateBlock(Block block, AccountState asSnapshot, DelegateSt

// [1] check block header
Block latest = chain.getLatestBlock();
if (!block.validateHeader(latest.getHeader(), header)) {
if (!Block.validateHeader(header, latest.getHeader())) {
logger.error("Invalid block header");
return false;
}
Expand All @@ -561,11 +561,11 @@ protected boolean validateBlock(Block block, AccountState asSnapshot, DelegateSt
}

// [2] check transactions and results
if (!block.validateTransactions(header, transactions, config.network())) {
if (!Block.validateTransactions(header, transactions, config.network())) {
logger.error("Invalid block transactions");
return false;
}
if (!block.validateResults(header, block.getResults())) {
if (!Block.validateResults(header, block.getResults())) {
logger.error("Invalid results");
return false;
}
Expand All @@ -581,7 +581,7 @@ protected boolean validateBlock(Block block, AccountState asSnapshot, DelegateSt
new SemuxBlock(block.getHeader(), config.spec().maxBlockGasLimit()), chain);
block.setResults(results);

if (!block.validateResults(header, results)) {
if (!Block.validateResults(header, results)) {
logger.error("Invalid transactions");
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/semux/core/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ public void setVotes(List<Signature> votes) {
* Validates block header.
*
* @param header
* @param previous
* @param parentHeader
* @return
*/
public boolean validateHeader(BlockHeader previous, BlockHeader header) {
public static boolean validateHeader(BlockHeader header, BlockHeader parentHeader) {
if (header == null) {
logger.warn("Header was null.");
return false;
Expand All @@ -153,17 +153,17 @@ public boolean validateHeader(BlockHeader previous, BlockHeader header) {
return false;
}

if (header.getNumber() != previous.getNumber() + 1) {
if (header.getNumber() != parentHeader.getNumber() + 1) {
logger.warn("Header number was not one greater than previous block.");
return false;
}

if (!Arrays.equals(header.getParentHash(), previous.getHash())) {
if (!Arrays.equals(header.getParentHash(), parentHeader.getHash())) {
logger.warn("Header parent hash was not equal to previous block hash.");
return false;
}

if (header.getTimestamp() <= previous.getTimestamp()) {
if (header.getTimestamp() <= parentHeader.getTimestamp()) {
logger.warn("Header timestamp was before previous block.");
return false;
}
Expand All @@ -179,7 +179,7 @@ public boolean validateHeader(BlockHeader previous, BlockHeader header) {
* @param network
* @return
*/
public boolean validateTransactions(BlockHeader header, List<Transaction> transactions, Network network) {
public static boolean validateTransactions(BlockHeader header, List<Transaction> transactions, Network network) {
return validateTransactions(header, transactions, transactions, network);
}

Expand All @@ -197,7 +197,7 @@ public boolean validateTransactions(BlockHeader header, List<Transaction> transa
* network
* @return
*/
public boolean validateTransactions(BlockHeader header, Collection<Transaction> unvalidatedTransactions,
public static boolean validateTransactions(BlockHeader header, Collection<Transaction> unvalidatedTransactions,
List<Transaction> allTransactions, Network network) {

// validate transactions
Expand Down Expand Up @@ -229,7 +229,7 @@ public boolean validateTransactions(BlockHeader header, Collection<Transaction>
* @param results
* @return
*/
public boolean validateResults(BlockHeader header, List<TransactionResult> results) {
public static boolean validateResults(BlockHeader header, List<TransactionResult> results) {
// validate results
for (TransactionResult result : results) {
if (result.getCode().isRejected()) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/semux/core/PendingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ public synchronized List<PendingTransaction> getPendingTransactions(long blockGa
while (it.hasNext() && blockGasLimit > 0) {
PendingTransaction tx = it.next();

long gasUsage = tx.transaction.isVMTransaction() ? tx.result.getGasUsed() : kernel.getConfig().spec().nonVMTransactionGasCost();
long gasUsage = tx.transaction.isVMTransaction() ? tx.result.getGasUsed()
: kernel.getConfig().spec().nonVMTransactionGasCost();
if (blockGasLimit > gasUsage) {
txs.add(tx);
blockGasLimit -= gasUsage;
Expand Down
22 changes: 8 additions & 14 deletions src/test/java/org/semux/bench/BlockchainPerformance.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public static Block testBlockCreation() {
List<Transaction> txs = new ArrayList<>();
List<TransactionResult> res = new ArrayList<>();

// TODO: use block gas limit

int total = 0;
for (int i = 0;; i++) {
long remainingBlockGas = config.spec().maxBlockGasLimit();
for (int i = 0; remainingBlockGas >= config.spec().nonVMTransactionGasCost(); i++) {
Network network = config.network();
TransactionType type = TransactionType.TRANSFER;
byte[] to = Bytes.random(20);
Expand All @@ -59,14 +57,10 @@ public static Block testBlockCreation() {
long timestamp = TimeUtil.currentTimeMillis();
byte[] data = Bytes.EMPTY_BYTES;
Transaction tx = new Transaction(network, type, to, value, fee, nonce, timestamp, data).sign(key);

if (total + tx.size() > 1024 * 1024) {
break;
}

txs.add(tx);
res.add(new TransactionResult());
total += tx.size();

remainingBlockGas -= config.spec().nonVMTransactionGasCost();
}

long number = 1;
Expand Down Expand Up @@ -101,12 +95,12 @@ public static Block testBlockCreation() {
}

public static void testBlockValidation(Block block) {
Genesis gen = Genesis.load(Network.DEVNET);
Genesis genesis = Genesis.load(Network.DEVNET);

long t1 = System.nanoTime();
block.validateHeader(gen.getHeader(), block.getHeader());
block.validateTransactions(gen.getHeader(), block.getTransactions(), config.network());
block.validateResults(gen.getHeader(), block.getResults());
Block.validateHeader(block.getHeader(), genesis.getHeader());
Block.validateTransactions(block.getHeader(), block.getTransactions(), config.network());
Block.validateResults(block.getHeader(), block.getResults());
// block votes validation skipped
long t2 = System.nanoTime();

Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/semux/core/BlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public void testValidateTransactions() {
resultsRoot, stateRoot, data);
Block block = new Block(header, transactions);

assertTrue(block.validateHeader(previousHeader, header));
assertTrue(block.validateTransactions(previousHeader, transactions, Network.DEVNET));
assertTrue(block.validateResults(previousHeader, results));
assertTrue(Block.validateHeader(header, previousHeader));
assertTrue(Block.validateTransactions(previousHeader, transactions, Network.DEVNET));
assertTrue(Block.validateResults(previousHeader, results));
}

@Test
Expand All @@ -131,9 +131,9 @@ public void testValidateTransactionsSparse() {
resultsRoot, stateRoot, data);
Block block = new Block(header, transactions);

assertTrue(block.validateHeader(previousHeader, header));
assertTrue(block.validateTransactions(previousHeader, Collections.singleton(transactions.get(0)), transactions,
assertTrue(Block.validateHeader(header, previousHeader));
assertTrue(Block.validateTransactions(previousHeader, Collections.singleton(transactions.get(0)), transactions,
Network.DEVNET));
assertTrue(block.validateResults(previousHeader, results));
assertTrue(Block.validateResults(previousHeader, results));
}
}
6 changes: 3 additions & 3 deletions src/test/java/org/semux/integration/SyncingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ public void testSync() throws IOException {
for (int i = 1; i <= targetHeight(); i++) {
Block block = kernel4.getBlockchain().getBlock(i);
Block previousBlock = kernel4.getBlockchain().getBlock(i - 1);
assertTrue(block.validateHeader(previousBlock.getHeader(), block.getHeader()));
assertTrue(block.validateTransactions(previousBlock.getHeader(), block.getTransactions(),
assertTrue(Block.validateHeader(block.getHeader(), previousBlock.getHeader()));
assertTrue(Block.validateTransactions(previousBlock.getHeader(), block.getTransactions(),
kernel4.getConfig().network()));
assertTrue(block.validateResults(previousBlock.getHeader(), block.getResults()));
assertTrue(Block.validateResults(previousBlock.getHeader(), block.getResults()));

assertTrue(block.getVotes().size() >= 3 * 2 / 3);
}
Expand Down

0 comments on commit 185986d

Please sign in to comment.