Review and polish migration to mempool.space API

- Polish whitespace and newlines; wrap comments at 90 chars

 - Use package-private vs protected visibility when exposing
   BitcoinFeeRateProvider constants for testing

 - Document that 'maxBlocks' is dead code, but do not remove it yet, as
   it would disrupt the process of getting this fix out quickly because
   it would require operators to change the way they start their
   pricenodes.
This commit is contained in:
Chris Beams 2020-05-05 17:37:02 +02:00
parent e18f0406af
commit 86fcecc5d0
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
2 changed files with 12 additions and 9 deletions

View File

@ -34,7 +34,6 @@ import org.springframework.web.util.UriComponentsBuilder;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
@ -42,14 +41,18 @@ import java.util.stream.Stream;
@Component
class BitcoinFeeRateProvider extends FeeRateProvider {
protected static final long MIN_FEE_RATE = 10; // satoshi/byte
protected static final long MAX_FEE_RATE = 1000;
static final long MIN_FEE_RATE = 10; // satoshi/byte
static final long MAX_FEE_RATE = 1000;
private static final int DEFAULT_MAX_BLOCKS = 2;
private static final int DEFAULT_REFRESH_INTERVAL = 2;
private final RestTemplate restTemplate = new RestTemplate();
// TODO: As of the switch to the mempool.space API this field and related members are
// now dead code and should be removed, including removing the positional
// command-line argument from startup scripts. Operators need to be notified of this
// when it happens.
private final int maxBlocks;
public BitcoinFeeRateProvider(Environment env) {
@ -79,11 +82,12 @@ class BitcoinFeeRateProvider extends FeeRateProvider {
return restTemplate.exchange(
RequestEntity
.get(UriComponentsBuilder
// Temporarily call mempool.space centralized API endpoint
// A more de-centralized solution discussed in https://github.com/bisq-network/projects/issues/27
// Temporarily call mempool.space centralized API endpoint as an
// alternative to the too-expensive bitcoinfees.earn.com until a more
// decentralized solution is available as per
// https://github.com/bisq-network/projects/issues/27
.fromUriString("https://mempool.space/api/v1/fees/recommended")
.build().toUri())
.header("User-Agent", "") // required to avoid 403
.build(),
new ParameterizedTypeReference<Map<String, Long>>() { }
).getBody().entrySet().stream();

View File

@ -29,17 +29,16 @@ public class BitcoinFeeRateProviderTest {
@Test
public void doGet_successfulCall() {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
BitcoinFeeRateProvider feeRateProvider = new BitcoinFeeRateProvider(ctx.getEnvironment());
// Make a call to the API, retrieve the recommended fee rate
// If the API call fails, or the response body cannot be parsed, the test will fail with an exception
// If the API call fails, or the response body cannot be parsed, the test will
// fail with an exception
FeeRate retrievedFeeRate = feeRateProvider.doGet();
// Check that the FeeRateProvider returns a fee within the defined parameters
assertTrue(retrievedFeeRate.getPrice() >= BitcoinFeeRateProvider.MIN_FEE_RATE);
assertTrue(retrievedFeeRate.getPrice() <= BitcoinFeeRateProvider.MAX_FEE_RATE);
}
}