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;
esp_websocket_client_handle_t blockNotifyClient = NULL;
uint currentBlockHeight = 816000;
uint currentBlockHeight = 840000;
uint blockMedianFee = 1;
bool blockNotifyInit = false;
unsigned long int lastBlockUpdate;
@ -59,7 +59,7 @@ void setupBlockNotify()
String mempoolInstance =
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
while (dnsErr != 1)
while (dnsErr != 1 && !strchr(mempoolInstance.c_str(), ':'))
{
dnsErr = WiFi.hostByName(mempoolInstance.c_str(), result);
@ -73,8 +73,10 @@ void setupBlockNotify()
}
// Get current block height through regular API
int blockFetch = getBlockFetch();
currentBlockHeight = getBlockFetch();
if (blockFetch > currentBlockHeight)
currentBlockHeight = blockFetch;
if (currentBlockHeight != -1)
{
@ -95,13 +97,19 @@ void setupBlockNotify()
// std::strcpy(wsServer, String("wss://" + mempoolInstance +
// "/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 = {
.uri = "wss://mempool.space/api/v1/ws",
// .uri = "wss://mempool.space/api/v1/ws",
// .task_stack = (6*1024),
// .cert_pem = mempoolWsCert,
.user_agent = USER_AGENT,
};
config.uri = mempoolUri.c_str();
blockNotifyClient = esp_websocket_client_init(&config);
esp_websocket_register_events(blockNotifyClient, WEBSOCKET_EVENT_ANY,
onWebsocketBlockEvent, blockNotifyClient);
@ -282,21 +290,40 @@ void restartBlockNotify()
int getBlockFetch()
{
String mempoolInstance =
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
try {
WiFiClientSecure client;
client.setInsecure();
// Get current block height through regular API
HTTPClient *http = new HTTPClient();
http->begin("https://" + mempoolInstance + "/api/blocks/tip/height");
int httpCode = http->GET();
String mempoolInstance =
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
if (httpCode > 0 && httpCode == HTTP_CODE_OK)
{
String blockHeightStr = http->getString();
return blockHeightStr.toInt();
// Get current block height through regular API
HTTPClient http;
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)
{
String blockHeightStr = http.getString();
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()

View file

@ -46,6 +46,10 @@ void setup()
{
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();
setupTimers();
xTaskCreate(setupWebsocketClients, "setupWebsocketClients", 4096, NULL,
xTaskCreate(setupWebsocketClients, "setupWebsocketClients", 8192, NULL,
tskIDLE_PRIORITY, NULL);
setupButtonTask();

View file

@ -27,8 +27,10 @@
#define NTP_SERVER "pool.ntp.org"
#define DEFAULT_MEMPOOL_INSTANCE "mempool.space"
#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
#endif
#define DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE 30
#define DEFAULT_MINUTES_FULL_REFRESH 60

View file

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