|
| 1 | +package org.xrpl.xrpl4j.tests.environment; |
| 2 | + |
| 3 | +/*- |
| 4 | + * ========================LICENSE_START================================= |
| 5 | + * xrpl4j :: integration-tests |
| 6 | + * %% |
| 7 | + * Copyright (C) 2020 - 2022 XRPL Foundation and its contributors |
| 8 | + * %% |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * =========================LICENSE_END================================== |
| 21 | + */ |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.slf4j.LoggerFactory.getLogger; |
| 25 | + |
| 26 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 27 | +import com.google.common.base.Preconditions; |
| 28 | +import okhttp3.HttpUrl; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.xrpl.xrpl4j.client.JsonRpcClientErrorException; |
| 31 | +import org.xrpl.xrpl4j.client.XrplAdminClient; |
| 32 | +import org.xrpl.xrpl4j.client.XrplClient; |
| 33 | +import org.xrpl.xrpl4j.crypto.keys.Base58EncodedSecret; |
| 34 | +import org.xrpl.xrpl4j.crypto.keys.KeyPair; |
| 35 | +import org.xrpl.xrpl4j.crypto.keys.PrivateKey; |
| 36 | +import org.xrpl.xrpl4j.crypto.keys.Seed; |
| 37 | +import org.xrpl.xrpl4j.crypto.signing.SignatureService; |
| 38 | +import org.xrpl.xrpl4j.crypto.signing.SingleSignedTransaction; |
| 39 | +import org.xrpl.xrpl4j.crypto.signing.bc.BcSignatureService; |
| 40 | +import org.xrpl.xrpl4j.model.client.accounts.AccountInfoRequestParams; |
| 41 | +import org.xrpl.xrpl4j.model.client.accounts.AccountInfoResult; |
| 42 | +import org.xrpl.xrpl4j.model.client.admin.AcceptLedgerResult; |
| 43 | +import org.xrpl.xrpl4j.model.client.fees.FeeResult; |
| 44 | +import org.xrpl.xrpl4j.model.client.fees.FeeUtils; |
| 45 | +import org.xrpl.xrpl4j.model.client.transactions.SubmitResult; |
| 46 | +import org.xrpl.xrpl4j.model.transactions.Address; |
| 47 | +import org.xrpl.xrpl4j.model.transactions.Payment; |
| 48 | +import org.xrpl.xrpl4j.model.transactions.TransactionResultCodes; |
| 49 | +import org.xrpl.xrpl4j.model.transactions.XrpCurrencyAmount; |
| 50 | + |
| 51 | +import java.math.BigDecimal; |
| 52 | +import java.time.Duration; |
| 53 | +import java.util.Objects; |
| 54 | +import java.util.concurrent.Executors; |
| 55 | +import java.util.concurrent.ScheduledExecutorService; |
| 56 | +import java.util.concurrent.TimeUnit; |
| 57 | + |
| 58 | +/** |
| 59 | + * Environment that connects to a locally running rippled node in standalone mode. |
| 60 | + */ |
| 61 | +public class LocalRippledEnvironmentForConfidentialMPT implements XrplEnvironment { |
| 62 | + |
| 63 | + private static final Logger LOGGER = getLogger(LocalRippledEnvironmentForConfidentialMPT.class); |
| 64 | + |
| 65 | + /** |
| 66 | + * The URL of the locally running rippled node. |
| 67 | + */ |
| 68 | + private static final HttpUrl LOCAL_RIPPLED_URL = HttpUrl.parse("http://127.0.0.1:51234/"); |
| 69 | + |
| 70 | + /** |
| 71 | + * Seed for the Master/Root wallet in the rippled standalone mode. |
| 72 | + */ |
| 73 | + private static final String MASTER_WALLET_SEED = "snoPBrXtMeMyMHUVTgbuqAfg1SUTb"; |
| 74 | + |
| 75 | + private static ScheduledExecutorService ledgerAcceptor; |
| 76 | + |
| 77 | + private final XrplClient xrplClient; |
| 78 | + private final XrplAdminClient xrplAdminClient; |
| 79 | + private final SignatureService<PrivateKey> signatureService = new BcSignatureService(); |
| 80 | + |
| 81 | + /** |
| 82 | + * No-args constructor that initializes the connection to the local rippled node. |
| 83 | + */ |
| 84 | + public LocalRippledEnvironmentForConfidentialMPT() { |
| 85 | + this.xrplClient = new XrplClient(LOCAL_RIPPLED_URL); |
| 86 | + this.xrplAdminClient = new XrplAdminClient(LOCAL_RIPPLED_URL); |
| 87 | + // Start the ledger acceptor with a default interval |
| 88 | + this.startLedgerAcceptor(Duration.ofMillis(1000)); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the {@link KeyPair} of the master account. |
| 93 | + * |
| 94 | + * @return The {@link KeyPair} of the master account. |
| 95 | + */ |
| 96 | + public static KeyPair getMasterKeyPair() { |
| 97 | + return Seed.fromBase58EncodedSecret(Base58EncodedSecret.of(MASTER_WALLET_SEED)).deriveKeyPair(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public XrplClient getXrplClient() { |
| 102 | + return xrplClient; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void fundAccount(Address classicAddress) { |
| 107 | + // accounts are funded from the genesis account that holds all XRP when the ledger starts. |
| 108 | + try { |
| 109 | + sendPayment( |
| 110 | + getMasterKeyPair(), |
| 111 | + classicAddress, |
| 112 | + XrpCurrencyAmount.ofXrp(BigDecimal.valueOf(1000)) |
| 113 | + ); |
| 114 | + } catch (JsonRpcClientErrorException | JsonProcessingException e) { |
| 115 | + throw new RuntimeException("could not fund account", e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + protected AccountInfoResult getCurrentAccountInfo(Address classicAddress) { |
| 120 | + try { |
| 121 | + AccountInfoRequestParams params = AccountInfoRequestParams.of(classicAddress); |
| 122 | + return getXrplClient().accountInfo(params); |
| 123 | + } catch (Exception e) { |
| 124 | + throw new RuntimeException(e.getMessage(), e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + protected void sendPayment(KeyPair sourceKeyPair, Address destinationAddress, XrpCurrencyAmount paymentAmount) |
| 129 | + throws JsonRpcClientErrorException, JsonProcessingException { |
| 130 | + FeeResult feeResult = getXrplClient().fee(); |
| 131 | + AccountInfoResult accountInfo = this.getCurrentAccountInfo(sourceKeyPair.publicKey().deriveAddress()); |
| 132 | + Payment payment = Payment.builder() |
| 133 | + .account(sourceKeyPair.publicKey().deriveAddress()) |
| 134 | + .fee(FeeUtils.computeNetworkFees(feeResult).recommendedFee()) |
| 135 | + .sequence(accountInfo.accountData().sequence()) |
| 136 | + .destination(destinationAddress) |
| 137 | + .amount(paymentAmount) |
| 138 | + .signingPublicKey(sourceKeyPair.publicKey()) |
| 139 | + .build(); |
| 140 | + |
| 141 | + SingleSignedTransaction<Payment> signedPayment = signatureService.sign(sourceKeyPair.privateKey(), payment); |
| 142 | + SubmitResult<Payment> result = getXrplClient().submit(signedPayment); |
| 143 | + assertThat(result.engineResult()).isEqualTo(TransactionResultCodes.TES_SUCCESS); |
| 144 | + LOGGER.info("Payment successful: " + LOCAL_RIPPLED_URL + |
| 145 | + result.transactionResult().transaction()); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Method to accept next ledger ad hoc. |
| 150 | + */ |
| 151 | + @Override |
| 152 | + public void acceptLedger() { |
| 153 | + try { |
| 154 | + AcceptLedgerResult status = xrplAdminClient.acceptLedger(); |
| 155 | + LOGGER.info("Accepted ledger status: {}", status); |
| 156 | + } catch (RuntimeException | JsonRpcClientErrorException e) { |
| 157 | + LOGGER.warn("Ledger accept failed", e); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void startLedgerAcceptor(Duration acceptIntervalMillis) { |
| 163 | + Objects.requireNonNull(acceptIntervalMillis, "acceptIntervalMillis must not be null"); |
| 164 | + Preconditions.checkArgument(acceptIntervalMillis.toMillis() > 0, "acceptIntervalMillis must be greater than 0"); |
| 165 | + |
| 166 | + // Stop any existing ledger acceptor |
| 167 | + if (ledgerAcceptor != null && !ledgerAcceptor.isShutdown()) { |
| 168 | + stopLedgerAcceptor(); |
| 169 | + } |
| 170 | + |
| 171 | + // rippled is run in standalone mode which means that ledgers won't automatically close. You have to manually |
| 172 | + // advance the ledger using the "ledger_accept" method on the admin API. To mimic the behavior of a networked |
| 173 | + // rippled, run a scheduled task to trigger the "ledger_accept" method. |
| 174 | + ledgerAcceptor = Executors.newScheduledThreadPool(1); |
| 175 | + ledgerAcceptor.scheduleAtFixedRate(this::acceptLedger, |
| 176 | + acceptIntervalMillis.toMillis(), |
| 177 | + acceptIntervalMillis.toMillis(), |
| 178 | + TimeUnit.MILLISECONDS |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void stopLedgerAcceptor() { |
| 184 | + if (ledgerAcceptor != null) { |
| 185 | + try { |
| 186 | + ledgerAcceptor.shutdown(); |
| 187 | + ledgerAcceptor.awaitTermination(5, TimeUnit.SECONDS); |
| 188 | + } catch (InterruptedException e) { |
| 189 | + throw new RuntimeException("Unable to stop ledger acceptor", e); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments