Skip to content

Commit

Permalink
fix: Retry when getting THROTTLED_AT_CONSENSUS (#1065)
Browse files Browse the repository at this point in the history
* fix: retry when getting THROTTLED_AT_CONSENSUS

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: add remaing status codes

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

---------

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>
  • Loading branch information
0xivanov authored Sep 11, 2024
1 parent ba92959 commit b52ab70
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.4
github.com/cenkalti/backoff/v4 v4.3.0
github.com/ethereum/go-ethereum v1.13.15
github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20240828100549-72dfb73947c6
github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20240910141930-3e9d10484c9a
github.com/json-iterator/go v1.1.12
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w=
github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20240828100549-72dfb73947c6 h1:gtphIZkv7KESCPsZTFvB4AN6vIDhRdQRmh5Mfac6D7I=
github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20240828100549-72dfb73947c6/go.mod h1:vN4EAg6FOvgRjeqR7tYBqv9lxT0wqImRh/FVxBUHOkM=
github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20240910141930-3e9d10484c9a h1:pevBrJ/CTxZlKOYfOP3UU2pNStEeP7deGXQOfX+ivKA=
github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20240910141930-3e9d10484c9a/go.mod h1:vN4EAg6FOvgRjeqR7tYBqv9lxT0wqImRh/FVxBUHOkM=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
Expand Down
9 changes: 9 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ const (
StatusPendingNftAirdropAlreadyExists Status = 364
StatusAccountHasPendingAirdrops Status = 365
StatusThrottledAtConsensus Status = 366
StatusInvalidPendingAirdropId Status = 367
StatusTokenAirdropWithFallbackRoyalty Status = 368
StatusInvalidTokenIdPendingAirdrop Status = 369
)

// String() returns a string representation of the status
Expand Down Expand Up @@ -1005,6 +1008,12 @@ func (status Status) String() string { // nolint
return "ACCOUNT_HAS_PENDING_AIRDROPS"
case StatusThrottledAtConsensus:
return "THROTTLED_AT_CONSENSUS"
case StatusInvalidPendingAirdropId:
return "INVALID_PENDING_AIRDROP_ID"
case StatusTokenAirdropWithFallbackRoyalty:
return "TOKEN_AIRDROP_WITH_FALLBACK_ROYALTY"
case StatusInvalidTokenIdPendingAirdrop:
return "INVALID_TOKEN_IN_PENDING_AIRDROP"
}

panic(fmt.Sprintf("unreachable: Status.String() switch statement is non-exhaustive. Status: %v", uint32(status)))
Expand Down
2 changes: 1 addition & 1 deletion transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4755,7 +4755,7 @@ func TransactionExecute(transaction interface{}, client *Client) (TransactionRes
func (tx *Transaction) shouldRetry(_ Executable, response interface{}) _ExecutionState {
status := Status(response.(*services.TransactionResponse).NodeTransactionPrecheckCode)
switch status {
case StatusPlatformTransactionNotCreated, StatusPlatformNotActive, StatusBusy:
case StatusPlatformTransactionNotCreated, StatusPlatformNotActive, StatusBusy, StatusThrottledAtConsensus:
return executionStateRetry
case StatusTransactionExpired:
return executionStateExpired
Expand Down
26 changes: 26 additions & 0 deletions transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,32 @@ func TestUnitTransactionSignSwitchCasesPointers(t *testing.T) {
}
}

func TestUnitTransactionThrottleAtConsensusGracefulHandling(t *testing.T) {
t.Parallel()

responses := [][]interface{}{{
&services.TransactionResponse{
NodeTransactionPrecheckCode: services.ResponseCodeEnum_THROTTLED_AT_CONSENSUS,
},
&services.TransactionResponse{
NodeTransactionPrecheckCode: services.ResponseCodeEnum_THROTTLED_AT_CONSENSUS,
},
&services.TransactionResponse{
NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
},
}}

client, server := NewMockClientAndServer(responses)
defer server.Close()
_, err := NewTransferTransaction().
SetNodeAccountIDs([]AccountID{{Account: 3}}).
AddHbarTransfer(AccountID{Account: 2}, HbarFromTinybar(-1)).
AddHbarTransfer(AccountID{Account: 3}, HbarFromTinybar(1)).
Execute(client)
client.SetMaxAttempts(3)
require.NoError(t, err)

}
func TestUnitTransactionAttributes(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit b52ab70

Please sign in to comment.