From a89a9128986674ac6266dae313f09fd81b417236 Mon Sep 17 00:00:00 2001 From: DcruBro Date: Thu, 23 Apr 2026 21:34:12 +0200 Subject: [PATCH] quality-of-life improvements, lower client slave thread stack to 512KB (maybe still too much), dynamic fullverify - freeing transactions after verification --- TODO.txt | 2 ++ include/constants.h | 2 ++ include/tcpd/tcpclient.h | 1 + include/tcpd/tcpserver.h | 1 + src/block/transaction.c | 2 +- src/main.c | 5 +++++ src/tcpd/tcpclient.c | 8 +++++++- src/tcpd/tcpserver.c | 8 +++++++- 8 files changed, 26 insertions(+), 3 deletions(-) diff --git a/TODO.txt b/TODO.txt index 60d2ed6..8ebb9a7 100644 --- a/TODO.txt +++ b/TODO.txt @@ -8,6 +8,8 @@ I need to figure out a way to make the privacy work without a UTXO system, and i Move the Networking Code to support win32 as well, as I'm just doing POSIX right now Maybe move the node system to an async event loop instead of spawning threads. +A potential race could occur if the P2P node receives a new block, or flushes a new block to disk while the user is running a full verify. + DONE: I want to move away from the Monero emission. I want to do something a bit radical for cryptocurrency, but I feel like it's necessary to make it more like money: a constant inflation rate of 1.5% per year. It's lower than fiat (USD is ~2.8% per year), and it additionally doesn't fluctuate during crisis. It's constant. diff --git a/include/constants.h b/include/constants.h index 8156c92..1ebf243 100644 --- a/include/constants.h +++ b/include/constants.h @@ -10,6 +10,8 @@ // Nets #define MAX_CONS 32 // Some baseline for now #define LISTEN_PORT 9393 +#define TCP_THREAD_STACK_SIZE (512 * 1024) // 512 KB. We could get away with like 128 KB since it's mostly just recv bufs, but it's good having some breathing room. + // This is also for client threads. The server has the default (~8 MB on POSIX). // Economics #define DECIMALS 1000000000000ULL diff --git a/include/tcpd/tcpclient.h b/include/tcpd/tcpclient.h index b25983c..b7c2138 100644 --- a/include/tcpd/tcpclient.h +++ b/include/tcpd/tcpclient.h @@ -4,6 +4,7 @@ #include #include +#include #include typedef struct { diff --git a/include/tcpd/tcpserver.h b/include/tcpd/tcpserver.h index 139e7f9..119fedd 100644 --- a/include/tcpd/tcpserver.h +++ b/include/tcpd/tcpserver.h @@ -4,6 +4,7 @@ #include #include #include +#include #include diff --git a/src/block/transaction.c b/src/block/transaction.c index a9f03f9..4acf3ac 100644 --- a/src/block/transaction.c +++ b/src/block/transaction.c @@ -85,7 +85,7 @@ bool Transaction_Verify(const signed_transaction_t* tx) { // If all checks pass, verify the signature return Crypto_VerifySignature( txHash, - sizeof(transaction_t), + 32, tx->signature.signature, tx->transaction.compressedPublicKey ); diff --git a/src/main.c b/src/main.c index 8001f42..cb51158 100644 --- a/src/main.c +++ b/src/main.c @@ -416,6 +416,11 @@ static bool VerifyChainFully(blockchain_t* chain) { if (memcmp(blk->header.merkleRoot, expectedMerkle, sizeof(expectedMerkle)) != 0) { return false; } + + // Transactions are persisted on disk. Once this block is fully verified, + // release its in-memory transaction list to reduce peak memory usage. + DynArr_destroy(blk->transactions); + blk->transactions = NULL; } return true; diff --git a/src/tcpd/tcpclient.c b/src/tcpd/tcpclient.c index 758b59d..6841e92 100644 --- a/src/tcpd/tcpclient.c +++ b/src/tcpd/tcpclient.c @@ -126,12 +126,18 @@ int TcpClient_Connect( client->on_connect(conn); } - if (pthread_create(&conn->ioThread, NULL, TcpClient_ThreadProc, client) != 0) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, TCP_THREAD_STACK_SIZE); + + if (pthread_create(&conn->ioThread, &attr, TcpClient_ThreadProc, client) != 0) { TcpConnection_Destroy(conn); free(conn); client->connection = NULL; + pthread_attr_destroy(&attr); return -1; } + pthread_attr_destroy(&attr); return 0; } diff --git a/src/tcpd/tcpserver.c b/src/tcpd/tcpserver.c index d50e36b..304ce9e 100644 --- a/src/tcpd/tcpserver.c +++ b/src/tcpd/tcpserver.c @@ -144,11 +144,17 @@ static void* TcpServer_threadprocess(void* ptr) { arg->clientPtr = heapCli; arg->serverPtr = svr; - if (pthread_create(&heapCli->ioThread, NULL, TcpServer_clientthreadprocess, arg) != 0) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, TCP_THREAD_STACK_SIZE); + + if (pthread_create(&heapCli->ioThread, &attr, TcpServer_clientthreadprocess, arg) != 0) { free(arg); TcpServer_Disconnect(svr, heapCli); + pthread_attr_destroy(&attr); continue; } + pthread_attr_destroy(&attr); } return NULL;