From 463ae5ba31cc92e993c8ccbd918a672c3c3f1948 Mon Sep 17 00:00:00 2001 From: Jamie Li Date: Fri, 24 Jan 2025 09:04:26 +0800 Subject: [PATCH] Extract LedgerApi from Horizon --- .../anchor/{horizon => ledger}/Horizon.java | 55 ++++++++- .../org/stellar/anchor/ledger/LedgerApi.java | 111 ++++++++++++++++++ .../org/stellar/anchor/ledger/StellarRpc.java | 84 +++++++++++++ .../stellar/anchor/sep10/Sep10Service.java | 25 ++-- .../anchor/{horizon => ledger}/HorizonTest.kt | 24 ++-- .../stellar/anchor/sep10/Sep10ServiceTest.kt | 41 +++---- .../component/custody/FireblocksBeans.java | 2 +- .../platform/PlatformServerBeans.java | 2 +- .../component/platform/RpcActionBeans.java | 2 +- .../platform/component/sep/SepBeans.java | 2 +- .../component/share/UtilityBeans.java | 2 +- .../fireblocks/FireblocksEventService.java | 2 +- .../platform/job/TrustlineCheckJob.java | 4 +- .../platform/rpc/DoStellarPaymentHandler.java | 12 +- .../NotifyOnchainFundsReceivedHandler.java | 2 +- .../rpc/NotifyOnchainFundsSentHandler.java | 2 +- .../fireblocks/FireblocksEventServiceTest.kt | 2 +- .../platform/job/TrustlineCheckJobTest.kt | 6 +- .../rpc/DoStellarPaymentHandlerTest.kt | 10 +- .../NotifyOnchainFundsReceivedHandlerTest.kt | 2 +- .../rpc/NotifyOnchainFundsSentHandlerTest.kt | 2 +- 21 files changed, 318 insertions(+), 76 deletions(-) rename core/src/main/java/org/stellar/anchor/{horizon => ledger}/Horizon.java (54%) create mode 100644 core/src/main/java/org/stellar/anchor/ledger/LedgerApi.java create mode 100644 core/src/main/java/org/stellar/anchor/ledger/StellarRpc.java rename core/src/test/kotlin/org/stellar/anchor/{horizon => ledger}/HorizonTest.kt (86%) diff --git a/core/src/main/java/org/stellar/anchor/horizon/Horizon.java b/core/src/main/java/org/stellar/anchor/ledger/Horizon.java similarity index 54% rename from core/src/main/java/org/stellar/anchor/horizon/Horizon.java rename to core/src/main/java/org/stellar/anchor/ledger/Horizon.java index d648264363..d8155be4f4 100644 --- a/core/src/main/java/org/stellar/anchor/horizon/Horizon.java +++ b/core/src/main/java/org/stellar/anchor/ledger/Horizon.java @@ -1,22 +1,25 @@ -package org.stellar.anchor.horizon; +package org.stellar.anchor.ledger; import static org.stellar.anchor.api.asset.AssetInfo.NATIVE_ASSET_CODE; import java.util.List; +import java.util.stream.Collectors; import lombok.Getter; import org.stellar.anchor.config.AppConfig; import org.stellar.anchor.util.AssetHelper; import org.stellar.sdk.AssetTypeCreditAlphaNum; import org.stellar.sdk.Server; +import org.stellar.sdk.Transaction; import org.stellar.sdk.TrustLineAsset; import org.stellar.sdk.exception.NetworkException; import org.stellar.sdk.requests.PaymentsRequestBuilder; import org.stellar.sdk.responses.AccountResponse; +import org.stellar.sdk.responses.TransactionResponse; import org.stellar.sdk.responses.operations.OperationResponse; import org.stellar.sdk.xdr.AssetType; /** The horizon-server. */ -public class Horizon { +public class Horizon implements LedgerApi { @Getter private final String horizonUrl; @Getter private final String stellarNetworkPassphrase; @@ -32,7 +35,7 @@ public Server getServer() { return this.horizonServer; } - public boolean isTrustlineConfigured(String account, String asset) throws NetworkException { + public boolean hasTrustline(String account, String asset) throws NetworkException { String assetCode = AssetHelper.getAssetCode(asset); if (NATIVE_ASSET_CODE.equals(assetCode)) { return true; @@ -56,6 +59,46 @@ public boolean isTrustlineConfigured(String account, String asset) throws Networ }); } + @Override + public Account getAccount(String account) throws NetworkException { + AccountResponse response = getServer().accounts().account(account); + AccountResponse.Thresholds thresholds = response.getThresholds(); + + return Account.builder() + .accountId(response.getAccountId()) + .sequenceNumber(response.getSequenceNumber()) + .thresholds( + LedgerApi.Thresholds.builder() + .lowThreshold(thresholds.getLowThreshold()) + .medThreshold(thresholds.getMedThreshold()) + .highThreshold(thresholds.getHighThreshold()) + .build()) + .balances( + response.getBalances().stream() + .map( + b -> + Balance.builder() + .assetType(b.getAssetType()) + .assetCode(b.getAssetCode()) + .assetIssuer(b.getAssetIssuer()) + .liquidityPoolId(b.getLiquidityPoolId()) + .limit(b.getLimit()) + .build()) + .collect(Collectors.toList())) + .signers( + response.getSigners().stream() + .map( + s -> + Signer.builder() + .key(s.getKey()) + .type(s.getType()) + .weight(s.getWeight()) + .sponsor(s.getSponsor()) + .build()) + .collect(Collectors.toList())) + .build(); + } + /** * Get payment operations for a transaction. * @@ -63,6 +106,7 @@ public boolean isTrustlineConfigured(String account, String asset) throws Networ * @return the operations * @throws NetworkException request failed, see {@link PaymentsRequestBuilder#execute()} */ + @Override public List getStellarTxnOperations(String stellarTxnId) { return getServer() .payments() @@ -71,4 +115,9 @@ public List getStellarTxnOperations(String stellarTxnId) { .execute() .getRecords(); } + + @Override + public TransactionResponse submitTransaction(Transaction transaction) throws NetworkException { + return getServer().submitTransaction(transaction, false); + } } diff --git a/core/src/main/java/org/stellar/anchor/ledger/LedgerApi.java b/core/src/main/java/org/stellar/anchor/ledger/LedgerApi.java new file mode 100644 index 0000000000..1d7c7cef41 --- /dev/null +++ b/core/src/main/java/org/stellar/anchor/ledger/LedgerApi.java @@ -0,0 +1,111 @@ +package org.stellar.anchor.ledger; + +import java.io.IOException; +import java.util.List; +import lombok.Builder; +import lombok.Getter; +import lombok.Value; +import org.stellar.sdk.KeyPair; +import org.stellar.sdk.Transaction; +import org.stellar.sdk.TransactionBuilderAccount; +import org.stellar.sdk.exception.NetworkException; +import org.stellar.sdk.responses.TransactionResponse; +import org.stellar.sdk.responses.operations.OperationResponse; + +public interface LedgerApi { + /** + * Check if the account has a trustline for the given asset. + * + * @param account The account to check. + * @param asset The asset to check. + * @return True if the account has a trustline for the asset. + * @throws NetworkException If there was an error communicating with the network. + */ + boolean hasTrustline(String account, String asset) throws NetworkException, IOException; + + /** + * Get the account details for the given account. + * + * @param account The account to get. + * @return The account details. + * @throws NetworkException If there was an error communicating with the network. + */ + Account getAccount(String account) throws NetworkException; + + /** + * Get the operations for the given Stellar transaction. + * + * @param stellarTxnId The Stellar transaction ID. + * @return The operations for the transaction. + */ + List getStellarTxnOperations(String stellarTxnId); + + /** + * Submit a transaction to the network. + * + * @param transaction + * @return The transaction response. + * @throws NetworkException + */ + TransactionResponse submitTransaction(Transaction transaction) throws NetworkException; + + @Builder + @Getter + class Account implements TransactionBuilderAccount { + private String accountId; + private Long sequenceNumber; + + private Thresholds thresholds; + private List balances; + private List signers; + + @Override + public KeyPair getKeyPair() { + return KeyPair.fromAccountId(accountId); + } + + @Override + public void setSequenceNumber(long seqNum) { + sequenceNumber = seqNum; + } + + @Override + public Long getIncrementedSequenceNumber() { + return sequenceNumber + 1; + } + + /** Increments sequence number in this object by one. */ + public void incrementSequenceNumber() { + sequenceNumber++; + } + } + + @Builder + @Getter + class Thresholds { + Integer lowThreshold; + Integer medThreshold; + Integer highThreshold; + } + + @Builder + @Getter + class Balance { + String assetType; + String assetCode; + String assetIssuer; + String liquidityPoolId; + String limit; + String balance; + } + + @Value + @Builder + @Getter + class Signer { + String key; + String type; + Integer weight; + String sponsor; + } +} diff --git a/core/src/main/java/org/stellar/anchor/ledger/StellarRpc.java b/core/src/main/java/org/stellar/anchor/ledger/StellarRpc.java new file mode 100644 index 0000000000..6ccd578f32 --- /dev/null +++ b/core/src/main/java/org/stellar/anchor/ledger/StellarRpc.java @@ -0,0 +1,84 @@ +package org.stellar.anchor.ledger; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import lombok.SneakyThrows; +import org.stellar.sdk.KeyPair; +import org.stellar.sdk.SorobanServer; +import org.stellar.sdk.Transaction; +import org.stellar.sdk.exception.NetworkException; +import org.stellar.sdk.responses.TransactionResponse; +import org.stellar.sdk.responses.operations.OperationResponse; +import org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse; +import org.stellar.sdk.xdr.AccountEntry; +import org.stellar.sdk.xdr.LedgerEntry; +import org.stellar.sdk.xdr.LedgerEntryType; +import org.stellar.sdk.xdr.LedgerKey; + +public class StellarRpc implements LedgerApi { + String rpcServerUrl; + SorobanServer sorobanServer; + + public StellarRpc(String rpcServerUrl) { + this.rpcServerUrl = rpcServerUrl; + sorobanServer = new SorobanServer(rpcServerUrl); + } + + @SneakyThrows + @Override + public boolean hasTrustline(String account, String asset) throws NetworkException { + AccountEntry accountEntry = fetchAccountEntry(account); + // TODO: Implement this method + return false; + } + + @Override + public Account getAccount(String account) throws NetworkException { + // TODO: Implement this method + return null; + } + + @Override + public List getStellarTxnOperations(String stellarTxnId) { + // TODO: Implement this method + return List.of(); + } + + @Override + public TransactionResponse submitTransaction(Transaction transaction) throws NetworkException { + // TODO: Implement this method + return null; + } + + private AccountEntry fetchAccountEntry(String account) throws IOException { + // TODO: Implement this method + return null; +// GetLedgerEntriesResponse response; +// KeyPair keyPair = KeyPair.fromAccountId(account); +// +// // Create ledger keys +// List ledgerKeys = +// Collections.singletonList( +// LedgerKey.builder() +// .account( +// LedgerKey.LedgerKeyAccount.builder() +// .accountID(keyPair.getXdrAccountId()) +// .build()) +// .discriminant(LedgerEntryType.ACCOUNT) +// .build()); +// +// // Get ledger entries +// response = sorobanServer.getLedgerEntries(ledgerKeys); +// +// // Print ledger entries +// for (GetLedgerEntriesResponse.LedgerEntryResult result : response.getEntries()) { +// LedgerEntry.LedgerEntryData ledgerEntryData = +// LedgerEntry.LedgerEntryData.fromXdrBase64(result.getXdr()); +// if (ledgerEntryData.getDiscriminant() == LedgerEntryType.ACCOUNT) { +// return ledgerEntryData.getAccount(); +// } +// } +// throw new NetworkException(404, "Account not found"); + } +} diff --git a/core/src/main/java/org/stellar/anchor/sep10/Sep10Service.java b/core/src/main/java/org/stellar/anchor/sep10/Sep10Service.java index f8f1c8a4e7..fb140eeda6 100644 --- a/core/src/main/java/org/stellar/anchor/sep10/Sep10Service.java +++ b/core/src/main/java/org/stellar/anchor/sep10/Sep10Service.java @@ -31,7 +31,7 @@ import org.stellar.anchor.config.AppConfig; import org.stellar.anchor.config.SecretConfig; import org.stellar.anchor.config.Sep10Config; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.LedgerApi; import org.stellar.anchor.util.Log; import org.stellar.sdk.*; import org.stellar.sdk.Sep10Challenge.ChallengeTransaction; @@ -39,14 +39,13 @@ import org.stellar.sdk.exception.NetworkException; import org.stellar.sdk.operations.ManageDataOperation; import org.stellar.sdk.operations.Operation; -import org.stellar.sdk.responses.AccountResponse; /** The Sep-10 protocol service. */ public class Sep10Service implements ISep10Service { final AppConfig appConfig; final SecretConfig secretConfig; final Sep10Config sep10Config; - final Horizon horizon; + final LedgerApi ledgerApi; final JwtService jwtService; final ClientFinder clientFinder; final String serverAccountId; @@ -57,7 +56,7 @@ public Sep10Service( AppConfig appConfig, SecretConfig secretConfig, Sep10Config sep10Config, - Horizon horizon, + LedgerApi ledgerApi, JwtService jwtService, ClientFinder clientFinder) { debug("appConfig:", appConfig); @@ -65,7 +64,7 @@ public Sep10Service( this.appConfig = appConfig; this.secretConfig = secretConfig; this.sep10Config = sep10Config; - this.horizon = horizon; + this.ledgerApi = ledgerApi; this.jwtService = jwtService; this.clientFinder = clientFinder; this.serverAccountId = @@ -127,11 +126,11 @@ public ValidationResponse validateChallenge(ValidationRequest request) // fetch the client domain from the transaction String clientDomain = fetchClientDomain(challenge); - // fetch the account response from the horizon - AccountResponse account = fetchAccount(request, challenge, clientDomain); + // fetch the account response from the ledgerApi + LedgerApi.Account account = fetchAccount(request, challenge, clientDomain); if (account == null) { - // The account does not exist from Horizon, using the client's master key to verify. + // The account does not exist from LedgerApi, using the client's master key to verify. return ValidationResponse.of(generateSep10Jwt(challenge, clientDomain, homeDomain)); } // Since the account exists, we should check the signers and the client domain @@ -388,7 +387,7 @@ void validateAccountFormat(ChallengeRequest request) throws SepException { } void validateChallengeRequest( - ValidationRequest request, AccountResponse account, String clientDomain) + ValidationRequest request, LedgerApi.Account account, String clientDomain) throws SepValidationException { // fetch the signers from the transaction Set signers = fetchSigners(account); @@ -414,7 +413,7 @@ void validateChallengeRequest( signers); } - Set fetchSigners(AccountResponse account) { + Set fetchSigners(LedgerApi.Account account) { // Find the signers of the client account. return account.getSigners().stream() .filter(as -> as.getType().equals("ed25519_public_key")) @@ -422,14 +421,14 @@ Set fetchSigners(AccountResponse account) { .collect(Collectors.toSet()); } - AccountResponse fetchAccount( + LedgerApi.Account fetchAccount( ValidationRequest request, ChallengeTransaction challenge, String clientDomain) throws SepValidationException { // Check the client's account - AccountResponse account; + LedgerApi.Account account; try { infoF("Checking if {} exists in the Stellar network", challenge.getClientAccountId()); - account = horizon.getServer().accounts().account(challenge.getClientAccountId()); + account = ledgerApi.getAccount(challenge.getClientAccountId()); traceF("challenge account: {}", account); sep10ChallengeValidatedCounter.increment(); return account; diff --git a/core/src/test/kotlin/org/stellar/anchor/horizon/HorizonTest.kt b/core/src/test/kotlin/org/stellar/anchor/ledger/HorizonTest.kt similarity index 86% rename from core/src/test/kotlin/org/stellar/anchor/horizon/HorizonTest.kt rename to core/src/test/kotlin/org/stellar/anchor/ledger/HorizonTest.kt index f23648b48b..f53d4932e4 100644 --- a/core/src/test/kotlin/org/stellar/anchor/horizon/HorizonTest.kt +++ b/core/src/test/kotlin/org/stellar/anchor/ledger/HorizonTest.kt @@ -1,4 +1,4 @@ -package org.stellar.anchor.horizon +package org.stellar.anchor.ledger import io.mockk.every import io.mockk.mockk @@ -37,7 +37,7 @@ internal class HorizonTest { } @Test - fun test_isTrustlineConfigured_native() { + fun test_hasTrustline_native() { val appConfig = mockk() every { appConfig.horizonUrl } returns TEST_HORIZON_URI every { appConfig.stellarNetworkPassphrase } returns TEST_HORIZON_PASSPHRASE @@ -47,11 +47,11 @@ internal class HorizonTest { val account = "testAccount" val asset = "stellar:native" - assertTrue(horizon.isTrustlineConfigured(account, asset)) + assertTrue(horizon.hasTrustline(account, asset)) } @Test - fun test_isTrustlineConfigured_horizonError() { + fun test_hasTrustline_horizonError() { val appConfig = mockk() val server = mockk() val account = "testAccount" @@ -63,13 +63,13 @@ internal class HorizonTest { val horizon = mockk() every { horizon.server } returns server - every { horizon.isTrustlineConfigured(account, asset) } answers { callOriginal() } + every { horizon.hasTrustline(account, asset) } answers { callOriginal() } - assertThrows { horizon.isTrustlineConfigured(account, asset) } + assertThrows { horizon.hasTrustline(account, asset) } } @Test - fun test_isTrustlineConfigured_present() { + fun test_hasTrustline_present() { val appConfig = mockk() val server = mockk() val account = "testAccount" @@ -99,12 +99,12 @@ internal class HorizonTest { val horizon = mockk() every { horizon.server } returns server - every { horizon.isTrustlineConfigured(account, asset) } answers { callOriginal() } - assertTrue(horizon.isTrustlineConfigured(account, asset)) + every { horizon.hasTrustline(account, asset) } answers { callOriginal() } + assertTrue(horizon.hasTrustline(account, asset)) } @Test - fun test_isTrustlineConfigured_absent() { + fun test_hasTrustline_absent() { val appConfig = mockk() val server = mockk() val account = "testAccount" @@ -139,7 +139,7 @@ internal class HorizonTest { val horizon = mockk() every { horizon.server } returns server - every { horizon.isTrustlineConfigured(account, asset) } answers { callOriginal() } - assertFalse(horizon.isTrustlineConfigured(account, asset)) + every { horizon.hasTrustline(account, asset) } answers { callOriginal() } + assertFalse(horizon.hasTrustline(account, asset)) } } diff --git a/core/src/test/kotlin/org/stellar/anchor/sep10/Sep10ServiceTest.kt b/core/src/test/kotlin/org/stellar/anchor/sep10/Sep10ServiceTest.kt index ebef7b94de..0327002735 100644 --- a/core/src/test/kotlin/org/stellar/anchor/sep10/Sep10ServiceTest.kt +++ b/core/src/test/kotlin/org/stellar/anchor/sep10/Sep10ServiceTest.kt @@ -54,7 +54,8 @@ import org.stellar.anchor.config.AppConfig import org.stellar.anchor.config.CustodySecretConfig import org.stellar.anchor.config.SecretConfig import org.stellar.anchor.config.Sep10Config -import org.stellar.anchor.horizon.Horizon +import org.stellar.anchor.ledger.Horizon +import org.stellar.anchor.ledger.LedgerApi import org.stellar.anchor.setupMock import org.stellar.anchor.util.FileUtil import org.stellar.anchor.util.GsonUtils @@ -65,7 +66,6 @@ import org.stellar.sdk.exception.BadRequestException import org.stellar.sdk.exception.InvalidSep10ChallengeException import org.stellar.sdk.operations.ManageDataOperation import org.stellar.sdk.operations.SetOptionsOperation -import org.stellar.sdk.responses.AccountResponse import org.stellar.walletsdk.auth.DefaultAuthHeaderSigner import org.stellar.walletsdk.auth.createAuthSignToken import org.stellar.walletsdk.horizon.AccountKeyPair @@ -79,10 +79,10 @@ internal class TestSigner( @SerializedName("weight") val weight: Int, @SerializedName("sponsor") val sponsor: String ) { - fun toSigner(): AccountResponse.Signer { + fun toSigner(): LedgerApi.Signer { val gson = GsonUtils.getInstance() val json = gson.toJson(this) - return gson.fromJson(json, AccountResponse.Signer::class.java) + return gson.fromJson(json, LedgerApi.Signer::class.java) } } @@ -119,7 +119,7 @@ internal class Sep10ServiceTest { @MockK(relaxed = true) lateinit var secretConfig: SecretConfig @MockK(relaxed = true) lateinit var custodySecretConfig: CustodySecretConfig @MockK(relaxed = true) lateinit var sep10Config: Sep10Config - @MockK(relaxed = true) lateinit var horizon: Horizon + @MockK(relaxed = true) lateinit var ledgerApi: LedgerApi @MockK(relaxed = true) lateinit var clientFinder: ClientFinder private lateinit var jwtService: JwtService @@ -142,7 +142,7 @@ internal class Sep10ServiceTest { this.jwtService = spyk(JwtService(secretConfig, custodySecretConfig)) this.sep10Service = - Sep10Service(appConfig, secretConfig, sep10Config, horizon, jwtService, clientFinder) + Sep10Service(appConfig, secretConfig, sep10Config, ledgerApi, jwtService, clientFinder) this.httpClient = `create httpClient`() } @@ -239,9 +239,10 @@ internal class Sep10ServiceTest { every { secretConfig.sep10SigningSeed } returns String(serverKP.secretSeed) every { appConfig.horizonUrl } returns horizonUrl every { appConfig.stellarNetworkPassphrase } returns TESTNET.networkPassphrase - val horizon = Horizon(appConfig) + val ledgerApi = Horizon(appConfig) + // TODO: to be parameterized for Stellar RPC and Horizon this.sep10Service = - Sep10Service(appConfig, secretConfig, sep10Config, horizon, jwtService, clientFinder) + Sep10Service(appConfig, secretConfig, sep10Config, ledgerApi, jwtService, clientFinder) // 3 ------ Run tests val validationRequest = ValidationRequest.of(transaction.toEnvelopeXdrBase64()) @@ -322,9 +323,9 @@ internal class Sep10ServiceTest { every { secretConfig.sep10SigningSeed } returns String(serverKP.secretSeed) every { appConfig.horizonUrl } returns mockHorizonUrl every { appConfig.stellarNetworkPassphrase } returns TESTNET.networkPassphrase - val horizon = Horizon(appConfig) + val ledgerApi = Horizon(appConfig) this.sep10Service = - Sep10Service(appConfig, secretConfig, sep10Config, horizon, jwtService, clientFinder) + Sep10Service(appConfig, secretConfig, sep10Config, ledgerApi, jwtService, clientFinder) // 3 ------ Run tests val validationRequest = ValidationRequest.of(transaction.toEnvelopeXdrBase64()) @@ -375,14 +376,14 @@ internal class Sep10ServiceTest { val mockSigners = listOf(TestSigner(clientKeyPair.accountId, "ed25519_public_key", 1, "").toSigner()) val accountResponse = - mockk { + mockk { every { accountId } returns clientKeyPair.accountId every { sequenceNumber } returns 1 every { signers } returns mockSigners every { thresholds.medThreshold } returns 1 } - every { horizon.server.accounts().account(ofType(String::class)) } returns accountResponse + every { ledgerApi.getAccount(any()) } returns accountResponse val response = sep10Service.validateChallenge(vr) val jwt = jwtService.decode(response.token, Sep10Jwt::class.java) @@ -399,14 +400,14 @@ internal class Sep10ServiceTest { ) val accountResponse = - mockk { + mockk { every { accountId } returns clientKeyPair.accountId every { sequenceNumber } returns 1 every { signers } returns mockSigners every { thresholds.medThreshold } returns 1 } - every { horizon.server.accounts().account(ofType(String::class)) } returns accountResponse + every { ledgerApi.getAccount(any()) } returns accountResponse val vr = ValidationRequest() vr.transaction = createTestChallenge(TEST_CLIENT_DOMAIN, TEST_HOME_DOMAIN, true) @@ -423,7 +424,7 @@ internal class Sep10ServiceTest { // Test when the transaction was not signed by the client domain and the client account not // exists - every { horizon.server.accounts().account(ofType(String::class)) } answers + every { ledgerApi.getAccount(any()) } answers { throw BadRequestException(400, "mock error", null, null) } @@ -437,7 +438,7 @@ internal class Sep10ServiceTest { val vr = ValidationRequest() vr.transaction = createTestChallenge("", TEST_HOME_DOMAIN, false) - every { horizon.server.accounts().account(ofType(String::class)) } answers + every { ledgerApi.getAccount(any()) } answers { throw BadRequestException(400, "mock error", null, null) } @@ -768,9 +769,9 @@ internal class Sep10ServiceTest { every { secretConfig.sep10SigningSeed } returns String(serverKP.secretSeed) every { appConfig.horizonUrl } returns horizonUrl every { appConfig.stellarNetworkPassphrase } returns network.networkPassphrase - val horizon = Horizon(appConfig) + val ledgerApi = Horizon(appConfig) this.sep10Service = - Sep10Service(appConfig, secretConfig, sep10Config, horizon, jwtService, clientFinder) + Sep10Service(appConfig, secretConfig, sep10Config, ledgerApi, jwtService, clientFinder) // 3 ------ Setup multisig val httpRequest = @@ -782,7 +783,7 @@ internal class Sep10ServiceTest { val response = httpClient.newCall(httpRequest).execute() assertEquals(200, response.code) - val clientAccount = horizon.server.accounts().account(clientMasterKP.accountId) + val clientAccount = ledgerApi.getAccount(clientMasterKP.accountId) val multisigTx = TransactionBuilder(clientAccount, network) .addPreconditions( @@ -801,7 +802,7 @@ internal class Sep10ServiceTest { ) .build() multisigTx.sign(clientMasterKP) - horizon.server.submitTransaction(multisigTx) + ledgerApi.submitTransaction(multisigTx) // 4 ------ Run tests val validationRequest = ValidationRequest.of(transaction.toEnvelopeXdrBase64()) diff --git a/platform/src/main/java/org/stellar/anchor/platform/component/custody/FireblocksBeans.java b/platform/src/main/java/org/stellar/anchor/platform/component/custody/FireblocksBeans.java index 881e3aff9b..ec624cfac8 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/component/custody/FireblocksBeans.java +++ b/platform/src/main/java/org/stellar/anchor/platform/component/custody/FireblocksBeans.java @@ -9,7 +9,7 @@ import org.stellar.anchor.api.custody.fireblocks.TransactionDetails; import org.stellar.anchor.api.exception.InvalidConfigException; import org.stellar.anchor.config.CustodySecretConfig; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.platform.config.FireblocksConfig; import org.stellar.anchor.platform.custody.*; import org.stellar.anchor.platform.custody.fireblocks.FireblocksApiClient; diff --git a/platform/src/main/java/org/stellar/anchor/platform/component/platform/PlatformServerBeans.java b/platform/src/main/java/org/stellar/anchor/platform/component/platform/PlatformServerBeans.java index 985651f2ae..d1d5c6247a 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/component/platform/PlatformServerBeans.java +++ b/platform/src/main/java/org/stellar/anchor/platform/component/platform/PlatformServerBeans.java @@ -17,7 +17,7 @@ import org.stellar.anchor.filter.ApiKeyFilter; import org.stellar.anchor.filter.NoneFilter; import org.stellar.anchor.filter.PlatformAuthJwtFilter; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.platform.apiclient.CustodyApiClient; import org.stellar.anchor.platform.config.PlatformApiConfig; import org.stellar.anchor.platform.config.PlatformServerConfig; diff --git a/platform/src/main/java/org/stellar/anchor/platform/component/platform/RpcActionBeans.java b/platform/src/main/java/org/stellar/anchor/platform/component/platform/RpcActionBeans.java index 98cbc2526c..b0facbb94f 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/component/platform/RpcActionBeans.java +++ b/platform/src/main/java/org/stellar/anchor/platform/component/platform/RpcActionBeans.java @@ -9,7 +9,7 @@ import org.stellar.anchor.config.CustodyConfig; import org.stellar.anchor.custody.CustodyService; import org.stellar.anchor.event.EventService; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.metrics.MetricsService; import org.stellar.anchor.platform.component.sep.ApiClientBeans; import org.stellar.anchor.platform.config.PropertyCustodyConfig; diff --git a/platform/src/main/java/org/stellar/anchor/platform/component/sep/SepBeans.java b/platform/src/main/java/org/stellar/anchor/platform/component/sep/SepBeans.java index cf43c80a87..290086a1fe 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/component/sep/SepBeans.java +++ b/platform/src/main/java/org/stellar/anchor/platform/component/sep/SepBeans.java @@ -17,7 +17,7 @@ import org.stellar.anchor.config.*; import org.stellar.anchor.event.EventService; import org.stellar.anchor.filter.Sep10JwtFilter; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.platform.condition.OnAllSepsEnabled; import org.stellar.anchor.platform.condition.OnAnySepsEnabled; import org.stellar.anchor.platform.config.*; diff --git a/platform/src/main/java/org/stellar/anchor/platform/component/share/UtilityBeans.java b/platform/src/main/java/org/stellar/anchor/platform/component/share/UtilityBeans.java index 1f88a541ab..0371d604c8 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/component/share/UtilityBeans.java +++ b/platform/src/main/java/org/stellar/anchor/platform/component/share/UtilityBeans.java @@ -15,7 +15,7 @@ import org.stellar.anchor.client.ClientService; import org.stellar.anchor.config.*; import org.stellar.anchor.healthcheck.HealthCheckable; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.platform.config.*; import org.stellar.anchor.platform.service.HealthCheckService; import org.stellar.anchor.platform.service.Sep24MoreInfoUrlConstructor; diff --git a/platform/src/main/java/org/stellar/anchor/platform/custody/fireblocks/FireblocksEventService.java b/platform/src/main/java/org/stellar/anchor/platform/custody/fireblocks/FireblocksEventService.java index da0511ad8d..82fe2521f7 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/custody/fireblocks/FireblocksEventService.java +++ b/platform/src/main/java/org/stellar/anchor/platform/custody/fireblocks/FireblocksEventService.java @@ -22,7 +22,7 @@ import org.stellar.anchor.api.exception.BadRequestException; import org.stellar.anchor.api.exception.InvalidConfigException; import org.stellar.anchor.api.exception.SepException; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.platform.config.FireblocksConfig; import org.stellar.anchor.platform.custody.*; import org.stellar.anchor.platform.data.JdbcCustodyTransactionRepo; diff --git a/platform/src/main/java/org/stellar/anchor/platform/job/TrustlineCheckJob.java b/platform/src/main/java/org/stellar/anchor/platform/job/TrustlineCheckJob.java index 7239f7a7a8..6099e318ad 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/job/TrustlineCheckJob.java +++ b/platform/src/main/java/org/stellar/anchor/platform/job/TrustlineCheckJob.java @@ -7,7 +7,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.stellar.anchor.api.exception.AnchorException; import org.stellar.anchor.api.rpc.method.NotifyTrustSetRequest; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.platform.config.PropertyCustodyConfig; import org.stellar.anchor.platform.data.JdbcTransactionPendingTrust; import org.stellar.anchor.platform.data.JdbcTransactionPendingTrustRepo; @@ -49,7 +49,7 @@ public void checkTrust() throws AnchorException { } else { boolean trustlineConfigured; try { - trustlineConfigured = horizon.isTrustlineConfigured(t.getAccount(), t.getAsset()); + trustlineConfigured = horizon.hasTrustline(t.getAccount(), t.getAsset()); } catch (NetworkException ex) { trustlineConfigured = false; } diff --git a/platform/src/main/java/org/stellar/anchor/platform/rpc/DoStellarPaymentHandler.java b/platform/src/main/java/org/stellar/anchor/platform/rpc/DoStellarPaymentHandler.java index 8617e1fc4e..90194cfa98 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/rpc/DoStellarPaymentHandler.java +++ b/platform/src/main/java/org/stellar/anchor/platform/rpc/DoStellarPaymentHandler.java @@ -24,7 +24,7 @@ import org.stellar.anchor.config.CustodyConfig; import org.stellar.anchor.custody.CustodyService; import org.stellar.anchor.event.EventService; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.metrics.MetricsService; import org.stellar.anchor.platform.data.*; import org.stellar.anchor.platform.validator.RequestValidator; @@ -92,13 +92,12 @@ protected SepTransactionStatus getNextStatus( switch (Sep.from(txn.getProtocol())) { case SEP_6: JdbcSep6Transaction txn6 = (JdbcSep6Transaction) txn; - trustlineConfigured = - horizon.isTrustlineConfigured(txn6.getToAccount(), txn6.getAmountOutAsset()); + trustlineConfigured = horizon.hasTrustline(txn6.getToAccount(), txn6.getAmountOutAsset()); break; case SEP_24: JdbcSep24Transaction txn24 = (JdbcSep24Transaction) txn; trustlineConfigured = - horizon.isTrustlineConfigured(txn24.getToAccount(), txn24.getAmountOutAsset()); + horizon.hasTrustline(txn24.getToAccount(), txn24.getAmountOutAsset()); break; default: break; @@ -148,8 +147,7 @@ protected void updateTransactionWithRpcRequest( JdbcSep6Transaction txn6 = (JdbcSep6Transaction) txn; try { - trustlineConfigured = - horizon.isTrustlineConfigured(txn6.getToAccount(), txn6.getAmountOutAsset()); + trustlineConfigured = horizon.hasTrustline(txn6.getToAccount(), txn6.getAmountOutAsset()); } catch (NetworkException ex) { trustlineConfigured = false; } @@ -171,7 +169,7 @@ protected void updateTransactionWithRpcRequest( try { trustlineConfigured = - horizon.isTrustlineConfigured(txn24.getToAccount(), txn24.getAmountOutAsset()); + horizon.hasTrustline(txn24.getToAccount(), txn24.getAmountOutAsset()); } catch (NetworkException ex) { trustlineConfigured = false; } diff --git a/platform/src/main/java/org/stellar/anchor/platform/rpc/NotifyOnchainFundsReceivedHandler.java b/platform/src/main/java/org/stellar/anchor/platform/rpc/NotifyOnchainFundsReceivedHandler.java index 463169e6c1..8fa766be33 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/rpc/NotifyOnchainFundsReceivedHandler.java +++ b/platform/src/main/java/org/stellar/anchor/platform/rpc/NotifyOnchainFundsReceivedHandler.java @@ -24,7 +24,7 @@ import org.stellar.anchor.api.sep.SepTransactionStatus; import org.stellar.anchor.asset.AssetService; import org.stellar.anchor.event.EventService; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.metrics.MetricsService; import org.stellar.anchor.platform.data.JdbcSep24Transaction; import org.stellar.anchor.platform.data.JdbcSep31Transaction; diff --git a/platform/src/main/java/org/stellar/anchor/platform/rpc/NotifyOnchainFundsSentHandler.java b/platform/src/main/java/org/stellar/anchor/platform/rpc/NotifyOnchainFundsSentHandler.java index 1bbf62f71a..ba9a5c8866 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/rpc/NotifyOnchainFundsSentHandler.java +++ b/platform/src/main/java/org/stellar/anchor/platform/rpc/NotifyOnchainFundsSentHandler.java @@ -23,7 +23,7 @@ import org.stellar.anchor.api.sep.SepTransactionStatus; import org.stellar.anchor.asset.AssetService; import org.stellar.anchor.event.EventService; -import org.stellar.anchor.horizon.Horizon; +import org.stellar.anchor.ledger.Horizon; import org.stellar.anchor.metrics.MetricsService; import org.stellar.anchor.platform.data.JdbcSep24Transaction; import org.stellar.anchor.platform.data.JdbcSep6Transaction; diff --git a/platform/src/test/kotlin/org/stellar/anchor/platform/custody/fireblocks/FireblocksEventServiceTest.kt b/platform/src/test/kotlin/org/stellar/anchor/platform/custody/fireblocks/FireblocksEventServiceTest.kt index bce1f5d309..4fef9b487b 100644 --- a/platform/src/test/kotlin/org/stellar/anchor/platform/custody/fireblocks/FireblocksEventServiceTest.kt +++ b/platform/src/test/kotlin/org/stellar/anchor/platform/custody/fireblocks/FireblocksEventServiceTest.kt @@ -18,7 +18,7 @@ import org.stellar.anchor.api.custody.fireblocks.TransactionDetails import org.stellar.anchor.api.custody.fireblocks.TransactionStatus import org.stellar.anchor.api.exception.BadRequestException import org.stellar.anchor.api.exception.InvalidConfigException -import org.stellar.anchor.horizon.Horizon +import org.stellar.anchor.ledger.Horizon import org.stellar.anchor.platform.config.FireblocksConfig import org.stellar.anchor.platform.config.PropertyCustodySecretConfig import org.stellar.anchor.platform.custody.CustodyPayment diff --git a/platform/src/test/kotlin/org/stellar/anchor/platform/job/TrustlineCheckJobTest.kt b/platform/src/test/kotlin/org/stellar/anchor/platform/job/TrustlineCheckJobTest.kt index cf8fe616f5..09eeffaef3 100644 --- a/platform/src/test/kotlin/org/stellar/anchor/platform/job/TrustlineCheckJobTest.kt +++ b/platform/src/test/kotlin/org/stellar/anchor/platform/job/TrustlineCheckJobTest.kt @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test import org.skyscreamer.jsonassert.JSONAssert import org.skyscreamer.jsonassert.JSONCompareMode import org.stellar.anchor.api.rpc.method.NotifyTrustSetRequest -import org.stellar.anchor.horizon.Horizon +import org.stellar.anchor.ledger.Horizon import org.stellar.anchor.platform.config.PropertyCustodyConfig import org.stellar.anchor.platform.config.PropertyCustodyConfig.Trustline import org.stellar.anchor.platform.data.JdbcTransactionPendingTrust @@ -56,7 +56,7 @@ class TrustlineCheckJobTest { every { transactionPendingTrustRepo.findAll() } returns listOf(txnPendingTrust) every { custodyConfig.trustline } returns trustline - every { horizon.isTrustlineConfigured(ACCOUNT, ASSET) } returns false + every { horizon.hasTrustline(ACCOUNT, ASSET) } returns false trustlineCheckJob.checkTrust() @@ -78,7 +78,7 @@ class TrustlineCheckJobTest { every { transactionPendingTrustRepo.findAll() } returns listOf(txnPendingTrust) every { custodyConfig.trustline } returns trustline - every { horizon.isTrustlineConfigured(ACCOUNT, ASSET) } returns true + every { horizon.hasTrustline(ACCOUNT, ASSET) } returns true every { notifyTrustSetHandler.handle(capture(notifyTrustSetRequestCapture)) } returns null trustlineCheckJob.checkTrust() diff --git a/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/DoStellarPaymentHandlerTest.kt b/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/DoStellarPaymentHandlerTest.kt index 9c3e78c121..6f23066fa8 100644 --- a/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/DoStellarPaymentHandlerTest.kt +++ b/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/DoStellarPaymentHandlerTest.kt @@ -32,7 +32,7 @@ import org.stellar.anchor.custody.CustodyService import org.stellar.anchor.event.EventService import org.stellar.anchor.event.EventService.EventQueue.TRANSACTION import org.stellar.anchor.event.EventService.Session -import org.stellar.anchor.horizon.Horizon +import org.stellar.anchor.ledger.Horizon import org.stellar.anchor.metrics.MetricsService import org.stellar.anchor.platform.data.JdbcSep24Transaction import org.stellar.anchor.platform.data.JdbcSep6Transaction @@ -245,7 +245,7 @@ class DoStellarPaymentHandlerTest { every { txn31Store.findByTransactionId(any()) } returns null every { txn24Store.save(capture(sep24TxnCapture)) } returns null every { custodyConfig.isCustodyIntegrationEnabled } returns true - every { horizon.isTrustlineConfigured(TO_ACCOUNT, AMOUNT_OUT_ASSET) } returns true + every { horizon.hasTrustline(TO_ACCOUNT, AMOUNT_OUT_ASSET) } returns true every { eventSession.publish(capture(anchorEventCapture)) } just Runs every { metricsService.counter(PLATFORM_RPC_TRANSACTION, "SEP", "sep24") } returns sepTransactionCounter @@ -330,7 +330,7 @@ class DoStellarPaymentHandlerTest { every { txn31Store.findByTransactionId(any()) } returns null every { txn24Store.save(capture(sep24TxnCapture)) } returns null every { custodyConfig.isCustodyIntegrationEnabled } returns true - every { horizon.isTrustlineConfigured(TO_ACCOUNT, AMOUNT_OUT_ASSET) } returns false + every { horizon.hasTrustline(TO_ACCOUNT, AMOUNT_OUT_ASSET) } returns false every { transactionPendingTrustRepo.save(capture(txnPendingTrustCapture)) } returns JdbcTransactionPendingTrust() every { eventSession.publish(capture(anchorEventCapture)) } just Runs @@ -504,7 +504,7 @@ class DoStellarPaymentHandlerTest { every { txn31Store.findByTransactionId(any()) } returns null every { txn6Store.save(capture(sep6TxnCapture)) } returns null every { custodyConfig.isCustodyIntegrationEnabled } returns true - every { horizon.isTrustlineConfigured(TO_ACCOUNT, AMOUNT_OUT_ASSET) } returns true + every { horizon.hasTrustline(TO_ACCOUNT, AMOUNT_OUT_ASSET) } returns true every { eventSession.publish(capture(anchorEventCapture)) } just Runs every { metricsService.counter(PLATFORM_RPC_TRANSACTION, "SEP", "sep6") } returns sepTransactionCounter @@ -591,7 +591,7 @@ class DoStellarPaymentHandlerTest { every { txn31Store.findByTransactionId(any()) } returns null every { txn6Store.save(capture(sep6TxnCapture)) } returns null every { custodyConfig.isCustodyIntegrationEnabled } returns true - every { horizon.isTrustlineConfigured(TO_ACCOUNT, AMOUNT_OUT_ASSET) } returns false + every { horizon.hasTrustline(TO_ACCOUNT, AMOUNT_OUT_ASSET) } returns false every { transactionPendingTrustRepo.save(capture(txnPendingTrustCapture)) } returns JdbcTransactionPendingTrust() every { eventSession.publish(capture(anchorEventCapture)) } just Runs diff --git a/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/NotifyOnchainFundsReceivedHandlerTest.kt b/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/NotifyOnchainFundsReceivedHandlerTest.kt index 1839456cb5..1bdaf3ba1d 100644 --- a/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/NotifyOnchainFundsReceivedHandlerTest.kt +++ b/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/NotifyOnchainFundsReceivedHandlerTest.kt @@ -33,7 +33,7 @@ import org.stellar.anchor.asset.DefaultAssetService import org.stellar.anchor.event.EventService import org.stellar.anchor.event.EventService.EventQueue.TRANSACTION import org.stellar.anchor.event.EventService.Session -import org.stellar.anchor.horizon.Horizon +import org.stellar.anchor.ledger.Horizon import org.stellar.anchor.metrics.MetricsService import org.stellar.anchor.platform.data.JdbcSep24Transaction import org.stellar.anchor.platform.data.JdbcSep31Transaction diff --git a/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/NotifyOnchainFundsSentHandlerTest.kt b/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/NotifyOnchainFundsSentHandlerTest.kt index 5dbbf13962..70fa54505e 100644 --- a/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/NotifyOnchainFundsSentHandlerTest.kt +++ b/platform/src/test/kotlin/org/stellar/anchor/platform/rpc/NotifyOnchainFundsSentHandlerTest.kt @@ -34,7 +34,7 @@ import org.stellar.anchor.asset.AssetService import org.stellar.anchor.event.EventService import org.stellar.anchor.event.EventService.EventQueue.TRANSACTION import org.stellar.anchor.event.EventService.Session -import org.stellar.anchor.horizon.Horizon +import org.stellar.anchor.ledger.Horizon import org.stellar.anchor.metrics.MetricsService import org.stellar.anchor.platform.data.JdbcSep24Transaction import org.stellar.anchor.platform.data.JdbcSep6Transaction