diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 51 | ||||
| -rw-r--r-- | src/btfaddr.cpp | 40 | ||||
| -rw-r--r-- | src/btfrv.cpp | 106 | ||||
| -rw-r--r-- | src/btfrv.h | 19 | ||||
| -rw-r--r-- | src/btftunnel.cpp | 140 | ||||
| -rw-r--r-- | src/btftunnel.h | 38 | ||||
| -rw-r--r-- | src/compactblock.cpp | 175 | ||||
| -rw-r--r-- | src/compactblock.h | 113 | ||||
| -rw-r--r-- | src/db.cpp | 16 | ||||
| -rw-r--r-- | src/db.h | 23 | ||||
| -rw-r--r-- | src/gui.cpp | 34 | ||||
| -rw-r--r-- | src/headers_core.h | 1 | ||||
| -rw-r--r-- | src/main.cpp | 1177 | ||||
| -rw-r--r-- | src/main.h | 88 | ||||
| -rw-r--r-- | src/main_gui.cpp | 154 | ||||
| -rw-r--r-- | src/makefile.mingw | 4 | ||||
| -rw-r--r-- | src/market.cpp | 264 | ||||
| -rw-r--r-- | src/market.h | 182 | ||||
| -rw-r--r-- | src/net.cpp | 244 | ||||
| -rw-r--r-- | src/net.h | 280 | ||||
| -rw-r--r-- | src/nostr.cpp | 150 | ||||
| -rw-r--r-- | src/nostr.h | 11 | ||||
| -rw-r--r-- | src/randomx_pow.cpp | 28 | ||||
| -rw-r--r-- | src/readme.txt | 2 | ||||
| -rw-r--r-- | src/rpc.cpp | 4317 | ||||
| -rw-r--r-- | src/selftest.cpp | 225 | ||||
| -rw-r--r-- | src/test_btfaddr.cpp | 19 | ||||
| -rw-r--r-- | src/test_compactblock.cpp | 133 | ||||
| -rw-r--r-- | src/test_rvaccept.cpp | 180 | ||||
| -rw-r--r-- | src/tor.cpp | 25 | ||||
| -rw-r--r-- | src/util.cpp | 153 | ||||
| -rw-r--r-- | src/util.h | 128 |
32 files changed, 7214 insertions, 1306 deletions
diff --git a/src/Makefile b/src/Makefile index ab0b307..c997ae3 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -6,7 +6,9 @@ FUZZ_CXX ?= clang++ | |||
| 6 | 6 | ||
| 7 | SECP_INC ?= /usr/local/include | 7 | SECP_INC ?= /usr/local/include |
| 8 | SECP_LIB ?= /usr/local/lib | 8 | SECP_LIB ?= /usr/local/lib |
| 9 | RANDOMX_DIR ?= $(HOME)/RandomX | 9 | # The root Makefile exports RANDOMX_DIR (default: <repo>/randomx-build, built |
| 10 | # by deps-randomx). This fallback only applies when src/ is built standalone. | ||
| 11 | RANDOMX_DIR ?= $(abspath $(CURDIR)/..)/randomx-build | ||
| 10 | 12 | ||
| 11 | IMGUI_DIR = imgui | 13 | IMGUI_DIR = imgui |
| 12 | 14 | ||
| @@ -52,17 +54,18 @@ LIBS = -L$(SECP_LIB) -lsecp256k1 \ | |||
| 52 | # binary. Nothing crashed here, and nothing would say so if it had. | 54 | # binary. Nothing crashed here, and nothing would say so if it had. |
| 53 | HEADERS = headers_core.h headers.h util.h main.h serialize.h uint256.h \ | 55 | HEADERS = headers_core.h headers.h util.h main.h serialize.h uint256.h \ |
| 54 | key.h bignum.h script.h db.h base58.h net.h nostr.h \ | 56 | key.h bignum.h script.h db.h base58.h net.h nostr.h \ |
| 55 | randomx_pow.h btfaddr.h btfchan.h btfrv.h btftunnel.h market.h \ | 57 | randomx_pow.h btfaddr.h btfchan.h btfrv.h btftunnel.h \ |
| 56 | selftest.h walletcmd.h wallet_sqlite.h compat.h sockcount.h proxy.h tor.h font_roboto.h | 58 | selftest.h walletcmd.h wallet_sqlite.h compat.h sockcount.h proxy.h tor.h font_roboto.h \ |
| 59 | compactblock.h | ||
| 57 | 60 | ||
| 58 | IMGUI_OBJS = obj/imgui.o obj/imgui_draw.o obj/imgui_tables.o \ | 61 | IMGUI_OBJS = obj/imgui.o obj/imgui_draw.o obj/imgui_tables.o \ |
| 59 | obj/imgui_widgets.o obj/imgui_impl_glfw.o obj/imgui_impl_opengl3.o | 62 | obj/imgui_widgets.o obj/imgui_impl_glfw.o obj/imgui_impl_opengl3.o |
| 60 | 63 | ||
| 61 | NODE_OBJS = obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o \ | 64 | NODE_OBJS = obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o \ |
| 62 | obj/market.o obj/sha.o obj/nostr.o obj/randomx_pow.o \ | 65 | obj/sha.o obj/nostr.o obj/randomx_pow.o \ |
| 63 | obj/btfaddr.o obj/btfchan.o obj/btfrv.o obj/btftunnel.o obj/rpc.o \ | 66 | obj/btfaddr.o obj/btfchan.o obj/btfrv.o obj/btftunnel.o obj/rpc.o \ |
| 64 | obj/selftest.o obj/bip32.o obj/walletcmd.o obj/wallet_sqlite.o \ | 67 | obj/selftest.o obj/bip32.o obj/walletcmd.o obj/wallet_sqlite.o \ |
| 65 | obj/proxy.o obj/tor.o | 68 | obj/proxy.o obj/tor.o obj/compactblock.o |
| 66 | 69 | ||
| 67 | GUI_OBJS = obj/gui.o obj/main_gui.o | 70 | GUI_OBJS = obj/gui.o obj/main_gui.o |
| 68 | 71 | ||
| @@ -101,7 +104,7 @@ obj/imgui_impl_opengl3.o: $(IMGUI_DIR)/imgui_impl_opengl3.cpp | |||
| 101 | $(CXX) -c $(CXXFLAGS) -o $@ $< | 104 | $(CXX) -c $(CXXFLAGS) -o $@ $< |
| 102 | 105 | ||
| 103 | bitflash: $(IMGUI_OBJS) $(NODE_OBJS) $(GUI_OBJS) | 106 | bitflash: $(IMGUI_OBJS) $(NODE_OBJS) $(GUI_OBJS) |
| 104 | $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS) | 107 | $(CXX) $(CXXFLAGS) $(LDHARDFLAGS) -o $@ $^ $(LIBS) |
| 105 | @echo "Built: bitflash" | 108 | @echo "Built: bitflash" |
| 106 | 109 | ||
| 107 | # ---- headless node --------------------------------------------------------- | 110 | # ---- headless node --------------------------------------------------------- |
| @@ -115,6 +118,20 @@ bitflash: $(IMGUI_OBJS) $(NODE_OBJS) $(GUI_OBJS) | |||
| 115 | # Objects go to obj-node/ because main_gui.cpp is compiled twice, with and | 118 | # Objects go to obj-node/ because main_gui.cpp is compiled twice, with and |
| 116 | # without -DBITFLASH_NO_GUI, and the two must not overwrite each other. | 119 | # without -DBITFLASH_NO_GUI, and the two must not overwrite each other. |
| 117 | 120 | ||
| 121 | # RandomX links a hand-written assembly JIT stub, jit_compiler_x86_static.S, | ||
| 122 | # which carries no .note.GNU-stack marker. Older binutils -- Debian 12's, which | ||
| 123 | # is what builds against the oldest glibc we ship to -- reads a missing marker | ||
| 124 | # as "this object wants an executable stack" and marks the whole binary RWE, | ||
| 125 | # switching NX off for the process. Newer toolchains default the other way, so | ||
| 126 | # identical source produced GNU_STACK RW on Ubuntu 26.04 and RWE on bookworm, | ||
| 127 | # announced by nothing louder than a linker warning in the middle of the build. | ||
| 128 | # | ||
| 129 | # Assert it here rather than inherit whichever default the build host happens | ||
| 130 | # to carry. The JIT maps its own W^X pages and never executes from the stack, | ||
| 131 | # so nothing needs the permission this takes away. Check with: | ||
| 132 | # readelf -lW bitflash-node | grep -A1 GNU_STACK # expect RW, not RWE | ||
| 133 | LDHARDFLAGS = -Wl,-z,noexecstack | ||
| 134 | |||
| 118 | NODE_LIBS = -L$(SECP_LIB) -lsecp256k1 \ | 135 | NODE_LIBS = -L$(SECP_LIB) -lsecp256k1 \ |
| 119 | -L$(RANDOMX_DIR)/build -lrandomx \ | 136 | -L$(RANDOMX_DIR)/build -lrandomx \ |
| 120 | -lsodium -lssl -lcrypto -ldb_cxx -lsqlite3 -lpthread | 137 | -lsodium -lssl -lcrypto -ldb_cxx -lsqlite3 -lpthread |
| @@ -135,7 +152,7 @@ obj-node/sha.o: sha.cpp sha.h | |||
| 135 | $(CXX) -c $(CXXFLAGS) -DBITFLASH_NO_GUI -O3 -o $@ $< | 152 | $(CXX) -c $(CXXFLAGS) -DBITFLASH_NO_GUI -O3 -o $@ $< |
| 136 | 153 | ||
| 137 | bitflash-node: $(NODE_ONLY_OBJS) | 154 | bitflash-node: $(NODE_ONLY_OBJS) |
| 138 | $(CXX) $(CXXFLAGS) -o $@ $^ $(NODE_LIBS) | 155 | $(CXX) $(CXXFLAGS) $(LDHARDFLAGS) -o $@ $^ $(NODE_LIBS) |
| 139 | @echo "Built: bitflash-node (headless, no libGL needed)" | 156 | @echo "Built: bitflash-node (headless, no libGL needed)" |
| 140 | 157 | ||
| 141 | obj-fuzz/%.o: %.cpp $(HEADERS) | 158 | obj-fuzz/%.o: %.cpp $(HEADERS) |
| @@ -178,13 +195,28 @@ fuzz-script-smoke: fuzz-script | |||
| 178 | ./fuzz-script -runs=128 ../work/fuzz-corpus/script | 195 | ./fuzz-script -runs=128 ../work/fuzz-corpus/script |
| 179 | 196 | ||
| 180 | clean: | 197 | clean: |
| 181 | rm -rf obj obj-node obj-fuzz bitflash bitflash-node fuzz-net-message fuzz-script test_bip32 | 198 | rm -rf obj obj-node obj-fuzz bitflash bitflash-node fuzz-net-message fuzz-script test_bip32 test_compactblock |
| 182 | 199 | ||
| 183 | test_bip32: test_bip32.cpp bip32.cpp bip32.h bip39_english.h | 200 | test_bip32: test_bip32.cpp bip32.cpp bip32.h bip39_english.h |
| 184 | $(CXX) $(CXXFLAGS) -o $@ test_bip32.cpp bip32.cpp -lssl -lcrypto | 201 | $(CXX) $(CXXFLAGS) -o $@ test_bip32.cpp bip32.cpp -lssl -lcrypto |
| 185 | 202 | ||
| 186 | tests: test_bip32 bitflash-node | 203 | # Upstream shipped test_compactblock.cpp with no build rule at all, so it was |
| 204 | # never compiled there. It links the standalone way test_bip32 does: it supplies | ||
| 205 | # its own mapTransactions and GetAppDir so main.cpp stays out of the link, and | ||
| 206 | # only needs sha.cpp/util.cpp for Hash() and the logging helpers. | ||
| 207 | # | ||
| 208 | # TEST_LIBS is overridable for the same reason NODE_LIBS is: the pinned dep tree | ||
| 209 | # ships libsodium as a static archive only, so a build against it passes | ||
| 210 | # TEST_LIBS="... -l:libsodium.a ...". | ||
| 211 | TEST_LIBS ?= -lsodium -lssl -lcrypto -lpthread | ||
| 212 | |||
| 213 | test_compactblock: test_compactblock.cpp compactblock.cpp compactblock.h $(HEADERS) | ||
| 214 | $(CXX) $(CXXFLAGS) -o $@ test_compactblock.cpp compactblock.cpp sha.cpp util.cpp \ | ||
| 215 | $(TEST_LIBS) | ||
| 216 | |||
| 217 | tests: test_bip32 test_compactblock bitflash-node | ||
| 187 | ./test_bip32 | 218 | ./test_bip32 |
| 219 | ./test_compactblock | ||
| 188 | ./bitflash-node -selftest=wallet-keypool | 220 | ./bitflash-node -selftest=wallet-keypool |
| 189 | ./bitflash-node -selftest=wallet-hd | 221 | ./bitflash-node -selftest=wallet-hd |
| 190 | ./bitflash-node -selftest=wallet-format | 222 | ./bitflash-node -selftest=wallet-format |
| @@ -196,6 +228,7 @@ tests: test_bip32 bitflash-node | |||
| 196 | ./bitflash-node -selftest=network-params | 228 | ./bitflash-node -selftest=network-params |
| 197 | ./bitflash-node -selftest=consensus-limits | 229 | ./bitflash-node -selftest=consensus-limits |
| 198 | ./bitflash-node -selftest=pool-stratum | 230 | ./bitflash-node -selftest=pool-stratum |
| 231 | ./bitflash-node -selftest=payout-ledger | ||
| 199 | ./bitflash-node -selftest=parse-money | 232 | ./bitflash-node -selftest=parse-money |
| 200 | ./bitflash-node -selftest=socks5-proxy | 233 | ./bitflash-node -selftest=socks5-proxy |
| 201 | ./bitflash-node -selftest=managed-tor | 234 | ./bitflash-node -selftest=managed-tor |
diff --git a/src/btfaddr.cpp b/src/btfaddr.cpp index ce2ac86..502220e 100644 --- a/src/btfaddr.cpp +++ b/src/btfaddr.cpp | |||
| @@ -319,20 +319,32 @@ bool VerifyDescriptor(void* ctxv, const std::string& jsonStr, | |||
| 319 | std::string onion; | 319 | std::string onion; |
| 320 | if (v.contains("onion")) | 320 | if (v.contains("onion")) |
| 321 | { | 321 | { |
| 322 | if (v["onion"].is_string() && v.contains("sig3") && v["sig3"].is_string()) | 322 | // A descriptor that carries an onion endpoint must prove it. The |
| 323 | { | 323 | // endpoint is covered by sig3 and only sig3, so any failure here -- |
| 324 | std::string onionNorm; | 324 | // missing sig3, bad hex, unnormalised endpoint, bad signature -- |
| 325 | std::string onionRaw = v["onion"].get<std::string>(); | 325 | // means the descriptor was tampered with, and a tampered |
| 326 | if (NormalizeOnionEndpoint(onionRaw, onionNorm) && onionNorm == onionRaw) | 326 | // descriptor is rejected outright. Upstream dropped just the onion |
| 327 | { | 327 | // and accepted the rest, which let any relay convert an onion |
| 328 | unsigned char sig3[64]; | 328 | // descriptor into a rendezvous-only one by deleting a field. |
| 329 | unsigned char digest3[32]; | 329 | // |
| 330 | DescriptorDigestV3(expect_pubkey, enc, meeting_node, onionNorm, created, digest3); | 330 | // Residual, by design: removing BOTH "onion" and "sig3" still |
| 331 | if (HexDecode(v["sig3"].get<std::string>(), sig3, 64) && | 331 | // verifies, because the result is indistinguishable from a peer |
| 332 | secp256k1_schnorrsig_verify(ctx, sig3, digest3, 32, &xpub)) | 332 | // that never had an onion endpoint. Closing that requires binding |
| 333 | onion = onionNorm; | 333 | // the onion into the primary sig (the V2 digest), which every |
| 334 | } | 334 | // upstream 1.2.18/1.2.19 verifier would reject -- an upstream |
| 335 | } | 335 | // protocol change, not something this fork can do unilaterally. |
| 336 | if (!v["onion"].is_string()) return false; | ||
| 337 | if (!v.contains("sig3") || !v["sig3"].is_string()) return false; | ||
| 338 | std::string onionNorm; | ||
| 339 | std::string onionRaw = v["onion"].get<std::string>(); | ||
| 340 | if (!NormalizeOnionEndpoint(onionRaw, onionNorm) || onionNorm != onionRaw) | ||
| 341 | return false; | ||
| 342 | unsigned char sig3[64]; | ||
| 343 | unsigned char digest3[32]; | ||
| 344 | DescriptorDigestV3(expect_pubkey, enc, meeting_node, onionNorm, created, digest3); | ||
| 345 | if (!HexDecode(v["sig3"].get<std::string>(), sig3, 64)) return false; | ||
| 346 | if (!secp256k1_schnorrsig_verify(ctx, sig3, digest3, 32, &xpub)) return false; | ||
| 347 | onion = onionNorm; | ||
| 336 | } | 348 | } |
| 337 | 349 | ||
| 338 | memcpy(out.pubkey, expect_pubkey, 32); | 350 | memcpy(out.pubkey, expect_pubkey, 32); |
diff --git a/src/btfrv.cpp b/src/btfrv.cpp index d4f079f..7d705ef 100644 --- a/src/btfrv.cpp +++ b/src/btfrv.cpp | |||
| @@ -40,6 +40,8 @@ typedef int SOCKET; | |||
| 40 | #include <thread> | 40 | #include <thread> |
| 41 | #include <mutex> | 41 | #include <mutex> |
| 42 | #include <atomic> | 42 | #include <atomic> |
| 43 | #include <chrono> | ||
| 44 | #include <system_error> | ||
| 43 | 45 | ||
| 44 | namespace btf | 46 | namespace btf |
| 45 | { | 47 | { |
| @@ -102,31 +104,42 @@ static SOCKET ConnectTo(const char* host, unsigned short port) | |||
| 102 | #else | 104 | #else |
| 103 | struct addrinfo hints, *res = NULL; | 105 | struct addrinfo hints, *res = NULL; |
| 104 | memset(&hints, 0, sizeof(hints)); | 106 | memset(&hints, 0, sizeof(hints)); |
| 105 | hints.ai_family = AF_INET; | 107 | hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 (some relays are v6-only) |
| 106 | hints.ai_socktype = SOCK_STREAM; | 108 | hints.ai_socktype = SOCK_STREAM; |
| 107 | char portstr[16]; sprintf(portstr, "%u", (unsigned)port); | 109 | char portstr[16]; sprintf(portstr, "%u", (unsigned)port); |
| 108 | if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET; | 110 | if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET; |
| 109 | SOCKET s = BtfSocketTag(socket(res->ai_family, res->ai_socktype, res->ai_protocol), SOCK_RV_DIAL); | ||
| 110 | if (s == INVALID_SOCKET) { freeaddrinfo(res); return INVALID_SOCKET; } | ||
| 111 | 111 | ||
| 112 | // Apply a short timeout only for the connect() and initial handshake so an | 112 | // Try each resolved address until one connects. |
| 113 | // unresponsive relay doesn't block the thread. Once the tunnel is live the | 113 | SOCKET s = INVALID_SOCKET; |
| 114 | // timeout is cleared -- a forwarding pipe must block indefinitely on recv(). | 114 | for (struct addrinfo* rp = res; rp != NULL; rp = rp->ai_next) |
| 115 | { | ||
| 116 | s = BtfSocketTag(socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol), SOCK_RV_DIAL); | ||
| 117 | if (s == INVALID_SOCKET) | ||
| 118 | continue; | ||
| 119 | |||
| 120 | // Apply a short timeout only for the connect() and initial handshake so an | ||
| 121 | // unresponsive relay doesn't block the thread. Once the tunnel is live the | ||
| 122 | // timeout is cleared -- a forwarding pipe must block indefinitely on recv(). | ||
| 115 | #ifdef _WIN32 | 123 | #ifdef _WIN32 |
| 116 | DWORD tvShort = 10000; | 124 | DWORD tvShort = 10000; |
| 117 | setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tvShort, sizeof(tvShort)); | 125 | setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tvShort, sizeof(tvShort)); |
| 118 | setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tvShort, sizeof(tvShort)); | 126 | setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tvShort, sizeof(tvShort)); |
| 119 | #else | 127 | #else |
| 120 | struct timeval tvShort; tvShort.tv_sec = 10; tvShort.tv_usec = 0; | 128 | struct timeval tvShort; tvShort.tv_sec = 10; tvShort.tv_usec = 0; |
| 121 | setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tvShort, sizeof(tvShort)); | 129 | setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&tvShort, sizeof(tvShort)); |
| 122 | setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tvShort, sizeof(tvShort)); | 130 | setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char*)&tvShort, sizeof(tvShort)); |
| 123 | #endif | 131 | #endif |
| 124 | 132 | ||
| 125 | if (connect(s, res->ai_addr, (int)res->ai_addrlen) != 0) | 133 | if (connect(s, rp->ai_addr, (int)rp->ai_addrlen) != 0) |
| 126 | { | 134 | { |
| 127 | CLOSESOCK(s); freeaddrinfo(res); return INVALID_SOCKET; | 135 | CLOSESOCK(s); |
| 136 | s = INVALID_SOCKET; | ||
| 137 | continue; | ||
| 138 | } | ||
| 139 | break; | ||
| 128 | } | 140 | } |
| 129 | freeaddrinfo(res); | 141 | freeaddrinfo(res); |
| 142 | if (s == INVALID_SOCKET) return INVALID_SOCKET; | ||
| 130 | #endif | 143 | #endif |
| 131 | 144 | ||
| 132 | // Clear the timeout -- the socket is now a live tunnel and must not | 145 | // Clear the timeout -- the socket is now a live tunnel and must not |
| @@ -198,8 +211,28 @@ static void HandleIncoming(SOCKET s) | |||
| 198 | bool a = WriteN(svc, &ok, 1); | 211 | bool a = WriteN(svc, &ok, 1); |
| 199 | bool b = WriteN(s, &ok, 1); | 212 | bool b = WriteN(s, &ok, 1); |
| 200 | if (!a || !b) { CLOSESOCK(svc); CLOSESOCK(s); return; } | 213 | if (!a || !b) { CLOSESOCK(svc); CLOSESOCK(s); return; } |
| 201 | std::thread(Forward, s, svc).detach(); | 214 | // One thread pumps s -> svc; this thread pumps svc -> s. Spawning a |
| 202 | std::thread(Forward, svc, s).detach(); | 215 | // thread per direction left no clean way to fail the second spawn: |
| 216 | // std::thread throws std::system_error when pthread_create is refused | ||
| 217 | // (NPROC limit, container pids cgroup, memory pressure), the first | ||
| 218 | // pump was already detached, and closing its sockets from here would | ||
| 219 | // race it -- so nothing caught the throw and it reached | ||
| 220 | // std::terminate, killing the relay over one pair it could not serve. | ||
| 221 | // Running one direction on this thread leaves exactly one spawn to | ||
| 222 | // guard, and a failure here has nothing running yet to race. | ||
| 223 | try | ||
| 224 | { | ||
| 225 | std::thread(Forward, s, svc).detach(); | ||
| 226 | } | ||
| 227 | catch (const std::system_error& e) | ||
| 228 | { | ||
| 229 | fprintf(stderr, "rendezvous relay: cannot spawn forward thread (%s), pair dropped\n", | ||
| 230 | e.what()); | ||
| 231 | CLOSESOCK(svc); | ||
| 232 | CLOSESOCK(s); | ||
| 233 | return; | ||
| 234 | } | ||
| 235 | Forward(svc, s); | ||
| 203 | } | 236 | } |
| 204 | else | 237 | else |
| 205 | { | 238 | { |
| @@ -225,6 +258,7 @@ bool RvRelayRun(unsigned short port) | |||
| 225 | if (bind(g_listen, (struct sockaddr*)&addr, sizeof(addr)) != 0) { CLOSESOCK(g_listen); return false; } | 258 | if (bind(g_listen, (struct sockaddr*)&addr, sizeof(addr)) != 0) { CLOSESOCK(g_listen); return false; } |
| 226 | if (listen(g_listen, 16) != 0) { CLOSESOCK(g_listen); return false; } | 259 | if (listen(g_listen, 16) != 0) { CLOSESOCK(g_listen); return false; } |
| 227 | 260 | ||
| 261 | int nSpawnFails = 0; | ||
| 228 | while (!g_stop) | 262 | while (!g_stop) |
| 229 | { | 263 | { |
| 230 | struct sockaddr_in cli; | 264 | struct sockaddr_in cli; |
| @@ -235,7 +269,27 @@ bool RvRelayRun(unsigned short port) | |||
| 235 | if (g_stop) break; | 269 | if (g_stop) break; |
| 236 | continue; | 270 | continue; |
| 237 | } | 271 | } |
| 238 | std::thread(HandleIncoming, s).detach(); | 272 | try |
| 273 | { | ||
| 274 | std::thread(HandleIncoming, s).detach(); | ||
| 275 | nSpawnFails = 0; | ||
| 276 | } | ||
| 277 | catch (const std::system_error& e) | ||
| 278 | { | ||
| 279 | // A refused pthread_create is backpressure, not a reason to die: | ||
| 280 | // the condition behind it (NPROC, pids cgroup, memory) does not | ||
| 281 | // clear in milliseconds, and the connection count feeding it is | ||
| 282 | // attacker-influenced. Drop this one peer, stop accepting for a | ||
| 283 | // moment -- longer each time, capped -- and keep serving the | ||
| 284 | // connections already established. | ||
| 285 | fprintf(stderr, "rendezvous relay: cannot spawn connection thread (%s), connection dropped\n", | ||
| 286 | e.what()); | ||
| 287 | CLOSESOCK(s); | ||
| 288 | nSpawnFails++; | ||
| 289 | int nWaitMs = nSpawnFails < 5 ? nSpawnFails * 1000 : 5000; | ||
| 290 | for (int i = 0; i < nWaitMs && !g_stop; i += 100) | ||
| 291 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
| 292 | } | ||
| 239 | } | 293 | } |
| 240 | CLOSESOCK(g_listen); | 294 | CLOSESOCK(g_listen); |
| 241 | g_listen = INVALID_SOCKET; | 295 | g_listen = INVALID_SOCKET; |
| @@ -323,17 +377,29 @@ bool RvServiceWaitPaired(RvSocket s, int nTimeoutSecs) | |||
| 323 | } | 377 | } |
| 324 | 378 | ||
| 325 | RvSocket RvClientConnect(const char* relay_host, unsigned short port, | 379 | RvSocket RvClientConnect(const char* relay_host, unsigned short port, |
| 326 | const unsigned char target_pubkey[32]) | 380 | const unsigned char target_pubkey[32], |
| 381 | RvClientFail* pFail) | ||
| 327 | { | 382 | { |
| 383 | if (pFail) *pFail = RVC_FAIL_DIAL; | ||
| 328 | if (!RvInit()) return RV_INVALID; | 384 | if (!RvInit()) return RV_INVALID; |
| 329 | SOCKET s = ConnectTo(relay_host, port); | 385 | SOCKET s = ConnectTo(relay_host, port); |
| 330 | if (s == INVALID_SOCKET) return RV_INVALID; | 386 | if (s == INVALID_SOCKET) return RV_INVALID; |
| 387 | if (pFail) *pFail = RVC_FAIL_PAIR; | ||
| 331 | unsigned char hdr[33]; | 388 | unsigned char hdr[33]; |
| 332 | hdr[0] = (unsigned char)'C'; | 389 | hdr[0] = (unsigned char)'C'; |
| 333 | memcpy(hdr + 1, target_pubkey, 32); | 390 | memcpy(hdr + 1, target_pubkey, 32); |
| 334 | if (!WriteN(s, hdr, 33)) { CLOSESOCK(s); return RV_INVALID; } | 391 | if (!WriteN(s, hdr, 33)) { CLOSESOCK(s); return RV_INVALID; } |
| 335 | unsigned char paired = 0; | 392 | unsigned char paired = 0; |
| 336 | if (!ReadN(s, &paired, 1) || paired != 0x01) { CLOSESOCK(s); return RV_INVALID; } | 393 | if (!ReadN(s, &paired, 1)) { CLOSESOCK(s); return RV_INVALID; } |
| 394 | if (paired != 0x01) | ||
| 395 | { | ||
| 396 | // The relay answered and declined: HandleIncoming writes 0x00 and | ||
| 397 | // closes when no service is parked under this pubkey right now. | ||
| 398 | if (pFail) *pFail = RVC_FAIL_REFUSED; | ||
| 399 | CLOSESOCK(s); | ||
| 400 | return RV_INVALID; | ||
| 401 | } | ||
| 402 | if (pFail) *pFail = RVC_FAIL_NONE; | ||
| 337 | return (RvSocket)s; | 403 | return (RvSocket)s; |
| 338 | } | 404 | } |
| 339 | 405 | ||
diff --git a/src/btfrv.h b/src/btfrv.h index 9cd7476..1e85cb0 100644 --- a/src/btfrv.h +++ b/src/btfrv.h | |||
| @@ -57,10 +57,25 @@ RvSocket RvServiceRegister(const char* relay_host, unsigned short port, | |||
| 57 | bool RvServiceWaitPaired(RvSocket s, int nTimeoutSecs = 0); | 57 | bool RvServiceWaitPaired(RvSocket s, int nTimeoutSecs = 0); |
| 58 | 58 | ||
| 59 | // ---- client side (the node dialing a .btf address) ---- | 59 | // ---- client side (the node dialing a .btf address) ---- |
| 60 | // Where a failed RvClientConnect stopped. RV_INVALID alone collapses three | ||
| 61 | // different situations -- relay unreachable, relay reachable but nobody | ||
| 62 | // registered, connection died mid-pairing -- and they point at different | ||
| 63 | // components. The caller can only report the right one if this side says | ||
| 64 | // which it saw. | ||
| 65 | enum RvClientFail | ||
| 66 | { | ||
| 67 | RVC_FAIL_NONE = 0, | ||
| 68 | RVC_FAIL_DIAL, // TCP connect to the relay itself failed | ||
| 69 | RVC_FAIL_REFUSED, // relay answered 0x00: no service registered under this pubkey | ||
| 70 | RVC_FAIL_PAIR, // relay connection died before pairing completed | ||
| 71 | }; | ||
| 72 | |||
| 60 | // Connect through the relay to the service registered under `target_pubkey`. | 73 | // Connect through the relay to the service registered under `target_pubkey`. |
| 61 | // Returns the paired socket, or RV_INVALID on error. | 74 | // Returns the paired socket, or RV_INVALID on error -- with `pFail`, if |
| 75 | // given, naming the stage that failed. | ||
| 62 | RvSocket RvClientConnect(const char* relay_host, unsigned short port, | 76 | RvSocket RvClientConnect(const char* relay_host, unsigned short port, |
| 63 | const unsigned char target_pubkey[32]); | 77 | const unsigned char target_pubkey[32], |
| 78 | RvClientFail* pFail = 0); | ||
| 64 | 79 | ||
| 65 | // Blocking read/write of exactly n bytes over a rendezvous socket. Return false | 80 | // Blocking read/write of exactly n bytes over a rendezvous socket. Return false |
| 66 | // on error/EOF. | 81 | // on error/EOF. |
diff --git a/src/btftunnel.cpp b/src/btftunnel.cpp index 63d178b..4058f6b 100644 --- a/src/btftunnel.cpp +++ b/src/btftunnel.cpp | |||
| @@ -25,8 +25,12 @@ typedef int SOCKET; | |||
| 25 | #endif | 25 | #endif |
| 26 | #include "sockcount.h" | 26 | #include "sockcount.h" |
| 27 | #include <cstring> | 27 | #include <cstring> |
| 28 | #include <cstdio> | ||
| 29 | #include <memory> | ||
| 30 | #include <new> | ||
| 28 | #include <vector> | 31 | #include <vector> |
| 29 | #include <thread> | 32 | #include <thread> |
| 33 | #include <system_error> | ||
| 30 | 34 | ||
| 31 | namespace btf | 35 | namespace btf |
| 32 | { | 36 | { |
| @@ -119,35 +123,155 @@ static void PumpIn(SOCKET pumpEnd, RvSocket rv, std::vector<unsigned char> key) | |||
| 119 | shutdown((SOCKET)rv, SD_BOTH); | 123 | shutdown((SOCKET)rv, SD_BOTH); |
| 120 | } | 124 | } |
| 121 | 125 | ||
| 126 | // The pump end and the rendezvous socket, owned jointly by whoever is still | ||
| 127 | // using them. Closed once, by the last reference to go. | ||
| 128 | // | ||
| 129 | // Joint ownership rather than "the inbound pump owns the cleanup", because | ||
| 130 | // either pump can reach its exit first and BOTH touch BOTH descriptors on the | ||
| 131 | // way out -- PumpOut ends in shutdown(rv), shutdown(pumpEnd) and PumpIn ends in | ||
| 132 | // the mirror image. A single owner therefore has no exit at which the other is | ||
| 133 | // guaranteed to be finished. Holding a reference for the whole body of each | ||
| 134 | // pump gives it one: the close cannot happen until the last pump has stopped | ||
| 135 | // touching the descriptors, so no shutdown() can land on a number that has | ||
| 136 | // already been closed and handed back out by the kernel to an unrelated | ||
| 137 | // connection. | ||
| 138 | struct TunnelEnds | ||
| 139 | { | ||
| 140 | SOCKET pumpEnd; | ||
| 141 | RvSocket rv; | ||
| 142 | TunnelEnds(SOCKET p, RvSocket r) : pumpEnd(p), rv(r) {} | ||
| 143 | ~TunnelEnds() { BtfCloseSocket(pumpEnd); RvClose(rv); } | ||
| 144 | private: | ||
| 145 | TunnelEnds(const TunnelEnds&); | ||
| 146 | TunnelEnds& operator=(const TunnelEnds&); | ||
| 147 | }; | ||
| 148 | |||
| 122 | // Start the two pumps and hand back the app-facing socket. When both pumps | 149 | // Start the two pumps and hand back the app-facing socket. When both pumps |
| 123 | // finish, close the pump end and the rendezvous socket. | 150 | // finish, close the pump end and the rendezvous socket. |
| 151 | // | ||
| 152 | // That sentence used to be a comment describing something the code did not do. | ||
| 153 | // The inbound pump closed both descriptors as soon as it exited, without | ||
| 154 | // waiting for the outbound pump, which then called shutdown() on both at its | ||
| 155 | // own exit. Whether that shutdown hit a live descriptor or a closed one came | ||
| 156 | // down to which pump got there first, and the rendezvous socket dying before | ||
| 157 | // the app socket -- a relay restart, the far side hanging up -- is precisely | ||
| 158 | // the order that puts the inbound pump ahead. Measured over 25 such teardowns | ||
| 159 | // against the real code: 29 shutdowns landed on an already-closed descriptor. | ||
| 160 | // | ||
| 161 | // appEnd is not part of this. It is handed to the caller on success and closed | ||
| 162 | // here on every failure path; no pump ever touches it. | ||
| 163 | // | ||
| 164 | // Every tunnel is two threads, and a tunnel is one peer connection (a .btf | ||
| 165 | // dial or accept, a pool miner, a participant's pool session) -- so the | ||
| 166 | // thread count scales with peer count, which is attacker-influenced. When | ||
| 167 | // pthread_create is refused std::thread throws std::system_error; uncaught it | ||
| 168 | // reached std::terminate and took the whole process down over one connection | ||
| 169 | // it could not serve. Both spawns are guarded, and the failure paths return | ||
| 170 | // INVALID_SOCKET, which every caller already handles by dropping just that | ||
| 171 | // connection. | ||
| 124 | static btf_socket_t StartTunnel(RvSocket rv, const unsigned char shared[32]) | 172 | static btf_socket_t StartTunnel(RvSocket rv, const unsigned char shared[32]) |
| 125 | { | 173 | { |
| 126 | btf_socket_t appEnd, pumpEnd; | 174 | btf_socket_t appEnd, pumpEnd; |
| 127 | if (!MakeLocalPair(appEnd, pumpEnd)) { RvClose(rv); return INVALID_SOCKET; } | 175 | if (!MakeLocalPair(appEnd, pumpEnd)) { RvClose(rv); return INVALID_SOCKET; } |
| 128 | 176 | ||
| 129 | std::vector<unsigned char> key(shared, shared + 32); | 177 | std::vector<unsigned char> key(shared, shared + 32); |
| 130 | std::thread(PumpOut, pumpEnd, rv, key).detach(); | 178 | |
| 131 | std::thread([pumpEnd, rv, key]() { | 179 | // make_shared, not shared_ptr(new ...): it allocates before it constructs, |
| 132 | PumpIn(pumpEnd, rv, key); | 180 | // so it either comes back owning both descriptors or throws having taken |
| 181 | // ownership of neither. The two-step form can run the deleter and then | ||
| 182 | // throw, which would close both here and again below. | ||
| 183 | std::shared_ptr<TunnelEnds> ends; | ||
| 184 | try | ||
| 185 | { | ||
| 186 | ends = std::make_shared<TunnelEnds>(pumpEnd, rv); | ||
| 187 | } | ||
| 188 | catch (const std::bad_alloc&) | ||
| 189 | { | ||
| 190 | fprintf(stderr, "btf tunnel: out of memory starting tunnel, tunnel dropped\n"); | ||
| 133 | BtfCloseSocket(pumpEnd); | 191 | BtfCloseSocket(pumpEnd); |
| 192 | BtfCloseSocket(appEnd); | ||
| 134 | RvClose(rv); | 193 | RvClose(rv); |
| 135 | }).detach(); | 194 | return INVALID_SOCKET; |
| 195 | } | ||
| 196 | |||
| 197 | try | ||
| 198 | { | ||
| 199 | std::thread([ends, key]() { PumpIn(ends->pumpEnd, ends->rv, key); }).detach(); | ||
| 200 | } | ||
| 201 | catch (const std::system_error& e) | ||
| 202 | { | ||
| 203 | // Nothing is running yet: `ends` is the only reference, and returning | ||
| 204 | // drops it, which is what closes pumpEnd and rv. | ||
| 205 | fprintf(stderr, "btf tunnel: cannot spawn inbound pump (%s), tunnel dropped\n", | ||
| 206 | e.what()); | ||
| 207 | BtfCloseSocket(appEnd); | ||
| 208 | return INVALID_SOCKET; | ||
| 209 | } | ||
| 210 | |||
| 211 | try | ||
| 212 | { | ||
| 213 | std::thread([ends, key]() { PumpOut(ends->pumpEnd, ends->rv, key); }).detach(); | ||
| 214 | } | ||
| 215 | catch (const std::system_error& e) | ||
| 216 | { | ||
| 217 | // The inbound pump may be parked in RvReadN(rv) or may already have | ||
| 218 | // exited -- when the spawn is refused because the process is loaded, | ||
| 219 | // the rendezvous socket is often dead for the same reason, and then | ||
| 220 | // PumpIn returns on its first length read. Either way this thread | ||
| 221 | // still holds a reference, so both descriptors are still open: the | ||
| 222 | // shutdown wakes the pump if it is parked and is harmless if it is | ||
| 223 | // not. The close belongs to whichever reference goes last. | ||
| 224 | fprintf(stderr, "btf tunnel: cannot spawn outbound pump (%s), tunnel dropped\n", | ||
| 225 | e.what()); | ||
| 226 | shutdown((SOCKET)ends->rv, SD_BOTH); | ||
| 227 | shutdown(ends->pumpEnd, SD_BOTH); | ||
| 228 | BtfCloseSocket(appEnd); | ||
| 229 | return INVALID_SOCKET; | ||
| 230 | } | ||
| 136 | return appEnd; | 231 | return appEnd; |
| 137 | } | 232 | } |
| 138 | 233 | ||
| 234 | const char* TunnelFailText(TunnelFail f) | ||
| 235 | { | ||
| 236 | switch (f) | ||
| 237 | { | ||
| 238 | case TUNNEL_FAIL_NONE: return "no failure"; | ||
| 239 | case TUNNEL_FAIL_INIT: return "local socket/crypto init failed"; | ||
| 240 | case TUNNEL_FAIL_DIAL: return "meeting node unreachable (tcp dial failed)"; | ||
| 241 | case TUNNEL_FAIL_REFUSED: return "relay reachable, but no service registered there for this address"; | ||
| 242 | case TUNNEL_FAIL_PAIR: return "relay connection died before pairing completed"; | ||
| 243 | case TUNNEL_FAIL_HANDSHAKE: return "end-to-end handshake failed"; | ||
| 244 | case TUNNEL_FAIL_LOCAL: return "local tunnel setup failed"; | ||
| 245 | } | ||
| 246 | return "unknown failure"; | ||
| 247 | } | ||
| 248 | |||
| 139 | btf_socket_t BtfClientTunnel(const char* meeting_host, unsigned short port, | 249 | btf_socket_t BtfClientTunnel(const char* meeting_host, unsigned short port, |
| 140 | const unsigned char target_pubkey[32], | 250 | const unsigned char target_pubkey[32], |
| 141 | const unsigned char service_enc_pub[32]) | 251 | const unsigned char service_enc_pub[32], |
| 252 | TunnelFail* pFail) | ||
| 142 | { | 253 | { |
| 254 | if (pFail) *pFail = TUNNEL_FAIL_INIT; | ||
| 143 | if (!TunnelInit()) return INVALID_SOCKET; | 255 | if (!TunnelInit()) return INVALID_SOCKET; |
| 144 | RvSocket rv = RvClientConnect(meeting_host, port, target_pubkey); | 256 | RvClientFail rvFail = RVC_FAIL_NONE; |
| 145 | if (rv == RV_INVALID) return INVALID_SOCKET; | 257 | RvSocket rv = RvClientConnect(meeting_host, port, target_pubkey, &rvFail); |
| 258 | if (rv == RV_INVALID) | ||
| 259 | { | ||
| 260 | if (pFail) | ||
| 261 | *pFail = rvFail == RVC_FAIL_DIAL ? TUNNEL_FAIL_DIAL | ||
| 262 | : rvFail == RVC_FAIL_REFUSED ? TUNNEL_FAIL_REFUSED | ||
| 263 | : TUNNEL_FAIL_PAIR; | ||
| 264 | return INVALID_SOCKET; | ||
| 265 | } | ||
| 146 | 266 | ||
| 147 | unsigned char eph_pub[32], shared[32]; | 267 | unsigned char eph_pub[32], shared[32]; |
| 268 | if (pFail) *pFail = TUNNEL_FAIL_HANDSHAKE; | ||
| 148 | if (!ChanClientHandshake(service_enc_pub, eph_pub, shared)) { RvClose(rv); return INVALID_SOCKET; } | 269 | if (!ChanClientHandshake(service_enc_pub, eph_pub, shared)) { RvClose(rv); return INVALID_SOCKET; } |
| 149 | if (!RvWriteN(rv, eph_pub, 32)) { RvClose(rv); return INVALID_SOCKET; } | 270 | if (!RvWriteN(rv, eph_pub, 32)) { RvClose(rv); return INVALID_SOCKET; } |
| 150 | return StartTunnel(rv, shared); | 271 | if (pFail) *pFail = TUNNEL_FAIL_LOCAL; |
| 272 | btf_socket_t app = StartTunnel(rv, shared); | ||
| 273 | if (app != INVALID_SOCKET && pFail) *pFail = TUNNEL_FAIL_NONE; | ||
| 274 | return app; | ||
| 151 | } | 275 | } |
| 152 | 276 | ||
| 153 | btf_socket_t BtfServiceWrap(RvSocket rv, const unsigned char my_enc_sk[32]) | 277 | btf_socket_t BtfServiceWrap(RvSocket rv, const unsigned char my_enc_sk[32]) |
diff --git a/src/btftunnel.h b/src/btftunnel.h index 1fa4f01..3cc87cd 100644 --- a/src/btftunnel.h +++ b/src/btftunnel.h | |||
| @@ -10,8 +10,12 @@ | |||
| 10 | // caller), then BtfClientTunnel connects through the relay, runs the echan | 10 | // caller), then BtfClientTunnel connects through the relay, runs the echan |
| 11 | // handshake, and returns a local SOCKET to hand a new outbound CNode. | 11 | // handshake, and returns a local SOCKET to hand a new outbound CNode. |
| 12 | // | 12 | // |
| 13 | // Service side: after RvServiceRegister pairs us with a client, BtfServiceWrap | 13 | // Service side: RvServiceRegister only registers -- callers must then block in |
| 14 | // completes the echan handshake and returns a local SOCKET for an inbound CNode. | 14 | // RvServiceWaitPaired until a client is actually paired to them. BtfServiceWrap |
| 15 | // completes the echan handshake on that paired socket and returns a local | ||
| 16 | // SOCKET for an inbound CNode. Wrapping a registered-but-unpaired socket reads | ||
| 17 | // the relay's 0x01 pairing byte as the first byte of the client's ephemeral | ||
| 18 | // key, which silently corrupts the tunnel instead of failing. | ||
| 15 | 19 | ||
| 16 | #ifndef BITFLASH_BTFTUNNEL_H | 20 | #ifndef BITFLASH_BTFTUNNEL_H |
| 17 | #define BITFLASH_BTFTUNNEL_H | 21 | #define BITFLASH_BTFTUNNEL_H |
| @@ -32,15 +36,37 @@ namespace btf | |||
| 32 | // Initialize (sockets + libsodium). Idempotent. | 36 | // Initialize (sockets + libsodium). Idempotent. |
| 33 | bool TunnelInit(); | 37 | bool TunnelInit(); |
| 34 | 38 | ||
| 39 | // Which stage of BtfClientTunnel failed. INVALID_SOCKET alone says only "no | ||
| 40 | // tunnel"; a caller that then guesses at causes in its log line sends whoever | ||
| 41 | // reads it to the wrong component (issue #17: "relay unreachable or pool not | ||
| 42 | // listening" blamed two healthy parts). The stage is what this side actually | ||
| 43 | // knows -- report that. | ||
| 44 | enum TunnelFail | ||
| 45 | { | ||
| 46 | TUNNEL_FAIL_NONE = 0, | ||
| 47 | TUNNEL_FAIL_INIT, // local socket/crypto init failed | ||
| 48 | TUNNEL_FAIL_DIAL, // TCP connect to the meeting node failed | ||
| 49 | TUNNEL_FAIL_REFUSED, // relay reachable, but no service registered under the pubkey | ||
| 50 | TUNNEL_FAIL_PAIR, // relay connection died before pairing completed | ||
| 51 | TUNNEL_FAIL_HANDSHAKE, // end-to-end channel handshake failed | ||
| 52 | TUNNEL_FAIL_LOCAL, // local pump plumbing (socket pair / threads) failed | ||
| 53 | }; | ||
| 54 | |||
| 55 | // Short description of a TunnelFail for log lines. | ||
| 56 | const char* TunnelFailText(TunnelFail f); | ||
| 57 | |||
| 35 | // Client: dial the service registered under `target_pubkey` at the meeting node | 58 | // Client: dial the service registered under `target_pubkey` at the meeting node |
| 36 | // (host:port), box to its static x25519 `service_enc_pub`, and return a local | 59 | // (host:port), box to its static x25519 `service_enc_pub`, and return a local |
| 37 | // plaintext SOCKET tunneled end-to-end to the service. INVALID_SOCKET on error. | 60 | // plaintext SOCKET tunneled end-to-end to the service. INVALID_SOCKET on error |
| 61 | // -- with `pFail`, if given, naming the stage that failed. | ||
| 38 | btf_socket_t BtfClientTunnel(const char* meeting_host, unsigned short port, | 62 | btf_socket_t BtfClientTunnel(const char* meeting_host, unsigned short port, |
| 39 | const unsigned char target_pubkey[32], | 63 | const unsigned char target_pubkey[32], |
| 40 | const unsigned char service_enc_pub[32]); | 64 | const unsigned char service_enc_pub[32], |
| 65 | TunnelFail* pFail = 0); | ||
| 41 | 66 | ||
| 42 | // Service: wrap an already-paired rendezvous socket (from RvServiceRegister) | 67 | // Service: wrap an already-paired rendezvous socket (RvServiceRegister followed |
| 43 | // with echan using my static x25519 secret, returning a local plaintext SOCKET | 68 | // by RvServiceWaitPaired -- the pairing byte MUST already be consumed) with |
| 69 | // echan using my static x25519 secret, returning a local plaintext SOCKET | ||
| 44 | // for an inbound CNode. Consumes `rv` (closed when the tunnel tears down). | 70 | // for an inbound CNode. Consumes `rv` (closed when the tunnel tears down). |
| 45 | btf_socket_t BtfServiceWrap(RvSocket rv, const unsigned char my_enc_sk[32]); | 71 | btf_socket_t BtfServiceWrap(RvSocket rv, const unsigned char my_enc_sk[32]); |
| 46 | 72 | ||
diff --git a/src/compactblock.cpp b/src/compactblock.cpp new file mode 100644 index 0000000..1ca6118 --- /dev/null +++ b/src/compactblock.cpp | |||
| @@ -0,0 +1,175 @@ | |||
| 1 | // Copyright (c) 2026 Bitflash developers | ||
| 2 | // Distributed under the MIT/X11 software license. | ||
| 3 | // | ||
| 4 | // See compactblock.h. | ||
| 5 | |||
| 6 | #include "headers.h" | ||
| 7 | #include "compactblock.h" | ||
| 8 | #include <sodium.h> | ||
| 9 | #include <map> | ||
| 10 | |||
| 11 | uint256 CCmpctBlock::HeaderHash() const | ||
| 12 | { | ||
| 13 | // Same technique CBlock::GetHash() uses: the header fields are laid out | ||
| 14 | // contiguously and in the same order here as in CBlock, so hashing the | ||
| 15 | // raw member range gives the identical block-identity hash. | ||
| 16 | return Hash(BEGIN(nVersion), END(nNonce)); | ||
| 17 | } | ||
| 18 | |||
| 19 | uint64 ComputeShortTxId(const uint256& hashPrevBlock, uint64 shortIdNonce, const uint256& txid) | ||
| 20 | { | ||
| 21 | // Derive a 16-byte SipHash key from (hashPrevBlock, shortIdNonce), so | ||
| 22 | // short IDs are specific to this block: they can't be precomputed | ||
| 23 | // before the block (and its freshly-chosen nonce) exist, and the same | ||
| 24 | // transaction gets a different short ID in every block it appears in | ||
| 25 | // (relevant across reorgs/re-announcements). | ||
| 26 | unsigned char keyMaterial[32 + sizeof(uint64)]; | ||
| 27 | memcpy(keyMaterial, BEGIN(hashPrevBlock), 32); | ||
| 28 | memcpy(keyMaterial + 32, &shortIdNonce, sizeof(uint64)); | ||
| 29 | uint256 keyHash = Hash((const char*)keyMaterial, (const char*)keyMaterial + sizeof(keyMaterial)); | ||
| 30 | |||
| 31 | unsigned char out[crypto_shorthash_BYTES]; // 8 bytes | ||
| 32 | crypto_shorthash(out, (const unsigned char*)BEGIN(txid), 32, (const unsigned char*)BEGIN(keyHash)); | ||
| 33 | |||
| 34 | uint64 id = 0; | ||
| 35 | memcpy(&id, out, sizeof(uint64)); | ||
| 36 | return id & 0xFFFFFFFFFFFFULL; // low 48 bits -- matches the wire field width | ||
| 37 | } | ||
| 38 | |||
| 39 | bool BuildCompactBlock(const CBlock& block, CCmpctBlock& out) | ||
| 40 | { | ||
| 41 | if (block.vtx.empty()) | ||
| 42 | return false; // every real block has at least the coinbase | ||
| 43 | |||
| 44 | out.nVersion = block.nVersion; | ||
| 45 | out.hashPrevBlock = block.hashPrevBlock; | ||
| 46 | out.hashMerkleRoot = block.hashMerkleRoot; | ||
| 47 | out.nTime = block.nTime; | ||
| 48 | out.nBits = block.nBits; | ||
| 49 | out.nNonce = block.nNonce; | ||
| 50 | |||
| 51 | // Upstream ignored the RAND_bytes return value here. On failure nonceBytes | ||
| 52 | // is left uninitialised and an indeterminate shortIdNonce goes out on the | ||
| 53 | // wire -- which is precisely the value this file relies on being | ||
| 54 | // unpredictable to stop short IDs being precomputed or ground against. | ||
| 55 | // Fail closed instead, matching every other RAND_bytes call site in this | ||
| 56 | // tree (bip32.cpp, db.cpp): the caller falls back to full-block relay, | ||
| 57 | // which is always correct. | ||
| 58 | unsigned char nonceBytes[sizeof(uint64)]; | ||
| 59 | if (RAND_bytes(nonceBytes, sizeof(nonceBytes)) != 1) | ||
| 60 | return false; | ||
| 61 | memcpy(&out.shortIdNonce, nonceBytes, sizeof(uint64)); | ||
| 62 | |||
| 63 | out.vPrefilledIndex.clear(); | ||
| 64 | out.vPrefilledTx.clear(); | ||
| 65 | out.vShortTxId.clear(); | ||
| 66 | |||
| 67 | // Only the coinbase is prefilled (index 0) -- see file comment in | ||
| 68 | // compactblock.h for why that's enough for this chain's scale. | ||
| 69 | out.vPrefilledIndex.push_back(0); | ||
| 70 | out.vPrefilledTx.push_back(block.vtx[0]); | ||
| 71 | |||
| 72 | for (size_t i = 1; i < block.vtx.size(); i++) | ||
| 73 | out.vShortTxId.push_back(ComputeShortTxId(out.hashPrevBlock, out.shortIdNonce, block.vtx[i].GetHash())); | ||
| 74 | |||
| 75 | return true; | ||
| 76 | } | ||
| 77 | |||
| 78 | bool TryReconstructBlock(const CCmpctBlock& cmpct, CBlock& outBlock, std::vector<unsigned int>& vMissingIndex) | ||
| 79 | { | ||
| 80 | vMissingIndex.clear(); | ||
| 81 | |||
| 82 | size_t nTotal = cmpct.vPrefilledIndex.size() + cmpct.vShortTxId.size(); | ||
| 83 | if (cmpct.vPrefilledIndex.size() != cmpct.vPrefilledTx.size() || nTotal == 0) | ||
| 84 | return false; // malformed | ||
| 85 | |||
| 86 | // nTotal comes straight off the wire once this is wired into the P2P | ||
| 87 | // layer, and serialize.h only bounds a vector length by MAX_SIZE | ||
| 88 | // (0x02000000), so an unbounded assign() below would let one 60-odd-byte | ||
| 89 | // message ask this node for ~33M default-constructed CTransactions. The | ||
| 90 | // smallest a transaction can serialise to is 10 bytes (nVersion, empty | ||
| 91 | // vin, empty vout, nLockTime), so no block within MAX_BLOCK_SIZE can hold | ||
| 92 | // more than MAX_BLOCK_SIZE/10 of them; anything above that is not a block | ||
| 93 | // we could ever accept. Rejecting here just falls back to full-block | ||
| 94 | // relay, which is always correct. | ||
| 95 | if (nTotal > MAX_BLOCK_SIZE / 10) | ||
| 96 | return false; // implausible tx count -- refuse to size a vector from it | ||
| 97 | |||
| 98 | outBlock.SetNull(); | ||
| 99 | outBlock.nVersion = cmpct.nVersion; | ||
| 100 | outBlock.hashPrevBlock = cmpct.hashPrevBlock; | ||
| 101 | outBlock.hashMerkleRoot = cmpct.hashMerkleRoot; | ||
| 102 | outBlock.nTime = cmpct.nTime; | ||
| 103 | outBlock.nBits = cmpct.nBits; | ||
| 104 | outBlock.nNonce = cmpct.nNonce; | ||
| 105 | outBlock.vtx.assign(nTotal, CTransaction()); | ||
| 106 | |||
| 107 | std::vector<bool> vFilled(nTotal, false); | ||
| 108 | for (size_t i = 0; i < cmpct.vPrefilledIndex.size(); i++) | ||
| 109 | { | ||
| 110 | unsigned int idx = cmpct.vPrefilledIndex[i]; | ||
| 111 | if (idx >= nTotal) | ||
| 112 | return false; // malformed | ||
| 113 | outBlock.vtx[idx] = cmpct.vPrefilledTx[i]; | ||
| 114 | vFilled[idx] = true; | ||
| 115 | } | ||
| 116 | |||
| 117 | // Index every mempool tx by its short ID under this block's keying, so | ||
| 118 | // resolving each block position is a map lookup rather than a linear | ||
| 119 | // scan of the mempool per short ID. | ||
| 120 | std::map<uint64, const CTransaction*> mapShortIdToTx; | ||
| 121 | for (std::map<uint256, CTransaction>::const_iterator mi = mapTransactions.begin(); | ||
| 122 | mi != mapTransactions.end(); ++mi) | ||
| 123 | { | ||
| 124 | uint64 sid = ComputeShortTxId(cmpct.hashPrevBlock, cmpct.shortIdNonce, mi->first); | ||
| 125 | // A short-ID collision between two distinct mempool transactions is | ||
| 126 | // possible (48 bits, birthday bound ~2^24 mempool entries) though | ||
| 127 | // very unlikely at this chain's scale; on collision, prefer keeping | ||
| 128 | // neither mapped rather than risk resolving to the wrong tx -- | ||
| 129 | // that position just falls through to getblocktxn like a genuine | ||
| 130 | // miss, which is always correct, only occasionally not optimal. | ||
| 131 | if (mapShortIdToTx.count(sid)) | ||
| 132 | mapShortIdToTx[sid] = NULL; | ||
| 133 | else | ||
| 134 | mapShortIdToTx[sid] = &mi->second; | ||
| 135 | } | ||
| 136 | |||
| 137 | size_t shortIdPos = 0; | ||
| 138 | for (size_t idx = 0; idx < nTotal; idx++) | ||
| 139 | { | ||
| 140 | if (vFilled[idx]) | ||
| 141 | continue; | ||
| 142 | if (shortIdPos >= cmpct.vShortTxId.size()) | ||
| 143 | return false; // malformed: more unfilled positions than short IDs | ||
| 144 | uint64 sid = cmpct.vShortTxId[shortIdPos++]; | ||
| 145 | |||
| 146 | std::map<uint64, const CTransaction*>::const_iterator found = mapShortIdToTx.find(sid); | ||
| 147 | if (found != mapShortIdToTx.end() && found->second != NULL) | ||
| 148 | { | ||
| 149 | outBlock.vtx[idx] = *found->second; | ||
| 150 | vFilled[idx] = true; | ||
| 151 | } | ||
| 152 | else | ||
| 153 | { | ||
| 154 | vMissingIndex.push_back(idx); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | return vMissingIndex.empty(); | ||
| 159 | } | ||
| 160 | |||
| 161 | bool FillMissingAndFinish(CBlock& outBlock, const std::vector<unsigned int>& vMissingIndex, | ||
| 162 | const std::vector<CTransaction>& vTxn) | ||
| 163 | { | ||
| 164 | if (vMissingIndex.size() != vTxn.size()) | ||
| 165 | return false; // peer didn't answer what was asked | ||
| 166 | |||
| 167 | for (size_t i = 0; i < vMissingIndex.size(); i++) | ||
| 168 | { | ||
| 169 | unsigned int idx = vMissingIndex[i]; | ||
| 170 | if (idx >= outBlock.vtx.size()) | ||
| 171 | return false; // malformed | ||
| 172 | outBlock.vtx[idx] = vTxn[i]; | ||
| 173 | } | ||
| 174 | return true; | ||
| 175 | } | ||
diff --git a/src/compactblock.h b/src/compactblock.h new file mode 100644 index 0000000..745ee3b --- /dev/null +++ b/src/compactblock.h | |||
| @@ -0,0 +1,113 @@ | |||
| 1 | // Copyright (c) 2026 Bitflash developers | ||
| 2 | // Distributed under the MIT/X11 software license. | ||
| 3 | // | ||
| 4 | // Compact block relay (BIP152-style). Instead of sending a peer's full set | ||
| 5 | // of transactions for a block they're about to receive -- almost all of | ||
| 6 | // which they already have in their mempool from normal tx relay -- send a | ||
| 7 | // short (48-bit, keyed-hash) ID per transaction instead. A peer that already | ||
| 8 | // has every referenced transaction reconstructs the full block locally from | ||
| 9 | // its mempool without any of the transaction bytes crossing the wire at | ||
| 10 | // all; only genuinely missing transactions (typically just the coinbase) | ||
| 11 | // need a follow-up round trip. | ||
| 12 | // | ||
| 13 | // Compatibility: this is purely additive. A peer that never sends | ||
| 14 | // "sendcmpct" is never sent a compact block -- it keeps getting full | ||
| 15 | // "block" messages exactly as before (see the getdata handler in | ||
| 16 | // main.cpp). Old, unupgraded nodes on the live network are completely | ||
| 17 | // unaffected; ProcessMessage's unknown-command handling means they simply | ||
| 18 | // never see the new message types. No coordinated network upgrade needed. | ||
| 19 | // | ||
| 20 | // Deliberately scoped down from full BIP152: no high-bandwidth (unsolicited | ||
| 21 | // push) mode, no differential-index encoding for prefilled transactions, | ||
| 22 | // only the current chain tip is ever offered in compact form (an old block | ||
| 23 | // is unlikely to still overlap much with any peer's current mempool, so the | ||
| 24 | // bandwidth win doesn't apply -- full "block" is sent for those, same as | ||
| 25 | // today). Only the coinbase is prefilled; every other transaction position | ||
| 26 | // carries a short ID. | ||
| 27 | |||
| 28 | #ifndef BITFLASH_COMPACTBLOCK_H | ||
| 29 | #define BITFLASH_COMPACTBLOCK_H | ||
| 30 | |||
| 31 | // Requires CBlock/CTransaction already visible -- #include "headers.h" (or | ||
| 32 | // main.h) before this file. Deliberately doesn't include main.h itself: | ||
| 33 | // main.h has no include guard (matches this codebase's existing headers, it | ||
| 34 | // relies on being pulled in exactly once via headers.h), so a second | ||
| 35 | // #include "main.h" here would break every translation unit that already | ||
| 36 | // brought it in via headers.h first. | ||
| 37 | #include <vector> | ||
| 38 | |||
| 39 | // The compact representation of a block: header fields, a per-block nonce | ||
| 40 | // (keys the short-ID hash so IDs can't be precomputed/ground across blocks), | ||
| 41 | // the coinbase in full, and a 48-bit short ID for every other transaction | ||
| 42 | // in block order. | ||
| 43 | class CCmpctBlock | ||
| 44 | { | ||
| 45 | public: | ||
| 46 | // header | ||
| 47 | int nVersion; | ||
| 48 | uint256 hashPrevBlock; | ||
| 49 | uint256 hashMerkleRoot; | ||
| 50 | unsigned int nTime; | ||
| 51 | unsigned int nBits; | ||
| 52 | unsigned int nNonce; | ||
| 53 | |||
| 54 | // Keys the short-ID hash for this block specifically -- chosen fresh | ||
| 55 | // per block (not reused from nNonce, which is public PoW data an | ||
| 56 | // attacker already knows and could otherwise grind against). | ||
| 57 | uint64 shortIdNonce; | ||
| 58 | |||
| 59 | // Sent in full. In this implementation: always exactly the coinbase, | ||
| 60 | // at index 0. | ||
| 61 | std::vector<unsigned int> vPrefilledIndex; | ||
| 62 | std::vector<CTransaction> vPrefilledTx; | ||
| 63 | |||
| 64 | // One short ID per NON-prefilled transaction, in ascending block-index | ||
| 65 | // order (i.e. the Nth entry here belongs to the Nth block position that | ||
| 66 | // isn't in vPrefilledIndex). | ||
| 67 | std::vector<uint64> vShortTxId; | ||
| 68 | |||
| 69 | IMPLEMENT_SERIALIZE | ||
| 70 | ( | ||
| 71 | READWRITE(this->nVersion); | ||
| 72 | nVersion = this->nVersion; | ||
| 73 | READWRITE(hashPrevBlock); | ||
| 74 | READWRITE(hashMerkleRoot); | ||
| 75 | READWRITE(nTime); | ||
| 76 | READWRITE(nBits); | ||
| 77 | READWRITE(nNonce); | ||
| 78 | READWRITE(shortIdNonce); | ||
| 79 | READWRITE(vPrefilledIndex); | ||
| 80 | READWRITE(vPrefilledTx); | ||
| 81 | READWRITE(vShortTxId); | ||
| 82 | ) | ||
| 83 | |||
| 84 | uint256 HeaderHash() const; | ||
| 85 | }; | ||
| 86 | |||
| 87 | // Compute the 48-bit short ID for a transaction under a given block's | ||
| 88 | // short-ID nonce. Keyed (via the block's hashPrevBlock + shortIdNonce) so | ||
| 89 | // IDs from one block can't be precomputed before that block exists, and | ||
| 90 | // differ block to block even for the same transaction. | ||
| 91 | uint64 ComputeShortTxId(const uint256& hashPrevBlock, uint64 shortIdNonce, const uint256& txid); | ||
| 92 | |||
| 93 | // Build the compact form of `block` (which must have vtx[0] as the | ||
| 94 | // coinbase, as every block on this chain does). | ||
| 95 | bool BuildCompactBlock(const CBlock& block, CCmpctBlock& out); | ||
| 96 | |||
| 97 | // Attempt to reconstruct a full block from a received compact block using | ||
| 98 | // the local mempool. On full success, returns true and fills `outBlock`. | ||
| 99 | // On partial success (some transactions not found locally), returns false | ||
| 100 | // and fills `vMissingIndex` with the block-position indices that still need | ||
| 101 | // to be requested via "getblocktxn" -- outBlock is left partially built | ||
| 102 | // (coinbase and any resolved positions filled; caller must not use it until | ||
| 103 | // FillMissingAndFinish completes it). | ||
| 104 | bool TryReconstructBlock(const CCmpctBlock& cmpct, CBlock& outBlock, std::vector<unsigned int>& vMissingIndex); | ||
| 105 | |||
| 106 | // Complete a partially-reconstructed block (from a prior TryReconstructBlock | ||
| 107 | // that returned false) using the transactions received in a "blocktxn" | ||
| 108 | // response. vTxn must be in the same order as the indices this node | ||
| 109 | // requested via "getblocktxn" for this block. | ||
| 110 | bool FillMissingAndFinish(CBlock& outBlock, const std::vector<unsigned int>& vMissingIndex, | ||
| 111 | const std::vector<CTransaction>& vTxn); | ||
| 112 | |||
| 113 | #endif | ||
| @@ -806,20 +806,12 @@ bool CTxDB::LoadBlockIndex() | |||
| 806 | 806 | ||
| 807 | 807 | ||
| 808 | // | 808 | // |
| 809 | // CReviewDB | 809 | // CReviewDB is gone with the "review" handler. Its write path was |
| 810 | // read-whole-vector / append-one / write-whole-vector, driven straight from an | ||
| 811 | // unauthenticated peer, which is quadratic disk on an unbounded record. | ||
| 812 | // Existing reviews.dat files are simply left alone and never opened. | ||
| 810 | // | 813 | // |
| 811 | 814 | ||
| 812 | bool CReviewDB::ReadReviews(uint256 hash, vector<CReview>& vReviews) | ||
| 813 | { | ||
| 814 | vReviews.size(); // msvc workaround, just need to do anything with vReviews | ||
| 815 | return Read(make_pair(string("reviews"), hash), vReviews); | ||
| 816 | } | ||
| 817 | |||
| 818 | bool CReviewDB::WriteReviews(uint256 hash, const vector<CReview>& vReviews) | ||
| 819 | { | ||
| 820 | return Write(make_pair(string("reviews"), hash), vReviews); | ||
| 821 | } | ||
| 822 | |||
| 823 | 815 | ||
| 824 | 816 | ||
| 825 | 817 | ||
| @@ -8,8 +8,6 @@ class CTxIndex; | |||
| 8 | class CDiskBlockIndex; | 8 | class CDiskBlockIndex; |
| 9 | class CDiskTxPos; | 9 | class CDiskTxPos; |
| 10 | class COutPoint; | 10 | class COutPoint; |
| 11 | class CUser; | ||
| 12 | class CReview; | ||
| 13 | class CAddress; | 11 | class CAddress; |
| 14 | class CWalletTx; | 12 | class CWalletTx; |
| 15 | 13 | ||
| @@ -314,27 +312,6 @@ public: | |||
| 314 | 312 | ||
| 315 | 313 | ||
| 316 | 314 | ||
| 317 | class CReviewDB : public CDB | ||
| 318 | { | ||
| 319 | public: | ||
| 320 | CReviewDB(const char* pszMode="r+", bool fTxn=false) : CDB("reviews.dat", pszMode, fTxn) { } | ||
| 321 | private: | ||
| 322 | CReviewDB(const CReviewDB&); | ||
| 323 | void operator=(const CReviewDB&); | ||
| 324 | public: | ||
| 325 | bool ReadUser(uint256 hash, CUser& user) | ||
| 326 | { | ||
| 327 | return Read(make_pair(string("user"), hash), user); | ||
| 328 | } | ||
| 329 | |||
| 330 | bool WriteUser(uint256 hash, const CUser& user) | ||
| 331 | { | ||
| 332 | return Write(make_pair(string("user"), hash), user); | ||
| 333 | } | ||
| 334 | |||
| 335 | bool ReadReviews(uint256 hash, vector<CReview>& vReviews); | ||
| 336 | bool WriteReviews(uint256 hash, const vector<CReview>& vReviews); | ||
| 337 | }; | ||
| 338 | 315 | ||
| 339 | 316 | ||
| 340 | 317 | ||
diff --git a/src/gui.cpp b/src/gui.cpp index 2a7f350..ff6059a 100644 --- a/src/gui.cpp +++ b/src/gui.cpp | |||
| @@ -20,6 +20,7 @@ | |||
| 20 | #include <atomic> | 20 | #include <atomic> |
| 21 | #include <mutex> | 21 | #include <mutex> |
| 22 | #include <thread> | 22 | #include <thread> |
| 23 | #include <system_error> | ||
| 23 | #include "bip32.h" | 24 | #include "bip32.h" |
| 24 | #include "walletcmd.h" | 25 | #include "walletcmd.h" |
| 25 | #include "font_roboto.h" | 26 | #include "font_roboto.h" |
| @@ -919,8 +920,16 @@ static void DrawOptionsDialog() | |||
| 919 | 920 | ||
| 920 | if (nMineMode == MINE_OPERATOR) { | 921 | if (nMineMode == MINE_OPERATOR) { |
| 921 | if (!gPoolServerRunning) { | 922 | if (!gPoolServerRunning) { |
| 922 | gPoolServerRunning = true; | 923 | // Claim the server is up only once it is. The flag used to |
| 923 | _beginthread(ThreadRPCServer, 0, NULL); | 924 | // be set first and the result never looked at, so a refused |
| 925 | // spawn left an operator with a pool that reports itself | ||
| 926 | // running, no RPC server for miners to reach, and nothing | ||
| 927 | // anywhere saying why. The flag also latches: with it set, | ||
| 928 | // this branch is never retried. | ||
| 929 | if (_beginthread(ThreadRPCServer, 0, NULL) == (uintptr_t)-1) | ||
| 930 | printf("Error: _beginthread(ThreadRPCServer) failed -- pool server not started\n"); | ||
| 931 | else | ||
| 932 | gPoolServerRunning = true; | ||
| 924 | } else if (!gPoolRunning) { | 933 | } else if (!gPoolRunning) { |
| 925 | gPoolRunning = true; | 934 | gPoolRunning = true; |
| 926 | } | 935 | } |
| @@ -1133,11 +1142,28 @@ static void DrawRestorePhraseDialog() | |||
| 1133 | } | 1142 | } |
| 1134 | g_restoreDerived.store(0); | 1143 | g_restoreDerived.store(0); |
| 1135 | g_restoreRecovered.store(0); | 1144 | g_restoreRecovered.store(0); |
| 1145 | // Set before the spawn, not after: RestoreThread clears this | ||
| 1146 | // flag itself when it finishes, and a short restore can beat | ||
| 1147 | // us back here and have its `false` overwritten, leaving the | ||
| 1148 | // dialog disabled forever. | ||
| 1136 | g_restoreRunning.store(true); | 1149 | g_restoreRunning.store(true); |
| 1137 | // On its own thread: a restore walks the whole chain once per | 1150 | // On its own thread: a restore walks the whole chain once per |
| 1138 | // batch and would otherwise freeze the window for as long as it | 1151 | // batch and would otherwise freeze the window for as long as it |
| 1139 | // takes. | 1152 | // takes. A refused spawn is one restore that does not happen, |
| 1140 | std::thread(RestoreThread, std::string(g_restoreInput)).detach(); | 1153 | // not a reason to take the window down with an uncaught |
| 1154 | // std::system_error -- and the flag has to come back off, or | ||
| 1155 | // the dialog sits greyed out reporting a search nobody is | ||
| 1156 | // running. | ||
| 1157 | try | ||
| 1158 | { | ||
| 1159 | std::thread(RestoreThread, std::string(g_restoreInput)).detach(); | ||
| 1160 | } | ||
| 1161 | catch (const std::system_error& e) | ||
| 1162 | { | ||
| 1163 | g_restoreRunning.store(false); | ||
| 1164 | std::lock_guard<std::mutex> lock(g_restoreResultMutex); | ||
| 1165 | g_restoreResult = std::string("Could not start the search: ") + e.what(); | ||
| 1166 | } | ||
| 1141 | } | 1167 | } |
| 1142 | } | 1168 | } |
| 1143 | if (fBusy) | 1169 | if (fBusy) |
diff --git a/src/headers_core.h b/src/headers_core.h index f890c2f..2824126 100644 --- a/src/headers_core.h +++ b/src/headers_core.h | |||
| @@ -71,7 +71,6 @@ inline typename std::common_type<T1,T2>::type max(const T1& a, const T2& b) | |||
| 71 | #include "nostr.h" | 71 | #include "nostr.h" |
| 72 | #include "randomx_pow.h" | 72 | #include "randomx_pow.h" |
| 73 | #include "main.h" | 73 | #include "main.h" |
| 74 | #include "market.h" | ||
| 75 | 74 | ||
| 76 | // GUI callback -- implemented in gui.cpp (or as no-op in headless builds) | 75 | // GUI callback -- implemented in gui.cpp (or as no-op in headless builds) |
| 77 | void MainFrameRepaint(); | 76 | void MainFrameRepaint(); |
diff --git a/src/main.cpp b/src/main.cpp index 5d266ff..0fa6b89 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | #include <atomic> | 14 | #include <atomic> |
| 15 | #include <mutex> | 15 | #include <mutex> |
| 16 | #include <thread> | 16 | #include <thread> |
| 17 | #include <system_error> | ||
| 17 | #include "bip32.h" | 18 | #include "bip32.h" |
| 18 | #include "btfaddr.h" | 19 | #include "btfaddr.h" |
| 19 | #include "btftunnel.h" | 20 | #include "btftunnel.h" |
| @@ -82,6 +83,137 @@ CBlockIndex* pindexBest = NULL; | |||
| 82 | map<uint256, CBlock*> mapOrphanBlocks; | 83 | map<uint256, CBlock*> mapOrphanBlocks; |
| 83 | multimap<uint256, CBlock*> mapOrphanBlocksByPrev; | 84 | multimap<uint256, CBlock*> mapOrphanBlocksByPrev; |
| 84 | 85 | ||
| 86 | // Block bodies we have sent a getdata for and not yet received, so the | ||
| 87 | // failover pass in SendMessages can tell "requested and being served" from | ||
| 88 | // "requested and gone quiet" and re-issue the request to a different peer | ||
| 89 | // (#33). Keyed by block hash; setAsked is the peers already tried since the | ||
| 90 | // round last restarted, so the retry rotates instead of hammering the | ||
| 91 | // announcer that is not serving. Guarded by cs_main, which both the message | ||
| 92 | // handlers and SendMessages already hold. | ||
| 93 | // | ||
| 94 | // Capped small on purpose. At the tip there is one block in flight; during | ||
| 95 | // initial download there are legitimately hundreds outstanding to one peer, | ||
| 96 | // and rotating those would duplicate the whole download to every other peer. | ||
| 97 | // | ||
| 98 | // The cap is a shared resource and nothing on the path from inv to insertion | ||
| 99 | // can tell a real block hash from 32 random bytes -- AlreadyHave only asks | ||
| 100 | // whether we already hold the block, and there is no header to check work | ||
| 101 | // against. So the table does not refuse a new hash when it is full; it makes | ||
| 102 | // room. See MakeRoomInBlockFetch for the rule, which is what stops one peer | ||
| 103 | // filling all eight slots and switching failover off for everybody. | ||
| 104 | // | ||
| 105 | // The initialisers matter: the insertion path uses operator[] and then tests | ||
| 106 | // nTimeFirstAsked == 0 to detect a fresh entry. That worked on | ||
| 107 | // value-initialisation alone, but only for as long as nobody gave this struct | ||
| 108 | // a constructor. | ||
| 109 | struct CBlockFetch | ||
| 110 | { | ||
| 111 | int64 nTimeFirstAsked = 0; // when the first getdata for this block went out | ||
| 112 | int64 nTimeAsked = 0; // when the latest getdata went out | ||
| 113 | CAddress addrAnnounced; // peer whose announcement put this hash here | ||
| 114 | set<CAddress> setAsked; // peers tried since the round last restarted | ||
| 115 | }; | ||
| 116 | map<uint256, CBlockFetch> mapBlockFetch; | ||
| 117 | static const int BLOCK_FETCH_TRACK_MAX = 8; | ||
| 118 | // A block nobody has served for this long is not a fetch problem: the hash may | ||
| 119 | // have been announced and never existed. Drop the entry rather than getdata | ||
| 120 | // forever; a re-announcement recreates it. | ||
| 121 | static const int BLOCK_FETCH_GIVEUP_SECS = 10 * 60; | ||
| 122 | |||
| 123 | // Failover firing at all is worth saying out loud: it means peers are | ||
| 124 | // announcing blocks they then do not serve, which is the pathology of #33 and | ||
| 125 | // the thing nobody could otherwise see. Naming the peer on every re-request is | ||
| 126 | // a different matter. A peer can drive that line as often as it likes by | ||
| 127 | // announcing hashes it will not serve, and what it prints is a peer address, | ||
| 128 | // which is the one thing the .btf transport otherwise takes trouble not to | ||
| 129 | // spread around. So the per-peer detail sits behind the net category and the | ||
| 130 | // unconditional line is a count over a window no peer can shorten: the first | ||
| 131 | // failover after a quiet spell prints at once, because the change from "blocks | ||
| 132 | // arrive" to "blocks are announced and not served" is the event worth seeing, | ||
| 133 | // and everything after it is summarised once a minute. | ||
| 134 | static const int BLOCK_FETCH_REPORT_SECS = 60; | ||
| 135 | static int64 nBlockFetchWindowStart = 0; | ||
| 136 | static int nBlockFetchFailovers = 0; | ||
| 137 | |||
| 138 | // How stale our own best block may be before we stop calling this the tip. | ||
| 139 | // Blocks target two minutes, so this is thirty intervals: far longer than any | ||
| 140 | // stall worth failing over from -- the one in #33 ran sixteen minutes -- and | ||
| 141 | // far shorter than catching up, which starts hours or days back. | ||
| 142 | static const int BLOCK_FETCH_TIP_AGE_SECS = 60 * 60; | ||
| 143 | |||
| 144 | // Whether this node is tracking the tip rather than still catching up. | ||
| 145 | // | ||
| 146 | // Measured off the age of our own best block, not off any height a peer told | ||
| 147 | // us. nStartingHeight is optional in the version message and every node | ||
| 148 | // released so far sends the short form, so it is -1 for essentially every | ||
| 149 | // peer; and where it is filled in later it is filled in from the height of a | ||
| 150 | // block that peer served us, which during catch-up tracks our own height and | ||
| 151 | // would report "at the tip" for the whole download. Block timestamps are the | ||
| 152 | // one clock here that peers cannot move: CheckBlock refuses anything more than | ||
| 153 | // two hours ahead and AcceptBlock refuses anything at or before the previous | ||
| 154 | // median, so the only way to make this read "at the tip" falsely is to mine, | ||
| 155 | // and all that buys is the 15s spacing this code had unconditionally anyway. | ||
| 156 | bool IsBlockFetchNearTip() | ||
| 157 | { | ||
| 158 | return pindexBest && (int64)pindexBest->nTime > GetTime() - BLOCK_FETCH_TIP_AGE_SECS; | ||
| 159 | } | ||
| 160 | |||
| 161 | // Drop one entry so a hash announced by addrNew can be tracked. | ||
| 162 | // | ||
| 163 | // Refusing the insertion instead was a remote off-switch for the whole | ||
| 164 | // feature. An inv carrying eight hashes that never existed filled every slot | ||
| 165 | // -- nothing between inv and here can tell those from real blocks -- and no | ||
| 166 | // genuinely new block was tracked again until the give-up timer expired ten | ||
| 167 | // minutes later, re-armable for one message per ten minutes from a single | ||
| 168 | // connection. | ||
| 169 | // | ||
| 170 | // So evict instead, by occupancy: a peer may only take a slot from a peer | ||
| 171 | // holding strictly more slots than itself, and otherwise recycles one of its | ||
| 172 | // own. That is what keeps eviction from becoming the same attack in a new | ||
| 173 | // shape. A peer that has filled the table is by definition the heaviest | ||
| 174 | // holder, so its own entries are the only ones it can displace, while a peer | ||
| 175 | // announcing its first block holds nothing and therefore always evicts from | ||
| 176 | // the heaviest -- a genuine announcement is never refused a slot, whatever the | ||
| 177 | // attacker holds or however many connections it holds it from. The one case | ||
| 178 | // that costs a real fetch is eight distinct peers holding one entry each, | ||
| 179 | // where a ninth takes the oldest; that is a busy healthy node, not an attack, | ||
| 180 | // and losing the oldest of eight beats refusing the ninth. | ||
| 181 | static void MakeRoomInBlockFetch(const CAddress& addrNew) | ||
| 182 | { | ||
| 183 | if (mapBlockFetch.empty()) | ||
| 184 | return; | ||
| 185 | |||
| 186 | map<CAddress, int> mapHeld; | ||
| 187 | for (map<uint256, CBlockFetch>::iterator mi = mapBlockFetch.begin(); mi != mapBlockFetch.end(); ++mi) | ||
| 188 | mapHeld[(*mi).second.addrAnnounced]++; | ||
| 189 | |||
| 190 | int nMine = mapHeld.count(addrNew) ? mapHeld[addrNew] : 0; | ||
| 191 | |||
| 192 | int nMax = 0; | ||
| 193 | CAddress addrVictim; | ||
| 194 | for (map<CAddress, int>::iterator mi = mapHeld.begin(); mi != mapHeld.end(); ++mi) | ||
| 195 | if ((*mi).second > nMax) | ||
| 196 | { | ||
| 197 | nMax = (*mi).second; | ||
| 198 | addrVictim = (*mi).first; | ||
| 199 | } | ||
| 200 | |||
| 201 | // Nobody is holding more than this peer, so it recycles its own oldest | ||
| 202 | // rather than taking a slot off an equal. | ||
| 203 | if (nMax <= nMine) | ||
| 204 | addrVictim = addrNew; | ||
| 205 | |||
| 206 | map<uint256, CBlockFetch>::iterator miVictim = mapBlockFetch.end(); | ||
| 207 | for (map<uint256, CBlockFetch>::iterator mi = mapBlockFetch.begin(); mi != mapBlockFetch.end(); ++mi) | ||
| 208 | if ((*mi).second.addrAnnounced == addrVictim && | ||
| 209 | (miVictim == mapBlockFetch.end() || | ||
| 210 | (*mi).second.nTimeFirstAsked < (*miVictim).second.nTimeFirstAsked)) | ||
| 211 | miVictim = mi; | ||
| 212 | |||
| 213 | if (miVictim != mapBlockFetch.end()) | ||
| 214 | mapBlockFetch.erase(miVictim); | ||
| 215 | } | ||
| 216 | |||
| 85 | map<uint256, CDataStream*> mapOrphanTransactions; | 217 | map<uint256, CDataStream*> mapOrphanTransactions; |
| 86 | multimap<uint256, CDataStream*> mapOrphanTransactionsByPrev; | 218 | multimap<uint256, CDataStream*> mapOrphanTransactionsByPrev; |
| 87 | 219 | ||
| @@ -105,6 +237,8 @@ CKey keyUser; | |||
| 105 | string strSetDataDir; | 237 | string strSetDataDir; |
| 106 | int nDropMessagesTest = 0; | 238 | int nDropMessagesTest = 0; |
| 107 | bool fSoloMineTest = false; // /solomine: mine without requiring a peer (local test) | 239 | bool fSoloMineTest = false; // /solomine: mine without requiring a peer (local test) |
| 240 | bool fPoolNoAnnounce = false; // /noannounce: operator serves miners but never publishes pool announcements | ||
| 241 | bool fPoolMineBlind = false; // /poolmineblind: issue jobs even when no peer has reported a height | ||
| 108 | 242 | ||
| 109 | // Settings | 243 | // Settings |
| 110 | int fGenerateBitcoins; | 244 | int fGenerateBitcoins; |
| @@ -120,6 +254,9 @@ CAddress addrIncoming; | |||
| 120 | int nMineMode = MINE_RELAY; | 254 | int nMineMode = MINE_RELAY; |
| 121 | bool fMineModeFromCommandLine = false; | 255 | bool fMineModeFromCommandLine = false; |
| 122 | string strParticipantPool; // participant mode: pool .btf address | 256 | string strParticipantPool; // participant mode: pool .btf address |
| 257 | string strPayoutAddress; // participant: /payoutaddr override (mine without a wallet) | ||
| 258 | string strPayoutFile; // participant: /payoutfile with one address per line, rotated per reconnect | ||
| 259 | int nHashThreads = 0; // participant: /hashthreads override (0 = cores-1) | ||
| 123 | string strPoolName = "Bitflash Pool"; | 260 | string strPoolName = "Bitflash Pool"; |
| 124 | string strPoolDashboardUrl; | 261 | string strPoolDashboardUrl; |
| 125 | string strPoolStatusFile; | 262 | string strPoolStatusFile; |
| @@ -238,12 +375,32 @@ int MinerThreadCount() | |||
| 238 | { | 375 | { |
| 239 | if (nMinerThreads > 0) | 376 | if (nMinerThreads > 0) |
| 240 | return nMinerThreads; | 377 | return nMinerThreads; |
| 241 | unsigned int nCores = std::thread::hardware_concurrency(); | 378 | unsigned int nCores = AvailableCoreCount(); |
| 242 | if (nCores <= 1) | 379 | if (nCores <= 1) |
| 243 | return 1; | 380 | return 1; |
| 244 | return (int)(nCores - 1); | 381 | return (int)(nCores - 1); |
| 245 | } | 382 | } |
| 246 | 383 | ||
| 384 | // Hash threads one participant session runs. | ||
| 385 | // | ||
| 386 | // Explicit /hashthreads=N is honored exactly, including 1 -- a miner who says | ||
| 387 | // one means one. Automatic leaves a core to the node, the Stratum I/O loop and | ||
| 388 | // everything else the process still has to do, for the same reason | ||
| 389 | // MinerThreadCount() does. | ||
| 390 | // | ||
| 391 | // Lives here, next to that function, because the two are easy to confuse and | ||
| 392 | // StartMinerThreads() needs to be able to tell them apart: only ONE session | ||
| 393 | // runs in participant mode, and this is what sizes it. | ||
| 394 | int ParticipantHashThreadCount() | ||
| 395 | { | ||
| 396 | if (nHashThreads > 0) | ||
| 397 | return nHashThreads; | ||
| 398 | int nWorkers = (int)AvailableCoreCount(); | ||
| 399 | if (nWorkers < 2) | ||
| 400 | nWorkers = 2; | ||
| 401 | return nWorkers - 1; | ||
| 402 | } | ||
| 403 | |||
| 247 | static void SecureClearBytes(vector<unsigned char>& vch); | 404 | static void SecureClearBytes(vector<unsigned char>& vch); |
| 248 | static void SecureClearKeyingMaterial(CKeyingMaterial& vch); | 405 | static void SecureClearKeyingMaterial(CKeyingMaterial& vch); |
| 249 | 406 | ||
| @@ -1626,7 +1783,7 @@ int CMerkleTx::GetBlocksToMaturity() const | |||
| 1626 | { | 1783 | { |
| 1627 | if (!IsCoinBase()) | 1784 | if (!IsCoinBase()) |
| 1628 | return 0; | 1785 | return 0; |
| 1629 | return max(0, (COINBASE_MATURITY+20) - GetDepthInMainChain()); | 1786 | return max(0, COINBASE_SPENDABLE_DEPTH - GetDepthInMainChain()); |
| 1630 | } | 1787 | } |
| 1631 | 1788 | ||
| 1632 | 1789 | ||
| @@ -1762,7 +1919,7 @@ void CWalletTx::RelayWalletTransaction(CTxDB& txdb) | |||
| 1762 | uint256 hash = GetHash(); | 1919 | uint256 hash = GetHash(); |
| 1763 | if (!txdb.ContainsTx(hash)) | 1920 | if (!txdb.ContainsTx(hash)) |
| 1764 | { | 1921 | { |
| 1765 | if (LogAcceptsCategory("net")) printf("Relaying wtx %s\n", hash.ToString().substr(0,6).c_str()); | 1922 | if (LogAcceptsCategory("relay")) printf("Relaying wtx %s\n", hash.ToString().substr(0,6).c_str()); |
| 1766 | RelayMessage(CInv(MSG_TX, hash), (CTransaction)*this); | 1923 | RelayMessage(CInv(MSG_TX, hash), (CTransaction)*this); |
| 1767 | } | 1924 | } |
| 1768 | } | 1925 | } |
| @@ -2738,7 +2895,35 @@ bool LoadBlockIndex(bool fAllowNew) | |||
| 2738 | // CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B) | 2895 | // CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B) |
| 2739 | // vMerkleTree: 4a5e1e | 2896 | // vMerkleTree: 4a5e1e |
| 2740 | 2897 | ||
| 2741 | // Bitflash genesis block -- own header (fair launch, 2026 stable relaunch) | 2898 | // Bitflash genesis block -- own header (fair launch) |
| 2899 | // | ||
| 2900 | // Upstream moved the construction into BuildGenesisBlock() so mainnet | ||
| 2901 | // and the isolated testnet can share it. The mainnet inputs it uses are | ||
| 2902 | // byte-identical to what this block built inline before the merge -- | ||
| 2903 | // timestamp string, nTime, nNonce, scriptPubKey, nBits and nVersion all | ||
| 2904 | // verified equal against MAINNET_GENESIS_* -- so the mainnet genesis | ||
| 2905 | // hash is unchanged. The warning below is why that was checked rather | ||
| 2906 | // than assumed, and it still applies to those constants: | ||
| 2907 | // | ||
| 2908 | // Do not change MAINNET_GENESIS_TIMESTAMP, the scriptPubKey, | ||
| 2909 | // MAINNET_GENESIS_TIME or MAINNET_GENESIS_NONCE. They are the inputs to | ||
| 2910 | // the genesis hash, and the genesis hash is consensus. Changing one on | ||
| 2911 | // its own trips the assert below, which is loud and reverts cleanly; | ||
| 2912 | // changing one and regenerating GENESIS_NONCE and hashGenesisBlock to | ||
| 2913 | // match -- which is exactly what the re-mining branch below prints out | ||
| 2914 | // and invites -- gives a node that starts without complaint, builds its | ||
| 2915 | // own chain from height 0, and rejects every real peer as being on a | ||
| 2916 | // different chain. That one fails silently, and the damage scales with | ||
| 2917 | // how many people run the "fixed" build. | ||
| 2918 | // | ||
| 2919 | // MAINNET_GENESIS_TIME is 1753315200, which is 2025-07-24T00:00:00Z, | ||
| 2920 | // while the headline in MAINNET_GENESIS_TIMESTAMP reads 2026 -- as does | ||
| 2921 | // the comment upstream put on that constant, which is wrong about the | ||
| 2922 | // year. This is a known discrepancy and not a typo in the value: it is | ||
| 2923 | // only consensus-critical as an input to the genesis hash, the block | ||
| 2924 | // hashes to hashGenesisBlock with it, and every node on the network | ||
| 2925 | // agrees. The mismatch is in the launched chain and cannot be corrected | ||
| 2926 | // -- see the warning above before deciding otherwise. | ||
| 2742 | CBlock block = BuildGenesisBlock(); | 2927 | CBlock block = BuildGenesisBlock(); |
| 2743 | 2928 | ||
| 2744 | // If the nonce has not been found yet, mine the genesis once (RandomX) | 2929 | // If the nonce has not been found yet, mine the genesis once (RandomX) |
| @@ -2764,6 +2949,11 @@ bool LoadBlockIndex(bool fAllowNew) | |||
| 2764 | printf("powhash = %s\n", block.GetPoWHash().ToString().c_str()); | 2949 | printf("powhash = %s\n", block.GetPoWHash().ToString().c_str()); |
| 2765 | printf("hash = %s\n", block.GetHash().ToString().c_str()); | 2950 | printf("hash = %s\n", block.GetHash().ToString().c_str()); |
| 2766 | printf("merkle = %s\n", block.hashMerkleRoot.ToString().c_str()); | 2951 | printf("merkle = %s\n", block.hashMerkleRoot.ToString().c_str()); |
| 2952 | printf("----------------------------------------\n"); | ||
| 2953 | printf("These constants bring up a NEW chain. Do not paste them\n"); | ||
| 2954 | printf("into a build for this network to make an edited nTime or\n"); | ||
| 2955 | printf("pszTimestamp fit -- a node whose genesis differs starts\n"); | ||
| 2956 | printf("cleanly on a chain of its own and rejects every real peer.\n"); | ||
| 2767 | printf("========================================\n"); | 2957 | printf("========================================\n"); |
| 2768 | } | 2958 | } |
| 2769 | 2959 | ||
| @@ -2890,9 +3080,10 @@ bool AlreadyHave(CTxDB& txdb, const CInv& inv) | |||
| 2890 | { | 3080 | { |
| 2891 | case MSG_TX: return mapTransactions.count(inv.hash) || txdb.ContainsTx(inv.hash); | 3081 | case MSG_TX: return mapTransactions.count(inv.hash) || txdb.ContainsTx(inv.hash); |
| 2892 | case MSG_BLOCK: return mapBlockIndex.count(inv.hash) || mapOrphanBlocks.count(inv.hash); | 3082 | case MSG_BLOCK: return mapBlockIndex.count(inv.hash) || mapOrphanBlocks.count(inv.hash); |
| 2893 | case MSG_REVIEW: return true; | ||
| 2894 | case MSG_PRODUCT: return mapProducts.count(inv.hash); | ||
| 2895 | } | 3083 | } |
| 3084 | // MSG_REVIEW / MSG_PRODUCT / MSG_TABLE are the 2009 marketplace inventory | ||
| 3085 | // types. Nothing announces or serves them any more (see ProcessMessage), | ||
| 3086 | // so they fall through to the default and are never requested. | ||
| 2896 | // Don't know what it is, just say we already got one | 3087 | // Don't know what it is, just say we already got one |
| 2897 | return true; | 3088 | return true; |
| 2898 | } | 3089 | } |
| @@ -2935,11 +3126,13 @@ bool ProcessMessages(CNode* pfrom) | |||
| 2935 | if (LogAcceptsCategory("net")) printf("\n\nPROCESSMESSAGE SKIPPED %d BYTES\n\n", (int)(pstart - vRecv.begin())); | 3126 | if (LogAcceptsCategory("net")) printf("\n\nPROCESSMESSAGE SKIPPED %d BYTES\n\n", (int)(pstart - vRecv.begin())); |
| 2936 | vRecv.erase(vRecv.begin(), pstart); | 3127 | vRecv.erase(vRecv.begin(), pstart); |
| 2937 | 3128 | ||
| 2938 | // Read the header from a copy first. The old path consumed the header, | 3129 | // Read the header from a copy first, and do not consume it until the |
| 2939 | // noticed the payload was incomplete, inserted the header back at the | 3130 | // whole body is here. The old path consumed the header, noticed the |
| 2940 | // front of the receive buffer, then slept inside the shared message | 3131 | // payload was incomplete, inserted the header back at the front of the |
| 2941 | // thread. A peer could announce a large payload and drip bytes forever, | 3132 | // receive buffer, then slept inside the shared message thread. A peer |
| 2942 | // making every pass pay for an O(n) insert and a 100 ms stall. | 3133 | // could announce a large payload and drip bytes forever, making every |
| 3134 | // pass pay for an O(n) insert and a 100 ms stall -- a cheap way for one | ||
| 3135 | // peer to slow the thread that serves all of them. (upstream #109) | ||
| 2943 | CMessageHeader hdr; | 3136 | CMessageHeader hdr; |
| 2944 | CDataStream vHeader(vRecv.begin(), vRecv.begin() + sizeof(CMessageHeader), | 3137 | CDataStream vHeader(vRecv.begin(), vRecv.begin() + sizeof(CMessageHeader), |
| 2945 | vRecv.nType, vRecv.nVersion); | 3138 | vRecv.nType, vRecv.nVersion); |
| @@ -2949,7 +3142,10 @@ bool ProcessMessages(CNode* pfrom) | |||
| 2949 | if (LogAcceptsCategory("net")) printf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand().c_str()); | 3142 | if (LogAcceptsCategory("net")) printf("\n\nPROCESSMESSAGE: ERRORS IN HEADER %s\n\n\n", hdr.GetCommand().c_str()); |
| 2950 | if (hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_SIZE) | 3143 | if (hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_SIZE) |
| 2951 | pfrom->fDisconnect = true; | 3144 | pfrom->fDisconnect = true; |
| 2952 | pfrom->nIncompleteMessageStart = 0; | 3145 | pfrom->nMsgWaitSince = 0; |
| 3146 | pfrom->nMsgWaitSize = 0; | ||
| 3147 | // The header was read from a copy, so it is still in vRecv. Skip it | ||
| 3148 | // explicitly or this loop never advances. | ||
| 2953 | vRecv.ignore(sizeof(CMessageHeader)); | 3149 | vRecv.ignore(sizeof(CMessageHeader)); |
| 2954 | continue; | 3150 | continue; |
| 2955 | } | 3151 | } |
| @@ -2960,30 +3156,63 @@ bool ProcessMessages(CNode* pfrom) | |||
| 2960 | unsigned int nPayloadAvailable = vRecv.size() - sizeof(CMessageHeader); | 3156 | unsigned int nPayloadAvailable = vRecv.size() - sizeof(CMessageHeader); |
| 2961 | if (nMessageSize > nPayloadAvailable) | 3157 | if (nMessageSize > nPayloadAvailable) |
| 2962 | { | 3158 | { |
| 2963 | if (pfrom->nIncompleteMessageStart == 0 || | 3159 | // Wait for the rest of the message. The header stays in vRecv. |
| 2964 | pfrom->nIncompleteMessageSize != nMessageSize || | 3160 | // |
| 2965 | pfrom->strIncompleteMessageCommand != strCommand) | 3161 | // This is the mechanism the original note asked for: "need a |
| 3162 | // mechanism to give up waiting for overlong message size error". | ||
| 3163 | // Without one the wait is unbounded, and unbounded here means | ||
| 3164 | // permanent. The header stays at the front of vRecv and the loop | ||
| 3165 | // breaks; if the body never completes, every later pass re-reads | ||
| 3166 | // that same header and breaks in the same place. The peer is never | ||
| 3167 | // parsed again -- no blocks, no inv, not even its version message, | ||
| 3168 | // so it never reports a height either, which is why a wedged node | ||
| 3169 | // has peers and GetPeerMedianHeight() still returns -1. | ||
| 3170 | // | ||
| 3171 | // Nothing rescues it. A header could claim up to 256 MB and still | ||
| 3172 | // pass CMessageHeader::IsValid() -- now capped at | ||
| 3173 | // MAX_PROTOCOL_MESSAGE_SIZE -- and any byte that arrives | ||
| 3174 | // refreshes nLastRecv, so the inactivity timeout that drops a | ||
| 3175 | // silent peer never fires on one that is merely unreadable. The | ||
| 3176 | // socket stays ESTABLISHED and select() keeps offering it. | ||
| 3177 | // Measured on a wedged pool: this branch ran 2014 times in the | ||
| 3178 | // deaf window, against 607 in the healthy window before it. | ||
| 3179 | if (LogAcceptsCategory("net")) printf("MESSAGE-BREAK 2\n"); | ||
| 3180 | // | ||
| 3181 | // Time the STALL, not the wait. A big message on a slow link is | ||
| 3182 | // still arriving, and dropping that peer would be a bug of our own | ||
| 3183 | // making; what must be caught is a buffer that has stopped growing | ||
| 3184 | // while we go on expecting more. So the clock restarts whenever | ||
| 3185 | // even one more byte has landed since the last look. | ||
| 3186 | // | ||
| 3187 | // Upstream #109 times this differently: it restarts the clock only | ||
| 3188 | // when the (size, command) pair changes, which drops a peer sending | ||
| 3189 | // one big message slowly but honestly. Progress is the better test, | ||
| 3190 | // so that half is kept as it was. | ||
| 3191 | int64 nNow = GetTime(); | ||
| 3192 | if (pfrom->nMsgWaitSince == 0 || nPayloadAvailable > pfrom->nMsgWaitSize) | ||
| 2966 | { | 3193 | { |
| 2967 | pfrom->nIncompleteMessageStart = GetTime(); | 3194 | pfrom->nMsgWaitSince = nNow; |
| 2968 | pfrom->nIncompleteMessageSize = nMessageSize; | 3195 | pfrom->nMsgWaitSize = nPayloadAvailable; |
| 2969 | pfrom->strIncompleteMessageCommand = strCommand; | ||
| 2970 | } | 3196 | } |
| 2971 | else if (GetTime() - pfrom->nIncompleteMessageStart > | 3197 | else if (nNow - pfrom->nMsgWaitSince > BTF_MSG_WAIT_MAX_SECS) |
| 2972 | BTF_INCOMPLETE_MESSAGE_TIMEOUT_SECS) | ||
| 2973 | { | 3198 | { |
| 2974 | LogPrint("net", | 3199 | // Deliberately not behind a debug category. This is the node |
| 2975 | "disconnecting %s: incomplete %s message, %u of %u bytes after %d seconds\n", | 3200 | // going deaf to a peer, which is the thing nobody could see. |
| 2976 | pfrom->addr.ToString().c_str(), strCommand.c_str(), | 3201 | printf("peer %s announced a %u byte '%s' and has sent nothing more " |
| 2977 | nPayloadAvailable, nMessageSize, | 3202 | "for %d s (%d of %u payload bytes) -- dropping, this peer is unreadable\n", |
| 2978 | BTF_INCOMPLETE_MESSAGE_TIMEOUT_SECS); | 3203 | pfrom->addr.ToString().c_str(), nMessageSize, |
| 3204 | strCommand.c_str(), (int)(nNow - pfrom->nMsgWaitSince), | ||
| 3205 | (int)nPayloadAvailable, nMessageSize); | ||
| 2979 | pfrom->fDisconnect = true; | 3206 | pfrom->fDisconnect = true; |
| 2980 | } | 3207 | } |
| 3208 | // No re-insert, no Sleep(): the header was never consumed, so there | ||
| 3209 | // is nothing to put back, and sleeping here stalls every peer. | ||
| 2981 | break; | 3210 | break; |
| 2982 | } | 3211 | } |
| 2983 | pfrom->nIncompleteMessageStart = 0; | ||
| 2984 | pfrom->nIncompleteMessageSize = 0; | ||
| 2985 | pfrom->strIncompleteMessageCommand.clear(); | ||
| 2986 | vRecv.ignore(sizeof(CMessageHeader)); | 3212 | vRecv.ignore(sizeof(CMessageHeader)); |
| 3213 | // The body arrived, so this peer is readable again. | ||
| 3214 | pfrom->nMsgWaitSince = 0; | ||
| 3215 | pfrom->nMsgWaitSize = 0; | ||
| 2987 | 3216 | ||
| 2988 | // Copy message to its own buffer | 3217 | // Copy message to its own buffer |
| 2989 | CDataStream vMsg(vRecv.begin(), vRecv.begin() + nMessageSize, vRecv.nType, vRecv.nVersion); | 3218 | CDataStream vMsg(vRecv.begin(), vRecv.begin() + nMessageSize, vRecv.nType, vRecv.nVersion); |
| @@ -3012,7 +3241,6 @@ bool ProcessMessages(CNode* pfrom) | |||
| 3012 | 3241 | ||
| 3013 | bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) | 3242 | bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) |
| 3014 | { | 3243 | { |
| 3015 | static map<unsigned int, vector<unsigned char> > mapReuseKey; | ||
| 3016 | if (LogAcceptsCategory("net")) { printf("received: %-12s (%d bytes) ", strCommand.c_str(), (int)vRecv.size()); for (int i = 0; i < min(vRecv.size(), (unsigned int)25); i++) printf("%02x ", vRecv[i] & 0xff); printf("\n"); } | 3244 | if (LogAcceptsCategory("net")) { printf("received: %-12s (%d bytes) ", strCommand.c_str(), (int)vRecv.size()); for (int i = 0; i < min(vRecv.size(), (unsigned int)25); i++) printf("%02x ", vRecv[i] & 0xff); printf("\n"); } |
| 3017 | if (nDropMessagesTest > 0 && GetRand(nDropMessagesTest) == 0) | 3245 | if (nDropMessagesTest > 0 && GetRand(nDropMessagesTest) == 0) |
| 3018 | { | 3246 | { |
| @@ -3251,22 +3479,18 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) | |||
| 3251 | } | 3479 | } |
| 3252 | 3480 | ||
| 3253 | 3481 | ||
| 3254 | else if (strCommand == "review") | 3482 | // "review" used to be handled here. It took a CReview off the wire from an |
| 3255 | { | 3483 | // unauthenticated peer, checked a signature the sender had made with a key |
| 3256 | CDataStream vMsg(vRecv); | 3484 | // of its own choosing -- which proves possession of that key and authorises |
| 3257 | CReview review; | 3485 | // nothing -- and then appended the message to reviews.dat by reading the |
| 3258 | vRecv >> review; | 3486 | // whole vector for the target hash, pushing one entry, and writing all of |
| 3259 | 3487 | // it back. That is quadratic disk for a stream an attacker controls, on a | |
| 3260 | CInv inv(MSG_REVIEW, review.GetHash()); | 3488 | // record whose mapValue is an unbounded map<string,string> read straight |
| 3261 | pfrom->AddInventoryKnown(inv); | 3489 | // from the socket, followed by AddAtomsAndPropagate walking a link graph |
| 3262 | 3490 | // the same peer supplied, and finally RelayMessage forwarding it to every | |
| 3263 | if (review.AcceptReview()) | 3491 | // other node. Unbounded remote disk growth with network amplification and |
| 3264 | { | 3492 | // no authorisation anywhere in the path. The marketplace it belonged to has |
| 3265 | // Relay the original message as-is in case it's a higher version than we know how to parse | 3493 | // not existed since 2009; the handler is gone rather than bounded. |
| 3266 | RelayMessage(inv, vMsg); | ||
| 3267 | mapAlreadyAskedFor.erase(inv); | ||
| 3268 | } | ||
| 3269 | } | ||
| 3270 | 3494 | ||
| 3271 | 3495 | ||
| 3272 | else if (strCommand == "block") | 3496 | else if (strCommand == "block") |
| @@ -3275,47 +3499,98 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) | |||
| 3275 | vRecv >> *pblock; | 3499 | vRecv >> *pblock; |
| 3276 | 3500 | ||
| 3277 | //// debug print | 3501 | //// debug print |
| 3278 | if (LogAcceptsCategory("net")) { printf("received block:\n"); pblock->print(); } | 3502 | if (LogAcceptsCategory("relay")) { printf("received block:\n"); pblock->print(); } |
| 3279 | 3503 | ||
| 3280 | CInv inv(MSG_BLOCK, pblock->GetHash()); | 3504 | CInv inv(MSG_BLOCK, pblock->GetHash()); |
| 3281 | pfrom->AddInventoryKnown(inv); | 3505 | pfrom->AddInventoryKnown(inv); |
| 3282 | 3506 | ||
| 3507 | // GetHash() covers the 80-byte header and nothing else, so the hash | ||
| 3508 | // coming out right does not make this the block we asked for. Every | ||
| 3509 | // peer that saw X announced holds X's header, and can answer our | ||
| 3510 | // getdata with that header bolted onto a transaction list of its own | ||
| 3511 | // invention -- the reply still hashes to X. hashMerkleRoot is the only | ||
| 3512 | // thing binding the header to the body, so check it here, before the | ||
| 3513 | // mapBlockFetch entry is touched and before CheckBlock spends a | ||
| 3514 | // RandomX hash on a body already known to be forged. | ||
| 3515 | // | ||
| 3516 | // There is no honest way to arrive here. Peer traffic rides the .btf | ||
| 3517 | // channel, which is XChaCha20-Poly1305, so a body that arrives at all | ||
| 3518 | // arrived intact: a mismatch is the peer's own construction, not line | ||
| 3519 | // noise. Drop the peer and leave the fetch outstanding so the failover | ||
| 3520 | // sweep asks somebody else for the real X, rather than recording X as | ||
| 3521 | // delivered on the strength of a header anyone could copy. | ||
| 3522 | if (pblock->hashMerkleRoot != pblock->BuildMerkleTree()) | ||
| 3523 | { | ||
| 3524 | if (LogAcceptsCategory("net")) | ||
| 3525 | printf("peer %s served a forged body for %s\n", | ||
| 3526 | pfrom->addr.ToString().c_str(), inv.ToString().c_str()); | ||
| 3527 | // One line per connection: ProcessMessages keeps draining the | ||
| 3528 | // receive buffer after fDisconnect is set, so a peer that packs | ||
| 3529 | // many forged bodies into one buffer must not get one log line | ||
| 3530 | // for each of them. | ||
| 3531 | if (!pfrom->fDisconnect) | ||
| 3532 | printf("block fetch: a peer answered the request for %s with a body that is not " | ||
| 3533 | "that block -- dropping it\n", inv.hash.ToString().substr(0,14).c_str()); | ||
| 3534 | pfrom->fDisconnect = true; | ||
| 3535 | return error("ProcessMessage() : forged block body for %s", | ||
| 3536 | inv.hash.ToString().substr(0,14).c_str()); | ||
| 3537 | } | ||
| 3538 | |||
| 3283 | if (ProcessBlock(pfrom, pblock.release())) | 3539 | if (ProcessBlock(pfrom, pblock.release())) |
| 3540 | { | ||
| 3284 | mapAlreadyAskedFor.erase(inv); | 3541 | mapAlreadyAskedFor.erase(inv); |
| 3285 | } | ||
| 3286 | |||
| 3287 | 3542 | ||
| 3288 | else if (strCommand == "checkorder" || strCommand == "submitorder") | 3543 | // The body arrived and validated, so the fetch really is over: |
| 3289 | { | 3544 | // the block is now in mapBlockIndex, or parked in mapOrphanBlocks, |
| 3290 | // Legacy Bitcoin 0.1 marketplace messages are not part of Bitflash's | 3545 | // and either way AlreadyHave answers for it from here on. |
| 3291 | // supported protocol. They used to let a remote peer make this wallet | 3546 | // |
| 3292 | // reserve a key or import/broadcast a wallet transaction. Keep the | 3547 | // This has to stay inside the success branch, next to the erase |
| 3293 | // command names explicit so old peers get a quiet no-op instead of a | 3548 | // above. When validation rejects a body nothing is stored, |
| 3294 | // wallet side effect. | 3549 | // AlreadyHave stays false, and the mapAskFor entry was already |
| 3295 | if (LogAcceptsCategory("net")) | 3550 | // consumed when the getdata went out -- so erasing the tracking |
| 3296 | printf("ProcessMessage(%s) : disabled legacy order message\n", strCommand.c_str()); | 3551 | // here would leave the block requested from nobody at all until |
| 3297 | vRecv.clear(); | 3552 | // some peer happened to re-announce it. That is exactly the stall |
| 3553 | // of #33, and erasing before ProcessBlock made it reachable on | ||
| 3554 | // demand. | ||
| 3555 | mapBlockFetch.erase(inv.hash); | ||
| 3556 | |||
| 3557 | // A peer that just handed us a block is demonstrably at least at | ||
| 3558 | // that block's height. Without this, nStartingHeight is a snapshot | ||
| 3559 | // taken once during the version handshake and never touched again, | ||
| 3560 | // so on a long-lived connection it is the peer's height from | ||
| 3561 | // however many hours ago it happened to connect. The pool's | ||
| 3562 | // behind-the-network check compares against the median of those | ||
| 3563 | // numbers, so as connections age it drifts toward "everyone is | ||
| 3564 | // behind us" and quietly stops firing -- the opposite of its job. | ||
| 3565 | map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(inv.hash); | ||
| 3566 | if (mi != mapBlockIndex.end() && mi->second) | ||
| 3567 | pfrom->nStartingHeight = max(pfrom->nStartingHeight, mi->second->nHeight); | ||
| 3568 | } | ||
| 3298 | } | 3569 | } |
| 3299 | 3570 | ||
| 3300 | 3571 | ||
| 3301 | else if (strCommand == "reply") | 3572 | // "checkorder", "submitorder" and "reply" -- the 2009 buy-from-a-merchant |
| 3302 | { | 3573 | // exchange -- used to be handled here. All three are gone. |
| 3303 | uint256 hashReply; | 3574 | // |
| 3304 | vRecv >> hashReply; | 3575 | // "checkorder" called GenerateNewKey() for any peer that asked, which is |
| 3305 | 3576 | // AddKey() and a wallet.dat write, and handed the pubkey back. The comment | |
| 3306 | CRequestTracker tracker; | 3577 | // said "keep giving the same key to the same ip until they use it", and on |
| 3307 | CRITICAL_BLOCK(pfrom->cs_mapRequests) | 3578 | // a 2009 network of routable IPs that held. It does not hold here: every |
| 3308 | { | 3579 | // inbound peer arrives through a rendezvous tunnel and is tagged with |
| 3309 | map<uint256, CRequestTracker>::iterator mi = pfrom->mapRequests.find(hashReply); | 3580 | // BtfMarkerAddr() over three random bytes (net.cpp), so addr.ip is freshly |
| 3310 | if (mi != pfrom->mapRequests.end()) | 3581 | // random per connection and the cache never hits. Reconnect in a loop and |
| 3311 | { | 3582 | // the node mints a key per connection -- unbounded wallet growth, unbounded |
| 3312 | tracker = (*mi).second; | 3583 | // growth of the static map that held them, and every one of those keys |
| 3313 | pfrom->mapRequests.erase(mi); | 3584 | // missing from every backup taken beforehand, which for this pre-BIP32 |
| 3314 | } | 3585 | // wallet is how coins are lost. |
| 3315 | } | 3586 | // |
| 3316 | if (!tracker.IsNull()) | 3587 | // "submitorder" took a CWalletTx from an unauthenticated peer and, if it |
| 3317 | tracker.fn(tracker.param1, vRecv); | 3588 | // was merely a valid transaction, AddToWallet()'d it. Valid is not the same |
| 3318 | } | 3589 | // as ours: any peer could write chosen transactions into wallet.dat. |
| 3590 | // | ||
| 3591 | // "reply" dispatched to a CRequestTracker callback. Nothing in this tree | ||
| 3592 | // has ever called PushRequest, so mapRequests was permanently empty and the | ||
| 3593 | // handler could only ever be a parser for hostile input. | ||
| 3319 | 3594 | ||
| 3320 | 3595 | ||
| 3321 | else if (strCommand == "ping") | 3596 | else if (strCommand == "ping") |
| @@ -3403,12 +3678,119 @@ bool SendMessages(CNode* pto) | |||
| 3403 | const CInv& inv = (*pto->mapAskFor.begin()).second; | 3678 | const CInv& inv = (*pto->mapAskFor.begin()).second; |
| 3404 | if (LogAcceptsCategory("net")) printf("sending getdata: %s\n", inv.ToString().c_str()); | 3679 | if (LogAcceptsCategory("net")) printf("sending getdata: %s\n", inv.ToString().c_str()); |
| 3405 | if (!AlreadyHave(txdb, inv)) | 3680 | if (!AlreadyHave(txdb, inv)) |
| 3681 | { | ||
| 3406 | vAskFor.push_back(inv); | 3682 | vAskFor.push_back(inv); |
| 3683 | // Blocks get their delivery tracked so the failover pass | ||
| 3684 | // below can chase the ones that go quiet. | ||
| 3685 | if (inv.type == MSG_BLOCK) | ||
| 3686 | { | ||
| 3687 | // Full, and this is a hash we are not already tracking: | ||
| 3688 | // make room rather than drop it on the floor, or one peer | ||
| 3689 | // announcing eight hashes turns failover off for everyone. | ||
| 3690 | if (!mapBlockFetch.count(inv.hash) && | ||
| 3691 | mapBlockFetch.size() >= BLOCK_FETCH_TRACK_MAX) | ||
| 3692 | MakeRoomInBlockFetch(pto->addr); | ||
| 3693 | |||
| 3694 | if (mapBlockFetch.count(inv.hash) || | ||
| 3695 | mapBlockFetch.size() < BLOCK_FETCH_TRACK_MAX) | ||
| 3696 | { | ||
| 3697 | CBlockFetch& fetch = mapBlockFetch[inv.hash]; | ||
| 3698 | if (fetch.nTimeFirstAsked == 0) | ||
| 3699 | { | ||
| 3700 | fetch.nTimeFirstAsked = GetTime(); | ||
| 3701 | // Who is answerable for this hash existing. Set | ||
| 3702 | // once, on creation: the failover sweep asks other | ||
| 3703 | // peers later, and none of them chose to announce | ||
| 3704 | // it, so none of them should be charged for it. | ||
| 3705 | fetch.addrAnnounced = pto->addr; | ||
| 3706 | } | ||
| 3707 | fetch.nTimeAsked = GetTime(); | ||
| 3708 | fetch.setAsked.insert(pto->addr); | ||
| 3709 | } | ||
| 3710 | } | ||
| 3711 | } | ||
| 3407 | pto->mapAskFor.erase(pto->mapAskFor.begin()); | 3712 | pto->mapAskFor.erase(pto->mapAskFor.begin()); |
| 3408 | } | 3713 | } |
| 3409 | if (!vAskFor.empty()) | 3714 | if (!vAskFor.empty()) |
| 3410 | pto->PushMessage("getdata", vAskFor); | 3715 | pto->PushMessage("getdata", vAskFor); |
| 3411 | 3716 | ||
| 3717 | |||
| 3718 | // | ||
| 3719 | // Message: getdata (block fetch failover) | ||
| 3720 | // | ||
| 3721 | // A requested block that has not arrived in BLOCK_FETCH_RETRY_SECS is | ||
| 3722 | // not still in transit -- a body is at most 1 MB and somebody | ||
| 3723 | // demonstrably has it -- so ask somebody else instead of waiting on | ||
| 3724 | // whoever was asked first (#33). Each peer the message handler visits | ||
| 3725 | // takes at most one turn per block per round; once every connected | ||
| 3726 | // peer has had its turn and a further interval passes with nobody | ||
| 3727 | // new to ask, the round restarts. A peer that declared itself a | ||
| 3728 | // client cannot serve block bodies and is no use as a target. | ||
| 3729 | if (!pto->fClient) | ||
| 3730 | { | ||
| 3731 | vector<CInv> vFailover; | ||
| 3732 | int64 nNowSecs = GetTime(); | ||
| 3733 | for (map<uint256, CBlockFetch>::iterator mi = mapBlockFetch.begin(); mi != mapBlockFetch.end();) | ||
| 3734 | { | ||
| 3735 | CBlockFetch& fetch = (*mi).second; | ||
| 3736 | CInv inv(MSG_BLOCK, (*mi).first); | ||
| 3737 | if (AlreadyHave(txdb, inv)) | ||
| 3738 | { | ||
| 3739 | // Arrived by some other road -- our own miner, an orphan | ||
| 3740 | // parked in mapOrphanBlocks. Nothing left to chase. | ||
| 3741 | mapBlockFetch.erase(mi++); | ||
| 3742 | continue; | ||
| 3743 | } | ||
| 3744 | if (nNowSecs - fetch.nTimeFirstAsked > BLOCK_FETCH_GIVEUP_SECS) | ||
| 3745 | { | ||
| 3746 | printf("block fetch: giving up on %s -- no peer served it in %d s\n", | ||
| 3747 | inv.ToString().c_str(), (int)(nNowSecs - fetch.nTimeFirstAsked)); | ||
| 3748 | mapBlockFetch.erase(mi++); | ||
| 3749 | continue; | ||
| 3750 | } | ||
| 3751 | if (nNowSecs - fetch.nTimeAsked >= BLOCK_FETCH_RETRY_SECS) | ||
| 3752 | { | ||
| 3753 | // The handler loop visits every peer within ~100ms, so a | ||
| 3754 | // request stale by two full intervals means nobody outside | ||
| 3755 | // setAsked was there to ask. Start the round over. | ||
| 3756 | if (nNowSecs - fetch.nTimeAsked >= 2 * BLOCK_FETCH_RETRY_SECS) | ||
| 3757 | fetch.setAsked.clear(); | ||
| 3758 | if (!fetch.setAsked.count(pto->addr)) | ||
| 3759 | { | ||
| 3760 | // Which block, how long, and which peer we are trying | ||
| 3761 | // next. Behind the category because it names a peer | ||
| 3762 | // and a peer decides how often it is written. | ||
| 3763 | if (LogAcceptsCategory("net")) | ||
| 3764 | printf("block fetch failover: %s unanswered for %d s, re-requesting from %s\n", | ||
| 3765 | inv.ToString().c_str(), (int)(nNowSecs - fetch.nTimeAsked), | ||
| 3766 | pto->addr.ToString().c_str()); | ||
| 3767 | |||
| 3768 | // The unconditional half: no addresses, and at most | ||
| 3769 | // one line per BLOCK_FETCH_REPORT_SECS however hard a | ||
| 3770 | // peer tries. See the counters for the reasoning. | ||
| 3771 | nBlockFetchFailovers++; | ||
| 3772 | if (nNowSecs - nBlockFetchWindowStart >= BLOCK_FETCH_REPORT_SECS) | ||
| 3773 | { | ||
| 3774 | printf("block fetch: %d block request%s went unanswered and %s been " | ||
| 3775 | "re-asked of other peers (%d tracked)\n", | ||
| 3776 | nBlockFetchFailovers, nBlockFetchFailovers == 1 ? "" : "s", | ||
| 3777 | nBlockFetchFailovers == 1 ? "has" : "have", | ||
| 3778 | (int)mapBlockFetch.size()); | ||
| 3779 | nBlockFetchWindowStart = nNowSecs; | ||
| 3780 | nBlockFetchFailovers = 0; | ||
| 3781 | } | ||
| 3782 | |||
| 3783 | vFailover.push_back(inv); | ||
| 3784 | fetch.nTimeAsked = nNowSecs; | ||
| 3785 | fetch.setAsked.insert(pto->addr); | ||
| 3786 | } | ||
| 3787 | } | ||
| 3788 | ++mi; | ||
| 3789 | } | ||
| 3790 | if (!vFailover.empty()) | ||
| 3791 | pto->PushMessage("getdata", vFailover); | ||
| 3792 | } | ||
| 3793 | |||
| 3412 | } | 3794 | } |
| 3413 | return true; | 3795 | return true; |
| 3414 | } | 3796 | } |
| @@ -3573,6 +3955,110 @@ static bool StratumRecvLine(SOCKET s, std::string& buf, | |||
| 3573 | // How long to wait for a mining.notify after authorize before giving up | 3955 | // How long to wait for a mining.notify after authorize before giving up |
| 3574 | static const int64 STUCK_NO_JOB_TIMEOUT_SECS = 120; | 3956 | static const int64 STUCK_NO_JOB_TIMEOUT_SECS = 120; |
| 3575 | 3957 | ||
| 3958 | // ------------------------------------------------------------------ | ||
| 3959 | // Multi-threaded pool worker. The original loop hashed a single nonce | ||
| 3960 | // per blocking 50ms socket poll, capping one instance at ~20 H/s no | ||
| 3961 | // matter how many cores the machine has. The socket and job bookkeeping | ||
| 3962 | // stay on the calling thread; N hash threads grind nonce ranges on their | ||
| 3963 | // own RandomX VMs (all sharing the one ~2 GB dataset) and drop found | ||
| 3964 | // shares on a queue the I/O thread drains and submits. | ||
| 3965 | // ------------------------------------------------------------------ | ||
| 3966 | struct ParticipantJobShared | ||
| 3967 | { | ||
| 3968 | std::mutex mtx; | ||
| 3969 | unsigned char header[80]; | ||
| 3970 | uint256 target; | ||
| 3971 | std::string jobId; | ||
| 3972 | uint64 jobSeq; | ||
| 3973 | bool haveJob; | ||
| 3974 | bool stop; | ||
| 3975 | ParticipantJobShared() : jobSeq(0), haveJob(false), stop(false) | ||
| 3976 | { | ||
| 3977 | memset(header, 0, sizeof(header)); | ||
| 3978 | } | ||
| 3979 | }; | ||
| 3980 | |||
| 3981 | struct ParticipantShareHit | ||
| 3982 | { | ||
| 3983 | uint64 jobSeq; | ||
| 3984 | std::string jobId; | ||
| 3985 | unsigned int nonce; | ||
| 3986 | }; | ||
| 3987 | |||
| 3988 | static void ParticipantHashWorker(ParticipantJobShared* sh, int idx, int nWorkers, | ||
| 3989 | std::vector<ParticipantShareHit>* hits, | ||
| 3990 | std::mutex* hitsMtx) | ||
| 3991 | { | ||
| 3992 | void* vm = RandomXCreateMinerVM(); | ||
| 3993 | if (!vm) | ||
| 3994 | { | ||
| 3995 | LogPrint("worker", "[worker] hash thread %d: RandomX VM creation failed\n", idx); | ||
| 3996 | return; | ||
| 3997 | } | ||
| 3998 | |||
| 3999 | unsigned char header[80]; | ||
| 4000 | uint256 target; | ||
| 4001 | std::string jobId; | ||
| 4002 | uint64 mySeq = 0; | ||
| 4003 | bool active = false; | ||
| 4004 | unsigned int nNonce = 0; | ||
| 4005 | uint64 localHashes = 0; | ||
| 4006 | |||
| 4007 | while (!fShutdown) | ||
| 4008 | { | ||
| 4009 | { | ||
| 4010 | std::lock_guard<std::mutex> lk(sh->mtx); | ||
| 4011 | if (sh->stop) | ||
| 4012 | break; | ||
| 4013 | if (sh->haveJob && sh->jobSeq != mySeq) | ||
| 4014 | { | ||
| 4015 | memcpy(header, sh->header, 80); | ||
| 4016 | target = sh->target; | ||
| 4017 | jobId = sh->jobId; | ||
| 4018 | mySeq = sh->jobSeq; | ||
| 4019 | nNonce = (unsigned int)(idx + 1); | ||
| 4020 | active = true; | ||
| 4021 | } | ||
| 4022 | } | ||
| 4023 | if (!active) | ||
| 4024 | { | ||
| 4025 | Sleep(50); | ||
| 4026 | continue; | ||
| 4027 | } | ||
| 4028 | |||
| 4029 | memcpy(header + 76, &nNonce, 4); | ||
| 4030 | uint256 hash = RandomXHashWithVM(vm, header, 80); | ||
| 4031 | localHashes++; | ||
| 4032 | if ((localHashes & 0x3ff) == 0) | ||
| 4033 | gParticipantHashes.fetch_add(0x400); | ||
| 4034 | |||
| 4035 | if (hash <= target) | ||
| 4036 | { | ||
| 4037 | LogPrint("worker", "[worker] share found! nonce=%u job=%s (thread %d)\n", | ||
| 4038 | nNonce, jobId.c_str(), idx); | ||
| 4039 | std::lock_guard<std::mutex> lk(*hitsMtx); | ||
| 4040 | hits->push_back(ParticipantShareHit{mySeq, jobId, nNonce}); | ||
| 4041 | } | ||
| 4042 | |||
| 4043 | unsigned int next = nNonce + (unsigned int)nWorkers; | ||
| 4044 | if (next < nNonce) // wrapped past 2^32: range exhausted, wait for next job | ||
| 4045 | active = false; | ||
| 4046 | else | ||
| 4047 | nNonce = next; | ||
| 4048 | } | ||
| 4049 | |||
| 4050 | gParticipantHashes.fetch_add(localHashes & 0x3ff); | ||
| 4051 | RandomXDestroyMinerVM(vm); | ||
| 4052 | } | ||
| 4053 | |||
| 4054 | // Run one participant session: resolve the pool, open the tunnel, subscribe, | ||
| 4055 | // hash, submit shares. Returns true only if the pool actually handed out work | ||
| 4056 | // -- at least one well-formed mining.notify arrived. Everything else is false, | ||
| 4057 | // including a tunnel that opened and was dropped before any job. | ||
| 4058 | // | ||
| 4059 | // The caller's reconnect backoff keys off this return value, so any path added | ||
| 4060 | // here that leaves the session without work MUST return false. "The session | ||
| 4061 | // ended tidily" is not the question being asked. | ||
| 3576 | static bool PoolParticipantMiner() | 4062 | static bool PoolParticipantMiner() |
| 3577 | { | 4063 | { |
| 3578 | // ------------------------------------------------------------------ | 4064 | // ------------------------------------------------------------------ |
| @@ -3587,6 +4073,51 @@ static bool PoolParticipantMiner() | |||
| 3587 | LogPrint("worker", "[worker] connecting to pool %s\n", strParticipantPool.c_str()); | 4073 | LogPrint("worker", "[worker] connecting to pool %s\n", strParticipantPool.c_str()); |
| 3588 | SetParticipantMiningStatus("resolving pool address"); | 4074 | SetParticipantMiningStatus("resolving pool address"); |
| 3589 | 4075 | ||
| 4076 | // ------------------------------------------------------------------ | ||
| 4077 | // Steps 1-2: connect to the pool. Two address forms: | ||
| 4078 | // tcp:HOST:PORT -- plain TCP straight to the pool's stratum port | ||
| 4079 | // (e.g. our own operator node on this machine), | ||
| 4080 | // no .btf resolution or rendezvous tunnel involved | ||
| 4081 | // <name>.btf -- resolve via Nostr, dial through a rendezvous relay | ||
| 4082 | // ------------------------------------------------------------------ | ||
| 4083 | btf_socket_t s = INVALID_SOCKET; | ||
| 4084 | if (strParticipantPool.compare(0, 4, "tcp:") == 0) | ||
| 4085 | { | ||
| 4086 | std::string hp = strParticipantPool.substr(4); | ||
| 4087 | size_t colon = hp.rfind(':'); | ||
| 4088 | if (colon == std::string::npos) { | ||
| 4089 | LogPrint("worker", "[worker] bad tcp pool address '%s' (want tcp:host:port)\n", | ||
| 4090 | strParticipantPool.c_str()); | ||
| 4091 | SetParticipantMiningStatus("bad pool address"); | ||
| 4092 | return false; | ||
| 4093 | } | ||
| 4094 | std::string host = hp.substr(0, colon); | ||
| 4095 | int port = atoi(hp.substr(colon + 1).c_str()); | ||
| 4096 | unsigned long ip = inet_addr(host.c_str()); | ||
| 4097 | if (port <= 0 || port > 65535 || ip == INADDR_NONE) { | ||
| 4098 | LogPrint("worker", "[worker] bad tcp pool endpoint '%s'\n", hp.c_str()); | ||
| 4099 | SetParticipantMiningStatus("bad pool endpoint"); | ||
| 4100 | return false; | ||
| 4101 | } | ||
| 4102 | SetParticipantMiningStatus("connecting to pool (direct tcp)"); | ||
| 4103 | struct sockaddr_in sa; | ||
| 4104 | memset(&sa, 0, sizeof(sa)); | ||
| 4105 | sa.sin_family = AF_INET; | ||
| 4106 | sa.sin_port = htons((unsigned short)port); | ||
| 4107 | sa.sin_addr.s_addr = ip; | ||
| 4108 | SOCKET cs = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
| 4109 | if (cs == INVALID_SOCKET || connect(cs, (struct sockaddr*)&sa, sizeof(sa)) == SOCKET_ERROR) { | ||
| 4110 | LogPrint("worker", "[worker] direct connect to %s FAILED\n", hp.c_str()); | ||
| 4111 | SetParticipantMiningStatus("pool connect failed"); | ||
| 4112 | if (cs != INVALID_SOCKET) closesocket(cs); | ||
| 4113 | return false; | ||
| 4114 | } | ||
| 4115 | LogPrint("worker", "[worker] connected directly to pool %s\n", hp.c_str()); | ||
| 4116 | s = (btf_socket_t)cs; | ||
| 4117 | goto connected; | ||
| 4118 | } | ||
| 4119 | |||
| 4120 | { | ||
| 3590 | unsigned char target_pubkey[32]; | 4121 | unsigned char target_pubkey[32]; |
| 3591 | if (!btf::ParseAddress(strParticipantPool, target_pubkey)) { | 4122 | if (!btf::ParseAddress(strParticipantPool, target_pubkey)) { |
| 3592 | LogPrint("worker", "[worker] invalid .btf address: %s\n", strParticipantPool.c_str()); | 4123 | LogPrint("worker", "[worker] invalid .btf address: %s\n", strParticipantPool.c_str()); |
| @@ -3605,6 +4136,33 @@ static bool PoolParticipantMiner() | |||
| 3605 | } | 4136 | } |
| 3606 | LogPrint("worker", "[worker] pool resolved to meeting node %s\n", meetingHostPort.c_str()); | 4137 | LogPrint("worker", "[worker] pool resolved to meeting node %s\n", meetingHostPort.c_str()); |
| 3607 | 4138 | ||
| 4139 | // The descriptor names ONE meeting node, but the pool parks a | ||
| 4140 | // registration at every relay it knows (one accept thread per relay, | ||
| 4141 | // rpc.cpp:AcceptOneFn), so any seed relay can pair us. Insisting on the | ||
| 4142 | // advertised entry forever turns one bad path into a miner that idles | ||
| 4143 | // indefinitely while a working path exists (issue #17); after a few | ||
| 4144 | // consecutive tunnel failures, rotate the seed relays into the attempts | ||
| 4145 | // too. Only ONE participant session runs (StartMinerThreads), so these | ||
| 4146 | // statics are single-threaded. | ||
| 4147 | static const int TUNNEL_FAILOVER_AFTER = 3; | ||
| 4148 | static int nTunnelFails = 0; // consecutive tunnel failures, this pool | ||
| 4149 | static size_t iFailoverRelay = 0; // rotation cursor over vBtfMeetingRelays | ||
| 4150 | static std::string strFailPool; // pool the counters belong to | ||
| 4151 | if (strFailPool != strParticipantPool) { | ||
| 4152 | strFailPool = strParticipantPool; | ||
| 4153 | nTunnelFails = 0; | ||
| 4154 | iFailoverRelay = 0; | ||
| 4155 | } | ||
| 4156 | if (nTunnelFails >= TUNNEL_FAILOVER_AFTER && !vBtfMeetingRelays.empty()) { | ||
| 4157 | std::string alt = vBtfMeetingRelays[iFailoverRelay++ % vBtfMeetingRelays.size()]; | ||
| 4158 | if (alt != meetingHostPort) { | ||
| 4159 | LogPrint("worker", "[worker] advertised meeting node %s failed %d" | ||
| 4160 | " times in a row -- trying seed relay %s instead\n", | ||
| 4161 | meetingHostPort.c_str(), nTunnelFails, alt.c_str()); | ||
| 4162 | meetingHostPort = alt; | ||
| 4163 | } | ||
| 4164 | } | ||
| 4165 | |||
| 3608 | size_t colon = meetingHostPort.rfind(':'); | 4166 | size_t colon = meetingHostPort.rfind(':'); |
| 3609 | if (colon == std::string::npos) { | 4167 | if (colon == std::string::npos) { |
| 3610 | LogPrint("worker", "[worker] bad meeting node format '%s'\n", meetingHostPort.c_str()); | 4168 | LogPrint("worker", "[worker] bad meeting node format '%s'\n", meetingHostPort.c_str()); |
| @@ -3621,25 +4179,65 @@ static bool PoolParticipantMiner() | |||
| 3621 | } | 4179 | } |
| 3622 | 4180 | ||
| 3623 | // ------------------------------------------------------------------ | 4181 | // ------------------------------------------------------------------ |
| 3624 | // Step 2: open encrypted tunnel to pool | 4182 | // Step 2b: open encrypted tunnel to pool |
| 3625 | // ------------------------------------------------------------------ | 4183 | // ------------------------------------------------------------------ |
| 3626 | SetParticipantMiningStatus("opening tunnel"); | 4184 | SetParticipantMiningStatus("opening tunnel"); |
| 3627 | btf_socket_t s = btf::BtfClientTunnel(host.c_str(), (unsigned short)port, | 4185 | btf::TunnelFail tunnelFail = btf::TUNNEL_FAIL_NONE; |
| 3628 | target_pubkey, service_enc_pub); | 4186 | s = btf::BtfClientTunnel(host.c_str(), (unsigned short)port, |
| 4187 | target_pubkey, service_enc_pub, &tunnelFail); | ||
| 3629 | if (s == INVALID_SOCKET) { | 4188 | if (s == INVALID_SOCKET) { |
| 3630 | LogPrint("worker", "[worker] tunnel connect FAILED via %s" | 4189 | nTunnelFails++; |
| 3631 | " (relay unreachable or pool not listening)\n", | 4190 | LogPrint("worker", "[worker] tunnel connect FAILED via %s: %s\n", |
| 3632 | meetingHostPort.c_str()); | 4191 | meetingHostPort.c_str(), btf::TunnelFailText(tunnelFail)); |
| 3633 | SetParticipantMiningStatus("tunnel connect failed"); | 4192 | SetParticipantMiningStatus(std::string("tunnel connect failed: ") |
| 4193 | + btf::TunnelFailText(tunnelFail)); | ||
| 3634 | return false; | 4194 | return false; |
| 3635 | } | 4195 | } |
| 4196 | nTunnelFails = 0; | ||
| 3636 | LogPrint("worker", "[worker] tunnel open via %s\n", meetingHostPort.c_str()); | 4197 | LogPrint("worker", "[worker] tunnel open via %s\n", meetingHostPort.c_str()); |
| 3637 | SetParticipantMiningStatus("tunnel open"); | 4198 | SetParticipantMiningStatus("tunnel open"); |
| 4199 | } | ||
| 4200 | |||
| 4201 | connected: | ||
| 3638 | 4202 | ||
| 3639 | // ------------------------------------------------------------------ | 4203 | // ------------------------------------------------------------------ |
| 3640 | // Step 3: determine our payout address (mining username) | 4204 | // Step 3: determine our payout address (mining username). /payoutaddr |
| 4205 | // wins -- it lets a machine mine to an address whose keys live | ||
| 4206 | // elsewhere, so no wallet file is needed on the mining machine. | ||
| 4207 | // /payoutfile rotates through a list of such addresses: one is picked | ||
| 4208 | // per (re)connection, and the session self-limits (see rotation break | ||
| 4209 | // in the main loop) so consecutive rounds land on fresh addresses. | ||
| 3641 | // ------------------------------------------------------------------ | 4210 | // ------------------------------------------------------------------ |
| 3642 | std::string username; | 4211 | static std::vector<std::string> vPayoutAddrs; |
| 4212 | static size_t nPayoutRot = 0; | ||
| 4213 | if (!strPayoutFile.empty() && vPayoutAddrs.empty()) | ||
| 4214 | { | ||
| 4215 | FILE* f = fopen(strPayoutFile.c_str(), "r"); | ||
| 4216 | if (f) | ||
| 4217 | { | ||
| 4218 | char line[256]; | ||
| 4219 | while (fgets(line, sizeof(line), f)) | ||
| 4220 | { | ||
| 4221 | std::string a = line; | ||
| 4222 | while (!a.empty() && (a.back() == '\n' || a.back() == '\r' || | ||
| 4223 | a.back() == ' ' || a.back() == '\t')) | ||
| 4224 | a.pop_back(); | ||
| 4225 | if (!a.empty()) | ||
| 4226 | vPayoutAddrs.push_back(a); | ||
| 4227 | } | ||
| 4228 | fclose(f); | ||
| 4229 | LogPrint("worker", "[worker] loaded %zu payout addresses from %s\n", | ||
| 4230 | vPayoutAddrs.size(), strPayoutFile.c_str()); | ||
| 4231 | } | ||
| 4232 | else | ||
| 4233 | LogPrint("worker", "[worker] cannot open payout file %s\n", | ||
| 4234 | strPayoutFile.c_str()); | ||
| 4235 | } | ||
| 4236 | |||
| 4237 | std::string username = strPayoutAddress; | ||
| 4238 | if (username.empty() && !vPayoutAddrs.empty()) | ||
| 4239 | username = vPayoutAddrs[nPayoutRot++ % vPayoutAddrs.size()]; | ||
| 4240 | if (username.empty()) | ||
| 3643 | { | 4241 | { |
| 3644 | std::vector<unsigned char> vchPubKey; | 4242 | std::vector<unsigned char> vchPubKey; |
| 3645 | if (CWalletDB("r").ReadDefaultKey(vchPubKey)) | 4243 | if (CWalletDB("r").ReadDefaultKey(vchPubKey)) |
| @@ -3677,16 +4275,14 @@ static bool PoolParticipantMiner() | |||
| 3677 | // ------------------------------------------------------------------ | 4275 | // ------------------------------------------------------------------ |
| 3678 | if (!RandomXFastReady()) { | 4276 | if (!RandomXFastReady()) { |
| 3679 | SetParticipantMiningStatus("building RandomX dataset (~2GB, one-time)"); | 4277 | SetParticipantMiningStatus("building RandomX dataset (~2GB, one-time)"); |
| 3680 | unsigned int nThreads = std::thread::hardware_concurrency(); | 4278 | // Fill with the cores we are permitted to use, not the ones the host |
| 4279 | // happens to have. More threads than the cpuset allows does not build | ||
| 4280 | // the dataset any faster -- the CPU budget is the same either way -- | ||
| 4281 | // it just oversubscribes, and this runs alongside the pool node. | ||
| 4282 | unsigned int nThreads = AvailableCoreCount(); | ||
| 3681 | RandomXInitDataset(nThreads > 0 ? (int)nThreads : 1); | 4283 | RandomXInitDataset(nThreads > 0 ? (int)nThreads : 1); |
| 3682 | } | 4284 | } |
| 3683 | void* rxvm = RandomXCreateMinerVM(); | 4285 | LogPrint("worker", "[worker] RandomX dataset ready (%s)\n", |
| 3684 | if (!rxvm) { | ||
| 3685 | LogPrint("worker", "[worker] failed to create RandomX VM\n"); | ||
| 3686 | SetParticipantMiningStatus("RandomX init failed"); | ||
| 3687 | closesocket(s); return false; | ||
| 3688 | } | ||
| 3689 | LogPrint("worker", "[worker] RandomX VM ready (%s)\n", | ||
| 3690 | RandomXFastReady() ? "fast 2GB" : "light 256MB"); | 4286 | RandomXFastReady() ? "fast 2GB" : "light 256MB"); |
| 3691 | 4287 | ||
| 3692 | // ------------------------------------------------------------------ | 4288 | // ------------------------------------------------------------------ |
| @@ -3704,7 +4300,6 @@ static bool PoolParticipantMiner() | |||
| 3704 | {"params",json::array()}})) { | 4300 | {"params",json::array()}})) { |
| 3705 | LogPrint("worker", "[worker] subscribe send FAILED -- pool dropped connection\n"); | 4301 | LogPrint("worker", "[worker] subscribe send FAILED -- pool dropped connection\n"); |
| 3706 | SetParticipantMiningStatus("subscribe send failed"); | 4302 | SetParticipantMiningStatus("subscribe send failed"); |
| 3707 | RandomXDestroyMinerVM(rxvm); | ||
| 3708 | closesocket(s); return false; | 4303 | closesocket(s); return false; |
| 3709 | } | 4304 | } |
| 3710 | 4305 | ||
| @@ -3714,18 +4309,70 @@ static bool PoolParticipantMiner() | |||
| 3714 | {"params",json::array({username,"x"})}})) { | 4309 | {"params",json::array({username,"x"})}})) { |
| 3715 | LogPrint("worker", "[worker] authorize send FAILED -- pool dropped connection\n"); | 4310 | LogPrint("worker", "[worker] authorize send FAILED -- pool dropped connection\n"); |
| 3716 | SetParticipantMiningStatus("authorize send failed"); | 4311 | SetParticipantMiningStatus("authorize send failed"); |
| 3717 | RandomXDestroyMinerVM(rxvm); | ||
| 3718 | closesocket(s); return false; | 4312 | closesocket(s); return false; |
| 3719 | } | 4313 | } |
| 3720 | 4314 | ||
| 3721 | // ------------------------------------------------------------------ | 4315 | // ------------------------------------------------------------------ |
| 4316 | // Step 6: spawn hash threads (each with its own RandomX VM on the | ||
| 4317 | // shared dataset), then run the I/O loop below: receive pool messages, | ||
| 4318 | // publish jobs to the threads, submit the shares they find. | ||
| 4319 | // ------------------------------------------------------------------ | ||
| 4320 | // One count, decided in one place (main.cpp:MinerThreadCount's neighbour), | ||
| 4321 | // so that what StartMinerThreads() reports at startup and what actually | ||
| 4322 | // hashes here can never drift apart. Explicit /hashthreads=N is honored | ||
| 4323 | // exactly, including 1; automatic leaves one core for node + I/O threads. | ||
| 4324 | int nWorkers = ParticipantHashThreadCount(); | ||
| 4325 | ParticipantJobShared jobShared; | ||
| 4326 | std::vector<ParticipantShareHit> shareHits; | ||
| 4327 | std::mutex shareHitsMtx; | ||
| 4328 | std::vector<std::thread> hashThreads; | ||
| 4329 | try | ||
| 4330 | { | ||
| 4331 | for (int i = 0; i < nWorkers; i++) | ||
| 4332 | hashThreads.emplace_back(ParticipantHashWorker, &jobShared, i, nWorkers, | ||
| 4333 | &shareHits, &shareHitsMtx); | ||
| 4334 | } | ||
| 4335 | catch (const std::system_error& e) | ||
| 4336 | { | ||
| 4337 | // A refused pthread_create used to reach std::terminate here and | ||
| 4338 | // kill the worker mid-session -- observed twice on an otherwise | ||
| 4339 | // healthy participant. Stop the workers that did start the same way | ||
| 4340 | // the disconnect path below does, drop this pool connection, and let | ||
| 4341 | // the caller's reconnect loop try again once the pressure clears. | ||
| 4342 | LogPrint("worker", "[worker] cannot spawn hash threads (%s)" | ||
| 4343 | " -- dropping pool connection\n", e.what()); | ||
| 4344 | SetParticipantMiningStatus("hash thread spawn failed"); | ||
| 4345 | { | ||
| 4346 | std::lock_guard<std::mutex> lk(jobShared.mtx); | ||
| 4347 | jobShared.stop = true; | ||
| 4348 | jobShared.haveJob = false; | ||
| 4349 | } | ||
| 4350 | for (std::thread& t : hashThreads) | ||
| 4351 | t.join(); | ||
| 4352 | closesocket(s); | ||
| 4353 | return false; | ||
| 4354 | } | ||
| 4355 | LogPrint("worker", "[worker] spawned %d hash threads on shared RandomX dataset\n", | ||
| 4356 | nWorkers); | ||
| 4357 | const uint64 sessionStartHashes = gParticipantHashes.load(); | ||
| 4358 | |||
| 4359 | // ------------------------------------------------------------------ | ||
| 3722 | // Step 6: main loop -- receive messages, hash, submit shares | 4360 | // Step 6: main loop -- receive messages, hash, submit shares |
| 3723 | // ------------------------------------------------------------------ | 4361 | // ------------------------------------------------------------------ |
| 3724 | std::string jobId; | 4362 | std::string jobId; |
| 3725 | unsigned char header[80] = {}; | 4363 | unsigned char header[80] = {}; |
| 3726 | uint256 target; | 4364 | uint256 target; |
| 4365 | // Cached answer to "which block is this job built on", for the | ||
| 4366 | // mining.notify handler below -- see the comment there. Session-local, so | ||
| 4367 | // a reconnect re-resolves rather than trusting an answer obtained before | ||
| 4368 | // a possible change of pool. | ||
| 4369 | uint256 hashPrevResolved; // prevhash the cached answer describes | ||
| 4370 | bool fPrevResolved = false; // whether that answer exists yet | ||
| 4371 | int nPrevHeight = -1; // its height, or -1 == unknown to us | ||
| 4372 | int nBestHeightResolved = -1; // nBestHeight when we last resolved | ||
| 3727 | double currentDifficulty = 1.0; // updated by mining.set_difficulty from pool | 4373 | double currentDifficulty = 1.0; // updated by mining.set_difficulty from pool |
| 3728 | bool haveJob = false; | 4374 | bool haveJob = false; |
| 4375 | bool gotAnyJob = false; // any job at all this session -- the return value | ||
| 3729 | bool gotSubscribeAck = false; | 4376 | bool gotSubscribeAck = false; |
| 3730 | bool gotAuthorizeAck = false; | 4377 | bool gotAuthorizeAck = false; |
| 3731 | int64 authWaitStart = 0; | 4378 | int64 authWaitStart = 0; |
| @@ -3735,6 +4382,8 @@ static bool PoolParticipantMiner() | |||
| 3735 | uint64 sessionSharesSent = 0; // this connection only | 4382 | uint64 sessionSharesSent = 0; // this connection only |
| 3736 | uint64 sessionSharesAccepted = 0; | 4383 | uint64 sessionSharesAccepted = 0; |
| 3737 | int64 sampleStart = GetTime(); | 4384 | int64 sampleStart = GetTime(); |
| 4385 | int64 connStart = GetTime(); | ||
| 4386 | const int64 PAYOUT_ROTATE_SECS = 300; // reconnect with next /payoutfile address | ||
| 3738 | 4387 | ||
| 3739 | SetParticipantMiningStatus("waiting for pool response"); | 4388 | SetParticipantMiningStatus("waiting for pool response"); |
| 3740 | 4389 | ||
| @@ -3840,10 +4489,75 @@ static bool PoolParticipantMiner() | |||
| 3840 | jobId = newJobId; | 4489 | jobId = newJobId; |
| 3841 | memcpy(header, hdrBytes.data(), 80); | 4490 | memcpy(header, hdrBytes.data(), 80); |
| 3842 | memcpy(&target, tgtBytes.data(), 32); | 4491 | memcpy(&target, tgtBytes.data(), 32); |
| 3843 | haveJob = true; | 4492 | haveJob = true; |
| 3844 | nNonce = 1; | 4493 | gotAnyJob = true; |
| 3845 | LogPrint("worker", "[pool->worker] new job %s received\n", | 4494 | nNonce = 1; |
| 3846 | jobId.c_str()); | 4495 | { |
| 4496 | std::lock_guard<std::mutex> lk(jobShared.mtx); | ||
| 4497 | memcpy(jobShared.header, header, 80); | ||
| 4498 | jobShared.target = target; | ||
| 4499 | jobShared.jobId = jobId; | ||
| 4500 | jobShared.jobSeq++; | ||
| 4501 | jobShared.haveJob = true; | ||
| 4502 | } | ||
| 4503 | // Log the chain position the pool is | ||
| 4504 | // building on, so a stale or private | ||
| 4505 | // fork is auditable from worker logs. | ||
| 4506 | // The prevhash comes from the header we | ||
| 4507 | // actually hash (== p[2] of the notify); | ||
| 4508 | // a brief cs_main lookup is safe here -- | ||
| 4509 | // this thread holds no other lock. | ||
| 4510 | // | ||
| 4511 | // Resolving it is the expensive half, and | ||
| 4512 | // this is the socket-reading thread: block | ||
| 4513 | // validation can hold cs_main for a long | ||
| 4514 | // time, and every moment spent waiting on | ||
| 4515 | // it here is a moment set_difficulty and | ||
| 4516 | // share responses are not being handled. | ||
| 4517 | // Most notifies in a round are a new job | ||
| 4518 | // on the SAME tip, so the answer is cached | ||
| 4519 | // and re-resolved only when it could come | ||
| 4520 | // out differently: a prevhash we have not | ||
| 4521 | // resolved, or one we could NOT resolve | ||
| 4522 | // last time while the chain has since | ||
| 4523 | // moved (unknown -> known). A block's | ||
| 4524 | // height is fixed once mapBlockIndex knows | ||
| 4525 | // it, so a cached "known" never goes | ||
| 4526 | // stale. nBestHeight is read unlocked on | ||
| 4527 | // purpose -- it only decides whether to | ||
| 4528 | // repeat a lookup, never what gets | ||
| 4529 | // printed, so a stale read costs at most | ||
| 4530 | // one deferred or one extra lookup. | ||
| 4531 | // The line still prints in full for every | ||
| 4532 | // job, cached or not. | ||
| 4533 | uint256 hashPrev; | ||
| 4534 | memcpy(&hashPrev, header + 4, 32); | ||
| 4535 | if (LogAcceptsCategory("worker")) | ||
| 4536 | { | ||
| 4537 | int nBestHeightNow = nBestHeight; | ||
| 4538 | if (!fPrevResolved | ||
| 4539 | || hashPrev != hashPrevResolved | ||
| 4540 | || (nPrevHeight < 0 && nBestHeightNow != nBestHeightResolved)) | ||
| 4541 | { | ||
| 4542 | nPrevHeight = -1; | ||
| 4543 | CRITICAL_BLOCK(cs_main) | ||
| 4544 | { | ||
| 4545 | map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashPrev); | ||
| 4546 | if (mi != mapBlockIndex.end()) | ||
| 4547 | nPrevHeight = (*mi).second->nHeight; | ||
| 4548 | } | ||
| 4549 | hashPrevResolved = hashPrev; | ||
| 4550 | nBestHeightResolved = nBestHeightNow; | ||
| 4551 | fPrevResolved = true; | ||
| 4552 | } | ||
| 4553 | if (nPrevHeight >= 0) | ||
| 4554 | LogPrint("worker", "[pool->worker] new job %s received, prevhash %s (known, job height %d)\n", | ||
| 4555 | jobId.c_str(), hashPrev.GetHex().c_str(), | ||
| 4556 | nPrevHeight + 1); | ||
| 4557 | else | ||
| 4558 | LogPrint("worker", "[pool->worker] new job %s received, prevhash %s (unknown to our chain)\n", | ||
| 4559 | jobId.c_str(), hashPrev.GetHex().c_str()); | ||
| 4560 | } | ||
| 3847 | SetParticipantMiningStatus("hashing job " + jobId); | 4561 | SetParticipantMiningStatus("hashing job " + jobId); |
| 3848 | } | 4562 | } |
| 3849 | } | 4563 | } |
| @@ -3897,46 +4611,71 @@ static bool PoolParticipantMiner() | |||
| 3897 | } | 4611 | } |
| 3898 | } | 4612 | } |
| 3899 | 4613 | ||
| 4614 | // -- Payout address rotation: end this session cleanly so the outer | ||
| 4615 | // loop reconnects and authorizes the next /payoutfile address -- | ||
| 4616 | if (!vPayoutAddrs.empty() && GetTime() - connStart > PAYOUT_ROTATE_SECS) | ||
| 4617 | { | ||
| 4618 | LogPrint("worker", "[worker] payout rotation interval reached" | ||
| 4619 | " -- reconnecting with next address\n"); | ||
| 4620 | break; | ||
| 4621 | } | ||
| 4622 | |||
| 3900 | if (!haveJob) { Sleep(100); continue; } | 4623 | if (!haveJob) { Sleep(100); continue; } |
| 3901 | 4624 | ||
| 3902 | // -- Hash one nonce -- | 4625 | // -- Hash rate accounting (hashing happens on the worker threads) -- |
| 3903 | memcpy(header+76, &nNonce, 4); | 4626 | hashesSinceSample = gParticipantHashes.load() - sessionStartHashes; |
| 3904 | uint256 hash = RandomXHashWithVM(rxvm, header, 80); | 4627 | UpdateParticipantHashRate(hashesSinceSample, sampleStart); |
| 3905 | hashesSinceSample++; | ||
| 3906 | gParticipantHashes.fetch_add(1); | ||
| 3907 | if ((hashesSinceSample & 0x3ff) == 0) | ||
| 3908 | UpdateParticipantHashRate(hashesSinceSample, sampleStart); | ||
| 3909 | |||
| 3910 | if (hash <= target) { | ||
| 3911 | LogPrint("worker", "[worker] share found! nonce=%u job=%s difficulty=%.4f\n", | ||
| 3912 | nNonce, jobId.c_str(), currentDifficulty); | ||
| 3913 | SetParticipantMiningStatus("share found; submitting"); | ||
| 3914 | int submitId = msgId++; | ||
| 3915 | pendingSubmitIds.insert(submitId); | ||
| 3916 | gParticipantSharesSent.fetch_add(1); | ||
| 3917 | sessionSharesSent++; | ||
| 3918 | json submit = {{"id",submitId},{"method","mining.submit"}, | ||
| 3919 | {"params",json::array({username, jobId, ToHexStr(&nNonce,4)})}}; | ||
| 3920 | if (!StratumSendLine(s, submit)) { | ||
| 3921 | LogPrint("worker", "[worker] share submit send FAILED" | ||
| 3922 | " -- pool connection lost\n"); | ||
| 3923 | break; | ||
| 3924 | } | ||
| 3925 | } | ||
| 3926 | 4628 | ||
| 3927 | nNonce++; | 4629 | // -- Submit any shares the hash threads found -- |
| 3928 | if (nNonce == 0) { | 4630 | bool submitFailed = false; |
| 3929 | LogPrint("worker", "[worker] nonce space exhausted for job %s" | 4631 | { |
| 3930 | " -- waiting for new job\n", jobId.c_str()); | 4632 | std::vector<ParticipantShareHit> pending; |
| 3931 | haveJob = false; | 4633 | { |
| 4634 | std::lock_guard<std::mutex> lk(shareHitsMtx); | ||
| 4635 | pending.swap(shareHits); | ||
| 4636 | } | ||
| 4637 | for (ParticipantShareHit& hit : pending) | ||
| 4638 | { | ||
| 4639 | if (hit.jobId != jobId) | ||
| 4640 | { | ||
| 4641 | LogPrint("worker", "[worker] dropping stale share for old job %s\n", | ||
| 4642 | hit.jobId.c_str()); | ||
| 4643 | continue; | ||
| 4644 | } | ||
| 4645 | LogPrint("worker", "[worker] share found! nonce=%u job=%s difficulty=%.4f\n", | ||
| 4646 | hit.nonce, hit.jobId.c_str(), currentDifficulty); | ||
| 4647 | SetParticipantMiningStatus("share found; submitting"); | ||
| 4648 | int submitId = msgId++; | ||
| 4649 | pendingSubmitIds.insert(submitId); | ||
| 4650 | gParticipantSharesSent.fetch_add(1); | ||
| 4651 | sessionSharesSent++; | ||
| 4652 | json submit = {{"id",submitId},{"method","mining.submit"}, | ||
| 4653 | {"params",json::array({username, hit.jobId, | ||
| 4654 | ToHexStr(&hit.nonce,4)})}}; | ||
| 4655 | if (!StratumSendLine(s, submit)) { | ||
| 4656 | LogPrint("worker", "[worker] share submit send FAILED" | ||
| 4657 | " -- pool connection lost\n"); | ||
| 4658 | submitFailed = true; | ||
| 4659 | break; | ||
| 4660 | } | ||
| 4661 | } | ||
| 3932 | } | 4662 | } |
| 4663 | if (submitFailed) | ||
| 4664 | break; | ||
| 3933 | } | 4665 | } |
| 3934 | 4666 | ||
| 3935 | // ------------------------------------------------------------------ | 4667 | // ------------------------------------------------------------------ |
| 3936 | // Cleanup | 4668 | // Cleanup: stop the hash threads, then drop the pool connection |
| 3937 | // ------------------------------------------------------------------ | 4669 | // ------------------------------------------------------------------ |
| 3938 | RandomXDestroyMinerVM(rxvm); | 4670 | { |
| 4671 | std::lock_guard<std::mutex> lk(jobShared.mtx); | ||
| 4672 | jobShared.stop = true; | ||
| 4673 | jobShared.haveJob = false; | ||
| 4674 | } | ||
| 4675 | for (std::thread& t : hashThreads) | ||
| 4676 | t.join(); | ||
| 3939 | closesocket(s); | 4677 | closesocket(s); |
| 4678 | hashesSinceSample = gParticipantHashes.load() - sessionStartHashes; | ||
| 3940 | UpdateParticipantHashRate(hashesSinceSample, sampleStart); | 4679 | UpdateParticipantHashRate(hashesSinceSample, sampleStart); |
| 3941 | SetParticipantMiningStatus("disconnected"); | 4680 | SetParticipantMiningStatus("disconnected"); |
| 3942 | LogPrint("worker", "[worker] disconnected from pool" | 4681 | LogPrint("worker", "[worker] disconnected from pool" |
| @@ -3944,7 +4683,11 @@ static bool PoolParticipantMiner() | |||
| 3944 | (unsigned long long)hashesSinceSample, | 4683 | (unsigned long long)hashesSinceSample, |
| 3945 | (unsigned long long)sessionSharesSent, | 4684 | (unsigned long long)sessionSharesSent, |
| 3946 | (unsigned long long)sessionSharesAccepted); | 4685 | (unsigned long long)sessionSharesAccepted); |
| 3947 | return true; | 4686 | // Not "we got here", which is true of a tunnel the pool accepted and then |
| 4687 | // dropped without ever sending a job -- exactly the case the caller's | ||
| 4688 | // backoff exists for. Reaching the end of the session is not evidence the | ||
| 4689 | // session was worth anything; a job is. | ||
| 4690 | return gotAnyJob; | ||
| 3948 | } | 4691 | } |
| 3949 | 4692 | ||
| 3950 | // --------------------------------------------------------------------------- | 4693 | // --------------------------------------------------------------------------- |
| @@ -4141,11 +4884,51 @@ bool BitcoinMiner(int nThreadId) | |||
| 4141 | // Participant mode: connect to external pool instead of mining solo/operator | 4884 | // Participant mode: connect to external pool instead of mining solo/operator |
| 4142 | if (nMineMode == MINE_PARTICIPANT) | 4885 | if (nMineMode == MINE_PARTICIPANT) |
| 4143 | { | 4886 | { |
| 4887 | // An attempt that never got work backs off (10s doubling to 120s) | ||
| 4888 | // instead of hammering the same path every flat 10s, and a streak | ||
| 4889 | // lasting minutes is said at printf level: the identical once-per-10s | ||
| 4890 | // LogPrint line is how a worker idled all night unnoticed (issue #17). | ||
| 4891 | // | ||
| 4892 | // Only a session the pool actually sent a job to resets either. The | ||
| 4893 | // weaker test -- "the session returned" -- was gameable: a pool that | ||
| 4894 | // completes the tunnel and drops without ever sending work leaves by | ||
| 4895 | // the same tidy cleanup path a healthy one does, which would pin the | ||
| 4896 | // retry at 10s forever and defeat the backoff in exactly the scenario | ||
| 4897 | // it exists for. | ||
| 4898 | int nRetrySecs = 10; | ||
| 4899 | int64 nFailingSince = 0; | ||
| 4144 | while (fGenerateBitcoins && nMineMode == MINE_PARTICIPANT && !fShutdown) { | 4900 | while (fGenerateBitcoins && nMineMode == MINE_PARTICIPANT && !fShutdown) { |
| 4145 | PoolParticipantMiner(); | 4901 | bool fGotWork = PoolParticipantMiner(); |
| 4146 | if (fGenerateBitcoins && nMineMode == MINE_PARTICIPANT && !fShutdown) { | 4902 | if (fGenerateBitcoins && nMineMode == MINE_PARTICIPANT && !fShutdown) { |
| 4147 | LogPrint("worker", "PoolParticipantMiner: reconnecting in 10s\n"); | 4903 | if (fGotWork) { |
| 4148 | for (int i = 0; i < 10 && !fShutdown; i++) Sleep(1000); | 4904 | nRetrySecs = 10; |
| 4905 | nFailingSince = 0; | ||
| 4906 | } else { | ||
| 4907 | if (nFailingSince == 0) | ||
| 4908 | nFailingSince = GetTime(); | ||
| 4909 | int64 nMins = (GetTime() - nFailingSince) / 60; | ||
| 4910 | if (nMins >= 1) | ||
| 4911 | printf("[worker] connection to pool %s has been failing" | ||
| 4912 | " for %lld minutes\n", | ||
| 4913 | strParticipantPool.c_str(), (long long)nMins); | ||
| 4914 | } | ||
| 4915 | LogPrint("worker", "PoolParticipantMiner: reconnecting in %ds\n", | ||
| 4916 | nRetrySecs); | ||
| 4917 | // Poll everything the enclosing while polls, not just | ||
| 4918 | // fShutdown. Raising the ceiling to 120s made a | ||
| 4919 | // shutdown-only predicate expensive: after the operator | ||
| 4920 | // clears Generate or switches to Relay, the session -- and | ||
| 4921 | // the vfThreadRunning[3] slot that gates restarting it -- | ||
| 4922 | // would stay held for up to two minutes. Measured 119s | ||
| 4923 | // before, 1s after. | ||
| 4924 | for (int i = 0; i < nRetrySecs && !fShutdown && fGenerateBitcoins | ||
| 4925 | && nMineMode == MINE_PARTICIPANT; i++) | ||
| 4926 | Sleep(1000); | ||
| 4927 | if (!fGotWork && nRetrySecs < 120) { | ||
| 4928 | nRetrySecs *= 2; | ||
| 4929 | if (nRetrySecs > 120) | ||
| 4930 | nRetrySecs = 120; | ||
| 4931 | } | ||
| 4149 | } | 4932 | } |
| 4150 | } | 4933 | } |
| 4151 | return true; | 4934 | return true; |
| @@ -4161,7 +4944,7 @@ bool BitcoinMiner(int nThreadId) | |||
| 4161 | // Build the fast (~2 GB dataset) RandomX mode once, up front, so we're | 4944 | // Build the fast (~2 GB dataset) RandomX mode once, up front, so we're |
| 4162 | // not stuck mining in the much slower light/cache-only mode forever. | 4945 | // not stuck mining in the much slower light/cache-only mode forever. |
| 4163 | if (!RandomXFastReady()) { | 4946 | if (!RandomXFastReady()) { |
| 4164 | unsigned int nThreads = std::thread::hardware_concurrency(); | 4947 | unsigned int nThreads = AvailableCoreCount(); |
| 4165 | RandomXInitDataset(nThreads > 0 ? (int)nThreads : 1); | 4948 | RandomXInitDataset(nThreads > 0 ? (int)nThreads : 1); |
| 4166 | } | 4949 | } |
| 4167 | 4950 | ||
| @@ -4247,8 +5030,40 @@ bool BitcoinMiner(int nThreadId) | |||
| 4247 | map<uint256, CTxIndex> mapTestPool; | 5030 | map<uint256, CTxIndex> mapTestPool; |
| 4248 | vector<char> vfAlreadyAdded(mapTransactions.size()); | 5031 | vector<char> vfAlreadyAdded(mapTransactions.size()); |
| 4249 | bool fFoundSomething = true; | 5032 | bool fFoundSomething = true; |
| 4250 | unsigned int nBlockSize = 0; | 5033 | // Assembly must stop below the consensus cap that CheckBlock() |
| 4251 | while (fFoundSomething && nBlockSize < MAX_SIZE/2) | 5034 | // enforces, or we mine a block our own node then rejects. |
| 5035 | // | ||
| 5036 | // This used to read MAX_SIZE/2 (16 MB), which was consistent while | ||
| 5037 | // CheckBlock() also tested against MAX_SIZE (32 MB). Adding | ||
| 5038 | // MAX_BLOCK_SIZE inverted it: assembly could fill 16 MB and | ||
| 5039 | // validation would refuse anything over 1 MB, so a full mempool | ||
| 5040 | // meant solving a block and throwing it away -- with this pool at | ||
| 5041 | // ~90% of hashrate, that is the network stalling, not just us. | ||
| 5042 | // | ||
| 5043 | // The budget test then sat only on this outer while, and the | ||
| 5044 | // comment claimed that bounded the overshoot to a single | ||
| 5045 | // transaction. It did not. The inner for walks all of | ||
| 5046 | // mapTransactions with no test of its own, so one pass could append | ||
| 5047 | // the entire mempool: the overshoot was bounded by mempool size, | ||
| 5048 | // which is to say not bounded at all. Ask per transaction, before | ||
| 5049 | // appending, and the overshoot is zero rather than small. | ||
| 5050 | // | ||
| 5051 | // Seeded with what the block already weighs -- header, tx count, | ||
| 5052 | // coinbase -- because CheckBlock() weighs the whole serialized | ||
| 5053 | // block and not just the transactions we chose to add to it. | ||
| 5054 | unsigned int nBlockSize = ::GetSerializeSize(*pblock, SER_DISK); | ||
| 5055 | // Sigops are a budget too, and CheckBlock() enforces one that no | ||
| 5056 | // assembler here counted against. Bytes do not bound validation | ||
| 5057 | // cost: every OP_CHECKSIG is an elliptic-curve verification, so a | ||
| 5058 | // template well inside the size limit can still be refused for | ||
| 5059 | // demanding more work than a block is allowed to. Counted with | ||
| 5060 | // CTransaction::GetSigOpCount, the function CheckBlock() itself | ||
| 5061 | // sums, so the two cannot disagree about what a transaction costs. | ||
| 5062 | // | ||
| 5063 | // Seeded from the coinbase because CheckBlock() sums over all of | ||
| 5064 | // vtx, coinbase included. | ||
| 5065 | unsigned int nBlockSigOps = pblock->vtx[0].GetSigOpCount(); | ||
| 5066 | while (fFoundSomething && nBlockSize < MAX_BLOCK_SIZE_GEN) | ||
| 4252 | { | 5067 | { |
| 4253 | fFoundSomething = false; | 5068 | fFoundSomething = false; |
| 4254 | unsigned int n = 0; | 5069 | unsigned int n = 0; |
| @@ -4260,6 +5075,29 @@ bool BitcoinMiner(int nThreadId) | |||
| 4260 | if (tx.IsCoinBase() || !tx.IsFinal()) | 5075 | if (tx.IsCoinBase() || !tx.IsFinal()) |
| 4261 | continue; | 5076 | continue; |
| 4262 | 5077 | ||
| 5078 | // Skip, not break: a transaction too big for the room left | ||
| 5079 | // says nothing about the next one, which may still fit. | ||
| 5080 | // | ||
| 5081 | // This is also the only ceiling on a single transaction's | ||
| 5082 | // size anywhere on the path into a block. CheckTransaction | ||
| 5083 | // does not cap it and neither does AcceptTransaction, so | ||
| 5084 | // the mempool will hold one of any size the wire allows; | ||
| 5085 | // without this test a lone 1.5 MB transaction offering a | ||
| 5086 | // fat fee is selected, and every template built from that | ||
| 5087 | // mempool is one CheckBlock() throws away. The fee is never | ||
| 5088 | // paid, because the block never confirms. | ||
| 5089 | // | ||
| 5090 | // Deliberately before ConnectInputs and not after: | ||
| 5091 | // ConnectInputs adds this transaction's fee to nFees by | ||
| 5092 | // reference on success. Passing it and then declining the | ||
| 5093 | // transaction would credit the coinbase for a fee no | ||
| 5094 | // transaction in the block pays, and the block would be | ||
| 5095 | // rejected for a wholly different reason. | ||
| 5096 | unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK); | ||
| 5097 | unsigned int nTxSigOps = tx.GetSigOpCount(); | ||
| 5098 | if (!BlockTemplateFits(nBlockSize, nBlockSigOps, nTxSize, nTxSigOps)) | ||
| 5099 | continue; | ||
| 5100 | |||
| 4263 | // Transaction fee requirements, mainly only needed for flood control | 5101 | // Transaction fee requirements, mainly only needed for flood control |
| 4264 | // Under 10K (about 80 inputs) is free for first 100 transactions | 5102 | // Under 10K (about 80 inputs) is free for first 100 transactions |
| 4265 | // Base rate is 0.01 per KB | 5103 | // Base rate is 0.01 per KB |
| @@ -4271,7 +5109,8 @@ bool BitcoinMiner(int nThreadId) | |||
| 4271 | swap(mapTestPool, mapTestPoolTmp); | 5109 | swap(mapTestPool, mapTestPoolTmp); |
| 4272 | 5110 | ||
| 4273 | pblock->vtx.push_back(tx); | 5111 | pblock->vtx.push_back(tx); |
| 4274 | nBlockSize += ::GetSerializeSize(tx, SER_NETWORK); | 5112 | nBlockSize += nTxSize; |
| 5113 | nBlockSigOps += nTxSigOps; | ||
| 4275 | vfAlreadyAdded[n] = true; | 5114 | vfAlreadyAdded[n] = true; |
| 4276 | fFoundSomething = true; | 5115 | fFoundSomething = true; |
| 4277 | } | 5116 | } |
| @@ -4320,7 +5159,7 @@ bool BitcoinMiner(int nThreadId) | |||
| 4320 | //// debug print | 5159 | //// debug print |
| 4321 | if (LogAcceptsCategory("net")) printf("BitcoinMiner:\n"); | 5160 | if (LogAcceptsCategory("net")) printf("BitcoinMiner:\n"); |
| 4322 | if (LogAcceptsCategory("net")) printf("RandomX proof-of-work found \n powhash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str()); | 5161 | if (LogAcceptsCategory("net")) printf("RandomX proof-of-work found \n powhash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str()); |
| 4323 | if (LogAcceptsCategory("net")) pblock->print(); | 5162 | if (LogAcceptsCategory("relay")) pblock->print(); |
| 4324 | 5163 | ||
| 4325 | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); | 5164 | SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); |
| 4326 | CRITICAL_BLOCK(cs_main) | 5165 | CRITICAL_BLOCK(cs_main) |
| @@ -4675,9 +5514,17 @@ bool SelectCoins(int64 nTargetValue, set<CWalletTx*>& setCoinsRet) | |||
| 4675 | 5514 | ||
| 4676 | 5515 | ||
| 4677 | 5516 | ||
| 4678 | bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, int64& nFeeRequiredRet) | 5517 | // presultRet, when given, says WHY this returned false: the caller cannot work |
| 5518 | // it out afterwards. "Coin selection came up short" and "the wallet could not | ||
| 5519 | // build the transaction" are indistinguishable from the outside -- both are | ||
| 5520 | // just `false` -- and the difference decides whether a pool defers a payment | ||
| 5521 | // or counts it against the payment's retry budget. See WalletSendResult. | ||
| 5522 | bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, int64& nFeeRequiredRet, | ||
| 5523 | WalletSendResult* presultRet) | ||
| 4679 | { | 5524 | { |
| 4680 | nFeeRequiredRet = 0; | 5525 | nFeeRequiredRet = 0; |
| 5526 | if (presultRet) | ||
| 5527 | *presultRet = WALLET_SEND_FAILED; | ||
| 4681 | CRITICAL_BLOCK(cs_main) | 5528 | CRITICAL_BLOCK(cs_main) |
| 4682 | { | 5529 | { |
| 4683 | // txdb must be opened before the mapWallet lock | 5530 | // txdb must be opened before the mapWallet lock |
| @@ -4708,10 +5555,21 @@ bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, in | |||
| 4708 | // fixed this in 107d9e288. | 5555 | // fixed this in 107d9e288. |
| 4709 | int64 nTotalValue = nValue + nFee; | 5556 | int64 nTotalValue = nValue + nFee; |
| 4710 | 5557 | ||
| 4711 | // Choose coins to use | 5558 | // Choose coins to use. |
| 5559 | // | ||
| 5560 | // Failing here is a funding shortfall and nothing else: there | ||
| 5561 | // is not enough spendable coin for nValue plus the fee this | ||
| 5562 | // pass is working with. On the second and later passes that | ||
| 5563 | // fee is GetMinFee() of the transaction the first pass built, | ||
| 5564 | // so this is also how a wallet that can afford the amount but | ||
| 5565 | // not the 11 * CENT charged at 10,000 bytes reports itself. | ||
| 4712 | set<CWalletTx*> setCoins; | 5566 | set<CWalletTx*> setCoins; |
| 4713 | if (!SelectCoins(nTotalValue, setCoins)) | 5567 | if (!SelectCoins(nTotalValue, setCoins)) |
| 5568 | { | ||
| 5569 | if (presultRet) | ||
| 5570 | *presultRet = WALLET_SEND_INSUFFICIENT_FUNDS; | ||
| 4714 | return false; | 5571 | return false; |
| 5572 | } | ||
| 4715 | int64 nValueIn = 0; | 5573 | int64 nValueIn = 0; |
| 4716 | foreach(CWalletTx* pcoin, setCoins) | 5574 | foreach(CWalletTx* pcoin, setCoins) |
| 4717 | nValueIn += pcoin->GetCredit(); | 5575 | nValueIn += pcoin->GetCredit(); |
| @@ -4781,6 +5639,8 @@ bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, in | |||
| 4781 | } | 5639 | } |
| 4782 | } | 5640 | } |
| 4783 | } | 5641 | } |
| 5642 | if (presultRet) | ||
| 5643 | *presultRet = WALLET_SEND_OK; | ||
| 4784 | return true; | 5644 | return true; |
| 4785 | } | 5645 | } |
| 4786 | 5646 | ||
| @@ -4843,16 +5703,38 @@ bool CommitTransactionSpent(const CWalletTx& wtxNew) | |||
| 4843 | 5703 | ||
| 4844 | 5704 | ||
| 4845 | 5705 | ||
| 4846 | bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew) | 5706 | // outcomeRet reports what happened rather than only whether it worked. A |
| 5707 | // caller that has to decide what a failure MEANS -- the pool payout thread -- | ||
| 5708 | // cannot reconstruct it from a balance reading afterwards, because the fee | ||
| 5709 | // that was actually required depends on the size of a transaction that was | ||
| 5710 | // never built. See WalletSendResult. | ||
| 5711 | bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, | ||
| 5712 | SendMoneyOutcome& outcomeRet) | ||
| 4847 | { | 5713 | { |
| 5714 | outcomeRet.result = WALLET_SEND_FAILED; | ||
| 5715 | outcomeRet.nFeeRequired = 0; | ||
| 4848 | CRITICAL_BLOCK(cs_main) | 5716 | CRITICAL_BLOCK(cs_main) |
| 4849 | { | 5717 | { |
| 4850 | int64 nFeeRequired; | 5718 | int64 nFeeRequired; |
| 4851 | if (!CreateTransaction(scriptPubKey, nValue, wtxNew, nFeeRequired)) | 5719 | WalletSendResult createResult = WALLET_SEND_FAILED; |
| 5720 | if (!CreateTransaction(scriptPubKey, nValue, wtxNew, nFeeRequired, &createResult)) | ||
| 4852 | { | 5721 | { |
| 5722 | outcomeRet.result = createResult; | ||
| 5723 | outcomeRet.nFeeRequired = nFeeRequired; | ||
| 4853 | string strError; | 5724 | string strError; |
| 4854 | if (nValue + nFeeRequired > GetBalance()) | 5725 | if (nValue + nFeeRequired > GetBalance()) |
| 4855 | strError = strprintf("Error: This is an oversized transaction that requires a transaction fee of %s ", FormatMoney(nFeeRequired).c_str()); | 5726 | // Not an oversized transaction -- this branch is simply "the |
| 5727 | // spendable balance is too small". Saying otherwise (with a fee | ||
| 5728 | // of 0.00, because CreateTransaction failed before setting one) | ||
| 5729 | // sends anyone reading the log after a failed payout hunting a | ||
| 5730 | // transaction-size problem that does not exist. | ||
| 5731 | strError = strprintf("Error: insufficient spendable balance:" | ||
| 5732 | " need %s (plus fee %s) but only %s is spendable" | ||
| 5733 | " -- mined coin counts as zero until it is %d deep", | ||
| 5734 | FormatMoney(nValue).c_str(), | ||
| 5735 | FormatMoney(nFeeRequired).c_str(), | ||
| 5736 | FormatMoney(GetBalance()).c_str(), | ||
| 5737 | COINBASE_SPENDABLE_DEPTH); | ||
| 4856 | else | 5738 | else |
| 4857 | strError = "Error: Transaction creation failed "; | 5739 | strError = "Error: Transaction creation failed "; |
| 4858 | return error("SendMoney() : %s\n", strError.c_str()); | 5740 | return error("SendMoney() : %s\n", strError.c_str()); |
| @@ -4876,6 +5758,13 @@ bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew) | |||
| 4876 | } | 5758 | } |
| 4877 | wtxNew.RelayWalletTransaction(); | 5759 | wtxNew.RelayWalletTransaction(); |
| 4878 | } | 5760 | } |
| 5761 | outcomeRet.result = WALLET_SEND_OK; | ||
| 4879 | MainFrameRepaint(); | 5762 | MainFrameRepaint(); |
| 4880 | return true; | 5763 | return true; |
| 4881 | } | 5764 | } |
| 5765 | |||
| 5766 | bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew) | ||
| 5767 | { | ||
| 5768 | SendMoneyOutcome ignored; | ||
| 5769 | return SendMoney(scriptPubKey, nValue, wtxNew, ignored); | ||
| 5770 | } | ||
| @@ -33,6 +33,35 @@ static const unsigned int MAX_BLOCK_SIGOPS = 20000; | |||
| 33 | // limit (32 MB), not a block rule. Bitcoin 0.1.0 predated the later 1 MB block | 33 | // limit (32 MB), not a block rule. Bitcoin 0.1.0 predated the later 1 MB block |
| 34 | // cap, so this tree inherited no real block-size consensus limit. | 34 | // cap, so this tree inherited no real block-size consensus limit. |
| 35 | static const unsigned int MAX_BLOCK_SIZE = 1000000; | 35 | static const unsigned int MAX_BLOCK_SIZE = 1000000; |
| 36 | // How much of that cap a template we build may fill. Half, which is what both | ||
| 37 | // assemblers already aimed at -- this only gives the number a name and one | ||
| 38 | // place to live. They had already drifted on how it was enforced, which is the | ||
| 39 | // bug this exists to stop repeating. | ||
| 40 | // | ||
| 41 | // NOT a consensus rule. CheckBlock() decides what the network accepts; this | ||
| 42 | // decides what we are willing to produce, and it has to stay under that. | ||
| 43 | static const unsigned int MAX_BLOCK_SIZE_GEN = MAX_BLOCK_SIZE / 2; | ||
| 44 | |||
| 45 | // Room for one more transaction in a template that currently weighs | ||
| 46 | // nBlockSize bytes and spends nBlockSigOps signature operations? | ||
| 47 | // | ||
| 48 | // Asked BEFORE the transaction is appended, never after. A template that has | ||
| 49 | // already overshot is one CheckBlock() rejects, and asking afterwards only | ||
| 50 | // measures the waste. Both assemblers call this, so neither can drift into a | ||
| 51 | // different idea of what fits. | ||
| 52 | // | ||
| 53 | // Size gets a margin (half the cap); sigops do not. There is no cheap way to | ||
| 54 | // spend the sigop budget usefully and no reason to reserve headroom in it, so | ||
| 55 | // assembly holds itself to the same number CheckBlock() enforces -- and to the | ||
| 56 | // same comparison. CheckBlock() fails on `> MAX_BLOCK_SIGOPS`, so a block | ||
| 57 | // sitting exactly on the limit is valid and this has to accept it too. | ||
| 58 | // Assembly and validation disagreeing by one is how the size bug started. | ||
| 59 | inline bool BlockTemplateFits(unsigned int nBlockSize, unsigned int nBlockSigOps, | ||
| 60 | unsigned int nTxSize, unsigned int nTxSigOps) | ||
| 61 | { | ||
| 62 | return (nBlockSize + nTxSize <= MAX_BLOCK_SIZE_GEN && | ||
| 63 | nBlockSigOps + nTxSigOps <= MAX_BLOCK_SIGOPS); | ||
| 64 | } | ||
| 36 | static const int64 COIN = 100000000; | 65 | static const int64 COIN = 100000000; |
| 37 | // Total Bitflash emission: identical to Bitcoin (21 million). | 66 | // Total Bitflash emission: identical to Bitcoin (21 million). |
| 38 | // MAX_MONEY guards against the value overflow bug (CVE-2010-5139), which in | 67 | // MAX_MONEY guards against the value overflow bug (CVE-2010-5139), which in |
| @@ -41,6 +70,12 @@ static const int64 MAX_MONEY = 21000000 * COIN; | |||
| 41 | inline bool MoneyRange(int64 nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } | 70 | inline bool MoneyRange(int64 nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } |
| 42 | static const int64 CENT = 1000000; | 71 | static const int64 CENT = 1000000; |
| 43 | static const int COINBASE_MATURITY = 100; | 72 | static const int COINBASE_MATURITY = 100; |
| 73 | // Depth at which a coinbase we mined is actually usable. CMerkleTx::GetCredit | ||
| 74 | // values a coinbase at zero until GetBlocksToMaturity() reaches 0, which is | ||
| 75 | // this depth -- not COINBASE_MATURITY. Anything that plans to *spend* mined | ||
| 76 | // coin must gate on this, or it will try to pay out of a balance the wallet | ||
| 77 | // still reports as zero. Both rules live here so they cannot drift apart. | ||
| 78 | static const int COINBASE_SPENDABLE_DEPTH = COINBASE_MATURITY + 20; | ||
| 44 | 79 | ||
| 45 | // Bitflash difficulty floor. The PoW is RandomX (memory-hard), ~1000x slower | 80 | // Bitflash difficulty floor. The PoW is RandomX (memory-hard), ~1000x slower |
| 46 | // per hash than SHA-256d, so the target is much easier than Bitcoin's to let a | 81 | // per hash than SHA-256d, so the target is much easier than Bitcoin's to let a |
| @@ -86,6 +121,11 @@ extern int nMineMode; | |||
| 86 | // something a previous session wrote. | 121 | // something a previous session wrote. |
| 87 | extern bool fMineModeFromCommandLine; | 122 | extern bool fMineModeFromCommandLine; |
| 88 | extern string strParticipantPool; // participant: pool .btf address | 123 | extern string strParticipantPool; // participant: pool .btf address |
| 124 | extern string strPayoutAddress; // participant: /payoutaddr override (mine without a wallet) | ||
| 125 | extern string strPayoutFile; // participant: /payoutfile address list, rotated per reconnect | ||
| 126 | extern bool fPoolNoAnnounce; // /noannounce: suppress pool announcements | ||
| 127 | extern bool fPoolMineBlind; // /poolmineblind: mine on even when deaf to every peer | ||
| 128 | extern int nHashThreads; // participant: /hashthreads override (0 = cores-1) | ||
| 89 | extern string strPoolName; // operator: announced pool name | 129 | extern string strPoolName; // operator: announced pool name |
| 90 | extern string strPoolDashboardUrl; // operator: optional dashboard URL | 130 | extern string strPoolDashboardUrl; // operator: optional dashboard URL |
| 91 | extern string strPoolStatusFile; // operator: optional JSON status output path | 131 | extern string strPoolStatusFile; // operator: optional JSON status output path |
| @@ -98,6 +138,9 @@ extern bool fSoloMineTest; // /solomine: mine without requiring a peer (local te | |||
| 98 | extern int nMinerThreads; | 138 | extern int nMinerThreads; |
| 99 | // Resolves the automatic case into a real count. | 139 | // Resolves the automatic case into a real count. |
| 100 | int MinerThreadCount(); | 140 | int MinerThreadCount(); |
| 141 | // Hash threads one participant session runs; resolves /hashthreads. Not the | ||
| 142 | // same number as MinerThreadCount(), and only ONE session ever runs. | ||
| 143 | int ParticipantHashThreadCount(); | ||
| 101 | extern bool gPoolServerRunning; // GUI/startup state | 144 | extern bool gPoolServerRunning; // GUI/startup state |
| 102 | extern volatile bool gPoolRunning; // runtime pool server loop flag (rpc.cpp) | 145 | extern volatile bool gPoolRunning; // runtime pool server loop flag (rpc.cpp) |
| 103 | 146 | ||
| @@ -332,9 +375,52 @@ struct WalletRecoveryAudit | |||
| 332 | } | 375 | } |
| 333 | }; | 376 | }; |
| 334 | WalletRecoveryAudit GetWalletRecoveryAudit(); | 377 | WalletRecoveryAudit GetWalletRecoveryAudit(); |
| 335 | bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& txNew, int64& nFeeRequiredRet); | 378 | |
| 379 | // Why a send did not go through. | ||
| 380 | // | ||
| 381 | // The pool's payout thread has to decide, on every failed SendMoney, whether | ||
| 382 | // the failure was the wallet's condition (wait for it to change) or a fault | ||
| 383 | // (something is wrong with this payment). It used to guess by re-reading | ||
| 384 | // GetBalance() afterwards and allowing a fixed CENT of headroom for the fee. | ||
| 385 | // That cannot work: the fee here is a step function of the SERIALISED SIZE of | ||
| 386 | // a transaction that no longer exists, and the step is from 0 to 11 * CENT at | ||
| 387 | // 10,000 bytes -- so a one-CENT allowance is either unnecessary or an order of | ||
| 388 | // magnitude short, never right. A pool wallet close to broke selects many | ||
| 389 | // small inputs BECAUSE it is close to broke, which is what pushes the | ||
| 390 | // transaction over 10 KB, so the guess is wrong exactly when the money is | ||
| 391 | // tight. | ||
| 392 | // | ||
| 393 | // The wallet already knows the answer; it just did not say. SelectCoins() | ||
| 394 | // failing is "there is not enough spendable coin to cover the amount plus | ||
| 395 | // whatever fee this size of transaction requires", and it is the only reason | ||
| 396 | // CreateTransaction() can fail that is about funding rather than about the | ||
| 397 | // wallet being broken. | ||
| 398 | enum WalletSendResult | ||
| 399 | { | ||
| 400 | WALLET_SEND_OK = 0, | ||
| 401 | WALLET_SEND_INSUFFICIENT_FUNDS, // coin selection could not cover amount+fee | ||
| 402 | WALLET_SEND_FAILED // anything else: could not build, sign or accept | ||
| 403 | }; | ||
| 404 | |||
| 405 | // What the wallet did with a send request. nFeeRequired is the fee the | ||
| 406 | // transaction turned out to need, which for a failed send is what coin | ||
| 407 | // selection was asked for on top of the amount and could not find -- 0 if it | ||
| 408 | // never got that far. | ||
| 409 | struct SendMoneyOutcome | ||
| 410 | { | ||
| 411 | WalletSendResult result = WALLET_SEND_OK; | ||
| 412 | int64 nFeeRequired = 0; | ||
| 413 | }; | ||
| 414 | |||
| 415 | bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& txNew, int64& nFeeRequiredRet, | ||
| 416 | WalletSendResult* presultRet=NULL); | ||
| 336 | bool CommitTransactionSpent(const CWalletTx& wtxNew); | 417 | bool CommitTransactionSpent(const CWalletTx& wtxNew); |
| 418 | // The three-argument form is kept, and kept as a distinct declaration rather | ||
| 419 | // than a defaulted argument, because gui.cpp declares SendMoney itself instead | ||
| 420 | // of including this header. | ||
| 337 | bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew); | 421 | bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew); |
| 422 | bool SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, | ||
| 423 | SendMoneyOutcome& outcomeRet); | ||
| 338 | 424 | ||
| 339 | 425 | ||
| 340 | 426 | ||
diff --git a/src/main_gui.cpp b/src/main_gui.cpp index a561174..2e4eb91 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp | |||
| @@ -111,6 +111,8 @@ static void PrintUsage() | |||
| 111 | printf(" /help, -help, --help, /?\n"); | 111 | printf(" /help, -help, --help, /?\n"); |
| 112 | printf(" /datadir=PATH\n"); | 112 | printf(" /datadir=PATH\n"); |
| 113 | printf(" /debug\n"); | 113 | printf(" /debug\n"); |
| 114 | printf(" /debugrelay (/debug plus per-message relay chatter and block dumps)\n"); | ||
| 115 | printf(" /maxdebugfilesize=N (roll debug.log at N MB, default 1024; 0 = never)\n"); | ||
| 114 | printf(" /gen\n"); | 116 | printf(" /gen\n"); |
| 115 | printf(" /nogui or /daemon\n"); | 117 | printf(" /nogui or /daemon\n"); |
| 116 | printf(" /selftest=wallet-keypool, wallet-hd, wallet-format, wallet-storage-sanity,\n"); | 118 | printf(" /selftest=wallet-keypool, wallet-hd, wallet-format, wallet-storage-sanity,\n"); |
| @@ -123,6 +125,9 @@ static void PrintUsage() | |||
| 123 | printf("Mining mode:\n"); | 125 | printf("Mining mode:\n"); |
| 124 | printf(" /operator\n"); | 126 | printf(" /operator\n"); |
| 125 | printf(" /participant=POOL_BTF_ADDRESS\n"); | 127 | printf(" /participant=POOL_BTF_ADDRESS\n"); |
| 128 | printf(" /payoutaddr=BTF_ADDRESS (participant: pay shares to this address, no wallet needed)\n"); | ||
| 129 | printf(" /payoutfile=FILE (participant: rotate payout addresses from file)\n"); | ||
| 130 | printf(" /hashthreads=N (participant: mining threads, default cores-1)\n"); | ||
| 126 | printf(" /stratumbridge=POOL_BTF_ADDRESS\n"); | 131 | printf(" /stratumbridge=POOL_BTF_ADDRESS\n"); |
| 127 | printf(" /stratumbridgeport=N (default 3333; listen on 127.0.0.1)\n"); | 132 | printf(" /stratumbridgeport=N (default 3333; listen on 127.0.0.1)\n"); |
| 128 | printf(" /solomine\n"); | 133 | printf(" /solomine\n"); |
| @@ -150,6 +155,7 @@ static void PrintUsage() | |||
| 150 | printf(" /socks=HOST:PORT (SOCKS5 proxy for Nostr, .btf relay, and onion dials)\n"); | 155 | printf(" /socks=HOST:PORT (SOCKS5 proxy for Nostr, .btf relay, and onion dials)\n"); |
| 151 | printf(" /tor[=HOST:PORT] (Tor mode; default SOCKS5 proxy is 127.0.0.1:9050)\n"); | 156 | printf(" /tor[=HOST:PORT] (Tor mode; default SOCKS5 proxy is 127.0.0.1:9050)\n"); |
| 152 | printf(" /managedtor[=PATH] (start Tor, create a hidden service, advertise its onion)\n"); | 157 | printf(" /managedtor[=PATH] (start Tor, create a hidden service, advertise its onion)\n"); |
| 158 | printf(" /automanagedtor (auto-start a bundled Tor if one sits next to the executable)\n"); | ||
| 153 | printf(" /nomanagedtor (disable automatic bundled Tor startup)\n"); | 159 | printf(" /nomanagedtor (disable automatic bundled Tor startup)\n"); |
| 154 | printf(" /oniononly (do not fall back to rendezvous when a .btf onion dial fails)\n"); | 160 | printf(" /oniononly (do not fall back to rendezvous when a .btf onion dial fails)\n"); |
| 155 | printf(" /btfseed=ADDRESS:ENCHEX (extra bootstrap peer, repeatable)\n"); | 161 | printf(" /btfseed=ADDRESS:ENCHEX (extra bootstrap peer, repeatable)\n"); |
| @@ -195,6 +201,9 @@ static void PrintUsage() | |||
| 195 | printf(" /walletsqlite=FILE (stage-load a SQLite wallet export, then exit)\n"); | 201 | printf(" /walletsqlite=FILE (stage-load a SQLite wallet export, then exit)\n"); |
| 196 | printf(" /walletbackend=sqlite (opt-in: run the node on <datadir>/wallet.sqlite\n"); | 202 | printf(" /walletbackend=sqlite (opt-in: run the node on <datadir>/wallet.sqlite\n"); |
| 197 | printf(" instead of wallet.dat; export one first)\n"); | 203 | printf(" instead of wallet.dat; export one first)\n"); |
| 204 | printf(" /createwallet (headless only: permit creating a new wallet in a\n"); | ||
| 205 | printf(" datadir that has none -- otherwise refused, since\n"); | ||
| 206 | printf(" a missing wallet is usually a mount/backup fault)\n"); | ||
| 198 | printf(" /rescan (walk the chain for coins this wallet owns\n"); | 207 | printf(" /rescan (walk the chain for coins this wallet owns\n"); |
| 199 | printf(" but never recorded, then exit)\n"); | 208 | printf(" but never recorded, then exit)\n"); |
| 200 | printf("\n"); | 209 | printf("\n"); |
| @@ -214,6 +223,54 @@ static void ParseStartupArguments(int argc, char* argv[]) | |||
| 214 | if (arg(argc,argv,"/debug") || arg(argc,argv,"-debug")) | 223 | if (arg(argc,argv,"/debug") || arg(argc,argv,"-debug")) |
| 215 | fDebug = true; | 224 | fDebug = true; |
| 216 | 225 | ||
| 226 | // /debugrelay -- everything /debug shows, plus the per-message firehose | ||
| 227 | // ("relay" and "nostrdiscovery" categories: sending: tx, Relaying wtx, | ||
| 228 | // raw CBlock dumps, per-peer discovery lines). That traffic was ~90% of | ||
| 229 | // a 13-hour production debug.log (issue #35), so plain /debug leaves it | ||
| 230 | // off; the message text itself is unchanged for anyone grepping it. | ||
| 231 | if (arg(argc,argv,"/debugrelay") || arg(argc,argv,"-debugrelay")) | ||
| 232 | { | ||
| 233 | fDebug = true; | ||
| 234 | fDebugRelay = true; | ||
| 235 | } | ||
| 236 | |||
| 237 | // /maxdebugfilesize=N -- roll debug.log to debug.log.old once it passes | ||
| 238 | // N MB, checked on each write. Default is 1024 (see util.cpp); 0 turns | ||
| 239 | // the check off and leaves rotation entirely to logrotate. | ||
| 240 | string strMaxDebugSize = argval2(argc, argv, "/maxdebugfilesize", "-maxdebugfilesize"); | ||
| 241 | if (!strMaxDebugSize.empty()) | ||
| 242 | { | ||
| 243 | // Parsed with atoi64() before, which is strtoll() with the endptr | ||
| 244 | // thrown away: it returns 0 for anything it cannot read, so | ||
| 245 | // /maxdebugfilesize=1G and =1024MB both came out as 0 -- "never | ||
| 246 | // roll", identical to asking for that on purpose, with nothing said | ||
| 247 | // to the operator. The old code also multiplied without a range | ||
| 248 | // check, so a big enough N wrapped to a negative cap, which the roll | ||
| 249 | // reads as "off" too. Every rejection is now reported and leaves the | ||
| 250 | // default in place; the value is a plain count of MB, no suffix. | ||
| 251 | const char* psz = strMaxDebugSize.c_str(); | ||
| 252 | char* pszEnd = NULL; | ||
| 253 | errno = 0; | ||
| 254 | int64 nMB = strtoll(psz, &pszEnd, 10); | ||
| 255 | // INT64_MAX, without depending on which limits header this tree pulls in. | ||
| 256 | const int64 nMaxMB = (int64)((~(uint64)0) >> 1) / (1024 * 1024); | ||
| 257 | const char* pszWhy = NULL; | ||
| 258 | if (pszEnd == psz) | ||
| 259 | pszWhy = "not a number"; | ||
| 260 | else if (*pszEnd != '\0') | ||
| 261 | pszWhy = "trailing characters -- the value is a count of MB, with no unit suffix"; | ||
| 262 | else if (errno == ERANGE || nMB < 0) | ||
| 263 | pszWhy = "not a size in MB"; | ||
| 264 | else if (nMB > nMaxMB) | ||
| 265 | pszWhy = "larger than a file size can be"; | ||
| 266 | if (pszWhy) | ||
| 267 | fprintf(stderr, "Ignoring /maxdebugfilesize=%s: %s (keeping %lld MB)\n", | ||
| 268 | strMaxDebugSize.c_str(), pszWhy, | ||
| 269 | (long long)(nMaxDebugFileSize / 1024 / 1024)); | ||
| 270 | else | ||
| 271 | nMaxDebugFileSize = nMB * 1024 * 1024; | ||
| 272 | } | ||
| 273 | |||
| 217 | if (arg(argc,argv,"/gen") || arg(argc,argv,"-gen")) | 274 | if (arg(argc,argv,"/gen") || arg(argc,argv,"-gen")) |
| 218 | { | 275 | { |
| 219 | fGenerateBitcoins = 1; | 276 | fGenerateBitcoins = 1; |
| @@ -245,7 +302,7 @@ static void ParseStartupArguments(int argc, char* argv[]) | |||
| 245 | if (!strProcLimit.empty()) | 302 | if (!strProcLimit.empty()) |
| 246 | { | 303 | { |
| 247 | int n = atoi(strProcLimit.c_str()); | 304 | int n = atoi(strProcLimit.c_str()); |
| 248 | unsigned int nCores = std::thread::hardware_concurrency(); | 305 | unsigned int nCores = AvailableCoreCount(); |
| 249 | if (n < 0) | 306 | if (n < 0) |
| 250 | fprintf(stderr, "Ignoring /genproclimit=%s: not a thread count\n", | 307 | fprintf(stderr, "Ignoring /genproclimit=%s: not a thread count\n", |
| 251 | strProcLimit.c_str()); | 308 | strProcLimit.c_str()); |
| @@ -271,6 +328,18 @@ static void ParseStartupArguments(int argc, char* argv[]) | |||
| 271 | fMineModeFromCommandLine = true; | 328 | fMineModeFromCommandLine = true; |
| 272 | } | 329 | } |
| 273 | 330 | ||
| 331 | string payoutAddr = argval2(argc, argv, "/payoutaddr", "-payoutaddr"); | ||
| 332 | if (!payoutAddr.empty()) | ||
| 333 | strPayoutAddress = payoutAddr; | ||
| 334 | |||
| 335 | string payoutFile = argval2(argc, argv, "/payoutfile", "-payoutfile"); | ||
| 336 | if (!payoutFile.empty()) | ||
| 337 | strPayoutFile = payoutFile; | ||
| 338 | |||
| 339 | string hashThreads = argval2(argc, argv, "/hashthreads", "-hashthreads"); | ||
| 340 | if (!hashThreads.empty()) | ||
| 341 | nHashThreads = atoi(hashThreads.c_str()); | ||
| 342 | |||
| 274 | if (arg(argc,argv,"/stratumbridge") || arg(argc,argv,"-stratumbridge")) | 343 | if (arg(argc,argv,"/stratumbridge") || arg(argc,argv,"-stratumbridge")) |
| 275 | { | 344 | { |
| 276 | fStratumBridge = true; | 345 | fStratumBridge = true; |
| @@ -297,6 +366,12 @@ static void ParseStartupArguments(int argc, char* argv[]) | |||
| 297 | if (!poolDash.empty()) | 366 | if (!poolDash.empty()) |
| 298 | strPoolDashboardUrl = poolDash; | 367 | strPoolDashboardUrl = poolDash; |
| 299 | 368 | ||
| 369 | if (arg(argc,argv,"/noannounce") || arg(argc,argv,"-noannounce")) | ||
| 370 | fPoolNoAnnounce = true; | ||
| 371 | |||
| 372 | if (arg(argc,argv,"/poolmineblind") || arg(argc,argv,"-poolmineblind")) | ||
| 373 | fPoolMineBlind = true; | ||
| 374 | |||
| 300 | string poolFee = argval2(argc, argv, "/poolfee", "-poolfee"); | 375 | string poolFee = argval2(argc, argv, "/poolfee", "-poolfee"); |
| 301 | if (!poolFee.empty()) | 376 | if (!poolFee.empty()) |
| 302 | dPoolFeePercent = atof(poolFee.c_str()); | 377 | dPoolFeePercent = atof(poolFee.c_str()); |
| @@ -317,8 +392,20 @@ static void ParseStartupArguments(int argc, char* argv[]) | |||
| 317 | string rvRelay = argval2(argc, argv, "/rvrelay", "-rvrelay"); | 392 | string rvRelay = argval2(argc, argv, "/rvrelay", "-rvrelay"); |
| 318 | if (!rvRelay.empty()) | 393 | if (!rvRelay.empty()) |
| 319 | { | 394 | { |
| 320 | vBtfMeetingRelays.clear(); | 395 | if (!LooksLikeRelay(rvRelay)) |
| 321 | vBtfMeetingRelays.push_back(rvRelay); | 396 | { |
| 397 | // A malformed pin must not replace the seed list: with the pin | ||
| 398 | // set, no discovered relay can ever mask the typo, and the pool | ||
| 399 | // would publish a garbage (or no) meeting node forever. | ||
| 400 | fprintf(stderr, "Ignoring /rvrelay=%s: expected host:port\n", | ||
| 401 | rvRelay.c_str()); | ||
| 402 | } | ||
| 403 | else | ||
| 404 | { | ||
| 405 | vBtfMeetingRelays.clear(); | ||
| 406 | vBtfMeetingRelays.push_back(rvRelay); | ||
| 407 | fBtfRelayPinned = true; | ||
| 408 | } | ||
| 322 | } | 409 | } |
| 323 | 410 | ||
| 324 | // net.cpp has described nListenPort as "tunable via /port" since it was | 411 | // net.cpp has described nListenPort as "tunable via /port" since it was |
| @@ -359,8 +446,16 @@ static void ParseStartupArguments(int argc, char* argv[]) | |||
| 359 | bool fTorMode = arg(argc, argv, "/tor") || arg(argc, argv, "-tor"); | 446 | bool fTorMode = arg(argc, argv, "/tor") || arg(argc, argv, "-tor"); |
| 360 | string socksProxy = argval2(argc, argv, "/socks", "-socks"); | 447 | string socksProxy = argval2(argc, argv, "/socks", "-socks"); |
| 361 | string bundledTorPath; | 448 | string bundledTorPath; |
| 449 | // Managed Tor is opt-in on this fork. Upstream 1.2.19 auto-starts a | ||
| 450 | // bundled tor whenever one sits next to the executable, which silently | ||
| 451 | // moves Nostr/.btf traffic onto Tor, creates a hidden service, and starts | ||
| 452 | // publishing descriptors with meeting_node="rendezvous-pending" on nodes | ||
| 453 | // that never asked for it -- against our pinned-relay posture. The | ||
| 454 | // upstream behavior remains available under the explicit /automanagedtor. | ||
| 362 | bool fAutoManagedTor = !fManagedTor && !fNoManagedTor && !fTorMode && | 455 | bool fAutoManagedTor = !fManagedTor && !fNoManagedTor && !fTorMode && |
| 363 | socksProxy.empty() && BtfBundledTorPath(bundledTorPath); | 456 | socksProxy.empty() && |
| 457 | (arg(argc, argv, "/automanagedtor") || arg(argc, argv, "-automanagedtor")) && | ||
| 458 | BtfBundledTorPath(bundledTorPath); | ||
| 364 | if (fManagedTor || fAutoManagedTor) | 459 | if (fManagedTor || fAutoManagedTor) |
| 365 | { | 460 | { |
| 366 | if (fManagedTor && (fTorMode || !socksProxy.empty())) | 461 | if (fManagedTor && (fTorMode || !socksProxy.empty())) |
| @@ -804,6 +899,32 @@ int main(int argc, char* argv[]) | |||
| 804 | else { fUseSQLite = true; fCreateSQLite = true; } | 899 | else { fUseSQLite = true; fCreateSQLite = true; } |
| 805 | } | 900 | } |
| 806 | 901 | ||
| 902 | // A headless node whose datadir holds no wallet of either kind is | ||
| 903 | // far more often an operations fault -- datadir not mounted, or | ||
| 904 | // wallet.dat caught mid-rename by a backup -- than a first run. | ||
| 905 | // Creating one here is not the harmless default it looks like: | ||
| 906 | // success also writes the backend marker, and from then on | ||
| 907 | // restoring the real wallet.dat no longer takes effect, because the | ||
| 908 | // marker keeps the node on the empty wallet it just made. Under | ||
| 909 | // 1.2.19 the same fault created a wallet.dat that a restore | ||
| 910 | // overwrote, so operators could recover by putting the file back. | ||
| 911 | // Refuse instead, and name the flag that makes it deliberate. | ||
| 912 | if (fCreateSQLite && fHeadlessStartup && | ||
| 913 | !arg(argc, argv, "/createwallet") && !arg(argc, argv, "-createwallet")) | ||
| 914 | { | ||
| 915 | AttachTerminal(); | ||
| 916 | fprintf(stderr, | ||
| 917 | "No wallet found in %s\n" | ||
| 918 | "Refusing to create one. On a headless node an absent wallet is\n" | ||
| 919 | "usually a mount or backup fault, and creating one would record a\n" | ||
| 920 | "backend marker that makes restoring the real wallet a no-op.\n" | ||
| 921 | "Pass -createwallet to start a new one deliberately, or\n" | ||
| 922 | "-walletbackend=bdb to run on a Berkeley DB wallet.dat.\n", | ||
| 923 | GetAppDir().c_str()); | ||
| 924 | fflush(stderr); | ||
| 925 | return 1; | ||
| 926 | } | ||
| 927 | |||
| 807 | if (fUseSQLite) | 928 | if (fUseSQLite) |
| 808 | { | 929 | { |
| 809 | AttachTerminal(); | 930 | AttachTerminal(); |
| @@ -1041,6 +1162,31 @@ int main(int argc, char* argv[]) | |||
| 1041 | 1162 | ||
| 1042 | ReacceptWalletTransactions(); | 1163 | ReacceptWalletTransactions(); |
| 1043 | 1164 | ||
| 1165 | // /dumpaddresses=N:FILE -- generate N fresh wallet keys and write their | ||
| 1166 | // addresses (one per line) to FILE, then exit. Keys stay in this wallet; | ||
| 1167 | // the file alone is enough for a miner elsewhere to rotate payout | ||
| 1168 | // addresses via /payoutfile. (No HD/xpub exists in this wallet format.) | ||
| 1169 | string dumpSpec = argval2(argc, argv, "/dumpaddresses", "-dumpaddresses"); | ||
| 1170 | if (!dumpSpec.empty()) | ||
| 1171 | { | ||
| 1172 | size_t colon = dumpSpec.rfind(':'); | ||
| 1173 | if (colon == string::npos) { fprintf(stderr, "usage: /dumpaddresses=N:FILE\n"); return 1; } | ||
| 1174 | int n = atoi(dumpSpec.substr(0, colon).c_str()); | ||
| 1175 | string file = dumpSpec.substr(colon + 1); | ||
| 1176 | FILE* f = fopen(file.c_str(), "w"); | ||
| 1177 | if (!f) { fprintf(stderr, "cannot open %s\n", file.c_str()); return 1; } | ||
| 1178 | for (int i = 0; i < n; i++) | ||
| 1179 | { | ||
| 1180 | CKey key; | ||
| 1181 | key.MakeNewKey(); | ||
| 1182 | if (!AddKey(key)) { fclose(f); fprintf(stderr, "AddKey failed at %d\n", i); return 1; } | ||
| 1183 | fprintf(f, "%s\n", PubKeyToAddress(key.GetPubKey()).c_str()); | ||
| 1184 | } | ||
| 1185 | fclose(f); | ||
| 1186 | printf("wrote %d fresh wallet addresses to %s\n", n, file.c_str()); | ||
| 1187 | return 0; | ||
| 1188 | } | ||
| 1189 | |||
| 1044 | if (!StartNode(strErrors)) | 1190 | if (!StartNode(strErrors)) |
| 1045 | { | 1191 | { |
| 1046 | fprintf(stderr,"StartNode: %s\n",strErrors.c_str()); | 1192 | fprintf(stderr,"StartNode: %s\n",strErrors.c_str()); |
diff --git a/src/makefile.mingw b/src/makefile.mingw index 910e025..362794a 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw | |||
| @@ -35,7 +35,7 @@ IMGUI_OBJS = obj/imgui.o obj/imgui_draw.o obj/imgui_tables.o \ | |||
| 35 | obj/imgui_widgets.o obj/imgui_impl_glfw.o obj/imgui_impl_opengl3.o | 35 | obj/imgui_widgets.o obj/imgui_impl_glfw.o obj/imgui_impl_opengl3.o |
| 36 | 36 | ||
| 37 | NODE_OBJS = obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o \ | 37 | NODE_OBJS = obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o \ |
| 38 | obj/market.o obj/sha.o obj/nostr.o obj/randomx_pow.o \ | 38 | obj/sha.o obj/nostr.o obj/randomx_pow.o \ |
| 39 | obj/btfaddr.o obj/btfchan.o obj/btfrv.o obj/btftunnel.o obj/rpc.o \ | 39 | obj/btfaddr.o obj/btfchan.o obj/btfrv.o obj/btftunnel.o obj/rpc.o \ |
| 40 | obj/selftest.o obj/bip32.o obj/walletcmd.o obj/wallet_sqlite.o \ | 40 | obj/selftest.o obj/bip32.o obj/walletcmd.o obj/wallet_sqlite.o \ |
| 41 | obj/proxy.o obj/tor.o | 41 | obj/proxy.o obj/tor.o |
| @@ -51,7 +51,7 @@ GUI_OBJS = obj/gui.o obj/main_gui.o obj/bitflash_res.o | |||
| 51 | # binary. Nothing crashed here, and nothing would say so if it had. | 51 | # binary. Nothing crashed here, and nothing would say so if it had. |
| 52 | HEADERS = headers_core.h headers.h util.h main.h serialize.h uint256.h \ | 52 | HEADERS = headers_core.h headers.h util.h main.h serialize.h uint256.h \ |
| 53 | key.h bignum.h script.h db.h base58.h net.h nostr.h \ | 53 | key.h bignum.h script.h db.h base58.h net.h nostr.h \ |
| 54 | randomx_pow.h btfaddr.h btfchan.h btfrv.h btftunnel.h market.h \ | 54 | randomx_pow.h btfaddr.h btfchan.h btfrv.h btftunnel.h \ |
| 55 | selftest.h walletcmd.h wallet_sqlite.h sockcount.h proxy.h tor.h compat.h font_roboto.h | 55 | selftest.h walletcmd.h wallet_sqlite.h sockcount.h proxy.h tor.h compat.h font_roboto.h |
| 56 | 56 | ||
| 57 | all: bitflash.exe | 57 | all: bitflash.exe |
diff --git a/src/market.cpp b/src/market.cpp deleted file mode 100644 index e5d34f4..0000000 --- a/src/market.cpp +++ /dev/null | |||
| @@ -1,264 +0,0 @@ | |||
| 1 | // Copyright (c) 2009 Satoshi Nakamoto | ||
| 2 | // Distributed under the MIT/X11 software license, see the accompanying | ||
| 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. | ||
| 4 | |||
| 5 | #include "headers.h" | ||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | // | ||
| 17 | // Global state variables | ||
| 18 | // | ||
| 19 | |||
| 20 | //// later figure out how these are persisted | ||
| 21 | map<uint256, CProduct> mapMyProducts; | ||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | map<uint256, CProduct> mapProducts; | ||
| 27 | CCriticalSection cs_mapProducts; | ||
| 28 | |||
| 29 | bool AdvertInsert(const CProduct& product) | ||
| 30 | { | ||
| 31 | uint256 hash = product.GetHash(); | ||
| 32 | bool fNew = false; | ||
| 33 | bool fUpdated = false; | ||
| 34 | |||
| 35 | CRITICAL_BLOCK(cs_mapProducts) | ||
| 36 | { | ||
| 37 | // Insert or find existing product | ||
| 38 | pair<map<uint256, CProduct>::iterator, bool> item = mapProducts.insert(make_pair(hash, product)); | ||
| 39 | CProduct* pproduct = &(*(item.first)).second; | ||
| 40 | fNew = item.second; | ||
| 41 | |||
| 42 | // Update if newer | ||
| 43 | if (product.nSequence > pproduct->nSequence) | ||
| 44 | { | ||
| 45 | *pproduct = product; | ||
| 46 | fUpdated = true; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | //if (fNew) | ||
| 51 | // NotifyProductAdded(hash); | ||
| 52 | //else if (fUpdated) | ||
| 53 | // NotifyProductUpdated(hash); | ||
| 54 | |||
| 55 | return (fNew || fUpdated); | ||
| 56 | } | ||
| 57 | |||
| 58 | void AdvertErase(const CProduct& product) | ||
| 59 | { | ||
| 60 | uint256 hash = product.GetHash(); | ||
| 61 | CRITICAL_BLOCK(cs_mapProducts) | ||
| 62 | mapProducts.erase(hash); | ||
| 63 | //NotifyProductDeleted(hash); | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | template<typename T> | ||
| 85 | unsigned int Union(T& v1, T& v2) | ||
| 86 | { | ||
| 87 | // v1 = v1 union v2 | ||
| 88 | // v1 and v2 must be sorted | ||
| 89 | // returns the number of elements added to v1 | ||
| 90 | |||
| 91 | ///// need to check that this is equivalent, then delete this comment | ||
| 92 | //vector<unsigned short> vUnion(v1.size() + v2.size()); | ||
| 93 | //vUnion.erase(set_union(v1.begin(), v1.end(), | ||
| 94 | // v2.begin(), v2.end(), | ||
| 95 | // vUnion.begin()), | ||
| 96 | // vUnion.end()); | ||
| 97 | |||
| 98 | T vUnion; | ||
| 99 | vUnion.reserve(v1.size() + v2.size()); | ||
| 100 | set_union(v1.begin(), v1.end(), | ||
| 101 | v2.begin(), v2.end(), | ||
| 102 | back_inserter(vUnion)); | ||
| 103 | unsigned int nAdded = vUnion.size() - v1.size(); | ||
| 104 | if (nAdded > 0) | ||
| 105 | v1 = vUnion; | ||
| 106 | return nAdded; | ||
| 107 | } | ||
| 108 | |||
| 109 | void CUser::AddAtom(unsigned short nAtom, bool fOrigin) | ||
| 110 | { | ||
| 111 | // Ignore duplicates | ||
| 112 | if (binary_search(vAtomsIn.begin(), vAtomsIn.end(), nAtom) || | ||
| 113 | find(vAtomsNew.begin(), vAtomsNew.end(), nAtom) != vAtomsNew.end()) | ||
| 114 | return; | ||
| 115 | |||
| 116 | //// instead of zero atom, should change to free atom that propagates, | ||
| 117 | //// limited to lower than a certain value like 5 so conflicts quickly | ||
| 118 | // The zero atom never propagates, | ||
| 119 | // new atoms always propagate through the user that created them | ||
| 120 | if (nAtom == 0 || fOrigin) | ||
| 121 | { | ||
| 122 | vector<unsigned short> vTmp(1, nAtom); | ||
| 123 | Union(vAtomsIn, vTmp); | ||
| 124 | if (fOrigin) | ||
| 125 | vAtomsOut.push_back(nAtom); | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | vAtomsNew.push_back(nAtom); | ||
| 130 | |||
| 131 | if (vAtomsNew.size() >= nFlowthroughRate || vAtomsOut.empty()) | ||
| 132 | { | ||
| 133 | // Select atom to flow through to vAtomsOut | ||
| 134 | vAtomsOut.push_back(vAtomsNew[GetRand(vAtomsNew.size())]); | ||
| 135 | |||
| 136 | // Merge vAtomsNew into vAtomsIn | ||
| 137 | sort(vAtomsNew.begin(), vAtomsNew.end()); | ||
| 138 | Union(vAtomsIn, vAtomsNew); | ||
| 139 | vAtomsNew.clear(); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | bool AddAtomsAndPropagate(uint256 hashUserStart, const vector<unsigned short>& vAtoms, bool fOrigin) | ||
| 144 | { | ||
| 145 | CReviewDB reviewdb; | ||
| 146 | map<uint256, vector<unsigned short> > pmapPropagate[2]; | ||
| 147 | pmapPropagate[0][hashUserStart] = vAtoms; | ||
| 148 | |||
| 149 | for (int side = 0; !pmapPropagate[side].empty(); side = 1 - side) | ||
| 150 | { | ||
| 151 | map<uint256, vector<unsigned short> >& mapFrom = pmapPropagate[side]; | ||
| 152 | map<uint256, vector<unsigned short> >& mapTo = pmapPropagate[1 - side]; | ||
| 153 | |||
| 154 | for (map<uint256, vector<unsigned short> >::iterator mi = mapFrom.begin(); mi != mapFrom.end(); ++mi) | ||
| 155 | { | ||
| 156 | const uint256& hashUser = (*mi).first; | ||
| 157 | const vector<unsigned short>& vReceived = (*mi).second; | ||
| 158 | |||
| 159 | ///// this would be a lot easier on the database if it put the new atom at the beginning of the list, | ||
| 160 | ///// so the change would be right next to the vector size. | ||
| 161 | |||
| 162 | // Read user | ||
| 163 | CUser user; | ||
| 164 | reviewdb.ReadUser(hashUser, user); | ||
| 165 | unsigned int nIn = user.vAtomsIn.size(); | ||
| 166 | unsigned int nNew = user.vAtomsNew.size(); | ||
| 167 | unsigned int nOut = user.vAtomsOut.size(); | ||
| 168 | |||
| 169 | // Add atoms received | ||
| 170 | foreach(unsigned short nAtom, vReceived) | ||
| 171 | user.AddAtom(nAtom, fOrigin); | ||
| 172 | fOrigin = false; | ||
| 173 | |||
| 174 | // Don't bother writing to disk if no changes | ||
| 175 | if (user.vAtomsIn.size() == nIn && user.vAtomsNew.size() == nNew) | ||
| 176 | continue; | ||
| 177 | |||
| 178 | // Propagate | ||
| 179 | if (user.vAtomsOut.size() > nOut) | ||
| 180 | foreach(const uint256& hash, user.vLinksOut) | ||
| 181 | mapTo[hash].insert(mapTo[hash].end(), user.vAtomsOut.begin() + nOut, user.vAtomsOut.end()); | ||
| 182 | |||
| 183 | // Write back | ||
| 184 | if (!reviewdb.WriteUser(hashUser, user)) | ||
| 185 | return false; | ||
| 186 | } | ||
| 187 | mapFrom.clear(); | ||
| 188 | } | ||
| 189 | return true; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | bool CReview::AcceptReview() | ||
| 198 | { | ||
| 199 | // Timestamp | ||
| 200 | nTime = GetTime(); | ||
| 201 | |||
| 202 | // Check signature | ||
| 203 | if (!CKey::Verify(vchPubKeyFrom, GetSigHash(), vchSig)) | ||
| 204 | return false; | ||
| 205 | |||
| 206 | CReviewDB reviewdb; | ||
| 207 | |||
| 208 | // Add review text to recipient | ||
| 209 | vector<CReview> vReviews; | ||
| 210 | reviewdb.ReadReviews(hashTo, vReviews); | ||
| 211 | vReviews.push_back(*this); | ||
| 212 | if (!reviewdb.WriteReviews(hashTo, vReviews)) | ||
| 213 | return false; | ||
| 214 | |||
| 215 | // Add link from sender | ||
| 216 | CUser user; | ||
| 217 | uint256 hashFrom = Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end()); | ||
| 218 | reviewdb.ReadUser(hashFrom, user); | ||
| 219 | user.vLinksOut.push_back(hashTo); | ||
| 220 | if (!reviewdb.WriteUser(hashFrom, user)) | ||
| 221 | return false; | ||
| 222 | |||
| 223 | reviewdb.Close(); | ||
| 224 | |||
| 225 | // Propagate atoms to recipient | ||
| 226 | vector<unsigned short> vZeroAtom(1, 0); | ||
| 227 | if (!AddAtomsAndPropagate(hashTo, user.vAtomsOut.size() ? user.vAtomsOut : vZeroAtom, false)) | ||
| 228 | return false; | ||
| 229 | |||
| 230 | return true; | ||
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | bool CProduct::CheckSignature() | ||
| 238 | { | ||
| 239 | return (CKey::Verify(vchPubKeyFrom, GetSigHash(), vchSig)); | ||
| 240 | } | ||
| 241 | |||
| 242 | bool CProduct::CheckProduct() | ||
| 243 | { | ||
| 244 | if (!CheckSignature()) | ||
| 245 | return false; | ||
| 246 | |||
| 247 | // Make sure it's a summary product | ||
| 248 | if (!mapDetails.empty() || !vOrderForm.empty()) | ||
| 249 | return false; | ||
| 250 | |||
| 251 | // Look up seller's atom count | ||
| 252 | CReviewDB reviewdb("r"); | ||
| 253 | CUser user; | ||
| 254 | reviewdb.ReadUser(GetUserHash(), user); | ||
| 255 | nAtoms = user.GetAtomCount(); | ||
| 256 | reviewdb.Close(); | ||
| 257 | |||
| 258 | ////// delme, this is now done by AdvertInsert | ||
| 259 | //// Store to memory | ||
| 260 | //CRITICAL_BLOCK(cs_mapProducts) | ||
| 261 | // mapProducts[GetHash()] = *this; | ||
| 262 | |||
| 263 | return true; | ||
| 264 | } | ||
diff --git a/src/market.h b/src/market.h deleted file mode 100644 index b5c8b3b..0000000 --- a/src/market.h +++ /dev/null | |||
| @@ -1,182 +0,0 @@ | |||
| 1 | // Copyright (c) 2009 Satoshi Nakamoto | ||
| 2 | // Distributed under the MIT/X11 software license, see the accompanying | ||
| 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. | ||
| 4 | |||
| 5 | class CUser; | ||
| 6 | class CReview; | ||
| 7 | class CProduct; | ||
| 8 | |||
| 9 | static const unsigned int nFlowthroughRate = 2; | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | bool AdvertInsert(const CProduct& product); | ||
| 15 | void AdvertErase(const CProduct& product); | ||
| 16 | bool AddAtomsAndPropagate(uint256 hashUserStart, const vector<unsigned short>& vAtoms, bool fOrigin); | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | class CUser | ||
| 26 | { | ||
| 27 | public: | ||
| 28 | vector<unsigned short> vAtomsIn; | ||
| 29 | vector<unsigned short> vAtomsNew; | ||
| 30 | vector<unsigned short> vAtomsOut; | ||
| 31 | vector<uint256> vLinksOut; | ||
| 32 | |||
| 33 | CUser() | ||
| 34 | { | ||
| 35 | } | ||
| 36 | |||
| 37 | IMPLEMENT_SERIALIZE | ||
| 38 | ( | ||
| 39 | if (!(nType & SER_GETHASH)) | ||
| 40 | READWRITE(nVersion); | ||
| 41 | READWRITE(vAtomsIn); | ||
| 42 | READWRITE(vAtomsNew); | ||
| 43 | READWRITE(vAtomsOut); | ||
| 44 | READWRITE(vLinksOut); | ||
| 45 | ) | ||
| 46 | |||
| 47 | void SetNull() | ||
| 48 | { | ||
| 49 | vAtomsIn.clear(); | ||
| 50 | vAtomsNew.clear(); | ||
| 51 | vAtomsOut.clear(); | ||
| 52 | vLinksOut.clear(); | ||
| 53 | } | ||
| 54 | |||
| 55 | uint256 GetHash() const { return SerializeHash(*this); } | ||
| 56 | |||
| 57 | |||
| 58 | int GetAtomCount() const | ||
| 59 | { | ||
| 60 | return (vAtomsIn.size() + vAtomsNew.size()); | ||
| 61 | } | ||
| 62 | |||
| 63 | void AddAtom(unsigned short nAtom, bool fOrigin); | ||
| 64 | }; | ||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | class CReview | ||
| 73 | { | ||
| 74 | public: | ||
| 75 | int nVersion; | ||
| 76 | uint256 hashTo; | ||
| 77 | map<string, string> mapValue; | ||
| 78 | vector<unsigned char> vchPubKeyFrom; | ||
| 79 | vector<unsigned char> vchSig; | ||
| 80 | |||
| 81 | // memory only | ||
| 82 | unsigned int nTime; | ||
| 83 | int nAtoms; | ||
| 84 | |||
| 85 | |||
| 86 | CReview() | ||
| 87 | { | ||
| 88 | nVersion = 1; | ||
| 89 | hashTo = 0; | ||
| 90 | nTime = 0; | ||
| 91 | nAtoms = 0; | ||
| 92 | } | ||
| 93 | |||
| 94 | IMPLEMENT_SERIALIZE | ||
| 95 | ( | ||
| 96 | READWRITE(this->nVersion); | ||
| 97 | nVersion = this->nVersion; | ||
| 98 | if (!(nType & SER_DISK)) | ||
| 99 | READWRITE(hashTo); | ||
| 100 | READWRITE(mapValue); | ||
| 101 | READWRITE(vchPubKeyFrom); | ||
| 102 | if (!(nType & SER_GETHASH)) | ||
| 103 | READWRITE(vchSig); | ||
| 104 | ) | ||
| 105 | |||
| 106 | uint256 GetHash() const { return SerializeHash(*this); } | ||
| 107 | uint256 GetSigHash() const { return SerializeHash(*this, SER_GETHASH|SER_SKIPSIG); } | ||
| 108 | uint256 GetUserHash() const { return Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end()); } | ||
| 109 | |||
| 110 | |||
| 111 | bool AcceptReview(); | ||
| 112 | }; | ||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | class CProduct | ||
| 121 | { | ||
| 122 | public: | ||
| 123 | int nVersion; | ||
| 124 | CAddress addr; | ||
| 125 | map<string, string> mapValue; | ||
| 126 | map<string, string> mapDetails; | ||
| 127 | vector<pair<string, string> > vOrderForm; | ||
| 128 | unsigned int nSequence; | ||
| 129 | vector<unsigned char> vchPubKeyFrom; | ||
| 130 | vector<unsigned char> vchSig; | ||
| 131 | |||
| 132 | // disk only | ||
| 133 | int nAtoms; | ||
| 134 | |||
| 135 | // memory only | ||
| 136 | set<unsigned int> setSources; | ||
| 137 | |||
| 138 | CProduct() | ||
| 139 | { | ||
| 140 | nVersion = 1; | ||
| 141 | nAtoms = 0; | ||
| 142 | nSequence = 0; | ||
| 143 | } | ||
| 144 | |||
| 145 | IMPLEMENT_SERIALIZE | ||
| 146 | ( | ||
| 147 | READWRITE(this->nVersion); | ||
| 148 | nVersion = this->nVersion; | ||
| 149 | READWRITE(addr); | ||
| 150 | READWRITE(mapValue); | ||
| 151 | if (!(nType & SER_GETHASH)) | ||
| 152 | { | ||
| 153 | READWRITE(mapDetails); | ||
| 154 | READWRITE(vOrderForm); | ||
| 155 | READWRITE(nSequence); | ||
| 156 | } | ||
| 157 | READWRITE(vchPubKeyFrom); | ||
| 158 | if (!(nType & SER_GETHASH)) | ||
| 159 | READWRITE(vchSig); | ||
| 160 | if (nType & SER_DISK) | ||
| 161 | READWRITE(nAtoms); | ||
| 162 | ) | ||
| 163 | |||
| 164 | uint256 GetHash() const { return SerializeHash(*this); } | ||
| 165 | uint256 GetSigHash() const { return SerializeHash(*this, SER_GETHASH|SER_SKIPSIG); } | ||
| 166 | uint256 GetUserHash() const { return Hash(vchPubKeyFrom.begin(), vchPubKeyFrom.end()); } | ||
| 167 | |||
| 168 | |||
| 169 | bool CheckSignature(); | ||
| 170 | bool CheckProduct(); | ||
| 171 | }; | ||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | extern map<uint256, CProduct> mapProducts; | ||
| 181 | extern CCriticalSection cs_mapProducts; | ||
| 182 | extern map<uint256, CProduct> mapMyProducts; | ||
diff --git a/src/net.cpp b/src/net.cpp index 287a4bd..0576402 100644 --- a/src/net.cpp +++ b/src/net.cpp | |||
| @@ -521,28 +521,9 @@ bool GetMyExternalIP(unsigned int& ipRet) | |||
| 521 | 521 | ||
| 522 | 522 | ||
| 523 | 523 | ||
| 524 | void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1) | 524 | // AbandonRequests() lived here, sweeping CNode::mapRequests for a callback a |
| 525 | { | 525 | // closing dialog had registered. Nothing ever registered one and nothing ever |
| 526 | // If the dialog might get closed before the reply comes back, | 526 | // called this, so it went with CRequestTracker. |
| 527 | // call this in the destructor so it doesn't get called after it's deleted. | ||
| 528 | CRITICAL_BLOCK(cs_vNodes) | ||
| 529 | { | ||
| 530 | foreach(CNode* pnode, vNodes) | ||
| 531 | { | ||
| 532 | CRITICAL_BLOCK(pnode->cs_mapRequests) | ||
| 533 | { | ||
| 534 | for (map<uint256, CRequestTracker>::iterator mi = pnode->mapRequests.begin(); mi != pnode->mapRequests.end();) | ||
| 535 | { | ||
| 536 | CRequestTracker& tracker = (*mi).second; | ||
| 537 | if (tracker.fn == fn && tracker.param1 == param1) | ||
| 538 | pnode->mapRequests.erase(mi++); | ||
| 539 | else | ||
| 540 | mi++; | ||
| 541 | } | ||
| 542 | } | ||
| 543 | } | ||
| 544 | } | ||
| 545 | } | ||
| 546 | 527 | ||
| 547 | 528 | ||
| 548 | 529 | ||
| @@ -550,25 +531,10 @@ void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1) | |||
| 550 | 531 | ||
| 551 | 532 | ||
| 552 | 533 | ||
| 553 | // | 534 | // The broadcast/subscribe channel system (AnySubscribed, CNode::IsSubscribed, |
| 554 | // Subscription methods for the broadcast and subscription system. | 535 | // Subscribe, CancelSubscribe) stood here. Its channels were MSG_TABLE and |
| 555 | // Channel numbers are message numbers, i.e. MSG_TABLE and MSG_PRODUCT. | 536 | // MSG_PRODUCT, there was never an inbound handler for "subscribe", |
| 556 | // | 537 | // "sub-cancel", "publish" or "pub-cancel", and the objects it carried are gone. |
| 557 | // The subscription system uses a meet-in-the-middle strategy. | ||
| 558 | // With 100,000 nodes, if senders broadcast to 1000 random nodes and receivers | ||
| 559 | // subscribe to 1000 random nodes, 99.995% (1 - 0.99^1000) of messages will get through. | ||
| 560 | // | ||
| 561 | |||
| 562 | bool AnySubscribed(unsigned int nChannel) | ||
| 563 | { | ||
| 564 | if (pnodeLocalHost->IsSubscribed(nChannel)) | ||
| 565 | return true; | ||
| 566 | CRITICAL_BLOCK(cs_vNodes) | ||
| 567 | foreach(CNode* pnode, vNodes) | ||
| 568 | if (pnode->IsSubscribed(nChannel)) | ||
| 569 | return true; | ||
| 570 | return false; | ||
| 571 | } | ||
| 572 | 538 | ||
| 573 | void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd) | 539 | void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd) |
| 574 | { | 540 | { |
| @@ -581,55 +547,6 @@ void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd) | |||
| 581 | PushMessage("getblocks", CBlockLocator(pindexBegin), hashEnd); | 547 | PushMessage("getblocks", CBlockLocator(pindexBegin), hashEnd); |
| 582 | } | 548 | } |
| 583 | 549 | ||
| 584 | bool CNode::IsSubscribed(unsigned int nChannel) | ||
| 585 | { | ||
| 586 | if (nChannel >= vfSubscribe.size()) | ||
| 587 | return false; | ||
| 588 | return vfSubscribe[nChannel]; | ||
| 589 | } | ||
| 590 | |||
| 591 | void CNode::Subscribe(unsigned int nChannel, unsigned int nHops) | ||
| 592 | { | ||
| 593 | if (nChannel >= vfSubscribe.size()) | ||
| 594 | return; | ||
| 595 | |||
| 596 | if (!AnySubscribed(nChannel)) | ||
| 597 | { | ||
| 598 | // Relay subscribe | ||
| 599 | CRITICAL_BLOCK(cs_vNodes) | ||
| 600 | foreach(CNode* pnode, vNodes) | ||
| 601 | if (pnode != this) | ||
| 602 | pnode->PushMessage("subscribe", nChannel, nHops); | ||
| 603 | } | ||
| 604 | |||
| 605 | vfSubscribe[nChannel] = true; | ||
| 606 | } | ||
| 607 | |||
| 608 | void CNode::CancelSubscribe(unsigned int nChannel) | ||
| 609 | { | ||
| 610 | if (nChannel >= vfSubscribe.size()) | ||
| 611 | return; | ||
| 612 | |||
| 613 | // Prevent from relaying cancel if wasn't subscribed | ||
| 614 | if (!vfSubscribe[nChannel]) | ||
| 615 | return; | ||
| 616 | vfSubscribe[nChannel] = false; | ||
| 617 | |||
| 618 | if (!AnySubscribed(nChannel)) | ||
| 619 | { | ||
| 620 | // Relay subscription cancel | ||
| 621 | CRITICAL_BLOCK(cs_vNodes) | ||
| 622 | foreach(CNode* pnode, vNodes) | ||
| 623 | if (pnode != this) | ||
| 624 | pnode->PushMessage("sub-cancel", nChannel); | ||
| 625 | |||
| 626 | // Clear memory, no longer subscribed | ||
| 627 | if (nChannel == MSG_PRODUCT) | ||
| 628 | CRITICAL_BLOCK(cs_mapProducts) | ||
| 629 | mapProducts.clear(); | ||
| 630 | } | ||
| 631 | } | ||
| 632 | |||
| 633 | 550 | ||
| 634 | 551 | ||
| 635 | 552 | ||
| @@ -1205,6 +1122,18 @@ CNode* ConnectNodeBtfResolved(const string& strBtfAddr, const string& strMeeting | |||
| 1205 | void ThreadBtfAccept(void* parg) | 1122 | void ThreadBtfAccept(void* parg) |
| 1206 | { | 1123 | { |
| 1207 | printf("ThreadBtfAccept started\n"); | 1124 | printf("ThreadBtfAccept started\n"); |
| 1125 | // In operator mode the pool server registers this same node identity as a | ||
| 1126 | // stratum service at the rendezvous relays, and a relay only holds ONE | ||
| 1127 | // service registration per pubkey -- peer-accept and pool-accept would | ||
| 1128 | // kick each other off forever, breaking both. The pool's registration is | ||
| 1129 | // the one that must be stable (that's what miners resolve and dial), so | ||
| 1130 | // the node skips inbound rendezvous here and relies on outbound .btf | ||
| 1131 | // connections for block sync. | ||
| 1132 | if (nMineMode == MINE_OPERATOR) | ||
| 1133 | { | ||
| 1134 | printf("ThreadBtfAccept: operator mode -- skipping peer rendezvous registration\n"); | ||
| 1135 | return; | ||
| 1136 | } | ||
| 1208 | unsigned char pk[32]; | 1137 | unsigned char pk[32]; |
| 1209 | unsigned char sk[32]; | 1138 | unsigned char sk[32]; |
| 1210 | while (!BtfGetIdentity(pk, sk)) | 1139 | while (!BtfGetIdentity(pk, sk)) |
| @@ -1354,17 +1283,6 @@ void CNode::Disconnect() | |||
| 1354 | BtfCloseSocket(hSocket); | 1283 | BtfCloseSocket(hSocket); |
| 1355 | hSocket = INVALID_SOCKET; | 1284 | hSocket = INVALID_SOCKET; |
| 1356 | 1285 | ||
| 1357 | // All of a nodes broadcasts and subscriptions are automatically torn down | ||
| 1358 | // when it goes down, so a node has to stay up to keep its broadcast going. | ||
| 1359 | |||
| 1360 | CRITICAL_BLOCK(cs_mapProducts) | ||
| 1361 | for (map<uint256, CProduct>::iterator mi = mapProducts.begin(); mi != mapProducts.end();) | ||
| 1362 | AdvertRemoveSource(this, MSG_PRODUCT, 0, (*(mi++)).second); | ||
| 1363 | |||
| 1364 | // Cancel subscriptions | ||
| 1365 | for (unsigned int nChannel = 0; nChannel < vfSubscribe.size(); nChannel++) | ||
| 1366 | if (vfSubscribe[nChannel]) | ||
| 1367 | CancelSubscribe(nChannel); | ||
| 1368 | } | 1286 | } |
| 1369 | 1287 | ||
| 1370 | 1288 | ||
| @@ -1489,7 +1407,6 @@ void ThreadSocketHandler2(void* parg) | |||
| 1489 | bool fDelete = false; | 1407 | bool fDelete = false; |
| 1490 | TRY_CRITICAL_BLOCK(pnode->cs_vSend) | 1408 | TRY_CRITICAL_BLOCK(pnode->cs_vSend) |
| 1491 | TRY_CRITICAL_BLOCK(pnode->cs_vRecv) | 1409 | TRY_CRITICAL_BLOCK(pnode->cs_vRecv) |
| 1492 | TRY_CRITICAL_BLOCK(pnode->cs_mapRequests) | ||
| 1493 | TRY_CRITICAL_BLOCK(pnode->cs_inventory) | 1410 | TRY_CRITICAL_BLOCK(pnode->cs_inventory) |
| 1494 | fDelete = true; | 1411 | fDelete = true; |
| 1495 | if (fDelete) | 1412 | if (fDelete) |
| @@ -1597,6 +1514,27 @@ void ThreadSocketHandler2(void* parg) | |||
| 1597 | printf("dropping node %s: its socket is no longer valid\n", | 1514 | printf("dropping node %s: its socket is no longer valid\n", |
| 1598 | pnode->addr.ToString().c_str()); | 1515 | pnode->addr.ToString().c_str()); |
| 1599 | pnode->fDisconnect = true; | 1516 | pnode->fDisconnect = true; |
| 1517 | |||
| 1518 | // Stop referring to it, or it goes into the next poll | ||
| 1519 | // set and fails it again. | ||
| 1520 | // | ||
| 1521 | // Marking fDisconnect is not enough: a node is only | ||
| 1522 | // removed from vNodes once it has drained or once | ||
| 1523 | // DISCONNECT_DRAIN_SECS has passed, so one dead | ||
| 1524 | // descriptor kept failing the wait for a full minute -- | ||
| 1525 | // and a failed wait skips the service loop below | ||
| 1526 | // entirely, which means NO peer is read on that pass. | ||
| 1527 | // Measured on a wedged pool under the old select() | ||
| 1528 | // implementation: 1,027 consecutive passes over 61 | ||
| 1529 | // seconds, one bad socket, the entire node deaf. poll() | ||
| 1530 | // reports a bad descriptor as POLLNVAL on that entry | ||
| 1531 | // rather than failing the whole call, so this path is | ||
| 1532 | // now the backstop rather than the common case. | ||
| 1533 | // | ||
| 1534 | // Not closed, only forgotten. getsockopt just told us | ||
| 1535 | // this descriptor is not ours any more; closing it now | ||
| 1536 | // is how an unrelated socket gets closed instead. | ||
| 1537 | pnode->hSocket = INVALID_SOCKET; | ||
| 1600 | } | 1538 | } |
| 1601 | } | 1539 | } |
| 1602 | } | 1540 | } |
| @@ -1870,6 +1808,13 @@ void ThreadMessageHandler(void* parg) | |||
| 1870 | } | 1808 | } |
| 1871 | } | 1809 | } |
| 1872 | 1810 | ||
| 1811 | // When any peer last told us a height. A wedged node -- one that still has | ||
| 1812 | // peers but has stopped hearing them -- is exactly a node where this stops | ||
| 1813 | // advancing, and that is the one thing the height comparison itself cannot | ||
| 1814 | // see: there is no height to compare. | ||
| 1815 | static int64 nLastPeerHeightSeen = 0; | ||
| 1816 | static int64 nPeerHeightFirstAsked = 0; | ||
| 1817 | |||
| 1873 | int GetPeerMedianHeight() | 1818 | int GetPeerMedianHeight() |
| 1874 | { | 1819 | { |
| 1875 | vector<int> vHeights; | 1820 | vector<int> vHeights; |
| @@ -1881,10 +1826,26 @@ int GetPeerMedianHeight() | |||
| 1881 | if (vHeights.empty()) | 1826 | if (vHeights.empty()) |
| 1882 | return -1; | 1827 | return -1; |
| 1883 | 1828 | ||
| 1829 | nLastPeerHeightSeen = GetTime(); | ||
| 1884 | sort(vHeights.begin(), vHeights.end()); | 1830 | sort(vHeights.begin(), vHeights.end()); |
| 1885 | return vHeights[vHeights.size() / 2]; | 1831 | return vHeights[vHeights.size() / 2]; |
| 1886 | } | 1832 | } |
| 1887 | 1833 | ||
| 1834 | // Seconds since any peer last reported a height. | ||
| 1835 | // | ||
| 1836 | // Before ever hearing one, measure from the first time somebody asked rather | ||
| 1837 | // than from the epoch -- otherwise a node refuses jobs the instant it starts, | ||
| 1838 | // before it has had a chance to complete a single handshake. | ||
| 1839 | int64 GetPeerHeightAgeSecs() | ||
| 1840 | { | ||
| 1841 | int64 nNow = GetTime(); | ||
| 1842 | if (nPeerHeightFirstAsked == 0) | ||
| 1843 | nPeerHeightFirstAsked = nNow; | ||
| 1844 | if (nLastPeerHeightSeen == 0) | ||
| 1845 | return nNow - nPeerHeightFirstAsked; | ||
| 1846 | return nNow - nLastPeerHeightSeen; | ||
| 1847 | } | ||
| 1848 | |||
| 1888 | 1849 | ||
| 1889 | // Say out loud when we are behind the network. | 1850 | // Say out loud when we are behind the network. |
| 1890 | // | 1851 | // |
| @@ -2021,7 +1982,21 @@ void ThreadBitcoinMiner(void* parg) | |||
| 2021 | // Start the miners. Returns how many actually got off the ground. | 1982 | // Start the miners. Returns how many actually got off the ground. |
| 2022 | int StartMinerThreads() | 1983 | int StartMinerThreads() |
| 2023 | { | 1984 | { |
| 2024 | int nThreads = MinerThreadCount(); | 1985 | // MinerThreadCount() sizes the SOLO miner, where each thread hashes a |
| 1986 | // template of its own and more threads is simply more hashrate. | ||
| 1987 | // | ||
| 1988 | // Participant mode is not that shape. Every thread that enters | ||
| 1989 | // BitcoinMiner() takes the MINE_PARTICIPANT branch, and that branch is a | ||
| 1990 | // whole miner: its own Stratum session to the pool, its own payout address | ||
| 1991 | // from the rotation, and its own pool of /hashthreads worker threads. So | ||
| 1992 | // the two counts multiply instead of dividing, and the machine ends up | ||
| 1993 | // running MinerThreadCount() x nHashThreads hashing threads plus one pool | ||
| 1994 | // connection each. On a 12-core host with /hashthreads=2 that is 11 | ||
| 1995 | // sessions and 22 hash threads -- roughly 2x the cores, which is not more | ||
| 1996 | // work done, only more contention -- and with /hashthreads unset it is 11 | ||
| 1997 | // sessions of 11 workers. One session is the entirety of participant mode; | ||
| 1998 | // /hashthreads is what sizes it, and it can then mean what it says. | ||
| 1999 | int nThreads = (nMineMode == MINE_PARTICIPANT) ? 1 : MinerThreadCount(); | ||
| 2025 | int nStarted = 0; | 2000 | int nStarted = 0; |
| 2026 | for (int i = 0; i < nThreads; i++) | 2001 | for (int i = 0; i < nThreads; i++) |
| 2027 | { | 2002 | { |
| @@ -2033,9 +2008,24 @@ int StartMinerThreads() | |||
| 2033 | // The dataset is one allocation shared by all of them; only the 2 MB | 2008 | // The dataset is one allocation shared by all of them; only the 2 MB |
| 2034 | // scratchpads multiply. Printed because "why is it using 2 GB" is the first | 2009 | // scratchpads multiply. Printed because "why is it using 2 GB" is the first |
| 2035 | // question anyone asks. | 2010 | // question anyone asks. |
| 2036 | printf("Mining with %d thread(s) of %u core(s) -- about %d MB " | 2011 | // |
| 2037 | "(~2080 MB shared RandomX dataset + ~2 MB per thread)\n", | 2012 | // Participant mode reports the workers that session will run, not the one |
| 2038 | nStarted, std::thread::hardware_concurrency(), 2080 + 2 * nStarted); | 2013 | // thread that runs it: the hash thread count is the number an operator |
| 2014 | // watches, and it is the number that was wrong here. It is otherwise only | ||
| 2015 | // logged under -debug, which is how 11 sessions ran unnoticed. | ||
| 2016 | if (nMineMode == MINE_PARTICIPANT) | ||
| 2017 | { | ||
| 2018 | int nWorkers = ParticipantHashThreadCount(); | ||
| 2019 | printf("Mining for the pool with %d hash thread(s) of %u core(s) -- about %d MB " | ||
| 2020 | "(~2080 MB shared RandomX dataset + ~2 MB per thread)\n", | ||
| 2021 | nWorkers, AvailableCoreCount(), 2080 + 2 * nWorkers); | ||
| 2022 | } | ||
| 2023 | else | ||
| 2024 | { | ||
| 2025 | printf("Mining with %d thread(s) of %u core(s) -- about %d MB " | ||
| 2026 | "(~2080 MB shared RandomX dataset + ~2 MB per thread)\n", | ||
| 2027 | nStarted, AvailableCoreCount(), 2080 + 2 * nStarted); | ||
| 2028 | } | ||
| 2039 | return nStarted; | 2029 | return nStarted; |
| 2040 | } | 2030 | } |
| 2041 | 2031 | ||
| @@ -2106,20 +2096,41 @@ bool StartNode(string& strError) | |||
| 2106 | // IP address, and port for the socket that is being bound | 2096 | // IP address, and port for the socket that is being bound |
| 2107 | int nRetryLimit = 15; | 2097 | int nRetryLimit = 15; |
| 2108 | struct sockaddr_in sockaddr = addrLocalHost.GetSockAddr(); | 2098 | struct sockaddr_in sockaddr = addrLocalHost.GetSockAddr(); |
| 2109 | // Keep addrLocalHost as this node's advertised address, but listen on all | 2099 | // Managed Tor forwards hidden-service traffic to 127.0.0.1:<port>, which a |
| 2110 | // local interfaces. Managed Tor forwards hidden-service traffic to | 2100 | // LAN-only bind cannot receive -- so a node that advertises an onion |
| 2111 | // 127.0.0.1:<port>, and binding only to the LAN address made that onion | 2101 | // endpoint must listen on all local interfaces. Upstream 1.2.19 bound |
| 2112 | // endpoint unreachable even though the P2P listener was running. | 2102 | // INADDR_ANY unconditionally, which also widened the P2P listener of every |
| 2113 | sockaddr.sin_addr.s_addr = INADDR_ANY; | 2103 | // non-Tor node to VPN tunnels, docker bridges and secondary NICs. Keep the |
| 2104 | // wildcard bind conditional on actually having an onion endpoint. | ||
| 2105 | if (!BtfLocalOnionEndpoint().empty()) | ||
| 2106 | sockaddr.sin_addr.s_addr = INADDR_ANY; | ||
| 2114 | if (bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR) | 2107 | if (bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR) |
| 2115 | { | 2108 | { |
| 2116 | int nErr = WSAGetLastError(); | 2109 | int nErr = WSAGetLastError(); |
| 2117 | if (nErr == WSAEADDRINUSE) | 2110 | if (nErr == WSAEADDRINUSE) |
| 2118 | strError = strprintf("Error: Unable to bind to port %s on this computer. The program is probably already running.", addrLocalHost.ToString().c_str()); | 2111 | { |
| 2112 | // Another local instance holds the default port. Fall back to an | ||
| 2113 | // ephemeral port: inbound direct-IP peering is optional, and .btf | ||
| 2114 | // rendezvous connectivity is unaffected by the local listen port. | ||
| 2115 | sockaddr.sin_port = htons(0); | ||
| 2116 | if (bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR) | ||
| 2117 | { | ||
| 2118 | strError = strprintf("Error: Unable to bind any port on this computer (bind returned error %d)", WSAGetLastError()); | ||
| 2119 | printf("%s\n", strError.c_str()); | ||
| 2120 | return false; | ||
| 2121 | } | ||
| 2122 | struct sockaddr_in bound; | ||
| 2123 | socklen_t nBoundLen = sizeof(bound); | ||
| 2124 | if (getsockname(hListenSocket, (struct sockaddr*)&bound, &nBoundLen) == 0) | ||
| 2125 | addrLocalHost.port = bound.sin_port; | ||
| 2126 | printf("default port in use by another instance; bound to ephemeral %s instead\n", addrLocalHost.ToString().c_str()); | ||
| 2127 | } | ||
| 2119 | else | 2128 | else |
| 2129 | { | ||
| 2120 | strError = strprintf("Error: Unable to bind to port %s on this computer (bind returned error %d)", addrLocalHost.ToString().c_str(), nErr); | 2130 | strError = strprintf("Error: Unable to bind to port %s on this computer (bind returned error %d)", addrLocalHost.ToString().c_str(), nErr); |
| 2121 | printf("%s\n", strError.c_str()); | 2131 | printf("%s\n", strError.c_str()); |
| 2122 | return false; | 2132 | return false; |
| 2133 | } | ||
| 2123 | } | 2134 | } |
| 2124 | printf("bound to addrLocalHost = %s\n\n", addrLocalHost.ToString().c_str()); | 2135 | printf("bound to addrLocalHost = %s\n\n", addrLocalHost.ToString().c_str()); |
| 2125 | 2136 | ||
| @@ -2137,7 +2148,7 @@ bool StartNode(string& strError) | |||
| 2137 | if (addrIncoming.ip) | 2148 | if (addrIncoming.ip) |
| 2138 | addrLocalHost.ip = addrIncoming.ip; | 2149 | addrLocalHost.ip = addrIncoming.ip; |
| 2139 | 2150 | ||
| 2140 | _beginthread([](void*) { | 2151 | if (_beginthread([](void*) { |
| 2141 | unsigned int ip = addrLocalHost.ip; | 2152 | unsigned int ip = addrLocalHost.ip; |
| 2142 | if (GetMyExternalIP(ip)) { | 2153 | if (GetMyExternalIP(ip)) { |
| 2143 | addrLocalHost.ip = ip; | 2154 | addrLocalHost.ip = ip; |
| @@ -2145,7 +2156,8 @@ bool StartNode(string& strError) | |||
| 2145 | CWalletDB().WriteSetting("addrIncoming", addrIncoming); | 2156 | CWalletDB().WriteSetting("addrIncoming", addrIncoming); |
| 2146 | printf("External IP updated: %s\n", addrLocalHost.ToStringIP().c_str()); | 2157 | printf("External IP updated: %s\n", addrLocalHost.ToStringIP().c_str()); |
| 2147 | } | 2158 | } |
| 2148 | }, 0, NULL); | 2159 | }, 0, NULL) == (uintptr_t)-1) |
| 2160 | printf("Error: _beginthread(GetMyExternalIP) failed\n"); | ||
| 2149 | 2161 | ||
| 2150 | // Peers that answered last time, dialled straight away. The relays are the | 2162 | // Peers that answered last time, dialled straight away. The relays are the |
| 2151 | // only way to find anyone otherwise, and reaching them plus walking their | 2163 | // only way to find anyone otherwise, and reaching them plus walking their |
| @@ -5,7 +5,6 @@ | |||
| 5 | class CMessageHeader; | 5 | class CMessageHeader; |
| 6 | class CAddress; | 6 | class CAddress; |
| 7 | class CInv; | 7 | class CInv; |
| 8 | class CRequestTracker; | ||
| 9 | class CNode; | 8 | class CNode; |
| 10 | // Defined in main.h, which is included after this file. Only ever used here as | 9 | // Defined in main.h, which is included after this file. Only ever used here as |
| 11 | // a pointer, so the forward declaration is enough. | 10 | // a pointer, so the forward declaration is enough. |
| @@ -13,6 +12,10 @@ class CBlockIndex; | |||
| 13 | 12 | ||
| 14 | 13 | ||
| 15 | 14 | ||
| 15 | // Held in host byte order, unlike the DEFAULT_PORT constant these replace, | ||
| 16 | // which was htons(8433). Nothing is lost: net.cpp's GetDefaultPort() applies | ||
| 17 | // htons() itself and is what every caller now uses, so the value that reaches | ||
| 18 | // a sockaddr is byte-for-byte what it was. Mainnet stays on 8433. | ||
| 16 | static const unsigned short MAINNET_PORT = 8433; | 19 | static const unsigned short MAINNET_PORT = 8433; |
| 17 | static const unsigned short TESTNET_PORT = 18433; | 20 | static const unsigned short TESTNET_PORT = 18433; |
| 18 | static const unsigned int PUBLISH_HOPS = 5; | 21 | static const unsigned int PUBLISH_HOPS = 5; |
| @@ -26,6 +29,12 @@ enum | |||
| 26 | 29 | ||
| 27 | 30 | ||
| 28 | 31 | ||
| 32 | // Defined in main.cpp. True while this node is tracking the tip rather than | ||
| 33 | // still catching up, measured off the age of our own best block -- see there | ||
| 34 | // for why that and not a peer-reported height. Declared here because net.h is | ||
| 35 | // parsed before main.h and CNode::AskFor needs it. | ||
| 36 | bool IsBlockFetchNearTip(); | ||
| 37 | |||
| 29 | bool GetMyExternalIP(unsigned int& ipRet); | 38 | bool GetMyExternalIP(unsigned int& ipRet); |
| 30 | bool IsTestNet(); | 39 | bool IsTestNet(); |
| 31 | unsigned short GetDefaultPort(); | 40 | unsigned short GetDefaultPort(); |
| @@ -95,12 +104,20 @@ static const int BTF_RECV_TIMEOUT_SECS = 30 * 60; | |||
| 95 | static const int BTF_SEND_STALL_SECS = 10 * 60; | 104 | static const int BTF_SEND_STALL_SECS = 10 * 60; |
| 96 | static const int BTF_HANDSHAKE_GRACE_SECS = 60; | 105 | static const int BTF_HANDSHAKE_GRACE_SECS = 60; |
| 97 | 106 | ||
| 107 | // How long a requested block gets before it is asked for again, in seconds. | ||
| 108 | // Used twice: CNode::AskFor spaces the queued per-announcer block retries this | ||
| 109 | // far apart instead of the two minutes transactions get, and the failover | ||
| 110 | // sweep in main.cpp re-issues a getdata to a different peer once a sent | ||
| 111 | // request has gone unanswered this long. A block body is at most 1 MB and | ||
| 112 | // somebody demonstrably has it -- they announced it -- so on this timescale | ||
| 113 | // "not here yet" means "not coming from that peer", not "still in transit". | ||
| 114 | static const int BLOCK_FETCH_RETRY_SECS = 15; | ||
| 115 | |||
| 98 | // A serialized object cannot be larger than MAX_SIZE, so no valid network | 116 | // A serialized object cannot be larger than MAX_SIZE, so no valid network |
| 99 | // message needs the old 256 MB header allowance. Keep the wire cap aligned with | 117 | // message needs the old 256 MB header allowance. Keep the wire cap aligned with |
| 100 | // what the deserializer can accept and bound how long one peer may sit on a | 118 | // what the deserializer can accept. How long a peer may sit on a partial |
| 101 | // partial payload after a complete header has arrived. | 119 | // payload after a complete header has arrived is BTF_MSG_WAIT_MAX_SECS, below. |
| 102 | static const unsigned int MAX_PROTOCOL_MESSAGE_SIZE = MAX_SIZE; | 120 | static const unsigned int MAX_PROTOCOL_MESSAGE_SIZE = MAX_SIZE; |
| 103 | static const int BTF_INCOMPLETE_MESSAGE_TIMEOUT_SECS = 2 * 60; | ||
| 104 | 121 | ||
| 105 | // Ceiling on simultaneous connections. | 122 | // Ceiling on simultaneous connections. |
| 106 | // | 123 | // |
| @@ -140,6 +157,42 @@ static const unsigned int MAX_CONNECTIONS_PER_IP = 32; | |||
| 140 | // this long regardless of what is still buffered. | 157 | // this long regardless of what is still buffered. |
| 141 | static const int64 DISCONNECT_DRAIN_SECS = 60; | 158 | static const int64 DISCONNECT_DRAIN_SECS = 60; |
| 142 | 159 | ||
| 160 | // How long a pool may go without hearing a height from ANY peer before it | ||
| 161 | // stops issuing jobs. Long enough that ordinary peer churn and a quiet | ||
| 162 | // handshake window do not trip it; short enough that a deaf pool cannot build | ||
| 163 | // more than a few blocks of private fork before it is stopped. Heights | ||
| 164 | // 3966-4085 -- 120 blocks, 5890.50 BTF -- were mined in exactly this state. | ||
| 165 | static const int64 BTF_NO_PEER_HEIGHT_MAX_SECS = 20 * 60; | ||
| 166 | |||
| 167 | // How long a peer may leave this node waiting for the rest of a message it | ||
| 168 | // announced in a header. | ||
| 169 | // | ||
| 170 | // ProcessMessages rewinds the header and breaks when the body has not all | ||
| 171 | // arrived, which is correct for a message still in flight and unbounded for | ||
| 172 | // one that never completes: the same header is re-read, the loop breaks | ||
| 173 | // again, and that peer is never parsed again for the life of the process. | ||
| 174 | // Satoshi left the note -- "need a mechanism to give up waiting for overlong | ||
| 175 | // message size error" -- and this is it. | ||
| 176 | // | ||
| 177 | // It never self-corrects, because any byte that arrives refreshes nLastRecv, | ||
| 178 | // so the inactivity timeout that would otherwise drop the peer never fires. | ||
| 179 | // The socket stays healthy, select() keeps reporting it, and the node goes | ||
| 180 | // silently deaf one peer at a time. | ||
| 181 | // | ||
| 182 | // A header may legitimately outrun its body by a round trip or two; two | ||
| 183 | // minutes is far beyond that, and well inside BTF_RECV_TIMEOUT_SECS. | ||
| 184 | static const int64 BTF_MSG_WAIT_MAX_SECS = 2 * 60; | ||
| 185 | |||
| 186 | // Largest payload a peer may announce in a message header: MAX_PROTOCOL_MESSAGE_SIZE | ||
| 187 | // is defined further up this file. This tree reached the same cap by the same | ||
| 188 | // reasoning as upstream #109 -- a 256 MB allowance is eight times what the | ||
| 189 | // deserializer will accept (serialize.h throws above MAX_SIZE), so the header | ||
| 190 | // check admitted an allocation the parser would only reject later, after | ||
| 191 | // BTF_MSG_WAIT_MAX_SECS of a stalled peer slot -- so only the note survives. | ||
| 192 | // | ||
| 193 | // Both definitions were textually clean and 59 lines apart, so the replay onto | ||
| 194 | // upstream merged them without a conflict and only the compiler objected. | ||
| 195 | |||
| 143 | // Send a ping after this long with nothing to say, so a peer running the | 196 | // Send a ping after this long with nothing to say, so a peer running the |
| 144 | // inactivity check above does not mistake a quiet node for a dead one. Must | 197 | // inactivity check above does not mistake a quiet node for a dead one. Must |
| 145 | // stay well under BTF_RECV_TIMEOUT_SECS. | 198 | // stay well under BTF_RECV_TIMEOUT_SECS. |
| @@ -180,6 +233,7 @@ extern int nBestHeight; | |||
| 180 | // has told us anything. Median rather than maximum so one peer claiming an | 233 | // has told us anything. Median rather than maximum so one peer claiming an |
| 181 | // absurd height cannot move it. | 234 | // absurd height cannot move it. |
| 182 | int GetPeerMedianHeight(); | 235 | int GetPeerMedianHeight(); |
| 236 | int64 GetPeerHeightAgeSecs(); | ||
| 183 | 237 | ||
| 184 | // Lightweight .btf churn accounting. These counters are diagnostics only: they | 238 | // Lightweight .btf churn accounting. These counters are diagnostics only: they |
| 185 | // let an operator tell stale descriptors, relay pairing failures, and paired | 239 | // let an operator tell stale descriptors, relay pairing failures, and paired |
| @@ -197,8 +251,6 @@ void BtfChurnNoteHandshakeTimeout(const string& strBtfAddr, const string& strMee | |||
| 197 | void BtfPexCollect(std::vector<std::string>& vDescOut); | 251 | void BtfPexCollect(std::vector<std::string>& vDescOut); |
| 198 | // Verify descriptors a peer sent and remember the good ones. Returns how many. | 252 | // Verify descriptors a peer sent and remember the good ones. Returns how many. |
| 199 | int BtfPexAccept(const std::vector<std::string>& vDesc); | 253 | int BtfPexAccept(const std::vector<std::string>& vDesc); |
| 200 | void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1); | ||
| 201 | bool AnySubscribed(unsigned int nChannel); | ||
| 202 | void ThreadBitcoinMiner(void* parg); | 254 | void ThreadBitcoinMiner(void* parg); |
| 203 | // Starts MinerThreadCount() miners; returns how many came up. | 255 | // Starts MinerThreadCount() miners; returns how many came up. |
| 204 | int StartMinerThreads(); | 256 | int StartMinerThreads(); |
| @@ -474,13 +526,19 @@ public: | |||
| 474 | 526 | ||
| 475 | 527 | ||
| 476 | 528 | ||
| 529 | // MSG_REVIEW, MSG_PRODUCT and MSG_TABLE no longer have a handler, a producer | ||
| 530 | // or a store -- the marketplace they belonged to is gone. They stay in the | ||
| 531 | // enum, and their names stay in ppszTypeName below, because CInv serialises | ||
| 532 | // `type` as a plain integer: dropping them would renumber nothing today but | ||
| 533 | // would make it tempting to renumber later, and MSG_TX and MSG_BLOCK are wire | ||
| 534 | // values shared with every other node. Reserved, never emitted, never served. | ||
| 477 | enum | 535 | enum |
| 478 | { | 536 | { |
| 479 | MSG_TX = 1, | 537 | MSG_TX = 1, |
| 480 | MSG_BLOCK, | 538 | MSG_BLOCK, |
| 481 | MSG_REVIEW, | 539 | MSG_REVIEW, // reserved, dead |
| 482 | MSG_PRODUCT, | 540 | MSG_PRODUCT, // reserved, dead |
| 483 | MSG_TABLE, | 541 | MSG_TABLE, // reserved, dead |
| 484 | }; | 542 | }; |
| 485 | 543 | ||
| 486 | static const char* ppszTypeName[] = | 544 | static const char* ppszTypeName[] = |
| @@ -567,23 +625,9 @@ public: | |||
| 567 | 625 | ||
| 568 | 626 | ||
| 569 | 627 | ||
| 570 | class CRequestTracker | 628 | // CRequestTracker and the PushRequest/"reply" round trip lived here. Nothing |
| 571 | { | 629 | // in this tree ever called PushRequest, so the table was always empty and the |
| 572 | public: | 630 | // "reply" handler was pure inbound parser. Both removed. |
| 573 | void (*fn)(void*, CDataStream&); | ||
| 574 | void* param1; | ||
| 575 | |||
| 576 | explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL) | ||
| 577 | { | ||
| 578 | fn = fnIn; | ||
| 579 | param1 = param1In; | ||
| 580 | } | ||
| 581 | |||
| 582 | bool IsNull() | ||
| 583 | { | ||
| 584 | return fn == NULL; | ||
| 585 | } | ||
| 586 | }; | ||
| 587 | 631 | ||
| 588 | 632 | ||
| 589 | 633 | ||
| @@ -629,9 +673,6 @@ protected: | |||
| 629 | public: | 673 | public: |
| 630 | int64 nReleaseTime; | 674 | int64 nReleaseTime; |
| 631 | int64 nDisconnectSince; | 675 | int64 nDisconnectSince; |
| 632 | map<uint256, CRequestTracker> mapRequests; | ||
| 633 | CCriticalSection cs_mapRequests; | ||
| 634 | |||
| 635 | // inventory based relay | 676 | // inventory based relay |
| 636 | set<CInv> setInventoryKnown; | 677 | set<CInv> setInventoryKnown; |
| 637 | set<CInv> setInventoryKnown2; | 678 | set<CInv> setInventoryKnown2; |
| @@ -639,9 +680,6 @@ public: | |||
| 639 | CCriticalSection cs_inventory; | 680 | CCriticalSection cs_inventory; |
| 640 | multimap<int64, CInv> mapAskFor; | 681 | multimap<int64, CInv> mapAskFor; |
| 641 | 682 | ||
| 642 | // publish and subscription | ||
| 643 | vector<char> vfSubscribe; | ||
| 644 | |||
| 645 | // Last "btfpeers" we accepted from this node, to rate-limit the exchange. | 683 | // Last "btfpeers" we accepted from this node, to rate-limit the exchange. |
| 646 | int64 nLastPexRecv; | 684 | int64 nLastPexRecv; |
| 647 | 685 | ||
| @@ -659,12 +697,17 @@ public: | |||
| 659 | int64 nLastSend; | 697 | int64 nLastSend; |
| 660 | int64 nLastRecv; | 698 | int64 nLastRecv; |
| 661 | int64 nLastSendEmpty; | 699 | int64 nLastSendEmpty; |
| 662 | int64 nIncompleteMessageStart; | ||
| 663 | unsigned int nIncompleteMessageSize; | ||
| 664 | string strIncompleteMessageCommand; | ||
| 665 | string strBtfAddr; | 700 | string strBtfAddr; |
| 666 | string strBtfMeeting; | 701 | string strBtfMeeting; |
| 667 | 702 | ||
| 703 | // When this peer's message loop last rewound a header without having made | ||
| 704 | // progress, or 0 when it is not waiting, plus how much was buffered then. | ||
| 705 | // The pair times a STALL rather than a duration, so a large message | ||
| 706 | // arriving slowly is never mistaken for one that is never arriving. | ||
| 707 | // See ProcessMessages. | ||
| 708 | int64 nMsgWaitSince; | ||
| 709 | unsigned int nMsgWaitSize; | ||
| 710 | |||
| 668 | // Height this peer announced in its version message, or -1 if it sent a | 711 | // Height this peer announced in its version message, or -1 if it sent a |
| 669 | // version message that predates the field. Knowing how far along everyone | 712 | // version message that predates the field. Knowing how far along everyone |
| 670 | // else is, is the only way a node can tell "in sync" from "not hearing". | 713 | // else is, is the only way a node can tell "in sync" from "not hearing". |
| @@ -694,13 +737,14 @@ public: | |||
| 694 | nLastSend = 0; | 737 | nLastSend = 0; |
| 695 | nLastRecv = 0; | 738 | nLastRecv = 0; |
| 696 | nLastSendEmpty = GetTime(); | 739 | nLastSendEmpty = GetTime(); |
| 697 | nIncompleteMessageStart = 0; | ||
| 698 | nIncompleteMessageSize = 0; | ||
| 699 | strIncompleteMessageCommand.clear(); | ||
| 700 | strBtfAddr.clear(); | 740 | strBtfAddr.clear(); |
| 701 | strBtfMeeting.clear(); | 741 | strBtfMeeting.clear(); |
| 742 | // Not waiting on anything yet. Left indeterminate, the first partial | ||
| 743 | // message this peer sends is measured against whatever was in this | ||
| 744 | // memory, and ProcessMessages drops it as unreadable on first contact. | ||
| 745 | nMsgWaitSince = 0; | ||
| 746 | nMsgWaitSize = 0; | ||
| 702 | nStartingHeight = -1; | 747 | nStartingHeight = -1; |
| 703 | vfSubscribe.assign(256, false); | ||
| 704 | 748 | ||
| 705 | // Push a version message | 749 | // Push a version message |
| 706 | /// when NTP implemented, change to just nTime = GetAdjustedTime() | 750 | /// when NTP implemented, change to just nTime = GetAdjustedTime() |
| @@ -728,6 +772,11 @@ public: | |||
| 728 | { | 772 | { |
| 729 | if (hSocket != INVALID_SOCKET) | 773 | if (hSocket != INVALID_SOCKET) |
| 730 | { | 774 | { |
| 775 | // Route through BtfCloseSocket so the accounting sees this close. | ||
| 776 | // A raw closesocket() here was invisible to sockcount, which is | ||
| 777 | // why the leak numbers never added up, and leaving the descriptor | ||
| 778 | // set afterwards is how a number that now belongs to somebody else | ||
| 779 | // gets closed a second time. (#103) | ||
| 731 | BtfCloseSocket(hSocket); | 780 | BtfCloseSocket(hSocket); |
| 732 | hSocket = INVALID_SOCKET; | 781 | hSocket = INVALID_SOCKET; |
| 733 | } | 782 | } |
| @@ -789,13 +838,51 @@ public: | |||
| 789 | static int64 nLastTime; | 838 | static int64 nLastTime; |
| 790 | nLastTime = nNow = max(nNow, ++nLastTime); | 839 | nLastTime = nNow = max(nNow, ++nLastTime); |
| 791 | 840 | ||
| 792 | // Each retry is 2 minutes after the last | 841 | // Each retry is 2 minutes after the last -- and because nRequestTime |
| 793 | nRequestTime = max(nRequestTime + 2 * 60 * 1000000, nNow); | 842 | // is shared across peers, the same spacing is what strings requests |
| 843 | // for one inv across its announcers. For transactions that is pacing; | ||
| 844 | // for a new block it was the stall of issue #33: nine peers announce | ||
| 845 | // the block, the first one asked does not serve it, and the ninth | ||
| 846 | // candidate is not even asked until sixteen minutes in, with the pool | ||
| 847 | // mining on the stale tip the whole time. | ||
| 848 | // | ||
| 849 | // Blocks retry on the fetch timescale, but only while we are at the | ||
| 850 | // tip. That is where the stall costs something -- the pool keeps | ||
| 851 | // handing out jobs against a dead tip -- and it is also where the | ||
| 852 | // spacing is nearly free: one block is in flight, and its body has | ||
| 853 | // almost always arrived before the second announcer's turn comes | ||
| 854 | // round, so AlreadyHave drops the duplicate before a getdata is sent. | ||
| 855 | // | ||
| 856 | // While catching up neither half holds. A peer working through a | ||
| 857 | // 500-deep queue can be minutes away from block N without anything | ||
| 858 | // being wrong, so at 15s the second announcer is asked at +15s, the | ||
| 859 | // third at +30s, and the bulk download arrives once per announcer -- | ||
| 860 | // up to eight times over. Nothing caps that: the eight-entry cap is on | ||
| 861 | // mapBlockFetch, which is a different mechanism from this spacing. | ||
| 862 | // Two minutes is the right number there, and it is what this line did | ||
| 863 | // before. Failover at the tip does not depend on this being 15s in any | ||
| 864 | // case: mapBlockFetch re-issues a stalled getdata to the next peer on | ||
| 865 | // its own timer, which is what actually fixes #33. | ||
| 866 | bool fFast = (inv.type == MSG_BLOCK && IsBlockFetchNearTip()); | ||
| 867 | int64 nRetryUsecs = (fFast ? BLOCK_FETCH_RETRY_SECS : 2 * 60) * 1000000LL; | ||
| 868 | nRequestTime = max(nRequestTime + nRetryUsecs, nNow); | ||
| 794 | mapAskFor.insert(make_pair(nRequestTime, inv)); | 869 | mapAskFor.insert(make_pair(nRequestTime, inv)); |
| 795 | } | 870 | } |
| 796 | 871 | ||
| 797 | 872 | ||
| 798 | 873 | ||
| 874 | // "sending: tx (N bytes)" alone was ~71% of a 13-hour production | ||
| 875 | // debug.log (issue #35), so tx sends are demoted to the /debugrelay-gated | ||
| 876 | // "relay" category; every other command's send trace stays on "net". | ||
| 877 | // The line is emitted in pieces across BeginMessage/EndMessage/ | ||
| 878 | // AbortMessage, so all three must agree on the category or orphaned | ||
| 879 | // "(N bytes)" fragments would leak into a plain -debug log. | ||
| 880 | // Valid only mid-message (nPushPos != -1). | ||
| 881 | const char* SendLogCategory() const | ||
| 882 | { | ||
| 883 | return strcmp(GetMessageCommand(), "tx") == 0 ? "relay" : "net"; | ||
| 884 | } | ||
| 885 | |||
| 799 | void BeginMessage(const char* pszCommand) | 886 | void BeginMessage(const char* pszCommand) |
| 800 | { | 887 | { |
| 801 | EnterCriticalSection(&cs_vSend); | 888 | EnterCriticalSection(&cs_vSend); |
| @@ -803,17 +890,19 @@ public: | |||
| 803 | AbortMessage(); | 890 | AbortMessage(); |
| 804 | nPushPos = vSend.size(); | 891 | nPushPos = vSend.size(); |
| 805 | vSend << CMessageHeader(pszCommand, 0); | 892 | vSend << CMessageHeader(pszCommand, 0); |
| 806 | if (LogAcceptsCategory("net")) printf("sending: %-12s ", pszCommand); | 893 | if (LogAcceptsCategory(SendLogCategory())) printf("sending: %-12s ", pszCommand); |
| 807 | } | 894 | } |
| 808 | 895 | ||
| 809 | void AbortMessage() | 896 | void AbortMessage() |
| 810 | { | 897 | { |
| 811 | if (nPushPos == -1) | 898 | if (nPushPos == -1) |
| 812 | return; | 899 | return; |
| 900 | // Capture before nPushPos is cleared; GetMessageCommand() needs it. | ||
| 901 | const char* pszCategory = SendLogCategory(); | ||
| 813 | vSend.resize(nPushPos); | 902 | vSend.resize(nPushPos); |
| 814 | nPushPos = -1; | 903 | nPushPos = -1; |
| 815 | LeaveCriticalSection(&cs_vSend); | 904 | LeaveCriticalSection(&cs_vSend); |
| 816 | if (LogAcceptsCategory("net")) printf("(aborted)\n"); | 905 | if (LogAcceptsCategory(pszCategory)) printf("(aborted)\n"); |
| 817 | } | 906 | } |
| 818 | 907 | ||
| 819 | void EndMessage() | 908 | void EndMessage() |
| @@ -821,7 +910,11 @@ public: | |||
| 821 | extern int nDropMessagesTest; | 910 | extern int nDropMessagesTest; |
| 822 | if (nDropMessagesTest > 0 && GetRand(nDropMessagesTest) == 0) | 911 | if (nDropMessagesTest > 0 && GetRand(nDropMessagesTest) == 0) |
| 823 | { | 912 | { |
| 824 | if (LogAcceptsCategory("net")) printf("dropmessages DROPPING SEND MESSAGE\n"); | 913 | // Same category as the "sending: " this line interrupts, so a tx |
| 914 | // does not announce its own drop under "net" having been gated | ||
| 915 | // off under "relay". nPushPos can be -1 here, and then | ||
| 916 | // GetMessageCommand() returns "" and this is "net" as before. | ||
| 917 | if (LogAcceptsCategory(SendLogCategory())) printf("dropmessages DROPPING SEND MESSAGE\n"); | ||
| 825 | AbortMessage(); | 918 | AbortMessage(); |
| 826 | return; | 919 | return; |
| 827 | } | 920 | } |
| @@ -833,10 +926,10 @@ public: | |||
| 833 | unsigned int nSize = vSend.size() - nPushPos - sizeof(CMessageHeader); | 926 | unsigned int nSize = vSend.size() - nPushPos - sizeof(CMessageHeader); |
| 834 | memcpy((char*)&vSend[nPushPos] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize)); | 927 | memcpy((char*)&vSend[nPushPos] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize)); |
| 835 | 928 | ||
| 836 | if (LogAcceptsCategory("net")) printf("(%d bytes) ", nSize); | 929 | if (LogAcceptsCategory(SendLogCategory())) printf("(%d bytes) ", nSize); |
| 837 | //for (int i = nPushPos+sizeof(CMessageHeader); i < min(vSend.size(), nPushPos+sizeof(CMessageHeader)+20U); i++) | 930 | //for (int i = nPushPos+sizeof(CMessageHeader); i < min(vSend.size(), nPushPos+sizeof(CMessageHeader)+20U); i++) |
| 838 | // printf("%02x ", vSend[i] & 0xff); | 931 | // printf("%02x ", vSend[i] & 0xff); |
| 839 | if (LogAcceptsCategory("net")) printf("\n"); | 932 | if (LogAcceptsCategory(SendLogCategory())) printf("\n"); |
| 840 | 933 | ||
| 841 | nPushPos = -1; | 934 | nPushPos = -1; |
| 842 | LeaveCriticalSection(&cs_vSend); | 935 | LeaveCriticalSection(&cs_vSend); |
| @@ -974,49 +1067,6 @@ public: | |||
| 974 | } | 1067 | } |
| 975 | 1068 | ||
| 976 | 1069 | ||
| 977 | void PushRequest(const char* pszCommand, | ||
| 978 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 979 | { | ||
| 980 | uint256 hashReply; | ||
| 981 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 982 | |||
| 983 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 984 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 985 | |||
| 986 | PushMessage(pszCommand, hashReply); | ||
| 987 | } | ||
| 988 | |||
| 989 | template<typename T1> | ||
| 990 | void PushRequest(const char* pszCommand, const T1& a1, | ||
| 991 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 992 | { | ||
| 993 | uint256 hashReply; | ||
| 994 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 995 | |||
| 996 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 997 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 998 | |||
| 999 | PushMessage(pszCommand, hashReply, a1); | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | template<typename T1, typename T2> | ||
| 1003 | void PushRequest(const char* pszCommand, const T1& a1, const T2& a2, | ||
| 1004 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 1005 | { | ||
| 1006 | uint256 hashReply; | ||
| 1007 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 1008 | |||
| 1009 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 1010 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 1011 | |||
| 1012 | PushMessage(pszCommand, hashReply, a1, a2); | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | |||
| 1016 | |||
| 1017 | bool IsSubscribed(unsigned int nChannel); | ||
| 1018 | void Subscribe(unsigned int nChannel, unsigned int nHops=0); | ||
| 1019 | void CancelSubscribe(unsigned int nChannel); | ||
| 1020 | void Disconnect(); | 1070 | void Disconnect(); |
| 1021 | }; | 1071 | }; |
| 1022 | 1072 | ||
| @@ -1073,50 +1123,8 @@ inline void RelayMessage<>(const CInv& inv, const CDataStream& ss) | |||
| 1073 | 1123 | ||
| 1074 | 1124 | ||
| 1075 | 1125 | ||
| 1076 | // | 1126 | // The publish/subscribe advert templates that used to close this file |
| 1077 | // Templates for the publish and subscription system. | 1127 | // (AdvertStartPublish / AdvertStopPublish / AdvertRemoveSource) are gone along |
| 1078 | // The object being published as T& obj needs to have: | 1128 | // with the marketplace objects they carried. Nothing ever handled an inbound |
| 1079 | // a set<unsigned int> setSources member | 1129 | // "publish" or "pub-cancel", so the machinery only ever pushed; what it pushed |
| 1080 | // specializations of AdvertInsert and AdvertErase | 1130 | // was CProduct, which no longer exists. |
| 1081 | // Currently implemented for CTable and CProduct. | ||
| 1082 | // | ||
| 1083 | |||
| 1084 | template<typename T> | ||
| 1085 | void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1086 | { | ||
| 1087 | // Add to sources | ||
| 1088 | obj.setSources.insert(pfrom->addr.ip); | ||
| 1089 | |||
| 1090 | if (!AdvertInsert(obj)) | ||
| 1091 | return; | ||
| 1092 | |||
| 1093 | // Relay | ||
| 1094 | CRITICAL_BLOCK(cs_vNodes) | ||
| 1095 | foreach(CNode* pnode, vNodes) | ||
| 1096 | if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel))) | ||
| 1097 | pnode->PushMessage("publish", nChannel, nHops, obj); | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | template<typename T> | ||
| 1101 | void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1102 | { | ||
| 1103 | uint256 hash = obj.GetHash(); | ||
| 1104 | |||
| 1105 | CRITICAL_BLOCK(cs_vNodes) | ||
| 1106 | foreach(CNode* pnode, vNodes) | ||
| 1107 | if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel))) | ||
| 1108 | pnode->PushMessage("pub-cancel", nChannel, nHops, hash); | ||
| 1109 | |||
| 1110 | AdvertErase(obj); | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | template<typename T> | ||
| 1114 | void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1115 | { | ||
| 1116 | // Remove a source | ||
| 1117 | obj.setSources.erase(pfrom->addr.ip); | ||
| 1118 | |||
| 1119 | // If no longer supported by any sources, cancel it | ||
| 1120 | if (obj.setSources.empty()) | ||
| 1121 | AdvertStopPublish(pfrom, nChannel, nHops, obj); | ||
| 1122 | } | ||
diff --git a/src/nostr.cpp b/src/nostr.cpp index 35fde43..f44dc08 100644 --- a/src/nostr.cpp +++ b/src/nostr.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include "headers.h" | 6 | #include "headers.h" |
| 7 | #include <openssl/ssl.h> | 7 | #include <openssl/ssl.h> |
| 8 | #include <openssl/err.h> | 8 | #include <openssl/err.h> |
| 9 | #include <openssl/x509.h> | ||
| 9 | #include <openssl/x509v3.h> | 10 | #include <openssl/x509v3.h> |
| 10 | #ifdef _WIN32 | 11 | #ifdef _WIN32 |
| 11 | #include <wincrypt.h> | 12 | #include <wincrypt.h> |
| @@ -82,9 +83,20 @@ static CCriticalSection cs_btfPeers; | |||
| 82 | // independent relays. Kept only one Cloudflare-fronted relay (damus.io, the | 83 | // independent relays. Kept only one Cloudflare-fronted relay (damus.io, the |
| 83 | // most established) and filled the rest with relays on different hosts | 84 | // most established) and filled the rest with relays on different hosts |
| 84 | // (Hetzner, various independents) so no single upstream provider is a | 85 | // (Hetzner, various independents) so no single upstream provider is a |
| 85 | // single point of failure for peer discovery. 7 relays lands inside the | 86 | // single point of failure for peer discovery. 8 relays lands inside the |
| 86 | // commonly recommended 5-10 range for redundancy without keeping an | 87 | // commonly recommended 5-10 range for redundancy without keeping an |
| 87 | // excessive number of persistent sockets open per node. | 88 | // excessive number of persistent sockets open per node. |
| 89 | // | ||
| 90 | // Four entries were removed after probing all twelve from two independent | ||
| 91 | // networks (a residential line and the pool's VPS, over both v4 and v6): | ||
| 92 | // relay.current.fyi, relay.nostr.bg -- no A record, and the AAAA for both | ||
| 93 | // lands on the same parked host (2a04:c300:1000::381) serving a | ||
| 94 | // certificate that fails verification. The domains are gone. | ||
| 95 | // relay.nostr.band, nostr.fmt.wiz.biz -- TCP connects, TLS handshake never | ||
| 96 | // completes, from both networks. Whether they are down or refusing | ||
| 97 | // non-browser clients, this node cannot use them. | ||
| 98 | // A dead relay is not free: each one holds a connect/retry slot and delays | ||
| 99 | // discovery, so carrying it costs latency for no redundancy. | ||
| 88 | const char* pszNostrRelays[] = { | 100 | const char* pszNostrRelays[] = { |
| 89 | "wss://relay.damus.io", | 101 | "wss://relay.damus.io", |
| 90 | "wss://nos.lol", | 102 | "wss://nos.lol", |
| @@ -97,6 +109,8 @@ const char* pszNostrRelays[] = { | |||
| 97 | "wss://offchain.pub", | 109 | "wss://offchain.pub", |
| 98 | "wss://nostr.mom", | 110 | "wss://nostr.mom", |
| 99 | "wss://relay.primal.net", | 111 | "wss://relay.primal.net", |
| 112 | "wss://nostr.oxtr.dev", | ||
| 113 | "wss://relay.nos.social", | ||
| 100 | }; | 114 | }; |
| 101 | const int nNostrRelays = ARRAYLEN(pszNostrRelays); | 115 | const int nNostrRelays = ARRAYLEN(pszNostrRelays); |
| 102 | 116 | ||
| @@ -110,6 +124,12 @@ vector<string> vBtfMeetingRelays = { | |||
| 110 | "31.44.4.249:8434", // New Jersey, US | 124 | "31.44.4.249:8434", // New Jersey, US |
| 111 | "90.156.222.107:8434", // Almaty, KZ | 125 | "90.156.222.107:8434", // Almaty, KZ |
| 112 | }; | 126 | }; |
| 127 | // True when the operator pinned the relay list with /rvrelay: discovered | ||
| 128 | // relays must NOT be merged in (BtfAllRelays), otherwise one accept thread | ||
| 129 | // per discovered relay is spawned and each one optimistically overwrites the | ||
| 130 | // advertised meeting node (BtfSetActiveRelay) before registering -- a | ||
| 131 | // discovered but unreachable relay then flaps the published .btf descriptor. | ||
| 132 | bool fBtfRelayPinned = false; | ||
| 113 | string strBtfActiveRelay; | 133 | string strBtfActiveRelay; |
| 114 | static CCriticalSection cs_activeRelay; | 134 | static CCriticalSection cs_activeRelay; |
| 115 | static string strBtfOnionEndpoint; | 135 | static string strBtfOnionEndpoint; |
| @@ -131,6 +151,19 @@ string BtfActiveRelay() | |||
| 131 | 151 | ||
| 132 | void BtfSetActiveRelay(const string& relay) | 152 | void BtfSetActiveRelay(const string& relay) |
| 133 | { | 153 | { |
| 154 | // With /rvrelay set, the descriptor must never name anything but the pin. | ||
| 155 | // No caller can reach here with another relay today -- both registration | ||
| 156 | // paths draw their list from BtfAllRelays(), which returns the pin alone -- | ||
| 157 | // but the descriptor is the one thing a miner obeys, so the invariant is | ||
| 158 | // enforced at the sink instead of being retrusted to every future caller. | ||
| 159 | if (fBtfRelayPinned && | ||
| 160 | find(vBtfMeetingRelays.begin(), vBtfMeetingRelays.end(), relay) == vBtfMeetingRelays.end()) | ||
| 161 | { | ||
| 162 | LogPrint("pool", "refusing to advertise meeting relay %s: /rvrelay pins %s\n", | ||
| 163 | relay.c_str(), | ||
| 164 | vBtfMeetingRelays.empty() ? "an empty list" : vBtfMeetingRelays[0].c_str()); | ||
| 165 | return; | ||
| 166 | } | ||
| 134 | CRITICAL_BLOCK(cs_activeRelay) | 167 | CRITICAL_BLOCK(cs_activeRelay) |
| 135 | strBtfActiveRelay = relay; | 168 | strBtfActiveRelay = relay; |
| 136 | } | 169 | } |
| @@ -166,7 +199,7 @@ static map<string, BtfPoolAnnouncement> g_discoveredPools; | |||
| 166 | static CCriticalSection cs_discoveredPools; | 199 | static CCriticalSection cs_discoveredPools; |
| 167 | 200 | ||
| 168 | // Basic sanity: "host:port" with a numeric 1..65535 port and a plausible host. | 201 | // Basic sanity: "host:port" with a numeric 1..65535 port and a plausible host. |
| 169 | static bool LooksLikeRelay(const string& r) | 202 | bool LooksLikeRelay(const string& r) |
| 170 | { | 203 | { |
| 171 | size_t colon = r.rfind(':'); | 204 | size_t colon = r.rfind(':'); |
| 172 | if (colon == string::npos || colon == 0 || colon + 1 >= r.size()) | 205 | if (colon == string::npos || colon == 0 || colon + 1 >= r.size()) |
| @@ -194,6 +227,8 @@ static bool LooksLikeRelay(const string& r) | |||
| 194 | vector<string> BtfAllRelays() | 227 | vector<string> BtfAllRelays() |
| 195 | { | 228 | { |
| 196 | vector<string> all = vBtfMeetingRelays; | 229 | vector<string> all = vBtfMeetingRelays; |
| 230 | if (fBtfRelayPinned) | ||
| 231 | return all; | ||
| 197 | CRITICAL_BLOCK(cs_discoveredRelays) | 232 | CRITICAL_BLOCK(cs_discoveredRelays) |
| 198 | foreach(const string& r, g_discoveredRelays) | 233 | foreach(const string& r, g_discoveredRelays) |
| 199 | if (find(all.begin(), all.end(), r) == all.end()) | 234 | if (find(all.begin(), all.end(), r) == all.end()) |
| @@ -201,6 +236,18 @@ vector<string> BtfAllRelays() | |||
| 201 | return all; | 236 | return all; |
| 202 | } | 237 | } |
| 203 | 238 | ||
| 239 | // Relays learned from Nostr, whether or not a pin is in force. Reporting only: | ||
| 240 | // it exists so the pool can name what /rvrelay is excluding. Anything choosing | ||
| 241 | // a relay to register or dial at must keep using BtfAllRelays(). | ||
| 242 | vector<string> BtfDiscoveredRelays() | ||
| 243 | { | ||
| 244 | vector<string> out; | ||
| 245 | CRITICAL_BLOCK(cs_discoveredRelays) | ||
| 246 | foreach(const string& r, g_discoveredRelays) | ||
| 247 | out.push_back(r); | ||
| 248 | return out; | ||
| 249 | } | ||
| 250 | |||
| 204 | 251 | ||
| 205 | // | 252 | // |
| 206 | // Utilities | 253 | // Utilities |
| @@ -578,26 +625,52 @@ public: | |||
| 578 | { | 625 | { |
| 579 | sslctx = SSL_CTX_new(TLS_client_method()); | 626 | sslctx = SSL_CTX_new(TLS_client_method()); |
| 580 | if (!sslctx) return false; | 627 | if (!sslctx) return false; |
| 628 | |||
| 629 | // SSL_CTX_new defaults to SSL_VERIFY_NONE with no trust store | ||
| 630 | // loaded, so until now every wss:// relay connection was accepted | ||
| 631 | // without looking at the certificate at all -- any on-path | ||
| 632 | // attacker could impersonate a relay. Our own events are signed, | ||
| 633 | // so a MITM cannot forge them, but it could drop the rendezvous | ||
| 634 | // registration (leaving the pool unreachable to miners) or answer | ||
| 635 | // subscriptions with whatever it liked. | ||
| 581 | SSL_CTX_set_verify(sslctx, SSL_VERIFY_PEER, NULL); | 636 | SSL_CTX_set_verify(sslctx, SSL_VERIFY_PEER, NULL); |
| 637 | |||
| 638 | // OpenSSL's default verify paths are usually empty on Windows, so | ||
| 639 | // the system ROOT store is loaded too -- without it verification | ||
| 640 | // fails closed there and every relay becomes unreachable. | ||
| 582 | bool fTrustLoaded = (SSL_CTX_set_default_verify_paths(sslctx) == 1); | 641 | bool fTrustLoaded = (SSL_CTX_set_default_verify_paths(sslctx) == 1); |
| 583 | #ifdef _WIN32 | 642 | #ifdef _WIN32 |
| 584 | fTrustLoaded = BtfLoadWindowsRootCertificates(sslctx) || fTrustLoaded; | 643 | fTrustLoaded = BtfLoadWindowsRootCertificates(sslctx) || fTrustLoaded; |
| 585 | #endif | 644 | #endif |
| 586 | if (!fTrustLoaded) | 645 | if (!fTrustLoaded) |
| 587 | return error("Nostr: failed to load TLS trust store"); | 646 | return error("Nostr: failed to load TLS trust store"); |
| 647 | |||
| 648 | // Floor the protocol version. Verification says who the peer is, | ||
| 649 | // not how weak a cipher suite we will agree to; without this a | ||
| 650 | // relay offering nothing better still gets TLS 1.0. | ||
| 651 | SSL_CTX_set_min_proto_version(sslctx, TLS1_2_VERSION); | ||
| 652 | |||
| 588 | ssl = SSL_new(sslctx); | 653 | ssl = SSL_new(sslctx); |
| 589 | if (!ssl) return false; | 654 | if (!ssl) return false; |
| 590 | SSL_set_fd(ssl, (int)hSocket); | 655 | SSL_set_fd(ssl, (int)hSocket); |
| 591 | if (SSL_set_tlsext_host_name(ssl, host.c_str()) != 1) | 656 | if (SSL_set_tlsext_host_name(ssl, host.c_str()) != 1) // SNI |
| 592 | return error("Nostr: failed to set TLS SNI for %s", host.c_str()); | 657 | return error("Nostr: failed to set TLS SNI for %s", host.c_str()); |
| 658 | |||
| 659 | // Verifying the chain is not enough on its own: without this the | ||
| 660 | // relay could present any certificate a trusted CA ever issued, | ||
| 661 | // for any name. SSL_set1_host binds the check to the host we asked | ||
| 662 | // for. | ||
| 593 | if (SSL_set1_host(ssl, host.c_str()) != 1) | 663 | if (SSL_set1_host(ssl, host.c_str()) != 1) |
| 594 | return error("Nostr: failed to set TLS hostname for %s", host.c_str()); | 664 | return error("Nostr: failed to set TLS hostname for %s", host.c_str()); |
| 665 | |||
| 595 | if (SSL_connect(ssl) != 1) | 666 | if (SSL_connect(ssl) != 1) |
| 667 | { | ||
| 668 | long nVerify = SSL_get_verify_result(ssl); | ||
| 669 | if (nVerify != X509_V_OK) | ||
| 670 | return error("Nostr: certificate for %s rejected: %s", | ||
| 671 | host.c_str(), X509_verify_cert_error_string(nVerify)); | ||
| 596 | return error("Nostr: TLS handshake with %s failed", host.c_str()); | 672 | return error("Nostr: TLS handshake with %s failed", host.c_str()); |
| 597 | long nVerify = SSL_get_verify_result(ssl); | 673 | } |
| 598 | if (nVerify != X509_V_OK) | ||
| 599 | return error("Nostr: TLS certificate verification failed for %s: %s", | ||
| 600 | host.c_str(), X509_verify_cert_error_string(nVerify)); | ||
| 601 | } | 674 | } |
| 602 | 675 | ||
| 603 | // WebSocket handshake (HTTP Upgrade) | 676 | // WebSocket handshake (HTTP Upgrade) |
| @@ -1049,9 +1122,15 @@ static void HandlePoolAnnouncement(const json& ev) | |||
| 1049 | 1122 | ||
| 1050 | // Resolve a `.btf` address via a relay: fetch the descriptor published by that | 1123 | // Resolve a `.btf` address via a relay: fetch the descriptor published by that |
| 1051 | // address's key and verify it self-certifies under the decoded pubkey. | 1124 | // address's key and verify it self-certifies under the decoded pubkey. |
| 1125 | // fExchangeOk reports whether the relay actually finished the conversation | ||
| 1126 | // (an EOSE arrived) as opposed to the socket dying mid-read. The caller pools | ||
| 1127 | // this socket for reuse, and only a completed exchange leaves it in a state | ||
| 1128 | // the next caller can trust. | ||
| 1052 | static bool ResolveDescriptor(CWebSocket& ws, void* ctx, const string& btfAddr, | 1129 | static bool ResolveDescriptor(CWebSocket& ws, void* ctx, const string& btfAddr, |
| 1053 | btf::Descriptor& out, string* rawOut = NULL) | 1130 | btf::Descriptor& out, bool& fExchangeOk, |
| 1131 | string* rawOut = NULL) | ||
| 1054 | { | 1132 | { |
| 1133 | fExchangeOk = false; | ||
| 1055 | unsigned char pk[32]; | 1134 | unsigned char pk[32]; |
| 1056 | if (!btf::ParseAddress(btfAddr, pk)) | 1135 | if (!btf::ParseAddress(btfAddr, pk)) |
| 1057 | return false; | 1136 | return false; |
| @@ -1063,30 +1142,46 @@ static bool ResolveDescriptor(CWebSocket& ws, void* ctx, const string& btfAddr, | |||
| 1063 | json req = json::array({ "REQ", "btf-resolve", filter }); | 1142 | json req = json::array({ "REQ", "btf-resolve", filter }); |
| 1064 | if (!ws.SendText(req.dump())) | 1143 | if (!ws.SendText(req.dump())) |
| 1065 | return false; | 1144 | return false; |
| 1145 | bool fFound = false; | ||
| 1066 | for (;;) | 1146 | for (;;) |
| 1067 | { | 1147 | { |
| 1068 | string msg; | 1148 | string msg; |
| 1069 | if (!ws.RecvText(msg)) | 1149 | if (!ws.RecvText(msg)) |
| 1070 | break; | 1150 | return fFound; // socket died mid-exchange; fExchangeOk stays false |
| 1071 | json j; | 1151 | json j; |
| 1072 | try { j = json::parse(msg); } catch (...) { continue; } | 1152 | try { j = json::parse(msg); } catch (...) { continue; } |
| 1073 | if (!j.is_array() || j.empty() || !j[0].is_string()) continue; | 1153 | if (!j.is_array() || j.empty() || !j[0].is_string()) continue; |
| 1074 | string t = j[0].get<string>(); | 1154 | string t = j[0].get<string>(); |
| 1155 | // Every relay message names the subscription it belongs to. This socket | ||
| 1156 | // is pooled and reused, so a message left over from an earlier exchange | ||
| 1157 | // is the ordinary case here, not an exotic one -- and an unmatched EOSE | ||
| 1158 | // ends this query before it has begun, which is indistinguishable from | ||
| 1159 | // "that descriptor does not exist". Check the id. | ||
| 1160 | if (j.size() >= 2 && j[1].is_string() && j[1].get<string>() != "btf-resolve") | ||
| 1161 | continue; | ||
| 1075 | if (t == "EVENT" && j.size() >= 3) | 1162 | if (t == "EVENT" && j.size() >= 3) |
| 1076 | { | 1163 | { |
| 1077 | const json& ev = j[2]; | 1164 | const json& ev = j[2]; |
| 1078 | if (ev.contains("content") && | 1165 | if (!fFound && ev.contains("content") && |
| 1079 | btf::VerifyDescriptor(ctx, ev["content"].get<string>(), pk, out)) | 1166 | btf::VerifyDescriptor(ctx, ev["content"].get<string>(), pk, out)) |
| 1080 | { | 1167 | { |
| 1168 | fFound = true; | ||
| 1081 | if (rawOut) | 1169 | if (rawOut) |
| 1082 | *rawOut = ev["content"].get<string>(); | 1170 | *rawOut = ev["content"].get<string>(); |
| 1083 | return true; | ||
| 1084 | } | 1171 | } |
| 1172 | // Deliberately keep reading to EOSE rather than returning here. | ||
| 1173 | // Returning early leaves the rest of the relay's answer queued on a | ||
| 1174 | // socket that goes straight back into the pool, and the next caller | ||
| 1175 | // reads our leftovers as its own -- one message out of step, for | ||
| 1176 | // the life of the process. | ||
| 1085 | } | 1177 | } |
| 1086 | else if (t == "EOSE") | 1178 | else if (t == "EOSE") |
| 1179 | { | ||
| 1180 | fExchangeOk = true; // relay said "that is all": socket is clean | ||
| 1087 | break; | 1181 | break; |
| 1182 | } | ||
| 1088 | } | 1183 | } |
| 1089 | return false; | 1184 | return fFound; |
| 1090 | } | 1185 | } |
| 1091 | 1186 | ||
| 1092 | // Resolve a `.btf` address into its rendezvous coordinates by querying the | 1187 | // Resolve a `.btf` address into its rendezvous coordinates by querying the |
| @@ -1107,12 +1202,22 @@ bool BtfResolve(const std::string& btfAddr, std::string& meetingHostPort, | |||
| 1107 | if (!lease.Connected()) | 1202 | if (!lease.Connected()) |
| 1108 | continue; | 1203 | continue; |
| 1109 | btf::Descriptor d; | 1204 | btf::Descriptor d; |
| 1110 | if (!ResolveDescriptor(lease.Ws(), g_nostrKey.ctx, btfAddr, d)) | 1205 | bool fExchangeOk = false; |
| 1206 | if (!ResolveDescriptor(lease.Ws(), g_nostrKey.ctx, btfAddr, d, fExchangeOk)) | ||
| 1111 | { | 1207 | { |
| 1112 | lease.fOk = true; // exchange completed, just no match | 1208 | // Only keep the pooled socket if the relay actually finished |
| 1209 | // the conversation. This used to say "exchange completed, just | ||
| 1210 | // no match" on every failure -- including a dead socket -- so | ||
| 1211 | // ~CRelayLease never closed it and fConnected was never | ||
| 1212 | // cleared. A relay connection that died was then pinned in the | ||
| 1213 | // pool, reported healthy, and reused for every later lookup, | ||
| 1214 | // which is why "no Nostr descriptor found" persisted for a | ||
| 1215 | // descriptor that was demonstrably published, and why only a | ||
| 1216 | // restart cleared it. | ||
| 1217 | lease.fOk = fExchangeOk; | ||
| 1113 | continue; | 1218 | continue; |
| 1114 | } | 1219 | } |
| 1115 | lease.fOk = true; | 1220 | lease.fOk = fExchangeOk; |
| 1116 | if (d.meeting_node.empty() || d.meeting_node == "rendezvous-pending") | 1221 | if (d.meeting_node.empty() || d.meeting_node == "rendezvous-pending") |
| 1117 | { | 1222 | { |
| 1118 | if (!onionHostPort || d.onion.empty()) | 1223 | if (!onionHostPort || d.onion.empty()) |
| @@ -1360,7 +1465,7 @@ static bool SeedFromRelay(CNostrKey& key, const string& relay) | |||
| 1360 | else if (createdAt > it->second) it->second = createdAt; // refresh | 1465 | else if (createdAt > it->second) it->second = createdAt; // refresh |
| 1361 | } | 1466 | } |
| 1362 | if (fNew && fDebug) | 1467 | if (fNew && fDebug) |
| 1363 | LogPrint("nostr", "Nostr: discovered .btf peer %s\n", peerAddr.c_str()); | 1468 | LogPrint("nostrdiscovery", "Nostr: discovered .btf peer %s\n", peerAddr.c_str()); |
| 1364 | if (++nPeers > 500) break; | 1469 | if (++nPeers > 500) break; |
| 1365 | } | 1470 | } |
| 1366 | else if (type == "EOSE") | 1471 | else if (type == "EOSE") |
| @@ -1450,7 +1555,7 @@ void ThreadBtfPoolAnnouncer(void*) | |||
| 1450 | LogPrint("pool", "ThreadBtfPoolAnnouncer started\n"); | 1555 | LogPrint("pool", "ThreadBtfPoolAnnouncer started\n"); |
| 1451 | while (!fShutdown) | 1556 | while (!fShutdown) |
| 1452 | { | 1557 | { |
| 1453 | if (nMineMode == MINE_OPERATOR && gPoolRunning) | 1558 | if (nMineMode == MINE_OPERATOR && gPoolRunning && !fPoolNoAnnounce) |
| 1454 | { | 1559 | { |
| 1455 | BtfPoolAnnouncement ann; | 1560 | BtfPoolAnnouncement ann; |
| 1456 | ann.btfAddress = BtfLocalAddress(); | 1561 | ann.btfAddress = BtfLocalAddress(); |
| @@ -1785,14 +1890,17 @@ void ThreadNostrSeed(void* parg) | |||
| 1785 | PublishDescriptor(ws, key); | 1890 | PublishDescriptor(ws, key); |
| 1786 | Sleep(700); // let the relay store the replaceable event | 1891 | Sleep(700); // let the relay store the replaceable event |
| 1787 | btf::Descriptor d; | 1892 | btf::Descriptor d; |
| 1788 | if (ResolveDescriptor(ws, key.ctx, key.BtfAddress(), d)) | 1893 | bool fExchangeOk = false; |
| 1894 | if (ResolveDescriptor(ws, key.ctx, key.BtfAddress(), d, fExchangeOk)) | ||
| 1789 | { | 1895 | { |
| 1790 | LogPrint("nostr", "Nostr: .btf self-resolve OK via %s (meeting_node=%s, enc=%s...)\n", | 1896 | LogPrint("nostr", "Nostr: .btf self-resolve OK via %s (meeting_node=%s, enc=%s...)\n", |
| 1791 | pszNostrRelays[i], d.meeting_node.c_str(), d.enc.substr(0, 16).c_str()); | 1897 | pszNostrRelays[i], d.meeting_node.c_str(), d.enc.substr(0, 16).c_str()); |
| 1792 | lease.fOk = true; | 1898 | lease.fOk = fExchangeOk; |
| 1793 | break; | 1899 | break; |
| 1794 | } | 1900 | } |
| 1795 | lease.fOk = true; | 1901 | // Same rule as BtfResolve: a socket only goes back in the pool if |
| 1902 | // the relay finished the conversation on it. | ||
| 1903 | lease.fOk = fExchangeOk; | ||
| 1796 | } | 1904 | } |
| 1797 | CATCH_PRINT_EXCEPTION("btf self-test") | 1905 | CATCH_PRINT_EXCEPTION("btf self-test") |
| 1798 | } | 1906 | } |
| @@ -1824,7 +1932,7 @@ void ThreadNostrSeed(void* parg) | |||
| 1824 | } | 1932 | } |
| 1825 | CATCH_PRINT_EXCEPTION("ConnectDiscoveredBtfPeers") | 1933 | CATCH_PRINT_EXCEPTION("ConnectDiscoveredBtfPeers") |
| 1826 | 1934 | ||
| 1827 | if (nMineMode == MINE_OPERATOR && gPoolRunning) | 1935 | if (nMineMode == MINE_OPERATOR && gPoolRunning && !fPoolNoAnnounce) |
| 1828 | { | 1936 | { |
| 1829 | BtfPoolAnnouncement ann; | 1937 | BtfPoolAnnouncement ann; |
| 1830 | ann.btfAddress = BtfLocalAddress(); | 1938 | ann.btfAddress = BtfLocalAddress(); |
diff --git a/src/nostr.h b/src/nostr.h index dc4288c..0dd919d 100644 --- a/src/nostr.h +++ b/src/nostr.h | |||
| @@ -50,8 +50,19 @@ std::string BtfLocalOnionEndpoint(); | |||
| 50 | // If set (via /announcerelay=host:port), this node announces that relay on | 50 | // If set (via /announcerelay=host:port), this node announces that relay on |
| 51 | // Nostr so other nodes discover it automatically -- no manual seed-list edit. | 51 | // Nostr so other nodes discover it automatically -- no manual seed-list edit. |
| 52 | extern std::string strBtfAnnounceRelay; | 52 | extern std::string strBtfAnnounceRelay; |
| 53 | // True when /rvrelay pinned the meeting relay list: BtfAllRelays then returns | ||
| 54 | // ONLY vBtfMeetingRelays, never Nostr-discovered volunteer relays. Without | ||
| 55 | // this, a pool that pins one public relay still spawns an accept thread per | ||
| 56 | // discovered relay, and each thread's optimistic BtfSetActiveRelay makes the | ||
| 57 | // published descriptor flap to relays the pool may not be reachable at. | ||
| 58 | extern bool fBtfRelayPinned; | ||
| 59 | // Basic sanity check on a "host:port" relay address (numeric port 1..65535). | ||
| 60 | bool LooksLikeRelay(const std::string& r); | ||
| 53 | // Every relay to try: curated seeds + relays discovered from Nostr announcements. | 61 | // Every relay to try: curated seeds + relays discovered from Nostr announcements. |
| 54 | std::vector<std::string> BtfAllRelays(); | 62 | std::vector<std::string> BtfAllRelays(); |
| 63 | // Relays discovered from Nostr, ignoring the pin. For reporting what a pin | ||
| 64 | // excludes -- never for choosing a relay to register or dial at. | ||
| 65 | std::vector<std::string> BtfDiscoveredRelays(); | ||
| 55 | 66 | ||
| 56 | struct BtfPoolAnnouncement | 67 | struct BtfPoolAnnouncement |
| 57 | { | 68 | { |
diff --git a/src/randomx_pow.cpp b/src/randomx_pow.cpp index 4454fd1..a509543 100644 --- a/src/randomx_pow.cpp +++ b/src/randomx_pow.cpp | |||
| @@ -8,6 +8,7 @@ | |||
| 8 | #include <thread> | 8 | #include <thread> |
| 9 | #include <vector> | 9 | #include <vector> |
| 10 | #include <mutex> | 10 | #include <mutex> |
| 11 | #include <system_error> | ||
| 11 | 12 | ||
| 12 | // Fixed key (seed) that determines the RandomX cache/dataset. Keeping it | 13 | // Fixed key (seed) that determines the RandomX cache/dataset. Keeping it |
| 13 | // constant avoids per-epoch "reseed" logic; for the CPU+RAM fairness goal | 14 | // constant avoids per-epoch "reseed" logic; for the CPU+RAM fairness goal |
| @@ -183,13 +184,28 @@ bool RandomXInitDataset(int nThreads) | |||
| 183 | 184 | ||
| 184 | std::vector<std::thread> workers; | 185 | std::vector<std::thread> workers; |
| 185 | unsigned long per = total / nThreads; | 186 | unsigned long per = total / nThreads; |
| 186 | for (int i = 0; i < nThreads; i++) | 187 | try |
| 187 | { | 188 | { |
| 188 | unsigned long start = i * per; | 189 | for (int i = 0; i < nThreads; i++) |
| 189 | unsigned long count = (i == nThreads - 1) ? (total - start) : per; | 190 | { |
| 190 | workers.emplace_back([start, count]() { | 191 | unsigned long start = i * per; |
| 191 | randomx_init_dataset(g_dataset, g_cache, start, count); | 192 | unsigned long count = (i == nThreads - 1) ? (total - start) : per; |
| 192 | }); | 193 | workers.emplace_back([start, count]() { |
| 194 | randomx_init_dataset(g_dataset, g_cache, start, count); | ||
| 195 | }); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | catch (const std::system_error& e) | ||
| 199 | { | ||
| 200 | // Same refusal as the allocation failure above: light mode is slow | ||
| 201 | // but alive, std::terminate is neither. Join the workers that did | ||
| 202 | // start before releasing the dataset out from under them. | ||
| 203 | for (auto& w : workers) w.join(); | ||
| 204 | randomx_release_dataset(g_dataset); | ||
| 205 | g_dataset = NULL; | ||
| 206 | printf("RandomX: cannot spawn dataset init threads (%s), staying in light mode\n", | ||
| 207 | e.what()); | ||
| 208 | return false; | ||
| 193 | } | 209 | } |
| 194 | for (auto& w : workers) w.join(); | 210 | for (auto& w : workers) w.join(); |
| 195 | 211 | ||
diff --git a/src/readme.txt b/src/readme.txt index 810e8b7..455884c 100644 --- a/src/readme.txt +++ b/src/readme.txt | |||
| @@ -16,7 +16,7 @@ This product includes software developed by the OpenSSL Project. | |||
| 16 | Differences from the original Bitcoin | 16 | Differences from the original Bitcoin |
| 17 | ------------------------------------- | 17 | ------------------------------------- |
| 18 | - Own genesis block (22/Jul/2026 headline, independent chain) | 18 | - Own genesis block (22/Jul/2026 headline, independent chain) |
| 19 | - Own magic bytes (0xbf 0x1a 0x5c 0xfd) and P2P port 8433 | 19 | - Own magic bytes (0xbf 0x20 0x5c 0xfd) and P2P port 8433 |
| 20 | - Addresses start with "B" (ADDRESSVERSION = 25) | 20 | - Addresses start with "B" (ADDRESSVERSION = 25) |
| 21 | - Consensus fixes applied: value overflow (CVE-2010-5139), duplicate inputs, | 21 | - Consensus fixes applied: value overflow (CVE-2010-5139), duplicate inputs, |
| 22 | money range checks (MoneyRange) | 22 | money range checks (MoneyRange) |
diff --git a/src/rpc.cpp b/src/rpc.cpp index 813f1d1..3827cd5 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp | |||
| @@ -6,8 +6,8 @@ | |||
| 6 | // Architecture: | 6 | // Architecture: |
| 7 | // PoolEventLoop -- single thread, owns all miner fds, all job/share/payout | 7 | // PoolEventLoop -- single thread, owns all miner fds, all job/share/payout |
| 8 | // state. No locks needed on any of that data. | 8 | // state. No locks needed on any of that data. |
| 9 | // AcceptOneFn -- one thread per relay. Blocks on RvServiceRegister + | 9 | // AcceptOneFn -- one thread per relay. Registers, waits to be paired, |
| 10 | // BtfServiceWrap, pushes finished socket to gReadyQueue. | 10 | // wraps the tunnel, pushes finished socket to gReadyQueue. |
| 11 | // AcceptLoopFn -- spawns AcceptOneFn threads, re-polls relay list every | 11 | // AcceptLoopFn -- spawns AcceptOneFn threads, re-polls relay list every |
| 12 | // 60s so dynamically discovered relays get picked up. | 12 | // 60s so dynamically discovered relays get picked up. |
| 13 | // | 13 | // |
| @@ -31,10 +31,15 @@ | |||
| 31 | #include <netinet/in.h> | 31 | #include <netinet/in.h> |
| 32 | #include <arpa/inet.h> | 32 | #include <arpa/inet.h> |
| 33 | #include <unistd.h> | 33 | #include <unistd.h> |
| 34 | #include <fcntl.h> | ||
| 35 | #include <dirent.h> | ||
| 36 | #else | ||
| 37 | #include <io.h> | ||
| 34 | #endif | 38 | #endif |
| 35 | 39 | ||
| 36 | #include <atomic> | 40 | #include <atomic> |
| 37 | #include <mutex> | 41 | #include <mutex> |
| 42 | #include <thread> | ||
| 38 | #include <map> | 43 | #include <map> |
| 39 | #include <set> | 44 | #include <set> |
| 40 | #include <deque> | 45 | #include <deque> |
| @@ -110,15 +115,28 @@ static uint256 ShareTargetFromDifficulty(double difficulty) | |||
| 110 | { | 115 | { |
| 111 | if (difficulty <= 0.0) difficulty = 1.0; | 116 | if (difficulty <= 0.0) difficulty = 1.0; |
| 112 | static const uint256 diff1 = CBigNum(bnProofOfWorkLimit).getuint256(); | 117 | static const uint256 diff1 = CBigNum(bnProofOfWorkLimit).getuint256(); |
| 118 | // Representability floor. shareTarget = diff1 / difficulty, and diff1 is | ||
| 119 | // ~uint256(0) >> 12, i.e. a hair under 2^244 -- so the true target stops | ||
| 120 | // fitting in a uint256 once difficulty reaches 2^-12 (= 1/4096), and the | ||
| 121 | // fixed-point divide below breaks even earlier in a second way: the | ||
| 122 | // (uint64)(difficulty * 65536.0) divisor truncates to zero for any | ||
| 123 | // difficulty under 2^-16. Both thresholds sit far below VARDIFF_MIN | ||
| 124 | // (0.001), so live operation never comes near this, but a caller that | ||
| 125 | // bypasses vardiff (or a future VARDIFF_MIN) must not divide by zero or | ||
| 126 | // hand back getuint256()'s low-256-bit truncation of an oversized | ||
| 127 | // quotient -- a wrapped target is not merely wrong, it is arbitrarily | ||
| 128 | // HARDER than difficulty 1. At these difficulties every hash is meant to | ||
| 129 | // be a share, so the honest answer is the easiest representable target. | ||
| 130 | // (This replaces a dead branch that read `if (t < diff1 && difficulty < | ||
| 131 | // 1.0)`: t = diff1/difficulty, so difficulty < 1.0 implies t > diff1 and | ||
| 132 | // the two conditions could never both hold.) | ||
| 133 | if (difficulty < 1.0 / 4096.0) return ~uint256(0); | ||
| 113 | // shareTarget = diff1 / difficulty | 134 | // shareTarget = diff1 / difficulty |
| 114 | CBigNum bn; bn.setuint256(diff1); | 135 | CBigNum bn; bn.setuint256(diff1); |
| 115 | // Multiply by 2^16 to keep precision, divide, shift back | 136 | // Multiply by 2^16 to keep precision, divide, shift back |
| 116 | bn <<= 16; | 137 | bn <<= 16; |
| 117 | bn /= (uint64)(difficulty * 65536.0); | 138 | bn /= (uint64)(difficulty * 65536.0); |
| 118 | uint256 t = bn.getuint256(); | 139 | return bn.getuint256(); |
| 119 | // Cap at all-ones (maximum, easiest possible) | ||
| 120 | if (t < diff1 && difficulty < 1.0) t = ~uint256(0); | ||
| 121 | return t; | ||
| 122 | } | 140 | } |
| 123 | 141 | ||
| 124 | // Expected number of hashes to find one share at a given Stratum difficulty, | 142 | // Expected number of hashes to find one share at a given Stratum difficulty, |
| @@ -143,12 +161,104 @@ static bool RebuildJob() | |||
| 143 | return false; | 161 | return false; |
| 144 | } | 162 | } |
| 145 | 163 | ||
| 164 | // Don't issue jobs while we are behind the network: mining against a | ||
| 165 | // stale tip produces blocks the network will orphan, and payout | ||
| 166 | // entries that can never be paid (their coinbases never exist on the | ||
| 167 | // main chain). Require at least one peer, and require that no peer | ||
| 168 | // claims a longer chain than ours. | ||
| 169 | if (vNodes.empty()) { | ||
| 170 | LogPrint("pool", "RebuildJob: no peers yet -- not issuing jobs\n"); | ||
| 171 | return false; | ||
| 172 | } | ||
| 173 | // This used to compare the tip's timestamp against 30 minutes, which | ||
| 174 | // was wrong in both directions and cost real money. | ||
| 175 | // | ||
| 176 | // It cannot tell "we are not synced" from "the network is slow". A | ||
| 177 | // pool mining its own fork keeps its tip timestamp fresh forever, so | ||
| 178 | // the check passed for the whole of heights 3966-4085 -- 120 | ||
| 179 | // consecutive blocks, every one of them orphaned, 5890.50 BTF of | ||
| 180 | // payouts voided. Then, once the pool stopped finding blocks, the | ||
| 181 | // same check refused to issue jobs against a tip that was perfectly | ||
| 182 | // valid and was the network's own best, and the pool -- roughly 90% | ||
| 183 | // of the hashrate -- could not restart the chain it was the majority | ||
| 184 | // of. Stale tip, so no jobs; no jobs, so the tip stays stale. | ||
| 185 | // | ||
| 186 | // The height peers announce in their version message answers the | ||
| 187 | // question the age check was only guessing at. Median, so one peer | ||
| 188 | // claiming an absurd height cannot stall us, and one block of slack | ||
| 189 | // to match WarnIfBehind, because somebody is always mid-relay. A | ||
| 190 | // peer that predates the announcement reports -1 and is ignored; if | ||
| 191 | // nobody has told us anything we mine our best chain, which is what | ||
| 192 | // the node would do without a pool attached. | ||
| 193 | int nPeerHeight = GetPeerMedianHeight(); | ||
| 194 | if (nPeerHeight >= 0 && nBestHeight < nPeerHeight - 1) { | ||
| 195 | LogPrint("pool", "RebuildJob: %d blocks behind the network " | ||
| 196 | "(height %d, peers report %d) -- not issuing jobs\n", | ||
| 197 | nPeerHeight - nBestHeight, nBestHeight, nPeerHeight); | ||
| 198 | return false; | ||
| 199 | } | ||
| 200 | |||
| 201 | // The comparison above is blind in the one case that matters most. | ||
| 202 | // | ||
| 203 | // When the node goes deaf -- peers still in vNodes, nothing arriving -- | ||
| 204 | // no peer announces anything, GetPeerMedianHeight() returns -1, and the | ||
| 205 | // fallback ("nobody has told us anything, so mine our best chain") let | ||
| 206 | // the pool build a private fork for as long as the deafness lasted. | ||
| 207 | // That fallback is right for a node that has simply not been told yet | ||
| 208 | // and wrong for a node that has stopped being able to hear, and a | ||
| 209 | // height nobody sent cannot tell those apart. Time can. | ||
| 210 | // | ||
| 211 | // Deliberately not behind a debug category, and deliberately not a | ||
| 212 | // silent refusal: an operator whose pool has stopped issuing jobs needs | ||
| 213 | // the reason in the log. /poolmineblind is the escape hatch for a node | ||
| 214 | // that really is alone -- without one, a genuinely isolated pool that is | ||
| 215 | // the majority of the hashrate could never restart its own chain, which | ||
| 216 | // is the deadlock the tip-age check used to cause. | ||
| 217 | if (nPeerHeight < 0 && !fPoolMineBlind) { | ||
| 218 | int64 nAge = GetPeerHeightAgeSecs(); | ||
| 219 | if (nAge >= BTF_NO_PEER_HEIGHT_MAX_SECS) { | ||
| 220 | static int64 nLastSaid = 0; | ||
| 221 | if (GetTime() - nLastSaid > 60) { | ||
| 222 | nLastSaid = GetTime(); | ||
| 223 | printf("RebuildJob: no peer has reported a height for %d minutes " | ||
| 224 | "-- not issuing jobs (our height %d, %d peers connected). " | ||
| 225 | "This node is deaf; mining now would fork it. " | ||
| 226 | "Pass /poolmineblind to override.\n", | ||
| 227 | (int)(nAge / 60), nBestHeight, (int)vNodes.size()); | ||
| 228 | } | ||
| 229 | return false; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 146 | CBlockIndex* pindexPrev = pindexBest; | 233 | CBlockIndex* pindexPrev = pindexBest; |
| 147 | unsigned int nBits = GetNextWorkRequired(pindexPrev); | 234 | unsigned int nBits = GetNextWorkRequired(pindexPrev); |
| 148 | 235 | ||
| 149 | // From the pool, so the template's payout key is in wallet.dat before | 236 | // From the pool, so the template's payout key is in wallet.dat before |
| 150 | // any miner is handed work against it. | 237 | // any miner is handed work against it. |
| 151 | vector<unsigned char> vchPubKey = GetKeyFromPool(); | 238 | vector<unsigned char> vchPubKey = GetKeyFromPool(); |
| 239 | if (vchPubKey.empty()) { | ||
| 240 | // Empty means the wallet could not produce a key: encrypted and | ||
| 241 | // locked, or key generation itself failed. Serialised, an empty | ||
| 242 | // vector is a zero-length push, so the coinbase would become | ||
| 243 | // `<empty> OP_CHECKSIG` -- a script nobody can satisfy. Every | ||
| 244 | // block mined against such a job burns its whole subsidy, while | ||
| 245 | // QueuePayouts still records the full obligation and the miners | ||
| 246 | // are then paid out of coin that block never contributed. | ||
| 247 | // BitcoinMiner refuses to mine in this state; refuse to hand the | ||
| 248 | // pool's miners work in it too. printf and not LogPrint for the | ||
| 249 | // reason given above the deaf-node refusal, and throttled the | ||
| 250 | // same way -- the no-job watchdog retries every few seconds for | ||
| 251 | // as long as the condition lasts. | ||
| 252 | static int64 nLastSaidNoKey = 0; | ||
| 253 | if (GetTime() - nLastSaidNoKey > 60) { | ||
| 254 | nLastSaidNoKey = GetTime(); | ||
| 255 | printf("RebuildJob: no wallet key available for the coinbase " | ||
| 256 | "(wallet locked, or key generation failed -- the " | ||
| 257 | "GetKeyFromPool line above says which) -- not issuing " | ||
| 258 | "jobs\n"); | ||
| 259 | } | ||
| 260 | return false; | ||
| 261 | } | ||
| 152 | static std::atomic<uint32_t> sExtra{0}; | 262 | static std::atomic<uint32_t> sExtra{0}; |
| 153 | uint32_t extraNonce = ++sExtra; | 263 | uint32_t extraNonce = ++sExtra; |
| 154 | 264 | ||
| @@ -193,8 +303,29 @@ static bool RebuildJob() | |||
| 193 | map<uint256, CTxIndex> pool; | 303 | map<uint256, CTxIndex> pool; |
| 194 | vector<char> vfAlreadyAdded(mapTransactions.size()); | 304 | vector<char> vfAlreadyAdded(mapTransactions.size()); |
| 195 | bool fFoundSomething = true; | 305 | bool fFoundSomething = true; |
| 196 | unsigned int sz = 0; | 306 | // The cap is MAX_BLOCK_SIZE_GEN, not MAX_SIZE/2. MAX_SIZE is |
| 197 | while (fFoundSomething && sz < MAX_SIZE / 2) | 307 | // the 32 MB serialisation ceiling, so half of it is 16 MB -- |
| 308 | // this builder would happily assemble a template that its own | ||
| 309 | // CheckBlock() rejects at the 1 MB consensus cap. This is the | ||
| 310 | // pool's job builder, so a block over the cap is not one | ||
| 311 | // wasted solve, it is every miner on the pool working a | ||
| 312 | // template the network will reject. | ||
| 313 | // | ||
| 314 | // The inner break used to bound the overshoot to one | ||
| 315 | // transaction per pass. One transaction is still too many: | ||
| 316 | // nothing caps how big a single transaction may be, so that | ||
| 317 | // bound was worth up to MAX_SIZE. Asking whether each one fits | ||
| 318 | // before appending it bounds the overshoot at zero instead. | ||
| 319 | // | ||
| 320 | // Seeded with the header, tx count and coinbase, because | ||
| 321 | // CheckBlock() weighs the whole serialized block. | ||
| 322 | unsigned int sz = ::GetSerializeSize(block, SER_DISK); | ||
| 323 | // And the sigop budget, which CheckBlock() enforces and this | ||
| 324 | // builder never counted against. Same counting function | ||
| 325 | // CheckBlock() sums, seeded from the coinbase because it sums | ||
| 326 | // over all of vtx. | ||
| 327 | unsigned int nSigOps = block.vtx[0].GetSigOpCount(); | ||
| 328 | while (fFoundSomething && sz < MAX_BLOCK_SIZE_GEN) | ||
| 198 | { | 329 | { |
| 199 | fFoundSomething = false; | 330 | fFoundSomething = false; |
| 200 | unsigned int n = 0; | 331 | unsigned int n = 0; |
| @@ -205,13 +336,24 @@ static bool RebuildJob() | |||
| 205 | continue; | 336 | continue; |
| 206 | CTransaction& tx = (*mi).second; | 337 | CTransaction& tx = (*mi).second; |
| 207 | if (tx.IsCoinBase() || !tx.IsFinal()) continue; | 338 | if (tx.IsCoinBase() || !tx.IsFinal()) continue; |
| 339 | // Skip rather than stop: one transaction too big for | ||
| 340 | // the room left does not mean the next is. Ordered | ||
| 341 | // before ConnectInputs because ConnectInputs credits | ||
| 342 | // nFees by reference on success, and a transaction we | ||
| 343 | // then decline would leave the coinbase claiming a fee | ||
| 344 | // this block does not collect. | ||
| 345 | unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK); | ||
| 346 | unsigned int nTxSigOps = tx.GetSigOpCount(); | ||
| 347 | if (!BlockTemplateFits(sz, nSigOps, nTxSize, nTxSigOps)) | ||
| 348 | continue; | ||
| 208 | map<uint256, CTxIndex> tmp(pool); | 349 | map<uint256, CTxIndex> tmp(pool); |
| 209 | if (!tx.ConnectInputs(txdb, tmp, CDiskTxPos(1,1,1), 0, nFees, | 350 | if (!tx.ConnectInputs(txdb, tmp, CDiskTxPos(1,1,1), 0, nFees, |
| 210 | false, true, tx.GetMinFee(block.vtx.size() < 100))) | 351 | false, true, tx.GetMinFee(block.vtx.size() < 100))) |
| 211 | continue; | 352 | continue; |
| 212 | swap(pool, tmp); | 353 | swap(pool, tmp); |
| 213 | block.vtx.push_back(tx); | 354 | block.vtx.push_back(tx); |
| 214 | sz += ::GetSerializeSize(tx, SER_NETWORK); | 355 | sz += nTxSize; |
| 356 | nSigOps += nTxSigOps; | ||
| 215 | vfAlreadyAdded[n] = true; | 357 | vfAlreadyAdded[n] = true; |
| 216 | fFoundSomething = true; | 358 | fFoundSomething = true; |
| 217 | } | 359 | } |
| @@ -251,17 +393,45 @@ static bool RebuildJob() | |||
| 251 | // Payouts -- event loop thread only | 393 | // Payouts -- event loop thread only |
| 252 | // --------------------------------------------------------------------------- | 394 | // --------------------------------------------------------------------------- |
| 253 | struct PendingPayout { | 395 | struct PendingPayout { |
| 254 | std::string id; | 396 | // The block that funds this payout, identified by hash and not by height |
| 255 | int blockHeight; | 397 | // alone. A reorg replaces the block at a height without changing the |
| 256 | int matureAtHeight; | 398 | // height, so a height-keyed obligation outlives the coinbase that was |
| 399 | // supposed to pay it: a 280-block reorg once left ~14.4k BTF of debt | ||
| 400 | // owed against blocks that no longer existed, which then drained the | ||
| 401 | // wallet paying fork-era miners out of real-chain coin. | ||
| 402 | // | ||
| 403 | // Upstream keys these on the string "block-<height>". That is the same | ||
| 404 | // bug this fork already paid for, and height keying additionally lets | ||
| 405 | // FindRecordedPoolPayout match the *previous* round's transaction after a | ||
| 406 | // reorg and silently skip a miner, so their `id` is deliberately dropped. | ||
| 407 | int blockHeight = 0; | ||
| 408 | // Must be explicitly zeroed: uint256's default constructor is empty | ||
| 409 | // (uint256.h:514), so leaving this unassigned gives every ledger entry | ||
| 410 | // loaded from an older file a garbage hash, which then matches no block | ||
| 411 | // and gets voided as "orphaned". That mistake voided a live payout. | ||
| 412 | uint256 blockHash = 0; // zero = entry predates hash keying | ||
| 413 | int matureAtHeight = 0; | ||
| 257 | std::map<std::string, int64> amounts; // address -> satoshis | 414 | std::map<std::string, int64> amounts; // address -> satoshis |
| 415 | // From upstream: which recipients already have a transaction. This is what | ||
| 416 | // lets an entry stay on disk until every address in `amounts` is paid, | ||
| 417 | // instead of being erased the moment the batch is handed off. | ||
| 258 | std::map<std::string, std::string> paidTxids; // address -> txid | 418 | std::map<std::string, std::string> paidTxids; // address -> txid |
| 259 | int64 nextAttempt; | 419 | // Retry and backoff state does NOT live here. It used to be declared here |
| 260 | 420 | // as well as on PayingRecord, and the copy on this side was never written: | |
| 261 | PendingPayout() : blockHeight(0), matureAtHeight(0), nextAttempt(0) {} | 421 | // `retryAfter` was declared, serialised, loaded and tested at the flush |
| 422 | // gate, and the only assignment to it anywhere was `= 0`, so the gate could | ||
| 423 | // not fire. `retries` and `deferrals` were the same story. The real state | ||
| 424 | // is per (round, recipient) -- which is the granularity a payment can fail | ||
| 425 | // at -- and it lives on PayingRecord, which is now what persists it. | ||
| 262 | }; | 426 | }; |
| 263 | static std::vector<PendingPayout> gPendingPayouts; | 427 | static std::vector<PendingPayout> gPendingPayouts; |
| 264 | static std::mutex gPayoutLedgerMutex; | 428 | static std::mutex gPayoutLedgerMutex; |
| 429 | // Bumped on every serialisation, under gPayoutLedgerMutex. See PublishVersioned. | ||
| 430 | static uint64 gPayoutLedgerSeq = 0; | ||
| 431 | // Serialises writers of pending_payouts.json, and guards the sequence number | ||
| 432 | // of the image currently on disk. Never taken while a data mutex is held. | ||
| 433 | static std::mutex gPayoutLedgerFileMutex; | ||
| 434 | static uint64 gPayoutLedgerPublishedSeq = 0; | ||
| 265 | 435 | ||
| 266 | struct PoolRoundProof { | 436 | struct PoolRoundProof { |
| 267 | std::string id; | 437 | std::string id; |
| @@ -282,6 +452,12 @@ struct PoolRoundProof { | |||
| 282 | std::map<std::string, uint64> shares; | 452 | std::map<std::string, uint64> shares; |
| 283 | std::map<std::string, int64> payouts; | 453 | std::map<std::string, int64> payouts; |
| 284 | std::map<std::string, std::string> paidTxids; | 454 | std::map<std::string, std::string> paidTxids; |
| 455 | // Recipients the ledger has written off, and why. Without this the public | ||
| 456 | // round proof goes on advertising an address as owed with "txid": null | ||
| 457 | // forever, while the ledger has already dropped the obligation -- the two | ||
| 458 | // records of the same round permanently disagreeing, with the operator | ||
| 459 | // holding the money. | ||
| 460 | std::map<std::string, std::string> forfeited; // address -> reason | ||
| 285 | 461 | ||
| 286 | PoolRoundProof() | 462 | PoolRoundProof() |
| 287 | : blockHeight(0), matureAtHeight(0), createdAt(0), feePercent(0.0), | 463 | : blockHeight(0), matureAtHeight(0), createdAt(0), feePercent(0.0), |
| @@ -290,6 +466,9 @@ struct PoolRoundProof { | |||
| 290 | }; | 466 | }; |
| 291 | static std::vector<PoolRoundProof> gPoolRounds; | 467 | static std::vector<PoolRoundProof> gPoolRounds; |
| 292 | static std::mutex gPoolRoundsMutex; | 468 | static std::mutex gPoolRoundsMutex; |
| 469 | static uint64 gPoolRoundsSeq = 0; // guarded by gPoolRoundsMutex | ||
| 470 | static std::mutex gPoolRoundsFileMutex; | ||
| 471 | static uint64 gPoolRoundsPublishedSeq = 0; // guarded by gPoolRoundsFileMutex | ||
| 293 | static const size_t MAX_POOL_ROUNDS = 200; | 472 | static const size_t MAX_POOL_ROUNDS = 200; |
| 294 | 473 | ||
| 295 | static std::string PayoutFilePath() | 474 | static std::string PayoutFilePath() |
| @@ -304,9 +483,302 @@ static std::string PoolRoundsFilePath() | |||
| 304 | return GetAppDir() + "/pool_rounds.json"; | 483 | return GetAppDir() + "/pool_rounds.json"; |
| 305 | } | 484 | } |
| 306 | 485 | ||
| 307 | static std::string PayoutIdForBlock(int blockHeight) | 486 | // --------------------------------------------------------------------------- |
| 487 | // Atomic, ordered publication of the payout state files | ||
| 488 | // --------------------------------------------------------------------------- | ||
| 489 | // | ||
| 490 | // Every one of these files is a full dump of an in-memory structure, written | ||
| 491 | // tmp-then-rename so a crash cannot truncate it. Two things have to be true | ||
| 492 | // for that to actually be safe, and only the first was: | ||
| 493 | // | ||
| 494 | // 1. the rename must be atomic (it is), and | ||
| 495 | // 2. exactly one thread at a time may be writing the temp file, and a write | ||
| 496 | // must never publish an OLDER image over a newer one. | ||
| 497 | // | ||
| 498 | // (2) was broken for pending_payouts.json. The event loop wrote it with | ||
| 499 | // gPayoutLedgerMutex held, the payout thread wrote it with the mutex | ||
| 500 | // released, and nothing serialised the two: both opened the same | ||
| 501 | // "<path>.tmp" with independent handles and both renamed it. The interleaved | ||
| 502 | // file that lands is either a mixture of two images (LoadPendingPayouts then | ||
| 503 | // throws mid-loop and comes up with a nearly empty ledger -- every obligation | ||
| 504 | // not yet exploded, gone) or a whole newly-queued round silently overwritten | ||
| 505 | // by an older snapshot that predates it. | ||
| 506 | // | ||
| 507 | // The fix is a per-file write mutex, held only across the I/O and never | ||
| 508 | // across a data-structure mutex, plus a version on the snapshot. Serialising | ||
| 509 | // happens under the data mutex and stamps a monotonically increasing sequence | ||
| 510 | // number; publishing happens with the data mutex released and refuses any | ||
| 511 | // blob whose sequence number is not newer than what is already on disk. So | ||
| 512 | // writers can queue in any order and finish in any order -- the file only | ||
| 513 | // ever moves forwards. | ||
| 514 | // | ||
| 515 | // The other half of the rule is that no thread waits for I/O while holding | ||
| 516 | // gPayoutLedgerMutex, gPayoutExecMutex or gPoolRoundsMutex, which is the | ||
| 517 | // contention the merge was right to remove from the event loop. That was | ||
| 518 | // claimed here before it was true: pool_status.json is written by the same | ||
| 519 | // WriteFileAtomic and RefreshStats held gPayoutLedgerMutex at function scope | ||
| 520 | // across the call, so once the writes gained fsync() the event loop was doing | ||
| 521 | // two of them every ten seconds with the payout thread's mutex held -- worse | ||
| 522 | // than the buffered write the merge replaced. The status file now goes through | ||
| 523 | // the same serialise-under-the-lock / publish-outside-it split as the ledgers, | ||
| 524 | // and the rule holds for all four files. It is checked, not asserted: see the | ||
| 525 | // "no payout mutex is held across a publish" section of the payout-ledger | ||
| 526 | // self-test. | ||
| 527 | |||
| 528 | // A serialised file image plus the version of the in-memory state it was | ||
| 529 | // taken from. | ||
| 530 | struct VersionedBlob { | ||
| 531 | std::string blob; | ||
| 532 | uint64 seq = 0; | ||
| 533 | }; | ||
| 534 | |||
| 535 | // Push a file's contents out of the C library's buffer AND out of the kernel's | ||
| 536 | // page cache, so the bytes exist on the device before anything points at them. | ||
| 537 | static bool SyncFileHandle(FILE* f) | ||
| 538 | { | ||
| 539 | if (fflush(f) != 0) | ||
| 540 | return false; | ||
| 541 | #ifdef _WIN32 | ||
| 542 | return _commit(_fileno(f)) == 0; | ||
| 543 | #else | ||
| 544 | return fsync(fileno(f)) == 0; | ||
| 545 | #endif | ||
| 546 | } | ||
| 547 | |||
| 548 | // Make the directory entry that rename() created durable too. | ||
| 549 | // | ||
| 550 | // A tmp+rename is only atomic with respect to a crash of the process. Against | ||
| 551 | // a power cut or a hard VM reset the rename can reach the platter while the | ||
| 552 | // data it points at has not, and the file that comes back is short or empty. | ||
| 553 | // For pending_payouts.json that is not a corrupt file, it is a silent one: | ||
| 554 | // LoadPendingPayouts catches the parse error, logs a single warning, and the | ||
| 555 | // node comes up owing nobody anything. | ||
| 556 | // | ||
| 557 | // Best effort -- a filesystem that does not allow the directory to be opened | ||
| 558 | // for this is not a reason to refuse to write the ledger. | ||
| 559 | static void SyncDirectoryOf(const std::string& path) | ||
| 308 | { | 560 | { |
| 309 | return strprintf("block-%d", blockHeight); | 561 | #ifndef _WIN32 |
| 562 | size_t slash = path.find_last_of('/'); | ||
| 563 | std::string dir = (slash == std::string::npos) ? std::string(".") | ||
| 564 | : path.substr(0, slash); | ||
| 565 | if (dir.empty()) | ||
| 566 | dir = "/"; | ||
| 567 | int fd = open(dir.c_str(), O_RDONLY); | ||
| 568 | if (fd < 0) | ||
| 569 | return; | ||
| 570 | fsync(fd); | ||
| 571 | close(fd); | ||
| 572 | #endif | ||
| 573 | } | ||
| 574 | |||
| 575 | // Replace `path` with `data` atomically and durably. Caller must hold the | ||
| 576 | // write mutex for `path`: the temp name is derived from the target, so two | ||
| 577 | // threads in here at once for one path is precisely the race this exists to | ||
| 578 | // prevent. | ||
| 579 | // | ||
| 580 | // Every failure removes the temp file before returning. It used to be left | ||
| 581 | // where it fell, so a disk that filled up once left a short, valid-looking | ||
| 582 | // "pending_payouts.json.tmp" next to the good ledger for as long as the node | ||
| 583 | // ran. That file is a trap: it has the name of a half-finished save, and an | ||
| 584 | // operator cleaning up after an incident is exactly the person who will move | ||
| 585 | // it into place. | ||
| 586 | // | ||
| 587 | // Failures print unconditionally rather than through LogPrint(). These three | ||
| 588 | // files are written under two different debug categories ("payout" for the | ||
| 589 | // ledgers, "pool" for the status file), and a write failure is not a trace of | ||
| 590 | // normal operation -- it is the file the operator is about to read not being | ||
| 591 | // there. Gating it behind a category that had to be chosen before the failure | ||
| 592 | // happened is how "could not replace pool_status.json" went missing. | ||
| 593 | static bool WriteFileAtomic(const std::string& path, const std::string& data, | ||
| 594 | const char* what) | ||
| 595 | { | ||
| 596 | std::string tmp = path + ".tmp"; | ||
| 597 | FILE* f = fopen(tmp.c_str(), "w"); | ||
| 598 | if (!f) { | ||
| 599 | printf("[payout] WARNING: could not write %s (%s)\n", | ||
| 600 | tmp.c_str(), what); | ||
| 601 | return false; | ||
| 602 | } | ||
| 603 | if (!data.empty() && fwrite(data.c_str(), 1, data.size(), f) != data.size()) { | ||
| 604 | printf("[payout] WARNING: short write to %s (%s)\n", | ||
| 605 | tmp.c_str(), what); | ||
| 606 | fclose(f); | ||
| 607 | remove(tmp.c_str()); | ||
| 608 | return false; | ||
| 609 | } | ||
| 610 | // Before the rename, not after: publishing a name that points at nothing | ||
| 611 | // is the failure this ordering exists to prevent. If the data cannot be | ||
| 612 | // made durable, do not rename -- the previous file is still intact and | ||
| 613 | // still correct, and the next write will try again. | ||
| 614 | if (!SyncFileHandle(f)) { | ||
| 615 | printf("[payout] WARNING: could not flush %s to disk (%s)" | ||
| 616 | " -- keeping the previous file\n", tmp.c_str(), what); | ||
| 617 | fclose(f); | ||
| 618 | remove(tmp.c_str()); | ||
| 619 | return false; | ||
| 620 | } | ||
| 621 | // fclose() is where buffered data actually reaches the OS, so a failure | ||
| 622 | // here means the .tmp is short -- renaming it over the real file would | ||
| 623 | // destroy the good copy, which is exactly what these files exist to | ||
| 624 | // prevent. | ||
| 625 | if (fclose(f) != 0) { | ||
| 626 | printf("[payout] WARNING: could not close %s (%s)\n", | ||
| 627 | tmp.c_str(), what); | ||
| 628 | remove(tmp.c_str()); | ||
| 629 | return false; | ||
| 630 | } | ||
| 631 | #ifdef _WIN32 | ||
| 632 | // rename() on Windows will not replace an existing file. | ||
| 633 | remove(path.c_str()); | ||
| 634 | #endif | ||
| 635 | if (rename(tmp.c_str(), path.c_str()) != 0) { | ||
| 636 | printf("[payout] WARNING: could not replace %s (%s)\n", | ||
| 637 | path.c_str(), what); | ||
| 638 | remove(tmp.c_str()); | ||
| 639 | return false; | ||
| 640 | } | ||
| 641 | SyncDirectoryOf(path); | ||
| 642 | return true; | ||
| 643 | } | ||
| 644 | |||
| 645 | // Paths whose loader threw part-way through, and which must therefore never be | ||
| 646 | // written again by this process. | ||
| 647 | // | ||
| 648 | // All three loaders push entries into the live structure INSIDE the try, so an | ||
| 649 | // exception half way down a file leaves a PARTIAL ledger in memory -- the | ||
| 650 | // entries before the bad one, and nothing after it. The first publish after | ||
| 651 | // that then writes the partial state over the file, and the only record of | ||
| 652 | // what was owed is gone. ReconcilePayouts publishes unconditionally at | ||
| 653 | // startup, so "the first publish" is a few milliseconds later, before an | ||
| 654 | // operator could possibly intervene. a8e2f58 named this failure mode in its | ||
| 655 | // own commit message and then only fixed the write side. | ||
| 656 | // | ||
| 657 | // The file is renamed out of the way instead of being left in place, so the | ||
| 658 | // evidence survives even if the operator restarts into the same fault, and so | ||
| 659 | // a node that comes up owing nobody anything is visibly missing its ledger | ||
| 660 | // rather than quietly appearing to have none. | ||
| 661 | static std::set<std::string> gRefusedFiles; | ||
| 662 | static std::mutex gRefusedFilesMutex; | ||
| 663 | |||
| 664 | static bool FileIsRefused(const std::string& path) | ||
| 665 | { | ||
| 666 | std::lock_guard<std::mutex> lk(gRefusedFilesMutex); | ||
| 667 | return gRefusedFiles.count(path) > 0; | ||
| 668 | } | ||
| 669 | |||
| 670 | // Quarantine `path` and refuse to write it for the rest of the process. | ||
| 671 | // | ||
| 672 | // Prints unconditionally: an operator who has just lost a payout ledger must | ||
| 673 | // be told at whatever log settings they were already running with, and the | ||
| 674 | // name it was moved to is the only way back to the data. | ||
| 675 | static void RefuseFileAfterLoadFailure(const std::string& path, const char* what, | ||
| 676 | const char* why) | ||
| 677 | { | ||
| 678 | { | ||
| 679 | std::lock_guard<std::mutex> lk(gRefusedFilesMutex); | ||
| 680 | gRefusedFiles.insert(path); | ||
| 681 | } | ||
| 682 | std::string bad = strprintf("%s.bad.%lld", path.c_str(), (long long)GetTime()); | ||
| 683 | bool moved = (rename(path.c_str(), bad.c_str()) == 0); | ||
| 684 | printf("[payout] ERROR: %s (%s) did not parse: %s\n" | ||
| 685 | "[payout] ERROR: what loaded before the error is a PARTIAL picture," | ||
| 686 | " so this file will not be written again by this process --" | ||
| 687 | " restart once the cause is fixed\n" | ||
| 688 | "[payout] ERROR: the file %s\n", | ||
| 689 | path.c_str(), what, why, | ||
| 690 | moved ? strprintf("has been kept as %s", bad.c_str()).c_str() | ||
| 691 | : "could NOT be renamed out of the way -- copy it now"); | ||
| 692 | } | ||
| 693 | |||
| 694 | // Publish a versioned image of some in-memory state to `path`. | ||
| 695 | // | ||
| 696 | // `fileMutex` serialises writers of `path` and guards `publishedSeq`. A blob | ||
| 697 | // older than what is already published is dropped: these files are whole-state | ||
| 698 | // dumps, so a higher sequence number is by construction a later state of the | ||
| 699 | // same structure and publishing it loses nothing. On a write failure | ||
| 700 | // `publishedSeq` is deliberately left alone, so the next (newer) write still | ||
| 701 | // gets a chance to land. | ||
| 702 | static void PublishVersioned(const std::string& path, const VersionedBlob& v, | ||
| 703 | std::mutex& fileMutex, uint64& publishedSeq, | ||
| 704 | const char* what) | ||
| 705 | { | ||
| 706 | // A default-constructed blob is not a snapshot of anything; it is a | ||
| 707 | // VersionedBlob nobody filled in. Today they are already dropped, because | ||
| 708 | // every serialiser stamps ++gSeq and so never produces 0, and 0 fails the | ||
| 709 | // seq <= publishedSeq test below -- but only for as long as publishedSeq | ||
| 710 | // starts at 0 and nobody writes `seq = gSeq++`. One character, and the | ||
| 711 | // ledger file becomes "[]". Say it out loud instead of relying on it. | ||
| 712 | if (!v.seq) | ||
| 713 | return; | ||
| 714 | // A file whose loader threw is not a file this process knows the contents | ||
| 715 | // of. Writing it would replace a full record of what was owed with | ||
| 716 | // whatever happened to parse before the error. | ||
| 717 | if (FileIsRefused(path)) | ||
| 718 | return; | ||
| 719 | std::lock_guard<std::mutex> lk(fileMutex); | ||
| 720 | if (v.seq <= publishedSeq) | ||
| 721 | return; | ||
| 722 | if (!WriteFileAtomic(path, v.blob, what)) | ||
| 723 | return; | ||
| 724 | publishedSeq = v.seq; | ||
| 725 | } | ||
| 726 | |||
| 727 | // Identity of a payout round. Used as the pool-round proof id and, more | ||
| 728 | // importantly, as the `pool_payout_id` written into wallet transaction | ||
| 729 | // metadata and matched by FindRecordedPoolPayout(). | ||
| 730 | // | ||
| 731 | // Derived from the block HASH, not from its height alone. Upstream uses | ||
| 732 | // "block-<height>", which names two different blocks identically across a | ||
| 733 | // reorg -- and because this id is what the crash-recovery probe matches on, a | ||
| 734 | // height-keyed id lets that probe find the *previous* round's transaction and | ||
| 735 | // conclude a miner has already been paid when they have not. The height stays | ||
| 736 | // in the string for readability only. | ||
| 737 | // | ||
| 738 | // A zero hash means the entry predates hash keying; those keep the old form so | ||
| 739 | // anything already recorded against them still matches. | ||
| 740 | static std::string PayoutIdForBlock(int blockHeight, const uint256& blockHash) | ||
| 741 | { | ||
| 742 | if (blockHash == 0) | ||
| 743 | return strprintf("block-%d", blockHeight); | ||
| 744 | return strprintf("block-%d-%s", blockHeight, | ||
| 745 | blockHash.ToString().substr(0, 16).c_str()); | ||
| 746 | } | ||
| 747 | |||
| 748 | // Do these two references name the same payout round? | ||
| 749 | // | ||
| 750 | // The block hash alone does not decide it, and every ledger lookup used to | ||
| 751 | // assume it did. Entries grandfathered in from a file written before payouts | ||
| 752 | // were keyed by hash all carry hash 0, so under a hash-only comparison the | ||
| 753 | // first zero-hash entry answers for every other one: RecordPayoutTxid writes | ||
| 754 | // a txid against the wrong round (which is then erased as "fully paid" while | ||
| 755 | // the real debt still stands), and ReconcilePayouts drops a live in-flight | ||
| 756 | // record because a *different* zero-hash round happened to pay that address. | ||
| 757 | // | ||
| 758 | // A non-zero hash is unique and decides on its own -- that is the whole point | ||
| 759 | // of keying on it. A zero hash falls back to the height, which | ||
| 760 | // LoadPendingPayouts supplies for any entry that can name its own maturity. | ||
| 761 | // Mixed hash-ness never matches: refusing to match is the safe direction for | ||
| 762 | // every caller here, because each of them treats a match as "this debt is | ||
| 763 | // already dealt with". | ||
| 764 | static bool SamePayoutRound(int aHeight, const uint256& aHash, | ||
| 765 | int bHeight, const uint256& bHash) | ||
| 766 | { | ||
| 767 | if (aHash != bHash) | ||
| 768 | return false; | ||
| 769 | if (aHash != 0) | ||
| 770 | return true; | ||
| 771 | return aHeight == bHeight; | ||
| 772 | } | ||
| 773 | |||
| 774 | // An entry with neither a hash nor a height cannot be told apart from any | ||
| 775 | // other entry in the same state, so nothing may be recorded against it: a | ||
| 776 | // txid written to the wrong one reports a debt as settled that is still owed. | ||
| 777 | // Such an entry is never paid and is logged instead -- refusing to act is the | ||
| 778 | // safe failure, and no file this tree has ever written can produce one. | ||
| 779 | static bool PayoutRoundIdentifiable(int height, const uint256& hash) | ||
| 780 | { | ||
| 781 | return hash != 0 || height != 0; | ||
| 310 | } | 782 | } |
| 311 | 783 | ||
| 312 | static bool PayoutFullyPaid(const PendingPayout& pp) | 784 | static bool PayoutFullyPaid(const PendingPayout& pp) |
| @@ -317,15 +789,28 @@ static bool PayoutFullyPaid(const PendingPayout& pp) | |||
| 317 | return true; | 789 | return true; |
| 318 | } | 790 | } |
| 319 | 791 | ||
| 320 | static void SavePendingPayoutsLocked() | 792 | // Serialise the ledger and stamp it with the version it was taken at. Caller |
| 793 | // must hold gPayoutLedgerMutex. | ||
| 794 | // | ||
| 795 | // Split from the file write so no thread holds the ledger mutex across the | ||
| 796 | // I/O. Upstream writes the file inside the per-recipient loop with | ||
| 797 | // gPayoutLedgerMutex held, while the event loop's FlushMaturePayouts blocks on | ||
| 798 | // that same mutex -- one slow fsync per recipient, on the thread the miners' | ||
| 799 | // I/O shares. The sequence number is what lets the write happen later, and | ||
| 800 | // possibly out of order, without a stale image reaching the disk. | ||
| 801 | static VersionedBlob SerializePendingPayoutsLocked() | ||
| 321 | { | 802 | { |
| 322 | nlohmann::json arr = nlohmann::json::array(); | 803 | nlohmann::json arr = nlohmann::json::array(); |
| 323 | for (const auto& pp : gPendingPayouts) { | 804 | for (const auto& pp : gPendingPayouts) { |
| 324 | nlohmann::json obj; | 805 | nlohmann::json obj; |
| 325 | obj["id"] = pp.id; | 806 | obj["blockHeight"] = pp.blockHeight; |
| 326 | obj["blockHeight"] = pp.blockHeight; | 807 | obj["blockHash"] = pp.blockHash.ToString(); |
| 327 | obj["matureAtHeight"] = pp.matureAtHeight; | 808 | obj["matureAtHeight"] = pp.matureAtHeight; |
| 328 | obj["nextAttempt"] = pp.nextAttempt; | 809 | // No retryAfter: it was never written with anything but zero, and the |
| 810 | // retry clock that is actually used is on PayingRecord. Files that | ||
| 811 | // still carry the key load fine (it is ignored), and a build that | ||
| 812 | // expects it defaults an absent one to 0, which means "ready now" -- | ||
| 813 | // the same answer it always got. | ||
| 329 | nlohmann::json amounts = nlohmann::json::object(); | 814 | nlohmann::json amounts = nlohmann::json::object(); |
| 330 | for (const auto& kv : pp.amounts) | 815 | for (const auto& kv : pp.amounts) |
| 331 | amounts[kv.first] = kv.second; | 816 | amounts[kv.first] = kv.second; |
| @@ -336,24 +821,41 @@ static void SavePendingPayoutsLocked() | |||
| 336 | obj["paidTxids"] = paid; | 821 | obj["paidTxids"] = paid; |
| 337 | arr.push_back(obj); | 822 | arr.push_back(obj); |
| 338 | } | 823 | } |
| 339 | std::string path = PayoutFilePath(); | 824 | VersionedBlob v; |
| 340 | std::string tmp = path + ".tmp"; | 825 | v.blob = arr.dump(2); |
| 341 | FILE* f = fopen(tmp.c_str(), "w"); | 826 | v.seq = ++gPayoutLedgerSeq; |
| 342 | if (f) { | 827 | return v; |
| 343 | std::string s = arr.dump(2); | 828 | } |
| 344 | fwrite(s.c_str(), 1, s.size(), f); | 829 | |
| 345 | if (fclose(f) != 0) { | 830 | // Publish a ledger snapshot. Holds no data mutex; safe -- and intended -- to |
| 346 | LogPrint("payout", "[payout] WARNING: could not close %s\n", tmp.c_str()); | 831 | // call with gPayoutLedgerMutex released. |
| 347 | return; | 832 | static void PublishPendingPayouts(const VersionedBlob& v) |
| 348 | } | 833 | { |
| 349 | #ifdef _WIN32 | 834 | PublishVersioned(PayoutFilePath(), v, gPayoutLedgerFileMutex, |
| 350 | remove(path.c_str()); | 835 | gPayoutLedgerPublishedSeq, "pending payout ledger"); |
| 351 | #endif | 836 | } |
| 352 | if (rename(tmp.c_str(), path.c_str()) != 0) | 837 | |
| 353 | LogPrint("payout", "[payout] WARNING: could not replace %s\n", path.c_str()); | 838 | // Parse a persisted block-hash field, refusing to guess. SetHex() reads |
| 354 | } else { | 839 | // whatever hex prefix it finds and stops at the first character it does not |
| 355 | LogPrint("payout", "[payout] WARNING: could not write %s\n", tmp.c_str()); | 840 | // recognise, so a corrupt value does not fail to parse -- it parses as a |
| 356 | } | 841 | // DIFFERENT hash. Both shapes of that are wrong in a dangerous direction: a |
| 842 | // value with a hex-looking prefix ("deadbeef...") becomes a hash no block | ||
| 843 | // ever had, which PayoutBlockStillOnChain reads as an orphaned block and | ||
| 844 | // FlushMaturePayouts then VOIDS the debt; a value with no hex prefix at all | ||
| 845 | // becomes 0, which is grandfathered past the orphan check on the strength | ||
| 846 | // of garbage. A field that is not the 64-digit hex string ToString() wrote | ||
| 847 | // is corruption, and corruption is a load failure like any other parse | ||
| 848 | // error -- the file is quarantined, not read around. | ||
| 849 | static uint256 ParsePayoutBlockHash(const std::string& str) | ||
| 850 | { | ||
| 851 | if (str.size() != 64 || | ||
| 852 | str.find_first_not_of("0123456789abcdefABCDEF") != std::string::npos) | ||
| 853 | throw std::runtime_error( | ||
| 854 | strprintf("blockHash \"%s\" is not a 64-digit hex string", | ||
| 855 | str.c_str())); | ||
| 856 | uint256 h; | ||
| 857 | h.SetHex(str); | ||
| 858 | return h; | ||
| 357 | } | 859 | } |
| 358 | 860 | ||
| 359 | static void LoadPendingPayouts() | 861 | static void LoadPendingPayouts() |
| @@ -372,17 +874,27 @@ static void LoadPendingPayouts() | |||
| 372 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | 874 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); |
| 373 | for (const auto& obj : arr) { | 875 | for (const auto& obj : arr) { |
| 374 | PendingPayout pp; | 876 | PendingPayout pp; |
| 375 | if (obj.contains("blockHeight")) | ||
| 376 | pp.blockHeight = obj["blockHeight"].get<int>(); | ||
| 377 | pp.matureAtHeight = obj["matureAtHeight"].get<int>(); | 877 | pp.matureAtHeight = obj["matureAtHeight"].get<int>(); |
| 378 | if (obj.contains("id") && obj["id"].is_string()) | 878 | // Entries written before payouts were keyed by hash carry neither |
| 379 | pp.id = obj["id"].get<std::string>(); | 879 | // field. Leave blockHash zero and they are grandfathered past the |
| 380 | if (pp.blockHeight == 0) | 880 | // orphan check rather than voided on a guess. |
| 881 | if (obj.contains("blockHash") && obj["blockHash"].is_string()) | ||
| 882 | pp.blockHash = | ||
| 883 | ParsePayoutBlockHash(obj["blockHash"].get<std::string>()); | ||
| 884 | if (obj.contains("blockHeight") && obj["blockHeight"].is_number()) | ||
| 885 | pp.blockHeight = obj["blockHeight"].get<int>(); | ||
| 886 | // Upstream's tolerance for an entry with no usable height, kept | ||
| 887 | // because it costs nothing and an entry that cannot name its block | ||
| 888 | // is worse than one derived from its maturity. COINBASE_MATURITY | ||
| 889 | // and not COINBASE_SPENDABLE_DEPTH: this reverses whatever the | ||
| 890 | // writer of *that* file used, and only files old enough to lack | ||
| 891 | // blockHeight are affected -- they were written when maturity was | ||
| 892 | // still COINBASE_MATURITY. | ||
| 893 | if (pp.blockHeight == 0 && pp.matureAtHeight > COINBASE_MATURITY) | ||
| 381 | pp.blockHeight = pp.matureAtHeight - COINBASE_MATURITY; | 894 | pp.blockHeight = pp.matureAtHeight - COINBASE_MATURITY; |
| 382 | if (pp.id.empty()) | 895 | // "retryAfter" is deliberately not read. Files written by the |
| 383 | pp.id = PayoutIdForBlock(pp.blockHeight); | 896 | // merged build carry it, always as 0, and there is nowhere left to |
| 384 | if (obj.contains("nextAttempt")) | 897 | // put it -- the retry clock is PayingRecord's. |
| 385 | pp.nextAttempt = obj["nextAttempt"].get<int64>(); | ||
| 386 | for (auto it = obj["amounts"].begin(); it != obj["amounts"].end(); ++it) | 898 | for (auto it = obj["amounts"].begin(); it != obj["amounts"].end(); ++it) |
| 387 | pp.amounts[it.key()] = it.value().get<int64>(); | 899 | pp.amounts[it.key()] = it.value().get<int64>(); |
| 388 | if (obj.contains("paidTxids") && obj["paidTxids"].is_object()) | 900 | if (obj.contains("paidTxids") && obj["paidTxids"].is_object()) |
| @@ -393,11 +905,212 @@ static void LoadPendingPayouts() | |||
| 393 | LogPrint("payout", "[payout] loaded %zu pending payout(s) from disk\n", | 905 | LogPrint("payout", "[payout] loaded %zu pending payout(s) from disk\n", |
| 394 | gPendingPayouts.size()); | 906 | gPendingPayouts.size()); |
| 395 | } catch (const std::exception& e) { | 907 | } catch (const std::exception& e) { |
| 396 | LogPrint("payout", "[payout] WARNING: failed to parse %s: %s\n", | 908 | RefuseFileAfterLoadFailure(path, "pending payout ledger", e.what()); |
| 397 | path.c_str(), e.what()); | 909 | } |
| 910 | } | ||
| 911 | |||
| 912 | // A payment in flight: one record per (payout entry, address) pair, which is | ||
| 913 | // the granularity SendMoney works at and therefore the granularity at which a | ||
| 914 | // payment can be half-done when the process dies. | ||
| 915 | // | ||
| 916 | // This list is persisted. It used to be a memory-only queue, and matured | ||
| 917 | // entries were erased from pending_payouts.json *before* being handed to it, | ||
| 918 | // so any restart in between lost those payments with no record that anyone | ||
| 919 | // was owed anything: a container restart on 2026-07-30 silently dropped | ||
| 920 | // ~1,156 BTF that way. Writing the records before removing the ledger entry, | ||
| 921 | // and reconciling against the wallet on startup, is what makes a restart | ||
| 922 | // survivable. | ||
| 923 | struct PayingRecord { | ||
| 924 | // Identity of this SLOT, distinct from the identity of the payment it | ||
| 925 | // describes. ForgetPayment and ReschedulePayment used to find their record | ||
| 926 | // by comparing every field, which is only an identity while no two records | ||
| 927 | // can be equal -- and a restart used to produce byte-identical duplicates | ||
| 928 | // (see FlushMaturePayouts), so the first match absorbed every update and | ||
| 929 | // the second was never touched again. | ||
| 930 | // | ||
| 931 | // Process-local and deliberately not persisted: gPaying is rebuilt from | ||
| 932 | // disk at every start, so an id only ever has to name a slot within one | ||
| 933 | // run, and reading ids back would mean trusting a file to contain no | ||
| 934 | // duplicates. Assigned under gPayoutExecMutex from gPayingNextId. | ||
| 935 | uint64 id = 0; | ||
| 936 | int blockHeight = 0; | ||
| 937 | uint256 blockHash = 0; // zero = predates hash keying, see PendingPayout | ||
| 938 | int matureAtHeight = 0; | ||
| 939 | std::string address; | ||
| 940 | int64 amount = 0; | ||
| 941 | int retries = 0; | ||
| 942 | int deferrals = 0; | ||
| 943 | int64 retryAfter = 0; | ||
| 944 | // Ask the wallet whether this payment already went out, once, before the | ||
| 945 | // first send attempt. Upstream asks on every attempt: FindRecordedPoolPayout | ||
| 946 | // is a linear scan of mapWallet under cs_mapWallet, per recipient, so with | ||
| 947 | // 2000 rotating payout addresses and a retry budget of 1440 that is a | ||
| 948 | // scan per address per minute for a day. The question it answers -- "did a | ||
| 949 | // previous process send this before it died?" -- can only change across a | ||
| 950 | // restart, and every restart re-sets this flag (ReconcilePayouts), so | ||
| 951 | // asking once per record is the whole of the answer. | ||
| 952 | // | ||
| 953 | // Not persisted: a record read back from disk is by definition a restart. | ||
| 954 | bool fProbeWallet = true; | ||
| 955 | }; | ||
| 956 | static std::vector<PayingRecord> gPaying; // guarded by gPayoutExecMutex | ||
| 957 | static std::mutex gPayoutExecMutex; | ||
| 958 | static uint64 gPayingNextId = 0; // guarded by gPayoutExecMutex | ||
| 959 | static uint64 gPayingSeq = 0; // guarded by gPayoutExecMutex | ||
| 960 | static std::mutex gPayingFileMutex; | ||
| 961 | static uint64 gPayingPublishedSeq = 0; // guarded by gPayingFileMutex | ||
| 962 | static std::atomic<bool> gPayoutThreadRunning{false}; | ||
| 963 | |||
| 964 | // Upstream's gPayoutExecQueue/gPayoutExecMutex/gPayoutThreadRunning are | ||
| 965 | // declared above with gPaying: the in-flight list IS the exec queue in this | ||
| 966 | // tree, at per-recipient rather than per-round granularity, so a second queue | ||
| 967 | // would be a second answer to "who is owed money". Only the dedupe set is new. | ||
| 968 | // | ||
| 969 | // Ids of rounds currently exploded into gPaying, so a flush cannot queue the | ||
| 970 | // same round twice. Guarded by gPayoutExecMutex. Seeded from the restored | ||
| 971 | // in-flight list by ReconcilePayouts -- it is memory-only, and leaving it | ||
| 972 | // empty at startup was half of why a restart duplicated everything in flight. | ||
| 973 | static std::set<std::string> gQueuedPayouts; | ||
| 974 | |||
| 975 | // Is this exact payment already in flight? gPaying is keyed on | ||
| 976 | // (round, address): the ledger holds one amount per address per round, so two | ||
| 977 | // records for that pair are two standing orders to send the same money. | ||
| 978 | // Caller must hold gPayoutExecMutex. | ||
| 979 | static bool PaymentInFlightLocked(int blockHeight, const uint256& blockHash, | ||
| 980 | const std::string& address) | ||
| 981 | { | ||
| 982 | for (const PayingRecord& e : gPaying) | ||
| 983 | if (e.address == address | ||
| 984 | && SamePayoutRound(e.blockHeight, e.blockHash, blockHeight, blockHash)) | ||
| 985 | return true; | ||
| 986 | return false; | ||
| 987 | } | ||
| 988 | |||
| 989 | static std::string PayingFilePath() | ||
| 990 | { | ||
| 991 | return GetAppDir() + "/paying_payouts.json"; | ||
| 992 | } | ||
| 993 | |||
| 994 | // Serialise the in-flight list. Caller must hold gPayoutExecMutex. | ||
| 995 | // | ||
| 996 | // gPayoutExecMutex already serialises every writer of this file, so it never | ||
| 997 | // had the interleaving race pending_payouts.json had. It is still split the | ||
| 998 | // same way for a different reason: FlushMaturePayouts runs on the event loop | ||
| 999 | // and holds gPayoutExecMutex, so writing (and, since this series, fsyncing) | ||
| 1000 | // the file under that mutex parks miner I/O behind the payout thread's disk. | ||
| 1001 | // The version stamp keeps the file monotonic now that the write is outside | ||
| 1002 | // the mutex that orders the writers. | ||
| 1003 | static VersionedBlob SerializePayingLocked() | ||
| 1004 | { | ||
| 1005 | nlohmann::json arr = nlohmann::json::array(); | ||
| 1006 | for (const auto& r : gPaying) { | ||
| 1007 | nlohmann::json obj; | ||
| 1008 | obj["blockHeight"] = r.blockHeight; | ||
| 1009 | obj["blockHash"] = r.blockHash.ToString(); | ||
| 1010 | obj["matureAtHeight"] = r.matureAtHeight; | ||
| 1011 | obj["address"] = r.address; | ||
| 1012 | obj["amount"] = r.amount; | ||
| 1013 | obj["retries"] = r.retries; | ||
| 1014 | obj["deferrals"] = r.deferrals; | ||
| 1015 | // The live retry clock, which used not to be written at all: every | ||
| 1016 | // restart handed every backing-off payment a retryAfter of 0 and put | ||
| 1017 | // the whole set into the very next 500 ms batch, so a node restarted | ||
| 1018 | // in a loop retried continuously instead of once a minute. | ||
| 1019 | obj["retryAfter"] = r.retryAfter; | ||
| 1020 | arr.push_back(obj); | ||
| 1021 | } | ||
| 1022 | VersionedBlob v; | ||
| 1023 | v.blob = arr.dump(2); | ||
| 1024 | v.seq = ++gPayingSeq; | ||
| 1025 | return v; | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | // Publish an in-flight-list snapshot. Call with gPayoutExecMutex released. | ||
| 1029 | static void PublishPaying(const VersionedBlob& v) | ||
| 1030 | { | ||
| 1031 | PublishVersioned(PayingFilePath(), v, gPayingFileMutex, | ||
| 1032 | gPayingPublishedSeq, "in-flight payment list"); | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | static void LoadPaying() | ||
| 1036 | { | ||
| 1037 | std::string path = PayingFilePath(); | ||
| 1038 | FILE* f = fopen(path.c_str(), "r"); | ||
| 1039 | if (!f) return; | ||
| 1040 | fseek(f, 0, SEEK_END); | ||
| 1041 | long sz = ftell(f); | ||
| 1042 | fseek(f, 0, SEEK_SET); | ||
| 1043 | std::string s(sz, '\0'); | ||
| 1044 | fread(&s[0], 1, sz, f); | ||
| 1045 | fclose(f); | ||
| 1046 | try { | ||
| 1047 | nlohmann::json arr = nlohmann::json::parse(s); | ||
| 1048 | // gPaying and gPayingNextId are both annotated as guarded by | ||
| 1049 | // gPayoutExecMutex and this was mutating them without it. Safe today | ||
| 1050 | // -- LoadPaying only runs at startup, before the payout thread is | ||
| 1051 | // spawned -- but "safe because of where it is called from" is a fact | ||
| 1052 | // about the caller, not about this function, and the annotation is | ||
| 1053 | // what every other reader will trust. The mutex is uncontended here by | ||
| 1054 | // construction, so taking it costs nothing. | ||
| 1055 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 1056 | for (const auto& obj : arr) { | ||
| 1057 | PayingRecord r; | ||
| 1058 | r.address = obj["address"].get<std::string>(); | ||
| 1059 | r.amount = obj["amount"].get<int64>(); | ||
| 1060 | r.matureAtHeight = obj["matureAtHeight"].get<int>(); | ||
| 1061 | if (obj.contains("blockHash") && obj["blockHash"].is_string()) | ||
| 1062 | r.blockHash = | ||
| 1063 | ParsePayoutBlockHash(obj["blockHash"].get<std::string>()); | ||
| 1064 | if (obj.contains("blockHeight") && obj["blockHeight"].is_number()) | ||
| 1065 | r.blockHeight = obj["blockHeight"].get<int>(); | ||
| 1066 | if (obj.contains("retries") && obj["retries"].is_number()) | ||
| 1067 | r.retries = obj["retries"].get<int>(); | ||
| 1068 | if (obj.contains("deferrals") && obj["deferrals"].is_number()) | ||
| 1069 | r.deferrals = obj["deferrals"].get<int>(); | ||
| 1070 | // Absent in files written before this change; 0 means ready now, | ||
| 1071 | // which is exactly the behaviour those files used to get. | ||
| 1072 | if (obj.contains("retryAfter") && obj["retryAfter"].is_number()) | ||
| 1073 | r.retryAfter = obj["retryAfter"].get<int64>(); | ||
| 1074 | // Collapse duplicates as they are read. A build without the | ||
| 1075 | // ReconcilePayouts seeding below wrote a second (and after another | ||
| 1076 | // restart, a third) byte-identical record for every unpaid | ||
| 1077 | // recipient, so a file written by one of those has to be cleaned | ||
| 1078 | // up here or the pathology is inherited. The retry and deferral | ||
| 1079 | // counts are merged upwards so collapsing does not hand the | ||
| 1080 | // survivor a fresh budget. | ||
| 1081 | PayingRecord* dup = NULL; | ||
| 1082 | for (PayingRecord& e : gPaying) | ||
| 1083 | if (e.address == r.address | ||
| 1084 | && SamePayoutRound(e.blockHeight, e.blockHash, | ||
| 1085 | r.blockHeight, r.blockHash)) { | ||
| 1086 | dup = &e; | ||
| 1087 | break; | ||
| 1088 | } | ||
| 1089 | if (dup) { | ||
| 1090 | dup->retries = std::max(dup->retries, r.retries); | ||
| 1091 | dup->deferrals = std::max(dup->deferrals, r.deferrals); | ||
| 1092 | dup->retryAfter = std::max(dup->retryAfter, r.retryAfter); | ||
| 1093 | LogPrint("payout", "[payout] collapsing duplicate in-flight" | ||
| 1094 | " record for %s to %s -- one payment, one record\n", | ||
| 1095 | FormatMoney(r.amount).c_str(), r.address.c_str()); | ||
| 1096 | continue; | ||
| 1097 | } | ||
| 1098 | r.id = ++gPayingNextId; | ||
| 1099 | gPaying.push_back(r); | ||
| 1100 | } | ||
| 1101 | LogPrint("payout", "[payout] loaded %zu in-flight payment(s) from disk\n", | ||
| 1102 | gPaying.size()); | ||
| 1103 | } catch (const std::exception& e) { | ||
| 1104 | RefuseFileAfterLoadFailure(path, "in-flight payment list", e.what()); | ||
| 398 | } | 1105 | } |
| 399 | } | 1106 | } |
| 400 | 1107 | ||
| 1108 | // --------------------------------------------------------------------------- | ||
| 1109 | // Pool round proofs (upstream cbdd06d). Disjoint from the in-flight ledger | ||
| 1110 | // above -- this is the operator's published record of how a round was split, | ||
| 1111 | // not an obligation to pay. Both sides added code at this offset; both are | ||
| 1112 | // kept. | ||
| 1113 | // --------------------------------------------------------------------------- | ||
| 401 | static nlohmann::json PoolRoundToJson(const PoolRoundProof& r) | 1114 | static nlohmann::json PoolRoundToJson(const PoolRoundProof& r) |
| 402 | { | 1115 | { |
| 403 | nlohmann::json j; | 1116 | nlohmann::json j; |
| @@ -436,12 +1149,26 @@ static nlohmann::json PoolRoundToJson(const PoolRoundProof& r) | |||
| 436 | p["txid"] = tx->second; | 1149 | p["txid"] = tx->second; |
| 437 | else | 1150 | else |
| 438 | p["txid"] = nullptr; | 1151 | p["txid"] = nullptr; |
| 1152 | // Only emitted when it happened, so the common shape of the file is | ||
| 1153 | // unchanged for anything already parsing it. | ||
| 1154 | std::map<std::string, std::string>::const_iterator fo = r.forfeited.find(kv.first); | ||
| 1155 | if (fo != r.forfeited.end()) { | ||
| 1156 | p["forfeited"] = true; | ||
| 1157 | p["forfeitReason"] = fo->second; | ||
| 1158 | } | ||
| 439 | j["payouts"][kv.first] = p; | 1159 | j["payouts"][kv.first] = p; |
| 440 | } | 1160 | } |
| 441 | return j; | 1161 | return j; |
| 442 | } | 1162 | } |
| 443 | 1163 | ||
| 444 | static void SavePoolRoundsLocked() | 1164 | // Serialise the published round proofs. Caller must hold gPoolRoundsMutex. |
| 1165 | // | ||
| 1166 | // Like the in-flight list, gPoolRoundsMutex already serialises the writers | ||
| 1167 | // (RecordPoolRound on the event loop, MarkPoolRoundPaid on the payout thread), | ||
| 1168 | // so this file never had the interleaving race either -- but the event loop | ||
| 1169 | // should not be waiting on the payout thread's fsync to publish a round, so | ||
| 1170 | // the write moves outside the mutex on the same versioned terms. | ||
| 1171 | static VersionedBlob SerializePoolRoundsLocked() | ||
| 445 | { | 1172 | { |
| 446 | nlohmann::json root; | 1173 | nlohmann::json root; |
| 447 | root["schema"] = "bitflash-pool-rounds-1"; | 1174 | root["schema"] = "bitflash-pool-rounds-1"; |
| @@ -450,24 +1177,17 @@ static void SavePoolRoundsLocked() | |||
| 450 | for (const PoolRoundProof& r : gPoolRounds) | 1177 | for (const PoolRoundProof& r : gPoolRounds) |
| 451 | root["rounds"].push_back(PoolRoundToJson(r)); | 1178 | root["rounds"].push_back(PoolRoundToJson(r)); |
| 452 | 1179 | ||
| 453 | std::string path = PoolRoundsFilePath(); | 1180 | VersionedBlob v; |
| 454 | std::string tmp = path + ".tmp"; | 1181 | v.blob = root.dump(2); |
| 455 | FILE* f = fopen(tmp.c_str(), "w"); | 1182 | v.seq = ++gPoolRoundsSeq; |
| 456 | if (!f) { | 1183 | return v; |
| 457 | LogPrint("payout", "[payout] WARNING: could not write %s\n", tmp.c_str()); | 1184 | } |
| 458 | return; | 1185 | |
| 459 | } | 1186 | // Publish a round-proof snapshot. Call with gPoolRoundsMutex released. |
| 460 | std::string s = root.dump(2); | 1187 | static void PublishPoolRounds(const VersionedBlob& v) |
| 461 | fwrite(s.c_str(), 1, s.size(), f); | 1188 | { |
| 462 | if (fclose(f) != 0) { | 1189 | PublishVersioned(PoolRoundsFilePath(), v, gPoolRoundsFileMutex, |
| 463 | LogPrint("payout", "[payout] WARNING: could not close %s\n", tmp.c_str()); | 1190 | gPoolRoundsPublishedSeq, "pool round proofs"); |
| 464 | return; | ||
| 465 | } | ||
| 466 | #ifdef _WIN32 | ||
| 467 | remove(path.c_str()); | ||
| 468 | #endif | ||
| 469 | if (rename(tmp.c_str(), path.c_str()) != 0) | ||
| 470 | LogPrint("payout", "[payout] WARNING: could not replace %s\n", path.c_str()); | ||
| 471 | } | 1191 | } |
| 472 | 1192 | ||
| 473 | static void LoadPoolRounds() | 1193 | static void LoadPoolRounds() |
| @@ -512,6 +1232,9 @@ static void LoadPoolRounds() | |||
| 512 | r.payouts[it.key()] = it.value().value("amountSatoshis", (int64)0); | 1232 | r.payouts[it.key()] = it.value().value("amountSatoshis", (int64)0); |
| 513 | if (it.value().contains("txid") && it.value()["txid"].is_string()) | 1233 | if (it.value().contains("txid") && it.value()["txid"].is_string()) |
| 514 | r.paidTxids[it.key()] = it.value()["txid"].get<std::string>(); | 1234 | r.paidTxids[it.key()] = it.value()["txid"].get<std::string>(); |
| 1235 | if (it.value().value("forfeited", false)) | ||
| 1236 | r.forfeited[it.key()] = | ||
| 1237 | it.value().value("forfeitReason", std::string("unspecified")); | ||
| 515 | } else if (it.value().is_number_integer()) { | 1238 | } else if (it.value().is_number_integer()) { |
| 516 | r.payouts[it.key()] = it.value().get<int64>(); | 1239 | r.payouts[it.key()] = it.value().get<int64>(); |
| 517 | } | 1240 | } |
| @@ -524,55 +1247,343 @@ static void LoadPoolRounds() | |||
| 524 | LogPrint("payout", "[payout] loaded %zu pool round proof(s) from disk\n", | 1247 | LogPrint("payout", "[payout] loaded %zu pool round proof(s) from disk\n", |
| 525 | gPoolRounds.size()); | 1248 | gPoolRounds.size()); |
| 526 | } catch (const std::exception& e) { | 1249 | } catch (const std::exception& e) { |
| 527 | LogPrint("payout", "[payout] WARNING: failed to parse %s: %s\n", | 1250 | RefuseFileAfterLoadFailure(path, "pool round proofs", e.what()); |
| 528 | path.c_str(), e.what()); | ||
| 529 | } | 1251 | } |
| 530 | } | 1252 | } |
| 531 | 1253 | ||
| 1254 | // Is the block that funds this payout still on the main chain? | ||
| 1255 | // | ||
| 1256 | // A zero hash means the entry was written before payouts recorded one, so | ||
| 1257 | // there is nothing to check and we assume it is good -- voiding those would | ||
| 1258 | // mean refusing to pay miners on the strength of a missing field. | ||
| 1259 | static bool PayoutBlockStillOnChain(const uint256& blockHash) | ||
| 1260 | { | ||
| 1261 | if (blockHash == 0) | ||
| 1262 | return true; | ||
| 1263 | CRITICAL_BLOCK(cs_main) | ||
| 1264 | { | ||
| 1265 | std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(blockHash); | ||
| 1266 | if (mi == mapBlockIndex.end() || mi->second == NULL) | ||
| 1267 | return false; | ||
| 1268 | return mi->second->IsInMainChain(); | ||
| 1269 | } | ||
| 1270 | return false; | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | // The comment every pre-upgrade payout transaction carries, and therefore the | ||
| 1274 | // ONLY needle the legacy fallback in FindRecordedPoolPayout may search with. | ||
| 1275 | // This format is frozen: change it and every payment those builds made stops | ||
| 1276 | // matching, every one of those debts looks unpaid, and reconciliation pays | ||
| 1277 | // them all a second time. | ||
| 1278 | static std::string LegacyPayoutComment(int blockHeight) | ||
| 1279 | { | ||
| 1280 | return strprintf("Pool payout height %d", blockHeight); | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | // The comment written on new sends. It names the funding block, because the | ||
| 1284 | // height alone cannot: a height that is won, lost to a reorg, and won again | ||
| 1285 | // yields two rounds whose height-only comments are identical -- and identical | ||
| 1286 | // amounts too, whenever the same miner held both rounds. The structured | ||
| 1287 | // pool_payout_* metadata already keys the payment by hash for the code; this | ||
| 1288 | // puts the same fact where an operator reading the wallet can see it, and | ||
| 1289 | // makes a new transaction unmatchable by the legacy needle even if the | ||
| 1290 | // structured keys are ever lost. | ||
| 1291 | // | ||
| 1292 | // A zero hash keeps the legacy form: those records predate hash keying, and | ||
| 1293 | // there is no hash to name. | ||
| 1294 | static std::string PayoutComment(int blockHeight, const uint256& blockHash) | ||
| 1295 | { | ||
| 1296 | if (blockHash == 0) | ||
| 1297 | return LegacyPayoutComment(blockHeight); | ||
| 1298 | return strprintf("Pool payout height %d block %s", blockHeight, | ||
| 1299 | blockHash.ToString().c_str()); | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | // Startup reconciliation. | ||
| 1303 | // | ||
| 1304 | // This used to be the crash-recovery mechanism: it scanned the wallet with | ||
| 1305 | // WalletAlreadyPaid() and then dropped ledger entries that had already been | ||
| 1306 | // exploded into in-flight records. Neither job is right any more. | ||
| 1307 | // | ||
| 1308 | // The ledger is now authoritative and an entry survives until every recipient | ||
| 1309 | // has a txid recorded against it (upstream cbdd06d), so dropping ledger | ||
| 1310 | // entries here would reintroduce exactly the loss this merge is adopting that | ||
| 1311 | // model to prevent. And the wallet scan is now FindRecordedPoolPayout(), done | ||
| 1312 | // once per record on its first attempt, which is the same question asked in | ||
| 1313 | // the one place that can act on the answer. Running both would be two | ||
| 1314 | // authorities for the same debt. | ||
| 1315 | // | ||
| 1316 | // What is left is the useful part: the in-flight list restored from disk may | ||
| 1317 | // contain records the ledger already shows as paid (we died between the send | ||
| 1318 | // and the in-flight file being rewritten). The ledger wins. | ||
| 1319 | // | ||
| 1320 | // And the part that was missing: gQueuedPayouts is memory-only, so it came up | ||
| 1321 | // empty while gPaying came up full. FlushMaturePayouts deduped against that | ||
| 1322 | // empty set and against the ledger's paidTxids, and against nothing else -- | ||
| 1323 | // so a round that was exploded but not yet paid got a SECOND record per | ||
| 1324 | // unpaid recipient on the first flush after every restart, deterministically. | ||
| 1325 | // (Observed live: two byte-identical records for one (block, address).) The | ||
| 1326 | // surviving in-flight list is exactly the statement of which rounds are | ||
| 1327 | // already exploded, so it is what the set is rebuilt from. | ||
| 1328 | static void ReconcilePayouts() | ||
| 1329 | { | ||
| 1330 | VersionedBlob payingSnap; | ||
| 1331 | { | ||
| 1332 | std::lock_guard<std::mutex> exec(gPayoutExecMutex); | ||
| 1333 | std::lock_guard<std::mutex> ledger(gPayoutLedgerMutex); | ||
| 1334 | |||
| 1335 | size_t settled = 0; | ||
| 1336 | int64 settledAmount = 0; | ||
| 1337 | for (auto it = gPaying.begin(); it != gPaying.end(); ) { | ||
| 1338 | bool paid = false; | ||
| 1339 | for (const PendingPayout& pp : gPendingPayouts) | ||
| 1340 | if (SamePayoutRound(pp.blockHeight, pp.blockHash, | ||
| 1341 | it->blockHeight, it->blockHash) | ||
| 1342 | && pp.paidTxids.count(it->address)) { | ||
| 1343 | paid = true; | ||
| 1344 | break; | ||
| 1345 | } | ||
| 1346 | if (paid) { | ||
| 1347 | settledAmount += it->amount; | ||
| 1348 | settled++; | ||
| 1349 | it = gPaying.erase(it); | ||
| 1350 | } else { | ||
| 1351 | // Every restored record gets one wallet probe before it is | ||
| 1352 | // allowed to send: this is precisely the window where the | ||
| 1353 | // wallet may hold a transaction the ledger has not recorded. | ||
| 1354 | it->fProbeWallet = true; | ||
| 1355 | ++it; | ||
| 1356 | } | ||
| 1357 | } | ||
| 1358 | if (settled) { | ||
| 1359 | LogPrint("payout", "[payout] %zu in-flight payment(s) totalling %s" | ||
| 1360 | " are already recorded paid in the ledger -- not" | ||
| 1361 | " resending\n", | ||
| 1362 | settled, FormatMoney(settledAmount).c_str()); | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | // Rebuild the "already exploded" set from what survived. Without this | ||
| 1366 | // the first flush after a restart re-explodes every round in flight. | ||
| 1367 | gQueuedPayouts.clear(); | ||
| 1368 | for (const PayingRecord& r : gPaying) | ||
| 1369 | gQueuedPayouts.insert(PayoutIdForBlock(r.blockHeight, r.blockHash)); | ||
| 1370 | |||
| 1371 | payingSnap = SerializePayingLocked(); | ||
| 1372 | |||
| 1373 | if (!gPaying.empty()) { | ||
| 1374 | int64 owed = 0; | ||
| 1375 | for (const auto& r : gPaying) owed += r.amount; | ||
| 1376 | LogPrint("payout", "[payout] resuming %zu in-flight payment(s)" | ||
| 1377 | " totalling %s\n", | ||
| 1378 | gPaying.size(), FormatMoney(owed).c_str()); | ||
| 1379 | } | ||
| 1380 | } | ||
| 1381 | PublishPaying(payingSnap); | ||
| 1382 | } | ||
| 1383 | |||
| 532 | static void RecordPoolRound(const PoolRoundProof& round) | 1384 | static void RecordPoolRound(const PoolRoundProof& round) |
| 533 | { | 1385 | { |
| 534 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); | 1386 | VersionedBlob snap; |
| 535 | for (PoolRoundProof& existing : gPoolRounds) { | 1387 | { |
| 536 | if (existing.id == round.id) { | 1388 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); |
| 537 | existing = round; | 1389 | bool replaced = false; |
| 538 | SavePoolRoundsLocked(); | 1390 | for (PoolRoundProof& existing : gPoolRounds) { |
| 539 | return; | 1391 | if (existing.id == round.id) { |
| 1392 | existing = round; | ||
| 1393 | replaced = true; | ||
| 1394 | break; | ||
| 1395 | } | ||
| 1396 | } | ||
| 1397 | if (!replaced) { | ||
| 1398 | gPoolRounds.push_back(round); | ||
| 1399 | while (gPoolRounds.size() > MAX_POOL_ROUNDS) | ||
| 1400 | gPoolRounds.erase(gPoolRounds.begin()); | ||
| 540 | } | 1401 | } |
| 1402 | snap = SerializePoolRoundsLocked(); | ||
| 541 | } | 1403 | } |
| 542 | gPoolRounds.push_back(round); | 1404 | PublishPoolRounds(snap); |
| 543 | while (gPoolRounds.size() > MAX_POOL_ROUNDS) | ||
| 544 | gPoolRounds.erase(gPoolRounds.begin()); | ||
| 545 | SavePoolRoundsLocked(); | ||
| 546 | } | 1405 | } |
| 547 | 1406 | ||
| 548 | static void MarkPoolRoundPaid(const std::string& id, | 1407 | static void MarkPoolRoundPaid(const std::string& id, |
| 549 | const std::string& address, | 1408 | const std::string& address, |
| 550 | const std::string& txid) | 1409 | const std::string& txid) |
| 551 | { | 1410 | { |
| 552 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); | 1411 | VersionedBlob snap; |
| 553 | for (PoolRoundProof& r : gPoolRounds) { | 1412 | { |
| 554 | if (r.id != id) | 1413 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); |
| 555 | continue; | 1414 | bool found = false; |
| 556 | r.paidTxids[address] = txid; | 1415 | for (PoolRoundProof& r : gPoolRounds) { |
| 557 | SavePoolRoundsLocked(); | 1416 | if (r.id != id) |
| 558 | return; | 1417 | continue; |
| 1418 | r.paidTxids[address] = txid; | ||
| 1419 | found = true; | ||
| 1420 | break; | ||
| 1421 | } | ||
| 1422 | if (!found) | ||
| 1423 | return; | ||
| 1424 | snap = SerializePoolRoundsLocked(); | ||
| 559 | } | 1425 | } |
| 1426 | PublishPoolRounds(snap); | ||
| 560 | } | 1427 | } |
| 561 | 1428 | ||
| 562 | // Payouts ready to execute are moved here under gPayoutExecMutex. | 1429 | // Publish the fact that a recipient was written off, so the round proof and |
| 563 | // A dedicated thread drains this list so SendMoney (which takes cs_main) | 1430 | // the ledger agree. The ledger erases the amount (it is not owed any more and |
| 564 | // never runs on the event loop thread and can't stall miner I/O. | 1431 | // must not be reported as sent); the proof keeps the amount visible and marks |
| 565 | static std::vector<PendingPayout> gPayoutExecQueue; | 1432 | // it forfeited with a reason, because a miner who was promised a share is |
| 566 | static std::mutex gPayoutExecMutex; | 1433 | // entitled to see what happened to it. |
| 567 | static std::atomic<bool> gPayoutThreadRunning{false}; | 1434 | static void MarkPoolRoundForfeited(const std::string& id, |
| 1435 | const std::string& address, | ||
| 1436 | const std::string& reason) | ||
| 1437 | { | ||
| 1438 | VersionedBlob snap; | ||
| 1439 | { | ||
| 1440 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); | ||
| 1441 | bool found = false; | ||
| 1442 | for (PoolRoundProof& r : gPoolRounds) { | ||
| 1443 | if (r.id != id) | ||
| 1444 | continue; | ||
| 1445 | r.forfeited[address] = reason; | ||
| 1446 | found = true; | ||
| 1447 | break; | ||
| 1448 | } | ||
| 1449 | if (!found) | ||
| 1450 | return; | ||
| 1451 | snap = SerializePoolRoundsLocked(); | ||
| 1452 | } | ||
| 1453 | PublishPoolRounds(snap); | ||
| 1454 | } | ||
| 568 | 1455 | ||
| 569 | static std::set<std::string> gQueuedPayouts; | 1456 | // Does this round have a published round proof? |
| 1457 | // | ||
| 1458 | // Existence answers a temporal question the legacy fallback below needs: | ||
| 1459 | // which builds could have paid this round. RecordPoolRound writes the proof | ||
| 1460 | // when the round is created, and structured payout metadata (upstream | ||
| 1461 | // cbdd06d) is an ancestor of round proofs (upstream 83ba822) -- no build | ||
| 1462 | // able to write a proof has ever sent a payment that only a comment can | ||
| 1463 | // identify. A round with a proof therefore cannot have a comment-only | ||
| 1464 | // payment; one that matches it is another round's, colliding on the height. | ||
| 1465 | static bool PoolRoundHasProof(const std::string& id) | ||
| 1466 | { | ||
| 1467 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); | ||
| 1468 | for (const PoolRoundProof& r : gPoolRounds) | ||
| 1469 | if (r.id == id) | ||
| 1470 | return true; | ||
| 1471 | return false; | ||
| 1472 | } | ||
| 570 | 1473 | ||
| 571 | static bool FindRecordedPoolPayout(const std::string& id, | 1474 | // Which wallet transaction, if any, already paid this? |
| 572 | const std::string& address, | 1475 | // |
| 573 | int64 amount, | 1476 | // Two kinds of evidence, in order of quality. |
| 574 | std::string& txidRet) | 1477 | // |
| 1478 | // Structured (pool_payout_id / pool_payout_address / pool_payout_amount, | ||
| 1479 | // plus pool_payout_block since this series) is upstream's, extended. The id | ||
| 1480 | // is derived from the block hash, but only from the FIRST sixteen hex digits | ||
| 1481 | // of it -- and proof-of-work concentrates zeros exactly there, so the id can | ||
| 1482 | // name two same-height rounds identically. New sends therefore record the | ||
| 1483 | // full hash as pool_payout_block, and a structured match must agree with it | ||
| 1484 | // wherever both sides have one; a transaction without the key was sent | ||
| 1485 | // before it existed, and for those the id is the best evidence there is. | ||
| 1486 | // Every transaction this build sends carries all four keys. | ||
| 1487 | // | ||
| 1488 | // But NO transaction this pool has ever sent carries it. Every payout in the | ||
| 1489 | // existing wallet has only a "comment". So on the first boot after this | ||
| 1490 | // upgrade -- precisely the boot where the binary is swapped after an unclean | ||
| 1491 | // shutdown with payments in flight -- a structured-only probe finds nothing | ||
| 1492 | // and every one of those recipients is paid a second time. The deleted | ||
| 1493 | // WalletAlreadyPaid() matched on what the payment WAS, and that evidence is | ||
| 1494 | // still there; it just has to be read. | ||
| 1495 | // | ||
| 1496 | // The legacy fallback is therefore restored, with three constraints that | ||
| 1497 | // together stop it answering for a round that is not this one: | ||
| 1498 | // | ||
| 1499 | // 1. it is only consulted when the structured lookup found nothing; | ||
| 1500 | // 2. it ignores any transaction carrying pool_payout_id. That key means this | ||
| 1501 | // build wrote the transaction, and this build's transactions are | ||
| 1502 | // adjudicated by the exact path -- so a NEW round's payment can never be | ||
| 1503 | // offered as evidence for an old one. Only pre-upgrade transactions, | ||
| 1504 | // which is exactly the population the fallback exists for, are eligible; | ||
| 1505 | // 3. `excluded` holds every txid already credited to some OTHER | ||
| 1506 | // (round, address) pair -- from the ledger's paidTxids, which is durable, | ||
| 1507 | // and from what this process has already claimed. One legacy transaction | ||
| 1508 | // can therefore settle at most one debt, so N transactions can never | ||
| 1509 | // satisfy more than N obligations; | ||
| 1510 | // | ||
| 1511 | // 4. it is not consulted at all for a round that has a round proof | ||
| 1512 | // (fAllowLegacy, decided by the caller from PoolRoundHasProof). Proofs | ||
| 1513 | // are written when a round is created, and only by builds whose every | ||
| 1514 | // payment names its round exactly -- so a comment-only transaction | ||
| 1515 | // cannot be a payment for such a round, only a same-height collision | ||
| 1516 | // from an older one, and skipping the fallback cannot hide a real | ||
| 1517 | // payment: the structured probe above already found any that exist. | ||
| 1518 | // | ||
| 1519 | // What is left unresolved by construction is a comment-only transaction and | ||
| 1520 | // a PROOFLESS round at the same height paying the same address the same | ||
| 1521 | // amount: two grandfathered zero-hash twins, or a twin pair from the builds | ||
| 1522 | // that keyed the ledger by hash before any metadata reached the wallet. The | ||
| 1523 | // comment carries only the height, so nothing ties the transaction to one | ||
| 1524 | // twin rather than the other. That needs a reorg. | ||
| 1525 | // | ||
| 1526 | // This used to say the losing twin's ledger entry is voided by the orphan | ||
| 1527 | // check before it can be paid. That is wrong: PayoutBlockStillOnChain() | ||
| 1528 | // returns true for a zero hash, and EVERY pre-upgrade entry has a zero hash, | ||
| 1529 | // so the orphan check is a no-op for exactly the population this fallback | ||
| 1530 | // serves. | ||
| 1531 | // | ||
| 1532 | // The outcome is still safe, by a different mechanism. SamePayoutRound() | ||
| 1533 | // treats two zero-hash entries at the same height as the SAME round, so the | ||
| 1534 | // pair is indistinguishable everywhere, not only here: FlushMaturePayouts | ||
| 1535 | // explodes one of them and skips the other as already queued, | ||
| 1536 | // PaymentInFlightLocked refuses a second record for that (round, address), and | ||
| 1537 | // RecordPayoutTxid settles against the first match. The pair therefore | ||
| 1538 | // presents as ONE debt and can be paid at most once -- which is the direction | ||
| 1539 | // that matters for a fallback whose failure mode is treating one payment as | ||
| 1540 | // proof of two. | ||
| 1541 | // | ||
| 1542 | // The cost of that mechanism, stated plainly: only one of a genuine | ||
| 1543 | // same-height pair would ever be paid. It takes a reorg AND both rounds | ||
| 1544 | // predating hash keying, and no file this tree has written since can produce | ||
| 1545 | // one, so it is a property of history rather than a live risk. | ||
| 1546 | // | ||
| 1547 | // Where the evidence genuinely runs out the fallback simply does not match, | ||
| 1548 | // and the payment is made -- paying a debt we cannot prove was settled is the | ||
| 1549 | // correct default for a pool. | ||
| 1550 | enum PayoutProbeResult { | ||
| 1551 | PAYOUT_PROBE_NONE = 0, // the wallet holds no record of this payment | ||
| 1552 | PAYOUT_PROBE_STRUCTURED, // matched this build's pool_payout_* metadata | ||
| 1553 | PAYOUT_PROBE_LEGACY // matched a pre-upgrade comment and output | ||
| 1554 | }; | ||
| 1555 | |||
| 1556 | static PayoutProbeResult FindRecordedPoolPayout(const std::string& id, | ||
| 1557 | int blockHeight, | ||
| 1558 | const uint256& blockHash, | ||
| 1559 | const std::string& address, | ||
| 1560 | int64 amount, | ||
| 1561 | bool fAllowLegacy, | ||
| 1562 | const std::set<std::string>& excluded, | ||
| 1563 | std::string& txidRet) | ||
| 575 | { | 1564 | { |
| 1565 | // Built before cs_mapWallet is taken: an invalid address has no script to | ||
| 1566 | // compare against, and the caller has already rejected those. | ||
| 1567 | uint160 h160; | ||
| 1568 | bool fHaveScript = AddressToHash160(address, h160); | ||
| 1569 | CScript want; | ||
| 1570 | if (fHaveScript) | ||
| 1571 | want << OP_DUP << OP_HASH160 << h160 << OP_EQUALVERIFY << OP_CHECKSIG; | ||
| 1572 | const std::string comment = LegacyPayoutComment(blockHeight); | ||
| 1573 | |||
| 1574 | // Candidate legacy matches, adjudicated after cs_mapWallet is released. | ||
| 1575 | // | ||
| 1576 | // The depth check cannot happen inside the scan: GetDepthInMainChain() | ||
| 1577 | // reads mapBlockIndex and pindexBest, which belong to cs_main, and cs_main | ||
| 1578 | // is taken BEFORE cs_mapWallet everywhere else in this tree | ||
| 1579 | // (CreateTransaction, ConnectBlock). Taking it the other way round here | ||
| 1580 | // would be a lock-order inversion in the payout thread. There is at most a | ||
| 1581 | // handful of these: a candidate has to carry this exact height's comment | ||
| 1582 | // and an output of exactly this amount to this address. | ||
| 1583 | std::vector<uint256> legacyCandidates; | ||
| 1584 | |||
| 1585 | // One pass, not two: this scan runs under cs_mapWallet, which the whole | ||
| 1586 | // node contends on, and the merge is already paying for it once per record. | ||
| 576 | CRITICAL_BLOCK(cs_mapWallet) | 1587 | CRITICAL_BLOCK(cs_mapWallet) |
| 577 | { | 1588 | { |
| 578 | for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); | 1589 | for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); |
| @@ -580,23 +1591,145 @@ static bool FindRecordedPoolPayout(const std::string& id, | |||
| 580 | { | 1591 | { |
| 581 | const CWalletTx& wtx = it->second; | 1592 | const CWalletTx& wtx = it->second; |
| 582 | map<string, string>::const_iterator idIt = wtx.mapValue.find("pool_payout_id"); | 1593 | map<string, string>::const_iterator idIt = wtx.mapValue.find("pool_payout_id"); |
| 583 | if (idIt == wtx.mapValue.end() || idIt->second != id) | 1594 | if (idIt != wtx.mapValue.end()) { |
| 1595 | // Written by this build. Adjudicated exactly, and never | ||
| 1596 | // offered to the fallback below. | ||
| 1597 | if (idIt->second != id) | ||
| 1598 | continue; | ||
| 1599 | map<string, string>::const_iterator addrIt = wtx.mapValue.find("pool_payout_address"); | ||
| 1600 | if (addrIt == wtx.mapValue.end() || addrIt->second != address) | ||
| 1601 | continue; | ||
| 1602 | map<string, string>::const_iterator amtIt = wtx.mapValue.find("pool_payout_amount"); | ||
| 1603 | if (amtIt == wtx.mapValue.end() || amtIt->second != strprintf("%lld", amount)) | ||
| 1604 | continue; | ||
| 1605 | // The id alone cannot tell twin rounds at one height apart | ||
| 1606 | // (see above); the full hash can, where the transaction | ||
| 1607 | // recorded one. Where it did not, the id decides, as it | ||
| 1608 | // always has -- refusing those transactions would make every | ||
| 1609 | // payment sent before the key existed look unpaid and pay it | ||
| 1610 | // again. | ||
| 1611 | if (blockHash != 0) { | ||
| 1612 | map<string, string>::const_iterator blkIt = | ||
| 1613 | wtx.mapValue.find("pool_payout_block"); | ||
| 1614 | if (blkIt != wtx.mapValue.end() | ||
| 1615 | && blkIt->second != blockHash.ToString()) | ||
| 1616 | continue; // a twin round's payment, not this round's | ||
| 1617 | } | ||
| 1618 | txidRet = wtx.GetHash().GetHex(); | ||
| 1619 | return PAYOUT_PROBE_STRUCTURED; | ||
| 1620 | } | ||
| 1621 | |||
| 1622 | if (!fAllowLegacy) | ||
| 1623 | continue; // comment-only evidence is inadmissible: rule 4 | ||
| 1624 | if (!fHaveScript) | ||
| 584 | continue; | 1625 | continue; |
| 585 | map<string, string>::const_iterator addrIt = wtx.mapValue.find("pool_payout_address"); | 1626 | // The comment is written only by the payout path, so its presence |
| 586 | if (addrIt == wtx.mapValue.end() || addrIt->second != address) | 1627 | // already identifies the transaction as one of ours. |
| 1628 | map<string, string>::const_iterator ci = wtx.mapValue.find("comment"); | ||
| 1629 | if (ci == wtx.mapValue.end() || ci->second != comment) | ||
| 587 | continue; | 1630 | continue; |
| 588 | map<string, string>::const_iterator amtIt = wtx.mapValue.find("pool_payout_amount"); | 1631 | if (excluded.count(wtx.GetHash().GetHex())) |
| 589 | if (amtIt == wtx.mapValue.end() || amtIt->second != strprintf("%lld", amount)) | 1632 | continue; // already credited to a different debt |
| 1633 | foreach (const CTxOut& txout, wtx.vout) | ||
| 1634 | if (txout.nValue == amount && txout.scriptPubKey == want) { | ||
| 1635 | legacyCandidates.push_back(it->first); | ||
| 1636 | break; | ||
| 1637 | } | ||
| 1638 | } | ||
| 1639 | } | ||
| 1640 | |||
| 1641 | // A legacy match must have CONFIRMED. | ||
| 1642 | // | ||
| 1643 | // The structured path does not need this and deliberately does not have | ||
| 1644 | // it: this process sent that transaction moments ago, and a send that | ||
| 1645 | // failed leaves nothing behind, because SendMoney calls | ||
| 1646 | // RollbackTransactionSpent, which calls EraseFromWallet. | ||
| 1647 | // | ||
| 1648 | // The legacy path is the opposite case in every respect. Those | ||
| 1649 | // transactions are days or weeks old, so whether they confirmed is a | ||
| 1650 | // settled fact rather than an open question, and a pre-upgrade payout that | ||
| 1651 | // never confirmed is evidence of NON-payment about to be read as payment. | ||
| 1652 | // This chain has had a 280-block reorg (see FlushMaturePayouts): a payout | ||
| 1653 | // invalidated by it is still sitting in mapWallet with its comment intact, | ||
| 1654 | // and the first boot after this upgrade is exactly when it gets matched. | ||
| 1655 | // The cost of being wrong here is a miner who is never paid and no record | ||
| 1656 | // that they were owed. | ||
| 1657 | CRITICAL_BLOCK(cs_main) | ||
| 1658 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 1659 | { | ||
| 1660 | for (const uint256& hash : legacyCandidates) { | ||
| 1661 | map<uint256, CWalletTx>::const_iterator it = mapWallet.find(hash); | ||
| 1662 | if (it == mapWallet.end()) | ||
| 1663 | continue; // erased under us; it is not evidence of anything | ||
| 1664 | int depth = it->second.GetDepthInMainChain(); | ||
| 1665 | if (depth < 1) { | ||
| 1666 | // Loud: this is the difference between a miner having been | ||
| 1667 | // paid and a miner about to be paid twice, decided on a fact | ||
| 1668 | // that is easy to check and impossible to recover afterwards. | ||
| 1669 | LogPrint("payout", "[payout] wallet tx %s carries the payout" | ||
| 1670 | " comment for height %d and an output of exactly %s to" | ||
| 1671 | " %s, but it is NOT in the main chain (depth %d) --" | ||
| 1672 | " refusing to read it as payment; this recipient will" | ||
| 1673 | " be paid\n", | ||
| 1674 | hash.GetHex().c_str(), blockHeight, | ||
| 1675 | FormatMoney(amount).c_str(), address.c_str(), depth); | ||
| 590 | continue; | 1676 | continue; |
| 591 | txidRet = wtx.GetHash().GetHex(); | 1677 | } |
| 592 | return true; | 1678 | txidRet = hash.GetHex(); |
| 1679 | return PAYOUT_PROBE_LEGACY; | ||
| 593 | } | 1680 | } |
| 594 | } | 1681 | } |
| 595 | return false; | 1682 | return PAYOUT_PROBE_NONE; |
| 596 | } | 1683 | } |
| 597 | 1684 | ||
| 1685 | // txid -> "<round id>|<address>", for payments this run has already credited | ||
| 1686 | // to a debt on legacy evidence. Stops one pre-upgrade transaction being read | ||
| 1687 | // as proof that two different obligations were met. The ledger's paidTxids | ||
| 1688 | // covers the same ground durably; this covers the window before the recovered | ||
| 1689 | // txid reaches the ledger, and the case where the ledger entry has already | ||
| 1690 | // been retired. | ||
| 1691 | static std::map<std::string, std::string> gLegacyClaimedTxids; | ||
| 1692 | static std::mutex gClaimedTxidMutex; | ||
| 1693 | |||
| 1694 | // Every txid already spoken for by some payment OTHER than this one. Collected | ||
| 1695 | // before cs_mapWallet is taken: the ledger mutex must not be acquired under a | ||
| 1696 | // wallet lock. | ||
| 1697 | static void CollectForeignPayoutTxids(int blockHeight, const uint256& blockHash, | ||
| 1698 | const std::string& address, | ||
| 1699 | const std::string& claimKey, | ||
| 1700 | std::set<std::string>& out) | ||
| 1701 | { | ||
| 1702 | { | ||
| 1703 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 1704 | for (const PendingPayout& pp : gPendingPayouts) { | ||
| 1705 | bool sameRound = SamePayoutRound(pp.blockHeight, pp.blockHash, | ||
| 1706 | blockHeight, blockHash); | ||
| 1707 | for (const auto& kv : pp.paidTxids) | ||
| 1708 | if (!(sameRound && kv.first == address)) | ||
| 1709 | out.insert(kv.second); | ||
| 1710 | } | ||
| 1711 | } | ||
| 1712 | { | ||
| 1713 | std::lock_guard<std::mutex> lk(gClaimedTxidMutex); | ||
| 1714 | for (const auto& kv : gLegacyClaimedTxids) | ||
| 1715 | if (kv.second != claimKey) | ||
| 1716 | out.insert(kv.first); | ||
| 1717 | } | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | static void ClaimLegacyPayoutTxid(const std::string& txid, const std::string& claimKey) | ||
| 1721 | { | ||
| 1722 | std::lock_guard<std::mutex> lk(gClaimedTxidMutex); | ||
| 1723 | gLegacyClaimedTxids[txid] = claimKey; | ||
| 1724 | } | ||
| 1725 | |||
| 1726 | // blockHash stays a uint256 and not upstream's display string: it is the key | ||
| 1727 | // this fork's payout ledger is built on, and a key that has been through | ||
| 1728 | // ToString() is a key that can be compared wrong. It is converted to text only | ||
| 1729 | // where the round proof needs text. The other three arguments are upstream's, | ||
| 1730 | // carried for the round-proof data. | ||
| 598 | static void QueuePayouts(int blockHeight, | 1731 | static void QueuePayouts(int blockHeight, |
| 599 | const std::string& blockHash, | 1732 | const uint256& blockHash, |
| 600 | const std::string& coinbaseTxid, | 1733 | const std::string& coinbaseTxid, |
| 601 | int64 coinbaseValue, | 1734 | int64 coinbaseValue, |
| 602 | const std::string& foundBy, | 1735 | const std::string& foundBy, |
| @@ -604,11 +1737,11 @@ static void QueuePayouts(int blockHeight, | |||
| 604 | uint64 total) | 1737 | uint64 total) |
| 605 | { | 1738 | { |
| 606 | PoolRoundProof round; | 1739 | PoolRoundProof round; |
| 607 | round.id = PayoutIdForBlock(blockHeight); | 1740 | round.id = PayoutIdForBlock(blockHeight, blockHash); |
| 608 | round.blockHeight = blockHeight; | 1741 | round.blockHeight = blockHeight; |
| 609 | round.matureAtHeight = blockHeight + COINBASE_MATURITY; | 1742 | round.matureAtHeight = blockHeight + COINBASE_SPENDABLE_DEPTH; |
| 610 | round.createdAt = GetTime(); | 1743 | round.createdAt = GetTime(); |
| 611 | round.blockHash = blockHash; | 1744 | round.blockHash = blockHash.ToString(); |
| 612 | round.coinbaseTxid = coinbaseTxid; | 1745 | round.coinbaseTxid = coinbaseTxid; |
| 613 | round.coinbaseValue = coinbaseValue; | 1746 | round.coinbaseValue = coinbaseValue; |
| 614 | round.foundBy = foundBy; | 1747 | round.foundBy = foundBy; |
| @@ -639,9 +1772,21 @@ static void QueuePayouts(int blockHeight, | |||
| 639 | round.operatorReward = operatorCut; | 1772 | round.operatorReward = operatorCut; |
| 640 | 1773 | ||
| 641 | PendingPayout pp; | 1774 | PendingPayout pp; |
| 642 | pp.id = round.id; | ||
| 643 | pp.blockHeight = blockHeight; | 1775 | pp.blockHeight = blockHeight; |
| 644 | pp.matureAtHeight = blockHeight + COINBASE_MATURITY; | 1776 | pp.blockHash = blockHash; |
| 1777 | // COINBASE_SPENDABLE_DEPTH, not COINBASE_MATURITY. Paying at depth 100 | ||
| 1778 | // means the block's own coinbase is still 20 blocks short of spendable, so | ||
| 1779 | // the payment has to come out of some *other* mature coin -- the pool runs | ||
| 1780 | // a permanent ~19-block float and every payout fails until unrelated coin | ||
| 1781 | // matures. The earlier "96% of 934 payments went through at depth 100" | ||
| 1782 | // measurement that justified the shorter wait was taken while this node was | ||
| 1783 | // alone on a private fork paying itself, so it says nothing about a pool | ||
| 1784 | // competing on the real chain. Waiting the extra 20 blocks makes each | ||
| 1785 | // payout funded by construction, and puts the maturity gate past the | ||
| 1786 | // reorg depth that orphaned 280 blocks of obligations. Upstream uses | ||
| 1787 | // COINBASE_MATURITY here; this is a considered disagreement, not an | ||
| 1788 | // oversight -- see the merge commit message. | ||
| 1789 | pp.matureAtHeight = blockHeight + COINBASE_SPENDABLE_DEPTH; | ||
| 645 | 1790 | ||
| 646 | int skipped = 0; | 1791 | int skipped = 0; |
| 647 | int64 burned = 0; // amount owed to miners with bad/empty addresses | 1792 | int64 burned = 0; // amount owed to miners with bad/empty addresses |
| @@ -668,9 +1813,13 @@ static void QueuePayouts(int blockHeight, | |||
| 668 | round.forfeitedBadAddress = burned; | 1813 | round.forfeitedBadAddress = burned; |
| 669 | 1814 | ||
| 670 | { | 1815 | { |
| 671 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | 1816 | VersionedBlob snap; |
| 672 | gPendingPayouts.push_back(pp); | 1817 | { |
| 673 | SavePendingPayoutsLocked(); | 1818 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); |
| 1819 | gPendingPayouts.push_back(pp); | ||
| 1820 | snap = SerializePendingPayoutsLocked(); | ||
| 1821 | } | ||
| 1822 | PublishPendingPayouts(snap); | ||
| 674 | } | 1823 | } |
| 675 | RecordPoolRound(round); | 1824 | RecordPoolRound(round); |
| 676 | 1825 | ||
| @@ -689,37 +1838,340 @@ static void QueuePayouts(int blockHeight, | |||
| 689 | : ""); | 1838 | : ""); |
| 690 | } | 1839 | } |
| 691 | 1840 | ||
| 692 | // Called from event loop: copy matured entries to the exec queue (fast). | 1841 | // Called from the event loop: explode matured ledger entries into per-recipient |
| 693 | // The ledger entry stays on disk until every recipient has a txid recorded. | 1842 | // in-flight records (fast; no wallet or disk work beyond one small write). |
| 694 | // On restart, FindRecordedPoolPayout() checks wallet metadata before sending, | 1843 | // |
| 695 | // so a crash after SendMoney but before the ledger update does not double-pay. | 1844 | // The ledger entry SURVIVES here. That is upstream's model and it is the |
| 1845 | // crash-safe one: this fork used to erase the entry as soon as its payments | ||
| 1846 | // were handed off, so a death in that window left the debt described by | ||
| 1847 | // nothing at all -- a container restart on 2026-07-30 silently dropped | ||
| 1848 | // ~1,156 BTF exactly that way. An entry is removed only once every recipient | ||
| 1849 | // in it has a txid recorded (PayoutFullyPaid), which the payout thread does. | ||
| 1850 | // | ||
| 1851 | // gPaying is kept on top of that as the in-flight working set: it carries the | ||
| 1852 | // per-recipient retry/deferral state that a per-round `nextAttempt` cannot | ||
| 1853 | // express, and it is what the payout thread iterates. It is a cache of work | ||
| 1854 | // derived from the ledger, never an independent record of debt -- if it were | ||
| 1855 | // lost entirely the ledger would rebuild it on the next flush. | ||
| 696 | static void FlushMaturePayouts() | 1856 | static void FlushMaturePayouts() |
| 697 | { | 1857 | { |
| 1858 | // Which funding blocks are still on the main chain? Asked with no payout | ||
| 1859 | // lock held: PayoutBlockStillOnChain takes cs_main, and cs_main is held by | ||
| 1860 | // ProcessBlock and SendMoney for long stretches. Holding the ledger mutex | ||
| 1861 | // across it would let the event loop park the payout thread behind a chain | ||
| 1862 | // lock it has no interest in. (It is not a deadlock either way -- nothing | ||
| 1863 | // takes the ledger mutex while holding cs_main -- but it is exactly the | ||
| 1864 | // kind of stall this merge is removing from the ledger path.) | ||
| 1865 | std::vector<uint256> orphaned; | ||
| 1866 | { | ||
| 1867 | std::vector<uint256> hashes; | ||
| 1868 | { | ||
| 1869 | std::lock_guard<std::mutex> ledger(gPayoutLedgerMutex); | ||
| 1870 | for (const PendingPayout& pp : gPendingPayouts) | ||
| 1871 | hashes.push_back(pp.blockHash); | ||
| 1872 | } | ||
| 1873 | for (const uint256& h : hashes) | ||
| 1874 | if (!PayoutBlockStillOnChain(h)) | ||
| 1875 | orphaned.push_back(h); | ||
| 1876 | } | ||
| 1877 | |||
| 698 | std::vector<PendingPayout> ready; | 1878 | std::vector<PendingPayout> ready; |
| 1879 | VersionedBlob ledgerSnap; | ||
| 699 | { | 1880 | { |
| 700 | std::lock_guard<std::mutex> ledger(gPayoutLedgerMutex); | 1881 | std::lock_guard<std::mutex> ledger(gPayoutLedgerMutex); |
| 701 | std::lock_guard<std::mutex> queue(gPayoutExecMutex); | 1882 | |
| 702 | int64 now = GetTime(); | 1883 | // Void obligations whose funding block lost a reorg. The coin that was |
| 1884 | // meant to pay them does not exist, so paying anyway spends real-chain | ||
| 1885 | // coin the pool owes to somebody else. | ||
| 1886 | bool voided = false; | ||
| 1887 | for (auto it = gPendingPayouts.begin(); it != gPendingPayouts.end(); ) { | ||
| 1888 | if (std::find(orphaned.begin(), orphaned.end(), it->blockHash) == orphaned.end()) { | ||
| 1889 | ++it; | ||
| 1890 | continue; | ||
| 1891 | } | ||
| 1892 | int64 total = 0; | ||
| 1893 | for (auto& kv : it->amounts) total += kv.second; | ||
| 1894 | // Always: one line per voided entry, never a collapsed summary. | ||
| 1895 | // A reorg voids these in a burst from this one source line. | ||
| 1896 | LogPrintAlways("payout", "[payout] block %d (%s) was orphaned -- voiding %zu" | ||
| 1897 | " payment(s) totalling %s\n", | ||
| 1898 | it->blockHeight, it->blockHash.ToString().substr(0, 14).c_str(), | ||
| 1899 | it->amounts.size(), FormatMoney(total).c_str()); | ||
| 1900 | it = gPendingPayouts.erase(it); | ||
| 1901 | voided = true; | ||
| 1902 | } | ||
| 1903 | if (voided) ledgerSnap = SerializePendingPayoutsLocked(); | ||
| 1904 | |||
| 703 | for (const PendingPayout& pp : gPendingPayouts) { | 1905 | for (const PendingPayout& pp : gPendingPayouts) { |
| 704 | if (nBestHeight < pp.matureAtHeight) | 1906 | if (nBestHeight < pp.matureAtHeight) |
| 705 | continue; | 1907 | continue; |
| 706 | if (pp.nextAttempt > now) | ||
| 707 | continue; | ||
| 708 | if (PayoutFullyPaid(pp)) | 1908 | if (PayoutFullyPaid(pp)) |
| 709 | continue; | 1909 | continue; |
| 710 | if (gQueuedPayouts.count(pp.id)) | 1910 | // An entry with neither a hash nor a height cannot be told apart |
| 1911 | // from another in the same state, so a txid recorded against it | ||
| 1912 | // could settle the wrong debt. Paying it is the unsafe move; | ||
| 1913 | // leaving it standing and saying so is not. | ||
| 1914 | if (!PayoutRoundIdentifiable(pp.blockHeight, pp.blockHash)) { | ||
| 1915 | LogPrint("payout", "[payout] WARNING: ledger entry maturing at" | ||
| 1916 | " height %d names neither a block hash nor a height --" | ||
| 1917 | " refusing to pay %zu recipient(s) that cannot be told" | ||
| 1918 | " apart from another round's\n", | ||
| 1919 | pp.matureAtHeight, pp.amounts.size()); | ||
| 711 | continue; | 1920 | continue; |
| 712 | LogPrint("payout", "[payout] block height %d matured -- queuing payout %s (%zu recipients)\n", | 1921 | } |
| 713 | pp.matureAtHeight, pp.id.c_str(), pp.amounts.size()); | ||
| 714 | ready.push_back(pp); | 1922 | ready.push_back(pp); |
| 715 | gQueuedPayouts.insert(pp.id); | ||
| 716 | } | 1923 | } |
| 717 | for (auto& pp : ready) | ||
| 718 | gPayoutExecQueue.push_back(pp); | ||
| 719 | } | 1924 | } |
| 1925 | // Outside the ledger mutex: publishing is I/O and the event loop is the | ||
| 1926 | // one thread that must never wait on the payout thread's disk. | ||
| 1927 | PublishPendingPayouts(ledgerSnap); | ||
| 1928 | |||
| 1929 | if (ready.empty()) | ||
| 1930 | return; | ||
| 1931 | |||
| 1932 | VersionedBlob payingSnap; | ||
| 1933 | { | ||
| 1934 | std::lock_guard<std::mutex> queue(gPayoutExecMutex); | ||
| 1935 | size_t added = 0; | ||
| 1936 | for (const PendingPayout& pp : ready) { | ||
| 1937 | const std::string id = PayoutIdForBlock(pp.blockHeight, pp.blockHash); | ||
| 1938 | if (gQueuedPayouts.count(id)) | ||
| 1939 | continue; // already exploded into gPaying and still in flight | ||
| 1940 | size_t n = 0; | ||
| 1941 | for (const auto& kv : pp.amounts) { | ||
| 1942 | // Upstream's per-recipient dedupe: a recipient already carrying | ||
| 1943 | // a txid is done, even though the entry as a whole is not. | ||
| 1944 | if (pp.paidTxids.count(kv.first)) | ||
| 1945 | continue; | ||
| 1946 | // The invariant, checked against the thing it is an invariant | ||
| 1947 | // of: at most one in-flight record per (round, address). The | ||
| 1948 | // round-level set above is an optimisation that can be wrong -- | ||
| 1949 | // it is memory only, it is rebuilt from gPaying at startup, and | ||
| 1950 | // a stale `ready` snapshot can race a round finishing -- | ||
| 1951 | // whereas gPaying is the list being protected. A second record | ||
| 1952 | // here is a second standing order to send the same money. | ||
| 1953 | if (PaymentInFlightLocked(pp.blockHeight, pp.blockHash, kv.first)) | ||
| 1954 | continue; | ||
| 1955 | PayingRecord r; | ||
| 1956 | r.id = ++gPayingNextId; | ||
| 1957 | r.blockHeight = pp.blockHeight; | ||
| 1958 | r.blockHash = pp.blockHash; | ||
| 1959 | r.matureAtHeight = pp.matureAtHeight; | ||
| 1960 | r.address = kv.first; | ||
| 1961 | r.amount = kv.second; | ||
| 1962 | gPaying.push_back(r); | ||
| 1963 | n++; | ||
| 1964 | } | ||
| 1965 | gQueuedPayouts.insert(id); | ||
| 1966 | added += n; | ||
| 1967 | LogPrint("payout", "[payout] block height %d matured -- queuing" | ||
| 1968 | " payout %s (%zu of %zu recipients still owed)\n", | ||
| 1969 | pp.matureAtHeight, id.c_str(), n, pp.amounts.size()); | ||
| 1970 | } | ||
| 1971 | if (added) payingSnap = SerializePayingLocked(); | ||
| 1972 | } | ||
| 1973 | PublishPaying(payingSnap); | ||
| 1974 | } | ||
| 1975 | |||
| 1976 | // Settle a payment: it is either done or abandoned, either way it stops being | ||
| 1977 | // in flight. Note this only retires the working record -- whether the miner is | ||
| 1978 | // still OWED the money is the ledger's business, and the ledger is updated | ||
| 1979 | // separately by RecordPayoutTxid(). | ||
| 1980 | // | ||
| 1981 | // Erases by record id, not by comparing fields. Value-matching was only an | ||
| 1982 | // identity for as long as no two records could be equal, and the restart bug | ||
| 1983 | // above produced records that were equal in every field: the first match then | ||
| 1984 | // absorbed every erase and every state update, and the second was never | ||
| 1985 | // touched again. The id names the slot the caller took its copy from, so the | ||
| 1986 | // update lands where it belongs whatever else is in the list. | ||
| 1987 | static void ForgetPayment(const PayingRecord& r) | ||
| 1988 | { | ||
| 1989 | VersionedBlob snap; | ||
| 1990 | { | ||
| 1991 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 1992 | for (auto it = gPaying.begin(); it != gPaying.end(); ++it) | ||
| 1993 | if (it->id == r.id) { gPaying.erase(it); break; } | ||
| 1994 | |||
| 1995 | // When the last record for a round is gone the round is no longer in | ||
| 1996 | // flight, so a later flush may explode it again if the ledger still | ||
| 1997 | // shows recipients owed. That is the intended behaviour and it is now | ||
| 1998 | // the only way a stalled payment ever leaves this list: a payment that | ||
| 1999 | // keeps failing is retried forever rather than given up on, so its | ||
| 2000 | // record stays here, and the flush that would re-explode it is stopped | ||
| 2001 | // by PaymentInFlightLocked. | ||
| 2002 | bool stillInFlight = false; | ||
| 2003 | for (const auto& e : gPaying) | ||
| 2004 | if (SamePayoutRound(e.blockHeight, e.blockHash, | ||
| 2005 | r.blockHeight, r.blockHash)) { | ||
| 2006 | stillInFlight = true; | ||
| 2007 | break; | ||
| 2008 | } | ||
| 2009 | if (!stillInFlight) | ||
| 2010 | gQueuedPayouts.erase(PayoutIdForBlock(r.blockHeight, r.blockHash)); | ||
| 2011 | |||
| 2012 | snap = SerializePayingLocked(); | ||
| 2013 | } | ||
| 2014 | PublishPaying(snap); | ||
| 2015 | } | ||
| 2016 | |||
| 2017 | // Persist an updated attempt count / backoff for a payment still owed. | ||
| 2018 | static void ReschedulePayment(const PayingRecord& r) | ||
| 2019 | { | ||
| 2020 | VersionedBlob snap; | ||
| 2021 | { | ||
| 2022 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 2023 | for (auto& e : gPaying) | ||
| 2024 | if (e.id == r.id) { | ||
| 2025 | e.retries = r.retries; | ||
| 2026 | e.deferrals = r.deferrals; | ||
| 2027 | e.retryAfter = r.retryAfter; | ||
| 2028 | // Carry the cleared probe flag back, or every retry re-scans | ||
| 2029 | // the whole wallet -- the cost this merge is specifically | ||
| 2030 | // removing. | ||
| 2031 | e.fProbeWallet = r.fProbeWallet; | ||
| 2032 | break; | ||
| 2033 | } | ||
| 2034 | snap = SerializePayingLocked(); | ||
| 2035 | } | ||
| 2036 | PublishPaying(snap); | ||
| 2037 | } | ||
| 2038 | |||
| 2039 | // Record that a recipient has been paid, in the ledger, which is the only | ||
| 2040 | // durable statement that the debt is settled. | ||
| 2041 | // | ||
| 2042 | // The file write happens with gPayoutLedgerMutex RELEASED. Upstream writes the | ||
| 2043 | // file inside the per-recipient loop with the mutex held, so every recipient | ||
| 2044 | // costs the event loop a full serialise+fsync it has to wait for in | ||
| 2045 | // FlushMaturePayouts. Ordering against the event loop's own writes is the | ||
| 2046 | // snapshot version's job, not the mutex's -- see PublishVersioned. | ||
| 2047 | static void RecordPayoutTxid(const PayingRecord& r, const std::string& txid) | ||
| 2048 | { | ||
| 2049 | VersionedBlob blob; | ||
| 2050 | bool complete = false; | ||
| 2051 | { | ||
| 2052 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 2053 | for (auto it = gPendingPayouts.begin(); it != gPendingPayouts.end(); ++it) { | ||
| 2054 | if (!SamePayoutRound(it->blockHeight, it->blockHash, | ||
| 2055 | r.blockHeight, r.blockHash)) | ||
| 2056 | continue; | ||
| 2057 | it->paidTxids[r.address] = txid; | ||
| 2058 | if (PayoutFullyPaid(*it)) { | ||
| 2059 | LogPrint("payout", "[payout] payout for block %d complete --" | ||
| 2060 | " removing from ledger\n", it->blockHeight); | ||
| 2061 | gPendingPayouts.erase(it); | ||
| 2062 | complete = true; | ||
| 2063 | } | ||
| 2064 | break; | ||
| 2065 | } | ||
| 2066 | blob = SerializePendingPayoutsLocked(); | ||
| 2067 | } | ||
| 2068 | PublishPendingPayouts(blob); | ||
| 2069 | // The round proof records a txid per address, so this is per recipient and | ||
| 2070 | // not only on completion. | ||
| 2071 | MarkPoolRoundPaid(PayoutIdForBlock(r.blockHeight, r.blockHash), r.address, txid); | ||
| 2072 | if (complete) | ||
| 2073 | LogPrint("payout", "[payout] block %d fully settled\n", r.blockHeight); | ||
| 2074 | } | ||
| 2075 | |||
| 2076 | // Write a recipient off. | ||
| 2077 | // | ||
| 2078 | // Only ever called for a state that is DECIDABLE -- currently one: the ledger | ||
| 2079 | // names an address that is not a valid address, so no transaction paying it | ||
| 2080 | // can be constructed by this or any other build. It is not called, and must | ||
| 2081 | // not be, because a payment has failed some number of times. "The wallet said | ||
| 2082 | // no N times" is not a decidable state, it is a description of how long we | ||
| 2083 | // have been asking; a debt written off on the strength of it is a debt the | ||
| 2084 | // operator keeps, and the arithmetic that decided WHY the wallet said no was | ||
| 2085 | // wrong for a year (see PayoutThreadFn). | ||
| 2086 | // | ||
| 2087 | // The amount is removed from the entry rather than marked paid, so the money | ||
| 2088 | // is not reported as sent. Once nothing is left owed the entry retires. The | ||
| 2089 | // round proof is told separately, because dropping the obligation from the | ||
| 2090 | // ledger while pool_rounds.json goes on publishing the address as owed with | ||
| 2091 | // "txid": null leaves the pool's two records of the same round permanently | ||
| 2092 | // disagreeing. | ||
| 2093 | static void ForfeitPayoutRecipient(const PayingRecord& r, const std::string& reason) | ||
| 2094 | { | ||
| 2095 | LogPrintAlways("payout", "[payout] writing off %s owed to %s for block %d: %s\n", | ||
| 2096 | FormatMoney(r.amount).c_str(), r.address.c_str(), r.blockHeight, | ||
| 2097 | reason.c_str()); | ||
| 2098 | VersionedBlob blob; | ||
| 2099 | { | ||
| 2100 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 2101 | for (auto it = gPendingPayouts.begin(); it != gPendingPayouts.end(); ++it) { | ||
| 2102 | if (!SamePayoutRound(it->blockHeight, it->blockHash, | ||
| 2103 | r.blockHeight, r.blockHash)) | ||
| 2104 | continue; | ||
| 2105 | it->amounts.erase(r.address); | ||
| 2106 | // PayoutFullyPaid() returns true for an empty amounts map -- there | ||
| 2107 | // is no recipient in it without a txid -- so the | ||
| 2108 | // `amounts.empty()` disjunct that used to be here decided nothing. | ||
| 2109 | if (PayoutFullyPaid(*it)) | ||
| 2110 | gPendingPayouts.erase(it); | ||
| 2111 | break; | ||
| 2112 | } | ||
| 2113 | blob = SerializePendingPayoutsLocked(); | ||
| 2114 | } | ||
| 2115 | PublishPendingPayouts(blob); | ||
| 2116 | MarkPoolRoundForfeited(PayoutIdForBlock(r.blockHeight, r.blockHash), | ||
| 2117 | r.address, reason); | ||
| 2118 | } | ||
| 2119 | |||
| 2120 | // What a failed send means for the payment's retry budget. | ||
| 2121 | // | ||
| 2122 | // A deferral is "the wallet's condition is temporarily wrong": wait, and do | ||
| 2123 | // not hold it against this payment. A fault is "the wallet is funded and | ||
| 2124 | // unlocked and still would not do it", which is worth logging loudly and | ||
| 2125 | // retrying with backoff. | ||
| 2126 | // | ||
| 2127 | // This used to be decided by re-reading GetBalance() after the failure and | ||
| 2128 | // comparing it against `amount + nTransactionFee + CENT`. Every term of that | ||
| 2129 | // was wrong: | ||
| 2130 | // | ||
| 2131 | // * nTransactionFee is 0 in production. It is only ever assigned from the | ||
| 2132 | // GUI preferences dialog (gui.cpp), which a headless pool node does not | ||
| 2133 | // have, so the whole reserve was the CENT. | ||
| 2134 | // | ||
| 2135 | // * The fee the reserve is for is GetMinFee(fDiscount=true), a STEP | ||
| 2136 | // function of the serialised size: 0 below 10,000 bytes, and | ||
| 2137 | // (1 + bytes/1000) * CENT at or above it -- so 11 * CENT at the step. | ||
| 2138 | // There is no required-fee value between 1 and 11 CENT. A one-CENT | ||
| 2139 | // reserve is therefore never the right number: below the step nothing | ||
| 2140 | // needs reserving, and at the step it is short by a factor of eleven. | ||
| 2141 | // | ||
| 2142 | // * A pool wallet close to broke selects many small inputs BECAUSE it is | ||
| 2143 | // close to broke, and ~56 signed P2PKH inputs is 10 KB. The trigger and | ||
| 2144 | // the failure are positively correlated: the guess is wrong precisely | ||
| 2145 | // when the pool is nearly out of money. | ||
| 2146 | // | ||
| 2147 | // Concretely, with a balance of amount + 0.05: the pre-send gate passes, | ||
| 2148 | // CreateTransaction builds a >10 KB transaction, the fee becomes 0.11, | ||
| 2149 | // SelectCoins(amount + 0.11) fails, SendMoney returns false -- and the old | ||
| 2150 | // arithmetic said (amount + 0.05) < (amount + 0.01) is false, so "funded", | ||
| 2151 | // so a fault. The condition is stable, so it recurred every 60 s, and 1440 of | ||
| 2152 | // them erased the debt with the coin still in the operator's wallet. | ||
| 2153 | // | ||
| 2154 | // So stop guessing. The wallet is asked to say what it did, and this only | ||
| 2155 | // classifies the answer. WALLET_SEND_INSUFFICIENT_FUNDS is coin selection | ||
| 2156 | // failing to cover amount plus whatever fee that size of transaction | ||
| 2157 | // required, whatever that turned out to be -- no reserve to get wrong. | ||
| 2158 | enum PayoutFailureClass { | ||
| 2159 | PAYOUT_FAILURE_DEFER = 0, // conditions are wrong; wait for them to change | ||
| 2160 | PAYOUT_FAILURE_FAULT // funded and unlocked, and it still would not go | ||
| 2161 | }; | ||
| 2162 | |||
| 2163 | static PayoutFailureClass ClassifyPayoutSendFailure(WalletSendResult result, | ||
| 2164 | bool fWalletLocked) | ||
| 2165 | { | ||
| 2166 | // A locked wallet cannot sign. Nothing about this payment is wrong. | ||
| 2167 | if (fWalletLocked) | ||
| 2168 | return PAYOUT_FAILURE_DEFER; | ||
| 2169 | if (result == WALLET_SEND_INSUFFICIENT_FUNDS) | ||
| 2170 | return PAYOUT_FAILURE_DEFER; | ||
| 2171 | return PAYOUT_FAILURE_FAULT; | ||
| 720 | } | 2172 | } |
| 721 | 2173 | ||
| 722 | // Payout execution thread: drains gPayoutExecQueue, calls SendMoney. | 2174 | // Payout execution thread: works through gPaying, calls SendMoney. |
| 723 | // Runs independently of the event loop so slow wallet operations | 2175 | // Runs independently of the event loop so slow wallet operations |
| 724 | // (SendMoney takes cs_main) never stall miner I/O. | 2176 | // (SendMoney takes cs_main) never stall miner I/O. |
| 725 | static void PayoutThreadFn(void*) | 2177 | static void PayoutThreadFn(void*) |
| @@ -728,103 +2180,270 @@ static void PayoutThreadFn(void*) | |||
| 728 | LogPrint("payout", "[payout] payout thread started\n"); | 2180 | LogPrint("payout", "[payout] payout thread started\n"); |
| 729 | 2181 | ||
| 730 | while (!fShutdown && gPoolRunning) { | 2182 | while (!fShutdown && gPoolRunning) { |
| 731 | std::vector<PendingPayout> batch; | 2183 | std::vector<PayingRecord> batch; |
| 732 | { | 2184 | { |
| 733 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | 2185 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); |
| 734 | batch.swap(gPayoutExecQueue); | 2186 | int64 now = GetTime(); |
| 2187 | for (const auto& r : gPaying) | ||
| 2188 | if (r.retryAfter <= now) | ||
| 2189 | batch.push_back(r); | ||
| 735 | } | 2190 | } |
| 736 | 2191 | ||
| 737 | for (auto& ppQueued : batch) { | 2192 | for (PayingRecord r : batch) { |
| 738 | LogPrint("payout", "[payout] executing payments for block matured at %d" | 2193 | const std::string id = PayoutIdForBlock(r.blockHeight, r.blockHash); |
| 739 | " (%zu miners)\n", ppQueued.matureAtHeight, ppQueued.amounts.size()); | 2194 | |
| 740 | bool fAbortBatch = false; | 2195 | // Re-check here and not only at maturity: a payment can sit in this |
| 741 | for (auto& kv : ppQueued.amounts) { | 2196 | // list for hours across retries, and the reorg that kills its |
| 2197 | // funding block can land at any point in that window. Upstream has | ||
| 2198 | // no equivalent -- it decides once, when the entry matures. | ||
| 2199 | if (!PayoutBlockStillOnChain(r.blockHash)) { | ||
| 2200 | LogPrintAlways("payout", "[payout] block %d (%s) orphaned while queued" | ||
| 2201 | " -- dropping %s to %s\n", | ||
| 2202 | r.blockHeight, r.blockHash.ToString().substr(0, 14).c_str(), | ||
| 2203 | FormatMoney(r.amount).c_str(), r.address.c_str()); | ||
| 2204 | ForgetPayment(r); | ||
| 2205 | continue; | ||
| 2206 | } | ||
| 2207 | |||
| 2208 | // Upstream's per-recipient dedupe, against the ledger rather than | ||
| 2209 | // against the batch: if a txid is already recorded for this | ||
| 2210 | // address, the money went out and nothing more is owed. | ||
| 2211 | { | ||
| 742 | bool alreadyPaid = false; | 2212 | bool alreadyPaid = false; |
| 743 | { | 2213 | { |
| 744 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | 2214 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); |
| 745 | for (const PendingPayout& pp : gPendingPayouts) | 2215 | for (const PendingPayout& pp : gPendingPayouts) |
| 746 | if (pp.id == ppQueued.id && pp.paidTxids.count(kv.first)) | 2216 | if (SamePayoutRound(pp.blockHeight, pp.blockHash, |
| 2217 | r.blockHeight, r.blockHash) | ||
| 2218 | && pp.paidTxids.count(r.address)) { | ||
| 747 | alreadyPaid = true; | 2219 | alreadyPaid = true; |
| 2220 | break; | ||
| 2221 | } | ||
| 748 | } | 2222 | } |
| 749 | if (alreadyPaid) | 2223 | if (alreadyPaid) { |
| 2224 | ForgetPayment(r); | ||
| 750 | continue; | 2225 | continue; |
| 751 | |||
| 752 | uint160 h160; | ||
| 753 | if (!AddressToHash160(kv.first, h160)) { | ||
| 754 | // Should not happen -- bad addresses are filtered in QueuePayouts | ||
| 755 | LogPrint("payout", "[payout] SKIPPED bad address '%s'\n", | ||
| 756 | kv.first.c_str()); | ||
| 757 | continue; | ||
| 758 | } | ||
| 759 | CScript sc; | ||
| 760 | sc << OP_DUP << OP_HASH160 << h160 << OP_EQUALVERIFY << OP_CHECKSIG; | ||
| 761 | CWalletTx wtx; | ||
| 762 | wtx.mapValue["comment"] = strprintf("Pool payout height %d", ppQueued.matureAtHeight); | ||
| 763 | wtx.mapValue["pool_payout_id"] = ppQueued.id; | ||
| 764 | wtx.mapValue["pool_payout_address"] = kv.first; | ||
| 765 | wtx.mapValue["pool_payout_amount"] = strprintf("%lld", kv.second); | ||
| 766 | |||
| 767 | std::string txid; | ||
| 768 | if (FindRecordedPoolPayout(ppQueued.id, kv.first, kv.second, txid)) | ||
| 769 | { | ||
| 770 | LogPrint("payout", "[payout] recovered recorded payout %s to %s" | ||
| 771 | " from wallet tx %s\n", | ||
| 772 | FormatMoney(kv.second).c_str(), kv.first.c_str(), txid.c_str()); | ||
| 773 | MarkPoolRoundPaid(ppQueued.id, kv.first, txid); | ||
| 774 | } | ||
| 775 | else if (SendMoney(sc, kv.second, wtx)) | ||
| 776 | { | ||
| 777 | txid = wtx.GetHash().GetHex(); | ||
| 778 | LogPrint("payout", "[payout] paid %s to %s\n", | ||
| 779 | FormatMoney(kv.second).c_str(), kv.first.c_str()); | ||
| 780 | MarkPoolRoundPaid(ppQueued.id, kv.first, txid); | ||
| 781 | } | ||
| 782 | else | ||
| 783 | { | ||
| 784 | LogPrint("payout", "[payout] FAILED to pay %s to %s" | ||
| 785 | " (insufficient funds or wallet locked?)\n", | ||
| 786 | FormatMoney(kv.second).c_str(), kv.first.c_str()); | ||
| 787 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 788 | for (PendingPayout& pp : gPendingPayouts) | ||
| 789 | if (pp.id == ppQueued.id) | ||
| 790 | pp.nextAttempt = GetTime() + 60; | ||
| 791 | SavePendingPayoutsLocked(); | ||
| 792 | fAbortBatch = true; | ||
| 793 | break; | ||
| 794 | } | ||
| 795 | |||
| 796 | { | ||
| 797 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 798 | for (PendingPayout& pp : gPendingPayouts) { | ||
| 799 | if (pp.id != ppQueued.id) | ||
| 800 | continue; | ||
| 801 | pp.paidTxids[kv.first] = txid; | ||
| 802 | pp.nextAttempt = 0; | ||
| 803 | SavePendingPayoutsLocked(); | ||
| 804 | break; | ||
| 805 | } | ||
| 806 | } | 2226 | } |
| 807 | } | 2227 | } |
| 808 | 2228 | ||
| 809 | { | 2229 | uint160 h160; |
| 810 | std::lock_guard<std::mutex> ledger(gPayoutLedgerMutex); | 2230 | if (!AddressToHash160(r.address, h160)) { |
| 811 | for (auto it = gPendingPayouts.begin(); it != gPendingPayouts.end(); ) { | 2231 | // Should not happen -- bad addresses are filtered in |
| 812 | if (it->id == ppQueued.id && PayoutFullyPaid(*it)) { | 2232 | // QueuePayouts. If one is here anyway it is decidable and |
| 813 | LogPrint("payout", "[payout] payout %s complete -- removing from ledger\n", | 2233 | // permanent: there is no transaction that pays a string which |
| 814 | it->id.c_str()); | 2234 | // is not an address, so retrying is not waiting for anything. |
| 815 | it = gPendingPayouts.erase(it); | 2235 | // |
| 816 | SavePendingPayoutsLocked(); | 2236 | // This used to only ForgetPayment(), which drops the in-flight |
| 2237 | // record and leaves the ledger entry claiming the debt and | ||
| 2238 | // pool_rounds.json advertising the recipient as unpaid | ||
| 2239 | // forever, with the next flush re-exploding it. Write it off | ||
| 2240 | // properly and say why. | ||
| 2241 | const std::string reason = | ||
| 2242 | strprintf("'%s' is not a valid address, so no transaction" | ||
| 2243 | " can pay it", r.address.c_str()); | ||
| 2244 | LogPrintAlways("payout", "[payout] SKIPPED bad address '%s' --" | ||
| 2245 | " %s owed for block %d cannot be paid\n", | ||
| 2246 | r.address.c_str(), FormatMoney(r.amount).c_str(), | ||
| 2247 | r.blockHeight); | ||
| 2248 | ForfeitPayoutRecipient(r, reason); | ||
| 2249 | ForgetPayment(r); | ||
| 2250 | continue; | ||
| 2251 | } | ||
| 2252 | CScript sc; | ||
| 2253 | sc << OP_DUP << OP_HASH160 << h160 << OP_EQUALVERIFY << OP_CHECKSIG; | ||
| 2254 | |||
| 2255 | // Did a previous process send this and die before recording it? | ||
| 2256 | // Only asked once per record -- see PayingRecord::fProbeWallet. | ||
| 2257 | if (r.fProbeWallet) { | ||
| 2258 | std::string recovered; | ||
| 2259 | const std::string claimKey = id + "|" + r.address; | ||
| 2260 | std::set<std::string> excluded; | ||
| 2261 | CollectForeignPayoutTxids(r.blockHeight, r.blockHash, r.address, | ||
| 2262 | claimKey, excluded); | ||
| 2263 | // A round with a published proof was created by a build whose | ||
| 2264 | // every payment names its round exactly, so only structured | ||
| 2265 | // evidence can settle it -- a comment match would be another | ||
| 2266 | // round's payment colliding on the height. | ||
| 2267 | const bool fAllowLegacy = !PoolRoundHasProof(id); | ||
| 2268 | r.fProbeWallet = false; | ||
| 2269 | PayoutProbeResult probe = FindRecordedPoolPayout( | ||
| 2270 | id, r.blockHeight, r.blockHash, r.address, r.amount, | ||
| 2271 | fAllowLegacy, excluded, recovered); | ||
| 2272 | if (probe != PAYOUT_PROBE_NONE) { | ||
| 2273 | if (probe == PAYOUT_PROBE_LEGACY) { | ||
| 2274 | // Loud on purpose: this is the upgrade path, where the | ||
| 2275 | // only evidence a payment went out is a comment written | ||
| 2276 | // by a binary that is no longer running. | ||
| 2277 | LogPrint("payout", "[payout] %s to %s was already paid by" | ||
| 2278 | " a pre-upgrade build (wallet tx %s carries the" | ||
| 2279 | " payout comment for height %d and an output of" | ||
| 2280 | " exactly this amount, and no structured payout" | ||
| 2281 | " metadata) -- not resending\n", | ||
| 2282 | FormatMoney(r.amount).c_str(), r.address.c_str(), | ||
| 2283 | recovered.c_str(), r.blockHeight); | ||
| 2284 | ClaimLegacyPayoutTxid(recovered, claimKey); | ||
| 817 | } else { | 2285 | } else { |
| 818 | ++it; | 2286 | LogPrint("payout", "[payout] recovered recorded payout %s to %s" |
| 2287 | " from wallet tx %s -- not resending\n", | ||
| 2288 | FormatMoney(r.amount).c_str(), r.address.c_str(), | ||
| 2289 | recovered.c_str()); | ||
| 819 | } | 2290 | } |
| 2291 | RecordPayoutTxid(r, recovered); | ||
| 2292 | ForgetPayment(r); | ||
| 2293 | continue; | ||
| 820 | } | 2294 | } |
| 821 | } | 2295 | } |
| 822 | { | 2296 | |
| 823 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | 2297 | // Waiting for coin to mature is not a failure, so it must not |
| 824 | gQueuedPayouts.erase(ppQueued.id); | 2298 | // consume the retry budget. Burning a retry every 60s means a |
| 2299 | // payment the pool fully intends to honour can hit GIVING UP | ||
| 2300 | // 24h later purely because the subsidy funding it had not | ||
| 2301 | // matured yet -- the miner is then silently never paid. | ||
| 2302 | // | ||
| 2303 | // The comparison includes the fee. It used to be against the | ||
| 2304 | // amount alone, so a balance anywhere between amount and | ||
| 2305 | // amount+fee sailed past this gate, failed in SendMoney, and was | ||
| 2306 | // counted as a send FAILURE rather than a wait -- 1440 of those is | ||
| 2307 | // 24 hours, and the recipient was erased with the money left in | ||
| 2308 | // the operator's wallet and nothing on disk saying it was owed. | ||
| 2309 | // | ||
| 2310 | // nTransactionFee is the part of the fee that can be known before | ||
| 2311 | // the transaction exists, and in production it is 0 -- it is only | ||
| 2312 | // ever assigned from the GUI preferences dialog, which a pool node | ||
| 2313 | // does not have -- so including it here changes nothing on this | ||
| 2314 | // pool and is kept only because a GUI operator can set it. The | ||
| 2315 | // rest is GetMinFee(), a function of the serialised size and | ||
| 2316 | // therefore of how many inputs SelectCoins ends up picking, which | ||
| 2317 | // cannot be predicted from here -- so this gate deliberately does | ||
| 2318 | // NOT try to reserve it. Refusing to attempt a payment the wallet | ||
| 2319 | // could actually have made would be its own way of not paying a | ||
| 2320 | // miner. That residual is handled after the fact, by asking the | ||
| 2321 | // wallet why it refused -- see ClassifyPayoutSendFailure. | ||
| 2322 | int64 nSpendable = GetBalance(); | ||
| 2323 | if (nSpendable < r.amount + nTransactionFee) { | ||
| 2324 | r.deferrals++; | ||
| 2325 | r.retryAfter = GetTime() + 60; | ||
| 2326 | // Hourly, not every minute: a dry pool would otherwise | ||
| 2327 | // write one line per payment per minute forever. | ||
| 2328 | if (r.deferrals == 1 || r.deferrals % 60 == 0) | ||
| 2329 | LogPrint("payout", "[payout] deferring %s to %s -- only %s" | ||
| 2330 | " spendable, need %s with fee (waiting %d min so far)\n", | ||
| 2331 | FormatMoney(r.amount).c_str(), r.address.c_str(), | ||
| 2332 | FormatMoney(nSpendable).c_str(), | ||
| 2333 | FormatMoney(r.amount + nTransactionFee).c_str(), | ||
| 2334 | r.deferrals); | ||
| 2335 | ReschedulePayment(r); | ||
| 2336 | continue; | ||
| 825 | } | 2337 | } |
| 826 | if (fAbortBatch) | 2338 | |
| 2339 | // Likewise a locked wallet: it cannot sign, so every attempt fails | ||
| 2340 | // for a reason that has nothing to do with this payment being | ||
| 2341 | // payable. Left to the retry budget, an operator who locks the | ||
| 2342 | // wallet overnight comes back to erased debts. | ||
| 2343 | if (IsWalletLocked()) { | ||
| 2344 | r.deferrals++; | ||
| 2345 | r.retryAfter = GetTime() + 60; | ||
| 2346 | if (r.deferrals == 1 || r.deferrals % 60 == 0) | ||
| 2347 | LogPrint("payout", "[payout] deferring %s to %s -- wallet is" | ||
| 2348 | " locked (waiting %d min so far)\n", | ||
| 2349 | FormatMoney(r.amount).c_str(), r.address.c_str(), | ||
| 2350 | r.deferrals); | ||
| 2351 | ReschedulePayment(r); | ||
| 827 | continue; | 2352 | continue; |
| 2353 | } | ||
| 2354 | |||
| 2355 | CWalletTx wtx; | ||
| 2356 | wtx.mapValue["comment"] = PayoutComment(r.blockHeight, r.blockHash); | ||
| 2357 | // Upstream's structured metadata. This is what makes the wallet | ||
| 2358 | // probe above possible at all -- the old comment-only form could | ||
| 2359 | // not distinguish two rounds paying the same address the same | ||
| 2360 | // amount. Keyed by the hash-derived id, so a reorg cannot make one | ||
| 2361 | // round's transaction answer for another's. | ||
| 2362 | wtx.mapValue["pool_payout_id"] = id; | ||
| 2363 | wtx.mapValue["pool_payout_address"] = r.address; | ||
| 2364 | wtx.mapValue["pool_payout_amount"] = strprintf("%lld", r.amount); | ||
| 2365 | // The full hash, not only the id's sixteen-digit prefix: the | ||
| 2366 | // prefix of a proof-of-work hash is where the zeros live, and | ||
| 2367 | // the probe needs to tell twin rounds at one height apart. | ||
| 2368 | if (r.blockHash != 0) | ||
| 2369 | wtx.mapValue["pool_payout_block"] = r.blockHash.ToString(); | ||
| 2370 | |||
| 2371 | SendMoneyOutcome outcome; | ||
| 2372 | if (SendMoney(sc, r.amount, wtx, outcome)) { | ||
| 2373 | // The wallet holds the transaction before this returns, so a | ||
| 2374 | // crash between here and the ledger write is recoverable: the | ||
| 2375 | // probe above finds it by id+address+amount and does not | ||
| 2376 | // resend. Ledger first, in-flight record second -- the debt is | ||
| 2377 | // described by at least one of them at every instant. | ||
| 2378 | RecordPayoutTxid(r, wtx.GetHash().GetHex()); | ||
| 2379 | ForgetPayment(r); | ||
| 2380 | LogPrintAlways("payout", "[payout] paid %s to %s (tx %s)\n", | ||
| 2381 | FormatMoney(r.amount).c_str(), r.address.c_str(), | ||
| 2382 | wtx.GetHash().ToString().substr(0, 12).c_str()); | ||
| 2383 | } else { | ||
| 2384 | // Why did it fail? The answer used to decide whether the | ||
| 2385 | // payment eventually got erased, and it was decided by | ||
| 2386 | // guessing from a balance snapshot -- see | ||
| 2387 | // ClassifyPayoutSendFailure for the arithmetic and what it | ||
| 2388 | // cost. The wallet now says what it did, and nothing here | ||
| 2389 | // erases anything. | ||
| 2390 | // | ||
| 2391 | // The lock state is re-read rather than trusted from before | ||
| 2392 | // the attempt: SendMoney takes cs_main and can block for a | ||
| 2393 | // long time, so it may have moved underneath us. The funding | ||
| 2394 | // answer does not need re-reading -- it is a statement about | ||
| 2395 | // the attempt that was actually made. | ||
| 2396 | bool fLocked = IsWalletLocked(); | ||
| 2397 | int64 nAfter = GetBalance(); | ||
| 2398 | |||
| 2399 | if (ClassifyPayoutSendFailure(outcome.result, fLocked) | ||
| 2400 | == PAYOUT_FAILURE_DEFER) { | ||
| 2401 | // Conditions are temporarily wrong. Wait. | ||
| 2402 | r.deferrals++; | ||
| 2403 | r.retryAfter = GetTime() + 60; | ||
| 2404 | if (r.deferrals == 1 || r.deferrals % 60 == 0) | ||
| 2405 | LogPrint("payout", "[payout] deferring %s to %s -- send" | ||
| 2406 | " failed with %s (%s spendable; the wallet" | ||
| 2407 | " needed a fee of %s for a transaction this" | ||
| 2408 | " size); waiting %d min so far\n", | ||
| 2409 | FormatMoney(r.amount).c_str(), r.address.c_str(), | ||
| 2410 | fLocked ? "the wallet locked" : "too little coin", | ||
| 2411 | FormatMoney(nAfter).c_str(), | ||
| 2412 | FormatMoney(outcome.nFeeRequired).c_str(), | ||
| 2413 | r.deferrals); | ||
| 2414 | ReschedulePayment(r); | ||
| 2415 | } else { | ||
| 2416 | // Funded, unlocked, block on the main chain, and the | ||
| 2417 | // wallet still would not build, sign or accept the | ||
| 2418 | // transaction. That is worth shouting about; it is not | ||
| 2419 | // grounds to keep the money. | ||
| 2420 | // | ||
| 2421 | // There is deliberately no give-up branch. It used to | ||
| 2422 | // forfeit the debt after 1440 of these, which is not a | ||
| 2423 | // decidable state -- it is a count of how long we have | ||
| 2424 | // been asking, and it was reached by misclassifying a | ||
| 2425 | // funding shortfall as a fault. A debt that is never | ||
| 2426 | // written off cannot be written off wrongly. The payment | ||
| 2427 | // stays in flight, keeps its ledger entry, keeps being | ||
| 2428 | // reported in pool_status.json as owed, and keeps | ||
| 2429 | // retrying once a minute until the operator or the wallet | ||
| 2430 | // makes it possible. | ||
| 2431 | r.retries++; | ||
| 2432 | r.retryAfter = GetTime() + 60; | ||
| 2433 | ReschedulePayment(r); | ||
| 2434 | // Every minute at first, then hourly: a fault that is not | ||
| 2435 | // going away must not turn the log into one line a minute | ||
| 2436 | // forever, and must not go quiet either. | ||
| 2437 | if (r.retries <= 5 || r.retries % 60 == 0) | ||
| 2438 | LogPrintAlways("payout", "[payout] FAILED to pay %s to %s" | ||
| 2439 | " (%s spendable, wallet unlocked, block %d on" | ||
| 2440 | " the main chain) -- attempt %d, retrying in" | ||
| 2441 | " 60s; this debt stands until it is paid\n", | ||
| 2442 | FormatMoney(r.amount).c_str(), r.address.c_str(), | ||
| 2443 | FormatMoney(nAfter).c_str(), r.blockHeight, | ||
| 2444 | r.retries); | ||
| 2445 | } | ||
| 2446 | } | ||
| 828 | } | 2447 | } |
| 829 | 2448 | ||
| 830 | Sleep(500); // check for new work every 500ms | 2449 | Sleep(500); // check for new work every 500ms |
| @@ -877,7 +2496,22 @@ static std::string PoolStatusFilePath() | |||
| 877 | return GetAppDir() + "/pool_status.json"; | 2496 | return GetAppDir() + "/pool_status.json"; |
| 878 | } | 2497 | } |
| 879 | 2498 | ||
| 880 | static void WritePoolStatusJsonLocked() | 2499 | // Serialise the operator status file. Caller must hold gStatsMutex (this reads |
| 2500 | // gStats / gWorkerStats / gPayoutViews); gPoolRoundsMutex is taken and | ||
| 2501 | // released inside. | ||
| 2502 | // | ||
| 2503 | // Split from the write for the same reason as the three ledgers, and for a | ||
| 2504 | // worse instance of the same problem. This used to be called from RefreshStats | ||
| 2505 | // with gPayoutLedgerMutex held at function scope, and once the ledger writes | ||
| 2506 | // gained fsync() that call became two fsyncs -- one on the file, one on its | ||
| 2507 | // directory -- performed by the event loop, the thread that owns every miner's | ||
| 2508 | // socket, with the mutex the payout thread needs in its hand. Measured on the | ||
| 2509 | // build host (ext4 on LVM on SSD): 3.78 ms per publish, every 10 seconds, and | ||
| 2510 | // unbounded on a shared virtual disk under load. | ||
| 2511 | // | ||
| 2512 | // Nothing here needs the ledger mutex at all: the pending-payout rows were | ||
| 2513 | // already copied into gPayoutViews under it, further up RefreshStats. | ||
| 2514 | static std::string SerializePoolStatusLocked() | ||
| 881 | { | 2515 | { |
| 882 | nlohmann::json j; | 2516 | nlohmann::json j; |
| 883 | j["schema"] = "bitflash-pool-status-1"; | 2517 | j["schema"] = "bitflash-pool-status-1"; |
| @@ -933,10 +2567,19 @@ static void WritePoolStatusJsonLocked() | |||
| 933 | size_t start = gPoolRounds.size() > 10 ? gPoolRounds.size() - 10 : 0; | 2567 | size_t start = gPoolRounds.size() > 10 ? gPoolRounds.size() - 10 : 0; |
| 934 | for (size_t i = start; i < gPoolRounds.size(); i++) { | 2568 | for (size_t i = start; i < gPoolRounds.size(); i++) { |
| 935 | const PoolRoundProof& r = gPoolRounds[i]; | 2569 | const PoolRoundProof& r = gPoolRounds[i]; |
| 2570 | // A forfeited recipient is not "unpaid and pending" -- the ledger | ||
| 2571 | // has already dropped the obligation. Counting it as unpaid is | ||
| 2572 | // what made the round proof and the ledger disagree for good. | ||
| 936 | int unpaid = 0; | 2573 | int unpaid = 0; |
| 937 | for (const auto& kv : r.payouts) | 2574 | int forfeited = 0; |
| 938 | if (r.paidTxids.count(kv.first) == 0) | 2575 | for (const auto& kv : r.payouts) { |
| 2576 | if (r.paidTxids.count(kv.first)) | ||
| 2577 | continue; | ||
| 2578 | if (r.forfeited.count(kv.first)) | ||
| 2579 | forfeited++; | ||
| 2580 | else | ||
| 939 | unpaid++; | 2581 | unpaid++; |
| 2582 | } | ||
| 940 | j["recentRounds"].push_back({ | 2583 | j["recentRounds"].push_back({ |
| 941 | {"id", r.id}, | 2584 | {"id", r.id}, |
| 942 | {"blockHeight", r.blockHeight}, | 2585 | {"blockHeight", r.blockHeight}, |
| @@ -945,30 +2588,34 @@ static void WritePoolStatusJsonLocked() | |||
| 945 | {"totalShares", r.totalShares}, | 2588 | {"totalShares", r.totalShares}, |
| 946 | {"payouts", (int)r.payouts.size()}, | 2589 | {"payouts", (int)r.payouts.size()}, |
| 947 | {"unpaidPayouts", unpaid}, | 2590 | {"unpaidPayouts", unpaid}, |
| 2591 | {"forfeitedPayouts", forfeited}, | ||
| 948 | {"coinbaseValueBtf", FormatMoney(r.coinbaseValue)}, | 2592 | {"coinbaseValueBtf", FormatMoney(r.coinbaseValue)}, |
| 949 | {"minerRewardBtf", FormatMoney(r.minerReward)} | 2593 | {"minerRewardBtf", FormatMoney(r.minerReward)} |
| 950 | }); | 2594 | }); |
| 951 | } | 2595 | } |
| 952 | } | 2596 | } |
| 953 | 2597 | ||
| 954 | std::string path = PoolStatusFilePath(); | 2598 | return j.dump(2); |
| 955 | std::string tmp = path + ".tmp"; | 2599 | } |
| 956 | FILE* f = fopen(tmp.c_str(), "w"); | 2600 | |
| 957 | if (!f) { | 2601 | // Publish the operator status file. Call with every payout mutex released. |
| 958 | LogPrint("pool", "[pool] WARNING: could not write status file %s\n", tmp.c_str()); | 2602 | // |
| 959 | return; | 2603 | // Still no version guard and no write mutex, deliberately, and the split does |
| 960 | } | 2604 | // not change the argument: RefreshStats remains the only caller of either |
| 961 | std::string s = j.dump(2); | 2605 | // half, in production it runs only on the event loop, and it now calls this |
| 962 | fwrite(s.c_str(), 1, s.size(), f); | 2606 | // after leaving the scope that serialised -- so the serialise and the write |
| 963 | if (fclose(f) != 0) { | 2607 | // stay strictly ordered on one thread and pool_status.json still has a single |
| 964 | LogPrint("pool", "[pool] WARNING: could not close status file %s\n", tmp.c_str()); | 2608 | // writer by construction. It shares WriteFileAtomic so the durability rules |
| 965 | return; | 2609 | // stay in one place. |
| 966 | } | 2610 | // |
| 967 | #ifdef _WIN32 | 2611 | // "In production" is doing real work in that sentence. The payout-ledger |
| 968 | remove(path.c_str()); | 2612 | // self-test also calls RefreshStats, from one thread at a time and with the |
| 969 | #endif | 2613 | // pool not running, which is why it is still a single writer there too. A |
| 970 | if (rename(tmp.c_str(), path.c_str()) != 0) | 2614 | // second concurrent caller anywhere would invalidate this paragraph and want |
| 971 | LogPrint("pool", "[pool] WARNING: could not replace status file %s\n", path.c_str()); | 2615 | // the same versioned publish the ledgers use. |
| 2616 | static void PublishPoolStatus(const std::string& blob) | ||
| 2617 | { | ||
| 2618 | WriteFileAtomic(PoolStatusFilePath(), blob, "pool status"); | ||
| 972 | } | 2619 | } |
| 973 | 2620 | ||
| 974 | // --------------------------------------------------------------------------- | 2621 | // --------------------------------------------------------------------------- |
| @@ -996,9 +2643,35 @@ static const double VARDIFF_MAX = 1e12; | |||
| 996 | static const int64 VARDIFF_STALL_SECS = 45; // 3x share target -- reacts faster without false-triggering on normal variance | 2643 | static const int64 VARDIFF_STALL_SECS = 45; // 3x share target -- reacts faster without false-triggering on normal variance |
| 997 | static const double VARDIFF_STALL_CUT = 4.0; // divide difficulty by this much | 2644 | static const double VARDIFF_STALL_CUT = 4.0; // divide difficulty by this much |
| 998 | 2645 | ||
| 2646 | // #7: m->hashRate is an EWMA sampled only when a share arrives, so a miner | ||
| 2647 | // that stops hashing keeps its last estimate forever and RefreshStats sums | ||
| 2648 | // it into the announced pool total for as long as the connection sits open | ||
| 2649 | // -- the published pool hashrate ran several times over ground truth | ||
| 2650 | // because of exactly this. MinerReportedHashRate() below applies the | ||
| 2651 | // policy: a miner gets a grace of HASHRATE_IDLE_MISSES expected share | ||
| 2652 | // intervals (expected interval = expected hashes per share at its | ||
| 2653 | // difficulty, divided by the estimate itself) with no share, after which | ||
| 2654 | // the figure we report falls off with a half-life of one more such | ||
| 2655 | // interval. The interval is clamped to [HASHRATE_MIN_INTERVAL, | ||
| 2656 | // HASHRATE_MAX_INTERVAL] so a fast miner at the vardiff floor (expected | ||
| 2657 | // interval of milliseconds) is not punished for ordinary share-delivery | ||
| 2658 | // jitter, and a very slow miner's grace does not stretch past the point | ||
| 2659 | // where its estimate means anything. At the vardiff target of one share | ||
| 2660 | // per 15s that is a minute of grace, half the rate reported 75s in, and | ||
| 2661 | // nothing reported after about seventeen minutes of silence. | ||
| 2662 | static const double HASHRATE_IDLE_MISSES = 4.0; // grace, in expected share intervals | ||
| 2663 | static const double HASHRATE_MIN_INTERVAL = 1.0; // seconds; floor for the expected interval | ||
| 2664 | static const double HASHRATE_MAX_INTERVAL = 3600.0; // seconds; ceiling for the expected interval | ||
| 2665 | // Half-lives past the grace beyond which we report a flat zero. 2^-64 of the | ||
| 2666 | // original estimate is not a number anyone needs, and stopping here keeps | ||
| 2667 | // pow() from walking down into the denormals for a miner that has been quiet | ||
| 2668 | // for hours. | ||
| 2669 | static const double HASHRATE_DECAY_CUTOFF = 64.0; | ||
| 2670 | |||
| 999 | struct Miner { | 2671 | struct Miner { |
| 1000 | btf_socket_t fd; | 2672 | btf_socket_t fd; |
| 1001 | bool authorised = false; | 2673 | bool authorised = false; |
| 2674 | bool subscribed = false; // completed mining.subscribe (#19 census) | ||
| 1002 | std::string address; | 2675 | std::string address; |
| 1003 | std::string worker; | 2676 | std::string worker; |
| 1004 | std::string readBuf; | 2677 | std::string readBuf; |
| @@ -1022,11 +2695,80 @@ struct Miner { | |||
| 1022 | int64 lastShareTime = 0; // GetTime() of the previous accepted share | 2695 | int64 lastShareTime = 0; // GetTime() of the previous accepted share |
| 1023 | double hashRate = 0.0; // smoothed H/s estimate | 2696 | double hashRate = 0.0; // smoothed H/s estimate |
| 1024 | 2697 | ||
| 2698 | // Replay guard for mining.submit (#8): nonces already credited on this | ||
| 2699 | // connection for the job it is currently working. Keyed to the job id in | ||
| 2700 | // seenNoncesJob and cleared by SendJob whenever that id changes, so a | ||
| 2701 | // resubmission under a NEW job is never turned away, while a replayed | ||
| 2702 | // (job_id, nonce) pair under the same job is refused before it can be | ||
| 2703 | // credited twice into roundShareCount -- the figure the payout split | ||
| 2704 | // divides by. | ||
| 2705 | std::set<uint32_t> seenNonces; | ||
| 2706 | std::string seenNoncesJob; | ||
| 2707 | |||
| 1025 | explicit Miner(btf_socket_t f) | 2708 | explicit Miner(btf_socket_t f) |
| 1026 | : fd(f), connectTime(GetTime()), | 2709 | : fd(f), connectTime(GetTime()), |
| 1027 | extranonce2(gNextExtranonce2.fetch_add(1)) {} | 2710 | extranonce2(gNextExtranonce2.fetch_add(1)) {} |
| 1028 | }; | 2711 | }; |
| 1029 | 2712 | ||
| 2713 | // What we publish as a miner's H/s: the last EWMA sample, faded out if shares | ||
| 2714 | // have stopped arriving (#7, policy at HASHRATE_IDLE_MISSES). | ||
| 2715 | // | ||
| 2716 | // This reads m->hashRate and never writes it. That is the whole design. The | ||
| 2717 | // first attempt decayed the stored estimate in place from the event loop's | ||
| 2718 | // sweep, which put a running accumulator behind a figure that is only ever | ||
| 2719 | // displayed, and the accumulator misbehaved in two ways: | ||
| 2720 | // | ||
| 2721 | // - `expected` was recomputed from the already-decayed estimate on every | ||
| 2722 | // pass, so each decay raised `expected`, which raised `grace`, which is | ||
| 2723 | // the very thing gating the next decay. `grace` simply chased `silent` | ||
| 2724 | // upward and the reported rate settled into a 1/t crawl instead of the | ||
| 2725 | // half-life curve the comment above promises -- 291x too high at ten | ||
| 2726 | // half-lives past the grace. Issue #7 is about the pool over-reporting; | ||
| 2727 | // that under-corrected it for hours. | ||
| 2728 | // - Once `expected` reached HASHRATE_MAX_INTERVAL it could not rise any | ||
| 2729 | // further, the feedback that had been gating each pass was gone, and the | ||
| 2730 | // decay began compounding once a second against the total elapsed | ||
| 2731 | // silence -- taking a miner from 0.4% of its baseline to 1e-11 of it in | ||
| 2732 | // the next hundred seconds, four hours in, for no reason a reader of | ||
| 2733 | // either the code or the announced number could see. | ||
| 2734 | // | ||
| 2735 | // Computing the value instead of accumulating it removes the failure mode | ||
| 2736 | // rather than tuning it: with no stored decay state there is nothing for a | ||
| 2737 | // tick rate, a missed pass or a duplicated pass to perturb, and the answer | ||
| 2738 | // depends only on how long the miner has actually been quiet. It also keeps | ||
| 2739 | // the fade out of the estimator -- the EWMA on the share path still blends | ||
| 2740 | // against the value it last computed, not against a faded copy of it, so a | ||
| 2741 | // miner that resumes recovers on exactly the curve it would have without any | ||
| 2742 | // of this. | ||
| 2743 | // | ||
| 2744 | // `now` is a parameter rather than a GetTime() call so that one refresh fades | ||
| 2745 | // every miner to the same instant, and so the policy is directly testable. | ||
| 2746 | static double MinerReportedHashRate(const Miner* m, int64 now) | ||
| 2747 | { | ||
| 2748 | // Nothing sampled yet: a fresh connection, or one that has found a single | ||
| 2749 | // share and so has no interval to have been timed over. Nothing to fade, | ||
| 2750 | // and nothing to divide by below. | ||
| 2751 | if (m->hashRate <= 0.0 || m->lastShareTime <= 0) return m->hashRate; | ||
| 2752 | |||
| 2753 | double expected = ExpectedHashesForDifficulty(m->difficulty) / m->hashRate; | ||
| 2754 | // Written so that a NaN would land on the floor rather than sail through: | ||
| 2755 | // the clamp is what guarantees `expected` is finite and strictly positive | ||
| 2756 | // below, whatever the division produced. | ||
| 2757 | if (!(expected > HASHRATE_MIN_INTERVAL)) expected = HASHRATE_MIN_INTERVAL; | ||
| 2758 | if (expected > HASHRATE_MAX_INTERVAL) expected = HASHRATE_MAX_INTERVAL; | ||
| 2759 | |||
| 2760 | double silent = (double)(now - m->lastShareTime); | ||
| 2761 | double grace = HASHRATE_IDLE_MISSES * expected; | ||
| 2762 | // grace is at least HASHRATE_IDLE_MISSES seconds, so a clock that has | ||
| 2763 | // stepped backwards gives a negative `silent`, takes this branch, and | ||
| 2764 | // reports the sample unchanged. Nothing here can scale an estimate up. | ||
| 2765 | if (!(silent > grace)) return m->hashRate; | ||
| 2766 | |||
| 2767 | double halflives = (silent - grace) / expected; | ||
| 2768 | if (halflives >= HASHRATE_DECAY_CUTOFF) return 0.0; | ||
| 2769 | return m->hashRate * pow(0.5, halflives); | ||
| 2770 | } | ||
| 2771 | |||
| 1030 | static std::vector<Miner*> gMiners; | 2772 | static std::vector<Miner*> gMiners; |
| 1031 | 2773 | ||
| 1032 | struct FoundBlock { int height; int64 ts; std::string addr; }; | 2774 | struct FoundBlock { int height; int64 ts; std::string addr; }; |
| @@ -1044,6 +2786,57 @@ static void PushReady(btf_socket_t s) | |||
| 1044 | gReadyQueue.push_back(s); | 2786 | gReadyQueue.push_back(s); |
| 1045 | } | 2787 | } |
| 1046 | 2788 | ||
| 2789 | // Loopback stratum listener: lets a miner running on the SAME machine reach | ||
| 2790 | // this pool over plain TCP (127.0.0.1:8334), skipping the .btf rendezvous | ||
| 2791 | // entirely. The rendezvous relay only allows one service registration per | ||
| 2792 | // pubkey, so the pool service and the node's own peer-accept fight over the | ||
| 2793 | // node identity at the relay -- loopback avoids that fight for local miners. | ||
| 2794 | static void LocalAcceptFn(void*) | ||
| 2795 | { | ||
| 2796 | SOCKET ls = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
| 2797 | if (ls == INVALID_SOCKET) | ||
| 2798 | return; | ||
| 2799 | int yes = 1; | ||
| 2800 | setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes)); | ||
| 2801 | struct sockaddr_in addr; | ||
| 2802 | memset(&addr, 0, sizeof(addr)); | ||
| 2803 | addr.sin_family = AF_INET; | ||
| 2804 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | ||
| 2805 | addr.sin_port = htons(8334); | ||
| 2806 | if (bind(ls, (struct sockaddr*)&addr, sizeof(addr)) != 0 || listen(ls, 16) != 0) | ||
| 2807 | { | ||
| 2808 | LogPrint("pool", "[pool] loopback stratum bind/listen failed on 127.0.0.1:8334\n"); | ||
| 2809 | closesocket(ls); | ||
| 2810 | return; | ||
| 2811 | } | ||
| 2812 | LogPrint("pool", "[pool] loopback stratum listening on 127.0.0.1:8334\n"); | ||
| 2813 | while (!fShutdown && gPoolRunning) | ||
| 2814 | { | ||
| 2815 | SOCKET c = accept(ls, NULL, NULL); | ||
| 2816 | if (c == INVALID_SOCKET) | ||
| 2817 | { | ||
| 2818 | if (fShutdown || !gPoolRunning) | ||
| 2819 | break; | ||
| 2820 | continue; | ||
| 2821 | } | ||
| 2822 | // #6: let the OS probe a peer that has gone silent, so a half-open | ||
| 2823 | // session eventually errors out of recv and lands in the event | ||
| 2824 | // loop's existing reap path instead of sitting in gMiners forever. | ||
| 2825 | // This is a backstop, not the mechanism: the default cadence before | ||
| 2826 | // the first probe is hours on most stacks, so the idle timeout in | ||
| 2827 | // PoolEventLoop fires long before keepalive does. Miners arriving | ||
| 2828 | // via a relay ride a loopback socket pair whose remote transport is | ||
| 2829 | // the rendezvous socket owned by the tunnel pump threads; keepalive | ||
| 2830 | // cannot reach across that, which is part of why the idle timeout | ||
| 2831 | // exists. | ||
| 2832 | int keepalive = 1; | ||
| 2833 | setsockopt(c, SOL_SOCKET, SO_KEEPALIVE, (char*)&keepalive, sizeof(keepalive)); | ||
| 2834 | LogPrint("pool", "[pool] miner arrived via loopback -- pushing to event loop\n"); | ||
| 2835 | PushReady((btf_socket_t)c); | ||
| 2836 | } | ||
| 2837 | closesocket(ls); | ||
| 2838 | } | ||
| 2839 | |||
| 1047 | // --------------------------------------------------------------------------- | 2840 | // --------------------------------------------------------------------------- |
| 1048 | // I/O helpers -- event loop thread only | 2841 | // I/O helpers -- event loop thread only |
| 1049 | // --------------------------------------------------------------------------- | 2842 | // --------------------------------------------------------------------------- |
| @@ -1068,6 +2861,30 @@ static bool SendLine(btf_socket_t fd, const json& j) | |||
| 1068 | static const size_t MAX_STRATUM_LINE = 64 * 1024; | 2861 | static const size_t MAX_STRATUM_LINE = 64 * 1024; |
| 1069 | static const size_t MAX_POOL_MINERS = 1000; | 2862 | static const size_t MAX_POOL_MINERS = 1000; |
| 1070 | 2863 | ||
| 2864 | // #19 instrumentation: the "broadcast to N miner(s)" line counts connected | ||
| 2865 | // sockets, which says nothing about how many are actually mining. These split | ||
| 2866 | // the connection census into connected / subscribed / authorized / recently | ||
| 2867 | // active, so an operator can tell a real workforce from a pile of half-open | ||
| 2868 | // sessions at a glance. | ||
| 2869 | static const int64 POOL_CENSUS_SECS = 60; // how often to log the breakdown | ||
| 2870 | static const int64 POOL_ACTIVE_WINDOW = 300; // a miner is "active" if it | ||
| 2871 | // authorized and was seen within | ||
| 2872 | // this window (one payout-rotate | ||
| 2873 | // cycle) | ||
| 2874 | static const int64 POOL_UNAUTH_GRACE_SECS = 60; // a connection still not | ||
| 2875 | // authorized after this long is | ||
| 2876 | // idle, not merely starting up | ||
| 2877 | // #6: a Miner used to leave gMiners only when RecvLine reported a clean | ||
| 2878 | // disconnect. A half-open session (miner host died, NAT state expired, | ||
| 2879 | // cable pulled) sends no FIN and reports no error, so it sat in the table | ||
| 2880 | // forever -- authorize outpaced disconnect 2:1 on the live pool, and the | ||
| 2881 | // table grew toward MAX_POOL_MINERS, past which real miners are refused. | ||
| 2882 | // lastSeen is stamped on every complete line received; a miner that has | ||
| 2883 | // said nothing for this long is gone in every way that matters. Ten | ||
| 2884 | // minutes is far beyond the vardiff target (a share every 15s) and the | ||
| 2885 | // stall watchdog (45s), so no miner that is actually hashing comes close. | ||
| 2886 | static const int64 POOL_IDLE_TIMEOUT_SECS = 600; | ||
| 2887 | |||
| 1071 | // Non-blocking recv: pull whatever bytes are available now into buf, then | 2888 | // Non-blocking recv: pull whatever bytes are available now into buf, then |
| 1072 | // return one complete line if one is ready. Three outcomes: | 2889 | // return one complete line if one is ready. Three outcomes: |
| 1073 | // true -- line is filled; buf may still contain more lines | 2890 | // true -- line is filled; buf may still contain more lines |
| @@ -1151,6 +2968,17 @@ static json MakeNotifyParams(const StratumJob& job, const CBlock& b, | |||
| 1151 | static void SendJob(Miner* m, bool clean) | 2968 | static void SendJob(Miner* m, bool clean) |
| 1152 | { | 2969 | { |
| 1153 | if (!gHaveJob) return; | 2970 | if (!gHaveJob) return; |
| 2971 | // Every path that hands a miner a new job funnels through here, so this | ||
| 2972 | // is the one place the replay guard needs to follow the job id: nonces | ||
| 2973 | // credited under the previous job mean nothing under the new one. Keying | ||
| 2974 | // the clear to the id (rather than clearing unconditionally) keeps a | ||
| 2975 | // same-job re-notify -- the vardiff retarget and stall watchdog both | ||
| 2976 | // re-send the CURRENT job -- from reopening the duplicate window it | ||
| 2977 | // exists to close. | ||
| 2978 | if (m->seenNoncesJob != gCurrentJob.jobId) { | ||
| 2979 | m->seenNonces.clear(); | ||
| 2980 | m->seenNoncesJob = gCurrentJob.jobId; | ||
| 2981 | } | ||
| 1154 | CBlock mb = BuildMinerBlock(gCurrentJob, m->extranonce2); | 2982 | CBlock mb = BuildMinerBlock(gCurrentJob, m->extranonce2); |
| 1155 | // Use this miner's own shareTarget (set by vardiff) in the notify params. | 2983 | // Use this miner's own shareTarget (set by vardiff) in the notify params. |
| 1156 | // The participant miner reads p[3] as its share target, and HandleLine | 2984 | // The participant miner reads p[3] as its share target, and HandleLine |
| @@ -1176,6 +3004,65 @@ static void BroadcastJob(bool clean) | |||
| 1176 | } | 3004 | } |
| 1177 | 3005 | ||
| 1178 | // --------------------------------------------------------------------------- | 3006 | // --------------------------------------------------------------------------- |
| 3007 | // #19: connection census. "broadcast to N miner(s)" only counts authorized | ||
| 3008 | // sockets that got a job; it says nothing about the rest of the connection | ||
| 3009 | // table. This logs the full breakdown so an operator can tell a real workforce | ||
| 3010 | // apart from half-open / never-authorized / idle sessions, and flags when the | ||
| 3011 | // single select() loop is running out of room (issue #26). | ||
| 3012 | // | ||
| 3013 | // "Out of room" is not the same question on both platforms, and FD_SETSIZE | ||
| 3014 | // answers a different one on each. On POSIX it bounds the *value* of any | ||
| 3015 | // descriptor handed to FD_SET, so what matters is the highest fd we hold. On | ||
| 3016 | // Windows an fd_set is a counted array of SOCKET handles whose numeric values | ||
| 3017 | // are arbitrary, so what matters is how many we hold, not how large they are. | ||
| 3018 | // FD_SETSIZE comes from <sys/select.h> or winsock2.h; this file must not define | ||
| 3019 | // it, because redefining it after those headers changes nothing. | ||
| 3020 | // --------------------------------------------------------------------------- | ||
| 3021 | static void LogPoolConnectionCensus() | ||
| 3022 | { | ||
| 3023 | int connected = 0, subscribed = 0, authorized = 0, active = 0, neverAuthIdle = 0; | ||
| 3024 | int64 now = GetTime(); | ||
| 3025 | #ifndef _WIN32 | ||
| 3026 | int maxfd = 0; | ||
| 3027 | #endif | ||
| 3028 | for (Miner* m : gMiners) { | ||
| 3029 | connected++; | ||
| 3030 | #ifndef _WIN32 | ||
| 3031 | if ((int)m->fd > maxfd) maxfd = (int)m->fd; | ||
| 3032 | #endif | ||
| 3033 | if (m->subscribed) subscribed++; | ||
| 3034 | if (m->authorised) { | ||
| 3035 | authorized++; | ||
| 3036 | if (m->lastSeen > 0 && now - m->lastSeen <= POOL_ACTIVE_WINDOW) | ||
| 3037 | active++; | ||
| 3038 | } else if (now - m->connectTime >= POOL_UNAUTH_GRACE_SECS) { | ||
| 3039 | neverAuthIdle++; // past the grace period and still not authorized | ||
| 3040 | } | ||
| 3041 | } | ||
| 3042 | |||
| 3043 | #ifndef _WIN32 | ||
| 3044 | int headroom = (int)FD_SETSIZE - maxfd; | ||
| 3045 | LogPrint("pool", "[pool] census: connected=%d subscribed=%d authorized=%d" | ||
| 3046 | " active(last %llds)=%d never-auth-idle=%d" | ||
| 3047 | " (cap=%zu, maxfd=%d, FD_SETSIZE=%d)\n", | ||
| 3048 | connected, subscribed, authorized, (long long)POOL_ACTIVE_WINDOW, | ||
| 3049 | active, neverAuthIdle, MAX_POOL_MINERS, maxfd, (int)FD_SETSIZE); | ||
| 3050 | #else | ||
| 3051 | int headroom = (int)FD_SETSIZE - connected; | ||
| 3052 | LogPrint("pool", "[pool] census: connected=%d subscribed=%d authorized=%d" | ||
| 3053 | " active(last %llds)=%d never-auth-idle=%d" | ||
| 3054 | " (cap=%zu, FD_SETSIZE=%d sockets)\n", | ||
| 3055 | connected, subscribed, authorized, (long long)POOL_ACTIVE_WINDOW, | ||
| 3056 | active, neverAuthIdle, MAX_POOL_MINERS, (int)FD_SETSIZE); | ||
| 3057 | #endif | ||
| 3058 | if (headroom <= 16) | ||
| 3059 | LogPrint("pool", "[pool] WARNING: %d slots left before FD_SETSIZE=%d --" | ||
| 3060 | " the select() loop is about to walk into undefined behaviour" | ||
| 3061 | " (#26). Refuse new miners or rebuild with poll/epoll.\n", | ||
| 3062 | headroom, (int)FD_SETSIZE); | ||
| 3063 | } | ||
| 3064 | |||
| 3065 | // --------------------------------------------------------------------------- | ||
| 1179 | // Stratum message handler -- called for each complete line from a miner | 3066 | // Stratum message handler -- called for each complete line from a miner |
| 1180 | // Returns false if the miner should be disconnected immediately. | 3067 | // Returns false if the miner should be disconnected immediately. |
| 1181 | // --------------------------------------------------------------------------- | 3068 | // --------------------------------------------------------------------------- |
| @@ -1243,6 +3130,7 @@ static bool HandleLine(Miner* m, const std::string& rawLine, | |||
| 1243 | // ------------------------------------------------------------------ | 3130 | // ------------------------------------------------------------------ |
| 1244 | if (method == "mining.subscribe") { | 3131 | if (method == "mining.subscribe") { |
| 1245 | std::string sid = ToHex(&m->fd, 4); | 3132 | std::string sid = ToHex(&m->fd, 4); |
| 3133 | m->subscribed = true; | ||
| 1246 | LogPrint("worker", "[worker->pool] subscribe from fd=%d sid=%s\n", | 3134 | LogPrint("worker", "[worker->pool] subscribe from fd=%d sid=%s\n", |
| 1247 | (int)m->fd, sid.c_str()); | 3135 | (int)m->fd, sid.c_str()); |
| 1248 | reply(json::array({json::array({json::array({"mining.notify",sid})}),sid,4})); | 3136 | reply(json::array({json::array({json::array({"mining.notify",sid})}),sid,4})); |
| @@ -1349,6 +3237,22 @@ static bool HandleLine(Miner* m, const std::string& rawLine, | |||
| 1349 | } | 3237 | } |
| 1350 | unsigned int nNonce; memcpy(&nNonce, nb.data(), 4); | 3238 | unsigned int nNonce; memcpy(&nNonce, nb.data(), 4); |
| 1351 | 3239 | ||
| 3240 | // Reject a replayed (job, nonce) before it costs a rebuild and a | ||
| 3241 | // RandomX hash: nothing about the work below distinguishes a second | ||
| 3242 | // submission of a valid share from the first, so without this check | ||
| 3243 | // it passes every test and is credited again -- and roundShareCount | ||
| 3244 | // is what the payout split divides by, so each replay multiplies the | ||
| 3245 | // address's cut of the round. The set is per-connection and cleared | ||
| 3246 | // by SendJob on every job change, so only a same-job repeat is | ||
| 3247 | // refused. | ||
| 3248 | if (m->seenNonces.count(nNonce)) { | ||
| 3249 | LogPrint("worker", "[worker->pool] duplicate share from %s" | ||
| 3250 | " nonce=%u job=%s -- already credited, not counting again\n", | ||
| 3251 | m->address.c_str(), nNonce, submitJobId.c_str()); | ||
| 3252 | reply(false, "Duplicate share"); | ||
| 3253 | return true; | ||
| 3254 | } | ||
| 3255 | |||
| 1352 | // Rebuild this miner's exact block (unique merkle via extranonce2) | 3256 | // Rebuild this miner's exact block (unique merkle via extranonce2) |
| 1353 | CBlock b = BuildMinerBlock(gCurrentJob, m->extranonce2); | 3257 | CBlock b = BuildMinerBlock(gCurrentJob, m->extranonce2); |
| 1354 | b.nNonce = nNonce; | 3258 | b.nNonce = nNonce; |
| @@ -1376,6 +3280,7 @@ static bool HandleLine(Miner* m, const std::string& rawLine, | |||
| 1376 | m->roundShares++; | 3280 | m->roundShares++; |
| 1377 | m->totalShares++; | 3281 | m->totalShares++; |
| 1378 | m->vardiffShares++; | 3282 | m->vardiffShares++; |
| 3283 | m->seenNonces.insert(nNonce); // replay guard: credit each nonce once per job | ||
| 1379 | int64 shareTime = GetTime(); | 3284 | int64 shareTime = GetTime(); |
| 1380 | m->lastSeen = shareTime; | 3285 | m->lastSeen = shareTime; |
| 1381 | roundShareCount[m->address]++; | 3286 | roundShareCount[m->address]++; |
| @@ -1480,7 +3385,11 @@ static bool HandleLine(Miner* m, const std::string& rawLine, | |||
| 1480 | if (accepted) { | 3385 | if (accepted) { |
| 1481 | // Snapshot height before RebuildJob() (which increments it) | 3386 | // Snapshot height before RebuildJob() (which increments it) |
| 1482 | int foundHeight = gCurrentJob.height; | 3387 | int foundHeight = gCurrentJob.height; |
| 1483 | QueuePayouts(foundHeight, b.GetHash().GetHex(), b.vtx[0].GetHash().GetHex(), | 3388 | // b is the block we submitted, untouched by ProcessBlock (which |
| 3389 | // took a copy), so its hash is what the payout must be keyed on. | ||
| 3390 | // Passed as a uint256, not hex: the ledger key must stay a real | ||
| 3391 | // hash. The remaining arguments are upstream's round-proof data. | ||
| 3392 | QueuePayouts(foundHeight, b.GetHash(), b.vtx[0].GetHash().GetHex(), | ||
| 1484 | b.vtx[0].vout[0].nValue, m->address, | 3393 | b.vtx[0].vout[0].nValue, m->address, |
| 1485 | roundShareCount, roundShareTotal); | 3394 | roundShareCount, roundShareTotal); |
| 1486 | // Reset round shares | 3395 | // Reset round shares |
| @@ -1508,6 +3417,1600 @@ static bool HandleLine(Miner* m, const std::string& rawLine, | |||
| 1508 | return true; | 3417 | return true; |
| 1509 | } | 3418 | } |
| 1510 | 3419 | ||
| 3420 | // --------------------------------------------------------------------------- | ||
| 3421 | // payout-ledger self-test | ||
| 3422 | // | ||
| 3423 | // Regression cover for the payout defects fixed in this series. Everything | ||
| 3424 | // here runs against a scratch data directory and an in-memory wallet map; it | ||
| 3425 | // never opens wallet.dat, never touches a real datadir, and never sends | ||
| 3426 | // anything. | ||
| 3427 | // --------------------------------------------------------------------------- | ||
| 3428 | |||
| 3429 | static bool PayoutTestReadFile(const std::string& path, std::string& out) | ||
| 3430 | { | ||
| 3431 | FILE* f = fopen(path.c_str(), "r"); | ||
| 3432 | if (!f) return false; | ||
| 3433 | fseek(f, 0, SEEK_END); | ||
| 3434 | long sz = ftell(f); | ||
| 3435 | fseek(f, 0, SEEK_SET); | ||
| 3436 | out.assign((size_t)(sz > 0 ? sz : 0), '\0'); | ||
| 3437 | if (sz > 0 && fread(&out[0], 1, (size_t)sz, f) != (size_t)sz) { | ||
| 3438 | fclose(f); | ||
| 3439 | return false; | ||
| 3440 | } | ||
| 3441 | fclose(f); | ||
| 3442 | return true; | ||
| 3443 | } | ||
| 3444 | |||
| 3445 | // Delete the scratch data directory and everything in it. A sweep and not a | ||
| 3446 | // list of known names: LogPrint binds debug.log to whatever GetAppDir() is | ||
| 3447 | // when it first fires, so the test's own log lands in here too, and a | ||
| 3448 | // directory left behind in src/ after `make tests` is litter. | ||
| 3449 | static void PayoutTestRemoveDir(const std::string& dir) | ||
| 3450 | { | ||
| 3451 | #ifdef _WIN32 | ||
| 3452 | WIN32_FIND_DATAA findData; | ||
| 3453 | HANDLE hFind = FindFirstFileA((dir + "\\*").c_str(), &findData); | ||
| 3454 | if (hFind != INVALID_HANDLE_VALUE) { | ||
| 3455 | do { | ||
| 3456 | std::string name = findData.cFileName; | ||
| 3457 | if (name == "." || name == "..") continue; | ||
| 3458 | DeleteFileA((dir + "\\" + name).c_str()); | ||
| 3459 | } while (FindNextFileA(hFind, &findData)); | ||
| 3460 | FindClose(hFind); | ||
| 3461 | } | ||
| 3462 | RemoveDirectoryA(dir.c_str()); | ||
| 3463 | #else | ||
| 3464 | // One level of recursion, because CreateTransaction() opens a Berkeley DB | ||
| 3465 | // environment and BDB puts its logs in a "database" subdirectory. Without | ||
| 3466 | // this the scratch directory cannot be removed and the test leaves one | ||
| 3467 | // behind on every run, which is what d5b3503 was for. | ||
| 3468 | DIR* d = opendir(dir.c_str()); | ||
| 3469 | if (d) { | ||
| 3470 | struct dirent* ent; | ||
| 3471 | while ((ent = readdir(d)) != NULL) { | ||
| 3472 | std::string name = ent->d_name; | ||
| 3473 | if (name == "." || name == "..") continue; | ||
| 3474 | const std::string path = dir + "/" + name; | ||
| 3475 | if (unlink(path.c_str()) != 0) | ||
| 3476 | PayoutTestRemoveDir(path); | ||
| 3477 | } | ||
| 3478 | closedir(d); | ||
| 3479 | } | ||
| 3480 | rmdir(dir.c_str()); | ||
| 3481 | #endif | ||
| 3482 | } | ||
| 3483 | |||
| 3484 | static bool PayoutTestWriteFile(const std::string& path, const std::string& data) | ||
| 3485 | { | ||
| 3486 | FILE* f = fopen(path.c_str(), "w"); | ||
| 3487 | if (!f) return false; | ||
| 3488 | bool ok = data.empty() || fwrite(data.c_str(), 1, data.size(), f) == data.size(); | ||
| 3489 | return fclose(f) == 0 && ok; | ||
| 3490 | } | ||
| 3491 | |||
| 3492 | // Wipe every piece of payout state this test touches, so each section starts | ||
| 3493 | // from the same place and a "restart" can be simulated by reloading from disk. | ||
| 3494 | static void PayoutTestResetState() | ||
| 3495 | { | ||
| 3496 | { | ||
| 3497 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 3498 | gPendingPayouts.clear(); | ||
| 3499 | } | ||
| 3500 | { | ||
| 3501 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3502 | gPaying.clear(); | ||
| 3503 | gQueuedPayouts.clear(); | ||
| 3504 | } | ||
| 3505 | { | ||
| 3506 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); | ||
| 3507 | gPoolRounds.clear(); | ||
| 3508 | } | ||
| 3509 | { | ||
| 3510 | std::lock_guard<std::mutex> lk(gClaimedTxidMutex); | ||
| 3511 | gLegacyClaimedTxids.clear(); | ||
| 3512 | } | ||
| 3513 | { | ||
| 3514 | // A quarantine is meant to last the life of the process; each section | ||
| 3515 | // here is a fresh process as far as the code under test is concerned. | ||
| 3516 | std::lock_guard<std::mutex> lk(gRefusedFilesMutex); | ||
| 3517 | gRefusedFiles.clear(); | ||
| 3518 | } | ||
| 3519 | } | ||
| 3520 | |||
| 3521 | // A BTF address whose hash160 is entirely under the test's control, so the | ||
| 3522 | // output script the probe compares against can be built without a wallet. | ||
| 3523 | static std::string PayoutTestAddress(unsigned char seed) | ||
| 3524 | { | ||
| 3525 | uint160 h = 0; | ||
| 3526 | for (int i = 0; i < 20; i++) | ||
| 3527 | ((unsigned char*)&h)[i] = (unsigned char)(seed + i); | ||
| 3528 | return Hash160ToAddress(h); | ||
| 3529 | } | ||
| 3530 | |||
| 3531 | static CScript PayoutTestScriptFor(const std::string& address) | ||
| 3532 | { | ||
| 3533 | uint160 h160; | ||
| 3534 | CScript sc; | ||
| 3535 | if (AddressToHash160(address, h160)) | ||
| 3536 | sc << OP_DUP << OP_HASH160 << h160 << OP_EQUALVERIFY << OP_CHECKSIG; | ||
| 3537 | return sc; | ||
| 3538 | } | ||
| 3539 | |||
| 3540 | // Block index entries fabricated by PayoutTestAddWalletTx, so they can be | ||
| 3541 | // freed and unhooked when the test finishes. gPayoutTestChainTip is the end of | ||
| 3542 | // that chain: blocks are linked onto it rather than each replacing the tip, | ||
| 3543 | // because IsInMainChain() is "pnext is set, or I am the tip". | ||
| 3544 | static std::vector<CBlockIndex*> gPayoutTestBlockIndexes; | ||
| 3545 | static CBlockIndex* gPayoutTestChainTip = NULL; | ||
| 3546 | |||
| 3547 | static void PayoutTestClearBlockIndex() | ||
| 3548 | { | ||
| 3549 | CRITICAL_BLOCK(cs_main) | ||
| 3550 | { | ||
| 3551 | for (CBlockIndex* p : gPayoutTestBlockIndexes) { | ||
| 3552 | for (std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.begin(); | ||
| 3553 | it != mapBlockIndex.end(); ) { | ||
| 3554 | if (it->second == p) mapBlockIndex.erase(it++); | ||
| 3555 | else ++it; | ||
| 3556 | } | ||
| 3557 | delete p; | ||
| 3558 | } | ||
| 3559 | gPayoutTestBlockIndexes.clear(); | ||
| 3560 | gPayoutTestChainTip = NULL; | ||
| 3561 | pindexBest = NULL; | ||
| 3562 | } | ||
| 3563 | } | ||
| 3564 | |||
| 3565 | // Put a transaction in the wallet map that looks exactly like one this pool | ||
| 3566 | // sent. `nonce` only exists to keep the hashes distinct. | ||
| 3567 | // | ||
| 3568 | // fConfirmed decides what GetDepthInMainChain() will answer for it, which is | ||
| 3569 | // what the legacy probe now gates on. A confirmed one is given a block index | ||
| 3570 | // entry of its own: an empty merkle branch hashes to the transaction itself, | ||
| 3571 | // so a block whose merkle root IS that hash accepts it at index 0, and | ||
| 3572 | // pointing pindexBest at that block puts it in the main chain one deep. An | ||
| 3573 | // unconfirmed one keeps hashBlock 0, which is depth 0 -- the shape a payout | ||
| 3574 | // left behind by a reorg has. | ||
| 3575 | static std::string PayoutTestAddWalletTx(const std::string& comment, | ||
| 3576 | const std::string& address, | ||
| 3577 | int64 amount, | ||
| 3578 | int nonce, | ||
| 3579 | const std::string& structuredId, | ||
| 3580 | bool fConfirmed = true, | ||
| 3581 | const std::string& structuredBlock = "") | ||
| 3582 | { | ||
| 3583 | CWalletTx wtx; | ||
| 3584 | wtx.vin.resize(1); | ||
| 3585 | wtx.vin[0].prevout.SetNull(); | ||
| 3586 | wtx.vin[0].scriptSig << nonce; | ||
| 3587 | wtx.vout.resize(1); | ||
| 3588 | wtx.vout[0].nValue = amount; | ||
| 3589 | wtx.vout[0].scriptPubKey = PayoutTestScriptFor(address); | ||
| 3590 | if (!comment.empty()) | ||
| 3591 | wtx.mapValue["comment"] = comment; | ||
| 3592 | if (!structuredId.empty()) { | ||
| 3593 | wtx.mapValue["pool_payout_id"] = structuredId; | ||
| 3594 | wtx.mapValue["pool_payout_address"] = address; | ||
| 3595 | wtx.mapValue["pool_payout_amount"] = strprintf("%lld", amount); | ||
| 3596 | if (!structuredBlock.empty()) | ||
| 3597 | wtx.mapValue["pool_payout_block"] = structuredBlock; | ||
| 3598 | } | ||
| 3599 | uint256 hash = wtx.GetHash(); | ||
| 3600 | if (fConfirmed) { | ||
| 3601 | // A block hash distinct from the transaction's own. | ||
| 3602 | uint256 blockHash = hash; | ||
| 3603 | ((unsigned char*)&blockHash)[0] ^= 0xff; | ||
| 3604 | CRITICAL_BLOCK(cs_main) | ||
| 3605 | { | ||
| 3606 | CBlockIndex* pindex = new CBlockIndex(); | ||
| 3607 | pindex->hashMerkleRoot = hash; | ||
| 3608 | // Linked onto the end of the fabricated chain rather than simply | ||
| 3609 | // becoming the new tip: IsInMainChain() is "pnext is set, or I am | ||
| 3610 | // the tip", so a second confirmed transaction would otherwise take | ||
| 3611 | // the first one out of the chain again. | ||
| 3612 | pindex->pprev = gPayoutTestChainTip; | ||
| 3613 | pindex->nHeight = gPayoutTestChainTip ? gPayoutTestChainTip->nHeight + 1 : 1; | ||
| 3614 | if (gPayoutTestChainTip) | ||
| 3615 | gPayoutTestChainTip->pnext = pindex; | ||
| 3616 | gPayoutTestChainTip = pindex; | ||
| 3617 | std::map<uint256, CBlockIndex*>::iterator mi = | ||
| 3618 | mapBlockIndex.insert(std::make_pair(blockHash, pindex)).first; | ||
| 3619 | pindex->phashBlock = &mi->first; | ||
| 3620 | pindexBest = pindex; | ||
| 3621 | gPayoutTestBlockIndexes.push_back(pindex); | ||
| 3622 | } | ||
| 3623 | wtx.hashBlock = blockHash; | ||
| 3624 | wtx.nIndex = 0; | ||
| 3625 | } | ||
| 3626 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 3627 | mapWallet[hash] = wtx; | ||
| 3628 | return hash.GetHex(); | ||
| 3629 | } | ||
| 3630 | |||
| 3631 | // A real key, in the keystore, so a test wallet's outputs are IsMine() and can | ||
| 3632 | // actually be signed -- both are needed for CreateTransaction to build the | ||
| 3633 | // large transaction the fee schedule is being tested against. Inserted | ||
| 3634 | // straight into the key maps rather than through AddKey(), which would also | ||
| 3635 | // write it to a wallet.dat. | ||
| 3636 | static std::string PayoutTestAddSpendableKey() | ||
| 3637 | { | ||
| 3638 | CKey key; | ||
| 3639 | key.MakeNewKey(); | ||
| 3640 | std::vector<unsigned char> vchPubKey = key.GetPubKey(); | ||
| 3641 | CRITICAL_BLOCK(cs_mapKeys) | ||
| 3642 | { | ||
| 3643 | mapKeys[vchPubKey] = key.GetPrivKey(); | ||
| 3644 | mapPubKeys[Hash160(vchPubKey)] = vchPubKey; | ||
| 3645 | } | ||
| 3646 | return Hash160ToAddress(Hash160(vchPubKey)); | ||
| 3647 | } | ||
| 3648 | |||
| 3649 | // Give the wallet `n` spendable outputs of `each`, one transaction apiece -- | ||
| 3650 | // which is the granularity SelectCoins works at, and therefore the number of | ||
| 3651 | // inputs the transaction spending them will carry. | ||
| 3652 | // | ||
| 3653 | // Deliberately not coinbases: a null prevout would make CWalletTx::GetCredit | ||
| 3654 | // return 0 until COINBASE_SPENDABLE_DEPTH, and there is no chain here. | ||
| 3655 | static void PayoutTestFundWallet(int n, int64 each, const std::string& address) | ||
| 3656 | { | ||
| 3657 | const CScript sc = PayoutTestScriptFor(address); | ||
| 3658 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 3659 | { | ||
| 3660 | for (int i = 0; i < n; i++) { | ||
| 3661 | CWalletTx wtx; | ||
| 3662 | wtx.vin.resize(1); | ||
| 3663 | wtx.vin[0].prevout.hash = (uint64)(i + 1); | ||
| 3664 | wtx.vin[0].prevout.n = 0; | ||
| 3665 | wtx.vout.resize(1); | ||
| 3666 | wtx.vout[0].nValue = each; | ||
| 3667 | wtx.vout[0].scriptPubKey = sc; | ||
| 3668 | mapWallet[wtx.GetHash()] = wtx; | ||
| 3669 | } | ||
| 3670 | } | ||
| 3671 | } | ||
| 3672 | |||
| 3673 | // Defined below with the event loop; the status-file publication test drives | ||
| 3674 | // it directly. | ||
| 3675 | static void RefreshStats(int blocksFoundThisSession, uint64 roundShareTotal); | ||
| 3676 | |||
| 3677 | int RunPayoutLedgerSelfTest() | ||
| 3678 | { | ||
| 3679 | int nFail = 0; | ||
| 3680 | auto check = [&](bool ok, const char* msg) { | ||
| 3681 | fprintf(stdout, " %s %s\n", ok ? "ok" : "FAIL", msg); | ||
| 3682 | if (!ok) nFail++; | ||
| 3683 | }; | ||
| 3684 | |||
| 3685 | fprintf(stdout, "payout-ledger self-test\n"); | ||
| 3686 | |||
| 3687 | const std::string savedDataDir = strSetDataDir; | ||
| 3688 | const int savedBestHeight = nBestHeight; | ||
| 3689 | |||
| 3690 | // Scratch data directory in the current working directory. GetAppDir() | ||
| 3691 | // follows strSetDataDir, so this is what PayoutFilePath() and | ||
| 3692 | // PayingFilePath() resolve against for the rest of the test. | ||
| 3693 | #ifdef _WIN32 | ||
| 3694 | unsigned long pid = (unsigned long)GetCurrentProcessId(); | ||
| 3695 | #else | ||
| 3696 | unsigned long pid = (unsigned long)getpid(); | ||
| 3697 | #endif | ||
| 3698 | // Absolute, and the process moves into it. | ||
| 3699 | // | ||
| 3700 | // Both are needed by the CreateTransaction section: Berkeley DB resolves | ||
| 3701 | // its log directory relative to the environment home, so a relative | ||
| 3702 | // datadir makes dbenv.open() fail with DB_RUNRECOVERY; and BDB opens | ||
| 3703 | // db.log relative to the working directory, which is src/ during `make | ||
| 3704 | // tests`. Moving in puts it inside the scratch directory, where it dies | ||
| 3705 | // with everything else. | ||
| 3706 | std::string cwd = "."; | ||
| 3707 | { | ||
| 3708 | #ifdef _WIN32 | ||
| 3709 | char buf[MAX_PATH]; | ||
| 3710 | if (GetCurrentDirectoryA(MAX_PATH, buf) > 0) cwd = buf; | ||
| 3711 | #else | ||
| 3712 | char buf[4096]; | ||
| 3713 | if (getcwd(buf, sizeof(buf)) != NULL) cwd = buf; | ||
| 3714 | #endif | ||
| 3715 | } | ||
| 3716 | std::string dir = cwd + "/" + | ||
| 3717 | strprintf("payout_selftest_%d_%lu", (int)GetTime(), pid); | ||
| 3718 | #ifdef _WIN32 | ||
| 3719 | CreateDirectoryA(dir.c_str(), NULL); | ||
| 3720 | SetCurrentDirectoryA(dir.c_str()); | ||
| 3721 | #else | ||
| 3722 | mkdir(dir.c_str(), 0700); | ||
| 3723 | if (chdir(dir.c_str()) != 0) { /* stay put; the paths are absolute anyway */ } | ||
| 3724 | #endif | ||
| 3725 | strSetDataDir = dir; | ||
| 3726 | fprintf(stdout, " scratch datadir: %s\n", dir.c_str()); | ||
| 3727 | |||
| 3728 | const std::string ledgerPath = PayoutFilePath(); | ||
| 3729 | const std::string payingPath = PayingFilePath(); | ||
| 3730 | const std::string addrA = PayoutTestAddress(0x11); | ||
| 3731 | const std::string addrB = PayoutTestAddress(0x55); | ||
| 3732 | |||
| 3733 | // -- 1. A restart must not duplicate an in-flight record ----------------- | ||
| 3734 | // | ||
| 3735 | // The ledger entry is grandfathered (zero hash) on purpose: it keeps | ||
| 3736 | // PayoutBlockStillOnChain out of the picture, which needs a block index, | ||
| 3737 | // and it exercises the height fallback at the same time. | ||
| 3738 | { | ||
| 3739 | PayoutTestResetState(); | ||
| 3740 | remove(ledgerPath.c_str()); | ||
| 3741 | remove(payingPath.c_str()); | ||
| 3742 | |||
| 3743 | PendingPayout pp; | ||
| 3744 | pp.blockHeight = 4242; | ||
| 3745 | pp.blockHash = 0; | ||
| 3746 | pp.matureAtHeight = 4242; | ||
| 3747 | pp.amounts[addrA] = 10 * COIN; | ||
| 3748 | pp.amounts[addrB] = 20 * COIN; | ||
| 3749 | VersionedBlob snap; | ||
| 3750 | { | ||
| 3751 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 3752 | gPendingPayouts.push_back(pp); | ||
| 3753 | snap = SerializePendingPayoutsLocked(); | ||
| 3754 | } | ||
| 3755 | PublishPendingPayouts(snap); | ||
| 3756 | |||
| 3757 | nBestHeight = 5000; | ||
| 3758 | FlushMaturePayouts(); | ||
| 3759 | size_t afterFirstFlush = 0; | ||
| 3760 | { | ||
| 3761 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3762 | afterFirstFlush = gPaying.size(); | ||
| 3763 | } | ||
| 3764 | check(afterFirstFlush == 2, | ||
| 3765 | "a matured round explodes into one record per unpaid recipient"); | ||
| 3766 | |||
| 3767 | // Restart: drop everything in memory and come back from the files. | ||
| 3768 | for (int restart = 1; restart <= 2; restart++) { | ||
| 3769 | PayoutTestResetState(); | ||
| 3770 | LoadPendingPayouts(); | ||
| 3771 | LoadPaying(); | ||
| 3772 | ReconcilePayouts(); | ||
| 3773 | |||
| 3774 | size_t queued = 0; | ||
| 3775 | { | ||
| 3776 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3777 | queued = gQueuedPayouts.size(); | ||
| 3778 | } | ||
| 3779 | if (restart == 1) | ||
| 3780 | check(queued == 1, | ||
| 3781 | "startup rebuilds the exploded-round set from the restored" | ||
| 3782 | " in-flight list"); | ||
| 3783 | |||
| 3784 | FlushMaturePayouts(); | ||
| 3785 | size_t n = 0; | ||
| 3786 | { | ||
| 3787 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3788 | n = gPaying.size(); | ||
| 3789 | } | ||
| 3790 | check(n == 2, restart == 1 | ||
| 3791 | ? "a restart does not duplicate an in-flight record" | ||
| 3792 | : "a second restart does not duplicate one either"); | ||
| 3793 | } | ||
| 3794 | |||
| 3795 | // The other half of the guard: even with the round-level dedupe set | ||
| 3796 | // deliberately emptied, the per-recipient check against gPaying must | ||
| 3797 | // stop a second standing order for the same money. | ||
| 3798 | { | ||
| 3799 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3800 | gQueuedPayouts.clear(); | ||
| 3801 | } | ||
| 3802 | FlushMaturePayouts(); | ||
| 3803 | size_t n = 0; | ||
| 3804 | { | ||
| 3805 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3806 | n = gPaying.size(); | ||
| 3807 | } | ||
| 3808 | check(n == 2, | ||
| 3809 | "the flush refuses a second record for a (round, address) already" | ||
| 3810 | " in flight even with the round set lost"); | ||
| 3811 | } | ||
| 3812 | |||
| 3813 | // -- 2. Duplicates already on disk are collapsed as they are read -------- | ||
| 3814 | { | ||
| 3815 | PayoutTestResetState(); | ||
| 3816 | nlohmann::json arr = nlohmann::json::array(); | ||
| 3817 | for (int i = 0; i < 2; i++) { | ||
| 3818 | nlohmann::json o; | ||
| 3819 | o["blockHeight"] = 4242; | ||
| 3820 | o["blockHash"] = uint256(0).ToString(); | ||
| 3821 | o["matureAtHeight"] = 4242; | ||
| 3822 | o["address"] = addrA; | ||
| 3823 | o["amount"] = (int64)(10 * COIN); | ||
| 3824 | o["retries"] = i == 0 ? 7 : 0; | ||
| 3825 | o["deferrals"] = i == 0 ? 0 : 3; | ||
| 3826 | arr.push_back(o); | ||
| 3827 | } | ||
| 3828 | check(PayoutTestWriteFile(payingPath, arr.dump(2)), | ||
| 3829 | "wrote a paying file containing the duplicate the old build made"); | ||
| 3830 | LoadPaying(); | ||
| 3831 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3832 | check(gPaying.size() == 1, | ||
| 3833 | "loading collapses a duplicate in-flight record from disk"); | ||
| 3834 | check(gPaying.size() == 1 && gPaying[0].retries == 7 && gPaying[0].deferrals == 3, | ||
| 3835 | "collapsing merges the attempt counts upwards, not resetting them"); | ||
| 3836 | } | ||
| 3837 | |||
| 3838 | // -- 3. State updates address a slot, not a value ----------------------- | ||
| 3839 | // | ||
| 3840 | // Build the exact pathology the old code produced -- two records equal in | ||
| 3841 | // every field -- and prove the update lands on the one asked for. Under | ||
| 3842 | // value matching the first record absorbed everything and the second was | ||
| 3843 | // untouchable. | ||
| 3844 | { | ||
| 3845 | PayoutTestResetState(); | ||
| 3846 | PayingRecord a; | ||
| 3847 | a.blockHeight = 4242; a.blockHash = 0; a.matureAtHeight = 4242; | ||
| 3848 | a.address = addrA; a.amount = 10 * COIN; | ||
| 3849 | PayingRecord b = a; | ||
| 3850 | { | ||
| 3851 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3852 | a.id = ++gPayingNextId; | ||
| 3853 | b.id = ++gPayingNextId; | ||
| 3854 | gPaying.push_back(a); | ||
| 3855 | gPaying.push_back(b); | ||
| 3856 | } | ||
| 3857 | PayingRecord upd = b; | ||
| 3858 | upd.retries = 5; | ||
| 3859 | upd.retryAfter = 1234567; | ||
| 3860 | ReschedulePayment(upd); | ||
| 3861 | { | ||
| 3862 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3863 | check(gPaying.size() == 2 && | ||
| 3864 | gPaying[0].retries == 0 && gPaying[1].retries == 5, | ||
| 3865 | "rescheduling updates the record it was given, not the first" | ||
| 3866 | " one that looks like it"); | ||
| 3867 | } | ||
| 3868 | ForgetPayment(a); | ||
| 3869 | { | ||
| 3870 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3871 | check(gPaying.size() == 1 && gPaying[0].id == b.id, | ||
| 3872 | "forgetting erases the record it was given, not the first" | ||
| 3873 | " one that looks like it"); | ||
| 3874 | } | ||
| 3875 | } | ||
| 3876 | |||
| 3877 | // -- 4. retryAfter survives a restart ------------------------------------ | ||
| 3878 | { | ||
| 3879 | PayoutTestResetState(); | ||
| 3880 | remove(payingPath.c_str()); | ||
| 3881 | PayingRecord r; | ||
| 3882 | r.blockHeight = 77; r.blockHash = 0; r.matureAtHeight = 77; | ||
| 3883 | r.address = addrA; r.amount = 3 * COIN; | ||
| 3884 | r.retryAfter = 1700000000LL; | ||
| 3885 | r.deferrals = 9; | ||
| 3886 | VersionedBlob snap; | ||
| 3887 | { | ||
| 3888 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3889 | r.id = ++gPayingNextId; | ||
| 3890 | gPaying.push_back(r); | ||
| 3891 | snap = SerializePayingLocked(); | ||
| 3892 | } | ||
| 3893 | PublishPaying(snap); | ||
| 3894 | PayoutTestResetState(); | ||
| 3895 | LoadPaying(); | ||
| 3896 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 3897 | check(gPaying.size() == 1 && gPaying[0].retryAfter == 1700000000LL && | ||
| 3898 | gPaying[0].deferrals == 9, | ||
| 3899 | "the retry clock that is actually used survives a restart"); | ||
| 3900 | } | ||
| 3901 | |||
| 3902 | // -- 5. The ledger file cannot be published from a stale snapshot -------- | ||
| 3903 | { | ||
| 3904 | PayoutTestResetState(); | ||
| 3905 | remove(ledgerPath.c_str()); | ||
| 3906 | |||
| 3907 | PendingPayout one; | ||
| 3908 | one.blockHeight = 100; one.blockHash = 0; one.matureAtHeight = 100; | ||
| 3909 | one.amounts[addrA] = 1 * COIN; | ||
| 3910 | VersionedBlob older, newer; | ||
| 3911 | { | ||
| 3912 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 3913 | gPendingPayouts.push_back(one); | ||
| 3914 | older = SerializePendingPayoutsLocked(); | ||
| 3915 | PendingPayout two; | ||
| 3916 | two.blockHeight = 200; two.blockHash = 0; two.matureAtHeight = 200; | ||
| 3917 | two.amounts[addrB] = 2 * COIN; | ||
| 3918 | gPendingPayouts.push_back(two); | ||
| 3919 | newer = SerializePendingPayoutsLocked(); | ||
| 3920 | } | ||
| 3921 | check(newer.seq > older.seq, | ||
| 3922 | "a later serialisation carries a strictly higher version"); | ||
| 3923 | |||
| 3924 | // The order the race produced: the payout thread's older image | ||
| 3925 | // arriving after the event loop's newer one. | ||
| 3926 | PublishPendingPayouts(newer); | ||
| 3927 | PublishPendingPayouts(older); | ||
| 3928 | std::string onDisk; | ||
| 3929 | check(PayoutTestReadFile(ledgerPath, onDisk) && onDisk == newer.blob, | ||
| 3930 | "an older snapshot cannot overwrite a newer published ledger"); | ||
| 3931 | |||
| 3932 | // And the ordinary direction still works. | ||
| 3933 | VersionedBlob newest; | ||
| 3934 | { | ||
| 3935 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 3936 | gPendingPayouts.clear(); | ||
| 3937 | newest = SerializePendingPayoutsLocked(); | ||
| 3938 | } | ||
| 3939 | PublishPendingPayouts(newest); | ||
| 3940 | check(PayoutTestReadFile(ledgerPath, onDisk) && onDisk == newest.blob, | ||
| 3941 | "a newer snapshot does replace the published ledger"); | ||
| 3942 | } | ||
| 3943 | |||
| 3944 | // -- 6. Concurrent publishers cannot interleave a file ------------------- | ||
| 3945 | // | ||
| 3946 | // Two threads opening and renaming the same "<path>.tmp" over the same | ||
| 3947 | // target was the original defect. This test was mutation-tested and did | ||
| 3948 | // not catch it: with the file mutex removed the suite still went green 2 | ||
| 3949 | // runs in 5, and the "never leave it unparseable" assertion fired 0 times | ||
| 3950 | // in 5. Two reasons, both fixed here. | ||
| 3951 | // | ||
| 3952 | // First, every image had the same structure and near enough the same | ||
| 3953 | // length, so an interleaving of two of them was still valid JSON. The | ||
| 3954 | // images now alternate between ONE entry and FIVE THOUSAND, so a write | ||
| 3955 | // that lands on top of another leaves either a truncated array or a small | ||
| 3956 | // image followed by the tail of a large one -- neither of which parses. | ||
| 3957 | // | ||
| 3958 | // Second, and worse, almost nothing was being written. Each thread walked | ||
| 3959 | // a permutation of the images, and PublishVersioned drops any blob that is | ||
| 3960 | // not newer than what is on disk, so only the record highs in each | ||
| 3961 | // permutation ever reached the disk -- on the order of ten writes in | ||
| 3962 | // total, four threads, no contention to speak of. Every publish here is | ||
| 3963 | // stamped from a shared counter at the moment it is made, so every one of | ||
| 3964 | // them is newer than the last and every one of them writes. | ||
| 3965 | // | ||
| 3966 | // And the file is watched while the writers run, not only inspected after | ||
| 3967 | // they finish. "The published file is never observed unparseable" is the | ||
| 3968 | // actual invariant -- rename(2) is atomic, so a reader can only ever see a | ||
| 3969 | // completely written image -- and checking it once at the end samples the | ||
| 3970 | // one moment when a torn file is least likely to still be there. | ||
| 3971 | { | ||
| 3972 | const std::string racePath = dir + "/publish_race_selftest.json"; | ||
| 3973 | std::mutex raceMutex; | ||
| 3974 | uint64 racePublished = 0; | ||
| 3975 | |||
| 3976 | std::string tiny, huge; | ||
| 3977 | { | ||
| 3978 | nlohmann::json one = nlohmann::json::array(); | ||
| 3979 | nlohmann::json o; | ||
| 3980 | o["image"] = 0; | ||
| 3981 | o["entry"] = 0; | ||
| 3982 | o["address"] = addrA; | ||
| 3983 | o["amount"] = (int64)COIN; | ||
| 3984 | one.push_back(o); | ||
| 3985 | tiny = one.dump(2); | ||
| 3986 | |||
| 3987 | nlohmann::json many = nlohmann::json::array(); | ||
| 3988 | for (int k = 0; k < 5000; k++) { | ||
| 3989 | nlohmann::json e; | ||
| 3990 | e["image"] = 1; | ||
| 3991 | e["entry"] = k; | ||
| 3992 | e["address"] = addrA; | ||
| 3993 | e["amount"] = (int64)(k + 1) * COIN; | ||
| 3994 | many.push_back(e); | ||
| 3995 | } | ||
| 3996 | huge = many.dump(2); | ||
| 3997 | } | ||
| 3998 | |||
| 3999 | const int kThreads = 4; | ||
| 4000 | const int kPerThread = 100; | ||
| 4001 | const uint64 kTotal = (uint64)kThreads * kPerThread; | ||
| 4002 | |||
| 4003 | std::atomic<uint64> nextSeq(0); | ||
| 4004 | std::atomic<bool> racing(true); | ||
| 4005 | std::atomic<int> tornReads(0); | ||
| 4006 | std::atomic<int> goodReads(0); | ||
| 4007 | |||
| 4008 | std::thread watcher([&]() { | ||
| 4009 | bool seenOnce = false; | ||
| 4010 | while (racing) { | ||
| 4011 | std::string s; | ||
| 4012 | if (!PayoutTestReadFile(racePath, s)) { | ||
| 4013 | // Only meaningful once the file exists at all. | ||
| 4014 | if (seenOnce) tornReads++; | ||
| 4015 | continue; | ||
| 4016 | } | ||
| 4017 | seenOnce = true; | ||
| 4018 | try { | ||
| 4019 | if (nlohmann::json::parse(s).is_array()) goodReads++; | ||
| 4020 | else tornReads++; | ||
| 4021 | } catch (...) { | ||
| 4022 | tornReads++; | ||
| 4023 | } | ||
| 4024 | } | ||
| 4025 | }); | ||
| 4026 | |||
| 4027 | std::vector<std::thread> threads; | ||
| 4028 | for (int t = 0; t < kThreads; t++) { | ||
| 4029 | threads.push_back(std::thread([&]() { | ||
| 4030 | for (int i = 0; i < kPerThread; i++) { | ||
| 4031 | VersionedBlob v; | ||
| 4032 | v.seq = ++nextSeq; | ||
| 4033 | v.blob = (v.seq & 1) ? tiny : huge; | ||
| 4034 | PublishVersioned(racePath, v, raceMutex, racePublished, | ||
| 4035 | "publish race selftest"); | ||
| 4036 | } | ||
| 4037 | })); | ||
| 4038 | } | ||
| 4039 | for (std::thread& th : threads) | ||
| 4040 | th.join(); | ||
| 4041 | racing = false; | ||
| 4042 | watcher.join(); | ||
| 4043 | |||
| 4044 | check(goodReads > 0, | ||
| 4045 | "the race test actually observed the file while it was being" | ||
| 4046 | " written"); | ||
| 4047 | check(tornReads == 0, | ||
| 4048 | "four threads publishing the same file never leave it" | ||
| 4049 | " unparseable, at any instant while they are doing it"); | ||
| 4050 | |||
| 4051 | // The highest sequence number issued is the one the file must settle | ||
| 4052 | // on: PublishVersioned refuses anything older, so whichever order the | ||
| 4053 | // writes complete in, that image is the last one that can land. | ||
| 4054 | const std::string& expected = (kTotal & 1) ? tiny : huge; | ||
| 4055 | std::string onDisk; | ||
| 4056 | bool read = PayoutTestReadFile(racePath, onDisk); | ||
| 4057 | check(read && onDisk == expected, | ||
| 4058 | "the published file settles on the highest version, whatever the" | ||
| 4059 | " completion order"); | ||
| 4060 | remove(racePath.c_str()); | ||
| 4061 | remove((racePath + ".tmp").c_str()); | ||
| 4062 | } | ||
| 4063 | |||
| 4064 | // -- 7. Grandfathered zero-hash rounds do not answer for each other ------ | ||
| 4065 | { | ||
| 4066 | uint256 h1 = 0, h2 = 0; | ||
| 4067 | h1.SetHex("00000000000000000000000000000000000000000000000000000000000000aa"); | ||
| 4068 | h2.SetHex("00000000000000000000000000000000000000000000000000000000000000bb"); | ||
| 4069 | check(!SamePayoutRound(100, 0, 200, 0), | ||
| 4070 | "two zero-hash entries at different heights are different rounds"); | ||
| 4071 | check(SamePayoutRound(100, 0, 100, 0), | ||
| 4072 | "two zero-hash entries at the same height are the same round"); | ||
| 4073 | check(SamePayoutRound(100, h1, 200, h1), | ||
| 4074 | "a real hash identifies the round on its own"); | ||
| 4075 | check(!SamePayoutRound(100, h1, 100, h2), | ||
| 4076 | "different hashes at one height are different rounds"); | ||
| 4077 | check(!SamePayoutRound(100, h1, 100, 0), | ||
| 4078 | "a grandfathered entry never matches a hash-keyed one"); | ||
| 4079 | check(!PayoutRoundIdentifiable(0, 0) && PayoutRoundIdentifiable(0, h1) && | ||
| 4080 | PayoutRoundIdentifiable(7, 0), | ||
| 4081 | "an entry with neither hash nor height is not adjudicable"); | ||
| 4082 | |||
| 4083 | // What actually makes a grandfathered same-height pair safe, since the | ||
| 4084 | // comment on FindRecordedPoolPayout used to claim it was the orphan | ||
| 4085 | // check. It is not: the orphan check passes every zero-hash entry, and | ||
| 4086 | // every pre-upgrade entry has a zero hash. | ||
| 4087 | check(PayoutBlockStillOnChain(0), | ||
| 4088 | "the orphan check passes every grandfathered entry, so it cannot" | ||
| 4089 | " be what voids a losing twin"); | ||
| 4090 | PayoutTestResetState(); | ||
| 4091 | remove(ledgerPath.c_str()); | ||
| 4092 | remove(payingPath.c_str()); | ||
| 4093 | { | ||
| 4094 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4095 | PendingPayout twin; | ||
| 4096 | twin.blockHeight = 300; twin.blockHash = 0; twin.matureAtHeight = 300; | ||
| 4097 | twin.amounts[addrA] = 1 * COIN; | ||
| 4098 | gPendingPayouts.push_back(twin); | ||
| 4099 | gPendingPayouts.push_back(twin); // the losing side of a reorg | ||
| 4100 | } | ||
| 4101 | nBestHeight = 5000; | ||
| 4102 | FlushMaturePayouts(); | ||
| 4103 | { | ||
| 4104 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 4105 | check(gPaying.size() == 1, | ||
| 4106 | "two grandfathered rounds at one height are one round, so the" | ||
| 4107 | " pair presents as one debt and is paid at most once"); | ||
| 4108 | } | ||
| 4109 | |||
| 4110 | // End to end: recording a payment against one grandfathered round must | ||
| 4111 | // not settle the other's debt. | ||
| 4112 | PayoutTestResetState(); | ||
| 4113 | remove(ledgerPath.c_str()); | ||
| 4114 | { | ||
| 4115 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4116 | PendingPayout first; | ||
| 4117 | first.blockHeight = 100; first.blockHash = 0; first.matureAtHeight = 100; | ||
| 4118 | first.amounts[addrA] = 1 * COIN; | ||
| 4119 | PendingPayout second; | ||
| 4120 | second.blockHeight = 200; second.blockHash = 0; second.matureAtHeight = 200; | ||
| 4121 | second.amounts[addrA] = 1 * COIN; | ||
| 4122 | gPendingPayouts.push_back(first); | ||
| 4123 | gPendingPayouts.push_back(second); | ||
| 4124 | } | ||
| 4125 | PayingRecord r; | ||
| 4126 | r.blockHeight = 200; r.blockHash = 0; r.matureAtHeight = 200; | ||
| 4127 | r.address = addrA; r.amount = 1 * COIN; | ||
| 4128 | RecordPayoutTxid(r, "txid-for-the-second-round"); | ||
| 4129 | { | ||
| 4130 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4131 | // The second round was fully paid and retired; the first is | ||
| 4132 | // untouched and still owed. | ||
| 4133 | check(gPendingPayouts.size() == 1 && | ||
| 4134 | gPendingPayouts[0].blockHeight == 100 && | ||
| 4135 | gPendingPayouts[0].paidTxids.empty(), | ||
| 4136 | "a txid recorded against one zero-hash round leaves the other" | ||
| 4137 | " still owed"); | ||
| 4138 | } | ||
| 4139 | } | ||
| 4140 | |||
| 4141 | // -- 8. A payment with only a legacy comment is recognised as paid ------- | ||
| 4142 | { | ||
| 4143 | PayoutTestResetState(); | ||
| 4144 | const int height = 8080; | ||
| 4145 | const int64 amount = 7 * COIN; | ||
| 4146 | const std::string id = PayoutIdForBlock(height, 0); | ||
| 4147 | const std::string comment = LegacyPayoutComment(height); | ||
| 4148 | std::set<std::string> none; | ||
| 4149 | std::string found; | ||
| 4150 | // No round proof exists for these rounds, so legacy evidence is | ||
| 4151 | // admissible -- computed exactly as the payout thread computes it. | ||
| 4152 | const bool fAllowLegacy = !PoolRoundHasProof(id); | ||
| 4153 | |||
| 4154 | check(FindRecordedPoolPayout(id, height, 0, addrA, amount, fAllowLegacy, none, found) | ||
| 4155 | == PAYOUT_PROBE_NONE, | ||
| 4156 | "an empty wallet is not evidence that anything was paid"); | ||
| 4157 | |||
| 4158 | const std::string legacyTxid = | ||
| 4159 | PayoutTestAddWalletTx(comment, addrA, amount, 1, ""); | ||
| 4160 | check(FindRecordedPoolPayout(id, height, 0, addrA, amount, fAllowLegacy, none, found) | ||
| 4161 | == PAYOUT_PROBE_LEGACY && found == legacyTxid, | ||
| 4162 | "a payment carrying only a pre-upgrade comment is recognised as" | ||
| 4163 | " already paid"); | ||
| 4164 | |||
| 4165 | check(FindRecordedPoolPayout(id, height, 0, addrB, amount, fAllowLegacy, none, found) | ||
| 4166 | == PAYOUT_PROBE_NONE, | ||
| 4167 | "the legacy match requires the output to pay THIS recipient"); | ||
| 4168 | check(FindRecordedPoolPayout(id, height, 0, addrA, amount + COIN, fAllowLegacy, none, found) | ||
| 4169 | == PAYOUT_PROBE_NONE, | ||
| 4170 | "the legacy match requires the output to pay exactly this amount"); | ||
| 4171 | check(FindRecordedPoolPayout(id, height + 1, 0, addrA, amount, fAllowLegacy, none, found) | ||
| 4172 | == PAYOUT_PROBE_NONE, | ||
| 4173 | "the legacy match requires the comment for this block height"); | ||
| 4174 | |||
| 4175 | std::set<std::string> spent; | ||
| 4176 | spent.insert(legacyTxid); | ||
| 4177 | check(FindRecordedPoolPayout(id, height, 0, addrA, amount, fAllowLegacy, spent, found) | ||
| 4178 | == PAYOUT_PROBE_NONE, | ||
| 4179 | "one legacy transaction cannot settle a second debt"); | ||
| 4180 | |||
| 4181 | // A transaction written by THIS build carries pool_payout_id, and must | ||
| 4182 | // never be offered as legacy evidence for a different round -- that is | ||
| 4183 | // exactly what the structured metadata exists to prevent. | ||
| 4184 | const std::string otherId = PayoutIdForBlock(height, 0) + "-other"; | ||
| 4185 | PayoutTestAddWalletTx(comment, addrB, amount, 2, otherId); | ||
| 4186 | check(FindRecordedPoolPayout(id, height, 0, addrB, amount, fAllowLegacy, none, found) | ||
| 4187 | == PAYOUT_PROBE_NONE, | ||
| 4188 | "another round's structured payment is not legacy evidence for" | ||
| 4189 | " this one"); | ||
| 4190 | |||
| 4191 | // The structured path still wins where it applies. | ||
| 4192 | const std::string structuredTxid = | ||
| 4193 | PayoutTestAddWalletTx(comment, addrB, amount, 3, id); | ||
| 4194 | check(FindRecordedPoolPayout(id, height, 0, addrB, amount, fAllowLegacy, none, found) | ||
| 4195 | == PAYOUT_PROBE_STRUCTURED && found == structuredTxid, | ||
| 4196 | "structured payout metadata is matched in preference to a comment"); | ||
| 4197 | |||
| 4198 | // A comment-matching transaction that never made it into the chain is | ||
| 4199 | // evidence of NON-payment. This tree has seen a 280-block reorg; a | ||
| 4200 | // pre-upgrade payout invalidated by one is still in mapWallet with its | ||
| 4201 | // comment intact, and the first boot after the upgrade is exactly when | ||
| 4202 | // it would be matched. | ||
| 4203 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4204 | mapWallet.clear(); | ||
| 4205 | PayoutTestClearBlockIndex(); | ||
| 4206 | const std::string addrC = PayoutTestAddress(0x33); | ||
| 4207 | PayoutTestAddWalletTx(comment, addrC, amount, 4, "", false); | ||
| 4208 | check(FindRecordedPoolPayout(id, height, 0, addrC, amount, fAllowLegacy, none, found) | ||
| 4209 | == PAYOUT_PROBE_NONE, | ||
| 4210 | "a pre-upgrade payout that never confirmed is not read as" | ||
| 4211 | " payment"); | ||
| 4212 | |||
| 4213 | // The same transaction, confirmed, is. | ||
| 4214 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4215 | mapWallet.clear(); | ||
| 4216 | const std::string confirmedTxid = | ||
| 4217 | PayoutTestAddWalletTx(comment, addrC, amount, 4, "", true); | ||
| 4218 | check(FindRecordedPoolPayout(id, height, 0, addrC, amount, fAllowLegacy, none, found) | ||
| 4219 | == PAYOUT_PROBE_LEGACY && found == confirmedTxid, | ||
| 4220 | "the same payout, confirmed, is"); | ||
| 4221 | |||
| 4222 | // And an unconfirmed one does not shadow a confirmed one that would | ||
| 4223 | // have matched: the probe used to stop at the first comment match. | ||
| 4224 | const std::string alsoConfirmed = | ||
| 4225 | PayoutTestAddWalletTx(comment, addrC, amount, 6, "", true); | ||
| 4226 | PayoutTestAddWalletTx(comment, addrC, amount, 5, "", false); | ||
| 4227 | std::set<std::string> spentFirst; | ||
| 4228 | spentFirst.insert(confirmedTxid); | ||
| 4229 | check(FindRecordedPoolPayout(id, height, 0, addrC, amount, fAllowLegacy, spentFirst, found) | ||
| 4230 | == PAYOUT_PROBE_LEGACY && found == alsoConfirmed, | ||
| 4231 | "an unconfirmed transaction does not hide a confirmed one behind" | ||
| 4232 | " it"); | ||
| 4233 | |||
| 4234 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4235 | mapWallet.clear(); | ||
| 4236 | PayoutTestClearBlockIndex(); | ||
| 4237 | } | ||
| 4238 | |||
| 4239 | // -- 9. On-disk compatibility with the live production ledger ------------ | ||
| 4240 | { | ||
| 4241 | PayoutTestResetState(); | ||
| 4242 | // Exactly the shape of the file on the pool node: amounts, blockHash, | ||
| 4243 | // blockHeight, matureAtHeight, and nothing else. | ||
| 4244 | const char* live = | ||
| 4245 | "[{\"amounts\":{\"%s\":1234500000}," | ||
| 4246 | "\"blockHash\":\"00000000000000000000000000000000000000000000000000000000000000cd\"," | ||
| 4247 | "\"blockHeight\":1000," | ||
| 4248 | "\"matureAtHeight\":1120}]"; | ||
| 4249 | check(PayoutTestWriteFile(ledgerPath, strprintf(live, addrA.c_str())), | ||
| 4250 | "wrote a ledger file in the live production shape"); | ||
| 4251 | LoadPendingPayouts(); | ||
| 4252 | { | ||
| 4253 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4254 | check(gPendingPayouts.size() == 1 && | ||
| 4255 | gPendingPayouts[0].blockHeight == 1000 && | ||
| 4256 | gPendingPayouts[0].matureAtHeight == 1120 && | ||
| 4257 | gPendingPayouts[0].blockHash != 0 && | ||
| 4258 | gPendingPayouts[0].amounts.size() == 1 && | ||
| 4259 | gPendingPayouts[0].amounts[addrA] == 1234500000LL, | ||
| 4260 | "the live production pending_payouts.json still loads"); | ||
| 4261 | } | ||
| 4262 | |||
| 4263 | // And the shape the merged build writes, retryAfter included, which | ||
| 4264 | // this build no longer writes but must still read past. | ||
| 4265 | PayoutTestResetState(); | ||
| 4266 | const char* merged = | ||
| 4267 | "[{\"amounts\":{\"%s\":100000000}," | ||
| 4268 | "\"blockHash\":\"00000000000000000000000000000000000000000000000000000000000000ef\"," | ||
| 4269 | "\"blockHeight\":2000,\"matureAtHeight\":2120,\"retryAfter\":0," | ||
| 4270 | "\"paidTxids\":{\"%s\":\"abc123\"}}]"; | ||
| 4271 | check(PayoutTestWriteFile(ledgerPath, | ||
| 4272 | strprintf(merged, addrA.c_str(), addrA.c_str())), | ||
| 4273 | "wrote a ledger file in the merged build's shape"); | ||
| 4274 | LoadPendingPayouts(); | ||
| 4275 | { | ||
| 4276 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4277 | check(gPendingPayouts.size() == 1 && | ||
| 4278 | gPendingPayouts[0].blockHeight == 2000 && | ||
| 4279 | gPendingPayouts[0].paidTxids.size() == 1 && | ||
| 4280 | gPendingPayouts[0].paidTxids[addrA] == "abc123", | ||
| 4281 | "a ledger file carrying retryAfter and paidTxids still loads"); | ||
| 4282 | } | ||
| 4283 | |||
| 4284 | // A round trip through this build must not lose anything either. | ||
| 4285 | VersionedBlob snap; | ||
| 4286 | { | ||
| 4287 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4288 | snap = SerializePendingPayoutsLocked(); | ||
| 4289 | } | ||
| 4290 | PublishPendingPayouts(snap); | ||
| 4291 | PayoutTestResetState(); | ||
| 4292 | LoadPendingPayouts(); | ||
| 4293 | { | ||
| 4294 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4295 | check(gPendingPayouts.size() == 1 && | ||
| 4296 | gPendingPayouts[0].blockHeight == 2000 && | ||
| 4297 | gPendingPayouts[0].matureAtHeight == 2120 && | ||
| 4298 | gPendingPayouts[0].paidTxids[addrA] == "abc123", | ||
| 4299 | "what this build writes, this build reads back unchanged"); | ||
| 4300 | } | ||
| 4301 | } | ||
| 4302 | |||
| 4303 | // -- 10. A forfeit is published, not just dropped ------------------------ | ||
| 4304 | { | ||
| 4305 | PayoutTestResetState(); | ||
| 4306 | remove(ledgerPath.c_str()); | ||
| 4307 | const std::string roundsPath = dir + "/pool_rounds_selftest.json"; | ||
| 4308 | const std::string savedRoundsFile = strPoolRoundsFile; | ||
| 4309 | strPoolRoundsFile = roundsPath; | ||
| 4310 | remove(roundsPath.c_str()); | ||
| 4311 | |||
| 4312 | const int height = 606; | ||
| 4313 | PoolRoundProof round; | ||
| 4314 | round.id = PayoutIdForBlock(height, 0); | ||
| 4315 | round.blockHeight = height; | ||
| 4316 | round.matureAtHeight = height; | ||
| 4317 | round.payouts[addrA] = 5 * COIN; | ||
| 4318 | round.payouts[addrB] = 6 * COIN; | ||
| 4319 | RecordPoolRound(round); | ||
| 4320 | |||
| 4321 | { | ||
| 4322 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4323 | PendingPayout pp; | ||
| 4324 | pp.blockHeight = height; pp.blockHash = 0; pp.matureAtHeight = height; | ||
| 4325 | pp.amounts[addrA] = 5 * COIN; | ||
| 4326 | pp.amounts[addrB] = 6 * COIN; | ||
| 4327 | gPendingPayouts.push_back(pp); | ||
| 4328 | } | ||
| 4329 | |||
| 4330 | PayingRecord r; | ||
| 4331 | r.blockHeight = height; r.blockHash = 0; r.matureAtHeight = height; | ||
| 4332 | r.address = addrA; r.amount = 5 * COIN; | ||
| 4333 | ForfeitPayoutRecipient(r, "selftest reason"); | ||
| 4334 | |||
| 4335 | { | ||
| 4336 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4337 | check(gPendingPayouts.size() == 1 && | ||
| 4338 | gPendingPayouts[0].amounts.count(addrA) == 0 && | ||
| 4339 | gPendingPayouts[0].amounts.count(addrB) == 1, | ||
| 4340 | "a forfeit removes only that recipient from the ledger entry"); | ||
| 4341 | } | ||
| 4342 | |||
| 4343 | std::string onDisk; | ||
| 4344 | bool proofOk = false; | ||
| 4345 | if (PayoutTestReadFile(roundsPath, onDisk)) { | ||
| 4346 | try { | ||
| 4347 | nlohmann::json root = nlohmann::json::parse(onDisk); | ||
| 4348 | const nlohmann::json& p = root["rounds"][0]["payouts"]; | ||
| 4349 | proofOk = p[addrA].value("forfeited", false) && | ||
| 4350 | p[addrA].value("forfeitReason", "") == "selftest reason" && | ||
| 4351 | p[addrA]["txid"].is_null() && | ||
| 4352 | !p[addrB].contains("forfeited"); | ||
| 4353 | } catch (...) { | ||
| 4354 | proofOk = false; | ||
| 4355 | } | ||
| 4356 | } | ||
| 4357 | check(proofOk, | ||
| 4358 | "the published round proof records the forfeit instead of" | ||
| 4359 | " advertising the address as still owed"); | ||
| 4360 | |||
| 4361 | // And it survives a reload of the proof file. | ||
| 4362 | { | ||
| 4363 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); | ||
| 4364 | gPoolRounds.clear(); | ||
| 4365 | } | ||
| 4366 | LoadPoolRounds(); | ||
| 4367 | { | ||
| 4368 | std::lock_guard<std::mutex> lk(gPoolRoundsMutex); | ||
| 4369 | check(gPoolRounds.size() == 1 && | ||
| 4370 | gPoolRounds[0].forfeited.count(addrA) == 1 && | ||
| 4371 | gPoolRounds[0].forfeited[addrA] == "selftest reason", | ||
| 4372 | "the forfeit is read back from pool_rounds.json"); | ||
| 4373 | } | ||
| 4374 | |||
| 4375 | remove(roundsPath.c_str()); | ||
| 4376 | remove((roundsPath + ".tmp").c_str()); | ||
| 4377 | strPoolRoundsFile = savedRoundsFile; | ||
| 4378 | } | ||
| 4379 | |||
| 4380 | // -- 11. A ledger that failed to load is never written back -------------- | ||
| 4381 | // | ||
| 4382 | // The loaders push entries into the live vector inside the try, so an | ||
| 4383 | // exception part way down a file leaves a PARTIAL ledger in memory: the | ||
| 4384 | // entries before the bad one and nothing after it. Publishing that -- and | ||
| 4385 | // ReconcilePayouts publishes unconditionally, milliseconds into startup -- | ||
| 4386 | // writes the partial state over the file and destroys the only record of | ||
| 4387 | // what was owed. | ||
| 4388 | { | ||
| 4389 | PayoutTestResetState(); | ||
| 4390 | remove(ledgerPath.c_str()); | ||
| 4391 | |||
| 4392 | // Two entries, the second with a matureAtHeight that is not a number. | ||
| 4393 | // The first loads; the second throws where it is read. | ||
| 4394 | const std::string corrupt = strprintf( | ||
| 4395 | "[{\"amounts\":{\"%s\":100000000},\"blockHash\":\"%s\"," | ||
| 4396 | "\"blockHeight\":10,\"matureAtHeight\":10}," | ||
| 4397 | "{\"amounts\":{\"%s\":900000000},\"blockHash\":\"%s\"," | ||
| 4398 | "\"blockHeight\":20,\"matureAtHeight\":\"twenty\"}]", | ||
| 4399 | addrA.c_str(), uint256(0).ToString().c_str(), | ||
| 4400 | addrB.c_str(), uint256(0).ToString().c_str()); | ||
| 4401 | check(PayoutTestWriteFile(ledgerPath, corrupt), | ||
| 4402 | "wrote a ledger whose second entry does not parse"); | ||
| 4403 | |||
| 4404 | const int64 t0 = GetTime(); | ||
| 4405 | LoadPendingPayouts(); | ||
| 4406 | const int64 t1 = GetTime(); | ||
| 4407 | |||
| 4408 | size_t loaded = 0; | ||
| 4409 | { | ||
| 4410 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4411 | loaded = gPendingPayouts.size(); | ||
| 4412 | } | ||
| 4413 | check(loaded == 1, | ||
| 4414 | "a mid-file parse error does leave a partial ledger in memory --" | ||
| 4415 | " which is the reason for everything below"); | ||
| 4416 | |||
| 4417 | std::string gone; | ||
| 4418 | check(!PayoutTestReadFile(ledgerPath, gone), | ||
| 4419 | "the file that did not parse is moved out of the way"); | ||
| 4420 | |||
| 4421 | // The rename stamps GetTime(); it was taken between t0 and t1. | ||
| 4422 | std::string kept, keptPath; | ||
| 4423 | for (int64 t = t0; t <= t1 && keptPath.empty(); t++) { | ||
| 4424 | const std::string candidate = | ||
| 4425 | strprintf("%s.bad.%lld", ledgerPath.c_str(), (long long)t); | ||
| 4426 | if (PayoutTestReadFile(candidate, kept)) | ||
| 4427 | keptPath = candidate; | ||
| 4428 | } | ||
| 4429 | check(!keptPath.empty() && kept == corrupt, | ||
| 4430 | "it is kept, byte for byte, as <path>.bad.<timestamp>"); | ||
| 4431 | |||
| 4432 | // The publish that used to destroy it. | ||
| 4433 | VersionedBlob partial; | ||
| 4434 | { | ||
| 4435 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4436 | partial = SerializePendingPayoutsLocked(); | ||
| 4437 | } | ||
| 4438 | PublishPendingPayouts(partial); | ||
| 4439 | check(!PayoutTestReadFile(ledgerPath, gone), | ||
| 4440 | "and the partial ledger is not published over it"); | ||
| 4441 | |||
| 4442 | // The refusal is per path and lasts the process: a second attempt, | ||
| 4443 | // with more state, is refused too. | ||
| 4444 | { | ||
| 4445 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4446 | PendingPayout pp; | ||
| 4447 | pp.blockHeight = 30; pp.blockHash = 0; pp.matureAtHeight = 30; | ||
| 4448 | pp.amounts[addrA] = 5 * COIN; | ||
| 4449 | gPendingPayouts.push_back(pp); | ||
| 4450 | partial = SerializePendingPayoutsLocked(); | ||
| 4451 | } | ||
| 4452 | PublishPendingPayouts(partial); | ||
| 4453 | check(!PayoutTestReadFile(ledgerPath, gone), | ||
| 4454 | "the refusal is not a one-shot -- the path stays unwritable"); | ||
| 4455 | |||
| 4456 | if (!keptPath.empty()) | ||
| 4457 | remove(keptPath.c_str()); | ||
| 4458 | PayoutTestResetState(); | ||
| 4459 | remove(ledgerPath.c_str()); | ||
| 4460 | |||
| 4461 | // And a file that parses is not quarantined, so the ordinary path is | ||
| 4462 | // still the ordinary path. | ||
| 4463 | check(PayoutTestWriteFile(ledgerPath, "[]"), | ||
| 4464 | "wrote a ledger that parses"); | ||
| 4465 | LoadPendingPayouts(); | ||
| 4466 | { | ||
| 4467 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4468 | PendingPayout pp; | ||
| 4469 | pp.blockHeight = 40; pp.blockHash = 0; pp.matureAtHeight = 40; | ||
| 4470 | pp.amounts[addrA] = 2 * COIN; | ||
| 4471 | gPendingPayouts.push_back(pp); | ||
| 4472 | partial = SerializePendingPayoutsLocked(); | ||
| 4473 | } | ||
| 4474 | PublishPendingPayouts(partial); | ||
| 4475 | std::string written; | ||
| 4476 | check(PayoutTestReadFile(ledgerPath, written) && written == partial.blob, | ||
| 4477 | "a ledger that loaded cleanly is still published normally"); | ||
| 4478 | } | ||
| 4479 | |||
| 4480 | // -- 12. A send that failed for want of coin is not a fault -------------- | ||
| 4481 | // | ||
| 4482 | // The defect this replaces: the classifier decided "was this a funding | ||
| 4483 | // problem?" by re-reading GetBalance() and allowing amount + | ||
| 4484 | // nTransactionFee + CENT. nTransactionFee is 0 on a headless node, and | ||
| 4485 | // GetMinFee() is a step function -- 0 below 10,000 bytes, 11 * CENT at it | ||
| 4486 | // -- so the allowance was either unnecessary or eleven times too small, | ||
| 4487 | // and the ledger's own retry budget then wrote the debt off after 1440 | ||
| 4488 | // repeats of the same stable condition. | ||
| 4489 | // | ||
| 4490 | // This is tested against a real wallet and the real CreateTransaction, not | ||
| 4491 | // against injected values: the honest question is not "does my enum | ||
| 4492 | // switch work" but "does a wallet holding many small coins actually report | ||
| 4493 | // the state I think it does". 60 outputs of 1 BTF give ~180 signed P2PKH | ||
| 4494 | // bytes each, which is where the fee schedule steps. Nothing is committed | ||
| 4495 | // or broadcast -- CreateTransaction only builds. | ||
| 4496 | { | ||
| 4497 | PayoutTestResetState(); | ||
| 4498 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4499 | mapWallet.clear(); | ||
| 4500 | |||
| 4501 | // CreateTransaction opens CTxDB("r"), which throws if blkindex.dat is | ||
| 4502 | // not there. Create it in the scratch datadir first. | ||
| 4503 | bool dbOk = true; | ||
| 4504 | try { | ||
| 4505 | CTxDB txdb("cr"); | ||
| 4506 | } catch (const std::exception&) { | ||
| 4507 | dbOk = false; | ||
| 4508 | } | ||
| 4509 | check(dbOk, "a scratch block index exists for CreateTransaction to open"); | ||
| 4510 | |||
| 4511 | const std::string walletAddr = PayoutTestAddSpendableKey(); | ||
| 4512 | const std::string payee = PayoutTestAddress(0x77); | ||
| 4513 | const CScript payeeScript = PayoutTestScriptFor(payee); | ||
| 4514 | |||
| 4515 | // -- the >= 10 KB case: 60 small coins, balance = amount + 0.05 ------ | ||
| 4516 | PayoutTestFundWallet(60, 1 * COIN, walletAddr); | ||
| 4517 | const int64 balance = GetBalance(); | ||
| 4518 | check(balance == 60 * COIN, | ||
| 4519 | "the test wallet holds 60 separately spendable coins"); | ||
| 4520 | |||
| 4521 | const int64 amount = balance - 5 * CENT; | ||
| 4522 | // The pre-send gate in PayoutThreadFn passes: the balance covers the | ||
| 4523 | // amount and the only fee that can be known in advance. | ||
| 4524 | check(balance >= amount + nTransactionFee, | ||
| 4525 | "the pre-send gate would let this payment through"); | ||
| 4526 | |||
| 4527 | CWalletTx wtx; | ||
| 4528 | SendMoneyOutcome outcome; | ||
| 4529 | int64 feeRequired = 0; | ||
| 4530 | WalletSendResult result = WALLET_SEND_OK; | ||
| 4531 | bool built = true; | ||
| 4532 | if (dbOk) { | ||
| 4533 | try { | ||
| 4534 | built = CreateTransaction(payeeScript, amount, wtx, feeRequired, | ||
| 4535 | &result); | ||
| 4536 | } catch (const std::exception&) { | ||
| 4537 | built = true; // report as a failure below | ||
| 4538 | } | ||
| 4539 | } | ||
| 4540 | check(dbOk && !built, | ||
| 4541 | "a wallet of many small coins cannot fund amount + the fee its" | ||
| 4542 | " own size demands"); | ||
| 4543 | check(dbOk && result == WALLET_SEND_INSUFFICIENT_FUNDS, | ||
| 4544 | "the wallet reports that as a funding shortfall, not as a" | ||
| 4545 | " failure to build"); | ||
| 4546 | // GetMinFee(fDiscount) returns 0 below 10,000 bytes and | ||
| 4547 | // (1 + bytes/1000) * CENT at or above, so a required fee of 11 CENT or | ||
| 4548 | // more IS the statement that the transaction crossed 10 KB. Measured | ||
| 4549 | // through the real serialiser rather than asserted. | ||
| 4550 | check(dbOk && feeRequired >= 11 * CENT, | ||
| 4551 | "the transaction it built crossed 10,000 bytes, so the fee" | ||
| 4552 | " stepped from 0 to at least 11 CENT"); | ||
| 4553 | |||
| 4554 | // The defect, stated as arithmetic. The old classifier asked whether | ||
| 4555 | // the balance covered amount + nTransactionFee + CENT; it did, so it | ||
| 4556 | // called a permanent funding shortfall a fault and started counting | ||
| 4557 | // towards a write-off. | ||
| 4558 | check(!(balance < amount + nTransactionFee + CENT), | ||
| 4559 | "the reserve this replaces would have judged this send a fault"); | ||
| 4560 | check(balance < amount + feeRequired, | ||
| 4561 | "while the fee actually required was more than the balance had"); | ||
| 4562 | |||
| 4563 | outcome.result = result; | ||
| 4564 | outcome.nFeeRequired = feeRequired; | ||
| 4565 | check(dbOk && ClassifyPayoutSendFailure(outcome.result, false) | ||
| 4566 | == PAYOUT_FAILURE_DEFER, | ||
| 4567 | "a >= 10 KB send that ran out of coin is deferred, not counted" | ||
| 4568 | " against the payment"); | ||
| 4569 | |||
| 4570 | // -- the < 10 KB case: no reserve is needed at all ------------------- | ||
| 4571 | // | ||
| 4572 | // The other half of the arithmetic. Below the step the required fee is | ||
| 4573 | // 0, so a reserve of any size is a reason to refuse a payment the | ||
| 4574 | // wallet could have made -- which is its own way of not paying a | ||
| 4575 | // miner. | ||
| 4576 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4577 | mapWallet.clear(); | ||
| 4578 | PayoutTestFundWallet(3, 1 * COIN, walletAddr); | ||
| 4579 | const int64 smallAmount = 3 * COIN - 5 * CENT; | ||
| 4580 | CWalletTx smallTx; | ||
| 4581 | int64 smallFee = -1; | ||
| 4582 | WalletSendResult smallResult = WALLET_SEND_FAILED; | ||
| 4583 | bool smallBuilt = false; | ||
| 4584 | if (dbOk) { | ||
| 4585 | try { | ||
| 4586 | smallBuilt = CreateTransaction(payeeScript, smallAmount, smallTx, | ||
| 4587 | smallFee, &smallResult); | ||
| 4588 | } catch (const std::exception&) { | ||
| 4589 | smallBuilt = false; | ||
| 4590 | } | ||
| 4591 | } | ||
| 4592 | check(dbOk && smallBuilt && smallResult == WALLET_SEND_OK, | ||
| 4593 | "the same balance pays a small transaction with no headroom at" | ||
| 4594 | " all"); | ||
| 4595 | check(dbOk && smallFee == 0, | ||
| 4596 | "because below 10,000 bytes the required fee is zero"); | ||
| 4597 | |||
| 4598 | // -- and the classification of every other answer -------------------- | ||
| 4599 | check(ClassifyPayoutSendFailure(WALLET_SEND_INSUFFICIENT_FUNDS, false) | ||
| 4600 | == PAYOUT_FAILURE_DEFER && | ||
| 4601 | ClassifyPayoutSendFailure(WALLET_SEND_INSUFFICIENT_FUNDS, true) | ||
| 4602 | == PAYOUT_FAILURE_DEFER && | ||
| 4603 | ClassifyPayoutSendFailure(WALLET_SEND_FAILED, true) | ||
| 4604 | == PAYOUT_FAILURE_DEFER, | ||
| 4605 | "short of coin or unable to sign is always a deferral"); | ||
| 4606 | check(ClassifyPayoutSendFailure(WALLET_SEND_FAILED, false) | ||
| 4607 | == PAYOUT_FAILURE_FAULT, | ||
| 4608 | "funded and unlocked and still refused is a fault"); | ||
| 4609 | |||
| 4610 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4611 | mapWallet.clear(); | ||
| 4612 | } | ||
| 4613 | |||
| 4614 | // -- 13. No payout mutex is held across the status-file write ------------ | ||
| 4615 | // | ||
| 4616 | // RefreshStats runs on the event loop -- the thread that owns every | ||
| 4617 | // miner's socket -- and used to hold gStatsMutex and gPayoutLedgerMutex at | ||
| 4618 | // function scope across WriteFileAtomic. Once that write gained fsync() on | ||
| 4619 | // the file and on its directory, publishing pool_status.json parked the | ||
| 4620 | // payout thread behind two disk flushes every ten seconds. | ||
| 4621 | // | ||
| 4622 | // Timing assertions would be flaky, so this does not measure anything. It | ||
| 4623 | // makes the write block for as long as the test likes: strPoolStatusFile | ||
| 4624 | // points at a path whose ".tmp" is a FIFO, and open(2) for writing on a | ||
| 4625 | // FIFO does not return until a reader arrives. While RefreshStats is | ||
| 4626 | // parked in there, every mutex it must NOT be holding has to be | ||
| 4627 | // acquirable. Against the previous code this test does not merely fail, it | ||
| 4628 | // cannot pass: the writer is inside fopen() with both mutexes held and | ||
| 4629 | // nothing will release them until the test lets the write finish. | ||
| 4630 | #ifndef _WIN32 | ||
| 4631 | { | ||
| 4632 | PayoutTestResetState(); | ||
| 4633 | const std::string statusPath = dir + "/pool_status_selftest.json"; | ||
| 4634 | const std::string statusTmp = statusPath + ".tmp"; | ||
| 4635 | const std::string savedStatusFile = strPoolStatusFile; | ||
| 4636 | const int64 savedLastWrite = gLastPoolStatusWrite; | ||
| 4637 | remove(statusPath.c_str()); | ||
| 4638 | remove(statusTmp.c_str()); | ||
| 4639 | strPoolStatusFile = statusPath; | ||
| 4640 | gLastPoolStatusWrite = 0; // due a publish on the next refresh | ||
| 4641 | |||
| 4642 | // Give the status file something to report, so the ledger mutex is | ||
| 4643 | // genuinely taken on the way through rather than skipped over. | ||
| 4644 | { | ||
| 4645 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4646 | PendingPayout pp; | ||
| 4647 | pp.blockHeight = 909; pp.blockHash = 0; pp.matureAtHeight = 909; | ||
| 4648 | pp.amounts[addrA] = 3 * COIN; | ||
| 4649 | gPendingPayouts.push_back(pp); | ||
| 4650 | } | ||
| 4651 | |||
| 4652 | bool fifoOk = (mkfifo(statusTmp.c_str(), 0600) == 0); | ||
| 4653 | check(fifoOk, "a FIFO stands in for the status file's temp file, so the" | ||
| 4654 | " write can be held open"); | ||
| 4655 | if (fifoOk) { | ||
| 4656 | std::atomic<bool> done(false); | ||
| 4657 | std::thread writer([&done]() { | ||
| 4658 | RefreshStats(0, 0); | ||
| 4659 | done = true; | ||
| 4660 | }); | ||
| 4661 | |||
| 4662 | // Settle: the work before the write is a handful of vector | ||
| 4663 | // operations, so anything past this point is inside fopen(). The | ||
| 4664 | // !done check below is what actually proves it -- with no reader | ||
| 4665 | // there is no other way for this thread to still be running. | ||
| 4666 | Sleep(500); | ||
| 4667 | bool blocked = !done; | ||
| 4668 | bool gotLedger = false, gotStats = false, gotExec = false, | ||
| 4669 | gotRounds = false; | ||
| 4670 | for (int i = 0; i < 1000 && blocked; i++) { | ||
| 4671 | if (!gotLedger && gPayoutLedgerMutex.try_lock()) { | ||
| 4672 | gotLedger = true; gPayoutLedgerMutex.unlock(); | ||
| 4673 | } | ||
| 4674 | if (!gotStats && gStatsMutex.try_lock()) { | ||
| 4675 | gotStats = true; gStatsMutex.unlock(); | ||
| 4676 | } | ||
| 4677 | if (!gotExec && gPayoutExecMutex.try_lock()) { | ||
| 4678 | gotExec = true; gPayoutExecMutex.unlock(); | ||
| 4679 | } | ||
| 4680 | if (!gotRounds && gPoolRoundsMutex.try_lock()) { | ||
| 4681 | gotRounds = true; gPoolRoundsMutex.unlock(); | ||
| 4682 | } | ||
| 4683 | if (gotLedger && gotStats && gotExec && gotRounds) | ||
| 4684 | break; | ||
| 4685 | Sleep(2); | ||
| 4686 | } | ||
| 4687 | |||
| 4688 | check(blocked, | ||
| 4689 | "the status-file write is held open for the duration of this" | ||
| 4690 | " test"); | ||
| 4691 | check(!blocked || gotLedger, | ||
| 4692 | "gPayoutLedgerMutex is free while pool_status.json is being" | ||
| 4693 | " written"); | ||
| 4694 | check(!blocked || (gotExec && gotRounds), | ||
| 4695 | "gPayoutExecMutex and gPoolRoundsMutex are free while" | ||
| 4696 | " pool_status.json is being written"); | ||
| 4697 | check(!blocked || gotStats, | ||
| 4698 | "gStatsMutex is free while pool_status.json is being written"); | ||
| 4699 | |||
| 4700 | // Let the write finish. Drain to EOF rather than closing early: | ||
| 4701 | // the writer is still filling the pipe and a closed read end would | ||
| 4702 | // deliver it SIGPIPE. | ||
| 4703 | std::thread reader([&statusTmp]() { | ||
| 4704 | FILE* rf = fopen(statusTmp.c_str(), "r"); | ||
| 4705 | if (!rf) return; | ||
| 4706 | char buf[4096]; | ||
| 4707 | while (fread(buf, 1, sizeof(buf), rf) > 0) { } | ||
| 4708 | fclose(rf); | ||
| 4709 | }); | ||
| 4710 | writer.join(); | ||
| 4711 | reader.join(); | ||
| 4712 | } | ||
| 4713 | remove(statusTmp.c_str()); | ||
| 4714 | |||
| 4715 | // And the ordinary path still produces the file it always did. | ||
| 4716 | gLastPoolStatusWrite = 0; | ||
| 4717 | RefreshStats(0, 0); | ||
| 4718 | std::string onDisk; | ||
| 4719 | bool statusOk = false; | ||
| 4720 | if (PayoutTestReadFile(statusPath, onDisk)) { | ||
| 4721 | try { | ||
| 4722 | nlohmann::json j = nlohmann::json::parse(onDisk); | ||
| 4723 | statusOk = j.value("schema", "") == "bitflash-pool-status-1" && | ||
| 4724 | j["pendingPayouts"].is_array() && | ||
| 4725 | j["pendingPayouts"].size() == 1 && | ||
| 4726 | j["pendingPayouts"][0].value("matureAtHeight", 0) == 909 && | ||
| 4727 | j["pendingPayouts"][0].value("totalAmountSatoshis", | ||
| 4728 | (int64)0) == 3 * COIN; | ||
| 4729 | } catch (...) { | ||
| 4730 | statusOk = false; | ||
| 4731 | } | ||
| 4732 | } | ||
| 4733 | check(statusOk, | ||
| 4734 | "splitting the serialise from the write leaves pool_status.json" | ||
| 4735 | " saying the same thing"); | ||
| 4736 | |||
| 4737 | remove(statusPath.c_str()); | ||
| 4738 | remove((statusPath + ".tmp").c_str()); | ||
| 4739 | strPoolStatusFile = savedStatusFile; | ||
| 4740 | gLastPoolStatusWrite = savedLastWrite; | ||
| 4741 | } | ||
| 4742 | #endif | ||
| 4743 | |||
| 4744 | // -- 14. The payout comment names the funding block ---------------------- | ||
| 4745 | // | ||
| 4746 | // Same height, two hashes: the shape a reorg leaves when the pool wins a | ||
| 4747 | // height, loses the block, and wins the height again. The legacy needle is | ||
| 4748 | // frozen at the pre-upgrade format, so pre-upgrade payments keep matching; | ||
| 4749 | // a new-format comment never matches it, so a new send can never be read | ||
| 4750 | // as a pre-upgrade payment for a twin round. | ||
| 4751 | { | ||
| 4752 | PayoutTestResetState(); | ||
| 4753 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4754 | mapWallet.clear(); | ||
| 4755 | PayoutTestClearBlockIndex(); | ||
| 4756 | |||
| 4757 | const int height = 9090; | ||
| 4758 | const int64 amount = 3 * COIN; | ||
| 4759 | uint256 hashA = 0, hashB = 0; | ||
| 4760 | hashA.SetHex("00000000000000000000000000000000000000000000000000000000000000aa"); | ||
| 4761 | hashB.SetHex("00000000000000000000000000000000000000000000000000000000000000bb"); | ||
| 4762 | |||
| 4763 | check(LegacyPayoutComment(height) == | ||
| 4764 | strprintf("Pool payout height %d", height), | ||
| 4765 | "the legacy needle is frozen at the format pre-upgrade builds" | ||
| 4766 | " wrote"); | ||
| 4767 | check(PayoutComment(height, 0) == LegacyPayoutComment(height), | ||
| 4768 | "a grandfathered zero-hash record still writes the legacy form"); | ||
| 4769 | check(PayoutComment(height, hashA) == | ||
| 4770 | strprintf("Pool payout height %d block %s", height, | ||
| 4771 | hashA.ToString().c_str()), | ||
| 4772 | "a hash-keyed record's comment names its funding block"); | ||
| 4773 | check(PayoutComment(height, hashA) != PayoutComment(height, hashB), | ||
| 4774 | "twin rounds at one height carry distinguishable comments"); | ||
| 4775 | |||
| 4776 | // A transaction wearing the NEW comment format and -- hypothetically | ||
| 4777 | // -- no structured metadata must not be matched by the legacy needle: | ||
| 4778 | // the fallback exists for pre-upgrade transactions, and the new format | ||
| 4779 | // proves this is not one. | ||
| 4780 | const std::string idA = PayoutIdForBlock(height, hashA); | ||
| 4781 | const std::string idB = PayoutIdForBlock(height, hashB); | ||
| 4782 | std::set<std::string> none; | ||
| 4783 | std::string found; | ||
| 4784 | // Neither twin has a round proof here (section 15 covers the gate), | ||
| 4785 | // and their ids collide, so one answer serves both. | ||
| 4786 | const bool fAllowLegacy = !PoolRoundHasProof(idA); | ||
| 4787 | PayoutTestAddWalletTx(PayoutComment(height, hashA), addrA, amount, 1, ""); | ||
| 4788 | check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, fAllowLegacy, none, found) | ||
| 4789 | == PAYOUT_PROBE_NONE, | ||
| 4790 | "a new-format comment is never legacy evidence for a twin round"); | ||
| 4791 | check(FindRecordedPoolPayout(idA, height, hashA, addrA, amount, fAllowLegacy, none, found) | ||
| 4792 | == PAYOUT_PROBE_NONE, | ||
| 4793 | "nor a match channel for its own round -- structured metadata is"); | ||
| 4794 | |||
| 4795 | // Round A's own payment, sent by this build, is found by its | ||
| 4796 | // structured id. | ||
| 4797 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4798 | mapWallet.clear(); | ||
| 4799 | PayoutTestClearBlockIndex(); | ||
| 4800 | const std::string paidA = | ||
| 4801 | PayoutTestAddWalletTx(PayoutComment(height, hashA), addrA, amount, | ||
| 4802 | 2, idA); | ||
| 4803 | check(FindRecordedPoolPayout(idA, height, hashA, addrA, amount, fAllowLegacy, none, found) | ||
| 4804 | == PAYOUT_PROBE_STRUCTURED && found == paidA, | ||
| 4805 | "the winning twin's own payment is found by its structured id"); | ||
| 4806 | |||
| 4807 | // The id is derived from the FIRST sixteen hex digits of the hash -- | ||
| 4808 | // the end proof-of-work fills with zeros -- so twin hashes that agree | ||
| 4809 | // there collide in id, and a payment carrying no pool_payout_block | ||
| 4810 | // answers for either twin. That is the accepted trade for payments | ||
| 4811 | // sent before the key existed, pinned here so it stays a decision. | ||
| 4812 | check(idA == idB, | ||
| 4813 | "twin hashes differing only past the sixteenth hex digit collide" | ||
| 4814 | " in id -- the reason the full hash must decide"); | ||
| 4815 | check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, fAllowLegacy, none, found) | ||
| 4816 | == PAYOUT_PROBE_STRUCTURED, | ||
| 4817 | "a payment without the block key answers for a colliding twin --" | ||
| 4818 | " the accepted residual for pre-key transactions"); | ||
| 4819 | |||
| 4820 | // With the full hash recorded -- as every send now records it -- the | ||
| 4821 | // twins are told apart and the re-won height is paid. | ||
| 4822 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4823 | mapWallet.clear(); | ||
| 4824 | PayoutTestClearBlockIndex(); | ||
| 4825 | const std::string keyedA = | ||
| 4826 | PayoutTestAddWalletTx(PayoutComment(height, hashA), addrA, amount, | ||
| 4827 | 3, idA, true, hashA.ToString()); | ||
| 4828 | check(FindRecordedPoolPayout(idA, height, hashA, addrA, amount, fAllowLegacy, none, found) | ||
| 4829 | == PAYOUT_PROBE_STRUCTURED && found == keyedA, | ||
| 4830 | "a payment carrying the full block hash settles its own round"); | ||
| 4831 | check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, fAllowLegacy, none, found) | ||
| 4832 | == PAYOUT_PROBE_NONE, | ||
| 4833 | "and is no evidence for the twin, so the re-won height is paid"); | ||
| 4834 | |||
| 4835 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4836 | mapWallet.clear(); | ||
| 4837 | PayoutTestClearBlockIndex(); | ||
| 4838 | } | ||
| 4839 | |||
| 4840 | // -- 15. A proof-bearing round rejects comment-only evidence ------------- | ||
| 4841 | // | ||
| 4842 | // The surviving half of the same-height collision: a pre-upgrade | ||
| 4843 | // transaction carries only "Pool payout height H", so if H is won again | ||
| 4844 | // after a reorg, it matches the new round's (height, address, amount) | ||
| 4845 | // exactly and would settle a debt it never paid. The new round has a | ||
| 4846 | // round proof -- written at creation, and only by builds whose payments | ||
| 4847 | // all name their round -- so for it, comment-only evidence is | ||
| 4848 | // inadmissible and the miner is paid. A round without a proof keeps | ||
| 4849 | // today's bias: the evidence is admissible and doubt resolves to | ||
| 4850 | // "already paid". | ||
| 4851 | { | ||
| 4852 | PayoutTestResetState(); | ||
| 4853 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4854 | mapWallet.clear(); | ||
| 4855 | PayoutTestClearBlockIndex(); | ||
| 4856 | const std::string roundsPath = dir + "/pool_rounds_gate_selftest.json"; | ||
| 4857 | const std::string savedRoundsFile = strPoolRoundsFile; | ||
| 4858 | strPoolRoundsFile = roundsPath; | ||
| 4859 | remove(roundsPath.c_str()); | ||
| 4860 | |||
| 4861 | const int height = 7171; | ||
| 4862 | const int64 amount = 4 * COIN; | ||
| 4863 | uint256 hashB = 0; | ||
| 4864 | hashB.SetHex("00000000000000000000000000000000000000000000000000000000000000cc"); | ||
| 4865 | const std::string idB = PayoutIdForBlock(height, hashB); | ||
| 4866 | std::set<std::string> none; | ||
| 4867 | std::string found; | ||
| 4868 | |||
| 4869 | // The colliding pre-upgrade payment: confirmed, comment-only, same | ||
| 4870 | // height, same address, same amount -- the strongest wrong evidence | ||
| 4871 | // there is. | ||
| 4872 | PayoutTestAddWalletTx(LegacyPayoutComment(height), addrA, amount, 1, ""); | ||
| 4873 | |||
| 4874 | check(!PoolRoundHasProof(idB), | ||
| 4875 | "a round with no recorded proof does not claim one"); | ||
| 4876 | check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, | ||
| 4877 | !PoolRoundHasProof(idB), none, found) | ||
| 4878 | == PAYOUT_PROBE_LEGACY, | ||
| 4879 | "without a proof the doubt is genuine and still resolves to" | ||
| 4880 | " already-paid"); | ||
| 4881 | |||
| 4882 | PoolRoundProof round; | ||
| 4883 | round.id = idB; | ||
| 4884 | round.blockHeight = height; | ||
| 4885 | round.matureAtHeight = height + COINBASE_SPENDABLE_DEPTH; | ||
| 4886 | round.payouts[addrA] = amount; | ||
| 4887 | RecordPoolRound(round); | ||
| 4888 | check(PoolRoundHasProof(idB), | ||
| 4889 | "recording the round proof marks the round as this lineage's"); | ||
| 4890 | check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, | ||
| 4891 | !PoolRoundHasProof(idB), none, found) | ||
| 4892 | == PAYOUT_PROBE_NONE, | ||
| 4893 | "with a proof, a same-height comment-only transaction is not" | ||
| 4894 | " evidence, and the re-won height is paid"); | ||
| 4895 | |||
| 4896 | // Structured evidence still is: the round's own payment, recovered. | ||
| 4897 | const std::string paidB = | ||
| 4898 | PayoutTestAddWalletTx(PayoutComment(height, hashB), addrA, amount, | ||
| 4899 | 2, idB, true, hashB.ToString()); | ||
| 4900 | check(FindRecordedPoolPayout(idB, height, hashB, addrA, amount, | ||
| 4901 | !PoolRoundHasProof(idB), none, found) | ||
| 4902 | == PAYOUT_PROBE_STRUCTURED && found == paidB, | ||
| 4903 | "the gate does not blind the probe to the round's own structured" | ||
| 4904 | " payment"); | ||
| 4905 | |||
| 4906 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4907 | mapWallet.clear(); | ||
| 4908 | PayoutTestClearBlockIndex(); | ||
| 4909 | remove(roundsPath.c_str()); | ||
| 4910 | remove((roundsPath + ".tmp").c_str()); | ||
| 4911 | strPoolRoundsFile = savedRoundsFile; | ||
| 4912 | } | ||
| 4913 | |||
| 4914 | // -- 16. A malformed block hash is corruption, not a hash ---------------- | ||
| 4915 | // | ||
| 4916 | // SetHex() parses whatever hex prefix it finds and stops, so a corrupt | ||
| 4917 | // blockHash did not fail to load -- it loaded as a DIFFERENT hash. | ||
| 4918 | // "deadbeef..." became 0xdeadbeef, a hash no block ever had, which the | ||
| 4919 | // orphan check reads as an orphaned block and the next flush VOIDS the | ||
| 4920 | // debt; a value with no hex prefix became 0, which is grandfathered past | ||
| 4921 | // the orphan check instead. Either way a corrupt file silently changed | ||
| 4922 | // what the pool owed. It is a load failure now, handled like every other | ||
| 4923 | // parse error: quarantine, and never rewrite. | ||
| 4924 | { | ||
| 4925 | PayoutTestResetState(); | ||
| 4926 | remove(ledgerPath.c_str()); | ||
| 4927 | remove(payingPath.c_str()); | ||
| 4928 | |||
| 4929 | const std::string badLedger = strprintf( | ||
| 4930 | "[{\"amounts\":{\"%s\":100000000}," | ||
| 4931 | "\"blockHash\":\"deadbeef oops, not a hash\"," | ||
| 4932 | "\"blockHeight\":10,\"matureAtHeight\":10}]", addrA.c_str()); | ||
| 4933 | check(PayoutTestWriteFile(ledgerPath, badLedger), | ||
| 4934 | "wrote a ledger whose blockHash is not a 64-digit hex string"); | ||
| 4935 | LoadPendingPayouts(); | ||
| 4936 | { | ||
| 4937 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4938 | check(gPendingPayouts.empty(), | ||
| 4939 | "no entry is loaded with a guessed hash"); | ||
| 4940 | } | ||
| 4941 | std::string gone; | ||
| 4942 | check(!PayoutTestReadFile(ledgerPath, gone), | ||
| 4943 | "the ledger with the corrupt hash is quarantined"); | ||
| 4944 | |||
| 4945 | const std::string badPaying = strprintf( | ||
| 4946 | "[{\"address\":\"%s\",\"amount\":100000000," | ||
| 4947 | "\"blockHash\":\"00000000cafe\",\"blockHeight\":10," | ||
| 4948 | "\"matureAtHeight\":10}]", addrA.c_str()); | ||
| 4949 | check(PayoutTestWriteFile(payingPath, badPaying), | ||
| 4950 | "wrote an in-flight list whose blockHash is truncated"); | ||
| 4951 | LoadPaying(); | ||
| 4952 | { | ||
| 4953 | std::lock_guard<std::mutex> lk(gPayoutExecMutex); | ||
| 4954 | check(gPaying.empty(), | ||
| 4955 | "no in-flight record is loaded with a guessed hash"); | ||
| 4956 | } | ||
| 4957 | check(!PayoutTestReadFile(payingPath, gone), | ||
| 4958 | "the in-flight list with the corrupt hash is quarantined"); | ||
| 4959 | |||
| 4960 | // The two spellings every real file uses still load: the 64-digit | ||
| 4961 | // hash of a keyed round, and the 64 zeros this build writes for a | ||
| 4962 | // grandfathered entry. | ||
| 4963 | PayoutTestResetState(); | ||
| 4964 | remove(ledgerPath.c_str()); | ||
| 4965 | const std::string goodLedger = strprintf( | ||
| 4966 | "[{\"amounts\":{\"%s\":100000000}," | ||
| 4967 | "\"blockHash\":\"00000000000000000000000000000000000000000000000000000000000000cd\"," | ||
| 4968 | "\"blockHeight\":10,\"matureAtHeight\":10}," | ||
| 4969 | "{\"amounts\":{\"%s\":200000000}," | ||
| 4970 | "\"blockHash\":\"%s\"," | ||
| 4971 | "\"blockHeight\":20,\"matureAtHeight\":20}]", | ||
| 4972 | addrA.c_str(), addrB.c_str(), uint256(0).ToString().c_str()); | ||
| 4973 | check(PayoutTestWriteFile(ledgerPath, goodLedger), | ||
| 4974 | "wrote a ledger in both real hash spellings"); | ||
| 4975 | LoadPendingPayouts(); | ||
| 4976 | { | ||
| 4977 | std::lock_guard<std::mutex> lk(gPayoutLedgerMutex); | ||
| 4978 | check(gPendingPayouts.size() == 2 && | ||
| 4979 | gPendingPayouts[0].blockHash != 0 && | ||
| 4980 | gPendingPayouts[1].blockHash == 0, | ||
| 4981 | "a real hash and a serialised zero hash both still load"); | ||
| 4982 | } | ||
| 4983 | remove(ledgerPath.c_str()); | ||
| 4984 | } | ||
| 4985 | |||
| 4986 | // -- clean up ------------------------------------------------------------ | ||
| 4987 | PayoutTestResetState(); | ||
| 4988 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 4989 | mapWallet.clear(); | ||
| 4990 | PayoutTestClearBlockIndex(); | ||
| 4991 | // Out of the scratch directory before removing it, and out of the key maps | ||
| 4992 | // too: the keys this test generated are process-local, but leaving them in | ||
| 4993 | // the keystore would let a later test think a script was its own. | ||
| 4994 | CRITICAL_BLOCK(cs_mapKeys) | ||
| 4995 | { | ||
| 4996 | mapKeys.clear(); | ||
| 4997 | mapPubKeys.clear(); | ||
| 4998 | } | ||
| 4999 | #ifdef _WIN32 | ||
| 5000 | SetCurrentDirectoryA(cwd.c_str()); | ||
| 5001 | #else | ||
| 5002 | if (chdir(cwd.c_str()) != 0) { /* nothing useful to do about it */ } | ||
| 5003 | #endif | ||
| 5004 | PayoutTestRemoveDir(dir); | ||
| 5005 | strSetDataDir = savedDataDir; | ||
| 5006 | nBestHeight = savedBestHeight; | ||
| 5007 | |||
| 5008 | fprintf(stdout, "%s (%d failure%s)\n", nFail == 0 ? "ALL TESTS PASSED" : "TESTS FAILED", | ||
| 5009 | nFail, nFail == 1 ? "" : "s"); | ||
| 5010 | fflush(stdout); | ||
| 5011 | return nFail == 0 ? 0 : 1; | ||
| 5012 | } | ||
| 5013 | |||
| 1511 | int RunPoolStratumSelfTest() | 5014 | int RunPoolStratumSelfTest() |
| 1512 | { | 5015 | { |
| 1513 | int nFail = 0; | 5016 | int nFail = 0; |
| @@ -1640,6 +5143,82 @@ int RunPoolStratumSelfTest() | |||
| 1640 | strPoolRoundsFile = savedRoundsFile; | 5143 | strPoolRoundsFile = savedRoundsFile; |
| 1641 | gPoolRounds = savedRounds; | 5144 | gPoolRounds = savedRounds; |
| 1642 | 5145 | ||
| 5146 | // -- hashrate decay (#7) -------------------------------------------------- | ||
| 5147 | // The first version of this decayed m->hashRate in place from the event | ||
| 5148 | // loop's sweep, which made the reported figure a function of how the loop | ||
| 5149 | // happened to run rather than of how long the miner had been quiet. These | ||
| 5150 | // pin the properties that fixes: sampling changes nothing, and the answer | ||
| 5151 | // depends only on elapsed silence. | ||
| 5152 | { | ||
| 5153 | // A miner exactly on the vardiff target: 4096 hashes per share at | ||
| 5154 | // difficulty 1.0, one share per 15s, so 4096/15 H/s. Its expected | ||
| 5155 | // interval is 15s and its grace is 60s. | ||
| 5156 | const double base = 4096.0 / 15.0; | ||
| 5157 | const int64 t0 = 1766000000; // any plausible non-zero wall clock | ||
| 5158 | Miner decayed(INVALID_SOCKET); | ||
| 5159 | decayed.difficulty = 1.0; | ||
| 5160 | decayed.hashRate = base; | ||
| 5161 | decayed.lastShareTime = t0; | ||
| 5162 | |||
| 5163 | check(MinerReportedHashRate(&decayed, t0 + 60) == base, | ||
| 5164 | "hashrate decay: a miner inside its grace period reports its full estimate"); | ||
| 5165 | |||
| 5166 | // Sampling must not consume the estimate. Twenty passes is one second | ||
| 5167 | // of the event loop's old sweep; a thousand is nearly a minute of it. | ||
| 5168 | double once = MinerReportedHashRate(&decayed, t0 + 61); | ||
| 5169 | bool idempotent = (decayed.hashRate == base); | ||
| 5170 | for (int i = 0; i < 1000 && idempotent; i++) | ||
| 5171 | idempotent = (MinerReportedHashRate(&decayed, t0 + 61) == once) && | ||
| 5172 | (decayed.hashRate == base); | ||
| 5173 | check(idempotent, | ||
| 5174 | "hashrate decay: repeated sampling returns one value and leaves the estimate alone"); | ||
| 5175 | |||
| 5176 | // One second past a 60s grace is 1/15th of a half-life: ~4.5% off, not | ||
| 5177 | // the estimate gone. A miner at the vardiff target overshoots its grace | ||
| 5178 | // roughly 1.8% of the time (e^-4) purely by share-timing variance, and | ||
| 5179 | // must not be zeroed for it. | ||
| 5180 | check(once > 0.94 * base && once < 0.96 * base, | ||
| 5181 | "hashrate decay: one second past grace costs a few percent, not the estimate"); | ||
| 5182 | double three = MinerReportedHashRate(&decayed, t0 + 63); | ||
| 5183 | check(three > 0.86 * base && three < 0.88 * base, | ||
| 5184 | "hashrate decay: three seconds past grace costs ~13%"); | ||
| 5185 | |||
| 5186 | // One half-life past the grace is exactly half. | ||
| 5187 | double half = MinerReportedHashRate(&decayed, t0 + 75); | ||
| 5188 | check(half > 0.499 * base && half < 0.501 * base, | ||
| 5189 | "hashrate decay: one expected interval past grace halves the estimate"); | ||
| 5190 | |||
| 5191 | // A miner that has genuinely stopped ends up reported as nothing -- | ||
| 5192 | // the whole point of #7. The old in-place version could not get here: | ||
| 5193 | // its decay fed back into its own grace and settled into a 1/t crawl. | ||
| 5194 | check(MinerReportedHashRate(&decayed, t0 + 3600) == 0.0, | ||
| 5195 | "hashrate decay: a miner silent for an hour is reported as zero"); | ||
| 5196 | |||
| 5197 | // A clock that steps backwards must never inflate what we announce. | ||
| 5198 | check(MinerReportedHashRate(&decayed, t0 - 3600) == base, | ||
| 5199 | "hashrate decay: a backwards clock reports the estimate unchanged, never higher"); | ||
| 5200 | |||
| 5201 | // Fresh connection, and a connection with one share and so no interval | ||
| 5202 | // to have timed: both must report nothing rather than divide by zero. | ||
| 5203 | Miner fresh(INVALID_SOCKET); | ||
| 5204 | check(MinerReportedHashRate(&fresh, t0) == 0.0, | ||
| 5205 | "hashrate decay: a miner that never submitted a share reports zero"); | ||
| 5206 | Miner single(INVALID_SOCKET); | ||
| 5207 | single.lastShareTime = t0; | ||
| 5208 | check(MinerReportedHashRate(&single, t0 + 86400) == 0.0, | ||
| 5209 | "hashrate decay: a miner with no rate sample yet reports zero"); | ||
| 5210 | |||
| 5211 | // Nothing in the swept range may produce a NaN, an infinity, a negative | ||
| 5212 | // rate, or a rate above the estimate it started from. | ||
| 5213 | bool finite = true; | ||
| 5214 | for (int64 s = -100; s < 200000 && finite; s += 37) { | ||
| 5215 | double v = MinerReportedHashRate(&decayed, t0 + s); | ||
| 5216 | finite = !std::isnan(v) && !std::isinf(v) && v >= 0.0 && v <= base; | ||
| 5217 | } | ||
| 5218 | check(finite, | ||
| 5219 | "hashrate decay: stays finite, non-negative and non-amplifying across a long silence"); | ||
| 5220 | } | ||
| 5221 | |||
| 1643 | gCurrentJob = savedJob; | 5222 | gCurrentJob = savedJob; |
| 1644 | gHaveJob = savedHaveJob; | 5223 | gHaveJob = savedHaveJob; |
| 1645 | 5224 | ||
| @@ -1654,56 +5233,77 @@ int RunPoolStratumSelfTest() | |||
| 1654 | // --------------------------------------------------------------------------- | 5233 | // --------------------------------------------------------------------------- |
| 1655 | static void RefreshStats(int blocksFoundThisSession, uint64 roundShareTotal) | 5234 | static void RefreshStats(int blocksFoundThisSession, uint64 roundShareTotal) |
| 1656 | { | 5235 | { |
| 1657 | std::lock_guard<std::mutex> lk(gStatsMutex); | 5236 | std::string statusBlob; |
| 1658 | 5237 | bool fPublish = false; | |
| 1659 | gStats.authorizedMiners = 0; | 5238 | { |
| 1660 | gStats.blocksFound = blocksFoundThisSession; | 5239 | std::lock_guard<std::mutex> lk(gStatsMutex); |
| 1661 | gStats.roundShares = roundShareTotal; | 5240 | |
| 1662 | gStats.totalHashRate = 0.0; | 5241 | gStats.authorizedMiners = 0; |
| 1663 | for (Miner* m : gMiners) | 5242 | gStats.blocksFound = blocksFoundThisSession; |
| 1664 | if (m->authorised) gStats.authorizedMiners++; | 5243 | gStats.roundShares = roundShareTotal; |
| 1665 | 5244 | gStats.totalHashRate = 0.0; | |
| 1666 | gWorkerStats.clear(); | 5245 | for (Miner* m : gMiners) |
| 1667 | for (Miner* m : gMiners) { | 5246 | if (m->authorised) gStats.authorizedMiners++; |
| 1668 | if (!m->authorised || m->lastSeen <= 0) continue; | 5247 | |
| 1669 | PoolWorkerStatView v; | 5248 | gWorkerStats.clear(); |
| 1670 | v.address = m->address; | 5249 | // One timestamp for the whole sweep (#7): every miner is faded to the |
| 1671 | v.worker = m->worker; | 5250 | // same instant, so the per-worker rows and the total they are summed |
| 1672 | v.roundShares = m->roundShares; | 5251 | // into describe one moment rather than a smear across the loop. |
| 1673 | v.totalShares = m->totalShares; | 5252 | int64 nowStats = GetTime(); |
| 1674 | v.lastSeen = m->lastSeen; | 5253 | for (Miner* m : gMiners) { |
| 1675 | v.hashRate = m->hashRate; | 5254 | if (!m->authorised || m->lastSeen <= 0) continue; |
| 1676 | gWorkerStats.push_back(v); | 5255 | PoolWorkerStatView v; |
| 1677 | gStats.totalHashRate += m->hashRate; | 5256 | v.address = m->address; |
| 1678 | } | 5257 | v.worker = m->worker; |
| 1679 | std::sort(gWorkerStats.begin(), gWorkerStats.end(), | 5258 | v.roundShares = m->roundShares; |
| 1680 | [](const PoolWorkerStatView& a, const PoolWorkerStatView& b) { | 5259 | v.totalShares = m->totalShares; |
| 1681 | return a.roundShares != b.roundShares ? a.roundShares > b.roundShares | 5260 | v.lastSeen = m->lastSeen; |
| 1682 | : a.lastSeen > b.lastSeen; | 5261 | // Not m->hashRate: a miner that has stopped submitting shares must |
| 1683 | }); | 5262 | // not keep contributing its last sample to the announced total. |
| 5263 | v.hashRate = MinerReportedHashRate(m, nowStats); | ||
| 5264 | gWorkerStats.push_back(v); | ||
| 5265 | gStats.totalHashRate += v.hashRate; | ||
| 5266 | } | ||
| 5267 | std::sort(gWorkerStats.begin(), gWorkerStats.end(), | ||
| 5268 | [](const PoolWorkerStatView& a, const PoolWorkerStatView& b) { | ||
| 5269 | return a.roundShares != b.roundShares ? a.roundShares > b.roundShares | ||
| 5270 | : a.lastSeen > b.lastSeen; | ||
| 5271 | }); | ||
| 1684 | 5272 | ||
| 1685 | gPayoutViews.clear(); | 5273 | // Scoped, not held to the end of the function. At function scope this |
| 1686 | std::lock_guard<std::mutex> ledger(gPayoutLedgerMutex); | 5274 | // lock covered the status-file write below, so the event loop did two |
| 1687 | for (const PendingPayout& pp : gPendingPayouts) { | 5275 | // fsyncs holding the mutex the payout thread takes to record that a |
| 1688 | PendingPayoutView pv; | 5276 | // miner has been paid. |
| 1689 | pv.matureAtHeight = pp.matureAtHeight; | 5277 | gPayoutViews.clear(); |
| 1690 | pv.recipients = 0; | 5278 | { |
| 1691 | pv.totalAmount = 0; | 5279 | std::lock_guard<std::mutex> ledger(gPayoutLedgerMutex); |
| 1692 | for (auto& kv : pp.amounts) { | 5280 | for (const PendingPayout& pp : gPendingPayouts) { |
| 1693 | if (pp.paidTxids.count(kv.first)) | 5281 | PendingPayoutView pv; |
| 1694 | continue; | 5282 | pv.matureAtHeight = pp.matureAtHeight; |
| 1695 | pv.recipients++; | 5283 | pv.recipients = 0; |
| 1696 | pv.totalAmount += kv.second; | 5284 | pv.totalAmount = 0; |
| 5285 | for (auto& kv : pp.amounts) { | ||
| 5286 | if (pp.paidTxids.count(kv.first)) | ||
| 5287 | continue; | ||
| 5288 | pv.recipients++; | ||
| 5289 | pv.totalAmount += kv.second; | ||
| 5290 | } | ||
| 5291 | if (pv.recipients > 0) | ||
| 5292 | gPayoutViews.push_back(pv); | ||
| 5293 | } | ||
| 1697 | } | 5294 | } |
| 1698 | if (pv.recipients > 0) | ||
| 1699 | gPayoutViews.push_back(pv); | ||
| 1700 | } | ||
| 1701 | 5295 | ||
| 1702 | int64 now = GetTime(); | 5296 | int64 now = GetTime(); |
| 1703 | if (now - gLastPoolStatusWrite >= 10) { | 5297 | if (now - gLastPoolStatusWrite >= 10) { |
| 1704 | WritePoolStatusJsonLocked(); | 5298 | statusBlob = SerializePoolStatusLocked(); |
| 1705 | gLastPoolStatusWrite = now; | 5299 | gLastPoolStatusWrite = now; |
| 5300 | fPublish = true; | ||
| 5301 | } | ||
| 1706 | } | 5302 | } |
| 5303 | // Every payout mutex is released here, and gStatsMutex with them. This is | ||
| 5304 | // where the two fsyncs happen, and no other thread is waiting behind them. | ||
| 5305 | if (fPublish) | ||
| 5306 | PublishPoolStatus(statusBlob); | ||
| 1707 | } | 5307 | } |
| 1708 | 5308 | ||
| 1709 | // --------------------------------------------------------------------------- | 5309 | // --------------------------------------------------------------------------- |
| @@ -1714,6 +5314,7 @@ static void PoolEventLoop() | |||
| 1714 | int lastHeight = -1; | 5314 | int lastHeight = -1; |
| 1715 | int64 lastRefresh = GetTime(); | 5315 | int64 lastRefresh = GetTime(); |
| 1716 | int64 lastStatsRefresh = 0; | 5316 | int64 lastStatsRefresh = 0; |
| 5317 | int64 lastCensus = 0; // #19 connection census throttle | ||
| 1717 | int blocksFoundThisSession = 0; | 5318 | int blocksFoundThisSession = 0; |
| 1718 | int64 noJobSince = 0; // 0 = have a job | 5319 | int64 noJobSince = 0; // 0 = have a job |
| 1719 | bool warnedNoJob = false; | 5320 | bool warnedNoJob = false; |
| @@ -1742,6 +5343,31 @@ static void PoolEventLoop() | |||
| 1742 | sock_close(fd); | 5343 | sock_close(fd); |
| 1743 | continue; | 5344 | continue; |
| 1744 | } | 5345 | } |
| 5346 | // #26: select() + FD_SET goes undefined once we exceed what an | ||
| 5347 | // fd_set can carry, and MAX_POOL_MINERS (1000) alone does not | ||
| 5348 | // prevent it -- a Miner is only removed on clean disconnect, so | ||
| 5349 | // half-open zombies accumulate and the OS can hand out fds past | ||
| 5350 | // the limit long before the count reaches 1000. Refuse rather | ||
| 5351 | // than corrupt the fd_set. Which limit applies is platform | ||
| 5352 | // specific: POSIX bounds the descriptor value, Windows the | ||
| 5353 | // number of SOCKET handles an fd_set can hold. | ||
| 5354 | #ifndef _WIN32 | ||
| 5355 | if ((int)fd >= (int)FD_SETSIZE) { | ||
| 5356 | LogPrint("worker", "[pool] refusing miner fd=%d -- at/over" | ||
| 5357 | " FD_SETSIZE=%d, select() would be undefined (#26)\n", | ||
| 5358 | (int)fd, (int)FD_SETSIZE); | ||
| 5359 | sock_close(fd); | ||
| 5360 | continue; | ||
| 5361 | } | ||
| 5362 | #else | ||
| 5363 | if (gMiners.size() >= (size_t)FD_SETSIZE) { | ||
| 5364 | LogPrint("worker", "[pool] refusing miner -- %zu sockets held," | ||
| 5365 | " an fd_set carries at most FD_SETSIZE=%d (#26)\n", | ||
| 5366 | gMiners.size(), (int)FD_SETSIZE); | ||
| 5367 | sock_close(fd); | ||
| 5368 | continue; | ||
| 5369 | } | ||
| 5370 | #endif | ||
| 1745 | gMiners.push_back(new Miner(fd)); | 5371 | gMiners.push_back(new Miner(fd)); |
| 1746 | LogPrint("worker", "[pool] miner fd=%d entered event loop" | 5372 | LogPrint("worker", "[pool] miner fd=%d entered event loop" |
| 1747 | " -- awaiting subscribe/authorize\n", (int)fd); | 5373 | " -- awaiting subscribe/authorize\n", (int)fd); |
| @@ -1869,6 +5495,35 @@ static void PoolEventLoop() | |||
| 1869 | } | 5495 | } |
| 1870 | } | 5496 | } |
| 1871 | 5497 | ||
| 5498 | // 3.6. Idle reap (#6). Mirror the clean-disconnect path exactly -- | ||
| 5499 | // log, close, erase from gMiners via std::find, delete -- just with | ||
| 5500 | // a different reason. Shares the miner already earned stay in | ||
| 5501 | // roundShareCount, exactly as they do when a miner cleanly | ||
| 5502 | // disconnects mid-round. connectTime stands in for lastSeen on a | ||
| 5503 | // connection that never completed a line. Erasing inside the loop | ||
| 5504 | // above would invalidate the iteration, so collect then reap. | ||
| 5505 | { | ||
| 5506 | int64 now = GetTime(); | ||
| 5507 | std::vector<Miner*> idle; | ||
| 5508 | for (Miner* m : gMiners) { | ||
| 5509 | int64 last = (m->lastSeen > 0) ? m->lastSeen : m->connectTime; | ||
| 5510 | if (now - last > POOL_IDLE_TIMEOUT_SECS) | ||
| 5511 | idle.push_back(m); | ||
| 5512 | } | ||
| 5513 | for (Miner* m : idle) { | ||
| 5514 | LogPrint("worker", "[pool] dropping idle miner %s -- nothing" | ||
| 5515 | " received in over %llds (session shares: %llu," | ||
| 5516 | " round shares: %llu)\n", | ||
| 5517 | m->worker.empty() ? "(never authorized)" : m->worker.c_str(), | ||
| 5518 | (long long)POOL_IDLE_TIMEOUT_SECS, | ||
| 5519 | (unsigned long long)m->sessionShares, | ||
| 5520 | (unsigned long long)m->roundShares); | ||
| 5521 | sock_close(m->fd); | ||
| 5522 | gMiners.erase(std::find(gMiners.begin(), gMiners.end(), m)); | ||
| 5523 | delete m; | ||
| 5524 | } | ||
| 5525 | } | ||
| 5526 | |||
| 1872 | // 4. Select over miner sockets | 5527 | // 4. Select over miner sockets |
| 1873 | if (!gMiners.empty()) { | 5528 | if (!gMiners.empty()) { |
| 1874 | fd_set fds; FD_ZERO(&fds); | 5529 | fd_set fds; FD_ZERO(&fds); |
| @@ -1891,6 +5546,11 @@ static void PoolEventLoop() | |||
| 1891 | // terminate the whole node -- not just the pool. Every | 5546 | // terminate the whole node -- not just the pool. Every |
| 1892 | // line arrives from an unauthenticated socket. | 5547 | // line arrives from an unauthenticated socket. |
| 1893 | while (RecvLine(m->fd, m->readBuf, line, disc)) { | 5548 | while (RecvLine(m->fd, m->readBuf, line, disc)) { |
| 5549 | // Any complete line is proof of life (#6): authorize | ||
| 5550 | // and valid shares already stamp lastSeen themselves, | ||
| 5551 | // but a miner that only ever subscribes (or sends | ||
| 5552 | // unrecognised methods) must count as alive too. | ||
| 5553 | m->lastSeen = GetTime(); | ||
| 1894 | try { | 5554 | try { |
| 1895 | HandleLine(m, line, roundShareCount, | 5555 | HandleLine(m, line, roundShareCount, |
| 1896 | roundShareTotal, blocksFoundThisSession); | 5556 | roundShareTotal, blocksFoundThisSession); |
| @@ -1931,6 +5591,14 @@ static void PoolEventLoop() | |||
| 1931 | RefreshStats(blocksFoundThisSession, roundShareTotal); | 5591 | RefreshStats(blocksFoundThisSession, roundShareTotal); |
| 1932 | lastStatsRefresh = GetTime(); | 5592 | lastStatsRefresh = GetTime(); |
| 1933 | } | 5593 | } |
| 5594 | |||
| 5595 | // 6. #19: log the connection census periodically. Cheap (one pass over | ||
| 5596 | // gMiners) and it is the single number an operator reads to judge pool | ||
| 5597 | // health, so it should not be an overstatement. | ||
| 5598 | if (GetTime() - lastCensus >= POOL_CENSUS_SECS) { | ||
| 5599 | LogPoolConnectionCensus(); | ||
| 5600 | lastCensus = GetTime(); | ||
| 5601 | } | ||
| 1934 | } | 5602 | } |
| 1935 | 5603 | ||
| 1936 | for (Miner* m : gMiners) { sock_close(m->fd); delete m; } | 5604 | for (Miner* m : gMiners) { sock_close(m->fd); delete m; } |
| @@ -1959,10 +5627,20 @@ static void AcceptOneFn(void* arg) | |||
| 1959 | std::string host = relay.substr(0, colon); | 5627 | std::string host = relay.substr(0, colon); |
| 1960 | int port = atoi(relay.substr(colon + 1).c_str()); | 5628 | int port = atoi(relay.substr(colon + 1).c_str()); |
| 1961 | 5629 | ||
| 1962 | LogPrint("pool", "[pool] accept thread started for relay %s\n", relay.c_str()); | 5630 | LogPrint("pool", "[pool] accept thread started for relay %s%s\n", relay.c_str(), |
| 5631 | fBtfRelayPinned ? " (/rvrelay pin)" : ""); | ||
| 5632 | |||
| 5633 | // Registration health for this relay. A pin has no fallback, so a pin that | ||
| 5634 | // never registers means the pool is simply unreachable -- and that has to | ||
| 5635 | // be loud. The 2026-07-30 incident went unnoticed because this loop logged | ||
| 5636 | // failures at one flat line and success not at all, so "which relay is this | ||
| 5637 | // pool actually registered at" could not be answered from the log. | ||
| 5638 | int nConsecFail = 0; | ||
| 5639 | int64 nFirstFail = 0; | ||
| 5640 | bool fEverRegistered = false; | ||
| 1963 | 5641 | ||
| 1964 | while (!fShutdown && gPoolRunning) { | 5642 | while (!fShutdown && gPoolRunning) { |
| 1965 | // Blocks until a miner connects to us at this relay | 5643 | // Returns as soon as the relay lists us -- registered, not yet paired. |
| 1966 | btf::RvSocket rv = btf::RvServiceRegister(host.c_str(), (unsigned short)port, pk); | 5644 | btf::RvSocket rv = btf::RvServiceRegister(host.c_str(), (unsigned short)port, pk); |
| 1967 | 5645 | ||
| 1968 | if (fShutdown || !gPoolRunning) { | 5646 | if (fShutdown || !gPoolRunning) { |
| @@ -1970,12 +5648,53 @@ static void AcceptOneFn(void* arg) | |||
| 1970 | break; | 5648 | break; |
| 1971 | } | 5649 | } |
| 1972 | if (rv == btf::RV_INVALID) { | 5650 | if (rv == btf::RV_INVALID) { |
| 1973 | LogPrint("pool", "[pool] relay %s: RvServiceRegister failed" | 5651 | nConsecFail++; |
| 1974 | " -- backing off 30s before retry\n", relay.c_str()); | 5652 | if (nFirstFail == 0) nFirstFail = GetTime(); |
| 5653 | LogPrint("pool", "[pool] relay %s: registration failed (attempt %d," | ||
| 5654 | " failing for %llds) -- backing off 30s before retry\n", | ||
| 5655 | relay.c_str(), nConsecFail, (long long)(GetTime() - nFirstFail)); | ||
| 5656 | if (fBtfRelayPinned && (nConsecFail == 3 || nConsecFail % 10 == 0)) | ||
| 5657 | LogPrint("pool", "[pool] WARNING: pinned relay %s refused registration" | ||
| 5658 | " %d times running (%llds); /rvrelay leaves no fallback, so" | ||
| 5659 | " miners cannot reach this pool%s\n", | ||
| 5660 | relay.c_str(), nConsecFail, (long long)(GetTime() - nFirstFail), | ||
| 5661 | fEverRegistered ? "" : " -- it has never registered once"); | ||
| 1975 | for (int i = 0; i < 30 && !fShutdown && gPoolRunning; i++) Sleep(1000); | 5662 | for (int i = 0; i < 30 && !fShutdown && gPoolRunning; i++) Sleep(1000); |
| 1976 | continue; | 5663 | continue; |
| 1977 | } | 5664 | } |
| 1978 | 5665 | ||
| 5666 | // Advertise THIS relay only now. Setting it before the call published a | ||
| 5667 | // meeting node we were not yet listed at, so a miner resolving the | ||
| 5668 | // descriptor inside that window burned its dial on a relay holding no | ||
| 5669 | // registration for us. net.cpp's peer-accept has always ordered it this | ||
| 5670 | // way round; the pool had not. | ||
| 5671 | BtfSetActiveRelay(relay); | ||
| 5672 | if (!fEverRegistered || nConsecFail > 0) | ||
| 5673 | LogPrint("pool", "[pool] registered at relay %s%s%s -- advertising it as" | ||
| 5674 | " the pool's meeting node\n", relay.c_str(), | ||
| 5675 | fBtfRelayPinned ? " (/rvrelay pin)" : "", | ||
| 5676 | nConsecFail > 0 ? ", recovered" : ""); | ||
| 5677 | fEverRegistered = true; | ||
| 5678 | nConsecFail = 0; | ||
| 5679 | nFirstFail = 0; | ||
| 5680 | |||
| 5681 | // Block for a miner to actually arrive. RvServiceRegister used to do | ||
| 5682 | // this itself and returned with the relay's 0x01 pairing byte already | ||
| 5683 | // consumed; it no longer does. Without waiting here, BtfServiceWrap | ||
| 5684 | // reads that 0x01 as the first byte of the miner's 32-byte ephemeral | ||
| 5685 | // key -- every subsequent byte is shifted by one, so the X25519 | ||
| 5686 | // handshake completes against a garbage shared secret (it has no | ||
| 5687 | // confirmation step to catch it), the tunnel is handed to the event | ||
| 5688 | // loop, and the miner's mining.subscribe never decrypts into a valid | ||
| 5689 | // line. The miner sees the pool close on it; the pool sees a peer that | ||
| 5690 | // never authorized. | ||
| 5691 | if (!btf::RvServiceWaitPaired(rv)) { | ||
| 5692 | btf::RvClose(rv); | ||
| 5693 | LogPrint("pool", "[pool] relay %s: dropped us before any miner dialed" | ||
| 5694 | " -- re-registering\n", relay.c_str()); | ||
| 5695 | continue; | ||
| 5696 | } | ||
| 5697 | |||
| 1979 | // Echan handshake -- blocking but fast (local + relay round-trip) | 5698 | // Echan handshake -- blocking but fast (local + relay round-trip) |
| 1980 | btf_socket_t fd = btf::BtfServiceWrap(rv, sk); | 5699 | btf_socket_t fd = btf::BtfServiceWrap(rv, sk); |
| 1981 | if (fd == INVALID_SOCKET) { | 5700 | if (fd == INVALID_SOCKET) { |
| @@ -2013,9 +5732,21 @@ static void AcceptLoopFn(void*) | |||
| 2013 | LogPrint("pool", "[pool] .btf identity ready after %ds\n", waitSecs * 2); | 5732 | LogPrint("pool", "[pool] .btf identity ready after %ds\n", waitSecs * 2); |
| 2014 | 5733 | ||
| 2015 | std::set<std::string> spawned; // relays we've already started a thread for | 5734 | std::set<std::string> spawned; // relays we've already started a thread for |
| 5735 | std::set<std::string> skipped; // discovered relays the pin excluded, logged once each | ||
| 2016 | 5736 | ||
| 2017 | while (!fShutdown && gPoolRunning) { | 5737 | while (!fShutdown && gPoolRunning) { |
| 5738 | // BtfAllRelays() already honours the pin: with /rvrelay set it returns | ||
| 5739 | // vBtfMeetingRelays alone, so no thread is ever spawned for a | ||
| 5740 | // discovered relay. Name the ones it drops -- a pool registering at a | ||
| 5741 | // stranger's relay is exactly the thing that should not be silent. | ||
| 2018 | vector<string> relays = BtfAllRelays(); | 5742 | vector<string> relays = BtfAllRelays(); |
| 5743 | if (fBtfRelayPinned) { | ||
| 5744 | for (const string& r : BtfDiscoveredRelays()) | ||
| 5745 | if (std::find(relays.begin(), relays.end(), r) == relays.end() && | ||
| 5746 | skipped.insert(r).second) | ||
| 5747 | LogPrint("pool", "[pool] not registering at discovered relay %s" | ||
| 5748 | " -- /rvrelay pins the relay list\n", r.c_str()); | ||
| 5749 | } | ||
| 2019 | if (relays.empty()) { | 5750 | if (relays.empty()) { |
| 2020 | LogPrint("pool", "[pool] no relays available -- miners cannot connect yet\n"); | 5751 | LogPrint("pool", "[pool] no relays available -- miners cannot connect yet\n"); |
| 2021 | } else { | 5752 | } else { |
| @@ -2031,8 +5762,9 @@ static void AcceptLoopFn(void*) | |||
| 2031 | relay.c_str()); | 5762 | relay.c_str()); |
| 2032 | } else { | 5763 | } else { |
| 2033 | spawned.insert(relay); | 5764 | spawned.insert(relay); |
| 2034 | LogPrint("pool", "[pool] accept thread spawned for relay %s" | 5765 | LogPrint("pool", "[pool] accept thread spawned for relay %s%s" |
| 2035 | " (%zu total)\n", relay.c_str(), spawned.size()); | 5766 | " (%zu total)\n", relay.c_str(), |
| 5767 | fBtfRelayPinned ? " (/rvrelay pin)" : "", spawned.size()); | ||
| 2036 | } | 5768 | } |
| 2037 | } | 5769 | } |
| 2038 | } | 5770 | } |
| @@ -2062,10 +5794,23 @@ void ThreadRPCServer(void*) | |||
| 2062 | 5794 | ||
| 2063 | LoadPendingPayouts(); | 5795 | LoadPendingPayouts(); |
| 2064 | LoadPoolRounds(); | 5796 | LoadPoolRounds(); |
| 2065 | 5797 | LoadPaying(); | |
| 2066 | _beginthread(AcceptLoopFn, 0, NULL); | 5798 | // Must run before the payout thread starts, so nothing is resent that the |
| 2067 | _beginthread(ThreadBtfPoolAnnouncer, 0, NULL); | 5799 | // ledger already shows as sent, and every restored in-flight record is |
| 2068 | _beginthread(PayoutThreadFn, 0, NULL); | 5800 | // armed for its one wallet probe before it is allowed to send. |
| 5801 | ReconcilePayouts(); | ||
| 5802 | |||
| 5803 | // _beginthread cannot throw (it reports -1), but an unchecked failure | ||
| 5804 | // leaves the pool running deaf: nobody accepts, announces, or pays, and | ||
| 5805 | // nothing in the log says why. | ||
| 5806 | if (_beginthread(AcceptLoopFn, 0, NULL) == (uintptr_t)-1) | ||
| 5807 | LogPrint("pool", "[pool] FATAL: accept loop thread failed to spawn -- miners cannot connect\n"); | ||
| 5808 | if (_beginthread(LocalAcceptFn, 0, NULL) == (uintptr_t)-1) | ||
| 5809 | LogPrint("pool", "[pool] local accept thread failed to spawn\n"); | ||
| 5810 | if (_beginthread(ThreadBtfPoolAnnouncer, 0, NULL) == (uintptr_t)-1) | ||
| 5811 | LogPrint("pool", "[pool] announcer thread failed to spawn -- pool will not be advertised\n"); | ||
| 5812 | if (_beginthread(PayoutThreadFn, 0, NULL) == (uintptr_t)-1) | ||
| 5813 | LogPrint("pool", "[pool] FATAL: payout thread failed to spawn -- no one gets paid\n"); | ||
| 2069 | 5814 | ||
| 2070 | PoolEventLoop(); // blocks until gPoolRunning = false or fShutdown | 5815 | PoolEventLoop(); // blocks until gPoolRunning = false or fShutdown |
| 2071 | 5816 | ||
diff --git a/src/selftest.cpp b/src/selftest.cpp index 1a72e3a..357d532 100644 --- a/src/selftest.cpp +++ b/src/selftest.cpp | |||
| @@ -19,6 +19,7 @@ | |||
| 19 | #include "wallet_sqlite.h" | 19 | #include "wallet_sqlite.h" |
| 20 | 20 | ||
| 21 | extern int RunPoolStratumSelfTest(); | 21 | extern int RunPoolStratumSelfTest(); |
| 22 | extern int RunPayoutLedgerSelfTest(); | ||
| 22 | 23 | ||
| 23 | // Test results go to the terminal, not to debug.log. | 24 | // Test results go to the terminal, not to debug.log. |
| 24 | // | 25 | // |
| @@ -2579,7 +2580,11 @@ static int RunWalletBackendDefaultSelfTest() | |||
| 2579 | 2580 | ||
| 2580 | try | 2581 | try |
| 2581 | { | 2582 | { |
| 2582 | // 1. Fresh datadir, no flag -> new wallet.sqlite, marker "sqlite", no wallet.dat. | 2583 | // 1. Fresh datadir, no flag -> this fork refuses, where upstream creates. |
| 2584 | // An empty datadir on a headless node is far more often a mount or | ||
| 2585 | // backup fault than a first run, and creating a wallet also pins the | ||
| 2586 | // backend marker, after which restoring the real wallet is a no-op. | ||
| 2587 | // Nothing may be left behind: no wallet of either kind, no marker. | ||
| 2583 | string d1 = tmp + "/fresh"; | 2588 | string d1 = tmp + "/fresh"; |
| 2584 | nFail += Check(MakeDirLocal(d1), "fresh scenario dir can be created") ? 0 : 1; | 2589 | nFail += Check(MakeDirLocal(d1), "fresh scenario dir can be created") ? 0 : 1; |
| 2585 | vector<string> a1; | 2590 | vector<string> a1; |
| @@ -2587,19 +2592,31 @@ static int RunWalletBackendDefaultSelfTest() | |||
| 2587 | a1.push_back("-nomanagedtor"); | 2592 | a1.push_back("-nomanagedtor"); |
| 2588 | a1.push_back("-nogui"); | 2593 | a1.push_back("-nogui"); |
| 2589 | a1.push_back("-newaddress"); | 2594 | a1.push_back("-newaddress"); |
| 2590 | int r1 = RunBitflashChild(strExe, a1); | 2595 | int r0 = RunBitflashChild(strExe, a1); |
| 2596 | nFail += Check(r0 != 0 && | ||
| 2597 | !FileExists((d1 + "/wallet.sqlite").c_str()) && | ||
| 2598 | !FileExists((d1 + "/wallet.dat").c_str()) && | ||
| 2599 | !FileExists((d1 + "/wallet-backend").c_str()), | ||
| 2600 | "a flagless headless start refuses to create a wallet") ? 0 : 1; | ||
| 2601 | |||
| 2602 | // 1a. -createwallet says it was meant: new wallet.sqlite, marker | ||
| 2603 | // "sqlite", no wallet.dat. | ||
| 2604 | vector<string> a1c = a1; | ||
| 2605 | a1c.push_back("-createwallet"); | ||
| 2606 | int r1 = RunBitflashChild(strExe, a1c); | ||
| 2591 | nFail += Check(r1 == 0 && | 2607 | nFail += Check(r1 == 0 && |
| 2592 | FileExists((d1 + "/wallet.sqlite").c_str()) && | 2608 | FileExists((d1 + "/wallet.sqlite").c_str()) && |
| 2593 | !FileExists((d1 + "/wallet.dat").c_str()) && | 2609 | !FileExists((d1 + "/wallet.dat").c_str()) && |
| 2594 | FileContainsText(d1 + "/wallet-backend", "sqlite"), | 2610 | FileContainsText(d1 + "/wallet-backend", "sqlite"), |
| 2595 | "a fresh install starts on SQLite (new default) and records the marker") ? 0 : 1; | 2611 | "-createwallet starts a fresh install on SQLite and records the marker") ? 0 : 1; |
| 2596 | 2612 | ||
| 2597 | // 1b. Second run reuses it, still SQLite, still no wallet.dat. | 2613 | // 1b. Second run reuses it without the flag -- the refusal only guards |
| 2614 | // creation, so an existing wallet still starts flagless. | ||
| 2598 | int r1b = RunBitflashChild(strExe, a1); | 2615 | int r1b = RunBitflashChild(strExe, a1); |
| 2599 | nFail += Check(r1b == 0 && | 2616 | nFail += Check(r1b == 0 && |
| 2600 | FileExists((d1 + "/wallet.sqlite").c_str()) && | 2617 | FileExists((d1 + "/wallet.sqlite").c_str()) && |
| 2601 | !FileExists((d1 + "/wallet.dat").c_str()), | 2618 | !FileExists((d1 + "/wallet.dat").c_str()), |
| 2602 | "the recorded SQLite choice survives a restart") ? 0 : 1; | 2619 | "the recorded SQLite choice survives a flagless restart") ? 0 : 1; |
| 2603 | 2620 | ||
| 2604 | // 2. Existing wallet.dat, no flag -> stays on Berkeley DB. | 2621 | // 2. Existing wallet.dat, no flag -> stays on Berkeley DB. |
| 2605 | string d2 = tmp + "/existing-bdb"; | 2622 | string d2 = tmp + "/existing-bdb"; |
| @@ -3023,6 +3040,13 @@ static int RunNetMessageSelfTest() | |||
| 3023 | int nFail = 0; | 3040 | int nFail = 0; |
| 3024 | try | 3041 | try |
| 3025 | { | 3042 | { |
| 3043 | // A peer that has not yet been kept waiting must not look like one that | ||
| 3044 | // has been waiting since the epoch. This is the whole bug: the stall | ||
| 3045 | // clock lives in CNode, so only the constructor can start it at zero. | ||
| 3046 | CNode opened(INVALID_SOCKET, CAddress("127.0.0.1")); | ||
| 3047 | nFail += Check(opened.nMsgWaitSince == 0 && opened.nMsgWaitSize == 0, | ||
| 3048 | "a new peer starts with its stall clock stopped") ? 0 : 1; | ||
| 3049 | |||
| 3026 | CNode complete(INVALID_SOCKET, CAddress("127.0.0.1")); | 3050 | CNode complete(INVALID_SOCKET, CAddress("127.0.0.1")); |
| 3027 | complete.nVersion = VERSION; | 3051 | complete.nVersion = VERSION; |
| 3028 | complete.vRecv << CMessageHeader("ping", 0); | 3052 | complete.vRecv << CMessageHeader("ping", 0); |
| @@ -3037,20 +3061,38 @@ static int RunNetMessageSelfTest() | |||
| 3037 | nFail += Check(ProcessMessages(&partial), "an incomplete message returns cleanly") ? 0 : 1; | 3061 | nFail += Check(ProcessMessages(&partial), "an incomplete message returns cleanly") ? 0 : 1; |
| 3038 | nFail += Check(partial.vRecv.size() == nPartialBefore, | 3062 | nFail += Check(partial.vRecv.size() == nPartialBefore, |
| 3039 | "an incomplete message keeps one header in the buffer") ? 0 : 1; | 3063 | "an incomplete message keeps one header in the buffer") ? 0 : 1; |
| 3040 | nFail += Check(partial.nIncompleteMessageStart != 0, | 3064 | nFail += Check(partial.nMsgWaitSince != 0, |
| 3041 | "an incomplete message starts a timeout clock") ? 0 : 1; | 3065 | "an incomplete message starts the stall clock") ? 0 : 1; |
| 3066 | nFail += Check(partial.nMsgWaitSize == 0, | ||
| 3067 | "the stall clock records how much payload had arrived") ? 0 : 1; | ||
| 3042 | nFail += Check(!partial.fDisconnect, | 3068 | nFail += Check(!partial.fDisconnect, |
| 3043 | "a fresh incomplete message does not disconnect immediately") ? 0 : 1; | 3069 | "a fresh incomplete message does not disconnect immediately") ? 0 : 1; |
| 3044 | 3070 | ||
| 3071 | // Still arriving, just slowly: more payload is here than when the clock | ||
| 3072 | // was started, so the clock restarts however old it was. Timing the | ||
| 3073 | // wait instead of the stall would drop this peer. | ||
| 3074 | CNode slow(INVALID_SOCKET, CAddress("127.0.0.1")); | ||
| 3075 | slow.nVersion = VERSION; | ||
| 3076 | slow.vRecv << CMessageHeader("block", 100); | ||
| 3077 | slow.vRecv.insert(slow.vRecv.end(), 50, '\0'); | ||
| 3078 | slow.nMsgWaitSince = GetTime() - BTF_MSG_WAIT_MAX_SECS - 1; | ||
| 3079 | slow.nMsgWaitSize = 10; | ||
| 3080 | ProcessMessages(&slow); | ||
| 3081 | nFail += Check(!slow.fDisconnect, | ||
| 3082 | "a peer still sending payload is not dropped") ? 0 : 1; | ||
| 3083 | nFail += Check(slow.nMsgWaitSize == 50, | ||
| 3084 | "progress restarts the stall clock") ? 0 : 1; | ||
| 3085 | |||
| 3086 | // Nothing more has arrived since the clock started, and the clock is | ||
| 3087 | // older than the limit. | ||
| 3045 | CNode stale(INVALID_SOCKET, CAddress("127.0.0.1")); | 3088 | CNode stale(INVALID_SOCKET, CAddress("127.0.0.1")); |
| 3046 | stale.nVersion = VERSION; | 3089 | stale.nVersion = VERSION; |
| 3047 | stale.vRecv << CMessageHeader("block", 100); | 3090 | stale.vRecv << CMessageHeader("block", 100); |
| 3048 | stale.nIncompleteMessageStart = GetTime() - BTF_INCOMPLETE_MESSAGE_TIMEOUT_SECS - 1; | 3091 | stale.nMsgWaitSince = GetTime() - BTF_MSG_WAIT_MAX_SECS - 1; |
| 3049 | stale.nIncompleteMessageSize = 100; | 3092 | stale.nMsgWaitSize = 0; |
| 3050 | stale.strIncompleteMessageCommand = "block"; | ||
| 3051 | ProcessMessages(&stale); | 3093 | ProcessMessages(&stale); |
| 3052 | nFail += Check(stale.fDisconnect, | 3094 | nFail += Check(stale.fDisconnect, |
| 3053 | "a stale incomplete message disconnects the peer") ? 0 : 1; | 3095 | "a stalled incomplete message disconnects the peer") ? 0 : 1; |
| 3054 | 3096 | ||
| 3055 | CNode oversized(INVALID_SOCKET, CAddress("127.0.0.1")); | 3097 | CNode oversized(INVALID_SOCKET, CAddress("127.0.0.1")); |
| 3056 | oversized.nVersion = VERSION; | 3098 | oversized.nVersion = VERSION; |
| @@ -3174,6 +3216,74 @@ static CBlock MakeSizedConsensusBlock(size_t nScriptBytes) | |||
| 3174 | return block; | 3216 | return block; |
| 3175 | } | 3217 | } |
| 3176 | 3218 | ||
| 3219 | // A distinct transaction of a chosen weight and verification cost. nUnique | ||
| 3220 | // only keeps the txids apart, so a mempool built from these is not a pile of | ||
| 3221 | // duplicates a block could never carry anyway. | ||
| 3222 | static CTransaction MakeSizedTx(size_t nPadBytes, unsigned int nSigOps, int nUnique) | ||
| 3223 | { | ||
| 3224 | CTransaction tx; | ||
| 3225 | tx.vin.push_back(CTxIn(COutPoint(), CScript() << (int64)nUnique << 1)); | ||
| 3226 | tx.vout.push_back(CTxOut(0, CScript())); | ||
| 3227 | for (unsigned int i = 0; i < nSigOps; i++) | ||
| 3228 | tx.vout[0].scriptPubKey << OP_CHECKSIG; | ||
| 3229 | tx.vout[0].scriptPubKey.insert(tx.vout[0].scriptPubKey.end(), nPadBytes, 0); | ||
| 3230 | return tx; | ||
| 3231 | } | ||
| 3232 | |||
| 3233 | static CBlock MakeCoinbaseOnlyTemplate() | ||
| 3234 | { | ||
| 3235 | CTransaction txNew; | ||
| 3236 | txNew.vin.resize(1); | ||
| 3237 | txNew.vin[0].prevout.SetNull(); | ||
| 3238 | txNew.vin[0].scriptSig << 1 << 1 << 1; | ||
| 3239 | txNew.vout.resize(1); | ||
| 3240 | txNew.vout[0].scriptPubKey << OP_CHECKSIG; | ||
| 3241 | |||
| 3242 | CBlock block; | ||
| 3243 | block.vtx.push_back(txNew); | ||
| 3244 | block.nVersion = 1; | ||
| 3245 | block.nTime = GetAdjustedTime(); | ||
| 3246 | block.nBits = bnProofOfWorkLimit.GetCompact(); | ||
| 3247 | block.nNonce = 0; | ||
| 3248 | return block; | ||
| 3249 | } | ||
| 3250 | |||
| 3251 | // The sum CheckBlock() takes, spelled the same way. | ||
| 3252 | static unsigned int BlockSigOpCount(const CBlock& block) | ||
| 3253 | { | ||
| 3254 | unsigned int n = 0; | ||
| 3255 | for (int i = 0; i < (int)block.vtx.size(); i++) | ||
| 3256 | n += block.vtx[i].GetSigOpCount(); | ||
| 3257 | return n; | ||
| 3258 | } | ||
| 3259 | |||
| 3260 | // Drive the selection rule the way both assemblers drive it: ask | ||
| 3261 | // BlockTemplateFits before appending, skip what does not fit, never stop early | ||
| 3262 | // on one that does not. | ||
| 3263 | // | ||
| 3264 | // This mirrors the loops in BitcoinMiner() and RebuildJob() rather than calling | ||
| 3265 | // them -- one needs a CTxDB, a wallet key and cs_main, the other is static in | ||
| 3266 | // rpc.cpp, so neither is reachable from here. What that leaves proven is the | ||
| 3267 | // rule the two of them share and the property it is meant to guarantee, which | ||
| 3268 | // is where the defect actually lived: the budget was right and the enforcement | ||
| 3269 | // was not. | ||
| 3270 | static void SelectIntoTemplate(CBlock& block, const vector<CTransaction>& vMempool) | ||
| 3271 | { | ||
| 3272 | unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK); | ||
| 3273 | unsigned int nBlockSigOps = block.vtx[0].GetSigOpCount(); | ||
| 3274 | for (int i = 0; i < (int)vMempool.size(); i++) | ||
| 3275 | { | ||
| 3276 | const CTransaction& tx = vMempool[i]; | ||
| 3277 | unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK); | ||
| 3278 | unsigned int nTxSigOps = tx.GetSigOpCount(); | ||
| 3279 | if (!BlockTemplateFits(nBlockSize, nBlockSigOps, nTxSize, nTxSigOps)) | ||
| 3280 | continue; | ||
| 3281 | block.vtx.push_back(tx); | ||
| 3282 | nBlockSize += nTxSize; | ||
| 3283 | nBlockSigOps += nTxSigOps; | ||
| 3284 | } | ||
| 3285 | } | ||
| 3286 | |||
| 3177 | static int RunConsensusLimitsSelfTest() | 3287 | static int RunConsensusLimitsSelfTest() |
| 3178 | { | 3288 | { |
| 3179 | fflush(stdout); | 3289 | fflush(stdout); |
| @@ -3197,6 +3307,95 @@ static int RunConsensusLimitsSelfTest() | |||
| 3197 | "the test block sits between 1 MB and the old 32 MB cap") ? 0 : 1; | 3307 | "the test block sits between 1 MB and the old 32 MB cap") ? 0 : 1; |
| 3198 | nFail += Check(!oversized.CheckSizeLimits(), | 3308 | nFail += Check(!oversized.CheckSizeLimits(), |
| 3199 | "a block above 1 MB is outside the consensus size limit") ? 0 : 1; | 3309 | "a block above 1 MB is outside the consensus size limit") ? 0 : 1; |
| 3310 | |||
| 3311 | // The other half of the cap. Everything above asks what validation | ||
| 3312 | // refuses; nothing asked what assembly produces, which is why a miner | ||
| 3313 | // that could overshoot the cap by an entire mempool went unnoticed. | ||
| 3314 | nFail += Check(MAX_BLOCK_SIZE_GEN < MAX_BLOCK_SIZE, | ||
| 3315 | "the assembly budget stays under the consensus cap") ? 0 : 1; | ||
| 3316 | |||
| 3317 | // The rule itself, at its edges. The sigop comparison has to match | ||
| 3318 | // CheckBlock(), which fails on `> MAX_BLOCK_SIGOPS` and so accepts a | ||
| 3319 | // block sitting exactly on the limit. | ||
| 3320 | nFail += Check(BlockTemplateFits(MAX_BLOCK_SIZE_GEN - 100, 0, 100, 0), | ||
| 3321 | "a transaction that exactly fills the size budget fits") ? 0 : 1; | ||
| 3322 | nFail += Check(!BlockTemplateFits(MAX_BLOCK_SIZE_GEN - 100, 0, 101, 0), | ||
| 3323 | "one byte past the size budget does not fit") ? 0 : 1; | ||
| 3324 | nFail += Check(BlockTemplateFits(0, 0, 0, MAX_BLOCK_SIGOPS), | ||
| 3325 | "sigops exactly on the limit fit, as CheckBlock() allows") ? 0 : 1; | ||
| 3326 | nFail += Check(!BlockTemplateFits(0, 0, 0, MAX_BLOCK_SIGOPS + 1), | ||
| 3327 | "one sigop past the limit does not fit") ? 0 : 1; | ||
| 3328 | |||
| 3329 | // A mempool several times the consensus cap, in ordinary-sized | ||
| 3330 | // transactions. The old miner tested its budget only on the outer | ||
| 3331 | // loop, so a single pass appended all of this. | ||
| 3332 | { | ||
| 3333 | CBlock block = MakeCoinbaseOnlyTemplate(); | ||
| 3334 | vector<CTransaction> vMempool; | ||
| 3335 | for (int i = 0; i < 300; i++) | ||
| 3336 | vMempool.push_back(MakeSizedTx(10000, 1, i)); | ||
| 3337 | SelectIntoTemplate(block, vMempool); | ||
| 3338 | block.hashMerkleRoot = block.BuildMerkleTree(); | ||
| 3339 | |||
| 3340 | nFail += Check(block.vtx.size() > 1, | ||
| 3341 | "a full mempool still fills a template") ? 0 : 1; | ||
| 3342 | nFail += Check(::GetSerializeSize(block, SER_DISK) <= MAX_BLOCK_SIZE_GEN, | ||
| 3343 | "a template built from a 3 MB mempool respects the budget") ? 0 : 1; | ||
| 3344 | nFail += Check(block.CheckSizeLimits(), | ||
| 3345 | "that template is one CheckBlock() would accept on size") ? 0 : 1; | ||
| 3346 | } | ||
| 3347 | |||
| 3348 | // One transaction too big for the budget. Nothing on the path into the | ||
| 3349 | // mempool caps transaction size, so this is a transaction a peer can | ||
| 3350 | // simply send. It must be skipped and the rest still taken -- skipped, | ||
| 3351 | // not stopped on. | ||
| 3352 | { | ||
| 3353 | CBlock block = MakeCoinbaseOnlyTemplate(); | ||
| 3354 | vector<CTransaction> vMempool; | ||
| 3355 | vMempool.push_back(MakeSizedTx(1500000, 1, 9999)); | ||
| 3356 | for (int i = 0; i < 10; i++) | ||
| 3357 | vMempool.push_back(MakeSizedTx(1000, 1, i)); | ||
| 3358 | |||
| 3359 | unsigned int nHugeSize = ::GetSerializeSize(vMempool[0], SER_NETWORK); | ||
| 3360 | nFail += Check(nHugeSize > MAX_BLOCK_SIZE && nHugeSize <= MAX_SIZE, | ||
| 3361 | "the outsized transaction is one the mempool would hold") ? 0 : 1; | ||
| 3362 | |||
| 3363 | SelectIntoTemplate(block, vMempool); | ||
| 3364 | block.hashMerkleRoot = block.BuildMerkleTree(); | ||
| 3365 | |||
| 3366 | bool fTookHuge = false; | ||
| 3367 | for (int i = 0; i < (int)block.vtx.size(); i++) | ||
| 3368 | if (block.vtx[i].GetHash() == vMempool[0].GetHash()) | ||
| 3369 | fTookHuge = true; | ||
| 3370 | |||
| 3371 | nFail += Check(!fTookHuge, | ||
| 3372 | "a transaction larger than the budget is left out") ? 0 : 1; | ||
| 3373 | nFail += Check(block.vtx.size() == 11, | ||
| 3374 | "the ten that do fit are taken anyway") ? 0 : 1; | ||
| 3375 | nFail += Check(block.CheckSizeLimits(), | ||
| 3376 | "one outsized transaction cannot invalidate a template") ? 0 : 1; | ||
| 3377 | } | ||
| 3378 | |||
| 3379 | // Transactions that are cheap in bytes and expensive to verify. Size | ||
| 3380 | // alone does not bound what a block costs, and CheckBlock() refuses on | ||
| 3381 | // the count regardless of how small the block is. | ||
| 3382 | { | ||
| 3383 | CBlock block = MakeCoinbaseOnlyTemplate(); | ||
| 3384 | vector<CTransaction> vMempool; | ||
| 3385 | for (int i = 0; i < 100; i++) | ||
| 3386 | vMempool.push_back(MakeSizedTx(0, 1000, i)); | ||
| 3387 | SelectIntoTemplate(block, vMempool); | ||
| 3388 | block.hashMerkleRoot = block.BuildMerkleTree(); | ||
| 3389 | |||
| 3390 | nFail += Check(block.vtx.size() > 1, | ||
| 3391 | "sigop-heavy transactions are still selected") ? 0 : 1; | ||
| 3392 | nFail += Check(BlockSigOpCount(block) <= MAX_BLOCK_SIGOPS, | ||
| 3393 | "selection stops at the sigop cap CheckBlock() enforces") ? 0 : 1; | ||
| 3394 | nFail += Check(::GetSerializeSize(block, SER_DISK) < MAX_BLOCK_SIZE_GEN, | ||
| 3395 | "and stops on sigops while still well inside the size budget") ? 0 : 1; | ||
| 3396 | nFail += Check(block.CheckSizeLimits(), | ||
| 3397 | "the sigop-bounded template is within the size limit too") ? 0 : 1; | ||
| 3398 | } | ||
| 3200 | } | 3399 | } |
| 3201 | catch (const std::exception& e) | 3400 | catch (const std::exception& e) |
| 3202 | { | 3401 | { |
| @@ -3604,6 +3803,8 @@ int RunSelfTest(const std::string& name) | |||
| 3604 | return RunConsensusLimitsSelfTest(); | 3803 | return RunConsensusLimitsSelfTest(); |
| 3605 | if (name == "pool-stratum") | 3804 | if (name == "pool-stratum") |
| 3606 | return RunPoolStratumSelfTest(); | 3805 | return RunPoolStratumSelfTest(); |
| 3806 | if (name == "payout-ledger") | ||
| 3807 | return RunPayoutLedgerSelfTest(); | ||
| 3607 | if (name == "parse-money") | 3808 | if (name == "parse-money") |
| 3608 | return RunParseMoneySelfTest(); | 3809 | return RunParseMoneySelfTest(); |
| 3609 | if (name == "socks5-proxy") | 3810 | if (name == "socks5-proxy") |
| @@ -3612,6 +3813,6 @@ int RunSelfTest(const std::string& name) | |||
| 3612 | return RunManagedTorSelfTest(); | 3813 | return RunManagedTorSelfTest(); |
| 3613 | 3814 | ||
| 3614 | printf("Unknown self-test '%s'\n", name.c_str()); | 3815 | printf("Unknown self-test '%s'\n", name.c_str()); |
| 3615 | printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-storage-sanity, db-env-reopen, wallet-sqlite, wallet-sqlite-migration, wallet-crypto, wallet-encrypt, wallet-sqlite-encrypt, wallet-backend-default, wallet-convert, wallet-portability, net-message, network-params, consensus-limits, pool-stratum, parse-money, socks5-proxy, managed-tor\n"); | 3816 | printf("Known self-tests: wallet-keypool, wallet-hd, wallet-format, wallet-storage-sanity, db-env-reopen, wallet-sqlite, wallet-sqlite-migration, wallet-crypto, wallet-encrypt, wallet-sqlite-encrypt, wallet-backend-default, wallet-convert, wallet-portability, net-message, network-params, consensus-limits, pool-stratum, payout-ledger, parse-money, socks5-proxy, managed-tor\n"); |
| 3616 | return 1; | 3817 | return 1; |
| 3617 | } | 3818 | } |
diff --git a/src/test_btfaddr.cpp b/src/test_btfaddr.cpp index 8de426e..9d33d63 100644 --- a/src/test_btfaddr.cpp +++ b/src/test_btfaddr.cpp | |||
| @@ -133,12 +133,23 @@ int main() | |||
| 133 | std::string d3 = btf::SignDescriptor(ctx, sk, ENC_HEX, "node-1", 5, ONION); | 133 | std::string d3 = btf::SignDescriptor(ctx, sk, ENC_HEX, "node-1", 5, ONION); |
| 134 | json j3 = json::parse(d3); | 134 | json j3 = json::parse(d3); |
| 135 | j3["onion"] = "bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde.onion:8433"; | 135 | j3["onion"] = "bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde.onion:8433"; |
| 136 | CHECK(btf::VerifyDescriptor(ctx, j3.dump(), pk, o) && o.onion.empty(), | 136 | CHECK(!btf::VerifyDescriptor(ctx, j3.dump(), pk, o), |
| 137 | "tampered onion ignored rather than trusted"); | 137 | "tampered onion rejects the descriptor"); |
| 138 | json j4 = json::parse(d3); | 138 | json j4 = json::parse(d3); |
| 139 | j4["sig3"] = std::string(128, '0'); | 139 | j4["sig3"] = std::string(128, '0'); |
| 140 | CHECK(btf::VerifyDescriptor(ctx, j4.dump(), pk, o) && o.onion.empty(), | 140 | CHECK(!btf::VerifyDescriptor(ctx, j4.dump(), pk, o), |
| 141 | "bad onion signature ignored rather than trusted"); | 141 | "bad onion signature rejects the descriptor"); |
| 142 | json j5 = json::parse(d3); | ||
| 143 | j5.erase("sig3"); | ||
| 144 | CHECK(!btf::VerifyDescriptor(ctx, j5.dump(), pk, o), | ||
| 145 | "unsigned onion rejects the descriptor"); | ||
| 146 | // Removing both fields still verifies: the result is indistinguishable | ||
| 147 | // from a peer that never had an onion endpoint. That residual strip | ||
| 148 | // needs the onion bound into the primary sig, an upstream change. | ||
| 149 | json j6 = json::parse(d3); | ||
| 150 | j6.erase("onion"); j6.erase("sig3"); | ||
| 151 | CHECK(btf::VerifyDescriptor(ctx, j6.dump(), pk, o) && o.onion.empty(), | ||
| 152 | "full strip indistinguishable from no-onion (documented residual)"); | ||
| 142 | } | 153 | } |
| 143 | 154 | ||
| 144 | printf("full_resolve_chain_from_address\n"); | 155 | printf("full_resolve_chain_from_address\n"); |
diff --git a/src/test_compactblock.cpp b/src/test_compactblock.cpp new file mode 100644 index 0000000..f4b5583 --- /dev/null +++ b/src/test_compactblock.cpp | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | // Standalone test of compactblock.cpp's core logic: build a compact block, | ||
| 2 | // reconstruct it from a simulated mempool, including the missing-tx path. | ||
| 3 | // Not wired into the P2P layer -- proves BuildCompactBlock/ | ||
| 4 | // TryReconstructBlock/FillMissingAndFinish are correct in isolation. | ||
| 5 | |||
| 6 | #include "headers.h" | ||
| 7 | #include "compactblock.h" | ||
| 8 | #include <cstdio> | ||
| 9 | #include <cassert> | ||
| 10 | |||
| 11 | // main.cpp defines this; we're not linking main.cpp, so define it here. | ||
| 12 | map<uint256, CTransaction> mapTransactions; | ||
| 13 | string GetAppDir() { return "."; } // util.h's debug-log path helper needs this | ||
| 14 | |||
| 15 | static int g_fail = 0; | ||
| 16 | #define CHECK(cond, msg) do { \ | ||
| 17 | if (cond) printf(" ok %s\n", msg); \ | ||
| 18 | else { printf(" FAIL %s\n", msg); g_fail++; } \ | ||
| 19 | } while (0) | ||
| 20 | |||
| 21 | // Build a syntactically-valid-enough CTransaction that GetHash()s uniquely | ||
| 22 | // per `nSeed` -- doesn't need to be consensus-valid, this test only | ||
| 23 | // exercises short-ID computation and reconstruction, not script validation. | ||
| 24 | static CTransaction MakeTx(int nSeed) | ||
| 25 | { | ||
| 26 | CTransaction tx; | ||
| 27 | tx.nVersion = 1; | ||
| 28 | tx.nLockTime = nSeed; // cheapest way to make each tx's serialization (and hash) unique | ||
| 29 | return tx; | ||
| 30 | } | ||
| 31 | |||
| 32 | int main() | ||
| 33 | { | ||
| 34 | // Build a block: coinbase + 20 regular transactions. | ||
| 35 | CBlock block; | ||
| 36 | block.nVersion = 1; | ||
| 37 | block.hashPrevBlock = uint256("0x00000000000000000000000000000000000000000000000000000000000abc"); | ||
| 38 | block.nTime = 1234567890; | ||
| 39 | block.nBits = 0x1f0fffff; | ||
| 40 | block.nNonce = 42; | ||
| 41 | block.vtx.push_back(MakeTx(0)); // coinbase | ||
| 42 | for (int i = 1; i <= 20; i++) | ||
| 43 | block.vtx.push_back(MakeTx(i)); | ||
| 44 | block.hashMerkleRoot = block.BuildMerkleTree(); | ||
| 45 | |||
| 46 | printf("build_and_reconstruct_full_mempool\n"); | ||
| 47 | { | ||
| 48 | CCmpctBlock cmpct; | ||
| 49 | CHECK(BuildCompactBlock(block, cmpct), "BuildCompactBlock succeeds"); | ||
| 50 | CHECK(cmpct.vPrefilledIndex.size() == 1 && cmpct.vPrefilledIndex[0] == 0, "only the coinbase is prefilled"); | ||
| 51 | CHECK(cmpct.vShortTxId.size() == 20, "one short ID per remaining tx"); | ||
| 52 | |||
| 53 | // Simulate a peer with every transaction already in its mempool. | ||
| 54 | mapTransactions.clear(); | ||
| 55 | for (size_t i = 1; i < block.vtx.size(); i++) | ||
| 56 | mapTransactions[block.vtx[i].GetHash()] = block.vtx[i]; | ||
| 57 | |||
| 58 | CBlock rebuilt; | ||
| 59 | vector<unsigned int> vMissing; | ||
| 60 | CHECK(TryReconstructBlock(cmpct, rebuilt, vMissing), "fully reconstructs when mempool has everything"); | ||
| 61 | CHECK(vMissing.empty(), "nothing reported missing"); | ||
| 62 | CHECK(rebuilt.GetHash() == block.GetHash(), "reconstructed block hash matches original exactly"); | ||
| 63 | CHECK(rebuilt.vtx.size() == block.vtx.size(), "same transaction count"); | ||
| 64 | bool fAllMatch = true; | ||
| 65 | for (size_t i = 0; i < block.vtx.size(); i++) | ||
| 66 | if (rebuilt.vtx[i].GetHash() != block.vtx[i].GetHash()) fAllMatch = false; | ||
| 67 | CHECK(fAllMatch, "every transaction in the right position"); | ||
| 68 | } | ||
| 69 | |||
| 70 | printf("partial_mempool_then_getblocktxn_completes_it\n"); | ||
| 71 | { | ||
| 72 | CCmpctBlock cmpct; | ||
| 73 | BuildCompactBlock(block, cmpct); | ||
| 74 | |||
| 75 | // Simulate a peer missing 3 of the 20 non-coinbase transactions. | ||
| 76 | mapTransactions.clear(); | ||
| 77 | vector<unsigned int> expectMissing; | ||
| 78 | for (size_t i = 1; i < block.vtx.size(); i++) | ||
| 79 | { | ||
| 80 | if (i == 5 || i == 12 || i == 19) { expectMissing.push_back((unsigned int)i); continue; } | ||
| 81 | mapTransactions[block.vtx[i].GetHash()] = block.vtx[i]; | ||
| 82 | } | ||
| 83 | |||
| 84 | CBlock rebuilt; | ||
| 85 | vector<unsigned int> vMissing; | ||
| 86 | CHECK(!TryReconstructBlock(cmpct, rebuilt, vMissing), "reports incomplete when mempool is missing some tx"); | ||
| 87 | CHECK(vMissing == expectMissing, "missing indices are exactly the ones not in mempool"); | ||
| 88 | |||
| 89 | // Simulate the getblocktxn round trip: peer sends back the missing txs. | ||
| 90 | vector<CTransaction> vTxn; | ||
| 91 | foreach(unsigned int idx, vMissing) | ||
| 92 | vTxn.push_back(block.vtx[idx]); | ||
| 93 | |||
| 94 | CHECK(FillMissingAndFinish(rebuilt, vMissing, vTxn), "FillMissingAndFinish succeeds with matching response"); | ||
| 95 | CHECK(rebuilt.GetHash() == block.GetHash(), "block hash matches after filling in the missing tx"); | ||
| 96 | } | ||
| 97 | |||
| 98 | printf("mismatched_blocktxn_response_rejected\n"); | ||
| 99 | { | ||
| 100 | CCmpctBlock cmpct; | ||
| 101 | BuildCompactBlock(block, cmpct); | ||
| 102 | mapTransactions.clear(); | ||
| 103 | mapTransactions[block.vtx[1].GetHash()] = block.vtx[1]; // only one in mempool | ||
| 104 | |||
| 105 | CBlock rebuilt; | ||
| 106 | vector<unsigned int> vMissing; | ||
| 107 | TryReconstructBlock(cmpct, rebuilt, vMissing); | ||
| 108 | CHECK(vMissing.size() == 19, "19 missing when mempool has only 1 of 20"); | ||
| 109 | |||
| 110 | vector<CTransaction> vWrongCount; | ||
| 111 | vWrongCount.push_back(block.vtx[2]); // way fewer than vMissing.size() | ||
| 112 | CHECK(!FillMissingAndFinish(rebuilt, vMissing, vWrongCount), "rejects a response with the wrong tx count"); | ||
| 113 | } | ||
| 114 | |||
| 115 | printf("different_blocks_give_different_short_ids_for_same_tx\n"); | ||
| 116 | { | ||
| 117 | // Same transaction, two different (simulated) blocks -- short IDs | ||
| 118 | // must differ, since they're keyed per-block (hashPrevBlock + | ||
| 119 | // fresh nonce each time), not just derived from the txid alone. | ||
| 120 | CTransaction tx = MakeTx(99); | ||
| 121 | uint256 prevA = uint256("0x1111111111111111111111111111111111111111111111111111111111111111"); | ||
| 122 | uint256 prevB = uint256("0x2222222222222222222222222222222222222222222222222222222222222222"); | ||
| 123 | uint64 id1 = ComputeShortTxId(prevA, 0xAAAABBBBCCCCDDDDULL, tx.GetHash()); | ||
| 124 | uint64 id2 = ComputeShortTxId(prevB, 0xAAAABBBBCCCCDDDDULL, tx.GetHash()); | ||
| 125 | uint64 id3 = ComputeShortTxId(prevA, 0x1111222233334444ULL, tx.GetHash()); | ||
| 126 | CHECK(id1 != id2, "different hashPrevBlock -> different short ID"); | ||
| 127 | CHECK(id1 != id3, "different nonce -> different short ID"); | ||
| 128 | CHECK(id1 <= 0xFFFFFFFFFFFFULL, "short ID fits in 48 bits"); | ||
| 129 | } | ||
| 130 | |||
| 131 | printf("\n%s (%d failures)\n", g_fail == 0 ? "ALL TESTS PASSED" : "TESTS FAILED", g_fail); | ||
| 132 | return g_fail == 0 ? 0 : 1; | ||
| 133 | } | ||
diff --git a/src/test_rvaccept.cpp b/src/test_rvaccept.cpp new file mode 100644 index 0000000..afa9046 --- /dev/null +++ b/src/test_rvaccept.cpp | |||
| @@ -0,0 +1,180 @@ | |||
| 1 | // Local regression test for the pool's rendezvous accept path. | ||
| 2 | // | ||
| 3 | // Reproduces the exact failure that took remote miners off the pool: since | ||
| 4 | // upstream #30, RvServiceRegister returns as soon as the relay lists the | ||
| 5 | // service and no longer consumes the relay's 0x01 pairing byte. A service that | ||
| 6 | // goes straight from RvServiceRegister to BtfServiceWrap therefore reads that | ||
| 7 | // 0x01 as the first byte of the client's 32-byte ephemeral key, shifting the | ||
| 8 | // whole stream by one. X25519 has no confirmation step, so the handshake | ||
| 9 | // "succeeds" against a garbage shared secret and the corruption only shows up | ||
| 10 | // as a tunnel that never carries a valid byte. | ||
| 11 | // | ||
| 12 | // Build: g++ -std=gnu++14 -O2 -pthread test_rvaccept.cpp btftunnel.cpp btfrv.cpp \ | ||
| 13 | // btfchan.cpp -lsodium -o test_rvaccept | ||
| 14 | // | ||
| 15 | // Build: g++ -std=gnu++14 -O2 -pthread test_rvaccept.cpp btftunnel.cpp \ | ||
| 16 | // btfrv.cpp btfchan.cpp -lsodium -o test_rvaccept | ||
| 17 | // | ||
| 18 | // Run against a bitflashd relay already listening on 127.0.0.1:<port>. | ||
| 19 | // test_rvaccept broken <port> -- the pre-fix pool code path, expected to FAIL | ||
| 20 | // test_rvaccept fixed <port> -- with RvServiceWaitPaired, expected to PASS | ||
| 21 | |||
| 22 | #include "btftunnel.h" | ||
| 23 | #include "btfrv.h" | ||
| 24 | #include "btfchan.h" | ||
| 25 | |||
| 26 | #include <sys/socket.h> | ||
| 27 | #include <sys/time.h> | ||
| 28 | #include <unistd.h> | ||
| 29 | #include <cstdio> | ||
| 30 | #include <cstdlib> | ||
| 31 | #include <cstring> | ||
| 32 | #include <string> | ||
| 33 | #include <vector> | ||
| 34 | #include <thread> | ||
| 35 | #include <chrono> | ||
| 36 | |||
| 37 | using namespace btf; | ||
| 38 | |||
| 39 | static const char* HOST = "127.0.0.1"; | ||
| 40 | static unsigned short PORT = 18446; | ||
| 41 | static const char* MSG = "mining.subscribe stand-in payload"; | ||
| 42 | |||
| 43 | static void SetTimeout(int fd, int secs) | ||
| 44 | { | ||
| 45 | struct timeval tv; | ||
| 46 | tv.tv_sec = secs; | ||
| 47 | tv.tv_usec = 0; | ||
| 48 | setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | ||
| 49 | setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); | ||
| 50 | } | ||
| 51 | |||
| 52 | static bool RecvN(int fd, void* buf, int n) | ||
| 53 | { | ||
| 54 | char* p = (char*)buf; | ||
| 55 | int off = 0; | ||
| 56 | while (off < n) { | ||
| 57 | int r = recv(fd, p + off, n - off, 0); | ||
| 58 | if (r <= 0) return false; | ||
| 59 | off += r; | ||
| 60 | } | ||
| 61 | return true; | ||
| 62 | } | ||
| 63 | |||
| 64 | static bool SendN(int fd, const void* buf, int n) | ||
| 65 | { | ||
| 66 | const char* p = (const char*)buf; | ||
| 67 | int off = 0; | ||
| 68 | while (off < n) { | ||
| 69 | int r = send(fd, p + off, n - off, 0); | ||
| 70 | if (r <= 0) return false; | ||
| 71 | off += r; | ||
| 72 | } | ||
| 73 | return true; | ||
| 74 | } | ||
| 75 | |||
| 76 | static bool g_serviceEcho = false; | ||
| 77 | static std::string g_serviceSaw; | ||
| 78 | |||
| 79 | // The pool's AcceptOneFn, in both shapes. | ||
| 80 | static void ServiceThread(bool fWaitPaired, | ||
| 81 | std::vector<unsigned char> id, | ||
| 82 | std::vector<unsigned char> enc_sk, | ||
| 83 | int len) | ||
| 84 | { | ||
| 85 | RvSocket rv = RvServiceRegister(HOST, PORT, id.data()); | ||
| 86 | if (rv == RV_INVALID) { printf(" service: RvServiceRegister failed\n"); return; } | ||
| 87 | |||
| 88 | if (fWaitPaired) { | ||
| 89 | if (!RvServiceWaitPaired(rv)) { | ||
| 90 | printf(" service: RvServiceWaitPaired failed\n"); | ||
| 91 | RvClose(rv); | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | printf(" service: paired (0x01 consumed by RvServiceWaitPaired)\n"); | ||
| 95 | } else { | ||
| 96 | printf(" service: skipped RvServiceWaitPaired (pre-fix behaviour)\n"); | ||
| 97 | } | ||
| 98 | |||
| 99 | btf_socket_t app = BtfServiceWrap(rv, enc_sk.data()); | ||
| 100 | if (app == INVALID_SOCKET) { printf(" service: BtfServiceWrap failed\n"); return; } | ||
| 101 | printf(" service: BtfServiceWrap returned a tunnel\n"); | ||
| 102 | |||
| 103 | SetTimeout(app, 5); | ||
| 104 | std::vector<char> buf(len); | ||
| 105 | if (!RecvN(app, buf.data(), len)) { | ||
| 106 | printf(" service: never received the client's plaintext\n"); | ||
| 107 | close(app); | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | g_serviceSaw.assign(buf.data(), len); | ||
| 111 | g_serviceEcho = SendN(app, buf.data(), len); | ||
| 112 | close(app); | ||
| 113 | } | ||
| 114 | |||
| 115 | int main(int argc, char* argv[]) | ||
| 116 | { | ||
| 117 | bool fWaitPaired = (argc >= 2 && strcmp(argv[1], "fixed") == 0); | ||
| 118 | if (argc >= 3) PORT = (unsigned short)atoi(argv[2]); | ||
| 119 | |||
| 120 | if (!TunnelInit()) { printf("TunnelInit failed\n"); return 2; } | ||
| 121 | |||
| 122 | unsigned char enc_pk[32], enc_sk[32]; | ||
| 123 | ChanKeypair(enc_pk, enc_sk); | ||
| 124 | |||
| 125 | std::vector<unsigned char> id(32); | ||
| 126 | for (int i = 0; i < 32; i++) id[i] = (unsigned char)(i * 7 + 13); | ||
| 127 | |||
| 128 | const std::string msg(MSG); | ||
| 129 | const int len = (int)msg.size(); | ||
| 130 | |||
| 131 | printf(" mode: %s relay: %s:%u\n", fWaitPaired ? "fixed" : "broken", HOST, PORT); | ||
| 132 | |||
| 133 | std::thread svc(ServiceThread, fWaitPaired, id, | ||
| 134 | std::vector<unsigned char>(enc_sk, enc_sk + 32), len); | ||
| 135 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); | ||
| 136 | |||
| 137 | int nFail = 0; | ||
| 138 | |||
| 139 | btf_socket_t app = BtfClientTunnel(HOST, PORT, id.data(), enc_pk); | ||
| 140 | if (app == INVALID_SOCKET) { | ||
| 141 | printf(" client: BtfClientTunnel failed\n"); | ||
| 142 | nFail++; | ||
| 143 | } else { | ||
| 144 | printf(" client: tunnel established\n"); | ||
| 145 | SetTimeout(app, 5); | ||
| 146 | if (!SendN(app, msg.data(), len)) { printf(" client: send failed\n"); nFail++; } | ||
| 147 | std::vector<char> echo(len); | ||
| 148 | if (!RecvN(app, echo.data(), len)) { | ||
| 149 | printf(" client: no echo came back\n"); | ||
| 150 | nFail++; | ||
| 151 | } else if (std::string(echo.data(), len) != msg) { | ||
| 152 | printf(" client: echo did not match what was sent\n"); | ||
| 153 | nFail++; | ||
| 154 | } else { | ||
| 155 | printf(" client: round-tripped the payload intact\n"); | ||
| 156 | } | ||
| 157 | close(app); | ||
| 158 | } | ||
| 159 | |||
| 160 | svc.join(); | ||
| 161 | |||
| 162 | if (g_serviceSaw != msg) { | ||
| 163 | printf(" service did not see the correct plaintext (saw %d bytes)\n", | ||
| 164 | (int)g_serviceSaw.size()); | ||
| 165 | nFail++; | ||
| 166 | } | ||
| 167 | if (!g_serviceEcho) { printf(" service could not echo\n"); nFail++; } | ||
| 168 | |||
| 169 | bool fPassed = (nFail == 0); | ||
| 170 | printf(" RESULT: %s\n", fPassed ? "PASS -- tunnel carries plaintext end to end" | ||
| 171 | : "FAIL -- tunnel never carried a valid byte"); | ||
| 172 | |||
| 173 | // A broken run is the expected outcome pre-fix, so exit 0 when the | ||
| 174 | // observed behaviour matches the expectation for the mode under test. | ||
| 175 | bool fAsExpected = (fWaitPaired ? fPassed : !fPassed); | ||
| 176 | printf(" EXPECTED %s -> %s\n", | ||
| 177 | fWaitPaired ? "PASS" : "FAIL", | ||
| 178 | fAsExpected ? "as expected" : "UNEXPECTED"); | ||
| 179 | return fAsExpected ? 0 : 1; | ||
| 180 | } | ||
diff --git a/src/tor.cpp b/src/tor.cpp index 20b4577..fea6912 100644 --- a/src/tor.cpp +++ b/src/tor.cpp | |||
| @@ -827,17 +827,26 @@ void BtfStopManagedTor() | |||
| 827 | memset(&g_managedTorProcess, 0, sizeof(g_managedTorProcess)); | 827 | memset(&g_managedTorProcess, 0, sizeof(g_managedTorProcess)); |
| 828 | } | 828 | } |
| 829 | #else | 829 | #else |
| 830 | if (g_managedTorPid > 0) | 830 | // Snapshot the pid under the lock. The watchdog's ManagedTorProcessAlive |
| 831 | // reaps the same child concurrently and resets g_managedTorPid under | ||
| 832 | // cs_managedTor; reading the global unlocked here could leave us waiting | ||
| 833 | // on ECHILD and then SIGKILLing a recycled PID. | ||
| 834 | pid_t pid = -1; | ||
| 835 | CRITICAL_BLOCK(cs_managedTor) | ||
| 836 | pid = g_managedTorPid; | ||
| 837 | if (pid > 0) | ||
| 831 | { | 838 | { |
| 832 | std::string shutdownErr; | 839 | std::string shutdownErr; |
| 833 | if (!RequestManagedTorShutdown(shutdownErr)) | 840 | if (!RequestManagedTorShutdown(shutdownErr)) |
| 834 | kill(g_managedTorPid, SIGTERM); | 841 | kill(pid, SIGTERM); |
| 835 | bool fExited = false; | 842 | bool fExited = false; |
| 836 | for (int i = 0; i < 100; i++) | 843 | for (int i = 0; i < 100; i++) |
| 837 | { | 844 | { |
| 838 | int status = 0; | 845 | int status = 0; |
| 839 | pid_t r = waitpid(g_managedTorPid, &status, WNOHANG); | 846 | pid_t r = waitpid(pid, &status, WNOHANG); |
| 840 | if (r == g_managedTorPid) | 847 | // ECHILD means the watchdog already reaped it -- that is exit, |
| 848 | // not a reason to signal anything. | ||
| 849 | if (r == pid || (r == -1 && errno == ECHILD)) | ||
| 841 | { | 850 | { |
| 842 | fExited = true; | 851 | fExited = true; |
| 843 | break; | 852 | break; |
| @@ -846,10 +855,12 @@ void BtfStopManagedTor() | |||
| 846 | } | 855 | } |
| 847 | if (!fExited) | 856 | if (!fExited) |
| 848 | { | 857 | { |
| 849 | kill(g_managedTorPid, SIGKILL); | 858 | kill(pid, SIGKILL); |
| 850 | waitpid(g_managedTorPid, NULL, 0); | 859 | waitpid(pid, NULL, 0); |
| 851 | } | 860 | } |
| 852 | g_managedTorPid = -1; | 861 | CRITICAL_BLOCK(cs_managedTor) |
| 862 | if (g_managedTorPid == pid) | ||
| 863 | g_managedTorPid = -1; | ||
| 853 | } | 864 | } |
| 854 | #endif | 865 | #endif |
| 855 | CRITICAL_BLOCK(cs_managedTor) | 866 | CRITICAL_BLOCK(cs_managedTor) |
diff --git a/src/util.cpp b/src/util.cpp index 0cd0133..ab29178 100644 --- a/src/util.cpp +++ b/src/util.cpp | |||
| @@ -7,6 +7,12 @@ | |||
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | bool fDebug = false; | 9 | bool fDebug = false; |
| 10 | bool fDebugRelay = false; | ||
| 11 | |||
| 12 | // In-process cap for debug.log, in bytes. Default matches the 1G trigger of | ||
| 13 | // the shipped logrotate policy so a long-running node stays bounded even | ||
| 14 | // without it; -maxdebugfilesize=N (MB) tunes it, 0 disables. | ||
| 15 | int64 nMaxDebugFileSize = 1024 * 1024 * (int64)1024; | ||
| 10 | 16 | ||
| 11 | // --------------------------------------------------------------------------- | 17 | // --------------------------------------------------------------------------- |
| 12 | // Categorized, burst-collapsing logging -- see the LogPrint doc comment in | 18 | // Categorized, burst-collapsing logging -- see the LogPrint doc comment in |
| @@ -26,10 +32,22 @@ static bool LogCategoryDefaultOn(const char* category) | |||
| 26 | strcmp(category, "payout") == 0; | 32 | strcmp(category, "payout") == 0; |
| 27 | } | 33 | } |
| 28 | 34 | ||
| 35 | // Categories so noisy they drown out everything else even under -debug: | ||
| 36 | // per-message send traces, wallet relay rebroadcasts, raw CBlock dumps and | ||
| 37 | // per-address discovery lines made up ~90% of a 13-hour production capture | ||
| 38 | // (issue #35). They need /debugrelay, not merely /debug. | ||
| 39 | static bool LogCategoryVerboseOnly(const char* category) | ||
| 40 | { | ||
| 41 | return strcmp(category, "relay") == 0 || | ||
| 42 | strcmp(category, "nostrdiscovery") == 0; | ||
| 43 | } | ||
| 44 | |||
| 29 | bool LogAcceptsCategory(const char* category) | 45 | bool LogAcceptsCategory(const char* category) |
| 30 | { | 46 | { |
| 31 | if (LogCategoryDefaultOn(category)) | 47 | if (LogCategoryDefaultOn(category)) |
| 32 | return true; | 48 | return true; |
| 49 | if (LogCategoryVerboseOnly(category)) | ||
| 50 | return fDebugRelay; | ||
| 33 | return fDebug; | 51 | return fDebug; |
| 34 | } | 52 | } |
| 35 | 53 | ||
| @@ -47,6 +65,61 @@ struct LogDedupState { | |||
| 47 | static std::map<std::string, LogDedupState> g_logDedup; | 65 | static std::map<std::string, LogDedupState> g_logDedup; |
| 48 | static CCriticalSection cs_logDedup; | 66 | static CCriticalSection cs_logDedup; |
| 49 | 67 | ||
| 68 | // A pending "(N similar)" summary, lifted out of the table while the lock is | ||
| 69 | // held and written after it is released. | ||
| 70 | struct LogDedupFlush { | ||
| 71 | std::string category; | ||
| 72 | int suppressed; | ||
| 73 | int64 elapsed; // how long the window that counted them ran | ||
| 74 | std::string lastMsg; | ||
| 75 | }; | ||
| 76 | |||
| 77 | static int64 g_logDedupLastSweep = 0; | ||
| 78 | |||
| 79 | static void EmitDedupSummary(const LogDedupFlush& f) | ||
| 80 | { | ||
| 81 | OutputDebugStringF("[%s] (%d similar in %llds, last: %s)\n", | ||
| 82 | f.category.c_str(), f.suppressed, | ||
| 83 | (long long)f.elapsed, f.lastMsg.c_str()); | ||
| 84 | } | ||
| 85 | |||
| 86 | // A window is otherwise only closed out when its own call site fires again, so | ||
| 87 | // a burst that simply stops -- 100 ledger entries voided in one pass and then | ||
| 88 | // silence -- leaves its count sitting in the table and the summary is never | ||
| 89 | // written at all. That is how 99 of 100 voids went unreported, with no "(N | ||
| 90 | // similar)" line to say anything was missing (issue #3). Sweep every expired | ||
| 91 | // window instead, so a finished burst is summarised as soon as anything at all | ||
| 92 | // is logged. | ||
| 93 | // | ||
| 94 | // Expired entries are erased rather than reopened: the window is over, so there | ||
| 95 | // is no state left worth keeping, and the next call from that site then takes | ||
| 96 | // the "first time" path and prints directly -- exactly what the rollover branch | ||
| 97 | // below would have done. It also stops the table growing without bound. | ||
| 98 | // | ||
| 99 | // Caller holds cs_logDedup; the summaries are written after it is released. | ||
| 100 | static void SweepExpiredWindowsLocked(int64 now, std::vector<LogDedupFlush>& out) | ||
| 101 | { | ||
| 102 | for (std::map<std::string, LogDedupState>::iterator it = g_logDedup.begin(); | ||
| 103 | it != g_logDedup.end(); ) | ||
| 104 | { | ||
| 105 | if (now - it->second.windowStart < DEDUP_WINDOW_SECS) | ||
| 106 | { | ||
| 107 | ++it; | ||
| 108 | continue; | ||
| 109 | } | ||
| 110 | if (it->second.suppressed > 0) | ||
| 111 | { | ||
| 112 | LogDedupFlush f; | ||
| 113 | f.category = it->first.substr(0, it->first.find('|')); | ||
| 114 | f.suppressed = it->second.suppressed; | ||
| 115 | f.elapsed = now - it->second.windowStart; | ||
| 116 | f.lastMsg = it->second.lastMsg; | ||
| 117 | out.push_back(f); | ||
| 118 | } | ||
| 119 | g_logDedup.erase(it++); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 50 | void LogPrintDedup(const char* category, const char* file, int line, const std::string& msgIn) | 123 | void LogPrintDedup(const char* category, const char* file, int line, const std::string& msgIn) |
| 51 | { | 124 | { |
| 52 | // Call sites follow the existing printf() convention of ending their | 125 | // Call sites follow the existing printf() convention of ending their |
| @@ -61,11 +134,19 @@ void LogPrintDedup(const char* category, const char* file, int line, const std:: | |||
| 61 | 134 | ||
| 62 | int64 now = GetTime(); | 135 | int64 now = GetTime(); |
| 63 | bool fEmitNow = false; | 136 | bool fEmitNow = false; |
| 64 | int suppressedToFlush = 0; | 137 | std::vector<LogDedupFlush> flushes; |
| 65 | std::string flushedMsg; | ||
| 66 | 138 | ||
| 67 | CRITICAL_BLOCK(cs_logDedup) | 139 | CRITICAL_BLOCK(cs_logDedup) |
| 68 | { | 140 | { |
| 141 | // At most once a second. The table holds one entry per call site that | ||
| 142 | // has fired in the last few seconds, so the walk is short, but it must | ||
| 143 | // not run once per line of a burst. | ||
| 144 | if (now != g_logDedupLastSweep) | ||
| 145 | { | ||
| 146 | g_logDedupLastSweep = now; | ||
| 147 | SweepExpiredWindowsLocked(now, flushes); | ||
| 148 | } | ||
| 149 | |||
| 69 | std::map<std::string, LogDedupState>::iterator it = g_logDedup.find(key); | 150 | std::map<std::string, LogDedupState>::iterator it = g_logDedup.find(key); |
| 70 | if (it == g_logDedup.end()) | 151 | if (it == g_logDedup.end()) |
| 71 | { | 152 | { |
| @@ -76,10 +157,18 @@ void LogPrintDedup(const char* category, const char* file, int line, const std:: | |||
| 76 | } | 157 | } |
| 77 | else if (now - it->second.windowStart >= DEDUP_WINDOW_SECS) | 158 | else if (now - it->second.windowStart >= DEDUP_WINDOW_SECS) |
| 78 | { | 159 | { |
| 79 | // Window rolled over: flush what was suppressed during it (if | 160 | // Window rolled over within the same second as the last sweep: |
| 80 | // anything), then start a new window with this call printed. | 161 | // flush what was suppressed during it (if anything), then start a |
| 81 | suppressedToFlush = it->second.suppressed; | 162 | // new window with this call printed. |
| 82 | flushedMsg = it->second.lastMsg; | 163 | if (it->second.suppressed > 0) |
| 164 | { | ||
| 165 | LogDedupFlush f; | ||
| 166 | f.category = category; | ||
| 167 | f.suppressed = it->second.suppressed; | ||
| 168 | f.elapsed = now - it->second.windowStart; | ||
| 169 | f.lastMsg = it->second.lastMsg; | ||
| 170 | flushes.push_back(f); | ||
| 171 | } | ||
| 83 | it->second.windowStart = now; | 172 | it->second.windowStart = now; |
| 84 | it->second.suppressed = 0; | 173 | it->second.suppressed = 0; |
| 85 | it->second.lastMsg = msg; | 174 | it->second.lastMsg = msg; |
| @@ -94,13 +183,23 @@ void LogPrintDedup(const char* category, const char* file, int line, const std:: | |||
| 94 | } | 183 | } |
| 95 | } | 184 | } |
| 96 | 185 | ||
| 97 | if (suppressedToFlush > 0) | 186 | for (size_t i = 0; i < flushes.size(); i++) |
| 98 | OutputDebugStringF("[%s] (%d similar in %llds, last: %s)\n", | 187 | EmitDedupSummary(flushes[i]); |
| 99 | category, suppressedToFlush, (long long)DEDUP_WINDOW_SECS, flushedMsg.c_str()); | ||
| 100 | if (fEmitNow) | 188 | if (fEmitNow) |
| 101 | OutputDebugStringF("[%s] %s\n", category, msg.c_str()); | 189 | OutputDebugStringF("[%s] %s\n", category, msg.c_str()); |
| 102 | } | 190 | } |
| 103 | 191 | ||
| 192 | // LogPrintAlways() -- see the doc comment in util.h. No window, no counting, | ||
| 193 | // no table entry: every call writes its own line. | ||
| 194 | void LogPrintNoCollapse(const char* category, const std::string& msgIn) | ||
| 195 | { | ||
| 196 | std::string msg = msgIn; | ||
| 197 | while (!msg.empty() && (msg.back() == '\n' || msg.back() == '\r')) | ||
| 198 | msg.pop_back(); | ||
| 199 | |||
| 200 | OutputDebugStringF("[%s] %s\n", category, msg.c_str()); | ||
| 201 | } | ||
| 202 | |||
| 104 | 203 | ||
| 105 | 204 | ||
| 106 | 205 | ||
| @@ -397,13 +496,37 @@ bool FileExists(const char* psz) | |||
| 397 | #endif | 496 | #endif |
| 398 | } | 497 | } |
| 399 | 498 | ||
| 400 | int GetFilesize(FILE* file) | 499 | #ifndef _WIN32 |
| 500 | // If off_t is 32 bits wide, ftello() truncates exactly like the int this | ||
| 501 | // function used to return. Fail the build rather than ship the silent | ||
| 502 | // version of the bug (32-bit hosts need -D_FILE_OFFSET_BITS=64). | ||
| 503 | static_assert(sizeof(off_t) >= 8, "GetFilesize needs a 64-bit off_t"); | ||
| 504 | #endif | ||
| 505 | |||
| 506 | // Returns the file's size in bytes, or -1 if it cannot be determined. | ||
| 507 | // | ||
| 508 | // This used to hold ftell()'s long in an int, so a file of 2 GiB or more came | ||
| 509 | // back negative. Its only caller is the debug.log roll in util.h, which skips | ||
| 510 | // the roll on a negative size -- i.e. rotation switched itself off for good at | ||
| 511 | // exactly the size the roll exists to prevent, and any cap of 2048 MB or more | ||
| 512 | // could never be exceeded at all. Both failures were silent. | ||
| 513 | int64 GetFilesize(FILE* file) | ||
| 401 | { | 514 | { |
| 402 | int nSavePos = ftell(file); | 515 | #ifdef _WIN32 |
| 403 | int nFilesize = -1; | 516 | __int64 nSavePos = _ftelli64(file); |
| 404 | if (fseek(file, 0, SEEK_END) == 0) | 517 | int64 nFilesize = -1; |
| 405 | nFilesize = ftell(file); | 518 | if (nSavePos >= 0 && _fseeki64(file, 0, SEEK_END) == 0) |
| 406 | fseek(file, nSavePos, SEEK_SET); | 519 | nFilesize = _ftelli64(file); |
| 520 | if (nSavePos >= 0) | ||
| 521 | _fseeki64(file, nSavePos, SEEK_SET); | ||
| 522 | #else | ||
| 523 | off_t nSavePos = ftello(file); | ||
| 524 | int64 nFilesize = -1; | ||
| 525 | if (nSavePos >= 0 && fseeko(file, 0, SEEK_END) == 0) | ||
| 526 | nFilesize = ftello(file); | ||
| 527 | if (nSavePos >= 0) | ||
| 528 | fseeko(file, nSavePos, SEEK_SET); | ||
| 529 | #endif | ||
| 407 | return nFilesize; | 530 | return nFilesize; |
| 408 | } | 531 | } |
| 409 | 532 | ||
| @@ -8,6 +8,13 @@ | |||
| 8 | #include <cxxabi.h> | 8 | #include <cxxabi.h> |
| 9 | #endif | 9 | #endif |
| 10 | 10 | ||
| 11 | // For AvailableCoreCount() below: sched_getaffinity on Linux, and | ||
| 12 | // hardware_concurrency as the fallback everywhere else. | ||
| 13 | #include <thread> | ||
| 14 | #ifdef __linux__ | ||
| 15 | #include <sched.h> | ||
| 16 | #endif | ||
| 17 | |||
| 11 | #if defined(_MSC_VER) || defined(__BORLANDC__) | 18 | #if defined(_MSC_VER) || defined(__BORLANDC__) |
| 12 | typedef __int64 int64; | 19 | typedef __int64 int64; |
| 13 | typedef unsigned __int64 uint64; | 20 | typedef unsigned __int64 uint64; |
| @@ -80,6 +87,15 @@ inline T& REF(const T& val) | |||
| 80 | 87 | ||
| 81 | extern bool fDebug; | 88 | extern bool fDebug; |
| 82 | 89 | ||
| 90 | // /debugrelay: the per-message firehose ("relay" and "nostrdiscovery" | ||
| 91 | // categories) that even -debug keeps off. See LogAcceptsCategory(). | ||
| 92 | extern bool fDebugRelay; | ||
| 93 | |||
| 94 | // debug.log is rolled to debug.log.old once it grows past this many bytes | ||
| 95 | // (set from -maxdebugfilesize, in MB; 0 disables the check). See the roll | ||
| 96 | // site in OutputDebugStringF() below. | ||
| 97 | extern int64 nMaxDebugFileSize; | ||
| 98 | |||
| 83 | // --------------------------------------------------------------------------- | 99 | // --------------------------------------------------------------------------- |
| 84 | // Categorized, burst-collapsing logging | 100 | // Categorized, burst-collapsing logging |
| 85 | // | 101 | // |
| @@ -99,15 +115,29 @@ extern bool fDebug; | |||
| 99 | // 2. Burst collapse: repeated LogPrint() calls from the *same source line* | 115 | // 2. Burst collapse: repeated LogPrint() calls from the *same source line* |
| 100 | // only emit one line per DEDUP_WINDOW_SECS; if more calls land inside | 116 | // only emit one line per DEDUP_WINDOW_SECS; if more calls land inside |
| 101 | // that window they're counted instead of reprinted, and the count is | 117 | // that window they're counted instead of reprinted, and the count is |
| 102 | // flushed as a single "(xN similar in Ws)" line the moment the window | 118 | // flushed as a single "(xN similar in Ws)" line once the window expires |
| 103 | // rolls over. A loop discovering 500 peers by address now produces a | 119 | // -- whether or not that call site ever fires again. A loop discovering |
| 104 | // small, bounded number of lines instead of 500 -- log growth is capped | 120 | // 500 peers by address now produces a small, bounded number of lines |
| 105 | // by elapsed time, not by how many similar events happen to fire. | 121 | // instead of 500 -- log growth is capped by elapsed time, not by how |
| 122 | // many similar events happen to fire. | ||
| 123 | // | ||
| 124 | // Use LogPrintAlways() instead for anything that reports an irreversible | ||
| 125 | // change to money: an obligation voided, a payment sent, a payment that | ||
| 126 | // failed. One line standing in for an unknown number of identical events is | ||
| 127 | // a tidy report of peer chatter and a false report of a ledger. A reorg | ||
| 128 | // voiding 100 entries hits one source line 100 times inside one window; | ||
| 129 | // under LogPrint() that read as a single block losing a single 49.49 BTF | ||
| 130 | // payment, while 4,950 BTF of obligations were actually destroyed, and the | ||
| 131 | // discrepancy was only found by counting entries in pending_payouts.json | ||
| 132 | // (issue #3). Bounded log growth is not worth that ambiguity here. | ||
| 106 | // --------------------------------------------------------------------------- | 133 | // --------------------------------------------------------------------------- |
| 107 | bool LogAcceptsCategory(const char* category); | 134 | bool LogAcceptsCategory(const char* category); |
| 108 | void LogPrintDedup(const char* category, const char* file, int line, const std::string& msg); | 135 | void LogPrintDedup(const char* category, const char* file, int line, const std::string& msg); |
| 136 | void LogPrintNoCollapse(const char* category, const std::string& msg); | ||
| 109 | #define LogPrint(category, ...) \ | 137 | #define LogPrint(category, ...) \ |
| 110 | do { if (LogAcceptsCategory(category)) LogPrintDedup((category), __FILE__, __LINE__, strprintf(__VA_ARGS__)); } while (0) | 138 | do { if (LogAcceptsCategory(category)) LogPrintDedup((category), __FILE__, __LINE__, strprintf(__VA_ARGS__)); } while (0) |
| 139 | #define LogPrintAlways(category, ...) \ | ||
| 140 | do { if (LogAcceptsCategory(category)) LogPrintNoCollapse((category), strprintf(__VA_ARGS__)); } while (0) | ||
| 111 | 141 | ||
| 112 | // Every format string in this project goes through one of the four functions | 142 | // Every format string in this project goes through one of the four functions |
| 113 | // below -- `printf` itself is remapped to OutputDebugStringF just above, and | 143 | // below -- `printf` itself is remapped to OutputDebugStringF just above, and |
| @@ -138,12 +168,40 @@ void ParseString(const string& str, char c, vector<string>& v); | |||
| 138 | string FormatMoney(int64 n, bool fPlus=false); | 168 | string FormatMoney(int64 n, bool fPlus=false); |
| 139 | bool ParseMoney(const char* pszIn, int64& nRet); | 169 | bool ParseMoney(const char* pszIn, int64& nRet); |
| 140 | bool FileExists(const char* psz); | 170 | bool FileExists(const char* psz); |
| 141 | int GetFilesize(FILE* file); | 171 | int64 GetFilesize(FILE* file); |
| 142 | uint64 GetRand(uint64 nMax); | 172 | uint64 GetRand(uint64 nMax); |
| 143 | int64 GetTime(); | 173 | int64 GetTime(); |
| 144 | int64 GetAdjustedTime(); | 174 | int64 GetAdjustedTime(); |
| 145 | void AddTimeData(unsigned int ip, int64 nTime); | 175 | void AddTimeData(unsigned int ip, int64 nTime); |
| 146 | 176 | ||
| 177 | // Cores this process is actually allowed to run on. | ||
| 178 | // | ||
| 179 | // std::thread::hardware_concurrency() reports the MACHINE's core count, which | ||
| 180 | // is not the same number inside a container. With --cpuset-cpus 0-5 on a | ||
| 181 | // 12-core host it still answers 12, so every caller that sized a thread pool | ||
| 182 | // from it oversubscribed by 2x and then wondered why the box was thrashing: | ||
| 183 | // the pool node started 11 miner threads on 6 permitted CPUs, and a burst | ||
| 184 | // miner asked for 2 cores filled the ~2 GB RandomX dataset with 12 threads. | ||
| 185 | // | ||
| 186 | // sched_getaffinity reports what the cpuset permits, which is the number every | ||
| 187 | // one of those callers meant. Falls back to hardware_concurrency where the | ||
| 188 | // call is unavailable, and never returns 0 -- callers divide by this. | ||
| 189 | inline unsigned int AvailableCoreCount() | ||
| 190 | { | ||
| 191 | #ifdef __linux__ | ||
| 192 | cpu_set_t cpuset; | ||
| 193 | CPU_ZERO(&cpuset); | ||
| 194 | if (sched_getaffinity(0, sizeof(cpuset), &cpuset) == 0) | ||
| 195 | { | ||
| 196 | int nAllowed = CPU_COUNT(&cpuset); | ||
| 197 | if (nAllowed > 0) | ||
| 198 | return (unsigned int)nAllowed; | ||
| 199 | } | ||
| 200 | #endif | ||
| 201 | unsigned int nCores = std::thread::hardware_concurrency(); | ||
| 202 | return nCores > 0 ? nCores : 1; | ||
| 203 | } | ||
| 204 | |||
| 147 | 205 | ||
| 148 | 206 | ||
| 149 | 207 | ||
| @@ -164,7 +222,7 @@ protected: | |||
| 164 | public: | 222 | public: |
| 165 | char* pszFile; | 223 | char* pszFile; |
| 166 | int nLine; | 224 | int nLine; |
| 167 | explicit CCriticalSection() { InitializeCriticalSection(&cs); } | 225 | explicit CCriticalSection() : pszFile(NULL), nLine(0) { InitializeCriticalSection(&cs); } |
| 168 | ~CCriticalSection() { DeleteCriticalSection(&cs); } | 226 | ~CCriticalSection() { DeleteCriticalSection(&cs); } |
| 169 | void Enter() { EnterCriticalSection(&cs); } | 227 | void Enter() { EnterCriticalSection(&cs); } |
| 170 | void Leave() { LeaveCriticalSection(&cs); } | 228 | void Leave() { LeaveCriticalSection(&cs); } |
| @@ -355,7 +413,65 @@ inline int OutputDebugStringF(const char* pszFormat, ...) | |||
| 355 | else | 413 | else |
| 356 | strDebugFile = "debug.log"; | 414 | strDebugFile = "debug.log"; |
| 357 | } | 415 | } |
| 416 | // Latched by a failed rename in the roll below, which cannot be | ||
| 417 | // retried usefully and must not be retried endlessly. | ||
| 418 | static bool fRollDisabled = false; | ||
| 358 | FILE* fileout = fopen(strDebugFile.c_str(), "a"); | 419 | FILE* fileout = fopen(strDebugFile.c_str(), "a"); |
| 420 | if (fileout && nMaxDebugFileSize > 0 && !fRollDisabled) | ||
| 421 | { | ||
| 422 | // Roll an overgrown debug.log before adding to it. Nothing in the | ||
| 423 | // tree shrinks this file once the process is running (there is no | ||
| 424 | // ShrinkDebugFile, and even Bitcoin's only ran at startup), so a | ||
| 425 | // node that stays up for months grows the log without bound -- | ||
| 426 | // production hit ~2 GiB. The file is opened and closed per line, | ||
| 427 | // so a rename is safe here: the next line simply reopens the | ||
| 428 | // path. One generation is kept as debug.log.old; logrotate | ||
| 429 | // (contrib/logrotate/) handles anything longer-term. | ||
| 430 | int64 nSize = GetFilesize(fileout); | ||
| 431 | if (nSize >= 0 && nSize > nMaxDebugFileSize) | ||
| 432 | { | ||
| 433 | fclose(fileout); | ||
| 434 | string strOldFile = strDebugFile + ".old"; | ||
| 435 | remove(strOldFile.c_str()); // rename() won't clobber on Windows | ||
| 436 | bool fRolled = (rename(strDebugFile.c_str(), strOldFile.c_str()) == 0); | ||
| 437 | int nRenameErrno = errno; // before fopen() can overwrite it | ||
| 438 | fileout = fopen(strDebugFile.c_str(), "a"); | ||
| 439 | if (fRolled) | ||
| 440 | { | ||
| 441 | if (fileout) | ||
| 442 | fprintf(fileout, | ||
| 443 | "debug.log grew past %lld MB; previous contents moved to debug.log.old (-maxdebugfilesize)\n", | ||
| 444 | (long long)(nMaxDebugFileSize / 1024 / 1024)); | ||
| 445 | } | ||
| 446 | else if (nRenameErrno == ENOENT) | ||
| 447 | { | ||
| 448 | // The file went away between the size check and the | ||
| 449 | // rename, so an external rotator (logrotate) got there | ||
| 450 | // first. Nothing left to roll and nothing wrong: the | ||
| 451 | // fopen above has already recreated it. Latching on this | ||
| 452 | // would let a benign race with logrotate switch the cap | ||
| 453 | // off for the rest of the run. | ||
| 454 | } | ||
| 455 | else | ||
| 456 | { | ||
| 457 | // The file is still over the cap, so this branch would be | ||
| 458 | // reached again on the next line and every line after it. | ||
| 459 | // Unlatched, the complaint about failing to roll becomes | ||
| 460 | // the runaway growth the roll exists to prevent. Say it | ||
| 461 | // once and stop trying -- on stderr too, because whatever | ||
| 462 | // blocked the rename (read-only mount, permissions, EXDEV | ||
| 463 | // across a bind mount) may equally block the log write. | ||
| 464 | fRollDisabled = true; | ||
| 465 | fprintf(stderr, | ||
| 466 | "cannot roll %s (rename to .old failed: %s); -maxdebugfilesize disabled for this run\n", | ||
| 467 | strDebugFile.c_str(), strerror(nRenameErrno)); | ||
| 468 | if (fileout) | ||
| 469 | fprintf(fileout, | ||
| 470 | "cannot roll debug.log (rename to .old failed: %s); -maxdebugfilesize disabled for this run\n", | ||
| 471 | strerror(nRenameErrno)); | ||
| 472 | } | ||
| 473 | } | ||
| 474 | } | ||
| 359 | if (fileout) | 475 | if (fileout) |
| 360 | { | 476 | { |
| 361 | va_list arg_ptr; | 477 | va_list arg_ptr; |