diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d310b650..3698d84fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## v2.1.5-beta.2 + +### Fixed + + * Executiong should retry on status `PLATFORM_TRANSACTION_NOT_CREATED` + * Error handling throughout the SDK + * A precheck error shoudl be returned when the exceptional status is in the header + * A receipt error should be returned when the exceptional status is in the receipt + ## v2.1.5-beta.1 ### Fixed diff --git a/account_balance_query.go b/account_balance_query.go index a45702a15..9d3298853 100644 --- a/account_balance_query.go +++ b/account_balance_query.go @@ -98,12 +98,12 @@ func (query *AccountBalanceQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + accountBalanceQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, accountBalanceQuery_getMethod, - accountBalanceQuery_mapResponseStatus, + accountBalanceQuery_mapStatusError, query_mapResponse, ) @@ -115,8 +115,14 @@ func (query *AccountBalanceQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func accountBalanceQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetCryptogetAccountBalance().Header.NodeTransactionPrecheckCode) +func accountBalanceQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetCryptogetAccountBalance().Header.NodeTransactionPrecheckCode)) +} + +func accountBalanceQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetCryptogetAccountBalance().Header.NodeTransactionPrecheckCode), + } } func accountBalanceQuery_getMethod(_ request, channel *channel) method { @@ -139,12 +145,12 @@ func (query *AccountBalanceQuery) Execute(client *Client) (AccountBalance, error request{ query: &query.Query, }, - query_shouldRetry, + accountBalanceQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, accountBalanceQuery_getMethod, - accountBalanceQuery_mapResponseStatus, + accountBalanceQuery_mapStatusError, query_mapResponse, ) diff --git a/account_create_transaction.go b/account_create_transaction.go index cb69317c6..e71877aa7 100644 --- a/account_create_transaction.go +++ b/account_create_transaction.go @@ -282,7 +282,7 @@ func (transaction *AccountCreateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, accountCreateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/account_delete_transaction.go b/account_delete_transaction.go index f673fe249..d4dbdb8f9 100644 --- a/account_delete_transaction.go +++ b/account_delete_transaction.go @@ -188,7 +188,7 @@ func (transaction *AccountDeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, accountDeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/account_info_query.go b/account_info_query.go index a3a2795fa..e5e310d63 100644 --- a/account_info_query.go +++ b/account_info_query.go @@ -23,8 +23,14 @@ func NewAccountInfoQuery() *AccountInfoQuery { } } -func accountInfoQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetCryptoGetInfo().Header.NodeTransactionPrecheckCode) +func accountInfoQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetCryptoGetInfo().Header.NodeTransactionPrecheckCode)) +} + +func accountInfoQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetCryptoGetInfo().Header.NodeTransactionPrecheckCode), + } } func accountInfoQuery_getMethod(_ request, channel *channel) method { @@ -58,12 +64,12 @@ func (query *AccountInfoQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + accountInfoQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, accountInfoQuery_getMethod, - accountInfoQuery_mapResponseStatus, + accountInfoQuery_mapStatusError, query_mapResponse, ) @@ -157,12 +163,12 @@ func (query *AccountInfoQuery) Execute(client *Client) (AccountInfo, error) { request{ query: &query.Query, }, - query_shouldRetry, + accountInfoQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, accountInfoQuery_getMethod, - accountInfoQuery_mapResponseStatus, + accountInfoQuery_mapStatusError, query_mapResponse, ) diff --git a/account_records_query.go b/account_records_query.go index c35bef399..48e1885be 100644 --- a/account_records_query.go +++ b/account_records_query.go @@ -63,12 +63,12 @@ func (query *AccountRecordsQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + accountRecordsQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, accountRecordsQuery_getMethod, - accountRecordsQuery_mapResponseStatus, + accountRecordsQuery_mapStatusError, query_mapResponse, ) @@ -80,8 +80,14 @@ func (query *AccountRecordsQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func accountRecordsQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetCryptoGetAccountRecords().Header.NodeTransactionPrecheckCode) +func accountRecordsQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetCryptoGetAccountRecords().Header.NodeTransactionPrecheckCode)) +} + +func accountRecordsQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetCryptoGetAccountRecords().Header.NodeTransactionPrecheckCode), + } } func accountRecordsQuery_getMethod(_ request, channel *channel) method { @@ -139,12 +145,12 @@ func (query *AccountRecordsQuery) Execute(client *Client) ([]TransactionRecord, request{ query: &query.Query, }, - query_shouldRetry, + accountRecordsQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, accountRecordsQuery_getMethod, - accountRecordsQuery_mapResponseStatus, + accountRecordsQuery_mapStatusError, query_mapResponse, ) diff --git a/account_stakers_query.go b/account_stakers_query.go index 5fb4cf779..b7b19513a 100644 --- a/account_stakers_query.go +++ b/account_stakers_query.go @@ -63,12 +63,12 @@ func (query *AccountStakersQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + accountStakersQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, accountStakersQuery_getMethod, - accountStakersQuery_mapResponseStatus, + accountStakersQuery_mapStatusError, query_mapResponse, ) @@ -80,8 +80,14 @@ func (query *AccountStakersQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func accountStakersQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetCryptoGetProxyStakers().Header.NodeTransactionPrecheckCode) +func accountStakersQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetCryptoGetProxyStakers().Header.NodeTransactionPrecheckCode)) +} + +func accountStakersQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetCryptoGetProxyStakers().Header.NodeTransactionPrecheckCode), + } } func accountStakersQuery_getMethod(_ request, channel *channel) method { @@ -137,12 +143,12 @@ func (query *AccountStakersQuery) Execute(client *Client) ([]Transfer, error) { request{ query: &query.Query, }, - query_shouldRetry, + accountStakersQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, accountStakersQuery_getMethod, - accountStakersQuery_mapResponseStatus, + accountStakersQuery_mapStatusError, query_mapResponse, ) diff --git a/account_update_transaction.go b/account_update_transaction.go index 4f84cd317..24290e78c 100644 --- a/account_update_transaction.go +++ b/account_update_transaction.go @@ -248,7 +248,7 @@ func (transaction *AccountUpdateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, accountUpdateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/contract_bytecode_query.go b/contract_bytecode_query.go index ecff818c5..3b674cecd 100644 --- a/contract_bytecode_query.go +++ b/contract_bytecode_query.go @@ -55,12 +55,12 @@ func (query *ContractBytecodeQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + contractBytecodeQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, contractBytecodeQuery_getMethod, - contractBytecodeQuery_mapResponseStatus, + contractBytecodeQuery_mapStatusError, query_mapResponse, ) @@ -72,8 +72,14 @@ func (query *ContractBytecodeQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func contractBytecodeQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetContractGetBytecodeResponse().Header.NodeTransactionPrecheckCode) +func contractBytecodeQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetContractGetBytecodeResponse().Header.NodeTransactionPrecheckCode)) +} + +func contractBytecodeQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetContractGetBytecodeResponse().Header.NodeTransactionPrecheckCode), + } } func contractBytecodeQuery_getMethod(_ request, channel *channel) method { @@ -129,12 +135,12 @@ func (query *ContractBytecodeQuery) Execute(client *Client) ([]byte, error) { request{ query: &query.Query, }, - query_shouldRetry, + contractBytecodeQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, contractBytecodeQuery_getMethod, - contractBytecodeQuery_mapResponseStatus, + contractBytecodeQuery_mapStatusError, query_mapResponse, ) diff --git a/contract_call_query.go b/contract_call_query.go index d630b63f9..ca1de3cd0 100644 --- a/contract_call_query.go +++ b/contract_call_query.go @@ -100,12 +100,12 @@ func (query *ContractCallQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + contractCallQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, contractCallQuery_getMethod, - contractCallQuery_mapResponseStatus, + contractCallQuery_mapStatusError, query_mapResponse, ) @@ -117,8 +117,14 @@ func (query *ContractCallQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func contractCallQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetContractCallLocal().Header.NodeTransactionPrecheckCode) +func contractCallQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetContractCallLocal().Header.NodeTransactionPrecheckCode)) +} + +func contractCallQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetContractCallLocal().Header.NodeTransactionPrecheckCode), + } } func contractCallQuery_getMethod(_ request, channel *channel) method { @@ -174,12 +180,12 @@ func (query *ContractCallQuery) Execute(client *Client) (ContractFunctionResult, request{ query: &query.Query, }, - query_shouldRetry, + contractCallQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, contractCallQuery_getMethod, - contractCallQuery_mapResponseStatus, + contractCallQuery_mapStatusError, query_mapResponse, ) diff --git a/contract_create_transaction.go b/contract_create_transaction.go index cd802c8a3..5b6be758e 100644 --- a/contract_create_transaction.go +++ b/contract_create_transaction.go @@ -278,7 +278,7 @@ func (transaction *ContractCreateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, contractCreateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/contract_delete_transaction.go b/contract_delete_transaction.go index 06100ed75..ba2374e13 100644 --- a/contract_delete_transaction.go +++ b/contract_delete_transaction.go @@ -197,7 +197,7 @@ func (transaction *ContractDeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, contractDeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/contract_execute_transaction.go b/contract_execute_transaction.go index 7ed840e18..a3d7223a2 100644 --- a/contract_execute_transaction.go +++ b/contract_execute_transaction.go @@ -224,7 +224,7 @@ func (transaction *ContractExecuteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, contractExecuteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/contract_info_query.go b/contract_info_query.go index 0ad445763..ed1e5d759 100644 --- a/contract_info_query.go +++ b/contract_info_query.go @@ -58,12 +58,12 @@ func (query *ContractInfoQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + contractInfoQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, contractInfoQuery_getMethod, - contractInfoQuery_mapResponseStatus, + contractInfoQuery_mapStatusError, query_mapResponse, ) @@ -79,8 +79,14 @@ func (query *ContractInfoQuery) GetCost(client *Client) (Hbar, error) { } } -func contractInfoQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetContractGetInfo().Header.NodeTransactionPrecheckCode) +func contractInfoQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetContractGetInfo().Header.NodeTransactionPrecheckCode)) +} + +func contractInfoQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetContractGetInfo().Header.NodeTransactionPrecheckCode), + } } func contractInfoQuery_getMethod(_ request, channel *channel) method { @@ -136,12 +142,12 @@ func (query *ContractInfoQuery) Execute(client *Client) (ContractInfo, error) { request{ query: &query.Query, }, - query_shouldRetry, + contractInfoQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, contractInfoQuery_getMethod, - contractInfoQuery_mapResponseStatus, + contractInfoQuery_mapStatusError, query_mapResponse, ) diff --git a/contract_update_transaction.go b/contract_update_transaction.go index c957646f0..f121c9a10 100644 --- a/contract_update_transaction.go +++ b/contract_update_transaction.go @@ -281,7 +281,7 @@ func (transaction *ContractUpdateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, contractUpdateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/executable.go b/executable.go index 3f4808802..c8f3b3fc9 100644 --- a/executable.go +++ b/executable.go @@ -14,6 +14,14 @@ import ( const maxAttempts = 10 +type executionState uint32 + +const ( + executionStateRetry executionState = 0 + executionStateFinished executionState = 1 + executionStateError executionState = 2 +) + type method struct { query func( context.Context, @@ -54,18 +62,17 @@ type request struct { func execute( client *Client, request request, - shouldRetry func(Status, response) bool, + shouldRetry func(request, response) executionState, makeRequest func(request) protoRequest, advanceRequest func(request), getNodeAccountID func(request) AccountID, getMethod func(request, *channel) method, - mapResponseStatus func(request, response) Status, + mapStatusError func(request, response) error, mapResponse func(request, response, AccountID, protoRequest) (intermediateResponse, error), ) (intermediateResponse, error) { maxAttempts := 10 var attempt int64 var errPersistent error - var responseStatus Status if request.query != nil { maxAttempts = request.query.maxRetry @@ -116,22 +123,22 @@ func execute( node.decreaseDelay() - responseStatus = mapResponseStatus(request, resp) + retry := shouldRetry(request, resp) - if shouldRetry(responseStatus, resp) && attempt <= int64(maxAttempts) { - delayForAttempt(attempt) - continue - } - - if responseStatus != StatusOk && responseStatus != StatusSuccess { - if request.query != nil { - return intermediateResponse{}, newErrHederaPreCheckStatus(TransactionID{}, responseStatus) + switch retry { + case executionStateRetry: + if attempt <= int64(maxAttempts) { + delayForAttempt(attempt) + continue } else { - return intermediateResponse{}, newErrHederaPreCheckStatus(request.transaction.GetTransactionID(), responseStatus) + errPersistent = mapStatusError(request, resp) + break } + case executionStateError: + return intermediateResponse{}, mapStatusError(request, resp) + case executionStateFinished: + return mapResponse(request, resp, node.accountID, protoRequest) } - - return mapResponse(request, resp, node.accountID, protoRequest) } return intermediateResponse{}, errors.Wrapf(errPersistent, "retry %d/%d", attempt, maxAttempts) diff --git a/file_append_transaction.go b/file_append_transaction.go index 124c06139..adabe0763 100644 --- a/file_append_transaction.go +++ b/file_append_transaction.go @@ -231,7 +231,7 @@ func (transaction *FileAppendTransaction) ExecuteAll( transaction_advanceRequest, transaction_getNodeAccountID, fileAppendTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/file_contents_query.go b/file_contents_query.go index d257cc169..5e8f82874 100644 --- a/file_contents_query.go +++ b/file_contents_query.go @@ -55,12 +55,12 @@ func (query *FileContentsQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + fileContentsQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, fileContentsQuery_getMethod, - fileContentsQuery_mapResponseStatus, + fileContentsQuery_mapStatusError, query_mapResponse, ) @@ -72,8 +72,14 @@ func (query *FileContentsQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func fileContentsQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetFileGetContents().Header.NodeTransactionPrecheckCode) +func fileContentsQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetFileGetContents().Header.NodeTransactionPrecheckCode)) +} + +func fileContentsQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetFileGetContents().Header.NodeTransactionPrecheckCode), + } } func fileContentsQuery_getMethod(_ request, channel *channel) method { @@ -129,12 +135,12 @@ func (query *FileContentsQuery) Execute(client *Client) ([]byte, error) { request{ query: &query.Query, }, - query_shouldRetry, + fileContentsQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, fileContentsQuery_getMethod, - fileContentsQuery_mapResponseStatus, + fileContentsQuery_mapStatusError, query_mapResponse, ) diff --git a/file_create_transaction.go b/file_create_transaction.go index 0e5689b4c..7bad11a22 100644 --- a/file_create_transaction.go +++ b/file_create_transaction.go @@ -239,7 +239,7 @@ func (transaction *FileCreateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, fileCreateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/file_delete_transaction.go b/file_delete_transaction.go index 69fbd9d99..92f05f8aa 100644 --- a/file_delete_transaction.go +++ b/file_delete_transaction.go @@ -167,7 +167,7 @@ func (transaction *FileDeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, fileDeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/file_info_query.go b/file_info_query.go index 32dcc7551..f80c1bcf8 100644 --- a/file_info_query.go +++ b/file_info_query.go @@ -51,12 +51,12 @@ func (query *FileInfoQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + fileInfoQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, fileInfoQuery_getMethod, - fileInfoQuery_mapResponseStatus, + fileInfoQuery_mapStatusError, query_mapResponse, ) @@ -72,8 +72,14 @@ func (query *FileInfoQuery) GetCost(client *Client) (Hbar, error) { } } -func fileInfoQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetFileGetInfo().Header.NodeTransactionPrecheckCode) +func fileInfoQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetFileGetInfo().Header.NodeTransactionPrecheckCode)) +} + +func fileInfoQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetFileGetInfo().Header.NodeTransactionPrecheckCode), + } } func fileInfoQuery_getMethod(_ request, channel *channel) method { @@ -129,12 +135,12 @@ func (query *FileInfoQuery) Execute(client *Client) (FileInfo, error) { request{ query: &query.Query, }, - query_shouldRetry, + fileInfoQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, fileInfoQuery_getMethod, - fileInfoQuery_mapResponseStatus, + fileInfoQuery_mapStatusError, query_mapResponse, ) diff --git a/file_update_transaction.go b/file_update_transaction.go index 893aa7aef..2ac6f9af0 100644 --- a/file_update_transaction.go +++ b/file_update_transaction.go @@ -234,7 +234,7 @@ func (transaction *FileUpdateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, fileUpdateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/freeze_transaction.go b/freeze_transaction.go index bb45e3c94..946bdaeff 100644 --- a/freeze_transaction.go +++ b/freeze_transaction.go @@ -195,7 +195,7 @@ func (transaction *FreezeTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, freezeTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/live_hash_add_transaction.go b/live_hash_add_transaction.go index 605815998..4b2acd76e 100644 --- a/live_hash_add_transaction.go +++ b/live_hash_add_transaction.go @@ -204,7 +204,7 @@ func (transaction *LiveHashAddTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, liveHashAddTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/live_hash_delete_transaction.go b/live_hash_delete_transaction.go index d5373510a..313fa78f9 100644 --- a/live_hash_delete_transaction.go +++ b/live_hash_delete_transaction.go @@ -167,7 +167,7 @@ func (transaction *LiveHashDeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, liveHashDeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/live_hash_query.go b/live_hash_query.go index 55d6dc08e..065be517a 100644 --- a/live_hash_query.go +++ b/live_hash_query.go @@ -60,12 +60,12 @@ func (query *LiveHashQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + liveHashQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, liveHashQuery_getMethod, - liveHashQuery_mapResponseStatus, + liveHashQuery_mapStatusError, query_mapResponse, ) @@ -77,8 +77,14 @@ func (query *LiveHashQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func liveHashQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetCryptoGetLiveHash().Header.NodeTransactionPrecheckCode) +func liveHashQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetCryptoGetLiveHash().Header.NodeTransactionPrecheckCode)) +} + +func liveHashQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetCryptoGetLiveHash().Header.NodeTransactionPrecheckCode), + } } func liveHashQuery_getMethod(_ request, channel *channel) method { @@ -134,12 +140,12 @@ func (query *LiveHashQuery) Execute(client *Client) (LiveHash, error) { request{ query: &query.Query, }, - query_shouldRetry, + liveHashQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, liveHashQuery_getMethod, - liveHashQuery_mapResponseStatus, + liveHashQuery_mapStatusError, query_mapResponse, ) diff --git a/network_version_info_query.go b/network_version_info_query.go index fd3dae29d..a8c1611d8 100644 --- a/network_version_info_query.go +++ b/network_version_info_query.go @@ -42,12 +42,12 @@ func (query *NetworkVersionInfoQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + networkVersionInfoQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, networkVersionInfoQuery_getMethod, - networkVersionInfoQuery_mapResponseStatus, + networkVersionInfoQuery_mapStatusError, query_mapResponse, ) @@ -63,8 +63,14 @@ func (query *NetworkVersionInfoQuery) GetCost(client *Client) (Hbar, error) { } } -func networkVersionInfoQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetNetworkGetVersionInfo().Header.NodeTransactionPrecheckCode) +func networkVersionInfoQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetNetworkGetVersionInfo().Header.NodeTransactionPrecheckCode)) +} + +func networkVersionInfoQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetNetworkGetVersionInfo().Header.NodeTransactionPrecheckCode), + } } func networkVersionInfoQuery_getMethod(_ request, channel *channel) method { @@ -120,12 +126,12 @@ func (query *NetworkVersionInfoQuery) Execute(client *Client) (NetworkVersionInf request{ query: &query.Query, }, - query_shouldRetry, + networkVersionInfoQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, networkVersionInfoQuery_getMethod, - networkVersionInfoQuery_mapResponseStatus, + networkVersionInfoQuery_mapStatusError, query_mapResponse, ) diff --git a/query.go b/query.go index 11887dbfd..9526a9030 100644 --- a/query.go +++ b/query.go @@ -87,8 +87,15 @@ func (query *Query) getIsPaymentRequired() bool { return true } -func query_shouldRetry(status Status, _ response) bool { - return status == StatusBusy +func query_shouldRetry(status Status) executionState { + switch status { + case StatusPlatformTransactionNotCreated, StatusBusy: + return executionStateRetry + case StatusOk: + return executionStateFinished + } + + return executionStateError } func query_makeRequest(request request) protoRequest { diff --git a/schedule_create_transaction.go b/schedule_create_transaction.go index 6e7e5df75..fc43aece5 100644 --- a/schedule_create_transaction.go +++ b/schedule_create_transaction.go @@ -197,7 +197,7 @@ func (transaction *ScheduleCreateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, scheduleCreateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/schedule_create_transaction_test.go b/schedule_create_transaction_test.go index 176339af2..09a8bcf6a 100644 --- a/schedule_create_transaction_test.go +++ b/schedule_create_transaction_test.go @@ -333,3 +333,45 @@ func TestScheduleCreateTransaction_CheckValidGetTransaction_Execute(t *testing.T assert.Equal(t, fmt.Sprintf("exceptional precheck status SCHEDULE_ALREADY_EXECUTED"), err.Error()) } } + +func TestScheduleCreateTransaction_Duplicate_Execute(t *testing.T) { + client := newTestClient(t, false) + + newKey, err := GeneratePrivateKey() + assert.NoError(t, err) + + newBalance := NewHbar(1) + + assert.Equal(t, HbarUnits.Hbar.numberOfTinybar(), newBalance.tinybar) + + transactionID := TransactionIDGenerate(client.GetOperatorAccountID()) + + tx := NewAccountCreateTransaction(). + SetTransactionID(transactionID). + SetKey(newKey.PublicKey()). + SetMaxTransactionFee(NewHbar(2)). + SetInitialBalance(newBalance) + + assert.NoError(t, err) + + scheduleTx, err := tx.Schedule() + assert.NoError(t, err) + + scheduleTx = scheduleTx. + SetPayerAccountID(client.GetOperatorAccountID()). + SetAdminKey(client.GetOperatorPublicKey()) + + resp, err := scheduleTx.Execute(client) + assert.NoError(t, err) + + _, err = resp.GetReceipt(client) + assert.NoError(t, err) + + _, err = scheduleTx.Execute(client) + assert.Error(t, err) + + if err != nil { + assert.Equal(t, fmt.Sprintf("exceptional precheck status DUPLICATE_TRANSACTION received for transaction %s", resp.TransactionID), err.Error()) + + } +} diff --git a/schedule_delete_transaction.go b/schedule_delete_transaction.go index 71ef3085c..abf9eec36 100644 --- a/schedule_delete_transaction.go +++ b/schedule_delete_transaction.go @@ -167,7 +167,7 @@ func (transaction *ScheduleDeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, scheduleDeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/schedule_info_query.go b/schedule_info_query.go index ee8146478..496d432ef 100644 --- a/schedule_info_query.go +++ b/schedule_info_query.go @@ -51,12 +51,12 @@ func (query *ScheduleInfoQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + scheduleInfoQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, scheduleInfoQuery_getMethod, - scheduleInfoQuery_mapResponseStatus, + scheduleInfoQuery_mapStatusError, query_mapResponse, ) @@ -72,8 +72,14 @@ func (query *ScheduleInfoQuery) GetCost(client *Client) (Hbar, error) { } } -func scheduleInfoQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetScheduleGetInfo().Header.NodeTransactionPrecheckCode) +func scheduleInfoQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetScheduleGetInfo().Header.NodeTransactionPrecheckCode)) +} + +func scheduleInfoQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetScheduleGetInfo().Header.NodeTransactionPrecheckCode), + } } func scheduleInfoQuery_getMethod(_ request, channel *channel) method { @@ -125,12 +131,12 @@ func (query *ScheduleInfoQuery) Execute(client *Client) (ScheduleInfo, error) { request{ query: &query.Query, }, - query_shouldRetry, + scheduleInfoQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, scheduleInfoQuery_getMethod, - scheduleInfoQuery_mapResponseStatus, + scheduleInfoQuery_mapStatusError, query_mapResponse, ) diff --git a/schedule_sign_transaction.go b/schedule_sign_transaction.go index 915e971c4..25cf4f9a4 100644 --- a/schedule_sign_transaction.go +++ b/schedule_sign_transaction.go @@ -158,7 +158,7 @@ func (transaction *ScheduleSignTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, scheduleSignTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/system_delete_transaction.go b/system_delete_transaction.go index 82a8bda6b..fbf8c8342 100644 --- a/system_delete_transaction.go +++ b/system_delete_transaction.go @@ -210,7 +210,7 @@ func (transaction *SystemDeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, systemDeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/system_undelete_transaction.go b/system_undelete_transaction.go index d3bcede78..6af3f369f 100644 --- a/system_undelete_transaction.go +++ b/system_undelete_transaction.go @@ -191,7 +191,7 @@ func (transaction *SystemUndeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, systemUndeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_associate_transaction.go b/token_associate_transaction.go index c288d8885..0a85f00e0 100644 --- a/token_associate_transaction.go +++ b/token_associate_transaction.go @@ -219,7 +219,7 @@ func (transaction *TokenAssociateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenAssociateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_burn_transaction.go b/token_burn_transaction.go index c839ffcee..12ed3ef68 100644 --- a/token_burn_transaction.go +++ b/token_burn_transaction.go @@ -194,7 +194,7 @@ func (transaction *TokenBurnTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenBurnTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_create_transaction.go b/token_create_transaction.go index 4b078b0e9..5b7952c69 100644 --- a/token_create_transaction.go +++ b/token_create_transaction.go @@ -382,7 +382,7 @@ func (transaction *TokenCreateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenCreateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_delete_transaction.go b/token_delete_transaction.go index 3ca8cb94e..79aea063a 100644 --- a/token_delete_transaction.go +++ b/token_delete_transaction.go @@ -172,7 +172,7 @@ func (transaction *TokenDeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenDeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_delete_transaction_test.go b/token_delete_transaction_test.go index a49fdd4b1..f0f0aeec7 100644 --- a/token_delete_transaction_test.go +++ b/token_delete_transaction_test.go @@ -65,6 +65,6 @@ func Test_TokenDelete_NoTokenID(t *testing.T) { Execute(client) assert.Error(t, err) if err != nil { - assert.Equal(t, fmt.Sprintf("exceptional precheck status NOT_SUPPORTED received for transaction %s", resp.TransactionID), err.Error()) + assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_TOKEN_ID received for transaction %s", resp.TransactionID), err.Error()) } } diff --git a/token_dissociate_transaction.go b/token_dissociate_transaction.go index 11f9e7c14..a6d04ac8d 100644 --- a/token_dissociate_transaction.go +++ b/token_dissociate_transaction.go @@ -201,7 +201,7 @@ func (transaction *TokenDissociateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenDissociateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_freeze_transaction.go b/token_freeze_transaction.go index 8ccca909b..40807baac 100644 --- a/token_freeze_transaction.go +++ b/token_freeze_transaction.go @@ -191,7 +191,7 @@ func (transaction *TokenFreezeTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenFreezeTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_grant_kyc_transaction.go b/token_grant_kyc_transaction.go index 54aa640c5..e964bff00 100644 --- a/token_grant_kyc_transaction.go +++ b/token_grant_kyc_transaction.go @@ -189,7 +189,7 @@ func (transaction *TokenGrantKycTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenGrantKycTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_info_query.go b/token_info_query.go index 764eb15b7..802911be6 100644 --- a/token_info_query.go +++ b/token_info_query.go @@ -54,12 +54,12 @@ func (query *TokenInfoQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + tokenInfoQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, tokenInfoQuery_getMethod, - tokenInfoQuery_mapResponseStatus, + tokenInfoQuery_mapStatusError, query_mapResponse, ) @@ -75,8 +75,14 @@ func (query *TokenInfoQuery) GetCost(client *Client) (Hbar, error) { } } -func tokenInfoQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetTokenGetInfo().Header.NodeTransactionPrecheckCode) +func tokenInfoQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetTokenGetInfo().Header.NodeTransactionPrecheckCode)) +} + +func tokenInfoQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetTokenGetInfo().Header.NodeTransactionPrecheckCode), + } } func tokenInfoQuery_getMethod(_ request, channel *channel) method { @@ -133,12 +139,12 @@ func (query *TokenInfoQuery) Execute(client *Client) (TokenInfo, error) { request{ query: &query.Query, }, - query_shouldRetry, + tokenInfoQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, tokenInfoQuery_getMethod, - tokenInfoQuery_mapResponseStatus, + tokenInfoQuery_mapStatusError, query_mapResponse, ) diff --git a/token_mint_transaction.go b/token_mint_transaction.go index 66c48bfea..3ba94c2cd 100644 --- a/token_mint_transaction.go +++ b/token_mint_transaction.go @@ -189,7 +189,7 @@ func (transaction *TokenMintTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenMintTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_revoke_kcy_transaction.go b/token_revoke_kcy_transaction.go index 0df48e027..23675cd19 100644 --- a/token_revoke_kcy_transaction.go +++ b/token_revoke_kcy_transaction.go @@ -189,7 +189,7 @@ func (transaction *TokenRevokeKycTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenRevokeKycTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_unfreeze_transaction.go b/token_unfreeze_transaction.go index fd6da3fc0..cd9d0cbe0 100644 --- a/token_unfreeze_transaction.go +++ b/token_unfreeze_transaction.go @@ -185,7 +185,7 @@ func (transaction *TokenUnfreezeTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenUnfreezeTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_update_transaction.go b/token_update_transaction.go index 7e89ed080..df895002f 100644 --- a/token_update_transaction.go +++ b/token_update_transaction.go @@ -359,7 +359,7 @@ func (transaction *TokenUpdateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenUpdateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/token_wipe_transaction.go b/token_wipe_transaction.go index 5e5faa652..ea40922f0 100644 --- a/token_wipe_transaction.go +++ b/token_wipe_transaction.go @@ -208,7 +208,7 @@ func (transaction *TokenWipeTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, tokenWipeTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/topic_create_transaction.go b/topic_create_transaction.go index 694584418..23251290d 100644 --- a/topic_create_transaction.go +++ b/topic_create_transaction.go @@ -236,7 +236,7 @@ func (transaction *TopicCreateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, topicCreateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/topic_delete_transaction.go b/topic_delete_transaction.go index 3f362011f..220f519e3 100644 --- a/topic_delete_transaction.go +++ b/topic_delete_transaction.go @@ -171,7 +171,7 @@ func (transaction *TopicDeleteTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, topicDeleteTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/topic_info_query.go b/topic_info_query.go index 388fd999d..fc5b6e96d 100644 --- a/topic_info_query.go +++ b/topic_info_query.go @@ -54,12 +54,12 @@ func (query *TopicInfoQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + topicInfoQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, topicInfoQuery_getMethod, - topicInfoQuery_mapResponseStatus, + topicInfoQuery_mapStatusError, query_mapResponse, ) @@ -75,8 +75,14 @@ func (query *TopicInfoQuery) GetCost(client *Client) (Hbar, error) { } } -func topicInfoQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetConsensusGetTopicInfo().Header.NodeTransactionPrecheckCode) +func topicInfoQuery_shouldRetry(_ request, response response) executionState { + return query_shouldRetry(Status(response.query.GetConsensusGetTopicInfo().Header.NodeTransactionPrecheckCode)) +} + +func topicInfoQuery_mapStatusError(_ request, response response) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetConsensusGetTopicInfo().Header.NodeTransactionPrecheckCode), + } } func topicInfoQuery_getMethod(_ request, channel *channel) method { @@ -133,12 +139,12 @@ func (query *TopicInfoQuery) Execute(client *Client) (TopicInfo, error) { request{ query: &query.Query, }, - query_shouldRetry, + topicInfoQuery_shouldRetry, query_makeRequest, query_advanceRequest, query_getNodeAccountID, topicInfoQuery_getMethod, - topicInfoQuery_mapResponseStatus, + topicInfoQuery_mapStatusError, query_mapResponse, ) diff --git a/topic_message_submit_transaction.go b/topic_message_submit_transaction.go index 2eb299e10..dcd59b2ec 100644 --- a/topic_message_submit_transaction.go +++ b/topic_message_submit_transaction.go @@ -226,7 +226,7 @@ func (transaction *TopicMessageSubmitTransaction) ExecuteAll( transaction_advanceRequest, transaction_getNodeAccountID, topicMessageSubmitTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/topic_update_transaction.go b/topic_update_transaction.go index 3d2971515..7c97756d3 100644 --- a/topic_update_transaction.go +++ b/topic_update_transaction.go @@ -278,7 +278,7 @@ func (transaction *TopicUpdateTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, topicUpdateTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/transaction.go b/transaction.go index 83a4dfdb0..2ef47d848 100644 --- a/transaction.go +++ b/transaction.go @@ -377,8 +377,15 @@ func (transaction *Transaction) keyAlreadySigned( return false } -func transaction_shouldRetry(status Status, _ response) bool { - return status == StatusBusy +func transaction_shouldRetry(_ request, response response) executionState { + switch Status(response.transaction.NodeTransactionPrecheckCode) { + case StatusPlatformTransactionNotCreated, StatusBusy: + return executionStateRetry + case StatusOk: + return executionStateFinished + } + + return executionStateError } func transaction_makeRequest(request request) protoRequest { @@ -400,11 +407,14 @@ func transaction_getNodeAccountID(request request) AccountID { return request.transaction.nodeIDs[request.transaction.nextNodeIndex] } -func transaction_mapResponseStatus( - _ request, +func transaction_mapStatusError( + request request, response response, -) Status { - return Status(response.transaction.NodeTransactionPrecheckCode) +) error { + return ErrHederaPreCheckStatus{ + Status: Status(response.transaction.NodeTransactionPrecheckCode), + TxID: request.transaction.GetTransactionID(), + } } func transaction_mapResponse(request request, _ response, nodeID AccountID, protoRequest protoRequest) (intermediateResponse, error) { diff --git a/transaction_receipt_query.go b/transaction_receipt_query.go index 5fc634457..3eb6e6b05 100644 --- a/transaction_receipt_query.go +++ b/transaction_receipt_query.go @@ -47,7 +47,7 @@ func (query *TransactionReceiptQuery) GetCost(client *Client) (Hbar, error) { costQuery_advanceRequest, costQuery_getNodeAccountID, transactionReceiptQuery_getMethod, - transactionReceiptQuery_mapResponseStatus, + transactionReceiptQuery_mapStatusError, query_mapResponse, ) @@ -59,43 +59,39 @@ func (query *TransactionReceiptQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func transactionReceiptQuery_shouldRetry(status Status, response response) bool { - if status == StatusPlatformTransactionNotCreated { - return true - } - - switch status { - case StatusBusy, StatusUnknown, StatusReceiptNotFound: - return true +func transactionReceiptQuery_shouldRetry(request request, response response) executionState { + switch Status(response.query.GetTransactionGetReceipt().GetHeader().GetNodeTransactionPrecheckCode()) { + case StatusPlatformTransactionNotCreated, StatusBusy, StatusUnknown, StatusReceiptNotFound, StatusRecordNotFound: + return executionStateRetry case StatusOk: break default: - return false + return executionStateError } - status = Status(response.query.GetTransactionGetReceipt().GetReceipt().GetStatus()) - - switch status { - case StatusBusy, StatusUnknown, StatusOk, StatusReceiptNotFound: - return true + switch Status(response.query.GetTransactionGetReceipt().GetReceipt().GetStatus()) { + case StatusBusy, StatusUnknown, StatusOk, StatusReceiptNotFound, StatusRecordNotFound: + return executionStateRetry + case StatusSuccess: + return executionStateFinished default: - return false + return executionStateError } } -func transactionReceiptQuery_mapResponseStatus(_ request, response response) Status { - status := Status(response.query.GetTransactionGetReceipt().GetHeader().GetNodeTransactionPrecheckCode()) - - switch status { - case StatusBusy, StatusUnknown, StatusReceiptNotFound: - return status - case StatusOk: +func transactionReceiptQuery_mapStatusError(_ request, response response) error { + switch Status(response.query.GetTransactionGetReceipt().GetHeader().GetNodeTransactionPrecheckCode()) { + case StatusPlatformTransactionNotCreated, StatusBusy, StatusUnknown, StatusReceiptNotFound, StatusRecordNotFound, StatusOk: break default: - return status + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetTransactionGetReceipt().GetHeader().GetNodeTransactionPrecheckCode()), + } } - return Status(response.query.GetTransactionGetReceipt().GetReceipt().GetStatus()) + return ErrHederaReceiptStatus{ + Status: Status(response.query.GetTransactionGetReceipt().GetReceipt().GetStatus()), + } } func transactionReceiptQuery_getMethod(_ request, channel *channel) method { @@ -152,7 +148,7 @@ func (query *TransactionReceiptQuery) Execute(client *Client) (TransactionReceip query_advanceRequest, query_getNodeAccountID, transactionReceiptQuery_getMethod, - transactionReceiptQuery_mapResponseStatus, + transactionReceiptQuery_mapStatusError, query_mapResponse, ) diff --git a/transaction_record_query.go b/transaction_record_query.go index c5e7aae57..572b0ca2c 100644 --- a/transaction_record_query.go +++ b/transaction_record_query.go @@ -42,12 +42,12 @@ func (query *TransactionRecordQuery) GetCost(client *Client) (Hbar, error) { request{ query: &query.Query, }, - query_shouldRetry, + transactionRecordQuery_shouldRetry, costQuery_makeRequest, costQuery_advanceRequest, costQuery_getNodeAccountID, transactionRecordQuery_getMethod, - transactionRecordQuery_mapResponseStatus, + transactionRecordQuery_mapStatusError, query_mapResponse, ) @@ -59,32 +59,39 @@ func (query *TransactionRecordQuery) GetCost(client *Client) (Hbar, error) { return HbarFromTinybar(cost), nil } -func transactionRecordQuery_shouldRetry(status Status, response response) bool { - if status == StatusPlatformTransactionNotCreated { - return true - } - - switch status { - case StatusBusy, StatusUnknown, StatusReceiptNotFound: - return true +func transactionRecordQuery_shouldRetry(request request, response response) executionState { + switch Status(response.query.GetTransactionGetRecord().GetHeader().GetNodeTransactionPrecheckCode()) { + case StatusPlatformTransactionNotCreated, StatusBusy, StatusUnknown, StatusReceiptNotFound, StatusRecordNotFound: + return executionStateRetry case StatusOk: break default: - return false + return executionStateError } - status = Status(response.query.GetTransactionGetRecord().GetTransactionRecord().GetReceipt().GetStatus()) - - switch status { - case StatusBusy, StatusUnknown, StatusOk, StatusReceiptNotFound: - return true + switch Status(response.query.GetTransactionGetRecord().GetTransactionRecord().GetReceipt().GetStatus()) { + case StatusBusy, StatusUnknown, StatusOk, StatusReceiptNotFound, StatusRecordNotFound: + return executionStateRetry + case StatusSuccess: + return executionStateFinished default: - return false + return executionStateError } } -func transactionRecordQuery_mapResponseStatus(_ request, response response) Status { - return Status(response.query.GetTransactionGetRecord().Header.NodeTransactionPrecheckCode) +func transactionRecordQuery_mapStatusError(_ request, response response) error { + switch Status(response.query.GetTransactionGetRecord().GetHeader().GetNodeTransactionPrecheckCode()) { + case StatusPlatformTransactionNotCreated, StatusBusy, StatusUnknown, StatusReceiptNotFound, StatusRecordNotFound, StatusOk: + break + default: + return ErrHederaPreCheckStatus{ + Status: Status(response.query.GetTransactionGetRecord().GetHeader().GetNodeTransactionPrecheckCode()), + } + } + + return ErrHederaReceiptStatus{ + Status: Status(response.query.GetTransactionGetRecord().GetTransactionRecord().GetReceipt().GetStatus()), + } } func transactionRecordQuery_getMethod(_ request, channel *channel) method { @@ -183,7 +190,7 @@ func (query *TransactionRecordQuery) Execute(client *Client) (TransactionRecord, query_advanceRequest, query_getNodeAccountID, transactionRecordQuery_getMethod, - transactionRecordQuery_mapResponseStatus, + transactionRecordQuery_mapStatusError, query_mapResponse, ) diff --git a/transfer_transaction.go b/transfer_transaction.go index b3e4a71f2..69dc2a278 100644 --- a/transfer_transaction.go +++ b/transfer_transaction.go @@ -230,7 +230,7 @@ func (transaction *TransferTransaction) Execute( transaction_advanceRequest, transaction_getNodeAccountID, transferTransaction_getMethod, - transaction_mapResponseStatus, + transaction_mapStatusError, transaction_mapResponse, ) diff --git a/utilities_for_test.go b/utilities_for_test.go index 060055fe8..4fc8f04f6 100644 --- a/utilities_for_test.go +++ b/utilities_for_test.go @@ -102,6 +102,8 @@ func newTestClient(t *testing.T, token bool) *Client { assert.NoError(t, err) client.SetOperator(*receipt.AccountID, newKey) + + time.Sleep(2000) } return client