Skip to content

Commit 05e87e1

Browse files
authored
feat: rpcv9 InvalidTransactionNonce error has data field (#2944)
* InvalidTransactionNonce error returns data * fix linter * New Gw errors * Test case for errors * add test for InvalidTransactionNonce as ValidationFailure * gatewayError.CompilationFailed returns data starting from rpcv8
1 parent 1fa1583 commit 05e87e1

File tree

5 files changed

+74
-34
lines changed

5 files changed

+74
-34
lines changed

clients/gateway/gateway.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ import (
1919
)
2020

2121
var (
22-
InvalidContractClass ErrorCode = "StarknetErrorCode.INVALID_CONTRACT_CLASS"
23-
UndeclaredClass ErrorCode = "StarknetErrorCode.UNDECLARED_CLASS"
24-
ClassAlreadyDeclared ErrorCode = "StarknetErrorCode.CLASS_ALREADY_DECLARED"
25-
InsufficientMaxFee ErrorCode = "StarknetErrorCode.INSUFFICIENT_MAX_FEE"
26-
InsufficientResourcesForValidate ErrorCode = "StarknetErrorCode.INSUFFICIENT_RESOURCES_FOR_VALIDATE"
27-
InsufficientAccountBalance ErrorCode = "StarknetErrorCode.INSUFFICIENT_ACCOUNT_BALANCE"
28-
ValidateFailure ErrorCode = "StarknetErrorCode.VALIDATE_FAILURE"
29-
ContractBytecodeSizeTooLarge ErrorCode = "StarknetErrorCode.CONTRACT_BYTECODE_SIZE_TOO_LARGE"
30-
DuplicatedTransaction ErrorCode = "StarknetErrorCode.DUPLICATED_TRANSACTION"
31-
InvalidTransactionNonce ErrorCode = "StarknetErrorCode.INVALID_TRANSACTION_NONCE"
32-
CompilationFailed ErrorCode = "StarknetErrorCode.COMPILATION_FAILED"
33-
InvalidCompiledClassHash ErrorCode = "StarknetErrorCode.INVALID_COMPILED_CLASS_HASH"
34-
ContractClassObjectSizeTooLarge ErrorCode = "StarknetErrorCode.CONTRACT_CLASS_OBJECT_SIZE_TOO_LARGE"
35-
InvalidTransactionVersion ErrorCode = "StarknetErrorCode.INVALID_TRANSACTION_VERSION"
36-
InvalidContractClassVersion ErrorCode = "StarknetErrorCode.INVALID_CONTRACT_CLASS_VERSION"
22+
InvalidContractClass ErrorCode = "StarknetErrorCode.INVALID_CONTRACT_CLASS"
23+
UndeclaredClass ErrorCode = "StarknetErrorCode.UNDECLARED_CLASS"
24+
ClassAlreadyDeclared ErrorCode = "StarknetErrorCode.CLASS_ALREADY_DECLARED"
25+
InsufficientMaxFee ErrorCode = "StarknetErrorCode.INSUFFICIENT_MAX_FEE"
26+
InsufficientResourcesForValidate ErrorCode = "StarknetErrorCode.INSUFFICIENT_RESOURCES_FOR_VALIDATE"
27+
InsufficientAccountBalance ErrorCode = "StarknetErrorCode.INSUFFICIENT_ACCOUNT_BALANCE"
28+
ValidateFailure ErrorCode = "StarknetErrorCode.VALIDATE_FAILURE"
29+
ContractBytecodeSizeTooLarge ErrorCode = "StarknetErrorCode.CONTRACT_BYTECODE_SIZE_TOO_LARGE"
30+
DuplicatedTransaction ErrorCode = "StarknetErrorCode.DUPLICATED_TRANSACTION"
31+
InvalidTransactionNonce ErrorCode = "StarknetErrorCode.INVALID_TRANSACTION_NONCE"
32+
CompilationFailed ErrorCode = "StarknetErrorCode.COMPILATION_FAILED"
33+
InvalidCompiledClassHash ErrorCode = "StarknetErrorCode.INVALID_COMPILED_CLASS_HASH"
34+
ContractClassObjectSizeTooLarge ErrorCode = "StarknetErrorCode.CONTRACT_CLASS_OBJECT_SIZE_TOO_LARGE"
35+
InvalidTransactionVersion ErrorCode = "StarknetErrorCode.INVALID_TRANSACTION_VERSION"
36+
InvalidContractClassVersion ErrorCode = "StarknetErrorCode.INVALID_CONTRACT_CLASS_VERSION"
37+
FeeBelowMinimum ErrorCode = "StarknetErrorCode.FEE_BELOW_MINIMUM"
38+
ReplacementTransactionUnderPriced ErrorCode = "StarknetErrorCode.REPLACEMENT_TRANSACTION_UNDERPRICED"
3739
)
3840

3941
type Client struct {

rpc/rpccore/rpccore.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,22 @@ var (
5959
ErrInsufficientAccountBalance = &jsonrpc.Error{Code: 54, Message: "Account balance is smaller than the transaction's max_fee"}
6060
ErrInsufficientAccountBalanceV0_8 = &jsonrpc.Error{Code: 54, Message: "Account balance is smaller than the transaction's " +
6161
"maximal fee (calculated as the sum of each resource's limit x max price)"}
62-
ErrValidationFailure = &jsonrpc.Error{Code: 55, Message: "Account validation failed"}
63-
ErrCompilationFailed = &jsonrpc.Error{Code: 56, Message: "Compilation failed"}
64-
ErrContractClassSizeTooLarge = &jsonrpc.Error{Code: 57, Message: "Contract class size is too large"}
65-
ErrNonAccount = &jsonrpc.Error{Code: 58, Message: "Sender address is not an account contract"}
66-
ErrDuplicateTx = &jsonrpc.Error{Code: 59, Message: "A transaction with the same hash already exists in the mempool"}
67-
ErrCompiledClassHashMismatch = &jsonrpc.Error{Code: 60, Message: "the compiled class hash did not match the one supplied in the transaction"} //nolint:lll
68-
ErrUnsupportedTxVersion = &jsonrpc.Error{Code: 61, Message: "the transaction version is not supported"}
69-
ErrUnsupportedContractClassVersion = &jsonrpc.Error{Code: 62, Message: "the contract class version is not supported"}
70-
ErrUnexpectedError = &jsonrpc.Error{Code: 63, Message: "An unexpected error occurred"}
71-
ErrInvalidSubscriptionID = &jsonrpc.Error{Code: 66, Message: "Invalid subscription id"}
72-
ErrTooManyAddressesInFilter = &jsonrpc.Error{Code: 67, Message: "Too many addresses in filter sender_address filter"}
73-
ErrTooManyBlocksBack = &jsonrpc.Error{Code: 68, Message: fmt.Sprintf("Cannot go back more than %v blocks", MaxBlocksBack)}
74-
ErrCallOnPending = &jsonrpc.Error{Code: 69, Message: "This method does not support being called on the pending block"}
75-
ErrCallOnPreConfirmed = &jsonrpc.Error{
62+
ErrValidationFailure = &jsonrpc.Error{Code: 55, Message: "Account validation failed"}
63+
ErrCompilationFailed = &jsonrpc.Error{Code: 56, Message: "Compilation failed"}
64+
ErrContractClassSizeTooLarge = &jsonrpc.Error{Code: 57, Message: "Contract class size is too large"}
65+
ErrNonAccount = &jsonrpc.Error{Code: 58, Message: "Sender address is not an account contract"}
66+
ErrDuplicateTx = &jsonrpc.Error{Code: 59, Message: "A transaction with the same hash already exists in the mempool"}
67+
ErrCompiledClassHashMismatch = &jsonrpc.Error{Code: 60, Message: "the compiled class hash did not match the one supplied in the transaction"} //nolint:lll
68+
ErrUnsupportedTxVersion = &jsonrpc.Error{Code: 61, Message: "the transaction version is not supported"}
69+
ErrUnsupportedContractClassVersion = &jsonrpc.Error{Code: 62, Message: "the contract class version is not supported"}
70+
ErrUnexpectedError = &jsonrpc.Error{Code: 63, Message: "An unexpected error occurred"}
71+
ErrReplacementTransactionUnderPriced = &jsonrpc.Error{Code: 64, Message: "Replacement transaction is underpriced"}
72+
ErrFeeBelowMinimum = &jsonrpc.Error{Code: 65, Message: "Transaction fee below minimum"}
73+
ErrInvalidSubscriptionID = &jsonrpc.Error{Code: 66, Message: "Invalid subscription id"}
74+
ErrTooManyAddressesInFilter = &jsonrpc.Error{Code: 67, Message: "Too many addresses in filter sender_address filter"}
75+
ErrTooManyBlocksBack = &jsonrpc.Error{Code: 68, Message: fmt.Sprintf("Cannot go back more than %v blocks", MaxBlocksBack)}
76+
ErrCallOnPending = &jsonrpc.Error{Code: 69, Message: "This method does not support being called on the pending block"}
77+
ErrCallOnPreConfirmed = &jsonrpc.Error{
7678
Code: 70, Message: "This method does not support being called on the pre_confirmed block",
7779
}
7880

rpc/v8/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ func makeJSONErrorFromGatewayError(err error) *jsonrpc.Error {
802802
case gateway.InvalidTransactionNonce:
803803
return rpccore.ErrInvalidTransactionNonce
804804
case gateway.CompilationFailed:
805-
return rpccore.ErrCompilationFailed
805+
return rpccore.ErrCompilationFailed.CloneWithData(gatewayErr.Message)
806806
case gateway.InvalidCompiledClassHash:
807807
return rpccore.ErrCompiledClassHashMismatch
808808
case gateway.InvalidTransactionVersion:

rpc/v9/transaction.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ func (h *Handler) TransactionStatusV0_7(
809809
}, nil
810810
}
811811

812-
func makeJSONErrorFromGatewayError(err error) *jsonrpc.Error {
812+
func makeJSONErrorFromGatewayError(err error) *jsonrpc.Error { //nolint:gocyclo
813813
gatewayErr, ok := err.(*gateway.Error)
814814
if !ok {
815815
return jsonrpc.Err(jsonrpc.InternalError, err.Error())
@@ -827,21 +827,29 @@ func makeJSONErrorFromGatewayError(err error) *jsonrpc.Error {
827827
case gateway.InsufficientAccountBalance:
828828
return rpccore.ErrInsufficientAccountBalanceV0_8
829829
case gateway.ValidateFailure:
830-
return rpccore.ErrValidationFailure.CloneWithData(gatewayErr.Message)
830+
if strings.Contains(gatewayErr.Message, rpccore.ErrInvalidTransactionNonce.Message) {
831+
return rpccore.ErrInvalidTransactionNonce.CloneWithData(gatewayErr.Message)
832+
} else {
833+
return rpccore.ErrValidationFailure.CloneWithData(gatewayErr.Message)
834+
}
831835
case gateway.ContractBytecodeSizeTooLarge, gateway.ContractClassObjectSizeTooLarge:
832836
return rpccore.ErrContractClassSizeTooLarge
833837
case gateway.DuplicatedTransaction:
834838
return rpccore.ErrDuplicateTx
835839
case gateway.InvalidTransactionNonce:
836-
return rpccore.ErrInvalidTransactionNonce
840+
return rpccore.ErrInvalidTransactionNonce.CloneWithData(gatewayErr.Message)
837841
case gateway.CompilationFailed:
838-
return rpccore.ErrCompilationFailed
842+
return rpccore.ErrCompilationFailed.CloneWithData(gatewayErr.Message)
839843
case gateway.InvalidCompiledClassHash:
840844
return rpccore.ErrCompiledClassHashMismatch
841845
case gateway.InvalidTransactionVersion:
842846
return rpccore.ErrUnsupportedTxVersion
843847
case gateway.InvalidContractClassVersion:
844848
return rpccore.ErrUnsupportedContractClassVersion
849+
case gateway.ReplacementTransactionUnderPriced:
850+
return rpccore.ErrReplacementTransactionUnderPriced
851+
case gateway.FeeBelowMinimum:
852+
return rpccore.ErrFeeBelowMinimum
845853
default:
846854
return rpccore.ErrUnexpectedError.CloneWithData(gatewayErr.Message)
847855
}

rpc/v9/transaction_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,34 @@ func TestAddTransaction(t *testing.T) {
13341334
gatewayError: &gateway.Error{Code: gateway.InsufficientAccountBalance},
13351335
expectedError: rpccore.ErrInsufficientAccountBalanceV0_8,
13361336
},
1337+
{
1338+
name: "FeeBelowMinimum error",
1339+
gatewayError: &gateway.Error{Code: gateway.FeeBelowMinimum},
1340+
expectedError: rpccore.ErrFeeBelowMinimum,
1341+
},
1342+
{
1343+
name: "ReplacementTransactionUnderPriced error",
1344+
gatewayError: &gateway.Error{Code: gateway.ReplacementTransactionUnderPriced},
1345+
expectedError: rpccore.ErrReplacementTransactionUnderPriced,
1346+
},
1347+
{
1348+
name: "InvalidTransactionNonce error",
1349+
gatewayError: &gateway.Error{
1350+
Code: gateway.InvalidTransactionNonce,
1351+
Message: "Invalid transaction nonce of contract at address 0x0000FFFFFFFFFF. Account nonce: 0x3; got: 0x1.",
1352+
},
1353+
expectedError: rpccore.ErrInvalidTransactionNonce.
1354+
CloneWithData("Invalid transaction nonce of contract at address 0x0000FFFFFFFFFF. Account nonce: 0x3; got: 0x1."),
1355+
},
1356+
{
1357+
name: "InvalidTransactionNonce error as ErrValidationFailure",
1358+
gatewayError: &gateway.Error{
1359+
Code: gateway.ValidateFailure,
1360+
Message: "Invalid transaction nonce of contract at address 0x0000FFFFFFFFFF. Account nonce: 0x3; got: 0x1.",
1361+
},
1362+
expectedError: rpccore.ErrInvalidTransactionNonce.
1363+
CloneWithData("Invalid transaction nonce of contract at address 0x0000FFFFFFFFFF. Account nonce: 0x3; got: 0x1."),
1364+
},
13371365
}
13381366

13391367
for _, tc := range errorTests {

0 commit comments

Comments
 (0)