Skip to content

Commit 0cdc081

Browse files
committed
#985 add cutsom macro for block finalize download TCP fallback & add tests for it
1 parent d113de5 commit 0cdc081

11 files changed

Lines changed: 122 additions & 39 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ target_link_libraries(consensusd consensus_full)
339339
file(GLOB_RECURSE COMMON_TEST_SOURCES
340340
"tests/unit/datastructures/*.cpp"
341341
"tests/unit/db/*.cpp"
342+
"tests/unit/network/*.cpp"
342343
"tests/consensus_tests.cpp"
343344
"tests/sgx_tests.cpp"
344345
"tests/e2e/BlockFinalizeTransportCompatTests.cpp"

SkaleCommon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ static constexpr uint64_t STUCK_RESTART_INTERVAL_MS = 3 * 60 * 60 * 1000; // th
182182

183183
static constexpr uint64_t WAIT_AFTER_NETWORK_ERROR_MS = 3000;
184184

185+
static constexpr uint64_t BLOCK_FINALIZE_DOWNLOAD_TCP_FALLBACK_MS = 30 * 60 * 1000; // 30 minutes
186+
185187
static constexpr uint64_t CONNECTION_REFUSED_LOG_INTERVAL_MS = 10 * 60 * 1000;
186188

187189
static constexpr uint64_t CATCHUP_TIMEOUT_SEC = 30;

blockfinalize/client/BlockFinalizeDownloader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ bool BlockFinalizeDownloader::shouldUseTCPFallback( schain_index _dstIndex ) {
119119
void BlockFinalizeDownloader::markTCPFallback( schain_index _dstIndex ) {
120120
LOCK( m );
121121
tcpFallbackUntilMs[_dstIndex] =
122-
Time::getCurrentTimeMs() + getNode()->getWaitAfterNetworkErrorMs();
122+
Time::getCurrentTimeMs() + getNode()->getBlockFinalizeDownloadTcpFallbackMs();
123123
}
124124

125125
void BlockFinalizeDownloader::clearTCPFallback( schain_index _dstIndex ) {

blockfinalize/client/BlockFinalizeDownloader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class BlockFinalizeDownloader : public Agent {
151151
bool shouldUseTCPFallback( schain_index _dstIndex );
152152

153153
/**
154-
* Mark '_dstIndex' node to use TCP fallback for some time in the future,
155-
* based on current time + configured wait time after network error.
154+
* Mark '_dstIndex' node to use TCP fallback for some time in the future,
155+
* based on current time + configured TCP fallback duration.
156156
*/
157157
void markTCPFallback( schain_index _dstIndex );
158158

node/Node.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ void Node::initParamsFromConfig() {
278278
stuckRestartIntervalMs = getParamUint64( "stuckRestartIntervalMs", STUCK_RESTART_INTERVAL_MS );
279279
waitAfterNetworkErrorMs =
280280
getParamUint64( "waitAfterNetworkErrorMs", WAIT_AFTER_NETWORK_ERROR_MS );
281+
blockFinalizeDownloadTcpFallbackMs =
282+
getParamUint64( "blockFinalizeDownloadTcpFallbackMs", BLOCK_FINALIZE_DOWNLOAD_TCP_FALLBACK_MS );
281283
maxCatchupDownloadBytes =
282284
getParamUint64( "maxCatchupDownloadBytes", MAX_CATCHUP_DOWNLOAD_BYTES );
283285
maxTransactionsPerBlock =

node/Node.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class TestConfig;
5656
class BlockSigShareDB;
5757
class DASigShareDB;
5858
class DAProofDB;
59+
class NodeTestAccess;
5960

6061
#ifdef BITE
6162
class TEDecryptionDB;
@@ -79,6 +80,7 @@ enum PricingStrategyEnum { ZERO, DOS_PROTECT };
7980

8081
class Node {
8182
friend class Schain; // Allow Schain to access private barrier methods for teardown
83+
friend class NodeTestAccess; // Allow test access to private members
8284

8385
ConsensusEngine* consensusEngine;
8486

@@ -217,6 +219,8 @@ class Node {
217219

218220
uint64_t waitAfterNetworkErrorMs = 0;
219221

222+
uint64_t blockFinalizeDownloadTcpFallbackMs = 0;
223+
220224
uint64_t emptyBlockIntervalMs = 0;
221225

222226
uint64_t emptyBlockIntervalAfterCatchupMs = 0;
@@ -485,6 +489,8 @@ class Node {
485489

486490
uint64_t getWaitAfterNetworkErrorMs();
487491

492+
uint64_t getBlockFinalizeDownloadTcpFallbackMs();
493+
488494
#ifdef FAIR
489495
uint64_t getConstantGasPrice() const;
490496
#endif

node/NodeGettersSetters.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ uint64_t Node::getWaitAfterNetworkErrorMs() {
279279
return waitAfterNetworkErrorMs;
280280
}
281281

282+
uint64_t Node::getBlockFinalizeDownloadTcpFallbackMs() {
283+
return blockFinalizeDownloadTcpFallbackMs;
284+
}
285+
282286
uint64_t Node::getEmptyBlockIntervalMs() const {
283287
return emptyBlockIntervalMs;
284288
}

tests/TestUtils.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#pragma once
2525

26+
#include <cstdlib>
2627
#include <memory>
2728
#include <string>
2829

@@ -36,6 +37,59 @@
3637

3738
namespace TestUtils {
3839

40+
struct TestEnvVarSnapshot {
41+
bool hadPreviousValue = false;
42+
string previousValue;
43+
};
44+
45+
inline TestEnvVarSnapshot captureTestEnvVar( const string& _envName ) {
46+
TestEnvVarSnapshot snapshot;
47+
const char* previousValue = std::getenv( _envName.c_str() );
48+
if ( previousValue != nullptr ) {
49+
snapshot.hadPreviousValue = true;
50+
snapshot.previousValue = previousValue;
51+
}
52+
return snapshot;
53+
}
54+
55+
inline TestEnvVarSnapshot setTestEnvVar(
56+
const string& _envName, const string& _envValue, int _overwrite = 1 ) {
57+
auto snapshot = captureTestEnvVar( _envName );
58+
CATCH_REQUIRE( setenv( _envName.c_str(), _envValue.c_str(), _overwrite ) == 0 );
59+
return snapshot;
60+
}
61+
62+
inline void unsetTestEnvVar( const string& _envName ) {
63+
CATCH_REQUIRE( unsetenv( _envName.c_str() ) == 0 );
64+
}
65+
66+
inline void restoreTestEnvVar( const string& _envName, const TestEnvVarSnapshot& _snapshot ) {
67+
if ( _snapshot.hadPreviousValue ) {
68+
CATCH_REQUIRE( setenv( _envName.c_str(), _snapshot.previousValue.c_str(), 1 ) == 0 );
69+
} else {
70+
unsetTestEnvVar( _envName );
71+
}
72+
}
73+
74+
class TestEnvVarGuard {
75+
string envName;
76+
TestEnvVarSnapshot snapshot;
77+
78+
public:
79+
explicit TestEnvVarGuard( const string& _envName )
80+
: envName( _envName ), snapshot( captureTestEnvVar( _envName ) ) {
81+
unsetTestEnvVar( envName );
82+
}
83+
84+
~TestEnvVarGuard() {
85+
if ( snapshot.hadPreviousValue ) {
86+
setenv( envName.c_str(), snapshot.previousValue.c_str(), 1 );
87+
} else {
88+
unsetenv( envName.c_str() );
89+
}
90+
}
91+
};
92+
3993
/**
4094
* @brief Creates a minimal test Node with the given configuration
4195
*

tests/e2e/BlockFinalizeTransportCompatTests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct ScenarioResult {
4040
static ScenarioResult runScenario( const string& _configDir ) {
4141
ConsensusEngine* testEngine = nullptr;
4242
auto finalizationDownloadOnlySnapshot =
43-
E2EHelper::setTestEnvVar( FINALIZATION_DOWNLOAD_ONLY_ENV, "1", 1 );
43+
TestUtils::setTestEnvVar( FINALIZATION_DOWNLOAD_ONLY_ENV, "1", 1 );
4444

4545
try {
4646
E2EHelper::configureTestEnvironment( true, _configDir );
@@ -56,15 +56,15 @@ static ScenarioResult runScenario( const string& _configDir ) {
5656
CATCH_REQUIRE( stats.totalRequests() > 0 );
5757

5858
E2EHelper::stopEngineGracefully( testEngine );
59-
E2EHelper::restoreTestEnvVar(
59+
TestUtils::restoreTestEnvVar(
6060
FINALIZATION_DOWNLOAD_ONLY_ENV, finalizationDownloadOnlySnapshot );
6161

6262
return { block_id( lastId ), stats };
6363
} catch ( ... ) {
6464
if ( testEngine != nullptr ) {
6565
E2EHelper::stopEngineGracefully( testEngine );
6666
}
67-
E2EHelper::restoreTestEnvVar(
67+
TestUtils::restoreTestEnvVar(
6868
FINALIZATION_DOWNLOAD_ONLY_ENV, finalizationDownloadOnlySnapshot );
6969
throw;
7070
}

tests/e2e/E2ETestHelper.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ class E2ETestHelper {
5959

6060
boost::filesystem::create_directories( TEST_DATA_ROOT_DIR );
6161

62-
setTestEnvVar( "DATA_DIR", TEST_DATA_ROOT_DIR.c_str() );
62+
TestUtils::setTestEnvVar( "DATA_DIR", TEST_DATA_ROOT_DIR.c_str() );
6363

6464
// Keep default runtime for fast tests unless caller already set TEST_TIME_S.
65-
setTestEnvVar( "TEST_TIME_S", "4", 0 );
65+
TestUtils::setTestEnvVar( "TEST_TIME_S", "4", 0 );
6666
}
6767

6868
static void startEngine(
@@ -126,37 +126,6 @@ class E2ETestHelper {
126126
delete _engine;
127127
_engine = nullptr;
128128
}
129-
130-
// ========= Environment variable helpers for tests =========
131-
struct TestEnvVarSnapshot {
132-
bool hadPreviousValue = false;
133-
string previousValue;
134-
};
135-
136-
static TestEnvVarSnapshot setTestEnvVar(
137-
const string& _envName, const string& _envValue, int _overwrite = 1 ) {
138-
TestEnvVarSnapshot snapshot;
139-
const char* previousValue = std::getenv( _envName.c_str() );
140-
if ( previousValue != nullptr ) {
141-
snapshot.hadPreviousValue = true;
142-
snapshot.previousValue = previousValue;
143-
}
144-
145-
CATCH_REQUIRE( setenv( _envName.c_str(), _envValue.c_str(), _overwrite ) == 0 );
146-
return snapshot;
147-
}
148-
149-
static void unsetTestEnvVar( const string& _envName ) {
150-
CATCH_REQUIRE( unsetenv( _envName.c_str() ) == 0 );
151-
}
152-
153-
static void restoreTestEnvVar( const string& _envName, const TestEnvVarSnapshot& _snapshot ) {
154-
if ( _snapshot.hadPreviousValue ) {
155-
CATCH_REQUIRE( setenv( _envName.c_str(), _snapshot.previousValue.c_str(), 1 ) == 0 );
156-
} else {
157-
unsetTestEnvVar( _envName );
158-
}
159-
}
160129
};
161130

162131
} // namespace E2ETestUtils

0 commit comments

Comments
 (0)