Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise a RpcError rather than a ValueError #54

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cryptoadvance/spectrum/elsock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time
from queue import Queue

from .spectrum_error import RPCError
from .util import FlaskThread, SpectrumInternalException, handle_exception

# TODO: normal handling of ctrl+C interrupt
Expand Down Expand Up @@ -559,7 +560,10 @@ def call(self, method, params=[]) -> dict:
)
res = self._results.pop(uid)
if "error" in res:
raise ValueError(res["error"])
if "code" in res["error"] and "message" in res["error"]:
raise RPCError(res["error"]["message"], res["error"]["code"])
else:
raise SpectrumInternalException(res)
if "result" in res:
return res["result"]

Expand Down
13 changes: 2 additions & 11 deletions src/cryptoadvance/spectrum/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from embit.transaction import TransactionInput, TransactionOutput
from sqlalchemy.sql import func

from .spectrum_error import RPCError
from .db import UTXO, Descriptor, Script, Tx, TxCategory, Wallet, db
from .elsock import ElectrumSocket, ElSockTimeoutException
from .util import (
Expand All @@ -36,6 +37,7 @@
scripthash,
)


logger = logging.getLogger(__name__)

# a set of registered rpc calls that do not need a wallet
Expand Down Expand Up @@ -68,17 +70,6 @@ def wrapper(*args, **kwargs):
return wrapper


class RPCError(Exception):
"""Should use one of : https://github.com/bitcoin/bitcoin/blob/v22.0/src/rpc/protocol.h#L25-L88"""

def __init__(self, message, code=-1): # -1 is RPC_MISC_ERROR
self.message = message
self.code = code

def to_dict(self):
return {"code": self.code, "message": self.message}


# we detect chain by looking at the hash of the 0th block
ROOT_HASHES = {
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f": "main",
Expand Down
9 changes: 9 additions & 0 deletions src/cryptoadvance/spectrum/spectrum_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RPCError(Exception):
"""Should use one of : https://github.com/bitcoin/bitcoin/blob/v22.0/src/rpc/protocol.h#L25-L88"""

def __init__(self, message, code=-1): # -1 is RPC_MISC_ERROR
self.message = message
self.code = code

def to_dict(self):
return {"code": self.code, "message": self.message}
Loading