17
17
*/
18
18
package org .ethereum .listener ;
19
19
20
- import org .apache .commons .collections4 .queue .CircularFifoQueue ;
21
20
import org .ethereum .core .Block ;
22
21
import org .ethereum .core .BlockSummary ;
23
22
import org .ethereum .core .Transaction ;
24
23
import org .ethereum .util .ByteUtil ;
25
24
26
25
import java .lang .reflect .Array ;
27
26
import java .util .Arrays ;
27
+ import java .util .LinkedList ;
28
28
import java .util .List ;
29
29
30
30
@@ -46,15 +46,11 @@ public class RecommendedGasPriceTracker extends EthereumListenerAdapter {
46
46
private static final int MIN_TRANSACTIONS = 512 ;
47
47
private static final int PERCENTILE_SHARE = 4 ;
48
48
49
- private CircularFifoQueue <long []> blockGasPrices ;
49
+ private LinkedList <long []> blockGasPrices = new LinkedList <>() ;
50
50
51
51
private int idx = 0 ;
52
52
private Long recommendedGasPrice = getDefaultPrice ();
53
53
54
- public RecommendedGasPriceTracker () {
55
- blockGasPrices = new CircularFifoQueue <>(Math .max (getMinTransactions (), getMinBlocks ()));
56
- }
57
-
58
54
@ Override
59
55
public void onBlock (BlockSummary blockSummary ) {
60
56
onBlock (blockSummary .getBlock ());
@@ -63,7 +59,7 @@ public void onBlock(BlockSummary blockSummary) {
63
59
private void onBlock (Block block ) {
64
60
if (onTransactions (block .getTransactionsList ())) {
65
61
++idx ;
66
- if (idx = = getBlocksRecount ()) {
62
+ if (idx > = getBlocksRecount ()) {
67
63
Long newGasPrice = getGasPrice ();
68
64
if (newGasPrice != null ) {
69
65
this .recommendedGasPrice = newGasPrice ;
@@ -81,11 +77,11 @@ private synchronized boolean onTransactions(List<Transaction> txs) {
81
77
gasPrices [i ] = ByteUtil .byteArrayToLong (txs .get (i ).getGasPrice ());
82
78
}
83
79
84
- while (blockGasPrices .size () >= getMinBlocks () &&
85
- (calcGasPricesSize () - blockGasPrices .get (0 ).length + gasPrices .length ) >= getMinTransactions ()) {
86
- blockGasPrices .remove (blockGasPrices .get (0 ));
87
- }
88
80
blockGasPrices .add (gasPrices );
81
+ while (blockGasPrices .size () > getMinBlocks () &&
82
+ (calcGasPricesSize () - blockGasPrices .getFirst ().length + gasPrices .length ) >= getMinTransactions ()) {
83
+ blockGasPrices .removeFirst ();
84
+ }
89
85
return true ;
90
86
}
91
87
@@ -99,11 +95,10 @@ private synchronized Long getGasPrice() {
99
95
if (size < getMinTransactions () ||
100
96
blockGasPrices .size () < getMinBlocks ()) return null ;
101
97
102
- long [] difficulties = new long [size > getMinTransactions () ? size : getMinTransactions () ];
98
+ long [] difficulties = new long [size ];
103
99
int index = 0 ;
104
- for (int i = 0 ; i < blockGasPrices .size (); ++i ) {
105
- long [] current = blockGasPrices .get (i );
106
- for (long currentDifficulty : current ) {
100
+ for (long [] currentBlock : blockGasPrices ) {
101
+ for (long currentDifficulty : currentBlock ) {
107
102
difficulties [index ] = currentDifficulty ;
108
103
++index ;
109
104
}
@@ -176,4 +171,4 @@ public static int getMinTransactions() {
176
171
public static int getPercentileShare () {
177
172
return PERCENTILE_SHARE ;
178
173
}
179
- }
174
+ }
0 commit comments