From a4ae191821f0dbc269518527fd79a9309c9ea7af Mon Sep 17 00:00:00 2001 From: jamesm Date: Mon, 28 Oct 2024 09:35:56 -0400 Subject: [PATCH] Fix compilation errors - need some advice on error codes though as i'm not convinced it's a great experience for the client --- .../java/com/lmax/solana4j/SolanaDriver.java | 90 +++++++++++-------- .../java/com/lmax/solana4j/SolanaNodeDsl.java | 8 +- 2 files changed, 53 insertions(+), 45 deletions(-) diff --git a/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaDriver.java b/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaDriver.java index 99cc24cd..1a1fd19e 100644 --- a/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaDriver.java +++ b/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaDriver.java @@ -7,8 +7,8 @@ import com.lmax.solana4j.client.api.AccountInfo; import com.lmax.solana4j.client.api.Blockhash; import com.lmax.solana4j.client.api.SolanaApi; -import com.lmax.solana4j.client.api.SolanaClientError; import com.lmax.solana4j.client.api.SolanaClientResponse; +import com.lmax.solana4j.client.api.TokenAmount; import com.lmax.solana4j.client.api.TransactionResponse; import com.lmax.solana4j.domain.TestKeyPair; import com.lmax.solana4j.domain.TestPublicKey; @@ -19,6 +19,7 @@ import com.lmax.solana4j.transactionblobs.V0TransactionBlobFactory; import java.util.List; +import java.util.concurrent.locks.LockSupport; import java.util.stream.Collectors; public class SolanaDriver @@ -33,27 +34,27 @@ public SolanaDriver(final SolanaApi solanaApi) public String requestAirdrop(final TestPublicKey address, final long amountLamports) { - return solanaApi.requestAirdrop(address.getPublicKeyBase58(), amountLamports); + return retryingClient(solanaApi.requestAirdrop(address.getPublicKeyBase58(), amountLamports)); } public TransactionResponse getTransactionResponse(final String transactionSignature) { - return solanaApi.getTransaction(transactionSignature); + return retryingClient(solanaApi.getTransaction(transactionSignature)); } public long getSlot() { - return solanaApi.getSlot(); + return retryingClient(solanaApi.getSlot()); } public long getBlockHeight() { - return solanaApi.getBlockHeight(); + return retryingClient(solanaApi.getBlockHeight()); } public AccountInfo getAccountInfo(final TestPublicKey address) { - return solanaApi.getAccountInfo(address.getPublicKeyBase58()); + return retryingClient(solanaApi.getAccountInfo(address.getPublicKeyBase58())); } public String createAddressLookupTable(final ProgramDerivedAddress programDerivedAddress, @@ -61,7 +62,7 @@ public String createAddressLookupTable(final ProgramDerivedAddress programDerive final TestKeyPair payer, final Slot slot) { - final Blockhash recentBlockhash = solanaApi.getLatestBlockhash(); + final Blockhash recentBlockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().createAddressLookupTable( programDerivedAddress, @@ -71,7 +72,7 @@ public String createAddressLookupTable(final ProgramDerivedAddress programDerive payer.getSolana4jPublicKey(), List.of(payer, authority)); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String extendAddressLookupTable(final TestPublicKey addressLookupTable, @@ -80,7 +81,7 @@ public String extendAddressLookupTable(final TestPublicKey addressLookupTable, final List addressesToAdd, final List addressLookupTables) { - final Blockhash recentBlockhash = solanaApi.getLatestBlockhash(); + final Blockhash recentBlockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().extendAddressLookupTable( addressLookupTable.getSolana4jPublicKey(), @@ -91,7 +92,7 @@ public String extendAddressLookupTable(final TestPublicKey addressLookupTable, List.of(payer, authority), addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String createMintAccount( @@ -104,8 +105,8 @@ public String createMintAccount( final int accountSpan, final List addressLookupTables) { - final Blockhash recentBlockhash = solanaApi.getLatestBlockhash(); - final long rentExemption = solanaApi.getMinimumBalanceForRentExemption(accountSpan); + final Blockhash recentBlockhash = retryingClient(solanaApi.getLatestBlockhash()); + final long rentExemption = retryingClient(solanaApi.getMinimumBalanceForRentExemption(accountSpan)); final String transactionBlob = getTransactionFactory().createMintAccount( tokenProgram, @@ -121,7 +122,7 @@ public String createMintAccount( addressLookupTables ); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String mintTo( @@ -133,7 +134,7 @@ public String mintTo( final TestKeyPair payer, final List addressLookupTables) { - final Blockhash recentBlockhash = solanaApi.getLatestBlockhash(); + final Blockhash recentBlockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().mintTo( tokenProgram, @@ -146,7 +147,7 @@ public String mintTo( addressLookupTables ); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String createTokenAccount( @@ -158,8 +159,8 @@ public String createTokenAccount( final int accountSpan, final List addressLookupTables) { - final Long rentExemption = solanaApi.getMinimumBalanceForRentExemption(accountSpan); - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Long rentExemption = retryingClient(solanaApi.getMinimumBalanceForRentExemption(accountSpan)); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().createTokenAccount( tokenProgram, @@ -173,17 +174,17 @@ public String createTokenAccount( List.of(payer, account), addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public long getBalance(final String address) { - return solanaApi.getBalance(address); + return retryingClient(solanaApi.getBalance(address)); } - public String getTokenBalance(final String address) + public TokenAmount getTokenBalance(final String address) { - return solanaApi.getTokenAccountBalance(address); + return retryingClient(solanaApi.getTokenAccountBalance(address)); } public String createNonceAccount( @@ -193,8 +194,8 @@ public String createNonceAccount( final int accountSpan, final List addressLookupTables) { - final Long rentExemption = solanaApi.getMinimumBalanceForRentExemption(accountSpan); - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Long rentExemption = retryingClient(solanaApi.getMinimumBalanceForRentExemption(accountSpan)); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().createNonce( nonceAccount.getSolana4jPublicKey(), @@ -206,7 +207,7 @@ public String createNonceAccount( List.of(payer, nonceAccount, nonceAuthority), addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String createMultiSigAccount( @@ -218,8 +219,8 @@ public String createMultiSigAccount( final TestKeyPair payer, final List addressLookupTables) { - final Long rentExemption = solanaApi.getMinimumBalanceForRentExemption(accountSpan); - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Long rentExemption = retryingClient(solanaApi.getMinimumBalanceForRentExemption(accountSpan)); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().createMultiSigAccount( tokenProgram, @@ -233,7 +234,7 @@ public String createMultiSigAccount( List.of(payer, account), addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String advanceNonce( @@ -242,7 +243,7 @@ public String advanceNonce( final TestKeyPair payer, final List addressLookupTables) { - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().advanceNonce( account.getSolana4jPublicKey(), @@ -252,7 +253,7 @@ public String advanceNonce( List.of(payer, authority), addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String tokenTransfer( @@ -265,7 +266,7 @@ public String tokenTransfer( final List signers, final List addressLookupTables) { - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().tokenTransfer( tokenProgram, @@ -278,7 +279,7 @@ public String tokenTransfer( signers, addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String transfer( @@ -288,7 +289,7 @@ public String transfer( final TestKeyPair payer, final List addressLookupTables) { - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().solTransfer( from.getSolana4jPublicKey(), @@ -299,7 +300,7 @@ public String transfer( List.of(payer, from), addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String createAssociatedTokenAccount( @@ -311,7 +312,7 @@ public String createAssociatedTokenAccount( final TestKeyPair payer, final List addressLookupTables) { - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().createAssociatedTokenAccount( tokenProgram, @@ -324,7 +325,7 @@ public String createAssociatedTokenAccount( List.of(payer), addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String setTokenAccountAuthority( @@ -337,7 +338,7 @@ public String setTokenAccountAuthority( final List signers, final List addressLookupTables) { - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().setTokenAccountAuthority( tokenProgram, @@ -350,12 +351,12 @@ public String setTokenAccountAuthority( signers, addressLookupTables); - return solanaApi.sendTransaction(transactionBlob); + return retryingClient(solanaApi.sendTransaction(transactionBlob)); } public String setComputeUnits(final int computeUnitLimit, final long computeUnitPrice, final TestKeyPair payer) { - final Blockhash blockhash = solanaApi.getLatestBlockhash(); + final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash()); final String transactionBlob = getTransactionFactory().setComputeUnits( computeUnitLimit, @@ -416,6 +417,17 @@ private TransactionBlobFactory getTransactionFactory() private T retryingClient(final SolanaClientResponse response) { - return response.getResponse(); + for (int i = 0; i < 10; i++) + { + if (response.getError() == null) + { + return response.getResponse(); + } + else + { + LockSupport.parkNanos(100_000); + } + } + throw new RuntimeException("Unable to get a response after retry."); } } diff --git a/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java b/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java index 1da2fc97..6564dd08 100644 --- a/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java +++ b/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java @@ -166,11 +166,7 @@ public void waitForSlot(final String... args) new RequiredArg("slot") ); - Waiter.waitFor(Condition.isTrue(() -> - { - long slot = solanaDriver.getSlot(); - slot > params.valueAsLong("slot")) - })); + Waiter.waitFor(Condition.isTrue(() -> solanaDriver.getSlot() > params.valueAsLong("slot"))); } public void createMintAccount(final String... args) @@ -308,7 +304,7 @@ public void tokenBalance(final String... args) final TestPublicKey address = testContext.data(TestDataType.TEST_PUBLIC_KEY).lookup(params.value("address")); final String amount = params.value("amount"); - Waiter.waitFor(Condition.isEqualTo(amount, () -> solanaDriver.getTokenBalance(address.getPublicKeyBase58()))); + Waiter.waitFor(Condition.isEqualTo(amount, () -> solanaDriver.getTokenBalance(address.getPublicKeyBase58()).getUiAmountString())); } public void solBalance(final String... args)