Skip to content

Commit

Permalink
Slow down ledger accept
Browse files Browse the repository at this point in the history
  • Loading branch information
sappenin committed Nov 26, 2024
1 parent 6c7545e commit 502be9e
Showing 1 changed file with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,51 @@ public class RippledContainer {
// Seed for the Master/Root wallet in the rippled docker container.
public static final String MASTER_WALLET_SEED = "snoPBrXtMeMyMHUVTgbuqAfg1SUTb";
private static final Logger LOGGER = getLogger(RippledContainer.class);

/**
* Advances the ledger by one on each call.
*
* @see "https://xrpl.org/docs/references/http-websocket-apis/admin-api-methods/server-control-methods/ledger_accept"
*/
private static final Consumer<RippledContainer> LEDGER_ACCEPTOR = (rippledContainer) -> {
try {
AcceptLedgerResult status = rippledContainer.getXrplAdminClient()
.acceptLedger();
LOGGER.info("Accepted ledger status: {}", status);
AcceptLedgerResult status = rippledContainer.getXrplAdminClient().acceptLedger();
LOGGER.info("LEDGER_ACCEPTOR: Accepted ledger status: {}", status);
} catch (RuntimeException | JsonRpcClientErrorException e) {
LOGGER.warn("Ledger accept failed", e);
}
};

private void acceptCurrentLedger(RippledContainer rippledContainer) {
private void acceptCurrentLedger() {
try {
AcceptLedgerResult status = rippledContainer.getXrplAdminClient().acceptLedger();
LOGGER.info("Accepted ledger status: {}", status);
AcceptLedgerResult status = this.getXrplAdminClient().acceptLedger();
LOGGER.info("acceptCurrentLedger(): Accepted ledger status: {}", status);
} catch (RuntimeException | JsonRpcClientErrorException e) {
LOGGER.warn("Ledger accept failed", e);
}
}

private final GenericContainer<?> rippledContainer;
private final ScheduledExecutorService ledgerAcceptor;
private XrplAdminClient xrplAdminClient;
private boolean started;

/**
* No-args constructor.
*/
public RippledContainer() {
rippledContainer = new GenericContainer<>("rippleci/rippled:2.2.0")
.withCreateContainerCmdModifier((Consumer<CreateContainerCmd>) (cmd) ->
cmd.withEntrypoint("/opt/ripple/bin/rippled"))
.withCommand("-a --start --conf /config/rippled.cfg")
.withExposedPorts(5005)
.withClasspathResourceMapping("rippled",
"/config",
BindMode.READ_ONLY)
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Application starting.*"));
ledgerAcceptor = Executors.newScheduledThreadPool(1);
try (GenericContainer<?> container = new GenericContainer<>("rippleci/rippled:2.2.0")) {
this.rippledContainer = container.withCreateContainerCmdModifier((Consumer<CreateContainerCmd>) (cmd) ->
cmd.withEntrypoint("/opt/ripple/bin/rippled"))
.withCommand("-a --start --conf /config/rippled.cfg")
.withExposedPorts(5005)
.withClasspathResourceMapping("rippled",
"/config",
BindMode.READ_ONLY)
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*Application starting.*"));
}

this.ledgerAcceptor = Executors.newScheduledThreadPool(1);
}

/**
Expand All @@ -108,7 +116,7 @@ public static KeyPair getMasterKeyPair() {
* Starts container with default interval (1s) for closing ledgers.
*/
public RippledContainer start() {
return this.start(500);
return this.start(2000);
}

/**
Expand All @@ -134,6 +142,10 @@ public RippledContainer start(int acceptIntervalMillis) {
TimeUnit.MILLISECONDS
);
waitForLedgerTimeToSync();

// Re-used the same client for all admin requests
xrplAdminClient = new XrplAdminClient(this.getBaseUri());

return this;
}

Expand Down Expand Up @@ -182,7 +194,7 @@ private void assertContainerStarted() {
* @return A {@link XrplAdminClient}.
*/
public XrplAdminClient getXrplAdminClient() {
return new XrplAdminClient(this.getBaseUri());
return this.xrplAdminClient;
}

/**
Expand All @@ -208,6 +220,6 @@ public HttpUrl getBaseUri() {
*/
public void acceptLedger() {
assertContainerStarted();
this.acceptCurrentLedger(this);
this.acceptCurrentLedger();
}
}

0 comments on commit 502be9e

Please sign in to comment.