Allow custom mempool instance

This commit is contained in:
Djuri Baars 2024-06-28 17:36:46 +02:00
parent 262eae22dc
commit 24c3b46365
5 changed files with 54 additions and 20 deletions

2
data

@ -1 +1 @@
Subproject commit fd76caa6f4168ca345b6f847f85e5fc9bb6543b2 Subproject commit 52e90dbdee7f53dcca5a9857dd411f98a569263a

View file

@ -2,7 +2,7 @@
char *wsServer; char *wsServer;
esp_websocket_client_handle_t blockNotifyClient = NULL; esp_websocket_client_handle_t blockNotifyClient = NULL;
uint currentBlockHeight = 816000; uint currentBlockHeight = 840000;
uint blockMedianFee = 1; uint blockMedianFee = 1;
bool blockNotifyInit = false; bool blockNotifyInit = false;
unsigned long int lastBlockUpdate; unsigned long int lastBlockUpdate;
@ -59,7 +59,7 @@ void setupBlockNotify()
String mempoolInstance = String mempoolInstance =
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE); preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
while (dnsErr != 1) while (dnsErr != 1 && !strchr(mempoolInstance.c_str(), ':'))
{ {
dnsErr = WiFi.hostByName(mempoolInstance.c_str(), result); dnsErr = WiFi.hostByName(mempoolInstance.c_str(), result);
@ -73,8 +73,10 @@ void setupBlockNotify()
} }
// Get current block height through regular API // Get current block height through regular API
int blockFetch = getBlockFetch();
currentBlockHeight = getBlockFetch(); if (blockFetch > currentBlockHeight)
currentBlockHeight = blockFetch;
if (currentBlockHeight != -1) if (currentBlockHeight != -1)
{ {
@ -95,13 +97,19 @@ void setupBlockNotify()
// std::strcpy(wsServer, String("wss://" + mempoolInstance + // std::strcpy(wsServer, String("wss://" + mempoolInstance +
// "/api/v1/ws").c_str()); // "/api/v1/ws").c_str());
const String protocol = preferences.getBool("mempoolSecure", true) ? "wss" : "ws";
String mempoolUri = protocol + "://" + preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE) + "/api/v1/ws";
esp_websocket_client_config_t config = { esp_websocket_client_config_t config = {
.uri = "wss://mempool.space/api/v1/ws", // .uri = "wss://mempool.space/api/v1/ws",
// .task_stack = (6*1024), // .task_stack = (6*1024),
// .cert_pem = mempoolWsCert, // .cert_pem = mempoolWsCert,
.user_agent = USER_AGENT, .user_agent = USER_AGENT,
}; };
config.uri = mempoolUri.c_str();
blockNotifyClient = esp_websocket_client_init(&config); blockNotifyClient = esp_websocket_client_init(&config);
esp_websocket_register_events(blockNotifyClient, WEBSOCKET_EVENT_ANY, esp_websocket_register_events(blockNotifyClient, WEBSOCKET_EVENT_ANY,
onWebsocketBlockEvent, blockNotifyClient); onWebsocketBlockEvent, blockNotifyClient);
@ -282,21 +290,40 @@ void restartBlockNotify()
int getBlockFetch() int getBlockFetch()
{ {
try {
WiFiClientSecure client;
client.setInsecure();
String mempoolInstance = String mempoolInstance =
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE); preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
// Get current block height through regular API // Get current block height through regular API
HTTPClient *http = new HTTPClient(); HTTPClient http;
http->begin("https://" + mempoolInstance + "/api/blocks/tip/height");
int httpCode = http->GET(); const String protocol = preferences.getBool("mempoolSecure", true) ? "https" : "http";
if (preferences.getBool("mempoolSecure", true))
http.begin(client, protocol + "://" + mempoolInstance + "/api/blocks/tip/height");
else
http.begin(protocol + "://" + mempoolInstance + "/api/blocks/tip/height");
Serial.println("Fetching block height from " + protocol + "://" + mempoolInstance + "/api/blocks/tip/height");
int httpCode = http.GET();
if (httpCode > 0 && httpCode == HTTP_CODE_OK) if (httpCode > 0 && httpCode == HTTP_CODE_OK)
{ {
String blockHeightStr = http->getString(); String blockHeightStr = http.getString();
return blockHeightStr.toInt(); return blockHeightStr.toInt();
} else {
Serial.println("HTTP code" + String(httpCode));
return 0;
}
}
catch (...) {
Serial.println(F("An exception occured while trying to get the latest block"));
} }
return -1; return 2203; // B-T-C
} }
uint getLastBlockUpdate() uint getLastBlockUpdate()

View file

@ -46,6 +46,10 @@ void setup()
{ {
delay(1000); delay(1000);
} }
} else if (mcp1.digitalRead(1) == LOW) {
preferences.clear();
queueLedEffect(LED_EFFECT_WIFI_ERASE_SETTINGS);
ESP.restart();
} }
} }
@ -60,7 +64,7 @@ void setup()
setupTasks(); setupTasks();
setupTimers(); setupTimers();
xTaskCreate(setupWebsocketClients, "setupWebsocketClients", 4096, NULL, xTaskCreate(setupWebsocketClients, "setupWebsocketClients", 8192, NULL,
tskIDLE_PRIORITY, NULL); tskIDLE_PRIORITY, NULL);
setupButtonTask(); setupButtonTask();

View file

@ -27,8 +27,10 @@
#define NTP_SERVER "pool.ntp.org" #define NTP_SERVER "pool.ntp.org"
#define DEFAULT_MEMPOOL_INSTANCE "mempool.space" #define DEFAULT_MEMPOOL_INSTANCE "mempool.space"
#define TIME_OFFSET_SECONDS 3600 #define TIME_OFFSET_SECONDS 3600
#define USER_AGENT "BTClock/2.0" #define USER_AGENT "BTClock/3.0"
#ifndef MCP_DEV_ADDR
#define MCP_DEV_ADDR 0x20 #define MCP_DEV_ADDR 0x20
#endif
#define DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE 30 #define DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE 30
#define DEFAULT_MINUTES_FULL_REFRESH 60 #define DEFAULT_MINUTES_FULL_REFRESH 60

View file

@ -460,7 +460,7 @@ void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json)
String boolSettings[] = {"fetchEurPrice", "ledTestOnPower", "ledFlashOnUpd", String boolSettings[] = {"fetchEurPrice", "ledTestOnPower", "ledFlashOnUpd",
"mdnsEnabled", "otaEnabled", "stealFocus", "mdnsEnabled", "otaEnabled", "stealFocus",
"mcapBigChar", "useSatsSymbol", "useBlkCountdown", "mcapBigChar", "useSatsSymbol", "useBlkCountdown",
"suffixPrice", "disableLeds", "ownDataSource", "flAlwaysOn", "flFlashOnUpd"}; "suffixPrice", "disableLeds", "ownDataSource", "flAlwaysOn", "flFlashOnUpd", "mempoolSecure"};
for (String setting : boolSettings) for (String setting : boolSettings)
{ {
@ -557,9 +557,10 @@ void onApiSettingsGet(AsyncWebServerRequest *request)
preferences.getUInt("fullRefreshMin", DEFAULT_MINUTES_FULL_REFRESH); preferences.getUInt("fullRefreshMin", DEFAULT_MINUTES_FULL_REFRESH);
root["wpTimeout"] = preferences.getUInt("wpTimeout", 600); root["wpTimeout"] = preferences.getUInt("wpTimeout", 600);
root["tzOffset"] = preferences.getInt("gmtOffset", TIME_OFFSET_SECONDS) / 60; root["tzOffset"] = preferences.getInt("gmtOffset", TIME_OFFSET_SECONDS) / 60;
root["useBitcoinNode"] = preferences.getBool("useNode", false); // root["useBitcoinNode"] = preferences.getBool("useNode", false);
root["mempoolInstance"] = root["mempoolInstance"] =
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE); preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
root["mempoolSecure"] = preferences.getBool("mempoolSecure", true);
root["ledTestOnPower"] = preferences.getBool("ledTestOnPower", true); root["ledTestOnPower"] = preferences.getBool("ledTestOnPower", true);
root["ledFlashOnUpd"] = preferences.getBool("ledFlashOnUpd", false); root["ledFlashOnUpd"] = preferences.getBool("ledFlashOnUpd", false);
root["ledBrightness"] = preferences.getUInt("ledBrightness", 128); root["ledBrightness"] = preferences.getUInt("ledBrightness", 128);