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 c29a2cb
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 29 deletions.
9 changes: 5 additions & 4 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 Down
2 changes: 1 addition & 1 deletion 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 Down
12 changes: 6 additions & 6 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 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 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
4 changes: 2 additions & 2 deletions src/test/java/org/semux/core/BlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void testValidateTransactions() {
resultsRoot, stateRoot, data);
Block block = new Block(header, transactions);

assertTrue(block.validateHeader(previousHeader, header));
assertTrue(block.validateHeader(header, previousHeader));
assertTrue(block.validateTransactions(previousHeader, transactions, Network.DEVNET));
assertTrue(block.validateResults(previousHeader, results));
}
Expand All @@ -131,7 +131,7 @@ public void testValidateTransactionsSparse() {
resultsRoot, stateRoot, data);
Block block = new Block(header, transactions);

assertTrue(block.validateHeader(previousHeader, header));
assertTrue(block.validateHeader(header, previousHeader));
assertTrue(block.validateTransactions(previousHeader, Collections.singleton(transactions.get(0)), transactions,
Network.DEVNET));
assertTrue(block.validateResults(previousHeader, results));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/semux/integration/SyncingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ 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.validateHeader(block.getHeader(), previousBlock.getHeader()));
assertTrue(block.validateTransactions(previousBlock.getHeader(), block.getTransactions(),
kernel4.getConfig().network()));
assertTrue(block.validateResults(previousBlock.getHeader(), block.getResults()));
Expand Down

0 comments on commit c29a2cb

Please sign in to comment.