Skip to content

Commit

Permalink
neorpc: add error codes and response errors
Browse files Browse the repository at this point in the history
Close nspcc-dev#2248

Signed-off-by: Tatiana Nesterenko <tatiana@nspcc.io>
  • Loading branch information
tatiana-nspcc committed Aug 10, 2023
1 parent 87eacfe commit 2a34841
Show file tree
Hide file tree
Showing 3 changed files with 1,036 additions and 540 deletions.
241 changes: 207 additions & 34 deletions pkg/neorpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,215 @@ const (
)

// RPC error codes defined by the Neo JSON-RPC specification extension.
// Codes for missing items.
const (
// RPCErrorCode is returned on RPC request processing error.
RPCErrorCode = -100
// ErrUnknownBlockCode is returned from a call that accepts as a parameter or searches for a header or a block
// as a part of its job can't find it.
ErrUnknownBlockCode = -101
// ErrUnknownContractCode is returned from a call that accepts as a parameter or searches for a contract
// as a part of its job can't find it.
ErrUnknownContractCode = -102
// ErrUnknownTransactionCode is returned from a call that accepts as a parameter or searches for a transaction
// as a part of its job can't find it.
ErrUnknownTransactionCode = -103
// ErrUnknownStorageItemCode is returned from a call that looks for an item in the contract storage
// as part of its job can't find it.
ErrUnknownStorageItemCode = -104
// ErrUnknownScriptContainerCode is returned from a call that accepts as a parameter or searches for a script
// container (a block or transaction) as a part of its job can't find it
// (this error generalizes -101 and -103 in cases where it's needed).
ErrUnknownScriptContainerCode = -105
// ErrUnknownStateRootCode is returned from a call that accepts as a parameter or searches for a state root
// as a part of its job can't find it.
ErrUnknownStateRootCode = -106
// ErrUnknownSessionCode is returned from a call that accepts as a parameter or searches for an iterator session
// as a part of its job can't find it.
ErrUnknownSessionCode = -107
// ErrUnknownIteratorCode is returned from a call that accepts as a parameter or searches for a session iterator
// as a part of its job can't find it.
ErrUnknownIteratorCode = -108
// ErrUnknownHeightCode is returned if block or header height passed as parameter or calculated during call
// execution is not correct (out of the range known to the node).
ErrUnknownHeightCode = -109
)

// Codes for calls that use a wallet (-300...-304) can be returned by the C# RPC server only,
// see the https://github.com/nspcc-dev/neo-go/blob/master/docs/rpc.md#unsupported-methods.
const (
// ErrInsufficientFundsWalletCode is returned if transaction that sends some assets can't be created
// because it fails. Can be returned only by the C# RPC server.
ErrInsufficientFundsWalletCode = -300
// ErrWalletFeeLimitCode is returned if transaction requires more network fee to be paid
// than is allowed by settings. Can be returned only by the C# RPC server.
ErrWalletFeeLimitCode = -301
// ErrNoOpenedWalletCode is returned if server doesn't have any opened wallet to operate with.
// Can be returned only by the C# RPC server.
ErrNoOpenedWalletCode = -302
// ErrWalletNotFoundCode is returned if specified (or configured) wallet file path is invalid.
// Can be returned only by the C# RPC server.
ErrWalletNotFoundCode = -303
// ErrWalletNotSupportedCode is returned if specified (or configured) file can't be opened as a wallet.
// Can be returned only by the C# RPC server.
ErrWalletNotSupportedCode = -304
)

// Inventory verification or verification script errors.
const (
// ErrVerificationFailedCode is returned on anything that can't be expressed by other codes.
// It is an unclassified inventory verification error.
ErrVerificationFailedCode = -500
// ErrAlreadyExistsCode is returned if block or transaction is already accepted and processed on chain.
ErrAlreadyExistsCode = -501
// ErrMempoolCapReachedCode is returned if no more transactions can be accepted into the memory pool
// (unless they have a priority) as its full capacity is reached.
ErrMempoolCapReachedCode = -502
// ErrAlreadyInPoolCode is returned if transaction is already pooled, but not yet accepted into a block.
ErrAlreadyInPoolCode = -503
// ErrInsufficientNetworkFeeCode is returned if transaction has incorrect (too small per Policy setting)
// network fee value.
ErrInsufficientNetworkFeeCode = -504
// ErrPolicyFailedCode is returned from a call denied by the Policy contract (one of signers is blocked) or
// if one of the Policy filters failed.
ErrPolicyFailedCode = -505
// ErrInvalidScriptCode is returned if transaction contains incorrect executable script.
ErrInvalidScriptCode = -506
// ErrInvalidAttributeCode is returned if transaction contains an invalid attribute.
ErrInvalidAttributeCode = -507
// ErrInvalidSignatureCode is returned if one of the verification scripts failed.
ErrInvalidSignatureCode = -508
// ErrInvalidSizeCode is returned if transaction or its script is too big.
ErrInvalidSizeCode = -509
// ErrExpiredTransactionCode is returned if transaction's ValidUntilBlock value is already in the past.
ErrExpiredTransactionCode = -510
// ErrInsufficientFundsCode is returned if sender doesn't have enough GAS to pay for all currently pooled transactions.
ErrInsufficientFundsCode = -511
// ErrInvalidVerificationFunctionCode is returned if contract doesn't have a verify method or
// this method doesn't return proper value.
ErrInvalidVerificationFunctionCode = -512
)

// Errors related to node configuration and various services.
const (
// ErrSessionsDisabledCode is returned if iterator session support is not enabled on the server.
ErrSessionsDisabledCode = -601
// ErrOracleDisabledCode is returned if Oracle service is not enabled in the configuration (service is not running).
ErrOracleDisabledCode = -602
// ErrOracleRequestFinishedCode is returned if Oracle request submitted is already completely processed.
// Can be returned only by the C# RPC server.
ErrOracleRequestFinishedCode = -603
// ErrOracleRequestNotFoundCode is returned if Oracle request submitted is not known to this node.
// Can be returned only by the C# RPC server.
ErrOracleRequestNotFoundCode = -604
// ErrOracleNotDesignatedNodeCode is returned if Oracle service is enabled, but this node is not designated
// to provide this functionality. Can be returned only by the C# RPC server.
ErrOracleNotDesignatedNodeCode = -605
// ErrUnsupportedStateCode is returned if this node can't answer requests for old state because it's configured
// to keep only the latest one. Can be returned only by the C# RPC server.
ErrUnsupportedStateCode = -606
// ErrInvalidProofCode is returned if state proof verification failed.
ErrInvalidProofCode = -607
// ErrExecutionFailedCode is returned from a call made a VM execution, but it has failed.
ErrExecutionFailedCode = -608
)

var (
// ErrInvalidParams represents a generic 'invalid parameters' error.
ErrInvalidParams = NewInvalidParamsError("invalid params")
// ErrUnknownBlock is returned if requested block is not found.
ErrUnknownBlock = NewError(RPCErrorCode, "Unknown block", "")
// ErrUnknownTransaction is returned if requested transaction is not found.
ErrUnknownTransaction = NewError(RPCErrorCode, "Unknown transaction", "")
// ErrUnknownHeader is returned when requested header is not found.
ErrUnknownHeader = NewError(RPCErrorCode, "Unknown header", "")
// ErrUnknownScriptContainer is returned when requested block or transaction is not found.
ErrUnknownScriptContainer = NewError(RPCErrorCode, "Unknown script container", "")
// ErrUnknownStateRoot is returned when requested state root is not found.
ErrUnknownStateRoot = NewError(RPCErrorCode, "Unknown state root", "")
// ErrAlreadyExists represents SubmitError with code -501.
ErrAlreadyExists = NewSubmitError(-501, "Block or transaction already exists and cannot be sent repeatedly.")
// ErrOutOfMemory represents SubmitError with code -502.
ErrOutOfMemory = NewSubmitError(-502, "The memory pool is full and no more transactions can be sent.")
// ErrUnableToVerify represents SubmitError with code -503.
ErrUnableToVerify = NewSubmitError(-503, "The block cannot be validated.")
// ErrValidationFailed represents SubmitError with code -504.
ErrValidationFailed = NewSubmitError(-504, "Block or transaction validation failed.")
// ErrPolicyFail represents SubmitError with code -505.
ErrPolicyFail = NewSubmitError(-505, "One of the Policy filters failed.")
// ErrUnknown represents SubmitError with code -500.
ErrUnknown = NewSubmitError(-500, "Unknown error.")
// ErrInvalidParams represents a generic "Invalid params" error.
ErrInvalidParams = NewInvalidParamsError("Invalid params")

// ErrUnknownBlock represents an error with code -101.
// Call that accepts as a parameter or searches for a header or a block as a part of its job can't find it.
ErrUnknownBlock = NewErrorWithCode(ErrUnknownBlockCode, "Unknown block")
// ErrUnknownContract represents an error with code -102.
// Call that accepts as a parameter or searches for a contract as a part of its job can't find it.
ErrUnknownContract = NewErrorWithCode(ErrUnknownContractCode, "Unknown contract")
// ErrUnknownTransaction represents an error with code -103.
// Call that accepts as a parameter or searches for a transaction as a part of its job can't find it.
ErrUnknownTransaction = NewErrorWithCode(ErrUnknownTransactionCode, "Unknown transaction")
// ErrUnknownStorageItem represents an error with code -104.
// Call that looks for an item in the contract storage as part of its job can't find it.
ErrUnknownStorageItem = NewErrorWithCode(ErrUnknownStorageItemCode, "Unknown storage item")
// ErrUnknownScriptContainer represents an error with code -105.
// Call that accepts as a parameter or searches for a script container (a block or transaction)
// as a part of its job can't find it (this error generalizes -101 and -103 in cases where it's needed).
ErrUnknownScriptContainer = NewErrorWithCode(ErrUnknownScriptContainerCode, "Unknown script container")
// ErrUnknownStateRoot represents an error with code -106.
// Call that accepts as a parameter or searches for a state root as a part of its job can't find it.
ErrUnknownStateRoot = NewErrorWithCode(ErrUnknownStateRootCode, "Unknown state root")
// ErrUnknownSession represents an error with code -107.
// Call that accepts as a parameter or searches for an iterator session as a part of its job can't find it.
ErrUnknownSession = NewErrorWithCode(ErrUnknownSessionCode, "Unknown session")
// ErrUnknownIterator represents an error with code -108.
// Call that accepts as a parameter or searches for a session iterator as a part of its job can't find it.
ErrUnknownIterator = NewErrorWithCode(ErrUnknownIteratorCode, "Unknown iterator")
// ErrUnknownHeight represents an error with code -109.
// Block or header height passed as parameter or calculated during call execution is not correct
// (out of the range known to the node).
ErrUnknownHeight = NewErrorWithCode(ErrUnknownHeightCode, "Unknown height")

// ErrVerificationFailed represents an error with code -500.
// Any verification error that can't be expressed by other codes.
ErrVerificationFailed = NewErrorWithCode(ErrVerificationFailedCode, "Unclassified inventory verification error")
// ErrAlreadyExists represents an error with code -501.
// Block or transaction is already accepted and processed on chain.
ErrAlreadyExists = NewErrorWithCode(ErrAlreadyExistsCode, "Inventory already exists on chain")
// ErrMempoolCapReached represents an error with code -502.
// No more transactions can be accepted into the memory pool (unless they have a priority) as its full capacity is reached.
ErrMempoolCapReached = NewErrorWithCode(ErrMempoolCapReachedCode, "The memory pool is full and no more transactions can be sent")
// ErrAlreadyInPool represents an error with code -503.
// Transaction is already pooled, but not yet accepted into a block.
ErrAlreadyInPool = NewErrorWithCode(ErrAlreadyInPoolCode, "Transaction already exists in the memory pool")
// ErrInsufficientNetworkFee represents an error with code -504.
// Transaction has incorrect (too small per Policy setting) network fee value.
ErrInsufficientNetworkFee = NewErrorWithCode(ErrInsufficientNetworkFeeCode, "Insufficient network fee")
// ErrPolicyFailed represents an error with code -505.
// Denied by the Policy contract (one of signers is blocked).
ErrPolicyFailed = NewErrorWithCode(ErrPolicyFailedCode, "One of the Policy filters failed")
// ErrInvalidScript represents an error with code -506.
// Transaction contains incorrect executable script.
ErrInvalidScript = NewErrorWithCode(ErrInvalidScriptCode, "Invalid script")
// ErrInvalidAttribute represents an error with code -507.
// Transaction contains an invalid attribute.
ErrInvalidAttribute = NewErrorWithCode(ErrInvalidAttributeCode, "Invalid transaction attribute")
// ErrInvalidSignature represents an error with code -508.
// One of the verification scripts failed.
ErrInvalidSignature = NewErrorWithCode(ErrInvalidSignatureCode, "Invalid signature")
// ErrInvalidSize represents an error with code -509.
// Transaction or its script is too big.
ErrInvalidSize = NewErrorWithCode(ErrInvalidSizeCode, "Invalid inventory size")
// ErrExpiredTransaction represents an error with code -510.
// Transaction's ValidUntilBlock value is already in the past.
ErrExpiredTransaction = NewErrorWithCode(ErrExpiredTransactionCode, "Expired transaction")
// ErrInsufficientFunds represents an error with code -511.
// Sender doesn't have enough GAS to pay for all currently pooled transactions.
ErrInsufficientFunds = NewErrorWithCode(ErrInsufficientFundsCode, "Insufficient funds")
// ErrInvalidVerificationFunction represents an error with code -512.
// Contract doesn't have a verify method or this method doesn't return proper value.
ErrInvalidVerificationFunction = NewErrorWithCode(ErrInvalidVerificationFunctionCode, "Invalid verification function")

// ErrSessionsDisabled represents an error with code -601.
// Iterator session support is not enabled on the server.
ErrSessionsDisabled = NewErrorWithCode(ErrSessionsDisabledCode, "Sessions disabled")
// ErrOracleDisabled represents an error with code -602.
// Service is not enabled in the configuration.
ErrOracleDisabled = NewErrorWithCode(ErrOracleDisabledCode, "Oracle service is not running")
// ErrOracleRequestFinished represents an error with code -603. Can be returned only by the C# RPC server.
// The oracle request submitted is already completely processed.
ErrOracleRequestFinished = NewErrorWithCode(ErrOracleRequestFinishedCode, "Oracle request has already been finished")
// ErrOracleRequestNotFound represents an error with code -604. Can be returned only by the C# RPC server.
// The oracle request submitted is not known to this node.
ErrOracleRequestNotFound = NewErrorWithCode(ErrOracleRequestNotFoundCode, "Oracle request is not found")
// ErrOracleNotDesignatedNode represents an error with code -605. Can be returned only by the C# RPC server.
// Oracle service is enabled, but this node is not designated to provide this functionality.
ErrOracleNotDesignatedNode = NewErrorWithCode(ErrOracleNotDesignatedNodeCode, "Not a designated oracle node")
// ErrUnsupportedState represents an error with code -606.
// This node can't answer requests for old state because it's configured to keep only the latest one.
ErrUnsupportedState = NewErrorWithCode(ErrUnsupportedStateCode, "Old state requests are not supported")
// ErrInvalidProof represents an error with code -607.
// State proof verification failed.
ErrInvalidProof = NewErrorWithCode(ErrInvalidProofCode, "Invalid proof")
// ErrExecutionFailed represents an error with code -608.
// Call made a VM execution, but it has failed.
ErrExecutionFailed = NewErrorWithCode(ErrExecutionFailedCode, "Execution failed")
)

// NewError is an Error constructor that takes Error contents from its parameters.
Expand Down Expand Up @@ -98,15 +277,9 @@ func NewInternalServerError(data string) *Error {
return NewError(InternalServerErrorCode, "Internal error", data)
}

// NewRPCError creates a new error with
// code -100.
func NewRPCError(message string, data string) *Error {
return NewError(RPCErrorCode, message, data)
}

// NewSubmitError creates a new error with
// NewErrorWithCode creates a new error with
// specified error code and error message.
func NewSubmitError(code int64, message string) *Error {
func NewErrorWithCode(code int64, message string) *Error {
return NewError(code, message, "")
}

Expand Down
Loading

0 comments on commit 2a34841

Please sign in to comment.