upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--src/Makefile1
-rw-r--r--src/main.cpp16
-rw-r--r--src/main_gui.cpp7
-rw-r--r--src/makefile.mingw1
-rw-r--r--src/net.cpp32
-rw-r--r--src/net.h20
-rw-r--r--src/readme.txt3
-rw-r--r--src/selftest.cpp74
9 files changed, 140 insertions, 17 deletions
diff --git a/README.md b/README.md
index 94c727d..4843f5c 100644
--- a/README.md
+++ b/README.md
@@ -355,7 +355,8 @@ Other options worth knowing:
355 355
356```bash 356```bash
357-datadir=PATH # wallet and chain data elsewhere 357-datadir=PATH # wallet and chain data elsewhere
358-port=N # P2P listen port, default 8433 358-testnet # isolated test network datadir, port 18433, and message magic
359-port=N # P2P listen port, default 8433; testnet default 18433
359-socks=HOST:PORT # SOCKS5 for outbound Nostr, .btf relay, and onion peer dials 360-socks=HOST:PORT # SOCKS5 for outbound Nostr, .btf relay, and onion peer dials
360-tor[=HOST:PORT] # Tor mode; default local Tor SOCKS5 proxy is 127.0.0.1:9050 361-tor[=HOST:PORT] # Tor mode; default local Tor SOCKS5 proxy is 127.0.0.1:9050
361-managedtor[=PATH] # start Tor, create a hidden service, advertise its onion 362-managedtor[=PATH] # start Tor, create a hidden service, advertise its onion
diff --git a/src/Makefile b/src/Makefile
index b83ff58..ab0b307 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -193,6 +193,7 @@ tests: test_bip32 bitflash-node
193 ./bitflash-node -selftest=wallet-crypto 193 ./bitflash-node -selftest=wallet-crypto
194 ./bitflash-node -selftest=wallet-encrypt 194 ./bitflash-node -selftest=wallet-encrypt
195 ./bitflash-node -selftest=net-message 195 ./bitflash-node -selftest=net-message
196 ./bitflash-node -selftest=network-params
196 ./bitflash-node -selftest=consensus-limits 197 ./bitflash-node -selftest=consensus-limits
197 ./bitflash-node -selftest=pool-stratum 198 ./bitflash-node -selftest=pool-stratum
198 ./bitflash-node -selftest=parse-money 199 ./bitflash-node -selftest=parse-money
diff --git a/src/main.cpp b/src/main.cpp
index 5bd4028..6fd8c82 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2497,11 +2497,21 @@ string GetAppDir()
2497 { 2497 {
2498 return "."; 2498 return ".";
2499 } 2499 }
2500 static bool fMkdirDone; 2500 static string strCreatedBaseDir;
2501 if (!fMkdirDone) 2501 if (strCreatedBaseDir != strDir)
2502 { 2502 {
2503 fMkdirDone = true;
2504 _mkdir(strDir.c_str()); 2503 _mkdir(strDir.c_str());
2504 strCreatedBaseDir = strDir;
2505 }
2506 if (IsTestNet())
2507 {
2508 strDir += "/testnet";
2509 static string strCreatedTestnetDir;
2510 if (strCreatedTestnetDir != strDir)
2511 {
2512 _mkdir(strDir.c_str());
2513 strCreatedTestnetDir = strDir;
2514 }
2505 } 2515 }
2506 return strDir; 2516 return strDir;
2507} 2517}
diff --git a/src/main_gui.cpp b/src/main_gui.cpp
index 943799f..0a961bf 100644
--- a/src/main_gui.cpp
+++ b/src/main_gui.cpp
@@ -118,7 +118,7 @@ static void PrintUsage()
118 printf(" wallet-crypto, wallet-encrypt, wallet-portability,\n"); 118 printf(" wallet-crypto, wallet-encrypt, wallet-portability,\n");
119 printf(" net-message,\n"); 119 printf(" net-message,\n");
120 printf(" consensus-limits, pool-stratum,\n"); 120 printf(" consensus-limits, pool-stratum,\n");
121 printf(" parse-money, socks5-proxy, or managed-tor\n"); 121 printf(" parse-money, network-params, socks5-proxy, or managed-tor\n");
122 printf("\n"); 122 printf("\n");
123 printf("Mining mode:\n"); 123 printf("Mining mode:\n");
124 printf(" /operator\n"); 124 printf(" /operator\n");
@@ -143,7 +143,8 @@ static void PrintUsage()
143 printf(" /onionservice=HOST.onion:PORT (advertise this node's Tor hidden service)\n"); 143 printf(" /onionservice=HOST.onion:PORT (advertise this node's Tor hidden service)\n");
144 printf("\n"); 144 printf("\n");
145 printf("Network:\n"); 145 printf("Network:\n");
146 printf(" /port=N (P2P listen port, default 8433)\n"); 146 printf(" /testnet (isolated test network datadir, port, and magic)\n");
147 printf(" /port=N (P2P listen port, default 8433; testnet 18433)\n");
147 printf(" /socks=HOST:PORT (SOCKS5 proxy for Nostr, .btf relay, and onion dials)\n"); 148 printf(" /socks=HOST:PORT (SOCKS5 proxy for Nostr, .btf relay, and onion dials)\n");
148 printf(" /tor[=HOST:PORT] (Tor mode; default SOCKS5 proxy is 127.0.0.1:9050)\n"); 149 printf(" /tor[=HOST:PORT] (Tor mode; default SOCKS5 proxy is 127.0.0.1:9050)\n");
149 printf(" /managedtor[=PATH] (start Tor, create a hidden service, advertise its onion)\n"); 150 printf(" /managedtor[=PATH] (start Tor, create a hidden service, advertise its onion)\n");
@@ -205,6 +206,8 @@ static void ParseStartupArguments(int argc, char* argv[])
205 if (arg(argc,argv,"/datadir") || arg(argc,argv,"-datadir")) 206 if (arg(argc,argv,"/datadir") || arg(argc,argv,"-datadir"))
206 strSetDataDir = argval2(argc, argv, "/datadir", "-datadir"); 207 strSetDataDir = argval2(argc, argv, "/datadir", "-datadir");
207 208
209 SelectNetworkParams(arg(argc, argv, "/testnet") || arg(argc, argv, "-testnet"));
210
208 if (arg(argc,argv,"/debug") || arg(argc,argv,"-debug")) 211 if (arg(argc,argv,"/debug") || arg(argc,argv,"-debug"))
209 fDebug = true; 212 fDebug = true;
210 213
diff --git a/src/makefile.mingw b/src/makefile.mingw
index 55f186b..910e025 100644
--- a/src/makefile.mingw
+++ b/src/makefile.mingw
@@ -116,6 +116,7 @@ tests: test_bip32.exe bitflash.exe
116 ./bitflash.exe -selftest=wallet-backend-default 116 ./bitflash.exe -selftest=wallet-backend-default
117 ./bitflash.exe -selftest=wallet-convert 117 ./bitflash.exe -selftest=wallet-convert
118 ./bitflash.exe -selftest=net-message 118 ./bitflash.exe -selftest=net-message
119 ./bitflash.exe -selftest=network-params
119 ./bitflash.exe -selftest=consensus-limits 120 ./bitflash.exe -selftest=consensus-limits
120 ./bitflash.exe -selftest=pool-stratum 121 ./bitflash.exe -selftest=pool-stratum
121 ./bitflash.exe -selftest=socks5-proxy 122 ./bitflash.exe -selftest=socks5-proxy
diff --git a/src/net.cpp b/src/net.cpp
index 4603d2d..ecfd565 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -50,10 +50,14 @@ static int BtfPoll(BtfPollFd* pfd, unsigned int nfd, int nTimeoutMs)
50// 50//
51// Global state variables 51// Global state variables
52// 52//
53static const char pchMainnetMessageStart[4] = { 0xbf, 0x20, 0x5c, 0xfd };
54static const char pchTestnetMessageStart[4] = { (char)0xce, (char)0xe2, (char)0xc0, (char)0xff };
55static bool fTestNet = false;
56char pchMessageStart[4] = { 0xbf, 0x20, 0x5c, 0xfd };
53bool fClient = false; 57bool fClient = false;
54uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); 58uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
55CAddress addrLocalHost(0, DEFAULT_PORT, nLocalServices); 59CAddress addrLocalHost(0, GetDefaultPort(), nLocalServices);
56unsigned short nListenPort = DEFAULT_PORT; // local P2P port (tunable via /port) 60unsigned short nListenPort = GetDefaultPort(); // local P2P port (tunable via /port)
57CNode nodeLocalHost(INVALID_SOCKET, CAddress("127.0.0.1", nLocalServices)); 61CNode nodeLocalHost(INVALID_SOCKET, CAddress("127.0.0.1", nLocalServices));
58CNode* pnodeLocalHost = &nodeLocalHost; 62CNode* pnodeLocalHost = &nodeLocalHost;
59bool fShutdown = false; 63bool fShutdown = false;
@@ -90,6 +94,29 @@ static int64 nBtfHandshakeSilent = 0;
90static string strBtfLastDialFail; 94static string strBtfLastDialFail;
91static string strBtfLastHandshakeTimeout; 95static string strBtfLastHandshakeTimeout;
92 96
97bool IsTestNet()
98{
99 return fTestNet;
100}
101
102unsigned short GetDefaultPort()
103{
104 return htons(fTestNet ? TESTNET_PORT : MAINNET_PORT);
105}
106
107void SelectNetworkParams(bool fTestNetIn)
108{
109 if (fTestNet == fTestNetIn)
110 return;
111
112 fTestNet = fTestNetIn;
113 memcpy(pchMessageStart,
114 fTestNet ? pchTestnetMessageStart : pchMainnetMessageStart,
115 sizeof(pchMessageStart));
116 nListenPort = GetDefaultPort();
117 addrLocalHost.port = nListenPort;
118}
119
93void BtfChurnNoteResolveAttempt() 120void BtfChurnNoteResolveAttempt()
94{ 121{
95 CRITICAL_BLOCK(cs_btfChurn) 122 CRITICAL_BLOCK(cs_btfChurn)
@@ -288,6 +315,7 @@ string GetDiagnosticsText()
288 int nMedian = GetPeerMedianHeight(); 315 int nMedian = GetPeerMedianHeight();
289 316
290 str += "Bitflash node diagnostics\n"; 317 str += "Bitflash node diagnostics\n";
318 str += strprintf(" network %s\n", IsTestNet() ? "testnet" : "mainnet");
291 str += strprintf(" uptime %s\n", 319 str += strprintf(" uptime %s\n",
292 FormatAge(nNodeStartTime ? nNow - nNodeStartTime : -1).c_str()); 320 FormatAge(nNodeStartTime ? nNow - nNodeStartTime : -1).c_str());
293 if (nMedian < 0) 321 if (nMedian < 0)
diff --git a/src/net.h b/src/net.h
index e3d4a93..56f1341 100644
--- a/src/net.h
+++ b/src/net.h
@@ -13,7 +13,8 @@ class CBlockIndex;
13 13
14 14
15 15
16static const unsigned short DEFAULT_PORT = htons(8433); 16static const unsigned short MAINNET_PORT = 8433;
17static const unsigned short TESTNET_PORT = 18433;
17static const unsigned int PUBLISH_HOPS = 5; 18static const unsigned int PUBLISH_HOPS = 5;
18enum 19enum
19{ 20{
@@ -26,6 +27,9 @@ enum
26 27
27 28
28bool GetMyExternalIP(unsigned int& ipRet); 29bool GetMyExternalIP(unsigned int& ipRet);
30bool IsTestNet();
31unsigned short GetDefaultPort();
32void SelectNetworkParams(bool fTestNetIn);
29CNode* ConnectNodeBtf(const string& strBtfAddr); 33CNode* ConnectNodeBtf(const string& strBtfAddr);
30CNode* ConnectNodeBtfResolved(const string& strBtfAddr, const string& strMeeting, 34CNode* ConnectNodeBtfResolved(const string& strBtfAddr, const string& strMeeting,
31 const string& strOnion, const unsigned char enc_pub[32], 35 const string& strOnion, const unsigned char enc_pub[32],
@@ -219,15 +223,17 @@ void CheckForShutdown(int n);
219// The message start string is designed to be unlikely to occur in normal data. 223// The message start string is designed to be unlikely to occur in normal data.
220// The characters are rarely used upper ascii, not valid as UTF-8, and produce 224// The characters are rarely used upper ascii, not valid as UTF-8, and produce
221// a large 4-byte int at any alignment. 225// a large 4-byte int at any alignment.
222// Magic bytes unique to the Bitflash network (0xbf = "BF"). Second byte bumped 226// Magic bytes unique to the selected Bitflash network. Mainnet uses 0xbf =
223// to 0x20 for the 2026 stable relaunch so the old test chain can't interfere. 227// "BF"; testnet swaps the whole sequence so a test node cannot handshake with
224static const char pchMessageStart[4] = { 0xbf, 0x20, 0x5c, 0xfd }; 228// mainnet before consensus-level testnet activation exists.
229extern char pchMessageStart[4];
225 230
226class CMessageHeader 231class CMessageHeader
227{ 232{
228public: 233public:
229 enum { COMMAND_SIZE=12 }; 234 enum { COMMAND_SIZE=12 };
230 char pchMessageStart[sizeof(::pchMessageStart)]; 235 enum { MESSAGE_START_SIZE=4 };
236 char pchMessageStart[MESSAGE_START_SIZE];
231 char pchCommand[COMMAND_SIZE]; 237 char pchCommand[COMMAND_SIZE];
232 unsigned int nMessageSize; 238 unsigned int nMessageSize;
233 239
@@ -318,7 +324,7 @@ public:
318 nServices = 0; 324 nServices = 0;
319 memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); 325 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
320 ip = 0; 326 ip = 0;
321 port = DEFAULT_PORT; 327 port = GetDefaultPort();
322 nTime = GetAdjustedTime(); 328 nTime = GetAdjustedTime();
323 nLastFailed = 0; 329 nLastFailed = 0;
324 } 330 }
@@ -348,7 +354,7 @@ public:
348 nServices = nServicesIn; 354 nServices = nServicesIn;
349 memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); 355 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
350 ip = 0; 356 ip = 0;
351 port = DEFAULT_PORT; 357 port = GetDefaultPort();
352 nTime = GetAdjustedTime(); 358 nTime = GetAdjustedTime();
353 nLastFailed = 0; 359 nLastFailed = 0;
354 360
diff --git a/src/readme.txt b/src/readme.txt
index 752be66..5d62791 100644
--- a/src/readme.txt
+++ b/src/readme.txt
@@ -41,7 +41,8 @@ The resulting executable is src/bitflash.exe.
41 41
42Command-line options (in addition to the original ones) 42Command-line options (in addition to the original ones)
43------------------------------------------------------- 43-------------------------------------------------------
44 /port=N listen on P2P port N (default 8433) 44 /testnet use isolated test network datadir, port 18433, and message magic
45 /port=N listen on P2P port N (default 8433; testnet default 18433)
45 /socks=HOST:PORT 46 /socks=HOST:PORT
46 route outbound Nostr, .btf rendezvous, and direct onion peer 47 route outbound Nostr, .btf rendezvous, and direct onion peer
47 dials through a SOCKS5 proxy, e.g. /socks=127.0.0.1:9050 for local Tor 48 dials through a SOCKS5 proxy, e.g. /socks=127.0.0.1:9050 for local Tor
diff --git a/src/selftest.cpp b/src/selftest.cpp
index dc9936e..30bd28e 100644
--- a/src/selftest.cpp
+++ b/src/selftest.cpp
@@ -3076,6 +3076,76 @@ static int RunNetMessageSelfTest()
3076 return nFail == 0 ? 0 : 1; 3076 return nFail == 0 ? 0 : 1;
3077} 3077}
3078 3078
3079static int RunNetworkParamsSelfTest()
3080{
3081 fflush(stdout);
3082 printf("network-params self-test\n");
3083
3084 int nFail = 0;
3085 std::string strOldDataDir = strSetDataDir;
3086 try
3087 {
3088 SelectNetworkParams(false);
3089 nFail += Check(!IsTestNet(), "starts on mainnet after explicit selection") ? 0 : 1;
3090 nFail += Check(ntohs(GetDefaultPort()) == MAINNET_PORT,
3091 "mainnet default P2P port is 8433") ? 0 : 1;
3092 CAddress mainAddr("127.0.0.1");
3093 nFail += Check(mainAddr.port == htons(MAINNET_PORT),
3094 "mainnet address parsing uses the mainnet default port") ? 0 : 1;
3095 CMessageHeader mainHeader("ping", 0);
3096 char mainMagic[4];
3097 memcpy(mainMagic, mainHeader.pchMessageStart, sizeof(mainMagic));
3098
3099 SelectNetworkParams(true);
3100 nFail += Check(IsTestNet(), "testnet selection is sticky") ? 0 : 1;
3101 nFail += Check(ntohs(GetDefaultPort()) == TESTNET_PORT,
3102 "testnet default P2P port is 18433") ? 0 : 1;
3103 CAddress testAddr("127.0.0.1");
3104 nFail += Check(testAddr.port == htons(TESTNET_PORT),
3105 "testnet address parsing uses the testnet default port") ? 0 : 1;
3106 CMessageHeader testHeader("ping", 0);
3107 nFail += Check(memcmp(mainMagic, testHeader.pchMessageStart, sizeof(mainMagic)) != 0,
3108 "testnet message magic differs from mainnet") ? 0 : 1;
3109
3110 std::string tmp;
3111 if (!MakeTempDir(tmp))
3112 {
3113 nFail += Check(false, "creates a temporary data directory") ? 0 : 1;
3114 }
3115 else
3116 {
3117 strSetDataDir = tmp;
3118 SelectNetworkParams(false);
3119 std::string mainDir = GetAppDir();
3120 SelectNetworkParams(true);
3121 std::string testDir = GetAppDir();
3122 nFail += Check(mainDir == tmp,
3123 "mainnet uses the selected data directory") ? 0 : 1;
3124 nFail += Check(testDir == tmp + "/testnet",
3125 "testnet stores data below a testnet subdirectory") ? 0 : 1;
3126 RemoveTree(tmp);
3127 }
3128 }
3129 catch (const std::exception& e)
3130 {
3131 printf(" FAIL exception: %s\n", e.what());
3132 nFail++;
3133 }
3134 catch (...)
3135 {
3136 printf(" FAIL unknown exception\n");
3137 nFail++;
3138 }
3139
3140 strSetDataDir = strOldDataDir;
3141 SelectNetworkParams(false);
3142
3143 printf("%s (%d failure%s)\n", nFail == 0 ? "ALL TESTS PASSED" : "TESTS FAILED",
3144 nFail, nFail == 1 ? "" : "s");
3145 fflush(stdout);
3146 return nFail == 0 ? 0 : 1;
3147}
3148
3079static CBlock MakeSizedConsensusBlock(size_t nScriptBytes) 3149static CBlock MakeSizedConsensusBlock(size_t nScriptBytes)
3080{ 3150{
3081 CTransaction tx; 3151 CTransaction tx;
@@ -3517,6 +3587,8 @@ int RunSelfTest(const std::string& name)
3517 return RunWalletPortabilitySelfTest(); 3587 return RunWalletPortabilitySelfTest();
3518 if (name == "net-message") 3588 if (name == "net-message")
3519 return RunNetMessageSelfTest(); 3589 return RunNetMessageSelfTest();
3590 if (name == "network-params")
3591 return RunNetworkParamsSelfTest();
3520 if (name == "consensus-limits") 3592 if (name == "consensus-limits")
3521 return RunConsensusLimitsSelfTest(); 3593 return RunConsensusLimitsSelfTest();
3522 if (name == "pool-stratum") 3594 if (name == "pool-stratum")
@@ -3529,6 +3601,6 @@ int RunSelfTest(const std::string& name)
3529 return RunManagedTorSelfTest(); 3601 return RunManagedTorSelfTest();
3530 3602
3531 printf("Unknown self-test '%s'\n", name.c_str()); 3603 printf("Unknown self-test '%s'\n", name.c_str());
3532 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, consensus-limits, pool-stratum, parse-money, socks5-proxy, managed-tor\n"); 3604 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");
3533 return 1; 3605 return 1;
3534} 3606}