From 7aaaa7a684e33591125963df1eb4da6da2fa6495 Mon Sep 17 00:00:00 2001 From: Bitflash Date: Tue, 4 Aug 2026 15:14:46 -0300 Subject: rpc: build the pool block template the way the solo miner does (#141) Two bugs in RebuildJob(), the operator's Stratum template builder. It is a separate path from BitcoinMiner() and had drifted from it. A single pass over mapTransactions is not enough. That map is keyed by hash and carries no dependency order, so a transaction spending another still-unconfirmed transaction's output -- change from a recent send, usually -- fails ConnectInputs whenever its parent sits later in map order. One pass drops it, and map order does not change between rebuilds, so every later rebuild drops it at the same point. That is deterministic exclusion rather than bad luck: where pool operators mine most blocks, a transaction of that shape can stay unconfirmed for as long as its parent is. Now it retries until a pass adds nothing, which is what the solo builder has always done. And the fees reached nobody. nFees was declared inside the scan loop, so each transaction reset it and the running total was discarded, while the coinbase line passed a hardcoded 0. An operator collected fees into a block and paid none of them out. The block stayed valid, since paying less than allowed always is, so nothing ever complained. Both the diagnosis and the fix are Vic-Nas's, from his fork of this code. He deleted the fork, so this brings the work back rather than letting it go with it. His description of the failure was precise enough that finding the same two defects here took one read of our RebuildJob. --- src/rpc.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 13 deletions(-) (limited to 'src/rpc.cpp') diff --git a/src/rpc.cpp b/src/rpc.cpp index 41c0328..d73b09c 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -167,29 +167,64 @@ static bool RebuildJob() CBlock block; block.vtx.push_back(txNew); + // Fill the block the same way BitcoinMiner() does, because this is the + // same job: a block that gets mined and submitted for real. + // + // It used to differ in two ways, and both were bugs. + // + // One pass over mapTransactions is not enough. That map is keyed by + // hash, so it has no dependency order, and a transaction spending the + // still-unconfirmed output of another (change from a recent send, most + // commonly) fails ConnectInputs whenever its parent happens to sit + // later in map order. A single pass then drops it -- and because map + // order does not change between rebuilds, every later rebuild drops it + // again at the same point. Deterministic exclusion, not bad luck: if + // pool operators mine most of the blocks, a transaction of that shape + // can stay unconfirmed for as long as its parent does. + // + // Diagnosis and fix both come from Vic-Nas, who found it in his fork + // of this code and described it exactly. The fork has since been + // deleted, so this carries it back rather than losing it. + int64 nFees = 0; { CTxDB txdb("r"); CRITICAL_BLOCK(cs_mapTransactions) { map pool; + vector vfAlreadyAdded(mapTransactions.size()); + bool fFoundSomething = true; unsigned int sz = 0; - for (auto& kv : mapTransactions) { - CTransaction& tx = kv.second; - if (tx.IsCoinBase() || !tx.IsFinal()) continue; - int64 nFees = 0; - map tmp(pool); - if (!tx.ConnectInputs(txdb, tmp, CDiskTxPos(1,1,1), 0, nFees, - false, true, tx.GetMinFee(block.vtx.size() < 100))) - continue; - pool = tmp; - block.vtx.push_back(tx); - sz += ::GetSerializeSize(tx, SER_NETWORK); - if (sz > MAX_SIZE / 2) break; + while (fFoundSomething && sz < MAX_SIZE / 2) + { + fFoundSomething = false; + unsigned int n = 0; + for (map::iterator mi = mapTransactions.begin(); + mi != mapTransactions.end(); ++mi, ++n) + { + if (vfAlreadyAdded[n]) + continue; + CTransaction& tx = (*mi).second; + if (tx.IsCoinBase() || !tx.IsFinal()) continue; + map tmp(pool); + if (!tx.ConnectInputs(txdb, tmp, CDiskTxPos(1,1,1), 0, nFees, + false, true, tx.GetMinFee(block.vtx.size() < 100))) + continue; + swap(pool, tmp); + block.vtx.push_back(tx); + sz += ::GetSerializeSize(tx, SER_NETWORK); + vfAlreadyAdded[n] = true; + fFoundSomething = true; + } } } } - block.vtx[0].vout[0].nValue = block.GetBlockValue(pindexPrev->nHeight + 1, 0); + // And the fees actually go to the coinbase now. nFees was declared + // inside the scan loop, so every transaction reset it and the total was + // thrown away, while this line passed a hardcoded 0. A pool operator + // collected fees into a block and paid none of them to itself. Solo + // mining has always passed nFees here; only this builder did not. + block.vtx[0].vout[0].nValue = block.GetBlockValue(pindexPrev->nHeight + 1, nFees); block.hashPrevBlock = pindexPrev->GetBlockHash(); block.hashMerkleRoot = block.BuildMerkleTree(); block.nTime = max((unsigned int)(pindexPrev->GetMedianTimePast() + 1), -- cgit v1.2.3 From 0b01d6cfce8be68150f5e06c11e13e0443372c4d Mon Sep 17 00:00:00 2001 From: Nsable-sh Date: Fri, 7 Aug 2026 11:25:45 -0300 Subject: pool: make payout ledger crash-safe --- src/rpc.cpp | 247 +++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 196 insertions(+), 51 deletions(-) (limited to 'src/rpc.cpp') diff --git a/src/rpc.cpp b/src/rpc.cpp index d73b09c..8f3740d 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -251,36 +251,72 @@ static bool RebuildJob() // Payouts -- event loop thread only // --------------------------------------------------------------------------- struct PendingPayout { + std::string id; + int blockHeight; int matureAtHeight; std::map amounts; // address -> satoshis + std::map paidTxids; // address -> txid + int64 nextAttempt; + + PendingPayout() : blockHeight(0), matureAtHeight(0), nextAttempt(0) {} }; static std::vector gPendingPayouts; +static std::mutex gPayoutLedgerMutex; static std::string PayoutFilePath() { return GetAppDir() + "/pending_payouts.json"; } -static void SavePendingPayouts() +static std::string PayoutIdForBlock(int blockHeight) +{ + return strprintf("block-%d", blockHeight); +} + +static bool PayoutFullyPaid(const PendingPayout& pp) +{ + for (const auto& kv : pp.amounts) + if (pp.paidTxids.count(kv.first) == 0) + return false; + return true; +} + +static void SavePendingPayoutsLocked() { nlohmann::json arr = nlohmann::json::array(); for (const auto& pp : gPendingPayouts) { nlohmann::json obj; + obj["id"] = pp.id; + obj["blockHeight"] = pp.blockHeight; obj["matureAtHeight"] = pp.matureAtHeight; + obj["nextAttempt"] = pp.nextAttempt; nlohmann::json amounts = nlohmann::json::object(); for (const auto& kv : pp.amounts) amounts[kv.first] = kv.second; obj["amounts"] = amounts; + nlohmann::json paid = nlohmann::json::object(); + for (const auto& kv : pp.paidTxids) + paid[kv.first] = kv.second; + obj["paidTxids"] = paid; arr.push_back(obj); } std::string path = PayoutFilePath(); - FILE* f = fopen(path.c_str(), "w"); + std::string tmp = path + ".tmp"; + FILE* f = fopen(tmp.c_str(), "w"); if (f) { std::string s = arr.dump(2); fwrite(s.c_str(), 1, s.size(), f); - fclose(f); + if (fclose(f) != 0) { + LogPrint("payout", "[payout] WARNING: could not close %s\n", tmp.c_str()); + return; + } +#ifdef _WIN32 + remove(path.c_str()); +#endif + if (rename(tmp.c_str(), path.c_str()) != 0) + LogPrint("payout", "[payout] WARNING: could not replace %s\n", path.c_str()); } else { - LogPrint("payout", "[payout] WARNING: could not write %s\n", path.c_str()); + LogPrint("payout", "[payout] WARNING: could not write %s\n", tmp.c_str()); } } @@ -297,11 +333,25 @@ static void LoadPendingPayouts() fclose(f); try { nlohmann::json arr = nlohmann::json::parse(s); + std::lock_guard lk(gPayoutLedgerMutex); for (const auto& obj : arr) { PendingPayout pp; + if (obj.contains("blockHeight")) + pp.blockHeight = obj["blockHeight"].get(); pp.matureAtHeight = obj["matureAtHeight"].get(); + if (obj.contains("id") && obj["id"].is_string()) + pp.id = obj["id"].get(); + if (pp.blockHeight == 0) + pp.blockHeight = pp.matureAtHeight - COINBASE_MATURITY; + if (pp.id.empty()) + pp.id = PayoutIdForBlock(pp.blockHeight); + if (obj.contains("nextAttempt")) + pp.nextAttempt = obj["nextAttempt"].get(); for (auto it = obj["amounts"].begin(); it != obj["amounts"].end(); ++it) pp.amounts[it.key()] = it.value().get(); + if (obj.contains("paidTxids") && obj["paidTxids"].is_object()) + for (auto it = obj["paidTxids"].begin(); it != obj["paidTxids"].end(); ++it) + pp.paidTxids[it.key()] = it.value().get(); gPendingPayouts.push_back(pp); } LogPrint("payout", "[payout] loaded %zu pending payout(s) from disk\n", @@ -319,6 +369,35 @@ static std::vector gPayoutExecQueue; static std::mutex gPayoutExecMutex; static std::atomic gPayoutThreadRunning{false}; +static std::set gQueuedPayouts; + +static bool FindRecordedPoolPayout(const std::string& id, + const std::string& address, + int64 amount, + std::string& txidRet) +{ + CRITICAL_BLOCK(cs_mapWallet) + { + for (map::const_iterator it = mapWallet.begin(); + it != mapWallet.end(); ++it) + { + const CWalletTx& wtx = it->second; + map::const_iterator idIt = wtx.mapValue.find("pool_payout_id"); + if (idIt == wtx.mapValue.end() || idIt->second != id) + continue; + map::const_iterator addrIt = wtx.mapValue.find("pool_payout_address"); + if (addrIt == wtx.mapValue.end() || addrIt->second != address) + continue; + map::const_iterator amtIt = wtx.mapValue.find("pool_payout_amount"); + if (amtIt == wtx.mapValue.end() || amtIt->second != strprintf("%lld", amount)) + continue; + txidRet = wtx.GetHash().GetHex(); + return true; + } + } + return false; +} + static void QueuePayouts(int blockHeight, const std::map& shareCount, uint64 total) @@ -341,6 +420,8 @@ static void QueuePayouts(int blockHeight, int64 minerReward = (int64)((double)nSubsidy * (1.0 - feeFrac)); PendingPayout pp; + pp.id = PayoutIdForBlock(blockHeight); + pp.blockHeight = blockHeight; pp.matureAtHeight = blockHeight + COINBASE_MATURITY; int skipped = 0; @@ -364,8 +445,11 @@ static void QueuePayouts(int blockHeight, pp.amounts[kv.first] = amount; } - gPendingPayouts.push_back(pp); - SavePendingPayouts(); + { + std::lock_guard lk(gPayoutLedgerMutex); + gPendingPayouts.push_back(pp); + SavePendingPayoutsLocked(); + } int64 operatorCut = nSubsidy - minerReward; LogPrint("payout", @@ -383,42 +467,33 @@ static void QueuePayouts(int blockHeight, : ""); } -// Called from event loop: move matured entries to the exec queue (fast). -// NOT crash-safe, despite the ordering below looking like it is. -// -// A matured payout is erased from gPendingPayouts, the file is rewritten -// without it, and only then does it reach the exec queue -- which lives in -// memory alone. SavePendingPayouts writes gPendingPayouts and nothing else; -// there is no exec-queue section of the file to recover from. Die anywhere -// between the rewrite and SendMoney and those miners are simply never paid, -// with no record that they were owed anything. -// -// Making this safe needs the payment to be idempotent -- record the txid and -// check the wallet for it on restart -- rather than a reordering, since -// re-queueing blindly would double-pay whoever was already sent to. +// Called from event loop: copy matured entries to the exec queue (fast). +// The ledger entry stays on disk until every recipient has a txid recorded. +// On restart, FindRecordedPoolPayout() checks wallet metadata before sending, +// so a crash after SendMoney but before the ledger update does not double-pay. static void FlushMaturePayouts() { std::vector ready; - for (auto it = gPendingPayouts.begin(); it != gPendingPayouts.end(); ) { - if (nBestHeight < it->matureAtHeight) { ++it; continue; } - LogPrint("payout", "[payout] block height %d matured -- queuing %zu payments\n", - it->matureAtHeight, it->amounts.size()); - ready.push_back(*it); // copy first; erase after save - ++it; - } - if (!ready.empty()) { - // Erase from pending then save before handing off to exec thread, - // so the file never contains an entry that is already being sent. - for (auto it = gPendingPayouts.begin(); it != gPendingPayouts.end(); ) { - if (nBestHeight >= it->matureAtHeight) - it = gPendingPayouts.erase(it); - else - ++it; + { + std::lock_guard ledger(gPayoutLedgerMutex); + std::lock_guard queue(gPayoutExecMutex); + int64 now = GetTime(); + for (const PendingPayout& pp : gPendingPayouts) { + if (nBestHeight < pp.matureAtHeight) + continue; + if (pp.nextAttempt > now) + continue; + if (PayoutFullyPaid(pp)) + continue; + if (gQueuedPayouts.count(pp.id)) + continue; + LogPrint("payout", "[payout] block height %d matured -- queuing payout %s (%zu recipients)\n", + pp.matureAtHeight, pp.id.c_str(), pp.amounts.size()); + ready.push_back(pp); + gQueuedPayouts.insert(pp.id); } - SavePendingPayouts(); - std::lock_guard lk(gPayoutExecMutex); for (auto& pp : ready) - gPayoutExecQueue.push_back(std::move(pp)); + gPayoutExecQueue.push_back(pp); } } @@ -437,10 +512,21 @@ static void PayoutThreadFn(void*) batch.swap(gPayoutExecQueue); } - for (auto& pp : batch) { + for (auto& ppQueued : batch) { LogPrint("payout", "[payout] executing payments for block matured at %d" - " (%zu miners)\n", pp.matureAtHeight, pp.amounts.size()); - for (auto& kv : pp.amounts) { + " (%zu miners)\n", ppQueued.matureAtHeight, ppQueued.amounts.size()); + bool fAbortBatch = false; + for (auto& kv : ppQueued.amounts) { + bool alreadyPaid = false; + { + std::lock_guard lk(gPayoutLedgerMutex); + for (const PendingPayout& pp : gPendingPayouts) + if (pp.id == ppQueued.id && pp.paidTxids.count(kv.first)) + alreadyPaid = true; + } + if (alreadyPaid) + continue; + uint160 h160; if (!AddressToHash160(kv.first, h160)) { // Should not happen -- bad addresses are filtered in QueuePayouts @@ -451,16 +537,70 @@ static void PayoutThreadFn(void*) CScript sc; sc << OP_DUP << OP_HASH160 << h160 << OP_EQUALVERIFY << OP_CHECKSIG; CWalletTx wtx; - wtx.mapValue["comment"] = strprintf("Pool payout height %d", - pp.matureAtHeight); - if (SendMoney(sc, kv.second, wtx)) + wtx.mapValue["comment"] = strprintf("Pool payout height %d", ppQueued.matureAtHeight); + wtx.mapValue["pool_payout_id"] = ppQueued.id; + wtx.mapValue["pool_payout_address"] = kv.first; + wtx.mapValue["pool_payout_amount"] = strprintf("%lld", kv.second); + + std::string txid; + if (FindRecordedPoolPayout(ppQueued.id, kv.first, kv.second, txid)) + { + LogPrint("payout", "[payout] recovered recorded payout %s to %s" + " from wallet tx %s\n", + FormatMoney(kv.second).c_str(), kv.first.c_str(), txid.c_str()); + } + else if (SendMoney(sc, kv.second, wtx)) + { + txid = wtx.GetHash().GetHex(); LogPrint("payout", "[payout] paid %s to %s\n", - FormatMoney(kv.second).c_str(), kv.first.c_str()); + FormatMoney(kv.second).c_str(), kv.first.c_str()); + } else + { LogPrint("payout", "[payout] FAILED to pay %s to %s" - " (insufficient funds or wallet locked?)\n", - FormatMoney(kv.second).c_str(), kv.first.c_str()); + " (insufficient funds or wallet locked?)\n", + FormatMoney(kv.second).c_str(), kv.first.c_str()); + std::lock_guard lk(gPayoutLedgerMutex); + for (PendingPayout& pp : gPendingPayouts) + if (pp.id == ppQueued.id) + pp.nextAttempt = GetTime() + 60; + SavePendingPayoutsLocked(); + fAbortBatch = true; + break; + } + + { + std::lock_guard lk(gPayoutLedgerMutex); + for (PendingPayout& pp : gPendingPayouts) { + if (pp.id != ppQueued.id) + continue; + pp.paidTxids[kv.first] = txid; + pp.nextAttempt = 0; + SavePendingPayoutsLocked(); + break; + } + } } + + { + std::lock_guard ledger(gPayoutLedgerMutex); + for (auto it = gPendingPayouts.begin(); it != gPendingPayouts.end(); ) { + if (it->id == ppQueued.id && PayoutFullyPaid(*it)) { + LogPrint("payout", "[payout] payout %s complete -- removing from ledger\n", + it->id.c_str()); + it = gPendingPayouts.erase(it); + SavePendingPayoutsLocked(); + } else { + ++it; + } + } + } + { + std::lock_guard lk(gPayoutExecMutex); + gQueuedPayouts.erase(ppQueued.id); + } + if (fAbortBatch) + continue; } Sleep(500); // check for new work every 500ms @@ -1073,16 +1213,21 @@ static void RefreshStats(int blocksFoundThisSession, uint64 roundShareTotal) : a.lastSeen > b.lastSeen; }); - // gPendingPayouts is event-loop-only data (no lock needed here since - // RefreshStats is always called from the event loop thread). gPayoutViews.clear(); + std::lock_guard ledger(gPayoutLedgerMutex); for (const PendingPayout& pp : gPendingPayouts) { PendingPayoutView pv; pv.matureAtHeight = pp.matureAtHeight; - pv.recipients = (int)pp.amounts.size(); + pv.recipients = 0; pv.totalAmount = 0; - for (auto& kv : pp.amounts) pv.totalAmount += kv.second; - gPayoutViews.push_back(pv); + for (auto& kv : pp.amounts) { + if (pp.paidTxids.count(kv.first)) + continue; + pv.recipients++; + pv.totalAmount += kv.second; + } + if (pv.recipients > 0) + gPayoutViews.push_back(pv); } } -- cgit v1.2.3 From 8c2e375600c0eb9bedb8f85ee4fa860825f23521 Mon Sep 17 00:00:00 2001 From: Nsable-sh Date: Fri, 7 Aug 2026 11:40:46 -0300 Subject: test: cover pool Stratum share accounting --- src/Makefile | 1 + src/makefile.mingw | 1 + src/rpc.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/selftest.cpp | 6 +++- 4 files changed, 92 insertions(+), 1 deletion(-) (limited to 'src/rpc.cpp') diff --git a/src/Makefile b/src/Makefile index cc52fd6..bc15ef8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -191,5 +191,6 @@ tests: test_bip32 bitflash-node ./bitflash-node -selftest=wallet-encrypt ./bitflash-node -selftest=net-message ./bitflash-node -selftest=consensus-limits + ./bitflash-node -selftest=pool-stratum .PHONY: tests fuzz-net-message-smoke fuzz-script-smoke diff --git a/src/makefile.mingw b/src/makefile.mingw index e076c49..b946e50 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw @@ -116,3 +116,4 @@ tests: test_bip32.exe bitflash.exe ./bitflash.exe -selftest=wallet-encrypt ./bitflash.exe -selftest=net-message ./bitflash.exe -selftest=consensus-limits + ./bitflash.exe -selftest=pool-stratum diff --git a/src/rpc.cpp b/src/rpc.cpp index 8f3740d..383769a 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -1180,6 +1180,91 @@ static bool HandleLine(Miner* m, const std::string& rawLine, return true; } +int RunPoolStratumSelfTest() +{ + int nFail = 0; + auto check = [&](bool ok, const char* msg) { + fprintf(stdout, " %s %s\n", ok ? "ok" : "FAIL", msg); + if (!ok) nFail++; + }; + + fprintf(stdout, "pool-stratum self-test\n"); + + StratumJob savedJob = gCurrentJob; + bool savedHaveJob = gHaveJob; + + CTransaction coinbase; + coinbase.vin.resize(1); + coinbase.vin[0].prevout.SetNull(); + coinbase.vin[0].scriptSig << 1 << (CBigNum)1; + coinbase.vout.resize(1); + coinbase.vout[0].nValue = 50 * COIN; + + CBlock block; + block.vtx.push_back(coinbase); + block.nVersion = 1; + block.nBits = 0x1f0fffff; + block.nTime = 1; + block.nNonce = 1; + block.hashMerkleRoot = block.BuildMerkleTree(); + + gCurrentJob.jobId = "selftest"; + gCurrentJob.block = block; + gCurrentJob.target = 0; // accepted share, not a block candidate + gCurrentJob.height = 1; + gHaveJob = true; + + Miner miner(INVALID_SOCKET); + miner.authorised = true; + miner.address = "selftest-address"; + miner.worker = "worker"; + miner.shareTarget = ~uint256(0); // every RandomX hash is a valid share + miner.difficulty = 0.001; + miner.vardiffWindowStart = GetTime(); + + std::map roundShareCount; + uint64 roundShareTotal = 0; + int blocksFound = 0; + + uint256 targetFromNotify; + CBlock minerBlock = BuildMinerBlock(gCurrentJob, miner.extranonce2); + json params = MakeNotifyParams(gCurrentJob, minerBlock, miner.shareTarget, true); + std::vector targetBytes = FromHex(params[3].get()); + if (targetBytes.size() == 32) + memcpy(&targetFromNotify, &targetBytes[0], 32); + check(targetFromNotify == miner.shareTarget, + "mining.notify exposes the same share target the pool validates"); + + json submit = { + {"id", 7}, + {"method", "mining.submit"}, + {"params", json::array({miner.address, "selftest", ToHex(&minerBlock.nNonce, 4)})} + }; + HandleLine(&miner, submit.dump(), roundShareCount, roundShareTotal, blocksFound); + + check(roundShareTotal == 1, "fake low-difficulty share increments the round total"); + check(roundShareCount[miner.address] == 1, "fake share is credited to the submitting address"); + check(miner.sessionShares == 1 && miner.roundShares == 1 && miner.totalShares == 1, + "miner share counters advance together"); + check(blocksFound == 0, "share below block target is not counted as a found block"); + + json stale = { + {"id", 8}, + {"method", "mining.submit"}, + {"params", json::array({miner.address, "stale", ToHex(&minerBlock.nNonce, 4)})} + }; + HandleLine(&miner, stale.dump(), roundShareCount, roundShareTotal, blocksFound); + check(roundShareTotal == 1, "stale submit does not increment shares"); + + gCurrentJob = savedJob; + gHaveJob = savedHaveJob; + + fprintf(stdout, "%s (%d failure%s)\n", nFail == 0 ? "ALL TESTS PASSED" : "TESTS FAILED", + nFail, nFail == 1 ? "" : "s"); + fflush(stdout); + return nFail == 0 ? 0 : 1; +} + // --------------------------------------------------------------------------- // Stats refresh -- called every 2s from event loop // --------------------------------------------------------------------------- diff --git a/src/selftest.cpp b/src/selftest.cpp index ff0289b..fadc959 100644 --- a/src/selftest.cpp +++ b/src/selftest.cpp @@ -10,6 +10,8 @@ #include "selftest.h" #include "walletcmd.h" +extern int RunPoolStratumSelfTest(); + // Test results go to the terminal, not to debug.log. // // printf in this tree is OutputDebugStringF, which writes into the data @@ -1234,8 +1236,10 @@ int RunSelfTest(const std::string& name) return RunNetMessageSelfTest(); if (name == "consensus-limits") return RunConsensusLimitsSelfTest(); + if (name == "pool-stratum") + return RunPoolStratumSelfTest(); printf("Unknown self-test '%s'\n", name.c_str()); - printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-crypto, wallet-encrypt, net-message, consensus-limits\n"); + printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-crypto, wallet-encrypt, net-message, consensus-limits, pool-stratum\n"); return 1; } -- cgit v1.2.3 From 5c2e66b70ab3c15603a6e2d7a4ea9e02f3cd0604 Mon Sep 17 00:00:00 2001 From: Nsable-sh Date: Fri, 7 Aug 2026 11:30:49 -0300 Subject: pool: write dashboard status JSON --- README.md | 6 ++++ src/main.cpp | 1 + src/main.h | 1 + src/main_gui.cpp | 5 ++++ src/rpc.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) (limited to 'src/rpc.cpp') diff --git a/README.md b/README.md index 6746e02..af56081 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,11 @@ Open **Options** from the menu bar. Under Mining Mode: Pool announcements are published on Nostr with live status fields, so external tools can query the latest pool state by `.btf` address. +Pool operators also write a local `pool_status.json` every ten seconds. By +default it is created in the data directory; use `-poolstatusfile=PATH` to write +it somewhere a dashboard or web sync can read. The file is informational only: +nodes still discover pools through Nostr and `.btf`, not through any web domain. + **Participant** — mine to someone else's pool. Enter or select the pool's `.btf` address and enable Start Mining. SRBMiner and XMRig connect to operator pools through the `.btf` path: @@ -236,6 +241,7 @@ xmrig -a rx/0 -o POOL_BTF_ADDRESS -u YOUR_BTF_ADDRESS -p x ./bitflash -nogui # node only ./bitflash -nogui -gen # node + solo mining ./bitflash -nogui -gen -operator # pool operator +./bitflash -nogui -gen -operator -poolstatusfile=/var/www/api/pools.json ./bitflash -nogui -gen -participant=POOL_BTF_ADDRESS # mine to pool ``` diff --git a/src/main.cpp b/src/main.cpp index 2610c45..6cb7353 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -104,6 +104,7 @@ bool fMineModeFromCommandLine = false; string strParticipantPool; // participant mode: pool .btf address string strPoolName = "Bitflash Pool"; string strPoolDashboardUrl; +string strPoolStatusFile; double dPoolFeePercent = 0.0; static std::atomic gParticipantSharesSent{0}; diff --git a/src/main.h b/src/main.h index c458412..3866855 100644 --- a/src/main.h +++ b/src/main.h @@ -86,6 +86,7 @@ extern bool fMineModeFromCommandLine; extern string strParticipantPool; // participant: pool .btf address extern string strPoolName; // operator: announced pool name extern string strPoolDashboardUrl; // operator: optional dashboard URL +extern string strPoolStatusFile; // operator: optional JSON status output path extern double dPoolFeePercent; // operator: announced fee percent extern bool fSoloMineTest; // /solomine: mine without requiring a peer (local test) // Threads to hash with; 0 = decide from the hardware. Set by /genproclimit. diff --git a/src/main_gui.cpp b/src/main_gui.cpp index 875988f..41661b9 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -123,6 +123,7 @@ static void PrintUsage() printf(" /poolname=NAME\n"); printf(" /poolfee=PCT\n"); printf(" /pooldashboard=URL (alias: /pooldash=URL)\n"); + printf(" /poolstatusfile=PATH (write pool status JSON for dashboards)\n"); printf("\n"); printf(".btf and rendezvous:\n"); printf(" /connectbtf=PEER_BTF_ADDRESS\n"); @@ -239,6 +240,10 @@ static void ParseStartupArguments(int argc, char* argv[]) if (!poolFee.empty()) dPoolFeePercent = atof(poolFee.c_str()); + string poolStatusFile = argval2(argc, argv, "/poolstatusfile", "-poolstatusfile"); + if (!poolStatusFile.empty()) + strPoolStatusFile = poolStatusFile; + string btfConnect = argval2(argc, argv, "/connectbtf", "-connectbtf"); if (!btfConnect.empty()) strBtfConnect = btfConnect; diff --git a/src/rpc.cpp b/src/rpc.cpp index 383769a..1aa4f9b 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -623,6 +623,7 @@ static StatsSnapshot gStats; static std::vector gWorkerStats; static std::vector gPayoutViews; static std::mutex gStatsMutex; +static int64 gLastPoolStatusWrite = 0; void GetPoolOperatorStats(int& authorizedMiners, int& blocksFound, uint64& roundShares, double& totalHashRate) { @@ -645,6 +646,82 @@ void GetPendingPayouts(std::vector& out) out = gPayoutViews; } +static std::string PoolStatusFilePath() +{ + if (!strPoolStatusFile.empty()) + return strPoolStatusFile; + return GetAppDir() + "/pool_status.json"; +} + +static void WritePoolStatusJsonLocked() +{ + nlohmann::json j; + j["schema"] = "bitflash-pool-status-1"; + j["network"] = "bitflash"; + j["updatedAt"] = GetTime(); + + j["operator"] = { + {"name", strPoolName}, + {"btfAddress", BtfLocalAddress()}, + {"feePercent", dPoolFeePercent}, + {"dashboardUrl", strPoolDashboardUrl} + }; + + j["node"] = { + {"height", nBestHeight}, + {"poolRunning", (bool)gPoolRunning}, + {"mode", nMineMode} + }; + + j["pool"] = { + {"authorizedMiners", gStats.authorizedMiners}, + {"blocksFoundSession", gStats.blocksFound}, + {"roundShares", gStats.roundShares}, + {"hashRate", gStats.totalHashRate} + }; + + j["workers"] = nlohmann::json::array(); + for (const PoolWorkerStatView& row : gWorkerStats) { + j["workers"].push_back({ + {"address", row.address}, + {"worker", row.worker}, + {"totalShares", row.totalShares}, + {"roundShares", row.roundShares}, + {"lastSeen", row.lastSeen}, + {"hashRate", row.hashRate} + }); + } + + j["pendingPayouts"] = nlohmann::json::array(); + for (const PendingPayoutView& row : gPayoutViews) { + j["pendingPayouts"].push_back({ + {"matureAtHeight", row.matureAtHeight}, + {"recipients", row.recipients}, + {"totalAmountSatoshis", row.totalAmount}, + {"totalAmountBtf", FormatMoney(row.totalAmount)} + }); + } + + std::string path = PoolStatusFilePath(); + std::string tmp = path + ".tmp"; + FILE* f = fopen(tmp.c_str(), "w"); + if (!f) { + LogPrint("pool", "[pool] WARNING: could not write status file %s\n", tmp.c_str()); + return; + } + std::string s = j.dump(2); + fwrite(s.c_str(), 1, s.size(), f); + if (fclose(f) != 0) { + LogPrint("pool", "[pool] WARNING: could not close status file %s\n", tmp.c_str()); + return; + } +#ifdef _WIN32 + remove(path.c_str()); +#endif + if (rename(tmp.c_str(), path.c_str()) != 0) + LogPrint("pool", "[pool] WARNING: could not replace status file %s\n", path.c_str()); +} + // --------------------------------------------------------------------------- // Miner state -- event loop thread only // --------------------------------------------------------------------------- @@ -1314,6 +1391,12 @@ static void RefreshStats(int blocksFoundThisSession, uint64 roundShareTotal) if (pv.recipients > 0) gPayoutViews.push_back(pv); } + + int64 now = GetTime(); + if (now - gLastPoolStatusWrite >= 10) { + WritePoolStatusJsonLocked(); + gLastPoolStatusWrite = now; + } } // --------------------------------------------------------------------------- -- cgit v1.2.3 From b1eb8b66b8d7b47c7fb2e85d5d4db86a11823366 Mon Sep 17 00:00:00 2001 From: Nsable-sh Date: Fri, 7 Aug 2026 17:14:36 -0300 Subject: pool: publish round proof ledger --- README.md | 14 +- docs/pool-directory.md | 12 +- scripts/build-pool-index.py | 18 ++- src/main.cpp | 1 + src/main.h | 1 + src/main_gui.cpp | 5 + src/readme.txt | 4 + src/rpc.cpp | 316 +++++++++++++++++++++++++++++++++++++++++++- 8 files changed, 359 insertions(+), 12 deletions(-) (limited to 'src/rpc.cpp') diff --git a/README.md b/README.md index 7f4d9b0..ec8e9c4 100644 --- a/README.md +++ b/README.md @@ -219,10 +219,12 @@ Open **Options** from the menu bar. Under Mining Mode: Pool announcements are published on Nostr with live status fields, so external tools can query the latest pool state by `.btf` address. -Pool operators also write a local `pool_status.json` every ten seconds. By -default it is created in the data directory; use `-poolstatusfile=PATH` to write -it somewhere a dashboard or web sync can read. The file is informational only: -nodes still discover pools through Nostr and `.btf`, not through any web domain. +Pool operators also write a local `pool_status.json` every ten seconds and a +`pool_rounds.json` proof ledger whenever they find blocks or pay miners. By +default both are created in the data directory; use `-poolstatusfile=PATH` and +`-poolroundsfile=PATH` to write them somewhere a dashboard or web sync can read. +The files are informational only: nodes still discover pools through Nostr and +`.btf`, not through any web domain. **Participant** — mine to someone else's pool. Enter or select the pool's `.btf` address and enable Start Mining. @@ -243,7 +245,9 @@ xmrig -a rx/0 -o 127.0.0.1:3333 -u YOUR_BTF_ADDRESS -p x ./bitflash -nogui # node only ./bitflash -nogui -gen # node + solo mining ./bitflash -nogui -gen -operator # pool operator -./bitflash -nogui -gen -operator -poolstatusfile=/var/www/api/pools.json +./bitflash -nogui -gen -operator \ + -poolstatusfile=/var/www/pool_status.json \ + -poolroundsfile=/var/www/pool_rounds.json ./bitflash -nogui -gen -participant=POOL_BTF_ADDRESS # mine to pool ./bitflash -nogui -stratumbridge=POOL_BTF_ADDRESS # local bridge for XMRig/SRBMiner ``` diff --git a/docs/pool-directory.md b/docs/pool-directory.md index 6456084..76269b9 100644 --- a/docs/pool-directory.md +++ b/docs/pool-directory.md @@ -4,10 +4,13 @@ Nodes discover pools through Nostr and `.btf`; the website only helps humans compare operators. -Operators can publish the `pool_status.json` file produced by: +Operators can publish the `pool_status.json` and `pool_rounds.json` files +produced by: ```bash -bitflash -nogui -gen -operator -poolstatusfile=/var/www/pool_status.json +bitflash -nogui -gen -operator \ + -poolstatusfile=/var/www/pool_status.json \ + -poolroundsfile=/var/www/pool_rounds.json ``` Build a static directory from one or more status files: @@ -23,6 +26,11 @@ The generated output contains: - `index.html`: human-readable ranking page. - `pools.json`: machine-readable API for `api.bitflash.network/pools.json`. +When a source is an HTTP(S) `pool_status.json`, the directory links to a +`pool_rounds.json` proof file next to it. That file records the block height, +block hash, coinbase txid, coinbase value, pool fee, total shares, per-address +share counts, computed payouts, and payout txids once paid. + Ranking order is intentionally simple: 1. online pools first; diff --git a/scripts/build-pool-index.py b/scripts/build-pool-index.py index 382b6c3..d3eb386 100644 --- a/scripts/build-pool-index.py +++ b/scripts/build-pool-index.py @@ -8,6 +8,7 @@ import html import json import os import time +import urllib.parse import urllib.request @@ -23,6 +24,10 @@ def normalize_pool(source, obj, now): operator = obj.get("operator", {}) pool = obj.get("pool", {}) node = obj.get("node", {}) + rounds_file = operator.get("roundsFile") or "pool_rounds.json" + rounds_url = "" + if source.startswith("http://") or source.startswith("https://"): + rounds_url = urllib.parse.urljoin(source, rounds_file) updated = int(obj.get("updatedAt") or 0) age = max(0, now - updated) if updated else None online = bool(node.get("poolRunning")) and age is not None and age <= 120 @@ -39,6 +44,8 @@ def normalize_pool(source, obj, now): "blocksFoundSession": int(pool.get("blocksFoundSession") or 0), "roundShares": int(pool.get("roundShares") or 0), "hashRate": float(pool.get("hashRate") or 0.0), + "roundsUrl": rounds_url, + "recentRounds": obj.get("recentRounds", []), } @@ -56,6 +63,7 @@ def render_html(pools, generated_at): rows = [] for p in pools: dash = p["dashboardUrl"] + rounds = p.get("roundsUrl", "") # Only linkify http(s). The dashboard URL comes from an operator's # self-published status file, so a "javascript:" or "data:" scheme # would become a clickable script-injection on this public page. @@ -65,6 +73,12 @@ def render_html(pools, generated_at): html.escape(dash, quote=True)) else: dash_html = "-" + rounds_lower = rounds.lower() + if rounds_lower.startswith("http://") or rounds_lower.startswith("https://"): + rounds_html = 'proof'.format( + html.escape(rounds, quote=True)) + else: + rounds_html = "-" rows.append( "" "{status}" @@ -76,6 +90,7 @@ def render_html(pools, generated_at): "{height}" "{age}" "{dash}" + "{rounds}" "".format( status="online" if p["online"] else "stale", name=html.escape(p["name"]), @@ -87,6 +102,7 @@ def render_html(pools, generated_at): height=p["height"], age="{}s".format(p["ageSeconds"]) if p["ageSeconds"] is not None else "-", dash=dash_html, + rounds=rounds_html, ) ) @@ -110,7 +126,7 @@ def render_html(pools, generated_at):

This directory is informational. Bitflash nodes discover pools through Nostr and .btf; this page only helps humans compare public operators.

- + {rows} diff --git a/src/main.cpp b/src/main.cpp index 2be9c54..2eb0e3a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -105,6 +105,7 @@ string strParticipantPool; // participant mode: pool .btf address string strPoolName = "Bitflash Pool"; string strPoolDashboardUrl; string strPoolStatusFile; +string strPoolRoundsFile; double dPoolFeePercent = 0.0; bool fStratumBridge = false; int nStratumBridgePort = 3333; diff --git a/src/main.h b/src/main.h index 2786a4a..eafbf16 100644 --- a/src/main.h +++ b/src/main.h @@ -87,6 +87,7 @@ extern string strParticipantPool; // participant: pool .btf address extern string strPoolName; // operator: announced pool name extern string strPoolDashboardUrl; // operator: optional dashboard URL extern string strPoolStatusFile; // operator: optional JSON status output path +extern string strPoolRoundsFile; // operator: optional public round proof path extern double dPoolFeePercent; // operator: announced fee percent extern bool fStratumBridge; // participant helper: local Stratum bridge extern int nStratumBridgePort; // local bridge listen port, default 3333 diff --git a/src/main_gui.cpp b/src/main_gui.cpp index ce6d2b5..7ad2075 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -126,6 +126,7 @@ static void PrintUsage() printf(" /poolfee=PCT\n"); printf(" /pooldashboard=URL (alias: /pooldash=URL)\n"); printf(" /poolstatusfile=PATH (write pool status JSON for dashboards)\n"); + printf(" /poolroundsfile=PATH (write public pool round proofs JSON)\n"); printf("\n"); printf(".btf and rendezvous:\n"); printf(" /connectbtf=PEER_BTF_ADDRESS\n"); @@ -262,6 +263,10 @@ static void ParseStartupArguments(int argc, char* argv[]) if (!poolStatusFile.empty()) strPoolStatusFile = poolStatusFile; + string poolRoundsFile = argval2(argc, argv, "/poolroundsfile", "-poolroundsfile"); + if (!poolRoundsFile.empty()) + strPoolRoundsFile = poolRoundsFile; + string btfConnect = argval2(argc, argv, "/connectbtf", "-connectbtf"); if (!btfConnect.empty()) strBtfConnect = btfConnect; diff --git a/src/readme.txt b/src/readme.txt index fa58b6e..b8b89c1 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -55,6 +55,10 @@ Command-line options (in addition to the original ones) /poolfee=PCT operator announcement fee percent (example: 0.75) /pooldashboard=URL operator announcement dashboard URL + /poolstatusfile=PATH + write pool status JSON for dashboards + /poolroundsfile=PATH + write public pool round proofs JSON /connectbtf=ADDR keep an outbound .btf connection to ADDR /rvrelay=HOST:PORT diff --git a/src/rpc.cpp b/src/rpc.cpp index 1aa4f9b..813f1d1 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -263,11 +263,47 @@ struct PendingPayout { static std::vector gPendingPayouts; static std::mutex gPayoutLedgerMutex; +struct PoolRoundProof { + std::string id; + int blockHeight; + int matureAtHeight; + int64 createdAt; + std::string blockHash; + std::string coinbaseTxid; + std::string foundBy; + double feePercent; + uint64 totalShares; + int64 coinbaseValue; + int64 subsidy; + int64 minerReward; + int64 operatorReward; + int skippedDust; + int64 forfeitedBadAddress; + std::map shares; + std::map payouts; + std::map paidTxids; + + PoolRoundProof() + : blockHeight(0), matureAtHeight(0), createdAt(0), feePercent(0.0), + totalShares(0), coinbaseValue(0), subsidy(0), minerReward(0), operatorReward(0), + skippedDust(0), forfeitedBadAddress(0) {} +}; +static std::vector gPoolRounds; +static std::mutex gPoolRoundsMutex; +static const size_t MAX_POOL_ROUNDS = 200; + static std::string PayoutFilePath() { return GetAppDir() + "/pending_payouts.json"; } +static std::string PoolRoundsFilePath() +{ + if (!strPoolRoundsFile.empty()) + return strPoolRoundsFile; + return GetAppDir() + "/pool_rounds.json"; +} + static std::string PayoutIdForBlock(int blockHeight) { return strprintf("block-%d", blockHeight); @@ -362,6 +398,167 @@ static void LoadPendingPayouts() } } +static nlohmann::json PoolRoundToJson(const PoolRoundProof& r) +{ + nlohmann::json j; + j["id"] = r.id; + j["blockHeight"] = r.blockHeight; + j["matureAtHeight"] = r.matureAtHeight; + j["createdAt"] = r.createdAt; + j["blockHash"] = r.blockHash; + j["coinbaseTxid"] = r.coinbaseTxid; + j["foundBy"] = r.foundBy; + j["feePercent"] = r.feePercent; + j["totalShares"] = r.totalShares; + j["coinbaseValueSatoshis"] = r.coinbaseValue; + j["coinbaseValueBtf"] = FormatMoney(r.coinbaseValue); + j["subsidySatoshis"] = r.subsidy; + j["subsidyBtf"] = FormatMoney(r.subsidy); + j["minerRewardSatoshis"] = r.minerReward; + j["minerRewardBtf"] = FormatMoney(r.minerReward); + j["operatorRewardSatoshis"] = r.operatorReward; + j["operatorRewardBtf"] = FormatMoney(r.operatorReward); + j["skippedDust"] = r.skippedDust; + j["forfeitedBadAddressSatoshis"] = r.forfeitedBadAddress; + j["forfeitedBadAddressBtf"] = FormatMoney(r.forfeitedBadAddress); + + j["shares"] = nlohmann::json::object(); + for (const auto& kv : r.shares) + j["shares"][kv.first] = kv.second; + + j["payouts"] = nlohmann::json::object(); + for (const auto& kv : r.payouts) { + nlohmann::json p; + p["amountSatoshis"] = kv.second; + p["amountBtf"] = FormatMoney(kv.second); + std::map::const_iterator tx = r.paidTxids.find(kv.first); + if (tx != r.paidTxids.end()) + p["txid"] = tx->second; + else + p["txid"] = nullptr; + j["payouts"][kv.first] = p; + } + return j; +} + +static void SavePoolRoundsLocked() +{ + nlohmann::json root; + root["schema"] = "bitflash-pool-rounds-1"; + root["updatedAt"] = GetTime(); + root["rounds"] = nlohmann::json::array(); + for (const PoolRoundProof& r : gPoolRounds) + root["rounds"].push_back(PoolRoundToJson(r)); + + std::string path = PoolRoundsFilePath(); + std::string tmp = path + ".tmp"; + FILE* f = fopen(tmp.c_str(), "w"); + if (!f) { + LogPrint("payout", "[payout] WARNING: could not write %s\n", tmp.c_str()); + return; + } + std::string s = root.dump(2); + fwrite(s.c_str(), 1, s.size(), f); + if (fclose(f) != 0) { + LogPrint("payout", "[payout] WARNING: could not close %s\n", tmp.c_str()); + return; + } +#ifdef _WIN32 + remove(path.c_str()); +#endif + if (rename(tmp.c_str(), path.c_str()) != 0) + LogPrint("payout", "[payout] WARNING: could not replace %s\n", path.c_str()); +} + +static void LoadPoolRounds() +{ + std::string path = PoolRoundsFilePath(); + FILE* f = fopen(path.c_str(), "r"); + if (!f) return; + fseek(f, 0, SEEK_END); + long sz = ftell(f); + fseek(f, 0, SEEK_SET); + std::string s(sz, '\0'); + fread(&s[0], 1, sz, f); + fclose(f); + + try { + nlohmann::json root = nlohmann::json::parse(s); + nlohmann::json arr = root.is_array() ? root : root["rounds"]; + std::lock_guard lk(gPoolRoundsMutex); + for (const auto& obj : arr) { + PoolRoundProof r; + if (obj.contains("id") && obj["id"].is_string()) r.id = obj["id"].get(); + if (obj.contains("blockHeight")) r.blockHeight = obj["blockHeight"].get(); + if (obj.contains("matureAtHeight")) r.matureAtHeight = obj["matureAtHeight"].get(); + if (obj.contains("createdAt")) r.createdAt = obj["createdAt"].get(); + if (obj.contains("blockHash") && obj["blockHash"].is_string()) r.blockHash = obj["blockHash"].get(); + if (obj.contains("coinbaseTxid") && obj["coinbaseTxid"].is_string()) r.coinbaseTxid = obj["coinbaseTxid"].get(); + if (obj.contains("foundBy") && obj["foundBy"].is_string()) r.foundBy = obj["foundBy"].get(); + if (obj.contains("feePercent")) r.feePercent = obj["feePercent"].get(); + if (obj.contains("totalShares")) r.totalShares = obj["totalShares"].get(); + if (obj.contains("coinbaseValueSatoshis")) r.coinbaseValue = obj["coinbaseValueSatoshis"].get(); + if (obj.contains("subsidySatoshis")) r.subsidy = obj["subsidySatoshis"].get(); + if (obj.contains("minerRewardSatoshis")) r.minerReward = obj["minerRewardSatoshis"].get(); + if (obj.contains("operatorRewardSatoshis")) r.operatorReward = obj["operatorRewardSatoshis"].get(); + if (obj.contains("skippedDust")) r.skippedDust = obj["skippedDust"].get(); + if (obj.contains("forfeitedBadAddressSatoshis")) r.forfeitedBadAddress = obj["forfeitedBadAddressSatoshis"].get(); + if (obj.contains("shares") && obj["shares"].is_object()) + for (auto it = obj["shares"].begin(); it != obj["shares"].end(); ++it) + r.shares[it.key()] = it.value().get(); + if (obj.contains("payouts") && obj["payouts"].is_object()) + for (auto it = obj["payouts"].begin(); it != obj["payouts"].end(); ++it) { + if (it.value().is_object()) { + r.payouts[it.key()] = it.value().value("amountSatoshis", (int64)0); + if (it.value().contains("txid") && it.value()["txid"].is_string()) + r.paidTxids[it.key()] = it.value()["txid"].get(); + } else if (it.value().is_number_integer()) { + r.payouts[it.key()] = it.value().get(); + } + } + if (!r.id.empty()) + gPoolRounds.push_back(r); + } + while (gPoolRounds.size() > MAX_POOL_ROUNDS) + gPoolRounds.erase(gPoolRounds.begin()); + LogPrint("payout", "[payout] loaded %zu pool round proof(s) from disk\n", + gPoolRounds.size()); + } catch (const std::exception& e) { + LogPrint("payout", "[payout] WARNING: failed to parse %s: %s\n", + path.c_str(), e.what()); + } +} + +static void RecordPoolRound(const PoolRoundProof& round) +{ + std::lock_guard lk(gPoolRoundsMutex); + for (PoolRoundProof& existing : gPoolRounds) { + if (existing.id == round.id) { + existing = round; + SavePoolRoundsLocked(); + return; + } + } + gPoolRounds.push_back(round); + while (gPoolRounds.size() > MAX_POOL_ROUNDS) + gPoolRounds.erase(gPoolRounds.begin()); + SavePoolRoundsLocked(); +} + +static void MarkPoolRoundPaid(const std::string& id, + const std::string& address, + const std::string& txid) +{ + std::lock_guard lk(gPoolRoundsMutex); + for (PoolRoundProof& r : gPoolRounds) { + if (r.id != id) + continue; + r.paidTxids[address] = txid; + SavePoolRoundsLocked(); + return; + } +} + // Payouts ready to execute are moved here under gPayoutExecMutex. // A dedicated thread drains this list so SendMoney (which takes cs_main) // never runs on the event loop thread and can't stall miner I/O. @@ -399,10 +596,28 @@ static bool FindRecordedPoolPayout(const std::string& id, } static void QueuePayouts(int blockHeight, + const std::string& blockHash, + const std::string& coinbaseTxid, + int64 coinbaseValue, + const std::string& foundBy, const std::map& shareCount, uint64 total) { + PoolRoundProof round; + round.id = PayoutIdForBlock(blockHeight); + round.blockHeight = blockHeight; + round.matureAtHeight = blockHeight + COINBASE_MATURITY; + round.createdAt = GetTime(); + round.blockHash = blockHash; + round.coinbaseTxid = coinbaseTxid; + round.coinbaseValue = coinbaseValue; + round.foundBy = foundBy; + round.feePercent = dPoolFeePercent; + round.totalShares = total; + round.shares = shareCount; + if (total == 0) { + RecordPoolRound(round); LogPrint("payout", "[payout] block %d: zero shares recorded -- nothing to pay out\n", blockHeight); return; @@ -418,9 +633,13 @@ static void QueuePayouts(int blockHeight, int64 nSubsidy = 50 * COIN; nSubsidy >>= (blockHeight / 210000); // halving schedule int64 minerReward = (int64)((double)nSubsidy * (1.0 - feeFrac)); + int64 operatorCut = nSubsidy - minerReward; + round.subsidy = nSubsidy; + round.minerReward = minerReward; + round.operatorReward = operatorCut; PendingPayout pp; - pp.id = PayoutIdForBlock(blockHeight); + pp.id = round.id; pp.blockHeight = blockHeight; pp.matureAtHeight = blockHeight + COINBASE_MATURITY; @@ -443,15 +662,18 @@ static void QueuePayouts(int blockHeight, continue; } pp.amounts[kv.first] = amount; + round.payouts[kv.first] = amount; } + round.skippedDust = skipped; + round.forfeitedBadAddress = burned; { std::lock_guard lk(gPayoutLedgerMutex); gPendingPayouts.push_back(pp); SavePendingPayoutsLocked(); } + RecordPoolRound(round); - int64 operatorCut = nSubsidy - minerReward; LogPrint("payout", "[payout] block %d: subsidy %s BTF, miners get %s" " (fee %.2f%% = %s to operator)," @@ -548,12 +770,14 @@ static void PayoutThreadFn(void*) LogPrint("payout", "[payout] recovered recorded payout %s to %s" " from wallet tx %s\n", FormatMoney(kv.second).c_str(), kv.first.c_str(), txid.c_str()); + MarkPoolRoundPaid(ppQueued.id, kv.first, txid); } else if (SendMoney(sc, kv.second, wtx)) { txid = wtx.GetHash().GetHex(); LogPrint("payout", "[payout] paid %s to %s\n", FormatMoney(kv.second).c_str(), kv.first.c_str()); + MarkPoolRoundPaid(ppQueued.id, kv.first, txid); } else { @@ -664,7 +888,8 @@ static void WritePoolStatusJsonLocked() {"name", strPoolName}, {"btfAddress", BtfLocalAddress()}, {"feePercent", dPoolFeePercent}, - {"dashboardUrl", strPoolDashboardUrl} + {"dashboardUrl", strPoolDashboardUrl}, + {"roundsFile", "pool_rounds.json"} }; j["node"] = { @@ -702,6 +927,30 @@ static void WritePoolStatusJsonLocked() }); } + j["recentRounds"] = nlohmann::json::array(); + { + std::lock_guard lk(gPoolRoundsMutex); + size_t start = gPoolRounds.size() > 10 ? gPoolRounds.size() - 10 : 0; + for (size_t i = start; i < gPoolRounds.size(); i++) { + const PoolRoundProof& r = gPoolRounds[i]; + int unpaid = 0; + for (const auto& kv : r.payouts) + if (r.paidTxids.count(kv.first) == 0) + unpaid++; + j["recentRounds"].push_back({ + {"id", r.id}, + {"blockHeight", r.blockHeight}, + {"blockHash", r.blockHash}, + {"coinbaseTxid", r.coinbaseTxid}, + {"totalShares", r.totalShares}, + {"payouts", (int)r.payouts.size()}, + {"unpaidPayouts", unpaid}, + {"coinbaseValueBtf", FormatMoney(r.coinbaseValue)}, + {"minerRewardBtf", FormatMoney(r.minerReward)} + }); + } + } + std::string path = PoolStatusFilePath(); std::string tmp = path + ".tmp"; FILE* f = fopen(tmp.c_str(), "w"); @@ -1231,7 +1480,9 @@ static bool HandleLine(Miner* m, const std::string& rawLine, if (accepted) { // Snapshot height before RebuildJob() (which increments it) int foundHeight = gCurrentJob.height; - QueuePayouts(foundHeight, roundShareCount, roundShareTotal); + QueuePayouts(foundHeight, b.GetHash().GetHex(), b.vtx[0].GetHash().GetHex(), + b.vtx[0].vout[0].nValue, m->address, + roundShareCount, roundShareTotal); // Reset round shares roundShareCount.clear(); roundShareTotal = 0; @@ -1333,6 +1584,62 @@ int RunPoolStratumSelfTest() HandleLine(&miner, stale.dump(), roundShareCount, roundShareTotal, blocksFound); check(roundShareTotal == 1, "stale submit does not increment shares"); + std::vector savedRounds = gPoolRounds; + std::string savedRoundsFile = strPoolRoundsFile; + std::string proofPath = "pool_rounds_selftest.json"; + remove(proofPath.c_str()); + remove((proofPath + ".tmp").c_str()); + gPoolRounds.clear(); + strPoolRoundsFile = proofPath; + + PoolRoundProof proof; + proof.id = "selftest-round"; + proof.blockHeight = 42; + proof.matureAtHeight = 42 + COINBASE_MATURITY; + proof.createdAt = GetTime(); + proof.blockHash = block.GetHash().GetHex(); + proof.coinbaseTxid = coinbase.GetHash().GetHex(); + proof.foundBy = miner.address; + proof.feePercent = 1.25; + proof.totalShares = 3; + proof.coinbaseValue = 50 * COIN; + proof.subsidy = 50 * COIN; + proof.minerReward = 49 * COIN; + proof.operatorReward = 1 * COIN; + proof.shares[miner.address] = 3; + proof.payouts[miner.address] = 49 * COIN; + RecordPoolRound(proof); + MarkPoolRoundPaid(proof.id, miner.address, "selftest-txid"); + + bool proofOk = false; + FILE* pf = fopen(proofPath.c_str(), "r"); + if (pf) { + fseek(pf, 0, SEEK_END); + long sz = ftell(pf); + fseek(pf, 0, SEEK_SET); + std::string s(sz, '\0'); + fread(&s[0], 1, sz, pf); + fclose(pf); + try { + json root = json::parse(s); + proofOk = + root.value("schema", "") == "bitflash-pool-rounds-1" && + root["rounds"].is_array() && + root["rounds"].size() == 1 && + root["rounds"][0].value("id", "") == proof.id && + root["rounds"][0]["shares"].value(miner.address, (uint64)0) == 3 && + root["rounds"][0]["payouts"][miner.address].value("txid", "") == "selftest-txid"; + } catch (...) { + proofOk = false; + } + } + check(proofOk, "pool round proof ledger is written and paid txid is recorded"); + + remove(proofPath.c_str()); + remove((proofPath + ".tmp").c_str()); + strPoolRoundsFile = savedRoundsFile; + gPoolRounds = savedRounds; + gCurrentJob = savedJob; gHaveJob = savedHaveJob; @@ -1754,6 +2061,7 @@ void ThreadRPCServer(void*) } LoadPendingPayouts(); + LoadPoolRounds(); _beginthread(AcceptLoopFn, 0, NULL); _beginthread(ThreadBtfPoolAnnouncer, 0, NULL); -- cgit v1.2.3
StatusPoolFeeMinersH/sBlocksHeightAgeLink
StatusPoolFeeMinersH/sBlocksHeightAgeLinkProof