-
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.
Adding GasWeighted calculator and extracted legacy calculation to be …
…able to choose which one could be used.
- Loading branch information
Showing
9 changed files
with
387 additions
and
41 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
32 changes: 32 additions & 0 deletions
32
rskj-core/src/main/java/org/ethereum/listener/GasCalculator.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,32 @@ | ||
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 GasCalculator { | ||
public enum GasCalculatorType { | ||
LEGACY, | ||
WEIGHTED; | ||
|
||
public static GasCalculatorType fromString(String type) { | ||
if (type == null) { | ||
return null; | ||
} | ||
switch (type.toLowerCase()) { | ||
case "weighted": | ||
return WEIGHTED; | ||
case "legacy": | ||
return LEGACY; | ||
default: | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
Optional<Coin> getGasPrice(); | ||
void onBlock(Block block, List<TransactionReceipt> receipts); | ||
} |
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
105 changes: 105 additions & 0 deletions
105
rskj-core/src/main/java/org/ethereum/listener/GasWeightedCalc.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,105 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2017 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 org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.*; | ||
|
||
public class GasWeightedCalc implements GasCalculator { | ||
private static final Logger logger = LoggerFactory.getLogger("gaspricetracker"); | ||
private static final int WINDOW_SIZE = 512; | ||
private final Deque<GasEntry> gasWindow = new ArrayDeque<>(WINDOW_SIZE); | ||
private final Map<Coin, Long> windowMap = new HashMap<>(); | ||
private int txCount = 0; | ||
private Coin cachedGasPrice = null; | ||
|
||
public synchronized void onBlock(Block block, List<TransactionReceipt> receipts) { | ||
Check notice Code scanning / CodeQL Missing Override annotation Note
This method overrides
GasCalculator.onBlock Error loading related location Loading |
||
for(TransactionReceipt receipt : receipts) { | ||
if (!(receipt.getTransaction() instanceof RemascTransaction)) { | ||
addTx(receipt.getTransaction(), new Coin(receipt.getGasUsed()).asBigInteger().longValue()); | ||
} | ||
} | ||
} | ||
|
||
private void addTx(Transaction tx, long gasUsed) { | ||
txCount++; | ||
|
||
Coin gasPrice = tx.getGasPrice(); | ||
|
||
if (gasWindow.size() == WINDOW_SIZE) { | ||
GasEntry entry = gasWindow.removeFirst(); | ||
long value = windowMap.get(entry.gasPrice) - entry.gasUsed; | ||
if (value > 0) { | ||
windowMap.put(entry.gasPrice, value); | ||
} else { | ||
windowMap.remove(entry.gasPrice); | ||
} | ||
} | ||
|
||
gasWindow.add(new GasEntry(gasPrice, gasUsed)); | ||
windowMap.merge(gasPrice, gasUsed, Long::sum); | ||
|
||
if (txCount >= WINDOW_SIZE) { | ||
txCount = 0; // Reset the count | ||
cachedGasPrice = calculateGasPrice(); | ||
logger.info("Updated gas price -> {}",cachedGasPrice); | ||
} | ||
} | ||
|
||
private synchronized Coin calculateGasPrice() { | ||
double weightedSum = 0; | ||
double totalGasUsed = 0; | ||
for(Map.Entry<Coin,Long> entry : windowMap.entrySet()) { | ||
weightedSum += entry.getKey().asBigInteger().doubleValue() * entry.getValue(); | ||
totalGasUsed += entry.getValue(); | ||
} | ||
|
||
if (totalGasUsed > 0) { | ||
double result = weightedSum / totalGasUsed; | ||
return new Coin(BigDecimal.valueOf(result).toBigInteger()); | ||
} | ||
return null; | ||
} | ||
|
||
public synchronized Optional<Coin> getGasPrice() { | ||
Check notice Code scanning / CodeQL Missing Override annotation Note
This method overrides
GasCalculator.getGasPrice Error loading related location Loading |
||
if(cachedGasPrice == null) { | ||
cachedGasPrice = calculateGasPrice(); | ||
} | ||
return cachedGasPrice == null ? Optional.empty() : Optional.of(cachedGasPrice); | ||
} | ||
|
||
static class GasEntry { | ||
protected Coin gasPrice; | ||
protected long gasUsed; | ||
|
||
GasEntry(Coin gasPrice, long gasUsed) { | ||
this.gasPrice = gasPrice; | ||
this.gasUsed = gasUsed; | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
rskj-core/src/main/java/org/ethereum/listener/LegacyGasCalculator.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,54 @@ | ||
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 LegacyGasCalculator implements GasCalculator { | ||
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; | ||
|
||
public synchronized Optional<Coin> getGasPrice() { | ||
Check notice Code scanning / CodeQL Missing Override annotation Note
This method overrides
GasCalculator.getGasPrice Error loading related location Loading |
||
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 void onBlock(Block block, List<TransactionReceipt> receipts) { | ||
onBlock(block.getTransactionsList()); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.