Skip to content

Commit

Permalink
fix: Use INSUFFICIENT_RESOURCES_FOR_VALIDATE instead of INSUFFICIENT_…
Browse files Browse the repository at this point in the history
…MAX_FEE in v8 (#2457)

* fix: Use INSUFFICIENT_RESOURCES_FOR_VALIDATE instead of INSUFFICIENT_MAX_FEE in v8

* fix: Remove automatic indentation

* fix: make format

* test: Add unit test
  • Loading branch information
hudem1 authored Feb 17, 2025
1 parent 73eb9b3 commit 2fc5f61
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 49 deletions.
29 changes: 15 additions & 14 deletions clients/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ import (
)

var (
InvalidContractClass ErrorCode = "StarknetErrorCode.INVALID_CONTRACT_CLASS"
UndeclaredClass ErrorCode = "StarknetErrorCode.UNDECLARED_CLASS"
ClassAlreadyDeclared ErrorCode = "StarknetErrorCode.CLASS_ALREADY_DECLARED"
InsufficientMaxFee ErrorCode = "StarknetErrorCode.INSUFFICIENT_MAX_FEE"
InsufficientAccountBalance ErrorCode = "StarknetErrorCode.INSUFFICIENT_ACCOUNT_BALANCE"
ValidateFailure ErrorCode = "StarknetErrorCode.VALIDATE_FAILURE"
ContractBytecodeSizeTooLarge ErrorCode = "StarknetErrorCode.CONTRACT_BYTECODE_SIZE_TOO_LARGE"
DuplicatedTransaction ErrorCode = "StarknetErrorCode.DUPLICATED_TRANSACTION"
InvalidTransactionNonce ErrorCode = "StarknetErrorCode.INVALID_TRANSACTION_NONCE"
CompilationFailed ErrorCode = "StarknetErrorCode.COMPILATION_FAILED"
InvalidCompiledClassHash ErrorCode = "StarknetErrorCode.INVALID_COMPILED_CLASS_HASH"
ContractClassObjectSizeTooLarge ErrorCode = "StarknetErrorCode.CONTRACT_CLASS_OBJECT_SIZE_TOO_LARGE"
InvalidTransactionVersion ErrorCode = "StarknetErrorCode.INVALID_TRANSACTION_VERSION"
InvalidContractClassVersion ErrorCode = "StarknetErrorCode.INVALID_CONTRACT_CLASS_VERSION"
InvalidContractClass ErrorCode = "StarknetErrorCode.INVALID_CONTRACT_CLASS"
UndeclaredClass ErrorCode = "StarknetErrorCode.UNDECLARED_CLASS"
ClassAlreadyDeclared ErrorCode = "StarknetErrorCode.CLASS_ALREADY_DECLARED"
InsufficientMaxFee ErrorCode = "StarknetErrorCode.INSUFFICIENT_MAX_FEE"
InsufficientResourcesForValidate ErrorCode = "StarknetErrorCode.INSUFFICIENT_RESOURCES_FOR_VALIDATE"
InsufficientAccountBalance ErrorCode = "StarknetErrorCode.INSUFFICIENT_ACCOUNT_BALANCE"
ValidateFailure ErrorCode = "StarknetErrorCode.VALIDATE_FAILURE"
ContractBytecodeSizeTooLarge ErrorCode = "StarknetErrorCode.CONTRACT_BYTECODE_SIZE_TOO_LARGE"
DuplicatedTransaction ErrorCode = "StarknetErrorCode.DUPLICATED_TRANSACTION"
InvalidTransactionNonce ErrorCode = "StarknetErrorCode.INVALID_TRANSACTION_NONCE"
CompilationFailed ErrorCode = "StarknetErrorCode.COMPILATION_FAILED"
InvalidCompiledClassHash ErrorCode = "StarknetErrorCode.INVALID_COMPILED_CLASS_HASH"
ContractClassObjectSizeTooLarge ErrorCode = "StarknetErrorCode.CONTRACT_CLASS_OBJECT_SIZE_TOO_LARGE"
InvalidTransactionVersion ErrorCode = "StarknetErrorCode.INVALID_TRANSACTION_VERSION"
InvalidContractClassVersion ErrorCode = "StarknetErrorCode.INVALID_CONTRACT_CLASS_VERSION"
)

type Client struct {
Expand Down
67 changes: 34 additions & 33 deletions rpc/rpccore/rpccore.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,40 @@ type TraceCacheKey struct {
}

var (
ErrContractNotFound = &jsonrpc.Error{Code: 20, Message: "Contract not found"}
ErrBlockNotFound = &jsonrpc.Error{Code: 24, Message: "Block not found"}
ErrInvalidTxHash = &jsonrpc.Error{Code: 25, Message: "Invalid transaction hash"}
ErrInvalidBlockHash = &jsonrpc.Error{Code: 26, Message: "Invalid block hash"}
ErrInvalidTxIndex = &jsonrpc.Error{Code: 27, Message: "Invalid transaction index in a block"}
ErrClassHashNotFound = &jsonrpc.Error{Code: 28, Message: "Class hash not found"}
ErrTxnHashNotFound = &jsonrpc.Error{Code: 29, Message: "Transaction hash not found"}
ErrPageSizeTooBig = &jsonrpc.Error{Code: 31, Message: "Requested page size is too big"}
ErrNoBlock = &jsonrpc.Error{Code: 32, Message: "There are no blocks"}
ErrInvalidContinuationToken = &jsonrpc.Error{Code: 33, Message: "Invalid continuation token"}
ErrTooManyKeysInFilter = &jsonrpc.Error{Code: 34, Message: "Too many keys provided in a filter"}
ErrContractError = &jsonrpc.Error{Code: 40, Message: "Contract error"}
ErrTransactionExecutionError = &jsonrpc.Error{Code: 41, Message: "Transaction execution error"}
ErrStorageProofNotSupported = &jsonrpc.Error{Code: 42, Message: "The node doesn't support storage proofs for blocks that are too far in the past"} //nolint:lll
ErrInvalidContractClass = &jsonrpc.Error{Code: 50, Message: "Invalid contract class"}
ErrClassAlreadyDeclared = &jsonrpc.Error{Code: 51, Message: "Class already declared"}
ErrInternal = &jsonrpc.Error{Code: jsonrpc.InternalError, Message: "Internal error"}
ErrInvalidTransactionNonce = &jsonrpc.Error{Code: 52, Message: "Invalid transaction nonce"}
ErrInsufficientMaxFee = &jsonrpc.Error{Code: 53, Message: "Max fee is smaller than the minimal transaction cost (validation plus fee transfer)"} //nolint:lll
ErrInsufficientAccountBalance = &jsonrpc.Error{Code: 54, Message: "Account balance is smaller than the transaction's max_fee"}
ErrValidationFailure = &jsonrpc.Error{Code: 55, Message: "Account validation failed"}
ErrCompilationFailed = &jsonrpc.Error{Code: 56, Message: "Compilation failed"}
ErrContractClassSizeTooLarge = &jsonrpc.Error{Code: 57, Message: "Contract class size is too large"}
ErrNonAccount = &jsonrpc.Error{Code: 58, Message: "Sender address is not an account contract"}
ErrDuplicateTx = &jsonrpc.Error{Code: 59, Message: "A transaction with the same hash already exists in the mempool"}
ErrCompiledClassHashMismatch = &jsonrpc.Error{Code: 60, Message: "the compiled class hash did not match the one supplied in the transaction"} //nolint:lll
ErrUnsupportedTxVersion = &jsonrpc.Error{Code: 61, Message: "the transaction version is not supported"}
ErrUnsupportedContractClassVersion = &jsonrpc.Error{Code: 62, Message: "the contract class version is not supported"}
ErrUnexpectedError = &jsonrpc.Error{Code: 63, Message: "An unexpected error occurred"}
ErrInvalidSubscriptionID = &jsonrpc.Error{Code: 66, Message: "Invalid subscription id"}
ErrTooManyAddressesInFilter = &jsonrpc.Error{Code: 67, Message: "Too many addresses in filter sender_address filter"}
ErrTooManyBlocksBack = &jsonrpc.Error{Code: 68, Message: fmt.Sprintf("Cannot go back more than %v blocks", MaxBlocksBack)}
ErrCallOnPending = &jsonrpc.Error{Code: 69, Message: "This method does not support being called on the pending block"}
ErrContractNotFound = &jsonrpc.Error{Code: 20, Message: "Contract not found"}
ErrBlockNotFound = &jsonrpc.Error{Code: 24, Message: "Block not found"}
ErrInvalidTxHash = &jsonrpc.Error{Code: 25, Message: "Invalid transaction hash"}
ErrInvalidBlockHash = &jsonrpc.Error{Code: 26, Message: "Invalid block hash"}
ErrInvalidTxIndex = &jsonrpc.Error{Code: 27, Message: "Invalid transaction index in a block"}
ErrClassHashNotFound = &jsonrpc.Error{Code: 28, Message: "Class hash not found"}
ErrTxnHashNotFound = &jsonrpc.Error{Code: 29, Message: "Transaction hash not found"}
ErrPageSizeTooBig = &jsonrpc.Error{Code: 31, Message: "Requested page size is too big"}
ErrNoBlock = &jsonrpc.Error{Code: 32, Message: "There are no blocks"}
ErrInvalidContinuationToken = &jsonrpc.Error{Code: 33, Message: "Invalid continuation token"}
ErrTooManyKeysInFilter = &jsonrpc.Error{Code: 34, Message: "Too many keys provided in a filter"}
ErrContractError = &jsonrpc.Error{Code: 40, Message: "Contract error"}
ErrTransactionExecutionError = &jsonrpc.Error{Code: 41, Message: "Transaction execution error"}
ErrStorageProofNotSupported = &jsonrpc.Error{Code: 42, Message: "The node doesn't support storage proofs for blocks that are too far in the past"} //nolint:lll
ErrInvalidContractClass = &jsonrpc.Error{Code: 50, Message: "Invalid contract class"}
ErrClassAlreadyDeclared = &jsonrpc.Error{Code: 51, Message: "Class already declared"}
ErrInternal = &jsonrpc.Error{Code: jsonrpc.InternalError, Message: "Internal error"}
ErrInvalidTransactionNonce = &jsonrpc.Error{Code: 52, Message: "Invalid transaction nonce"}
ErrInsufficientMaxFee = &jsonrpc.Error{Code: 53, Message: "Max fee is smaller than the minimal transaction cost (validation plus fee transfer)"} //nolint:lll
ErrInsufficientResourcesForValidate = &jsonrpc.Error{Code: 53, Message: "The transaction’s resources don’t cover validation or the minimal transaction fee"} //nolint:lll
ErrInsufficientAccountBalance = &jsonrpc.Error{Code: 54, Message: "Account balance is smaller than the transaction's max_fee"}
ErrValidationFailure = &jsonrpc.Error{Code: 55, Message: "Account validation failed"}
ErrCompilationFailed = &jsonrpc.Error{Code: 56, Message: "Compilation failed"}
ErrContractClassSizeTooLarge = &jsonrpc.Error{Code: 57, Message: "Contract class size is too large"}
ErrNonAccount = &jsonrpc.Error{Code: 58, Message: "Sender address is not an account contract"}
ErrDuplicateTx = &jsonrpc.Error{Code: 59, Message: "A transaction with the same hash already exists in the mempool"}
ErrCompiledClassHashMismatch = &jsonrpc.Error{Code: 60, Message: "the compiled class hash did not match the one supplied in the transaction"} //nolint:lll
ErrUnsupportedTxVersion = &jsonrpc.Error{Code: 61, Message: "the transaction version is not supported"}
ErrUnsupportedContractClassVersion = &jsonrpc.Error{Code: 62, Message: "the contract class version is not supported"}
ErrUnexpectedError = &jsonrpc.Error{Code: 63, Message: "An unexpected error occurred"}
ErrInvalidSubscriptionID = &jsonrpc.Error{Code: 66, Message: "Invalid subscription id"}
ErrTooManyAddressesInFilter = &jsonrpc.Error{Code: 67, Message: "Too many addresses in filter sender_address filter"}
ErrTooManyBlocksBack = &jsonrpc.Error{Code: 68, Message: fmt.Sprintf("Cannot go back more than %v blocks", MaxBlocksBack)}
ErrCallOnPending = &jsonrpc.Error{Code: 69, Message: "This method does not support being called on the pending block"}

// These errors can be only be returned by Juno-specific methods.
ErrSubscriptionNotFound = &jsonrpc.Error{Code: 100, Message: "Subscription not found"}
Expand Down
4 changes: 2 additions & 2 deletions rpc/v8/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,8 @@ func makeJSONErrorFromGatewayError(err error) *jsonrpc.Error {
return rpccore.ErrClassHashNotFound
case gateway.ClassAlreadyDeclared:
return rpccore.ErrClassAlreadyDeclared
case gateway.InsufficientMaxFee:
return rpccore.ErrInsufficientMaxFee
case gateway.InsufficientResourcesForValidate:
return rpccore.ErrInsufficientResourcesForValidate
case gateway.InsufficientAccountBalance:
return rpccore.ErrInsufficientAccountBalance
case gateway.ValidateFailure:
Expand Down
17 changes: 17 additions & 0 deletions rpc/v8/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/clients/gateway"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
Expand Down Expand Up @@ -1173,6 +1174,22 @@ func TestAddTransaction(t *testing.T) {
}, got)
})
}

t.Run("gateway returns InsufficientResourcesForValidate error", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockGateway := mocks.NewMockGateway(mockCtrl)
mockGateway.
EXPECT().
AddTransaction(gomock.Any(), gomock.Any()).
Return(nil, &gateway.Error{Code: gateway.InsufficientResourcesForValidate})

handler := rpc.New(nil, nil, nil, "", utils.NewNopZapLogger()).WithGateway(mockGateway)
addTxRes, rpcErr := handler.AddTransaction(context.Background(), tests["invoke v0"].txn)
require.Nil(t, addTxRes)
require.Equal(t, rpccore.ErrInsufficientResourcesForValidate, rpcErr)
})
}

func TestTransactionStatus(t *testing.T) {
Expand Down

0 comments on commit 2fc5f61

Please sign in to comment.