From be261fa65a128900b04abe58d3bc5daeadaefc5e Mon Sep 17 00:00:00 2001 From: Dmitry Shmatko Date: Wed, 18 Apr 2018 16:31:18 +0300 Subject: [PATCH] Polishing RecommendedGasPriceTracker --- .../java/org/ethereum/facade/Ethereum.java | 1 + .../listener/RecommendedGasPriceTracker.java | 27 ++++++++----------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java index aec5c8e99a..0fcd872280 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java @@ -202,6 +202,7 @@ ProgramResult callConstantFunction(String receiveAddress, ECKey senderPrivateKey void initSyncing(); /** + * @deprecated * Calculates a 'reasonable' Gas price based on statistics of the latest transaction's Gas prices * Normally the price returned should be sufficient to execute a transaction since ~25% of the latest * transactions were executed at this or lower price. diff --git a/ethereumj-core/src/main/java/org/ethereum/listener/RecommendedGasPriceTracker.java b/ethereumj-core/src/main/java/org/ethereum/listener/RecommendedGasPriceTracker.java index 02f7b97a95..1d5ab57ef0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/listener/RecommendedGasPriceTracker.java +++ b/ethereumj-core/src/main/java/org/ethereum/listener/RecommendedGasPriceTracker.java @@ -17,7 +17,6 @@ */ package org.ethereum.listener; -import org.apache.commons.collections4.queue.CircularFifoQueue; import org.ethereum.core.Block; import org.ethereum.core.BlockSummary; import org.ethereum.core.Transaction; @@ -25,6 +24,7 @@ import java.lang.reflect.Array; import java.util.Arrays; +import java.util.LinkedList; import java.util.List; @@ -46,15 +46,11 @@ public class RecommendedGasPriceTracker extends EthereumListenerAdapter { private static final int MIN_TRANSACTIONS = 512; private static final int PERCENTILE_SHARE = 4; - private CircularFifoQueue blockGasPrices; + private LinkedList blockGasPrices = new LinkedList<>(); private int idx = 0; private Long recommendedGasPrice = getDefaultPrice(); - public RecommendedGasPriceTracker() { - blockGasPrices = new CircularFifoQueue<>(Math.max(getMinTransactions(), getMinBlocks())); - } - @Override public void onBlock(BlockSummary blockSummary) { onBlock(blockSummary.getBlock()); @@ -63,7 +59,7 @@ public void onBlock(BlockSummary blockSummary) { private void onBlock(Block block) { if (onTransactions(block.getTransactionsList())) { ++idx; - if (idx == getBlocksRecount()) { + if (idx >= getBlocksRecount()) { Long newGasPrice = getGasPrice(); if (newGasPrice != null) { this.recommendedGasPrice = newGasPrice; @@ -81,11 +77,11 @@ private synchronized boolean onTransactions(List txs) { gasPrices[i] = ByteUtil.byteArrayToLong(txs.get(i).getGasPrice()); } - while (blockGasPrices.size() >= getMinBlocks() && - (calcGasPricesSize() - blockGasPrices.get(0).length + gasPrices.length) >= getMinTransactions()) { - blockGasPrices.remove(blockGasPrices.get(0)); - } blockGasPrices.add(gasPrices); + while (blockGasPrices.size() > getMinBlocks() && + (calcGasPricesSize() - blockGasPrices.getFirst().length + gasPrices.length) >= getMinTransactions()) { + blockGasPrices.removeFirst(); + } return true; } @@ -99,11 +95,10 @@ private synchronized Long getGasPrice() { if (size < getMinTransactions() || blockGasPrices.size() < getMinBlocks()) return null; - long[] difficulties = new long[size > getMinTransactions() ? size : getMinTransactions()]; + long[] difficulties = new long[size]; int index = 0; - for (int i = 0; i < blockGasPrices.size(); ++i) { - long[] current = blockGasPrices.get(i); - for (long currentDifficulty : current) { + for (long[] currentBlock : blockGasPrices) { + for (long currentDifficulty : currentBlock) { difficulties[index] = currentDifficulty; ++index; } @@ -176,4 +171,4 @@ public static int getMinTransactions() { public static int getPercentileShare() { return PERCENTILE_SHARE; } -} \ No newline at end of file +}