From d46994d8f00d32326b5aa1d8fc3372e78ecf13bf Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Sat, 22 Oct 2022 09:20:24 +0530 Subject: [PATCH 01/17] feat: added confirmTransaction --- include/solana.hpp | 4 ++++ lib/solana.cpp | 14 ++++++++++++++ tests/main.cpp | 10 +++------- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/include/solana.hpp b/include/solana.hpp index 24e3016..577e02c 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -507,6 +507,8 @@ class Connection { */ uint64_t getBlockHeight(const std::string &commitment = "finalized") const; + bool confirmTransaction(std::string transactionSignature,uint64_t timeout, std::string confirmLevel) const; + /** * Fetch the current statuses of a batch of signatures */ @@ -559,6 +561,8 @@ class Connection { return {res["context"], res["value"]}; } + + private: const std::string &rpc_url_; const std::string &commitment_; diff --git a/lib/solana.cpp b/lib/solana.cpp index dbe9655..aa9170b 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -495,6 +495,20 @@ Connection::getSignatureStatus(const std::string &signature, return {res.context, res.value[0]}; } +bool Connection::confirmTransaction(std::string transactionSignature, + uint64_t timeout, + std::string confirmLevel) const { + while (timeout>0) { + const auto res = getSignatureStatus(transactionSignature, true).value; + if (res.has_value() && res.value().confirmationStatus == confirmLevel) { + return true; + } + timeout--; + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + return false; +} + } // namespace rpc namespace subscription {} // namespace subscription diff --git a/tests/main.cpp b/tests/main.cpp index 3ca9456..ded5ec1 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -64,13 +64,9 @@ TEST_CASE("Request Airdrop") { // check signature status // this is a temporary fix. This will be changed to the confirmTransaction // function call once it gets implemented - while (timeout > 0) { - const auto res = connection.getSignatureStatus(signature, true).value; - if (res.has_value() && res.value().confirmationStatus == "finalized") { - break; - } - timeout--; - std::this_thread::sleep_for(std::chrono::seconds(1)); + bool confirmation=false; + while(!confirmation){ + confirmation=connection.confirmTransaction(signature,15,"finalized"); } // check if balance is updated after status is finalized const auto new_sol = connection.getBalance(keyPair.publicKey); From b3a97fa20d2032e4422e5900054241ae056f1d6d Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Sat, 22 Oct 2022 09:22:05 +0530 Subject: [PATCH 02/17] refactor: formatting --- include/solana.hpp | 5 ++--- lib/solana.cpp | 4 ++-- tests/main.cpp | 6 +++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/include/solana.hpp b/include/solana.hpp index 577e02c..6eecd3c 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -507,7 +507,8 @@ class Connection { */ uint64_t getBlockHeight(const std::string &commitment = "finalized") const; - bool confirmTransaction(std::string transactionSignature,uint64_t timeout, std::string confirmLevel) const; + bool confirmTransaction(std::string transactionSignature, uint64_t timeout, + std::string confirmLevel) const; /** * Fetch the current statuses of a batch of signatures @@ -561,8 +562,6 @@ class Connection { return {res["context"], res["value"]}; } - - private: const std::string &rpc_url_; const std::string &commitment_; diff --git a/lib/solana.cpp b/lib/solana.cpp index aa9170b..52dcdd8 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -496,9 +496,9 @@ Connection::getSignatureStatus(const std::string &signature, } bool Connection::confirmTransaction(std::string transactionSignature, - uint64_t timeout, + uint64_t timeout, std::string confirmLevel) const { - while (timeout>0) { + while (timeout > 0) { const auto res = getSignatureStatus(transactionSignature, true).value; if (res.has_value() && res.value().confirmationStatus == confirmLevel) { return true; diff --git a/tests/main.cpp b/tests/main.cpp index ded5ec1..be93bef 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -64,9 +64,9 @@ TEST_CASE("Request Airdrop") { // check signature status // this is a temporary fix. This will be changed to the confirmTransaction // function call once it gets implemented - bool confirmation=false; - while(!confirmation){ - confirmation=connection.confirmTransaction(signature,15,"finalized"); + bool confirmation = false; + while (!confirmation) { + confirmation = connection.confirmTransaction(signature, 15, "finalized"); } // check if balance is updated after status is finalized const auto new_sol = connection.getBalance(keyPair.publicKey); From 175ac64288d13ba03359c8456eaab838b5422beb Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Sat, 22 Oct 2022 09:22:38 +0530 Subject: [PATCH 03/17] refactor: use smaller datatypes --- include/solana.hpp | 2 +- lib/solana.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/solana.hpp b/include/solana.hpp index 6eecd3c..7501830 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -507,7 +507,7 @@ class Connection { */ uint64_t getBlockHeight(const std::string &commitment = "finalized") const; - bool confirmTransaction(std::string transactionSignature, uint64_t timeout, + bool confirmTransaction(std::string transactionSignature, uint8_t timeout, std::string confirmLevel) const; /** diff --git a/lib/solana.cpp b/lib/solana.cpp index 52dcdd8..e07c73b 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -496,7 +496,7 @@ Connection::getSignatureStatus(const std::string &signature, } bool Connection::confirmTransaction(std::string transactionSignature, - uint64_t timeout, + uint8_t timeout, std::string confirmLevel) const { while (timeout > 0) { const auto res = getSignatureStatus(transactionSignature, true).value; From 4ea4379d876956392384e42973efdcf8f904b86b Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Sat, 22 Oct 2022 09:30:21 +0530 Subject: [PATCH 04/17] doc: added documentaion for confirm transaction --- include/solana.hpp | 3 +++ tests/main.cpp | 6 ++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/solana.hpp b/include/solana.hpp index 7501830..cb72b01 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -507,6 +507,9 @@ class Connection { */ uint64_t getBlockHeight(const std::string &commitment = "finalized") const; + /** + * Returns of the current Transaction has been confirmed or not + */ bool confirmTransaction(std::string transactionSignature, uint8_t timeout, std::string confirmLevel) const; diff --git a/tests/main.cpp b/tests/main.cpp index be93bef..49dd564 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -54,16 +54,14 @@ TEST_CASE("Simulate & Send Transaction") { CHECK_EQ(transactionSignature, b58Sig); } -TEST_CASE("Request Airdrop") { +TEST_CASE("Request Airdrop & Confirm Transaction") { const solana::Keypair keyPair = solana::Keypair::fromFile(KEY_PAIR_FILE); const auto connection = solana::rpc::Connection(solana::DEVNET); // request Airdrop const auto prev_sol = connection.getBalance(keyPair.publicKey); const auto signature = connection.requestAirdrop(keyPair.publicKey, 50001); uint8_t timeout = 15; - // check signature status - // this is a temporary fix. This will be changed to the confirmTransaction - // function call once it gets implemented + // using confirmTransaction to check if the airdrop went through bool confirmation = false; while (!confirmation) { confirmation = connection.confirmTransaction(signature, 15, "finalized"); From 608b651be66f4a094e81556f9ebc2a9fb372a765 Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Sat, 22 Oct 2022 15:50:06 +0530 Subject: [PATCH 05/17] fix: remove timeout in the confirmTransaction function --- include/solana.hpp | 2 +- lib/solana.cpp | 9 ++------- tests/main.cpp | 8 +++++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/include/solana.hpp b/include/solana.hpp index cb72b01..0fdd1e1 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -510,7 +510,7 @@ class Connection { /** * Returns of the current Transaction has been confirmed or not */ - bool confirmTransaction(std::string transactionSignature, uint8_t timeout, + bool confirmTransaction(std::string transactionSignature, std::string confirmLevel) const; /** diff --git a/lib/solana.cpp b/lib/solana.cpp index e07c73b..4ccc1f1 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -496,16 +496,11 @@ Connection::getSignatureStatus(const std::string &signature, } bool Connection::confirmTransaction(std::string transactionSignature, - uint8_t timeout, std::string confirmLevel) const { - while (timeout > 0) { + const auto res = getSignatureStatus(transactionSignature, true).value; - if (res.has_value() && res.value().confirmationStatus == confirmLevel) { + if (res.has_value() && res.value().confirmationStatus == confirmLevel) return true; - } - timeout--; - std::this_thread::sleep_for(std::chrono::seconds(1)); - } return false; } diff --git a/tests/main.cpp b/tests/main.cpp index 49dd564..97133b6 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -62,9 +62,11 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { const auto signature = connection.requestAirdrop(keyPair.publicKey, 50001); uint8_t timeout = 15; // using confirmTransaction to check if the airdrop went through - bool confirmation = false; - while (!confirmation) { - confirmation = connection.confirmTransaction(signature, 15, "finalized"); + bool confirmed = false; + while (timeout>0 && !confirmed) { + confirmed = connection.confirmTransaction(signature, "finalized"); + timeout--; + std::this_thread::sleep_for(std::chrono::seconds(1)); } // check if balance is updated after status is finalized const auto new_sol = connection.getBalance(keyPair.publicKey); From 4d293a3cae6da7a70b072150c7f9ac21e7871b01 Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Sat, 22 Oct 2022 15:50:52 +0530 Subject: [PATCH 06/17] refactor: formatting --- lib/solana.cpp | 7 +++---- tests/main.cpp | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/solana.cpp b/lib/solana.cpp index 4ccc1f1..b61adb3 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -497,10 +497,9 @@ Connection::getSignatureStatus(const std::string &signature, bool Connection::confirmTransaction(std::string transactionSignature, std::string confirmLevel) const { - - const auto res = getSignatureStatus(transactionSignature, true).value; - if (res.has_value() && res.value().confirmationStatus == confirmLevel) - return true; + const auto res = getSignatureStatus(transactionSignature, true).value; + if (res.has_value() && res.value().confirmationStatus == confirmLevel) + return true; return false; } diff --git a/tests/main.cpp b/tests/main.cpp index 97133b6..177a2f3 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -63,7 +63,7 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { uint8_t timeout = 15; // using confirmTransaction to check if the airdrop went through bool confirmed = false; - while (timeout>0 && !confirmed) { + while (timeout > 0 && !confirmed) { confirmed = connection.confirmTransaction(signature, "finalized"); timeout--; std::this_thread::sleep_for(std::chrono::seconds(1)); From e489be25727b1c869e382dc109da844d4fd7e226 Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Mon, 24 Oct 2022 15:34:42 +0530 Subject: [PATCH 07/17] chore: rewrite confirmtransaction based on block height --- include/solana.hpp | 2 ++ lib/solana.cpp | 18 +++++++++++++++--- tests/main.cpp | 8 ++------ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/solana.hpp b/include/solana.hpp index 0fdd1e1..c856dc9 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -22,6 +22,8 @@ const std::string MEMO_PROGRAM_ID = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"; const std::string MAINNET_BETA = "https://api.mainnet-beta.solana.com"; const std::string DEVNET = "https://api.devnet.solana.com"; +const int MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION = 152; + struct PublicKey { static const auto SIZE = crypto_sign_PUBLICKEYBYTES; diff --git a/lib/solana.cpp b/lib/solana.cpp index b61adb3..6df9ace 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -1,12 +1,14 @@ #include #include +#include #include #include #include #include #include #include +#include #include #include "base64.hpp" @@ -497,9 +499,19 @@ Connection::getSignatureStatus(const std::string &signature, bool Connection::confirmTransaction(std::string transactionSignature, std::string confirmLevel) const { - const auto res = getSignatureStatus(transactionSignature, true).value; - if (res.has_value() && res.value().confirmationStatus == confirmLevel) - return true; + const auto timeoutBlockheight = + getLatestBlockhash(confirmLevel).lastValidBlockHeight + + solana::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; + auto currentBlockheight = getBlockHeight(confirmLevel); + + while(timeoutBlockheight > currentBlockheight) { + const auto res = getSignatureStatus(transactionSignature, true); + if (res.value.has_value() && res.value.value().confirmationStatus==confirmLevel) { + return true; + } + std::this_thread::sleep_for(std::chrono::seconds(1)); + currentBlockheight=getBlockHeight(confirmLevel); + } return false; } diff --git a/tests/main.cpp b/tests/main.cpp index 177a2f3..805122c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -63,13 +63,9 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { uint8_t timeout = 15; // using confirmTransaction to check if the airdrop went through bool confirmed = false; - while (timeout > 0 && !confirmed) { - confirmed = connection.confirmTransaction(signature, "finalized"); - timeout--; - std::this_thread::sleep_for(std::chrono::seconds(1)); - } - // check if balance is updated after status is finalized + confirmed = connection.confirmTransaction(signature, "finalized"); const auto new_sol = connection.getBalance(keyPair.publicKey); + CHECK_EQ(confirmed, true); CHECK_GT(new_sol, prev_sol); } From a3f554b1f5a49ba01d2b5d20baea832c2ef907fd Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Tue, 25 Oct 2022 10:50:39 +0530 Subject: [PATCH 08/17] refactor: rewrite to use the Commitment enums --- examples/sendTransaction.cpp | 24 +++++------------------- include/solana.hpp | 4 ++-- lib/solana.cpp | 6 +++--- tests/main.cpp | 3 ++- 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/examples/sendTransaction.cpp b/examples/sendTransaction.cpp index e044e38..72559c8 100644 --- a/examples/sendTransaction.cpp +++ b/examples/sendTransaction.cpp @@ -42,24 +42,10 @@ int main() { const auto timeoutBlockHeight = recentBlockHash.lastValidBlockHeight + mango_v3::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; - uint8_t timeout = 15; // add a 15 sec timeout - while (timeout > 0) { - // Check if we are past validBlockHeight - auto currentBlockHeight = - connection.getBlockHeight(solana::Commitment::CONFIRMED); - if (timeoutBlockHeight <= currentBlockHeight) { - spdlog::error("Timed out for txid: {}, current BlockHeight: {}", b58Sig, - currentBlockHeight); - return EXIT_FAILURE; - } - const auto res = connection.getSignatureStatus(b58Sig).value; - if (res.has_value() && !res.value().err.has_value() && - res.value().confirmationStatus == "finalized") { - break; - } - timeout--; - std::this_thread::sleep_for(std::chrono::seconds(1)); - } + bool confirmed = false; + confirmed = + connection.confirmTransaction(b58Sig, solana::Commitment::FINALIZED); + if (confirmed) return EXIT_SUCCESS; - return EXIT_SUCCESS; + return EXIT_FAILURE; } diff --git a/include/solana.hpp b/include/solana.hpp index 3ec3363..763305d 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -180,7 +180,7 @@ struct SignatureStatus { std::optional err = std::nullopt; /** cluster confirmation status, if data available. Possible responses: * `processed`, `confirmed`, `finalized` */ - std::string confirmationStatus; + Commitment confirmationStatus; }; /** @@ -538,7 +538,7 @@ class Connection { * Returns of the current Transaction has been confirmed or not */ bool confirmTransaction(std::string transactionSignature, - std::string confirmLevel) const; + Commitment confirmLevel) const; /** * Fetch the current statuses of a batch of signatures diff --git a/lib/solana.cpp b/lib/solana.cpp index 9b4a2ae..4cd1ddf 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -497,15 +497,15 @@ Connection::getSignatureStatus(const std::string &signature, } bool Connection::confirmTransaction(std::string transactionSignature, - std::string confirmLevel) const { + Commitment confirmLevel) const { const auto timeoutBlockheight = getLatestBlockhash(confirmLevel).lastValidBlockHeight + solana::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; auto currentBlockheight = getBlockHeight(confirmLevel); while(timeoutBlockheight > currentBlockheight) { - const auto res = getSignatureStatus(transactionSignature, true); - if (res.value.has_value() && res.value.value().confirmationStatus==confirmLevel) { + const auto res = getSignatureStatus(transactionSignature, true).value; + if (res.has_value() && res.value().confirmationStatus==confirmLevel) { return true; } std::this_thread::sleep_for(std::chrono::seconds(1)); diff --git a/tests/main.cpp b/tests/main.cpp index 805122c..d666efd 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -63,7 +63,8 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { uint8_t timeout = 15; // using confirmTransaction to check if the airdrop went through bool confirmed = false; - confirmed = connection.confirmTransaction(signature, "finalized"); + confirmed = + connection.confirmTransaction(signature, solana::Commitment::FINALIZED); const auto new_sol = connection.getBalance(keyPair.publicKey); CHECK_EQ(confirmed, true); CHECK_GT(new_sol, prev_sol); From 8a11dfb4a7aeb543a902f33ee5637b37405422c8 Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Wed, 26 Oct 2022 09:39:21 +0530 Subject: [PATCH 09/17] fix: added timeout for confirmTransaction --- examples/sendTransaction.cpp | 2 +- include/solana.hpp | 3 +-- lib/solana.cpp | 10 +++++++--- tests/main.cpp | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/sendTransaction.cpp b/examples/sendTransaction.cpp index 72559c8..d0db857 100644 --- a/examples/sendTransaction.cpp +++ b/examples/sendTransaction.cpp @@ -44,7 +44,7 @@ int main() { mango_v3::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; bool confirmed = false; confirmed = - connection.confirmTransaction(b58Sig, solana::Commitment::FINALIZED); + connection.confirmTransaction(b58Sig, 15, solana::Commitment::FINALIZED); if (confirmed) return EXIT_SUCCESS; return EXIT_FAILURE; diff --git a/include/solana.hpp b/include/solana.hpp index 763305d..73af63a 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -24,7 +24,6 @@ const std::string MAINNET_BETA = "https://api.mainnet-beta.solana.com"; const std::string DEVNET = "https://api.devnet.solana.com"; const int MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION = 152; - struct PublicKey { static const auto SIZE = crypto_sign_PUBLICKEYBYTES; typedef std::array array_t; @@ -537,7 +536,7 @@ class Connection { /** * Returns of the current Transaction has been confirmed or not */ - bool confirmTransaction(std::string transactionSignature, + bool confirmTransaction(std::string transactionSignature, uint8_t timeout, Commitment confirmLevel) const; /** diff --git a/lib/solana.cpp b/lib/solana.cpp index 4cd1ddf..1222085 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -497,19 +497,23 @@ Connection::getSignatureStatus(const std::string &signature, } bool Connection::confirmTransaction(std::string transactionSignature, + uint8_t timeout, Commitment confirmLevel) const { const auto timeoutBlockheight = getLatestBlockhash(confirmLevel).lastValidBlockHeight + solana::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; auto currentBlockheight = getBlockHeight(confirmLevel); - while(timeoutBlockheight > currentBlockheight) { + while (timeout > 0) { const auto res = getSignatureStatus(transactionSignature, true).value; - if (res.has_value() && res.value().confirmationStatus==confirmLevel) { + if (timeoutBlockheight <= currentBlockheight) + throw std::runtime_error("Transaction timeout"); + if (res.has_value() && res.value().confirmationStatus == confirmLevel) { return true; } std::this_thread::sleep_for(std::chrono::seconds(1)); - currentBlockheight=getBlockHeight(confirmLevel); + currentBlockheight = getBlockHeight(confirmLevel); + timeout--; } return false; } diff --git a/tests/main.cpp b/tests/main.cpp index d666efd..f00f992 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -64,7 +64,7 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { // using confirmTransaction to check if the airdrop went through bool confirmed = false; confirmed = - connection.confirmTransaction(signature, solana::Commitment::FINALIZED); + connection.confirmTransaction(signature,15, solana::Commitment::FINALIZED); const auto new_sol = connection.getBalance(keyPair.publicKey); CHECK_EQ(confirmed, true); CHECK_GT(new_sol, prev_sol); From ffe8e14c4620dc1114997e90e2fc770c3dee56d4 Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Wed, 26 Oct 2022 18:24:43 +0530 Subject: [PATCH 10/17] refactor: change default timeout to be about 3 mins and changed sleep to 500 ms --- examples/sendTransaction.cpp | 2 +- include/solana.hpp | 4 ++-- lib/solana.cpp | 9 ++++----- tests/main.cpp | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/examples/sendTransaction.cpp b/examples/sendTransaction.cpp index d0db857..72559c8 100644 --- a/examples/sendTransaction.cpp +++ b/examples/sendTransaction.cpp @@ -44,7 +44,7 @@ int main() { mango_v3::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; bool confirmed = false; confirmed = - connection.confirmTransaction(b58Sig, 15, solana::Commitment::FINALIZED); + connection.confirmTransaction(b58Sig, solana::Commitment::FINALIZED); if (confirmed) return EXIT_SUCCESS; return EXIT_FAILURE; diff --git a/include/solana.hpp b/include/solana.hpp index 73af63a..4bd8fd4 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -536,8 +536,8 @@ class Connection { /** * Returns of the current Transaction has been confirmed or not */ - bool confirmTransaction(std::string transactionSignature, uint8_t timeout, - Commitment confirmLevel) const; + bool confirmTransaction(std::string transactionSignature, + Commitment confirmLevel, uint8_t timeout=200) const; /** * Fetch the current statuses of a batch of signatures diff --git a/lib/solana.cpp b/lib/solana.cpp index 1222085..374b502 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -496,14 +496,13 @@ Connection::getSignatureStatus(const std::string &signature, return {res.context, res.value[0]}; } -bool Connection::confirmTransaction(std::string transactionSignature, - uint8_t timeout, - Commitment confirmLevel) const { +bool Connection::confirmTransaction(std::string transactionSignature, Commitment confirmLevel, + uint8_t timeout) const { const auto timeoutBlockheight = getLatestBlockhash(confirmLevel).lastValidBlockHeight + solana::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; auto currentBlockheight = getBlockHeight(confirmLevel); - + timeout = timeout * 2;//since we are checking every 500ms while (timeout > 0) { const auto res = getSignatureStatus(transactionSignature, true).value; if (timeoutBlockheight <= currentBlockheight) @@ -511,7 +510,7 @@ bool Connection::confirmTransaction(std::string transactionSignature, if (res.has_value() && res.value().confirmationStatus == confirmLevel) { return true; } - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); currentBlockheight = getBlockHeight(confirmLevel); timeout--; } diff --git a/tests/main.cpp b/tests/main.cpp index f00f992..d666efd 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -64,7 +64,7 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { // using confirmTransaction to check if the airdrop went through bool confirmed = false; confirmed = - connection.confirmTransaction(signature,15, solana::Commitment::FINALIZED); + connection.confirmTransaction(signature, solana::Commitment::FINALIZED); const auto new_sol = connection.getBalance(keyPair.publicKey); CHECK_EQ(confirmed, true); CHECK_GT(new_sol, prev_sol); From 07c78d242fc7f773649ba2e5d4c3f50915a8206c Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Wed, 26 Oct 2022 19:17:38 +0530 Subject: [PATCH 11/17] refactor: add a timeout of about 2 mins to tests and example --- examples/sendTransaction.cpp | 2 +- tests/main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/sendTransaction.cpp b/examples/sendTransaction.cpp index 72559c8..414b306 100644 --- a/examples/sendTransaction.cpp +++ b/examples/sendTransaction.cpp @@ -44,7 +44,7 @@ int main() { mango_v3::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; bool confirmed = false; confirmed = - connection.confirmTransaction(b58Sig, solana::Commitment::FINALIZED); + connection.confirmTransaction(b58Sig,140, solana::Commitment::FINALIZED); if (confirmed) return EXIT_SUCCESS; return EXIT_FAILURE; diff --git a/tests/main.cpp b/tests/main.cpp index d666efd..f609d53 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -60,7 +60,7 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { // request Airdrop const auto prev_sol = connection.getBalance(keyPair.publicKey); const auto signature = connection.requestAirdrop(keyPair.publicKey, 50001); - uint8_t timeout = 15; + uint8_t timeout = 140; // using confirmTransaction to check if the airdrop went through bool confirmed = false; confirmed = From 4de8670784a4c1b02ba088153efe8e76d9792e87 Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Wed, 26 Oct 2022 19:32:09 +0530 Subject: [PATCH 12/17] fix: wrong method params --- examples/sendTransaction.cpp | 2 +- tests/main.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/sendTransaction.cpp b/examples/sendTransaction.cpp index 414b306..d747850 100644 --- a/examples/sendTransaction.cpp +++ b/examples/sendTransaction.cpp @@ -44,7 +44,7 @@ int main() { mango_v3::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; bool confirmed = false; confirmed = - connection.confirmTransaction(b58Sig,140, solana::Commitment::FINALIZED); + connection.confirmTransaction(b58Sig, solana::Commitment::FINALIZED,140); if (confirmed) return EXIT_SUCCESS; return EXIT_FAILURE; diff --git a/tests/main.cpp b/tests/main.cpp index f609d53..987f46e 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -63,8 +63,8 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { uint8_t timeout = 140; // using confirmTransaction to check if the airdrop went through bool confirmed = false; - confirmed = - connection.confirmTransaction(signature, solana::Commitment::FINALIZED); + confirmed = connection.confirmTransaction( + signature, solana::Commitment::FINALIZED, timeout); const auto new_sol = connection.getBalance(keyPair.publicKey); CHECK_EQ(confirmed, true); CHECK_GT(new_sol, prev_sol); From 7631d4d63102dfc213a2a31d5e4a5306fe4fd173 Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Thu, 27 Oct 2022 08:30:24 +0530 Subject: [PATCH 13/17] fix: failing CI/CD --- lib/solana.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/solana.cpp b/lib/solana.cpp index 72dbb23..2c3774f 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -503,14 +503,14 @@ Connection::getSignatureStatus(const std::string &signature, return {res.context, res.value[0]}; } - -bool Connection::confirmTransaction(std::string transactionSignature, Commitment confirmLevel, +bool Connection::confirmTransaction(std::string transactionSignature, + Commitment confirmLevel, uint8_t timeout) const { const auto timeoutBlockheight = getLatestBlockhash(confirmLevel).lastValidBlockHeight + solana::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; auto currentBlockheight = getBlockHeight(confirmLevel); - timeout = timeout * 2;//since we are checking every 500ms + timeout = timeout * 2; // since we are checking every 500ms while (timeout > 0) { const auto res = getSignatureStatus(transactionSignature, true).value; if (timeoutBlockheight <= currentBlockheight) @@ -523,6 +523,7 @@ bool Connection::confirmTransaction(std::string transactionSignature, Commitment timeout--; } return false; +} Version Connection::getVersion() const { // create request @@ -538,7 +539,6 @@ uint64_t Connection::getFirstAvailableBlock() const { const json reqJson = jsonRequest("getFirstAvailableBlock", params); // send jsonRpc request return sendJsonRpcRequest(reqJson); - } } // namespace rpc From 0c351617567eefb7c7e7d196e9a924cfae93d7bd Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Thu, 27 Oct 2022 12:19:50 +0530 Subject: [PATCH 14/17] fix: overflow error with timeout --- include/solana.hpp | 2 +- lib/solana.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/solana.hpp b/include/solana.hpp index 7e48a2f..9052fae 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -547,7 +547,7 @@ class Connection { * Returns of the current Transaction has been confirmed or not */ bool confirmTransaction(std::string transactionSignature, - Commitment confirmLevel, uint8_t timeout=200) const; + Commitment confirmLevel, uint16_t timeout=200) const; /** * Fetch the current statuses of a batch of signatures diff --git a/lib/solana.cpp b/lib/solana.cpp index 2c3774f..fdf916b 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -505,7 +505,7 @@ Connection::getSignatureStatus(const std::string &signature, bool Connection::confirmTransaction(std::string transactionSignature, Commitment confirmLevel, - uint8_t timeout) const { + uint16_t timeout) const { const auto timeoutBlockheight = getLatestBlockhash(confirmLevel).lastValidBlockHeight + solana::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; From 22b1bf3d2f4f341c0f898e0ffeb074beede5385a Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Thu, 27 Oct 2022 21:11:01 +0530 Subject: [PATCH 15/17] refactor: rename timeout to retries --- lib/solana.cpp | 7 +++---- tests/main.cpp | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/solana.cpp b/lib/solana.cpp index fdf916b..43f0551 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -505,13 +505,12 @@ Connection::getSignatureStatus(const std::string &signature, bool Connection::confirmTransaction(std::string transactionSignature, Commitment confirmLevel, - uint16_t timeout) const { + uint16_t retries) const { const auto timeoutBlockheight = getLatestBlockhash(confirmLevel).lastValidBlockHeight + solana::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; auto currentBlockheight = getBlockHeight(confirmLevel); - timeout = timeout * 2; // since we are checking every 500ms - while (timeout > 0) { + while (retries > 0) { const auto res = getSignatureStatus(transactionSignature, true).value; if (timeoutBlockheight <= currentBlockheight) throw std::runtime_error("Transaction timeout"); @@ -520,7 +519,7 @@ bool Connection::confirmTransaction(std::string transactionSignature, } std::this_thread::sleep_for(std::chrono::milliseconds(500)); currentBlockheight = getBlockHeight(confirmLevel); - timeout--; + retries--; } return false; } diff --git a/tests/main.cpp b/tests/main.cpp index 9bf4c7e..4ef38d6 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -61,7 +61,7 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { // request Airdrop const auto prev_sol = connection.getBalance(keyPair.publicKey); const auto signature = connection.requestAirdrop(keyPair.publicKey, 50001); - uint8_t timeout = 140; + uint8_t retries = 140; // using confirmTransaction to check if the airdrop went through bool confirmed = false; confirmed = connection.confirmTransaction( From bcfa1bb684cca624e7a35baa91ed02b41ca1fe3b Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Thu, 27 Oct 2022 21:17:15 +0530 Subject: [PATCH 16/17] fix: wrong method params --- tests/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/main.cpp b/tests/main.cpp index 4ef38d6..a01eae2 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -65,7 +65,7 @@ TEST_CASE("Request Airdrop & Confirm Transaction") { // using confirmTransaction to check if the airdrop went through bool confirmed = false; confirmed = connection.confirmTransaction( - signature, solana::Commitment::FINALIZED, timeout); + signature, solana::Commitment::FINALIZED, retries); const auto new_sol = connection.getBalance(keyPair.publicKey); CHECK_EQ(confirmed, true); CHECK_GT(new_sol, prev_sol); From b68512d97325259355d92ca7a2df44884cda55b6 Mon Sep 17 00:00:00 2001 From: Lioncat2002 Date: Fri, 28 Oct 2022 14:47:45 +0530 Subject: [PATCH 17/17] fix: move currentblockheight into while loop --- lib/solana.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/solana.cpp b/lib/solana.cpp index 43f0551..6254e72 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -509,8 +509,8 @@ bool Connection::confirmTransaction(std::string transactionSignature, const auto timeoutBlockheight = getLatestBlockhash(confirmLevel).lastValidBlockHeight + solana::MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION; - auto currentBlockheight = getBlockHeight(confirmLevel); while (retries > 0) { + auto currentBlockheight = getBlockHeight(confirmLevel); const auto res = getSignatureStatus(transactionSignature, true).value; if (timeoutBlockheight <= currentBlockheight) throw std::runtime_error("Transaction timeout"); @@ -518,7 +518,7 @@ bool Connection::confirmTransaction(std::string transactionSignature, return true; } std::this_thread::sleep_for(std::chrono::milliseconds(500)); - currentBlockheight = getBlockHeight(confirmLevel); + retries--; } return false;