-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor PR updates, update config path for this feature
Updating from weighted average to weighted percentile method and naming updates Minor improvements related with PR comments Adding GasWeighted calculator and extracted legacy calculation to be able to choose which one could be used.
- Loading branch information
Showing
13 changed files
with
679 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
rskj-core/src/main/java/org/ethereum/listener/GasPriceCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.ethereum.listener; | ||
|
||
import co.rsk.core.Coin; | ||
import org.ethereum.core.Block; | ||
import org.ethereum.core.TransactionReceipt; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface GasPriceCalculator { | ||
public enum GasCalculatorType { | ||
PLAIN_PERCENTILE, | ||
WEIGHTED_PERCENTILE; | ||
|
||
public static GasCalculatorType fromString(String type) { | ||
if (type == null) { | ||
return null; | ||
} | ||
switch (type.toLowerCase()) { | ||
case "weighted_percentile": | ||
return WEIGHTED_PERCENTILE; | ||
case "plain_percentile": | ||
return PLAIN_PERCENTILE; | ||
default: | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
Optional<Coin> getGasPrice(); | ||
void onBlock(Block block, List<TransactionReceipt> receipts); | ||
|
||
GasCalculatorType getType(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
rskj-core/src/main/java/org/ethereum/listener/PercentileGasPriceCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.ethereum.listener; | ||
|
||
import co.rsk.core.Coin; | ||
import co.rsk.remasc.RemascTransaction; | ||
import org.ethereum.core.Block; | ||
import org.ethereum.core.Transaction; | ||
import org.ethereum.core.TransactionReceipt; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class PercentileGasPriceCalculator implements GasPriceCalculator { | ||
private static final int TX_WINDOW_SIZE = 512; | ||
|
||
private final Coin[] txWindow = new Coin[TX_WINDOW_SIZE]; | ||
private int txIdx = TX_WINDOW_SIZE - 1; | ||
private Coin lastVal; | ||
|
||
@Override | ||
public synchronized Optional<Coin> getGasPrice() { | ||
if (txWindow[0] == null) { // for some reason, not filled yet (i.e. not enough blocks on DB) | ||
return Optional.empty(); | ||
} else { | ||
if (lastVal == null) { | ||
Coin[] values = Arrays.copyOf(txWindow, TX_WINDOW_SIZE); | ||
Arrays.sort(values); | ||
lastVal = values[values.length / 4]; // 25% percentile | ||
} | ||
return Optional.of(lastVal); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void onBlock(Block block, List<TransactionReceipt> receipts) { | ||
onBlock(block.getTransactionsList()); | ||
} | ||
|
||
@Override | ||
public GasCalculatorType getType() { | ||
return GasCalculatorType.PLAIN_PERCENTILE; | ||
} | ||
|
||
private void onBlock(List<Transaction> transactionList) { | ||
for (Transaction tx : transactionList) { | ||
if (!(tx instanceof RemascTransaction)) { | ||
trackGasPrice(tx); | ||
} | ||
} | ||
} | ||
|
||
private void trackGasPrice(Transaction tx) { | ||
if (txIdx == -1) { | ||
txIdx = TX_WINDOW_SIZE - 1; | ||
lastVal = null; // recalculate only 'sometimes' | ||
} | ||
txWindow[txIdx--] = tx.getGasPrice(); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
rskj-core/src/main/java/org/ethereum/listener/WeightedPercentileCalc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.ethereum.listener; | ||
|
||
import co.rsk.core.Coin; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
class WeightedPercentileCalc { | ||
|
||
Coin calculateWeightedPercentile(float percentile, List<WeightedPercentileGasPriceCalculator.GasEntry> gasEntries) { | ||
if (gasEntries == null || gasEntries.isEmpty()) { | ||
return null; | ||
} | ||
|
||
Collections.sort(gasEntries); | ||
|
||
double totalWeight = gasEntries.stream().mapToLong(WeightedPercentileGasPriceCalculator.GasEntry::getGasUsed).sum(); | ||
|
||
double targetWeight = percentile / 100 * totalWeight; | ||
|
||
|
||
double cumulativeWeight = 0; | ||
for (WeightedPercentileGasPriceCalculator.GasEntry pair : gasEntries) { | ||
cumulativeWeight += pair.getGasUsed(); | ||
if (cumulativeWeight >= targetWeight) { | ||
return pair.getGasPrice(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.