Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snap v4 final #2233

Open
wants to merge 209 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
209 commits
Select commit Hold shift + click to select a range
8c0eb72
StateChunk message request created with encode and decode
julianlen May 22, 2023
cefd790
StateChunkRequest message added in the MessageVisitor and the BlockPr…
julianlen May 22, 2023
daab35b
StateChunkMessage now is part of SyncProcessor instead of BlockProcessor
julianlen May 22, 2023
619c088
Nevermind, request are processed in the BlockProcessor
julianlen May 22, 2023
9aceb78
Fixes for sonarcloud
julianlen May 23, 2023
8f4b5b3
Added new internal service that requests state periodically
May 23, 2023
f5572b1
Added config option to enable state requests
May 24, 2023
9668a28
Changes in tests so they consider the new parameter for ChannelManage…
julianlen May 24, 2023
c085795
No hash required as input for the StateChunkRequest message
julianlen May 24, 2023
d24ac4a
There was an unused hash in the MessageType for the STATE_CHUNK_REQ_MSG
julianlen May 25, 2023
12a1dfb
Not tested yet! - Creation of message to encode the tries as key value
julianlen May 25, 2023
2b56197
When the server sends the chunk advance the lastKey value, besides, t…
julianlen May 25, 2023
0edc830
Modified StateRequester to ping-pong state requests with a node
May 25, 2023
7296b12
Refactored StateRequester and moved state request logic there
May 25, 2023
0a24985
Fixed state encoding
May 26, 2023
236c8c5
Added snapshot chunk size to config
May 26, 2023
d4daefe
Made key collection more efficient
May 30, 2023
7203141
feat: snapshot sync with preload of the complete state in memory
patogallaiovlabs Jun 6, 2023
74688a7
feat: snapshot sync with cached iterator in memory
patogallaiovlabs Jun 6, 2023
4caa497
feat: small improvement on the iterator loading
patogallaiovlabs Jun 7, 2023
8c2ddc7
feat: snapshot sync improvement chaging iterator, less memory usage
patogallaiovlabs Jun 7, 2023
9a784a8
feat: add rsk_nodeId rpc method
patogallaiovlabs Jun 12, 2023
c9f1285
feat: infinite loop for snapshot requests
patogallaiovlabs Jun 26, 2023
38e93ec
feat: ability to limit chunk by bytes
patogallaiovlabs Jun 26, 2023
3342063
Adding rpc benchmarking skeleton
Apr 17, 2023
1a8d7a1
fix: tests with main
patogallaiovlabs Jul 5, 2023
a3ae786
feat:add JMH and measure state trie read from DB, 1st iteration
patogallaiovlabs Jul 7, 2023
ebe9fcc
feat: Added a log with the time spent between a block arriving and be…
julianlen Jul 10, 2023
88f57d2
feat: add DB access bench tests and logs
patogallaiovlabs Jul 11, 2023
72ae3d8
fix: run in boton
patogallaiovlabs Jul 13, 2023
ff7ba7f
feat: evaluate LZ4 performance
Jul 18, 2023
b8b60df
feat: do not use ByteBuffer for LZ4 decompress
Jul 18, 2023
447825c
feat: improved logging for LZ4 compress
Jul 19, 2023
68f135a
feat: logged total chunk prep time
Jul 19, 2023
4a744de
feat: reduced chunk size
Jul 20, 2023
e69d2d6
feat: log compression factor
Jul 20, 2023
6a8ad2b
feat: lz4 compression is now configurable
Jul 21, 2023
441456e
feat: preliminary implementation of SnapSyncState
Jul 25, 2023
767ea70
feat: added specific timeout for Snap chunk requests
Jul 25, 2023
e7bc15b
feat: properly reset time elapsed on SnapSyncState
Jul 25, 2023
de87668
feat: set from & blockNo to 0 for testing purposes
Jul 27, 2023
d555b17
feat: new trie DB serializer, less memory consumption
patogallaiovlabs Jul 31, 2023
19d0226
fix: reset reference
patogallaiovlabs Aug 1, 2023
07cd1e9
feat: resume from offset, reconstruct trie, still buggy
patogallaiovlabs Aug 7, 2023
c9b89e8
fix: recovery process, base case was returning always 0, no comments :|
patogallaiovlabs Aug 8, 2023
c684938
feat: first implementation of the unitrie share protocol
patogallaiovlabs Aug 17, 2023
05bb443
fix: serialization error when rebasing
patogallaiovlabs Aug 17, 2023
2baa92a
fix: fix the iterator after the merge
patogallaiovlabs Aug 18, 2023
59d2e56
it prints messages once the node is synced
julianlen Aug 28, 2023
77ab029
Merge pull request #2114 from rsksmart/snapshot-processingtime
patogallaiovlabs Aug 29, 2023
26de565
wip
casiojapi Sep 12, 2023
c3add44
add process SnapStatusRequest and Response
casiojapi Sep 13, 2023
f7ca7a4
fix: compression logic, in some cases the compressed bytes are longer…
patogallaiovlabs Sep 14, 2023
a7663ae
feat: add incremental chunk size: 100, 200, 400, 800, 1600.
patogallaiovlabs Sep 14, 2023
dffd9aa
fix: change snap logger db to trace
patogallaiovlabs Sep 18, 2023
3e507a8
delete SnapStatus
casiojapi Sep 20, 2023
28d75d0
fix flow on SnapshotProcessor
casiojapi Sep 20, 2023
f02019f
fix message decoding
casiojapi Sep 20, 2023
5a92962
update snap status message
casiojapi Oct 3, 2023
b6a889a
fix log
casiojapi Oct 3, 2023
83f6aa0
clean logs
casiojapi Oct 3, 2023
5c32b8d
feat:save state trie
patogallaiovlabs Oct 3, 2023
c04ee47
Merge branch 'snapshot-sync-multipeer-message-wip' into snap-exp-v3
patogallaiovlabs Oct 3, 2023
162d995
Merge branch 'snapshot-sync-multipeer-message-wip' into snap-exp-v3
patogallaiovlabs Oct 3, 2023
38094a7
disable backwards sync update
casiojapi Oct 3, 2023
041f732
add chunk task division and execution
casiojapi Oct 4, 2023
ff0fab8
fix chunk task flow, now working
casiojapi Oct 5, 2023
a7ee92d
add getNextPeer
casiojapi Oct 5, 2023
e565e8a
get peers list instead of just best peer
casiojapi Oct 9, 2023
ef8b7bc
add parallel flag to snapshot config
casiojapi Oct 18, 2023
1bda4fc
update trie verification to only check hashes
casiojapi Oct 22, 2023
9ca5d38
feat: connect Snapsync with Long sync, previous 4k block interchange
patogallaiovlabs Oct 23, 2023
c9013ed
fix: improve properties management, messages exchange, logs and decid…
patogallaiovlabs Oct 24, 2023
7631955
Merge remote-tracking branch 'origin/snapshot-sync-multipeer-sequenti…
patogallaiovlabs Oct 24, 2023
2e4838d
fix: lines lost in merge, working version
patogallaiovlabs Oct 24, 2023
4ec6855
feature: SNAP SYNC, make all the chunks verifiables to the root. Also…
patogallaiovlabs Nov 2, 2023
5c79d8e
feat: this is temporal and to be removed, only experimental
patogallaiovlabs Nov 13, 2023
9034599
feat: this is temporal and to be removed, only experimental
patogallaiovlabs Nov 14, 2023
aaeee53
feat: Added NewBlockHashes message
julianlen Nov 21, 2023
2f893a3
add some parallel updates (super wip/draft)
casiojapi Oct 18, 2023
7ba298e
add parallel working version
casiojapi Oct 27, 2023
5eaeb4e
get rid of repeated code on ChunkTasks assignment
casiojapi Oct 31, 2023
fb8ba27
delete unused peer parameters
casiojapi Nov 13, 2023
f8961ab
fix: merge
patogallaiovlabs Nov 24, 2023
67083cf
fix: cleaning up code, preparing for experiments
patogallaiovlabs Nov 29, 2023
881be93
feat: add infinite loop for experiments
patogallaiovlabs Dec 4, 2023
06f122f
feat: add infinite loop for experiments
patogallaiovlabs Dec 4, 2023
fad30ad
fix: parallel was not working as it is
patogallaiovlabs Dec 4, 2023
d1f67d1
feat: make chunk request process in parallel, so the msg queue become…
patogallaiovlabs Dec 11, 2023
873a556
fix: dont duplicate chunk size, only 25
patogallaiovlabs Dec 12, 2023
e9676f8
fix: 5 min delay btw runs
patogallaiovlabs Dec 12, 2023
957dd5b
feat: log to measure the moment a block hash is broadcasted
julianlen Dec 18, 2023
b6e239f
fix: error in the new log, replaced arrived by sent
julianlen Dec 18, 2023
d2fd886
fix: added the node id of the block sender
julianlen Dec 20, 2023
5a3af85
fix: just for experiment. purposes I fixed the number of the block wh…
julianlen Dec 20, 2023
92e2362
fix: just for experiment. fix in the processSnapStatusREquest
julianlen Dec 20, 2023
8d77b20
Merge branch 'master' into snap-exp-v4-final
julianlen Jan 23, 2024
eca9995
fix: cleaned up code altered exclusively for experiments
julianlen Jan 24, 2024
1307906
fix: deleted the infinite loop
julianlen Jan 24, 2024
20bbc87
fix: logback.xml merge issue solved
julianlen Jan 24, 2024
42f6d95
fix: deleted more logs used just for experiments and workaround left …
julianlen Jan 31, 2024
cdb948e
fix: One call to a deleted method was left in the code, now deleted
julianlen Jan 31, 2024
56ddf1b
fix stateChunkRequestMessage() test
casiojapi Feb 9, 2024
1f829d2
Merge pull request #2254 from rsksmart/fix-snapshot-unit-tests
Vovchyk Feb 9, 2024
097cd31
Cleaned some stuff up
Vovchyk Feb 15, 2024
59db1eb
Merge pull request #2255 from rsksmart/snap-cleanup
Vovchyk Feb 15, 2024
97bed82
remove unused imports
casiojapi Feb 15, 2024
643fc50
remove unnecessary log
casiojapi Feb 15, 2024
c2d4657
change return to default due to sonarcloud complain
casiojapi Feb 15, 2024
6343952
refactor conditionals
casiojapi Feb 15, 2024
10ca754
check for null porinters
casiojapi Feb 15, 2024
5ad489c
throw unsupported exception
casiojapi Feb 15, 2024
a3d4b66
Adding unit tests for the snapsynch classes
fmacleal Mar 1, 2024
bca0b7f
Addition of more tests for the messages
fmacleal Mar 1, 2024
0119b85
Addition of more tests for the messages
fmacleal Mar 5, 2024
bab3cec
Removing assertJ for now before present it to the team
fmacleal Mar 5, 2024
35af0b8
Adding SnapSyncStateTest
fmacleal Mar 7, 2024
fb4f261
Adding CC headers to the files
fmacleal Mar 7, 2024
49ad04f
Merge pull request #2260 from rsksmart/add-snapshot-unit-tests
Vovchyk Mar 8, 2024
40a23f6
Merge pull request #2256 from rsksmart/fix-snapshot-unit-tests
Vovchyk Mar 8, 2024
811f7ca
Merge branch 'master' into snap-v4-final
Vovchyk Mar 8, 2024
8efef40
Fixing sonar issues and adding unit test
asoto-iov Mar 4, 2024
f6f1760
Merge pull request #2269 from rsksmart/update_trieDTO_and_add_unit_test
Vovchyk Mar 19, 2024
c1a4158
Fixing sonar issues
asoto-iov Mar 19, 2024
189e4e2
Increasing unit tests
asoto-iov Mar 19, 2024
ab921c1
Merge pull request #2270 from rsksmart/fixing-sonar-issues-and-adding…
Vovchyk Mar 20, 2024
c0ea9ee
Fixing sonar issue and adding unit test
asoto-iov Mar 20, 2024
2c4ac29
Merge pull request #2271 from rsksmart/fixing-sonar-issues-and-adding…
Vovchyk Mar 20, 2024
88156f2
Adding some tests for SnapshotProcessor class
fmacleal Mar 20, 2024
cecbfea
Adding copyright headers for missing classes
fmacleal Mar 20, 2024
36e5af6
Merge pull request #2272 from rsksmart/improving-sonar-coverage
Vovchyk Mar 20, 2024
fdda355
Solving some CodeQL issues from branch
fmacleal Mar 21, 2024
0b0fb1e
Merge pull request #2274 from rsksmart/solving-codeql-issues
fmacleal Mar 21, 2024
4abf4d0
Removing unused import
fmacleal Mar 21, 2024
6cfb658
Merge pull request #2275 from rsksmart/fixing-unused-import-sonar-issue
fmacleal Mar 21, 2024
462bb03
Adding skeleton for integration tests for SnapshotSynch
fmacleal Apr 9, 2024
0200472
Refactored the NodeIntegrationTestCommandLine
fmacleal Apr 11, 2024
cc90ac8
[WIP] Refactored code to make nodes run on background
fmacleal Apr 15, 2024
7c170cf
[WIP] Refactored the command line in order to re-use it
fmacleal Apr 22, 2024
7fc7fb9
Finished the integration test for the snapshot sync
fmacleal Apr 24, 2024
ced6887
Adding block correlation check during snapshot sync
asoto-iov Apr 25, 2024
7fc4134
Updating tryStartSyncing condition
asoto-iov Apr 26, 2024
32c8721
Updating tests, fixing Difficulty handling and addresing PR comments
asoto-iov Apr 29, 2024
2efc451
Adding more data to unit test
asoto-iov May 2, 2024
0efea9f
Applied some suggestions from the review
fmacleal May 3, 2024
75ab715
Adding more resilience for the test to avoid infinite loop
fmacleal May 7, 2024
26e0f0f
Update rskj-core/src/integrationTest/java/co/rsk/cli/tools/CliToolsIn…
fmacleal May 9, 2024
f7e6c88
Fixing the failing test, due a deletion of resources
fmacleal May 9, 2024
9b73a1e
Adding copyright headers
fmacleal May 9, 2024
f9c53a2
Merge pull request #2285 from rsksmart/add-integration-tests-to-snapsync
fmacleal May 10, 2024
650dfe1
addressing PR improvements
asoto-iov May 13, 2024
fcb5bb2
Merge pull request #2301 from rsksmart/block_validation_snapshotsync
Vovchyk May 21, 2024
7b17941
Merge branch 'master' into snap-v4-final
Vovchyk May 22, 2024
276f711
Merge remote-tracking branch 'origin/master' into master-snap-v4-fina…
Vovchyk May 29, 2024
1d17242
Merge pull request #2414 from rsksmart/master-snap-v4-final-merge
Vovchyk May 29, 2024
d3c9d8b
Cleaned some stuff up
Vovchyk May 30, 2024
daeed58
chore: cleanup; add missing logger to logback.xml
Vovchyk May 29, 2024
b0454e5
Update getCode so it does not make a copy of the array
rmoreliovlabs May 24, 2024
e253b93
Merge pull request #2424 from rsksmart/vovchyk/snap/cleanup
Vovchyk May 30, 2024
7542abb
Merge branch 'master' into snap-v4-final
Vovchyk May 30, 2024
9c0852a
chore(snap): refactor PeerAndModeDecidingSyncState and SnapshotSyncIn…
Vovchyk Jun 5, 2024
1f7bdff
Merge pull request #2448 from rsksmart/vovchyk/snap/ref1
Vovchyk Jun 5, 2024
ad8985c
Configure the priority for the message types for Snapshot sync
rmoreliovlabs Jun 24, 2024
30d364c
Fixing flakiness in test
rmoreliovlabs Jun 25, 2024
e49b761
Adding and refactoring missing snap messages types and fixing test
rmoreliovlabs Jun 25, 2024
86aaae5
Merge branch 'master' into snap-v4-final
Vovchyk Jun 26, 2024
4199519
Renaming test
rmoreliovlabs Jun 27, 2024
9a3309d
Removing Optional and adding null condition
rmoreliovlabs Jun 27, 2024
3fa3a1f
chore(snap): refactor code to use blocking queues with threads for SN…
Vovchyk Jun 18, 2024
6042eea
chore(snap): add unit tests; fix sonar issues
Vovchyk Jun 19, 2024
4263972
chore(snap): code refactoring
Vovchyk Jun 27, 2024
f3b8875
Merge pull request #2500 from rsksmart/vovchyk/snap/ref2
Vovchyk Jul 1, 2024
172b1cf
Merge pull request #2519 from rsksmart/configure_priority_for_snap_me…
Vovchyk Jul 1, 2024
4df8f18
Merge branch 'master' into snap-v4-final
Vovchyk Jul 8, 2024
5cf6b27
Add in config a list of snap boot nodes to sync against
rmoreliovlabs Jun 17, 2024
bbac14a
Adding Snapshot setup and tests
rmoreliovlabs Jun 18, 2024
0c27798
Addressing comments
rmoreliovlabs Jun 20, 2024
7a15a4a
Removing repeated code in setupTopBestSnapshotScenario and setupTopBe…
rmoreliovlabs Jun 27, 2024
8c8d72e
Addressing comments
rmoreliovlabs Jul 8, 2024
1b43acf
addressing more comments
rmoreliovlabs Jul 9, 2024
40eaf51
Fixing integration test
rmoreliovlabs Jul 9, 2024
6d62e79
Merge pull request #2504 from rsksmart/config_list_of_snapboot_nodes
Vovchyk Jul 11, 2024
ecdb18b
Merge branch 'master' into snap-v4-final
Vovchyk Jul 18, 2024
2dffc6a
Adding SNAP capability
rmoreliovlabs Jul 22, 2024
274973a
Addressing comments
rmoreliovlabs Jul 28, 2024
5d5ad0a
Ensure RSK is present for handshake completion
rmoreliovlabs Jul 29, 2024
89df007
Enable snap syncing with RSK cli args
rmoreliovlabs Jul 29, 2024
2f81c28
removing unnecessary print statement
rmoreliovlabs Jul 30, 2024
400c58e
Addressing comments
rmoreliovlabs Aug 1, 2024
464c53a
Updating logged info and small refactor
asoto-iov Jul 25, 2024
94a9b8e
Refactor SNAP capability to use version 1
rmoreliovlabs Aug 2, 2024
46d1f85
Adding on snap block error behaviour
asoto-iov Aug 2, 2024
35be9d5
Add isSnapCapable() method to Peer interface and filter snap-capable …
rmoreliovlabs Aug 4, 2024
c29212a
addressing comments
rmoreliovlabs Aug 5, 2024
11e2b73
Adding cc to SyncMode
rmoreliovlabs Aug 5, 2024
2904f13
fixing int test
asoto-iov Aug 6, 2024
a565ccc
Fixing integration test and adding some basic unit tests
asoto-iov Aug 7, 2024
2d59ae7
Merge pull request #2653 from rsksmart/feature/add_snap_syncing_in_cl…
Vovchyk Aug 8, 2024
b22c903
Addressing comments
rmoreliovlabs Aug 13, 2024
dfa69b9
prioritize snap-capable peers in getBestSnapPeerCandidates() method
rmoreliovlabs Aug 16, 2024
4460e83
Apply suggestions from code review
fmacleal Aug 20, 2024
1595bcd
Merge branch 'master' into snap-v4-final
Aug 20, 2024
80483ff
Merge pull request #2628 from rsksmart/feature/SNAP_capability_discovery
fmacleal Aug 21, 2024
8fc7aa0
Merge pull request #2666 from rsksmart/add_on_error_handler_for_snap_…
fmacleal Aug 21, 2024
c36348f
Adding SNAP capability if the client or the server are enabled
rmoreliovlabs Aug 22, 2024
a3c6885
Merge pull request #2695 from rsksmart/adding-snap-capability-if-clie…
rmoreliovlabs Aug 23, 2024
bde288f
Merge remote-tracking branch 'origin/master' into snap-v4-final
rmoreliovlabs Aug 30, 2024
8a81c4f
Merge remote-tracking branch 'origin/master' into vovchyk/master-snap…
Vovchyk Oct 1, 2024
b756360
Merge pull request #2779 from rsksmart/vovchyk/master-snap-v4-final-m…
Vovchyk Oct 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.snapshotsync;

import co.rsk.util.FilesHelper;
import co.rsk.util.OkHttpClientTestFixture;
import co.rsk.util.RskjConfigurationFileFixture;
import co.rsk.util.ThreadTimerHelper;
import co.rsk.util.cli.ConnectBlocksCommandLine;
import co.rsk.util.cli.NodeIntegrationTestCommandLine;
import co.rsk.util.cli.RskjCommandLineBase;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static co.rsk.util.FilesHelper.readBytesFromFile;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SnapshotSyncIntegrationTest {
private static final int TEN_MINUTES_IN_MILLISECONDS = 600000;
private static final String TAG_TO_REPLACE_SERVER_RPC_PORT = "<SERVER_NODE_RPC_PORT>";
private static final String TAG_TO_REPLACE_SERVER_PORT = "<SERVER_NODE_PORT>";
private static final String TAG_TO_REPLACE_SERVER_DATABASE_PATH = "<SERVER_NODE_DATABASE_PATH>";
private static final String TAG_TO_REPLACE_NODE_ID = "<SERVER_NODE_ID>";
private static final String TAG_TO_REPLACE_CLIENT_DATABASE_PATH = "<CLIENT_NODE_DATABASE_PATH>";
private static final String TAG_TO_REPLACE_CLIENT_PORT = "<CLIENT_PORT>";
private static final String TAG_TO_REPLACE_CLIENT_RPC_HTTP_PORT = "<CLIENT_RPC_HTTP_PORT>";
private static final String TAG_TO_REPLACE_CLIENT_RPC_WS_PORT = "<CLIENT_RPC_WS_PORT>";

private static final String RSKJ_SERVER_CONF_FILE_NAME = "snap-sync-server-rskj.conf";
private static final String RSKJ_CLIENT_CONF_FILE_NAME = "snap-sync-client-rskj.conf";

private final int portServer = 50555;
private final int portServerRpc = portServer + 1;
private final int portClient = portServerRpc + 1;
private final int portClientRpc = portClient + 1;

@TempDir
public Path tempDirectory;

private NodeIntegrationTestCommandLine serverNode;
private NodeIntegrationTestCommandLine clientNode;

@AfterEach
void tearDown() throws InterruptedException {
for (NodeIntegrationTestCommandLine node : Stream.of(clientNode, serverNode).filter(Objects::nonNull).collect(Collectors.toList())) {
node.killNode();
}
}

@Test
public void whenStartTheServerAndClientNodes_thenTheClientWillSynchWithServer() throws IOException, InterruptedException {
//given
Path serverDbDir = tempDirectory.resolve("server/database");
Path clientDbDir = tempDirectory.resolve("client/database");

importTheExportedBlocksToRegtestNode(serverDbDir);
String rskConfFileChangedServer = configureServerWithGeneratedInformation(serverDbDir);
serverNode = new NodeIntegrationTestCommandLine(rskConfFileChangedServer, "--regtest");

serverNode.startNode();
ThreadTimerHelper.waitForSeconds(30);

JsonNode serverBestBlockResponse = OkHttpClientTestFixture.getJsonResponseForGetBestBlockMessage(portServerRpc, "latest");
String serverBestBlockNumber = serverBestBlockResponse.get(0).get("result").get("number").asText();

//when
String rskConfFileChangedClient = configureClientConfWithGeneratedInformation(serverDbDir, clientDbDir.toString());
clientNode = new NodeIntegrationTestCommandLine(rskConfFileChangedClient, "--regtest");
clientNode.startNode();

//then
long startTime = System.currentTimeMillis();
long endTime = startTime + TEN_MINUTES_IN_MILLISECONDS;
boolean isClientSynced = false;

while (System.currentTimeMillis() < endTime) {
if (clientNode.getOutput().contains("CLIENT - Starting Snapshot sync.") && clientNode.getOutput().contains("CLIENT - Snapshot sync finished successfully!")) {
try {
JsonNode jsonResponse = OkHttpClientTestFixture.getJsonResponseForGetBestBlockMessage(portClientRpc, serverBestBlockNumber);
JsonNode jsonResult = jsonResponse.get(0).get("result");
if (jsonResult.isObject()) {
String bestBlockNumber = jsonResult.get("number").asText();
if (bestBlockNumber.equals(serverBestBlockNumber)) { // We reached the tip of the test database imported on server on the client
isClientSynced = true;
break;
}
}
} catch (Exception e) {
System.out.println("Error while trying to get the best block number from the client: " + e.getMessage());
System.out.println("We will try again in 10 seconds.");
}
}
ThreadTimerHelper.waitForSeconds(2);
}

assertTrue(isClientSynced);
}

private void importTheExportedBlocksToRegtestNode(Path dbDir) throws IOException, InterruptedException {
String exportedBlocksCsv = FilesHelper.getIntegrationTestResourcesFullPath("server_blocks.csv");
RskjCommandLineBase rskjCommandLineBase = new ConnectBlocksCommandLine(exportedBlocksCsv, dbDir);
rskjCommandLineBase.executeCommand();
}

private String configureServerWithGeneratedInformation(Path tempDirDatabaseServerPath) throws IOException {
String originRskConfFileServer = FilesHelper.getAbsolutPathFromResourceFile(getClass(), RSKJ_SERVER_CONF_FILE_NAME);
Path rskConfFileServer = tempDirectory.resolve("server/" + RSKJ_SERVER_CONF_FILE_NAME);
rskConfFileServer.getParent().toFile().mkdirs();
Files.copy(Paths.get(originRskConfFileServer), rskConfFileServer);

List<Pair<String, String>> tagsWithValues = new ArrayList<>();
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_SERVER_DATABASE_PATH, tempDirDatabaseServerPath.toString()));
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_SERVER_PORT, String.valueOf(portServer)));
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_SERVER_RPC_PORT, String.valueOf(portServerRpc)));

RskjConfigurationFileFixture.substituteTagsOnRskjConfFile(rskConfFileServer.toString(), tagsWithValues);

return rskConfFileServer.toString();
}

private String configureClientConfWithGeneratedInformation(Path tempDirDatabaseServerPath, String tempDirDatabasePath) throws IOException {
String nodeId = readServerNodeId(tempDirDatabaseServerPath);
String originRskConfFileClient = FilesHelper.getAbsolutPathFromResourceFile(getClass(), RSKJ_CLIENT_CONF_FILE_NAME);
Path rskConfFileClient = tempDirectory.resolve("client/" + RSKJ_CLIENT_CONF_FILE_NAME);
rskConfFileClient.getParent().toFile().mkdirs();
Files.copy(Paths.get(originRskConfFileClient), rskConfFileClient);

List<Pair<String, String>> tagsWithValues = new ArrayList<>();
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_NODE_ID, nodeId));
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_SERVER_PORT, String.valueOf(portServer)));
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_CLIENT_PORT, String.valueOf(portClient)));
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_CLIENT_RPC_HTTP_PORT, String.valueOf(portClientRpc)));
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_CLIENT_RPC_WS_PORT, String.valueOf(portClient + 2)));
tagsWithValues.add(new ImmutablePair<>(TAG_TO_REPLACE_CLIENT_DATABASE_PATH, tempDirDatabasePath));

RskjConfigurationFileFixture.substituteTagsOnRskjConfFile(rskConfFileClient.toString(), tagsWithValues);

return rskConfFileClient.toString();
}

private String readServerNodeId(Path serverDatabasePath) throws IOException {
byte[] fileBytes = readBytesFromFile(String.format("%s/nodeId.properties", serverDatabasePath));
String fileContent = new String(fileBytes, StandardCharsets.UTF_8);
return StringUtils.substringAfter(fileContent, "nodeId=").trim();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

public class OkHttpClientTestFixture {

public static final String GET_BEST_BLOCK_CONTENT = "[{\n" +
public static final String GET_BLOCK_CONTENT = "[{\n" +
" \"method\": \"eth_getBlockByNumber\",\n" +
" \"params\": [\n" +
" \"latest\",\n" +
" \"<BLOCK_NUM_OR_TAG>\",\n" +
" true\n" +
" ],\n" +
" \"id\": 1,\n" +
Expand Down Expand Up @@ -94,11 +94,15 @@ public static Response sendJsonRpcMessage(String content, int port) throws IOExc
}

public static Response sendJsonRpcGetBestBlockMessage(int port) throws IOException {
return sendJsonRpcMessage(GET_BEST_BLOCK_CONTENT, port);
return sendJsonRpcGetBlockMessage(port, "latest");
}

public static JsonNode getJsonResponseForGetBestBlockMessage(int port) throws IOException {
Response response = sendJsonRpcGetBestBlockMessage(port);
public static Response sendJsonRpcGetBlockMessage(int port, String blockNumOrTag) throws IOException {
return sendJsonRpcMessage(GET_BLOCK_CONTENT.replace("<BLOCK_NUM_OR_TAG>", blockNumOrTag), port);
}

public static JsonNode getJsonResponseForGetBestBlockMessage(int port, String blockNumOrTag) throws IOException {
Response response = sendJsonRpcGetBlockMessage(port, blockNumOrTag);
return new ObjectMapper().readTree(response.body().string());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,29 @@
package co.rsk.util.cli;

import java.io.IOException;
import java.nio.file.Path;

public class ConnectBlocksCommandLine extends RskjCommandLineBase {

public ConnectBlocksCommandLine(String filePath){
super ("co.rsk.cli.tools.ConnectBlocks",
new String[]{},
new String[]{"-f", filePath, "--regtest"});
public ConnectBlocksCommandLine(String filePath) {
this(filePath, null);
}

public ConnectBlocksCommandLine(String filePath, Path dbDir) {
super("co.rsk.cli.tools.ConnectBlocks",
new String[]{ "-Dlogging.dir=./build/tmp" },
makeArgs(filePath, dbDir));
}

@Override
public Process executeCommand() throws IOException, InterruptedException {
return super.executeCommand(10);
}

private static String[] makeArgs(String filePath, Path dbDir) {
if (dbDir == null) {
return new String[]{"-f", filePath, "--regtest"};
}
return new String[]{"-f", filePath, "-Xdatabase.dir=" + dbDir, "--regtest"};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void appendToCommandIfArrayNotEmpty(StringBuilder command, String[] arra
command.append(" ");
}

private void appendLinesToProcessOutput(String output){
private synchronized void appendLinesToProcessOutput(String output){
processOutputBuilder.append(output).append(System.lineSeparator());
}

Expand Down Expand Up @@ -112,7 +112,7 @@ public Process executeCommand(int timeout) throws IOException, InterruptedExcept
return cliProcess; // We return the process so the test can use it to waitFor, to kill, to add in a Future operation
}

public String getOutput() {
public synchronized String getOutput() {
return processOutputBuilder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.util.cli;

import co.rsk.util.FilesHelper;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertTrue;

class RskjNodeCommandLineTest {

@Test
void whenExecuteCommandConnectBlocks_thenOutputWithSuccess() throws IOException, InterruptedException {
//given
String filePath = FilesHelper.getAbsolutPathFromResourceFile(getClass(), "server_blocks.csv");
//when
RskjCommandLineBase rskjCommandLineBase = new ConnectBlocksCommandLine(filePath);
Process cliProcess = rskjCommandLineBase.executeCommand();
String output = rskjCommandLineBase.getOutput();
//then
assertTrue(output.contains("ConnectBlocks finished"));
cliProcess.destroyForcibly();
}
}
Loading
Loading