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

feat(snap): adjust snap config #2797

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -16,14 +16,13 @@
* 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 pte;
package co.rsk.pte;

import co.rsk.util.OkHttpClientTestFixture;
import co.rsk.util.cli.CommandLineFixture;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.squareup.okhttp.Response;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -33,11 +32,15 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Stream;

import static co.rsk.util.OkHttpClientTestFixture.*;
import static co.rsk.util.OkHttpClientTestFixture.ETH_GET_BLOCK_BY_NUMBER;
import static co.rsk.util.OkHttpClientTestFixture.FromToAddressPair.of;
import static co.rsk.util.OkHttpClientTestFixture.getEnvelopedMethodCalls;

public class PteIntegrationTest {

Expand Down Expand Up @@ -86,24 +89,8 @@ public void setup() throws IOException {

@Test
void whenParallelizableTransactionsAreSent_someAreExecutedInParallel() throws Exception {

// Given

// Pre-funded Test Accounts on Regtest
List<String> accounts = Arrays.asList(
"0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826",
"0x7986b3df570230288501eea3d890bd66948c9b79",
"0x0a3aa774752ec2042c46548456c094a76c7f3a79",
"0xcf7cdbbb5f7ba79d3ffe74a0bba13fc0295f6036",
"0x39b12c05e8503356e3a7df0b7b33efa4c054c409",
"0xc354d97642faa06781b76ffb6786f72cd7746c97",
"0xdebe71e1de41fc77c44df4b6db940026e31b0e71",
"0x7857288e171c6159c5576d1bd9ac40c0c48a771c",
"0xa4dea4d5c954f5fd9e87f0e9752911e83a3d18b3",
"0x09a1eda29f664ac8f68106f6567276df0c65d859",
"0xec4ddeb4380ad69b3e509baad9f158cdf4e4681d"
);

Map<String, Response> txResponseMap = new HashMap<>();
Map<String, Map<Integer, String>> blocksResponseMap = new HashMap<>();

Expand All @@ -125,9 +112,13 @@ void whenParallelizableTransactionsAreSent_someAreExecutedInParallel() throws Ex

// Send bulk transactions

Response txResponse = sendBulkTransactions(
accounts.get(0), accounts.get(1), accounts.get(2), accounts.get(3),
accounts.get(4), accounts.get(5), accounts.get(6), accounts.get(7));
List<String> accounts = OkHttpClientTestFixture.PRE_FUNDED_ACCOUNTS;
Response txResponse = OkHttpClientTestFixture.sendBulkTransactions(
RPC_PORT,
of(accounts.get(0), accounts.get(1)),
of(accounts.get(2), accounts.get(3)),
of(accounts.get(4), accounts.get(5)),
of(accounts.get(6), accounts.get(7)));

txResponseMap.put("bulkTransactionsResponse", txResponse);

Expand Down Expand Up @@ -185,39 +176,6 @@ private Response getBlockByNumber(String number) throws IOException {
number)
);

System.out.println(content);

return OkHttpClientTestFixture.sendJsonRpcMessage(content, RPC_PORT);
}

private Response sendBulkTransactions(
String addressFrom1, String addressTo1,
String addressFrom2, String addressTo2,
String addressFrom3, String addressTo3,
String addressFrom4, String addressTo4) throws IOException {

String gas = "0x9C40";
String gasPrice = "0x10";
String value = "0x500";

String[] placeholders = new String[]{
"<ADDRESS_FROM>", "<ADDRESS_TO>", "<GAS>",
"<GAS_PRICE>", "<VALUE>"
};

String content = getEnvelopedMethodCalls(
StringUtils.replaceEach(ETH_SEND_TRANSACTION, placeholders,
new String[]{addressFrom1, addressTo1, gas, gasPrice, value}),
StringUtils.replaceEach(ETH_SEND_TRANSACTION, placeholders,
new String[]{addressFrom2, addressTo2, gas, gasPrice, value}),
StringUtils.replaceEach(ETH_SEND_TRANSACTION, placeholders,
new String[]{addressFrom3, addressTo3, gas, gasPrice, value}),
StringUtils.replaceEach(ETH_SEND_TRANSACTION, placeholders,
new String[]{addressFrom4, addressTo4, gas, gasPrice, value})
);

System.out.println(content);

return OkHttpClientTestFixture.sendJsonRpcMessage(content, RPC_PORT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
* 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;
package co.rsk.snap;

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.*;
import co.rsk.util.cli.NodeIntegrationTestCommandLine;
import co.rsk.util.cli.RskjCommandLineBase;
import com.fasterxml.jackson.databind.JsonNode;
import com.squareup.okhttp.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -38,11 +34,16 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

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

public class SnapshotSyncIntegrationTest {
Expand Down Expand Up @@ -83,15 +84,15 @@ public void whenStartTheServerAndClientNodes_thenTheClientWillSynchWithServer()
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);
ThreadTimerHelper.waitForSeconds(20);
generateBlocks();

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

//when
String rskConfFileChangedClient = configureClientConfWithGeneratedInformation(serverDbDir, clientDbDir.toString());
Expand Down Expand Up @@ -126,12 +127,6 @@ public void whenStartTheServerAndClientNodes_thenTheClientWillSynchWithServer()
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);
Expand Down Expand Up @@ -173,4 +168,17 @@ private String readServerNodeId(Path serverDatabasePath) throws IOException {
String fileContent = new String(fileBytes, StandardCharsets.UTF_8);
return StringUtils.substringAfter(fileContent, "nodeId=").trim();
}
}

private void generateBlocks() throws IOException {
List<String> accounts = OkHttpClientTestFixture.PRE_FUNDED_ACCOUNTS;
Random rand = new Random(111);

for (int i = 0; i < 700; i++) {
OkHttpClientTestFixture.FromToAddressPair[] pairs = IntStream.range(0, 10)
.mapToObj(n -> of(accounts.get(rand.nextInt(accounts.size())), accounts.get(rand.nextInt(accounts.size()))))
.toArray(OkHttpClientTestFixture.FromToAddressPair[]::new);
Response response = OkHttpClientTestFixture.sendBulkTransactions(portServerRpc, pairs);
assertTrue(response.isSuccessful());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,33 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.squareup.okhttp.*;
import org.apache.commons.lang3.StringUtils;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Objects;

public class OkHttpClientTestFixture {

// Pre-funded Test Accounts on Regtest
public static final List<String> PRE_FUNDED_ACCOUNTS = List.of(
"0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826",
"0x7986b3df570230288501eea3d890bd66948c9b79",
"0x0a3aa774752ec2042c46548456c094a76c7f3a79",
"0xcf7cdbbb5f7ba79d3ffe74a0bba13fc0295f6036",
"0x39b12c05e8503356e3a7df0b7b33efa4c054c409",
"0xc354d97642faa06781b76ffb6786f72cd7746c97",
"0xdebe71e1de41fc77c44df4b6db940026e31b0e71",
"0x7857288e171c6159c5576d1bd9ac40c0c48a771c",
"0xa4dea4d5c954f5fd9e87f0e9752911e83a3d18b3",
"0x09a1eda29f664ac8f68106f6567276df0c65d859",
"0xec4ddeb4380ad69b3e509baad9f158cdf4e4681d"
);

public static final String GET_BLOCK_CONTENT = "[{\n" +
" \"method\": \"eth_getBlockByNumber\",\n" +
" \"params\": [\n" +
Expand Down Expand Up @@ -135,4 +153,40 @@ public static String getEnvelopedMethodCalls(String... methodCall) {
return "[\n" + String.join(",\n", methodCall) + "]";
}

public static Response sendBulkTransactions(int rpcPort, FromToAddressPair... fromToAddresses) throws IOException {
Objects.requireNonNull(fromToAddresses);

String gas = "0x9C40";
String gasPrice = "0x10";
String value = "0x500";

String[] placeholders = new String[]{
"<ADDRESS_FROM>", "<ADDRESS_TO>", "<GAS>",
"<GAS_PRICE>", "<VALUE>"
};

String[] methodCalls = new String[fromToAddresses.length];
for (int i = 0; i < fromToAddresses.length; i++) {
FromToAddressPair fromToPair = fromToAddresses[i];
methodCalls[i] = StringUtils.replaceEach(ETH_SEND_TRANSACTION, placeholders,
new String[]{fromToPair.from, fromToPair.to, gas, gasPrice, value});
}
String content = getEnvelopedMethodCalls(methodCalls);

return OkHttpClientTestFixture.sendJsonRpcMessage(content, rpcPort);
}

public static class FromToAddressPair {
private final String from;
private final String to;

private FromToAddressPair(String from, String to) {
this.from = from;
this.to = to;
}

public static FromToAddressPair of(String from, String to) {
return new FromToAddressPair(from, to);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Process startNode(Consumer<Process> beforeDestroyFn) throws IOException,
try {
executeCommand(timeout);
} finally {
if(timeout != 0) {
if (timeout != 0) {
killNode(beforeDestroyFn);
}
}
Expand All @@ -77,10 +77,18 @@ public int killNode(Consumer<Process> beforeDestroyFn) throws InterruptedExcepti
if (beforeDestroyFn != null) {
beforeDestroyFn.accept(cliProcess);
}
if(cliProcess.isAlive()){
cliProcess.destroyForcibly();

if (cliProcess.isAlive()) {
cliProcess.destroy();
// We have to wait a bit so the process finishes the kill command
cliProcess.waitFor(1, TimeUnit.MINUTES);

if (cliProcess.isAlive()) {
cliProcess.destroyForcibly();
cliProcess.waitFor(10, TimeUnit.SECONDS);
}
}
cliProcess.waitFor(1, TimeUnit.MINUTES); // We have to wait a bit so the process finishes the kill command

return cliProcess.exitValue();
}
}

This file was deleted.

Loading