Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ nul

# Local config (sensitive)
config.json

# Local statistics regression tests
.test-output/
.test-cache/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,7 @@ GPL v3 License - see [LICENSE](LICENSE) file for details.

If you find a block, consider donating to support development:
`bc1qkg83n8lek6cwk4mpad9hrvvun7q0u7nlafws9p`

## Opt-in CYD ST7789 advanced statistics and SMTP

A separate profile adds local display/browser statistics, settings, resistive touch and direct SMTP reports for the dual-USB ST7789 board. See [hardware, build, tests and known limitations](docs/CYD_ADVANCED.md). This profile does not change the default build environment.
65 changes: 65 additions & 0 deletions docs/CYD_ADVANCED.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# CYD ST7789: local advanced statistics and direct SMTP

This opt-in profile extends SparkMiner with two display pages of local mining counters, a browser dashboard, local settings, resistive touch, and SMTP reports sent by the ESP32 itself. No permanently running computer, relay or monitoring service is required.

## Hardware and build

Validated on one ESP32-2432S028 dual-USB (USB-C and Micro-USB) board with ST7789, classic ESP32-D0WD-V3 revision 3.1, 4MB flash and 240MHz CPU. Board labels alone do not identify the display controller. ILI9341, S3 and other pin layouts need their own configuration and are not validated by this contribution.

```
python -m pip install platformio==6.2.0
python -m platformio run --project-conf platformio.mail-check.ini --environment cyd-mail-check
```

The separate configuration pins the platform, framework, compiler and resolved library versions. Default upstream environments remain unchanged. The custom partition table has app0 at 0x10000 and app1 at 0x1f0000, each 0x1e0000 bytes; NVS starts at 0x9000. Back up each device independently before changing its firmware or partition table. The tested app image is 1,415,008 bytes, SHA256 `6cf3e968e5f54dd607e5921036c4e181b2c23383948be9fa278b4d39a85035c6`. This identifies the tested local build, not a promise of identical timestamps/hashes after rebuilding.

ST7789 uses MOSI13/SCLK14/CS15/DC2/BL21, software reset (RST=-1), SPI mode 3 at 27MHz and native 240x320 dimensions. XPT2046 touch uses isolated software SPI on CS33/IRQ36/MOSI32/MISO39/SCLK25. First touch wakes a sleeping display; subsequent taps change the statistics page.

## Statistics and settings

Session and lifetime counters include hashes, submitted/accepted/rejected shares, uptime and accepted/(accepted+rejected) success percentage. The two displays and `/api/advanced-stats` use coherent snapshots. The browser dashboard is served at `/`; settings are at `/settings`. External fee, market and network statistics are omitted from this local display profile.

Settings include network/pools/worker, display and touch, difficulty suggestions, timezone and SMTP. Password visibility applies only to newly entered values. Stored passwords are never returned to the browser or settings export. Empty password fields preserve stored values; explicit clear controls remove them. Export/import excludes secrets. POST controls check token, Host and Origin. This remains a local trusted-LAN interface, not an authenticated Internet service. The HTTP settings page and stored NVS credentials must not be described as encrypted credential storage.

## Direct SMTP reports

Gmail is preset to smtp.gmail.com:465. Configure username, app password, sender and a single recipient locally, save, then click Testmail. Gmail requires an eligible account with two-step verification and an app password for this AUTH LOGIN implementation. Automatic sending starts disabled. A separate connection probe checks TLS and SMTP greeting without authentication or a message.

The daily report includes session/lifetime counters and, after a confirmed prior report, their delta. Scheduling uses the existing fixed timezone offset, with no automatic daylight-saving conversion. Enabling after the scheduled hour can trigger today's catch-up report. Warning conditions are pool/WiFi outage for five minutes, low hashrate for five minutes, and rejection percentage over a ten-minute window with at least 20 replies. An offline or powered-off miner cannot immediately email; WiFi recovery is required.

Failures use bounded retries, persisted attempt state and cooldowns. An uncertain final SMTP acknowledgment suppresses automatic duplication of that message. Server acceptance does not guarantee inbox delivery. A narrow power-loss window can still prevent exactly-once delivery.

TLS verifies certificate chain, hostname and validity. The checked-in Mozilla/Certifi trust material and licenses are in src/mail/. Gmail uses standard mbedTLS CA path building with current GTS roots, because the old compact certificate-bundle callback rejected its cross-signed server chain. Other hosts use the compact bundle. No insecure verification fallback is provided. Supported: implicit TLS/465 and AUTH LOGIN. OAuth, STARTTLS/587, multiple recipients and attachments are not implemented. Trust anchors require maintenance.

## SHA sharing and mining

The MIT-licensed classic ESP32 assembly kernel is attributed in src/mining/LICENSE.nerdminer-modded. Core 0 is reserved for network/UI in this opt-in fast profile; core 1 owns the nonce space. Startup known-answer tests run twice, and every hardware candidate is checked by the production software SHA implementation.

The mail profile selects the conservative timing variant. All SDK SHA algorithms share TEXT memory, so locking only the SHA256 engine is insufficient: the guard locks shared SHA memory around at most 1024 hashes and waits for idle before use. Locks are released before yielding or networking. SMTP briefly pauses mining to release SHA resources, closes TLS before resuming, and preserves the current nonce search unless the pool replaced the job. Failed self-tests or candidate verification use software SHA fallback.

## Validation and limits

- Pinned ESP32 cross-build, app size/header/checksum and app readback verification passed.
- Offline SMTP parsing, rejection/authentication errors, lost final acknowledgment, injection checks, retry/reboot policy, warnings and exclusive mining gate passed.
- Shared SHA bounded-lock tests and independent software SHA vectors passed.
- Production settings handlers and browser password/export/import/test-action tests passed; statistics accounting and HTTP cache stress tests passed.
- On one physical board, a session ran for roughly 7h13m, averaging 873kH/s, with 2758 accepted and 24 rejected shares (99.14% success). No reboot occurred within that observed session. Continuous network/serial history for the whole night was not recorded.
- After the TLS correction, the device passed certificate/greeting checks and resumed around 870kH/s; no panic, brownout or watchdog fault appeared in the three-minute boot check. The owner subsequently confirmed receipt of a real Gmail test message. No credentials or personal raw logs are included.
- A separate unresolved persistence issue was observed: after a controlled restart, lifetime totals were below previously observed live values by about 379 seconds and 46 accepted shares. The exact cause is not established; this contribution does not claim that restart persistence issue is fixed. Treat this as an experimental profile pending review.
- New-device factory installation, other boards, Windows compilation, multi-day automatic SMTP delivery and DST handling have not been validated.

Offline tests (Python with ziglang==0.13.0):

```
python tests/advanced/run_native_tests.py
python tests/mail/run_tests.py
```

Browser tests require Node.js, Playwright and an installed browser. Set CHROME_EXECUTABLE to its path, or install Playwright Chromium for the mail browser test. Run the C++ tests first to generate the settings fixture:

```
node tests/mail/test_mail_browser.cjs
node tests/advanced/dashboard_test.cjs
```

No physical flashing or real SMTP message is performed by these offline tests. See tests/mail/README.md for details. Preserve upstream GPLv3 and the bundled MIT/MPL/Apache notices.
41 changes: 41 additions & 0 deletions include/advanced_fast_kernel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// ABI and fail-closed known-answer test for MIT NerdMiner-Modded kernel.
#pragma once
#include <stdint.h>
#include <stddef.h>
namespace advanced_fast {
struct Context {
uint32_t hdr[20]; uint32_t nonce; uint32_t budget;
volatile uint32_t run; uint32_t pad; uint32_t chk;
};
static_assert(sizeof(Context)==100, "kernel ABI size");
static_assert(offsetof(Context,nonce)==80 && offsetof(Context,budget)==84 &&
offsetof(Context,run)==88 && offsetof(Context,pad)==92 && offsetof(Context,chk)==96,"kernel ABI offsets");
struct Diagnostic {uint32_t reason=0, before=0, after=0, budget=0, hit=0, found=0;};
using Kernel = uint32_t (*)(volatile uint32_t*, Context*);
// Each invocation is bounded; the callback permits Wi-Fi/display scheduling.
inline bool selftest(Kernel fn, volatile uint32_t* base, void (*yield)(), Diagnostic* diagnostic=nullptr) {
const uint32_t expected[]={0xa94c,0xe496,0x1f476,0x302f0,0x35af9,0x3cf5c};
Context c={};
for(uint32_t i=0;i<19;++i)c.hdr[i]=0x9e3779b9u*(i+1);
c.nonce=0x1000; c.pad=0x80000000u; c.run=1;
unsigned found=0;
for(uint32_t remaining=262144;remaining;) {
const uint32_t budget=remaining<32768?remaining:32768;
c.budget=budget;
const uint32_t before=c.nonce;
const uint32_t hit=fn(base,&c);
if(diagnostic){diagnostic->before=before;diagnostic->after=c.nonce;diagnostic->budget=c.budget;diagnostic->hit=hit;diagnostic->found=found;}
if(hit>1 || c.budget>=budget){if(diagnostic)diagnostic->reason=1;return false;}
const uint32_t done=budget-c.budget;
if(c.nonce-before!=done || (!hit && c.budget)){if(diagnostic)diagnostic->reason=2;return false;}
remaining-=done;
if(hit && (found>=6 || c.nonce-1-0x1000!=expected[found++])){if(diagnostic)diagnostic->reason=3;return false;}
yield();
}
if(diagnostic)diagnostic->reason=found==6?0:4;
return found==6;
}
}
extern "C" uint32_t pipelined_hw_mine_classic_v2(volatile uint32_t*,advanced_fast::Context*);
extern "C" uint32_t pipelined_hw_mine_classic_v2_margin(volatile uint32_t*,advanced_fast::Context*);
extern "C" uint32_t pipelined_hw_mine_classic_v2_wide(volatile uint32_t*,advanced_fast::Context*);
137 changes: 137 additions & 0 deletions include/advanced_statistics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#pragma once
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <limits>

namespace advanced {
template<class T> inline T add(T a, T b) {
const T limit = std::numeric_limits<T>::max();
return b > limit - a ? limit : static_cast<T>(a + b);
}
inline double best(double value) {
return std::isfinite(value) && value >= 0 ? value : 0;
}
struct Counters {
uint64_t hashes = 0;
uint32_t submitted = 0, accepted = 0, rejected = 0, blocks = 0;
uint32_t uptime_s = 0;
double best_difficulty = 0;
};
// Only pool replies belong in the denominator; submitted/pending shares do not.
inline double success_percent(const Counters& counts) {
uint64_t replies = static_cast<uint64_t>(counts.accepted) + counts.rejected;
if (!replies) return -1.0;
// Round once in integer basis points so display and browser agree.
uint64_t basis_points = (10000ULL * counts.accepted + replies / 2) / replies;
return static_cast<double>(basis_points) / 100.0;
}
inline void success_json(const Counters& counts, char* out, size_t capacity) {
double value = success_percent(counts);
if (value < 0) std::snprintf(out, capacity, "null");
else std::snprintf(out, capacity, "%.2f", value);
}
class Ledger {
Counters boot_;
public:
explicit Ledger(const Counters& boot = Counters()) : boot_(boot) {}
Counters total(const Counters& session) const {
Counters t;
t.hashes = add(boot_.hashes, session.hashes);
t.submitted = add(boot_.submitted, session.submitted);
t.accepted = add(boot_.accepted, session.accepted);
t.rejected = add(boot_.rejected, session.rejected);
t.blocks = add(boot_.blocks, session.blocks);
t.uptime_s = add(boot_.uptime_s, session.uptime_s);
t.best_difficulty = best(boot_.best_difficulty) > best(session.best_difficulty)
? best(boot_.best_difficulty) : best(session.best_difficulty);
return t;
}
// Save absolute totals. Neither a successful save nor a failed one changes boot_.
template<class Save> bool checkpoint(const Counters& session, Save save) const {
return save(total(session));
}
};
class SessionClock {
uint32_t last_ms_ = 0;
uint64_t elapsed_ms_ = 0;
public:
void start(uint32_t now) { last_ms_ = now; elapsed_ms_ = 0; }
uint64_t tick(uint32_t now) {
elapsed_ms_ += static_cast<uint32_t>(now - last_ms_);
last_ms_ = now;
return elapsed_ms_ / 1000;
}
};
class SavePolicy {
bool attempted_ = false, first_share_saved_ = false, early_done_ = false;
uint64_t last_attempt_ = 0, last_saved_ = 0;
public:
bool due(uint64_t seconds, uint32_t accepted) const {
if (attempted_ && seconds - last_attempt_ < 30) return false;
return (!first_share_saved_ && accepted > 0) ||
(!early_done_ && seconds >= 300) ||
(early_done_ && seconds - last_saved_ >= 3600);
}
void result(uint64_t seconds, uint32_t accepted, bool success) {
attempted_ = true; last_attempt_ = seconds;
if (!success) return;
if (accepted) first_share_saved_ = true;
if (seconds >= 300) early_done_ = true;
last_saved_ = seconds;
}
};
class Rate {
bool ready_ = false, have_rate_ = false;
uint64_t hashes_ = 0, ms_ = 0;
double value_ = 0;
public:
double sample(uint64_t hashes, uint64_t ms) {
if (ready_ && ms > ms_ && hashes >= hashes_) {
double instant = static_cast<double>(hashes - hashes_) * 1000.0 / (ms - ms_);
value_ = have_rate_ ? 0.15 * instant + 0.85 * value_ : instant;
have_rate_ = true;
} else if (ready_ && hashes < hashes_) {
value_ = 0; have_rate_ = false;
}
hashes_ = hashes; ms_ = ms; ready_ = true;
return value_;
}
};
struct Snapshot {
Counters session, lifetime;
uint64_t sampled_ms = 0, sequence = 0;
uint32_t boot_id = 0;
double hashrate_hs = 0;
bool pool_connected = false, wifi_connected = false;
bool last_save_ok = true;
bool save_attempted = false;
};
inline bool limited(const Counters& c) {
return c.hashes == UINT64_MAX || c.submitted == UINT32_MAX ||
c.accepted == UINT32_MAX || c.rejected == UINT32_MAX ||
c.blocks == UINT32_MAX || c.uptime_s == UINT32_MAX;
}
inline bool json(const Snapshot& s, uint64_t now_ms, char* out, size_t capacity) {
if (!out || !capacity) return false;
out[0] = '\0';
if (!std::isfinite(s.hashrate_hs) || s.hashrate_hs < 0 || now_ms < s.sampled_ms) return false;
const auto& a = s.session; const auto& t = s.lifetime;
char session_success[16], lifetime_success[16];
success_json(a, session_success, sizeof session_success);
success_json(t, lifetime_success, sizeof lifetime_success);
int n = std::snprintf(out, capacity,
"{\"schema\":1,\"boot_id\":\"%u\",\"sequence\":\"%llu\",\"sample_age_ms\":\"%llu\",\"hashrate_hs\":%.3f,"
"\"pool_connected\":%s,\"wifi_connected\":%s,\"save_attempted\":%s,\"last_save_ok\":%s,"
"\"counter_limit_reached\":%s,"
"\"session\":{\"hashes\":\"%llu\",\"submitted\":\"%u\",\"accepted\":\"%u\",\"rejected\":\"%u\",\"blocks\":\"%u\",\"uptime_s\":\"%u\",\"success_percent\":%s},"
"\"lifetime\":{\"hashes\":\"%llu\",\"submitted\":\"%u\",\"accepted\":\"%u\",\"rejected\":\"%u\",\"blocks\":\"%u\",\"uptime_s\":\"%u\",\"best_difficulty\":%.9g,\"success_percent\":%s}}",
(unsigned)s.boot_id, (unsigned long long)s.sequence, (unsigned long long)(now_ms-s.sampled_ms), s.hashrate_hs,
s.pool_connected?"true":"false", s.wifi_connected?"true":"false",
s.save_attempted?"true":"false", s.last_save_ok?"true":"false", limited(t)?"true":"false",
(unsigned long long)a.hashes, (unsigned)a.submitted, (unsigned)a.accepted, (unsigned)a.rejected, (unsigned)a.blocks, (unsigned)a.uptime_s, session_success,
(unsigned long long)t.hashes, (unsigned)t.submitted, (unsigned)t.accepted, (unsigned)t.rejected, (unsigned)t.blocks, (unsigned)t.uptime_s, best(t.best_difficulty), lifetime_success);
if (n < 0 || static_cast<size_t>(n) >= capacity) { out[0] = '\0'; return false; }
return true;
}
} // namespace advanced
13 changes: 6 additions & 7 deletions include/board_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@
#define USE_DISPLAY 1
#endif
#define DISPLAY_TYPE_TFT 1
// Undefine TFT dimensions if set by library, then set our values
#ifdef TFT_WIDTH
#undef TFT_WIDTH
// TFT_eSPI expects the native portrait dimensions; rotation is applied later.
// Preserve the selected board's build flags instead of replacing them.
#ifndef TFT_WIDTH
#define TFT_WIDTH 240
#endif
#define TFT_WIDTH 320
#ifdef TFT_HEIGHT
#undef TFT_HEIGHT
#ifndef TFT_HEIGHT
#define TFT_HEIGHT 320
#endif
#define TFT_HEIGHT 240

// LED (RGB, active low)
#ifndef LED_R_PIN
Expand Down
Loading