From fc6745129d5087a2dd5ab102ecf15594366785bb Mon Sep 17 00:00:00 2001 From: Duke Date: Wed, 11 Oct 2023 12:07:40 -0400 Subject: [PATCH 1/2] Fix randomx memory leak but create some mining errors This change lifts the declaration of the randomx VM out of an inner loop into the main function body of RandomXMiner(), which allows us to destroy it later on when catching exceptions. We cannot lift the allocation of it's memory (randomx_create_vm) because it depends on things that change in every iteration of the inner loop. Otherwise, the VM will only sometimes be destroyed, which is what I think causes the memleak. But this seems to create one invalid block when mining each block height : STDOUT: TestBlockValidity: failure C checkPOW=1 RandomXMiner: Invalid randomx block mined, try again 05f30f419133b2d862106b89c20059967639e4f2699dd5afc5d2b0832f1ac76a debug.log: 2023-10-11 16:10:41 CreateNewBlock(): total size 1000 blocktime.1697040642 nBits.200e77d1 2023-10-11 16:10:41 Running HushRandomXMiner with 1 transactions in block (260 bytes) 2023-10-11 16:10:41 ERROR: ContextualCheckBlock: block height mismatch in coinbase Mining does seem to continue normally when testing with -testnode=1 --- src/miner.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/miner.cpp b/src/miner.cpp index b1a4b917f..cae77d46a 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1122,6 +1122,7 @@ void static RandomXMiner() int randomxInterval = GetArg("-ac_randomx_interval",1024); // This lag is 80 mins for 75s blocktime and 64 mins for 60s (default) blocktime for HSCs int randomxBlockLag = GetArg("-ac_randomx_lag", 64); + randomx_vm *myVM = nullptr; try { // fprintf(stderr,"RandomXMiner: mining %s with randomx\n",SMART_CHAIN_SYMBOL); @@ -1198,7 +1199,7 @@ void static RandomXMiner() // randomx_init_dataset(randomxDataset, randomxCache, 0, datasetItemCount); rxdebug("%s: dataset initialized\n"); - randomx_vm *myVM = randomx_create_vm(flags, nullptr, randomxDataset); + myVM = randomx_create_vm(flags, nullptr, randomxDataset); if(myVM == NULL) { LogPrintf("RandomXMiner: Cannot create RandomX VM, aborting!\n"); return; @@ -1425,6 +1426,8 @@ void static RandomXMiner() miningTimer.stop(); c.disconnect(); + randomx_destroy_vm(myVM); + LogPrintf("%s: destroyed vm\n", __func__); randomx_release_dataset(randomxDataset); rxdebug("%s: released dataset\n"); randomx_release_cache(randomxCache); @@ -1437,6 +1440,8 @@ void static RandomXMiner() c.disconnect(); fprintf(stderr,"RandomXMiner: runtime error: %s\n", e.what()); + randomx_destroy_vm(myVM); + LogPrintf("%s: destroyed vm\n", __func__); randomx_release_dataset(randomxDataset); rxdebug("%s: released dataset\n"); randomx_release_cache(randomxCache); From 80bd3f262c66e7e55fdcf6e8defc2c8d1931f0bb Mon Sep 17 00:00:00 2001 From: Duke Date: Thu, 12 Oct 2023 10:01:01 -0400 Subject: [PATCH 2/2] Verbosify randomx debug logging in case that helps debug mismatched height coinbase issue --- src/miner.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index cae77d46a..11aae8122 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1427,11 +1427,11 @@ void static RandomXMiner() c.disconnect(); randomx_destroy_vm(myVM); - LogPrintf("%s: destroyed vm\n", __func__); + LogPrintf("%s: destroyed vm via thread interrupt\n", __func__); randomx_release_dataset(randomxDataset); - rxdebug("%s: released dataset\n"); + rxdebug("%s: released dataset via thread interrupt\n"); randomx_release_cache(randomxCache); - rxdebug("%s: released cache\n"); + rxdebug("%s: released cache via thread interrupt\n"); LogPrintf("HushRandomXMiner terminated\n"); throw; @@ -1441,19 +1441,19 @@ void static RandomXMiner() fprintf(stderr,"RandomXMiner: runtime error: %s\n", e.what()); randomx_destroy_vm(myVM); - LogPrintf("%s: destroyed vm\n", __func__); + LogPrintf("%s: destroyed vm because of error\n", __func__); randomx_release_dataset(randomxDataset); - rxdebug("%s: released dataset\n"); + rxdebug("%s: released dataset because of error\n"); randomx_release_cache(randomxCache); - rxdebug("%s: released cache\n"); + rxdebug("%s: released cache because of error\n"); return; } randomx_release_dataset(randomxDataset); - rxdebug("%s: released dataset\n"); + rxdebug("%s: released dataset in normal exit\n"); randomx_release_cache(randomxCache); - rxdebug("%s: released cache\n"); + rxdebug("%s: released cache in normal exit\n"); miningTimer.stop(); c.disconnect(); }