mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-24 12:46:45 +00:00
get and store fee rate histogram
This commit is contained in:
parent
80b7ee803b
commit
43bf6ab265
7 changed files with 64 additions and 5 deletions
|
@ -143,6 +143,8 @@ public class AppController implements Initializable {
|
||||||
|
|
||||||
private static Map<Integer, Double> targetBlockFeeRates;
|
private static Map<Integer, Double> targetBlockFeeRates;
|
||||||
|
|
||||||
|
private static Map<Long, Long> feeRateHistogram;
|
||||||
|
|
||||||
private static Double minimumRelayFeeRate;
|
private static Double minimumRelayFeeRate;
|
||||||
|
|
||||||
private static CurrencyRate fiatCurrencyExchangeRate;
|
private static CurrencyRate fiatCurrencyExchangeRate;
|
||||||
|
@ -650,6 +652,10 @@ public class AppController implements Initializable {
|
||||||
return targetBlockFeeRates;
|
return targetBlockFeeRates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map<Long, Long> getFeeRateHistogram() {
|
||||||
|
return feeRateHistogram;
|
||||||
|
}
|
||||||
|
|
||||||
public static Double getMinimumRelayFeeRate() {
|
public static Double getMinimumRelayFeeRate() {
|
||||||
return minimumRelayFeeRate == null ? Transaction.DEFAULT_MIN_RELAY_FEE : minimumRelayFeeRate;
|
return minimumRelayFeeRate == null ? Transaction.DEFAULT_MIN_RELAY_FEE : minimumRelayFeeRate;
|
||||||
}
|
}
|
||||||
|
@ -1429,6 +1435,7 @@ public class AppController implements Initializable {
|
||||||
public void newConnection(ConnectionEvent event) {
|
public void newConnection(ConnectionEvent event) {
|
||||||
currentBlockHeight = event.getBlockHeight();
|
currentBlockHeight = event.getBlockHeight();
|
||||||
targetBlockFeeRates = event.getTargetBlockFeeRates();
|
targetBlockFeeRates = event.getTargetBlockFeeRates();
|
||||||
|
feeRateHistogram = event.getFeeRateHistogram();
|
||||||
minimumRelayFeeRate = event.getMinimumRelayFeeRate();
|
minimumRelayFeeRate = event.getMinimumRelayFeeRate();
|
||||||
String banner = event.getServerBanner();
|
String banner = event.getServerBanner();
|
||||||
String status = "Connected to " + Config.get().getElectrumServer() + " at height " + event.getBlockHeight();
|
String status = "Connected to " + Config.get().getElectrumServer() + " at height " + event.getBlockHeight();
|
||||||
|
@ -1453,6 +1460,7 @@ public class AppController implements Initializable {
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void feesUpdated(FeeRatesUpdatedEvent event) {
|
public void feesUpdated(FeeRatesUpdatedEvent event) {
|
||||||
targetBlockFeeRates = event.getTargetBlockFeeRates();
|
targetBlockFeeRates = event.getTargetBlockFeeRates();
|
||||||
|
feeRateHistogram = event.getFeeRateHistogram();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
|
|
|
@ -12,8 +12,8 @@ public class ConnectionEvent extends FeeRatesUpdatedEvent {
|
||||||
private final BlockHeader blockHeader;
|
private final BlockHeader blockHeader;
|
||||||
private final Double minimumRelayFeeRate;
|
private final Double minimumRelayFeeRate;
|
||||||
|
|
||||||
public ConnectionEvent(List<String> serverVersion, String serverBanner, int blockHeight, BlockHeader blockHeader, Map<Integer, Double> targetBlockFeeRates, Double minimumRelayFeeRate) {
|
public ConnectionEvent(List<String> serverVersion, String serverBanner, int blockHeight, BlockHeader blockHeader, Map<Integer, Double> targetBlockFeeRates, Map<Long, Long> feeRateHistogram, Double minimumRelayFeeRate) {
|
||||||
super(targetBlockFeeRates);
|
super(targetBlockFeeRates, feeRateHistogram);
|
||||||
this.serverVersion = serverVersion;
|
this.serverVersion = serverVersion;
|
||||||
this.serverBanner = serverBanner;
|
this.serverBanner = serverBanner;
|
||||||
this.blockHeight = blockHeight;
|
this.blockHeight = blockHeight;
|
||||||
|
|
|
@ -4,12 +4,18 @@ import java.util.Map;
|
||||||
|
|
||||||
public class FeeRatesUpdatedEvent {
|
public class FeeRatesUpdatedEvent {
|
||||||
private final Map<Integer, Double> targetBlockFeeRates;
|
private final Map<Integer, Double> targetBlockFeeRates;
|
||||||
|
private final Map<Long, Long> feeRateHistogram;
|
||||||
|
|
||||||
public FeeRatesUpdatedEvent(Map<Integer, Double> targetBlockFeeRates) {
|
public FeeRatesUpdatedEvent(Map<Integer, Double> targetBlockFeeRates, Map<Long, Long> feeRateHistogram) {
|
||||||
this.targetBlockFeeRates = targetBlockFeeRates;
|
this.targetBlockFeeRates = targetBlockFeeRates;
|
||||||
|
this.feeRateHistogram = feeRateHistogram;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<Integer, Double> getTargetBlockFeeRates() {
|
public Map<Integer, Double> getTargetBlockFeeRates() {
|
||||||
return targetBlockFeeRates;
|
return targetBlockFeeRates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<Long, Long> getFeeRateHistogram() {
|
||||||
|
return feeRateHistogram;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.TreeMap;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
public class BatchedElectrumServerRpc implements ElectrumServerRpc {
|
public class BatchedElectrumServerRpc implements ElectrumServerRpc {
|
||||||
|
@ -229,6 +230,24 @@ public class BatchedElectrumServerRpc implements ElectrumServerRpc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Long, Long> getFeeRateHistogram(Transport transport) {
|
||||||
|
try {
|
||||||
|
JsonRpcClient client = new JsonRpcClient(transport);
|
||||||
|
Long[][] feesArray = new RetryLogic<Long[][]>(MAX_RETRIES, RETRY_DELAY, IllegalStateException.class).getResult(() ->
|
||||||
|
client.createRequest().returnAs(Long[][].class).method("mempool.get_fee_histogram").id(idCounter.incrementAndGet()).execute());
|
||||||
|
|
||||||
|
Map<Long, Long> feeRateHistogram = new TreeMap<>();
|
||||||
|
for(Long[] feePair : feesArray) {
|
||||||
|
feeRateHistogram.put(feePair[0], feePair[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return feeRateHistogram;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new ElectrumServerRpcException("Error getting fee rate histogram", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Double getMinimumRelayFee(Transport transport) {
|
public Double getMinimumRelayFee(Transport transport) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -610,6 +610,10 @@ public class ElectrumServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<Long, Long> getFeeRateHistogram() throws ServerException {
|
||||||
|
return electrumServerRpc.getFeeRateHistogram(getTransport());
|
||||||
|
}
|
||||||
|
|
||||||
public Double getMinimumRelayFee() throws ServerException {
|
public Double getMinimumRelayFee() throws ServerException {
|
||||||
Double minFeeRateBtcKb = electrumServerRpc.getMinimumRelayFee(getTransport());
|
Double minFeeRateBtcKb = electrumServerRpc.getMinimumRelayFee(getTransport());
|
||||||
if(minFeeRateBtcKb != null) {
|
if(minFeeRateBtcKb != null) {
|
||||||
|
@ -760,6 +764,7 @@ public class ElectrumServer {
|
||||||
String banner = electrumServer.getServerBanner();
|
String banner = electrumServer.getServerBanner();
|
||||||
|
|
||||||
Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(SendController.TARGET_BLOCKS_RANGE);
|
Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(SendController.TARGET_BLOCKS_RANGE);
|
||||||
|
Map<Long, Long> feeRateHistogram = electrumServer.getFeeRateHistogram();
|
||||||
feeRatesRetrievedAt = System.currentTimeMillis();
|
feeRatesRetrievedAt = System.currentTimeMillis();
|
||||||
|
|
||||||
Double minimumRelayFeeRate = electrumServer.getMinimumRelayFee();
|
Double minimumRelayFeeRate = electrumServer.getMinimumRelayFee();
|
||||||
|
@ -767,7 +772,7 @@ public class ElectrumServer {
|
||||||
blockTargetFeeRates.computeIfPresent(blockTarget, (blocks, feeRate) -> feeRate < minimumRelayFeeRate ? minimumRelayFeeRate : feeRate);
|
blockTargetFeeRates.computeIfPresent(blockTarget, (blocks, feeRate) -> feeRate < minimumRelayFeeRate ? minimumRelayFeeRate : feeRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ConnectionEvent(serverVersion, banner, tip.height, tip.getBlockHeader(), blockTargetFeeRates, minimumRelayFeeRate);
|
return new ConnectionEvent(serverVersion, banner, tip.height, tip.getBlockHeader(), blockTargetFeeRates, feeRateHistogram, minimumRelayFeeRate);
|
||||||
} else {
|
} else {
|
||||||
if(reader.isAlive()) {
|
if(reader.isAlive()) {
|
||||||
electrumServer.ping();
|
electrumServer.ping();
|
||||||
|
@ -775,8 +780,9 @@ public class ElectrumServer {
|
||||||
long elapsed = System.currentTimeMillis() - feeRatesRetrievedAt;
|
long elapsed = System.currentTimeMillis() - feeRatesRetrievedAt;
|
||||||
if(elapsed > FEE_RATES_PERIOD) {
|
if(elapsed > FEE_RATES_PERIOD) {
|
||||||
Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(SendController.TARGET_BLOCKS_RANGE);
|
Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(SendController.TARGET_BLOCKS_RANGE);
|
||||||
|
Map<Long, Long> feeRateHistogram = electrumServer.getFeeRateHistogram();
|
||||||
feeRatesRetrievedAt = System.currentTimeMillis();
|
feeRatesRetrievedAt = System.currentTimeMillis();
|
||||||
return new FeeRatesUpdatedEvent(blockTargetFeeRates);
|
return new FeeRatesUpdatedEvent(blockTargetFeeRates, feeRateHistogram);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resetConnection();
|
resetConnection();
|
||||||
|
|
|
@ -30,6 +30,8 @@ public interface ElectrumServerRpc {
|
||||||
|
|
||||||
Map<Integer, Double> getFeeEstimates(Transport transport, List<Integer> targetBlocks);
|
Map<Integer, Double> getFeeEstimates(Transport transport, List<Integer> targetBlocks);
|
||||||
|
|
||||||
|
Map<Long, Long> getFeeRateHistogram(Transport transport);
|
||||||
|
|
||||||
Double getMinimumRelayFee(Transport transport);
|
Double getMinimumRelayFee(Transport transport);
|
||||||
|
|
||||||
String broadcastTransaction(Transport transport, String txHex);
|
String broadcastTransaction(Transport transport, String txHex);
|
||||||
|
|
|
@ -246,6 +246,24 @@ public class SimpleElectrumServerRpc implements ElectrumServerRpc {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Long, Long> getFeeRateHistogram(Transport transport) {
|
||||||
|
try {
|
||||||
|
JsonRpcClient client = new JsonRpcClient(transport);
|
||||||
|
Long[][] feesArray = new RetryLogic<Long[][]>(MAX_RETRIES, RETRY_DELAY, IllegalStateException.class).getResult(() ->
|
||||||
|
client.createRequest().returnAs(Long[][].class).method("mempool.get_fee_histogram").id(idCounter.incrementAndGet()).execute());
|
||||||
|
|
||||||
|
Map<Long, Long> feeRateHistogram = new TreeMap<>();
|
||||||
|
for(Long[] feePair : feesArray) {
|
||||||
|
feeRateHistogram.put(feePair[0], feePair[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return feeRateHistogram;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new ElectrumServerRpcException("Error getting fee rate histogram", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Double getMinimumRelayFee(Transport transport) {
|
public Double getMinimumRelayFee(Transport transport) {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue