Skip to content

Commit

Permalink
Polishing RecommendedGasPriceTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Apr 18, 2018
1 parent 738f265 commit be261fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
*/
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;
import org.ethereum.util.ByteUtil;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


Expand All @@ -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<long[]> blockGasPrices;
private LinkedList<long[]> 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());
Expand All @@ -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;
Expand All @@ -81,11 +77,11 @@ private synchronized boolean onTransactions(List<Transaction> 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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -176,4 +171,4 @@ public static int getMinTransactions() {
public static int getPercentileShare() {
return PERCENTILE_SHARE;
}
}
}

0 comments on commit be261fa

Please sign in to comment.