Skip to content

Commit f09b47f

Browse files
authored
feat: InvalidTransactionNonce has data and new errors (#3003)
* 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 * update test data
1 parent ba6af44 commit f09b47f

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
@@ -806,7 +806,7 @@ func (h *Handler) TransactionStatusV0_7(
806806
}, nil
807807
}
808808

809-
func makeJSONErrorFromGatewayError(err error) *jsonrpc.Error {
809+
func makeJSONErrorFromGatewayError(err error) *jsonrpc.Error { //nolint:gocyclo
810810
gatewayErr, ok := err.(*gateway.Error)
811811
if !ok {
812812
return jsonrpc.Err(jsonrpc.InternalError, err.Error())
@@ -824,21 +824,29 @@ func makeJSONErrorFromGatewayError(err error) *jsonrpc.Error {
824824
case gateway.InsufficientAccountBalance:
825825
return rpccore.ErrInsufficientAccountBalanceV0_8
826826
case gateway.ValidateFailure:
827-
return rpccore.ErrValidationFailure.CloneWithData(gatewayErr.Message)
827+
if strings.Contains(gatewayErr.Message, rpccore.ErrInvalidTransactionNonce.Message) {
828+
return rpccore.ErrInvalidTransactionNonce.CloneWithData(gatewayErr.Message)
829+
} else {
830+
return rpccore.ErrValidationFailure.CloneWithData(gatewayErr.Message)
831+
}
828832
case gateway.ContractBytecodeSizeTooLarge, gateway.ContractClassObjectSizeTooLarge:
829833
return rpccore.ErrContractClassSizeTooLarge
830834
case gateway.DuplicatedTransaction:
831835
return rpccore.ErrDuplicateTx
832836
case gateway.InvalidTransactionNonce:
833-
return rpccore.ErrInvalidTransactionNonce
837+
return rpccore.ErrInvalidTransactionNonce.CloneWithData(gatewayErr.Message)
834838
case gateway.CompilationFailed:
835-
return rpccore.ErrCompilationFailed
839+
return rpccore.ErrCompilationFailed.CloneWithData(gatewayErr.Message)
836840
case gateway.InvalidCompiledClassHash:
837841
return rpccore.ErrCompiledClassHashMismatch
838842
case gateway.InvalidTransactionVersion:
839843
return rpccore.ErrUnsupportedTxVersion
840844
case gateway.InvalidContractClassVersion:
841845
return rpccore.ErrUnsupportedContractClassVersion
846+
case gateway.ReplacementTransactionUnderPriced:
847+
return rpccore.ErrReplacementTransactionUnderPriced
848+
case gateway.FeeBelowMinimum:
849+
return rpccore.ErrFeeBelowMinimum
842850
default:
843851
return rpccore.ErrUnexpectedError.CloneWithData(gatewayErr.Message)
844852
}

rpc/v9/transaction_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,34 @@ func TestAddTransaction(t *testing.T) {
13311331
gatewayError: &gateway.Error{Code: gateway.InsufficientAccountBalance},
13321332
expectedError: rpccore.ErrInsufficientAccountBalanceV0_8,
13331333
},
1334+
{
1335+
name: "FeeBelowMinimum error",
1336+
gatewayError: &gateway.Error{Code: gateway.FeeBelowMinimum},
1337+
expectedError: rpccore.ErrFeeBelowMinimum,
1338+
},
1339+
{
1340+
name: "ReplacementTransactionUnderPriced error",
1341+
gatewayError: &gateway.Error{Code: gateway.ReplacementTransactionUnderPriced},
1342+
expectedError: rpccore.ErrReplacementTransactionUnderPriced,
1343+
},
1344+
{
1345+
name: "InvalidTransactionNonce error",
1346+
gatewayError: &gateway.Error{
1347+
Code: gateway.InvalidTransactionNonce,
1348+
Message: "Expected: 2176, got: 845.",
1349+
},
1350+
expectedError: rpccore.ErrInvalidTransactionNonce.
1351+
CloneWithData("Expected: 2176, got: 845."),
1352+
},
1353+
{
1354+
name: "InvalidTransactionNonce error as ErrValidationFailure",
1355+
gatewayError: &gateway.Error{
1356+
Code: gateway.ValidateFailure,
1357+
Message: "StarknetError { code: KnownErrorCode(InvalidTransactionNonce), message: 'Invalid transaction nonce. Expected: 2176, got: 845.' }",
1358+
},
1359+
expectedError: rpccore.ErrInvalidTransactionNonce.
1360+
CloneWithData("StarknetError { code: KnownErrorCode(InvalidTransactionNonce), message: 'Invalid transaction nonce. Expected: 2176, got: 845.' }"),
1361+
},
13341362
}
13351363

13361364
for _, tc := range errorTests {

0 commit comments

Comments
 (0)