forked from btclock/btclock_v3
Refactor BlockNotify to a class, use websocketsClient
This commit is contained in:
parent
ebbec75e6b
commit
e330984ba2
9 changed files with 239 additions and 257 deletions
|
@ -1,186 +1,146 @@
|
|||
#include "block_notify.hpp"
|
||||
#include "led_handler.hpp"
|
||||
|
||||
char *wsServer;
|
||||
esp_websocket_client_handle_t blockNotifyClient = NULL;
|
||||
uint32_t currentBlockHeight = 873400;
|
||||
uint16_t blockMedianFee = 1;
|
||||
bool blockNotifyInit = false;
|
||||
unsigned long int lastBlockUpdate;
|
||||
// Initialize static members
|
||||
WebSocketsClient BlockNotify::webSocket;
|
||||
uint32_t BlockNotify::currentBlockHeight = 878000;
|
||||
uint16_t BlockNotify::blockMedianFee = 1;
|
||||
bool BlockNotify::notifyInit = false;
|
||||
unsigned long int BlockNotify::lastBlockUpdate = 0;
|
||||
TaskHandle_t BlockNotify::taskHandle = nullptr;
|
||||
|
||||
const char *mempoolWsCert = R"EOF(
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB
|
||||
iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl
|
||||
cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV
|
||||
BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw
|
||||
MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV
|
||||
BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU
|
||||
aGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNVBAMTJVVTRVJUcnVzdCBSU0EgQ2Vy
|
||||
dGlmaWNhdGlvbiBBdXRob3JpdHkwggI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X/JJpuXior7gtNn3/3
|
||||
ATiUFJVDBwn7YKnuHKsSjKCaXqeYalltiz8I+8jRRa8YFWSQEg9zKC7F4iRO/Fjs
|
||||
8PRF/iKz6y+O0tlFYQXBl2+odnKPi4w2r78NBc5xjeambx9spnFixdjQg3IM8WcR
|
||||
iQycE0xyNN+81XHfqnHd4blsjDwSXWXavVcStkNr/+XeTWYRUc+ZruwXtuhxkYze
|
||||
Sf7dNXGiFSeUHM9h4ya7b6NnJSFd5t0dCy5oGzuCr+yDZ4XUmFF0sbmZgIn/f3gZ
|
||||
XHlKYC6SQK5MNyosycdiyA5d9zZbyuAlJQG03RoHnHcAP9Dc1ew91Pq7P8yF1m9/
|
||||
qS3fuQL39ZeatTXaw2ewh0qpKJ4jjv9cJ2vhsE/zB+4ALtRZh8tSQZXq9EfX7mRB
|
||||
VXyNWQKV3WKdwrnuWih0hKWbt5DHDAff9Yk2dDLWKMGwsAvgnEzDHNb842m1R0aB
|
||||
L6KCq9NjRHDEjf8tM7qtj3u1cIiuPhnPQCjY/MiQu12ZIvVS5ljFH4gxQ+6IHdfG
|
||||
jjxDah2nGN59PRbxYvnKkKj9
|
||||
-----END CERTIFICATE-----
|
||||
)EOF";
|
||||
|
||||
void setupBlockNotify()
|
||||
void BlockNotify::taskNotify(void* pvParameters)
|
||||
{
|
||||
IPAddress result;
|
||||
|
||||
int dnsErr = -1;
|
||||
String mempoolInstance =
|
||||
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
|
||||
|
||||
while (dnsErr != 1 && !strchr(mempoolInstance.c_str(), ':'))
|
||||
{
|
||||
dnsErr = WiFi.hostByName(mempoolInstance.c_str(), result);
|
||||
|
||||
if (dnsErr != 1)
|
||||
for (;;)
|
||||
{
|
||||
Serial.print(mempoolInstance);
|
||||
Serial.println(F("mempool DNS could not be resolved"));
|
||||
WiFi.reconnect();
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
webSocket.loop();
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
// Get current block height through regular API
|
||||
int blockFetch = getBlockFetch();
|
||||
|
||||
if (blockFetch > currentBlockHeight)
|
||||
currentBlockHeight = blockFetch;
|
||||
|
||||
if (currentBlockHeight != -1)
|
||||
{
|
||||
lastBlockUpdate = esp_timer_get_time() / 1000000;
|
||||
}
|
||||
|
||||
if (workQueue != nullptr)
|
||||
{
|
||||
WorkItem blockUpdate = {TASK_BLOCK_UPDATE, 0};
|
||||
xQueueSend(workQueue, &blockUpdate, portMAX_DELAY);
|
||||
}
|
||||
|
||||
// std::strcpy(wsServer, String("wss://" + mempoolInstance +
|
||||
// "/api/v1/ws").c_str());
|
||||
|
||||
const String protocol = preferences.getBool("mempoolSecure", DEFAULT_MEMPOOL_SECURE) ? "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",
|
||||
.task_stack = (6*1024),
|
||||
.user_agent = USER_AGENT
|
||||
};
|
||||
|
||||
if (preferences.getBool("mempoolSecure", DEFAULT_MEMPOOL_SECURE)) {
|
||||
config.cert_pem = mempoolWsCert;
|
||||
}
|
||||
|
||||
config.uri = mempoolUri.c_str();
|
||||
|
||||
Serial.printf("Connecting to %s\r\n", preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE));
|
||||
|
||||
blockNotifyClient = esp_websocket_client_init(&config);
|
||||
esp_websocket_register_events(blockNotifyClient, WEBSOCKET_EVENT_ANY,
|
||||
onWebsocketBlockEvent, blockNotifyClient);
|
||||
esp_websocket_client_start(blockNotifyClient);
|
||||
}
|
||||
|
||||
void onWebsocketBlockEvent(void *handler_args, esp_event_base_t base,
|
||||
int32_t event_id, void *event_data)
|
||||
void BlockNotify::setupTask()
|
||||
{
|
||||
esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
|
||||
const String sub = "{\"action\": \"want\", \"data\":[\"blocks\", \"mempool-blocks\"]}";
|
||||
switch (event_id)
|
||||
{
|
||||
case WEBSOCKET_EVENT_CONNECTED:
|
||||
blockNotifyInit = true;
|
||||
xTaskCreate(taskNotify, "blockNotify", (6 * 1024), NULL, tskIDLE_PRIORITY,
|
||||
&taskHandle);
|
||||
}
|
||||
|
||||
Serial.println(F("Connected to Mempool.space WebSocket"));
|
||||
void BlockNotify::onWebsocketEvent(WStype_t type, uint8_t* payload, size_t length) {
|
||||
switch(type) {
|
||||
case WStype_DISCONNECTED: {
|
||||
Serial.println(F("Mempool.space WS Connection Closed"));
|
||||
break;
|
||||
}
|
||||
case WStype_CONNECTED: {
|
||||
notifyInit = true;
|
||||
Serial.print(F("Connected to "));
|
||||
Serial.println(preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE));
|
||||
|
||||
Serial.println(sub);
|
||||
if (esp_websocket_client_send_text(blockNotifyClient, sub.c_str(),
|
||||
sub.length(), portMAX_DELAY) == -1)
|
||||
JsonDocument doc;
|
||||
doc["action"] = "want";
|
||||
JsonArray data = doc.createNestedArray("data");
|
||||
data.add("blocks");
|
||||
data.add("mempool-blocks");
|
||||
|
||||
String sub;
|
||||
serializeJson(doc, sub);
|
||||
Serial.println(sub);
|
||||
webSocket.sendTXT(sub.c_str());
|
||||
break;
|
||||
}
|
||||
case WStype_TEXT: {
|
||||
JsonDocument doc;
|
||||
JsonDocument filter;
|
||||
filter["block"]["height"] = true;
|
||||
filter["mempool-blocks"][0]["medianFee"] = true;
|
||||
|
||||
deserializeJson(doc, (char*)payload, DeserializationOption::Filter(filter));
|
||||
|
||||
if (debugLogEnabled()) {
|
||||
Serial.println(doc.as<String>());
|
||||
}
|
||||
|
||||
if (doc["block"].is<JsonObject>())
|
||||
{
|
||||
JsonObject block = doc["block"];
|
||||
if (block["height"].as<uint>() != currentBlockHeight) {
|
||||
BlockNotify::getInstance().processNewBlock(block["height"].as<uint>());
|
||||
}
|
||||
}
|
||||
else if (doc["mempool-blocks"].is<JsonArray>())
|
||||
{
|
||||
JsonArray blockInfo = doc["mempool-blocks"].as<JsonArray>();
|
||||
uint medianFee = (uint)round(blockInfo[0]["medianFee"].as<double>());
|
||||
BlockNotify::getInstance().processNewBlockFee(medianFee);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WStype_BIN:
|
||||
case WStype_ERROR:
|
||||
case WStype_FRAGMENT_TEXT_START:
|
||||
case WStype_FRAGMENT_BIN_START:
|
||||
case WStype_FRAGMENT:
|
||||
case WStype_PING:
|
||||
case WStype_PONG:
|
||||
case WStype_FRAGMENT_FIN: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BlockNotify::setup()
|
||||
{
|
||||
IPAddress result;
|
||||
int dnsErr = -1;
|
||||
String mempoolInstance = preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
|
||||
|
||||
while (dnsErr != 1 && !strchr(mempoolInstance.c_str(), ':'))
|
||||
{
|
||||
Serial.println(F("Mempool.space WS Block Subscribe Error"));
|
||||
dnsErr = WiFi.hostByName(mempoolInstance.c_str(), result);
|
||||
|
||||
if (dnsErr != 1)
|
||||
{
|
||||
Serial.print(mempoolInstance);
|
||||
Serial.println(F("mempool DNS could not be resolved"));
|
||||
WiFi.reconnect();
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case WEBSOCKET_EVENT_DATA:
|
||||
onWebsocketBlockMessage(data);
|
||||
break;
|
||||
case WEBSOCKET_EVENT_ERROR:
|
||||
Serial.println(F("Mempool.space WS Connnection error"));
|
||||
break;
|
||||
case WEBSOCKET_EVENT_DISCONNECTED:
|
||||
Serial.println(F("Mempool.space WS Connnection Closed"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Get current block height through regular API
|
||||
int blockFetch = fetchLatestBlock();
|
||||
|
||||
void onWebsocketBlockMessage(esp_websocket_event_data_t *event_data)
|
||||
{
|
||||
JsonDocument doc;
|
||||
if (blockFetch > currentBlockHeight)
|
||||
currentBlockHeight = blockFetch;
|
||||
|
||||
JsonDocument filter;
|
||||
filter["block"]["height"] = true;
|
||||
filter["mempool-blocks"][0]["medianFee"] = true;
|
||||
|
||||
deserializeJson(doc, (char *)event_data->data_ptr, DeserializationOption::Filter(filter));
|
||||
|
||||
// if (error) {
|
||||
// Serial.print("deserializeJson() failed: ");
|
||||
// Serial.println(error.c_str());
|
||||
// return;
|
||||
// }
|
||||
|
||||
if (doc["block"].is<JsonObject>())
|
||||
{
|
||||
JsonObject block = doc["block"];
|
||||
|
||||
if (block["height"].as<uint>() == currentBlockHeight) {
|
||||
return;
|
||||
if (currentBlockHeight != -1)
|
||||
{
|
||||
lastBlockUpdate = esp_timer_get_time() / 1000000;
|
||||
}
|
||||
|
||||
processNewBlock(block["height"].as<uint>());
|
||||
}
|
||||
else if (doc["mempool-blocks"].is<JsonArray>())
|
||||
{
|
||||
JsonArray blockInfo = doc["mempool-blocks"].as<JsonArray>();
|
||||
if (workQueue != nullptr)
|
||||
{
|
||||
WorkItem blockUpdate = {TASK_BLOCK_UPDATE, 0};
|
||||
xQueueSend(workQueue, &blockUpdate, portMAX_DELAY);
|
||||
}
|
||||
|
||||
uint medianFee = (uint)round(blockInfo[0]["medianFee"].as<double>());
|
||||
const bool useSSL = preferences.getBool("mempoolSecure", DEFAULT_MEMPOOL_SECURE);
|
||||
const int port = useSSL ? 443 : 80;
|
||||
|
||||
processNewBlockFee(medianFee);
|
||||
}
|
||||
if (useSSL) {
|
||||
webSocket.beginSSL(mempoolInstance.c_str(), port, "/api/v1/ws");
|
||||
// webSocket.beginSSL("ws.btclock.dev", port, "/api/v1/ws");
|
||||
|
||||
doc.clear();
|
||||
} else {
|
||||
webSocket.begin(mempoolInstance.c_str(), port, "/api/v1/ws");
|
||||
}
|
||||
|
||||
webSocket.onEvent(onWebsocketEvent);
|
||||
webSocket.setReconnectInterval(5000);
|
||||
webSocket.enableHeartbeat(15000, 3000, 2);
|
||||
|
||||
setupTask();
|
||||
}
|
||||
|
||||
void processNewBlock(uint32_t newBlockHeight) {
|
||||
void BlockNotify::processNewBlock(uint32_t newBlockHeight) {
|
||||
if (newBlockHeight <= currentBlockHeight)
|
||||
{
|
||||
return;
|
||||
|
@ -220,76 +180,65 @@ void processNewBlock(uint32_t newBlockHeight) {
|
|||
}
|
||||
}
|
||||
|
||||
void processNewBlockFee(uint16_t newBlockFee) {
|
||||
if (blockMedianFee == newBlockFee)
|
||||
void BlockNotify::processNewBlockFee(uint16_t newBlockFee) {
|
||||
if (blockMedianFee == newBlockFee)
|
||||
{
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
// Serial.printf("New median fee: %d\r\n", medianFee);
|
||||
blockMedianFee = newBlockFee;
|
||||
|
||||
if (workQueue != nullptr)
|
||||
{
|
||||
WorkItem blockUpdate = {TASK_FEE_UPDATE, 0};
|
||||
xQueueSend(workQueue, &blockUpdate, portMAX_DELAY);
|
||||
WorkItem blockUpdate = {TASK_FEE_UPDATE, 0};
|
||||
xQueueSend(workQueue, &blockUpdate, portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t getBlockHeight() { return currentBlockHeight; }
|
||||
|
||||
void setBlockHeight(uint32_t newBlockHeight)
|
||||
{
|
||||
currentBlockHeight = newBlockHeight;
|
||||
uint32_t BlockNotify::getBlockHeight() const {
|
||||
return currentBlockHeight;
|
||||
}
|
||||
|
||||
uint16_t getBlockMedianFee() { return blockMedianFee; }
|
||||
|
||||
void setBlockMedianFee(uint16_t newBlockMedianFee)
|
||||
void BlockNotify::setBlockHeight(uint32_t newBlockHeight)
|
||||
{
|
||||
blockMedianFee = newBlockMedianFee;
|
||||
currentBlockHeight = newBlockHeight;
|
||||
}
|
||||
|
||||
bool isBlockNotifyConnected()
|
||||
{
|
||||
if (blockNotifyClient == NULL)
|
||||
return false;
|
||||
return esp_websocket_client_is_connected(blockNotifyClient);
|
||||
uint16_t BlockNotify::getBlockMedianFee() const {
|
||||
return blockMedianFee;
|
||||
}
|
||||
|
||||
bool getBlockNotifyInit()
|
||||
void BlockNotify::setBlockMedianFee(uint16_t newBlockMedianFee)
|
||||
{
|
||||
return blockNotifyInit;
|
||||
blockMedianFee = newBlockMedianFee;
|
||||
}
|
||||
|
||||
void stopBlockNotify()
|
||||
bool BlockNotify::isConnected() const
|
||||
{
|
||||
if (blockNotifyClient == NULL)
|
||||
return;
|
||||
|
||||
esp_websocket_client_close(blockNotifyClient, pdMS_TO_TICKS(5000));
|
||||
esp_websocket_client_stop(blockNotifyClient);
|
||||
esp_websocket_client_destroy(blockNotifyClient);
|
||||
|
||||
blockNotifyClient = NULL;
|
||||
return webSocket.isConnected();
|
||||
}
|
||||
|
||||
void restartBlockNotify()
|
||||
bool BlockNotify::isInitialized() const
|
||||
{
|
||||
stopBlockNotify();
|
||||
|
||||
if (blockNotifyClient == NULL) {
|
||||
setupBlockNotify();
|
||||
return;
|
||||
}
|
||||
|
||||
// esp_websocket_client_close(blockNotifyClient, pdMS_TO_TICKS(5000));
|
||||
// esp_websocket_client_stop(blockNotifyClient);
|
||||
// esp_websocket_client_start(blockNotifyClient);
|
||||
return notifyInit;
|
||||
}
|
||||
|
||||
void BlockNotify::stop()
|
||||
{
|
||||
webSocket.disconnect();
|
||||
if (taskHandle != NULL) {
|
||||
vTaskDelete(taskHandle);
|
||||
taskHandle = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int getBlockFetch() {
|
||||
void BlockNotify::restart()
|
||||
{
|
||||
stop();
|
||||
setup();
|
||||
}
|
||||
|
||||
int BlockNotify::fetchLatestBlock() {
|
||||
try {
|
||||
String mempoolInstance = preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
|
||||
const String protocol = preferences.getBool("mempoolSecure", DEFAULT_MEMPOOL_SECURE) ? "https" : "http";
|
||||
|
@ -312,12 +261,12 @@ int getBlockFetch() {
|
|||
return 2203; // B-T-C
|
||||
}
|
||||
|
||||
uint getLastBlockUpdate()
|
||||
uint BlockNotify::getLastBlockUpdate() const
|
||||
{
|
||||
return lastBlockUpdate;
|
||||
return lastBlockUpdate;
|
||||
}
|
||||
|
||||
void setLastBlockUpdate(uint lastUpdate)
|
||||
void BlockNotify::setLastBlockUpdate(uint lastUpdate)
|
||||
{
|
||||
lastBlockUpdate = lastUpdate;
|
||||
lastBlockUpdate = lastUpdate;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue