cryptoadvance / spectrum Goto Github PK
View Code? Open in Web Editor NEWElectrum adaptor simulating Bitcoin RPC protocol
License: MIT License
Electrum adaptor simulating Bitcoin RPC protocol
License: MIT License
We know that there are issues with running several nodes on the same network. E.g. a spectrum_node and a Bitcoin core node on mainnet. There might be bugs but there are also conceptual issues. Let's focus on the conceptual issues:
~/.specter/wallets/main/mywallet.json
)spectera8d0c2bcfa1dd45c/mywallet
So now it's possible to misuse this mechanism having more than one Node on the same network and then simply recreate the wallets on the API-side. There might be bugs:
So the switch_node method might not be working properly. This might be much more severe in this cases as it is in cases where more than one node but on different networks (e.g. liquid).
Should we allow that? I think we should, but we should setup a huge warning sign.
One of the main reasons specter doesn't show the correct amounts is a not-synced node. For Spectrum, this might also be the case but there is another reason on top: Spectrum just booted up and connected to the electrum server but have not yet sunscribed to all the scripthashes it needs to. A scripthash is basically an address and this looks like this in the log:
[2023-01-23 12:07:39,618] INFO in spectrum: Now subscribed to 2900 scripthashes
[2023-01-23 12:07:44,015] INFO in spectrum: Now subscribed to 3000 scripthashes
[2023-01-23 12:07:49,189] INFO in spectrum: Now subscribed to 3100 scripthashes
[2023-01-23 12:07:53,949] INFO in spectrum: Now subscribed to 3200 scripthashes
...
Potentially, this could be shown as a percentage in the logs as the potential scripthashes are all in the DB. Simplified code from spectrum.py
sync-method:
for sc in Script.query.all():
# ignore external scripts (labeled recepients)
if sc.index is None:
continue
subscription_logging_counter += 1
if subscription_logging_counter % 100 == 0:
logger.info(
f"Now subscribed to {subscription_logging_counter} scripthashes"
)
res = self.sock.call("blockchain.scripthash.subscribe", [sc.scripthash])
if res != sc.state:
self.sync_script(sc, res)
So unless this loop is finished, specter might be outdated. This should clearly be reflexted in the UI.
The current way of showing that looks like this:
If you click on the node, you get some details of that node which currently looks like this:
This is not optimal. I think it could be shown how much blocks are still too sync but both of the screenshots don't show that.
Anyway, this is about spectrum. Here, we don't have the warning, but you can only see the blockcount of the electrum-server:
So in order to give better feedback:
So how should that be implemented? I would suggest the following:
initialblockdownload=True
, i'd suggest that we implement it so that spectrum is not giving back false until the DB-sync is completed as well.getscripthashsyncinfo
which returns the status (which can be set as an attribute on the spectrum-object while syncing and the rpc-call reads it from there).spectrum.py
is way too long), like: core_rpc.py
, wallet_rpc.py
and spectrum_rpc.py
(with getscripthashsyncinfo
as the currently only call in there)spectrum_info.jinja
.Currently, Spectrum is not deleting the wallets on the database side.
Implementation approach:
Create a rpc call in Spectrum (sth. like "delete_wallet") that can be called from the Spectrum node on the Specter Core side.
How will the address validation in Spectrum work without Bitcoin Core code in the background?
Status Quo is unknown. Needs docuementation and propably some improvements.
Currently, in the wallet constructor we have:
self.rpc = self.manager.rpc.wallet(
os.path.join(self.manager.rpc_path, self.alias)
)
This means that if the walletManager changes its rpc-instance, this will not change the rpc-class of the wallet. As this rpc-instance is used down by a lot more objects (addresses/TxList/TxItem/...) this has quite some large impact.
Fixing that is possible in these ways:
Describe the bug
Traceback (most recent call last):
File "cryptoadvance/specterext/spectrum/bridge_rpc.py", line 86, in multi
File "cryptoadvance/specterext/spectrum/bridge_rpc.py", line 86, in <listcomp>
File "cryptoadvance/spectrum/spectrum.py", line 348, in jsonrpc
File "cryptoadvance/spectrum/spectrum.py", line 343, in jsonrpc
File "cryptoadvance/spectrum/spectrum.py", line 57, in wrapper
File "cryptoadvance/spectrum/spectrum.py", line 1025, in walletcreatefundedpsbt
cryptoadvance.spectrum.spectrum.RPCError: ('Insufficient funds', -4)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "flask/app.py", line 1523, in full_dispatch_request
File "flask/app.py", line 1509, in dispatch_request
File "flask_login/utils.py", line 272, in decorated_view
File "cryptoadvance/specter/server_endpoints/wallets/wallets.py", line 531, in send_new
File "cryptoadvance/specter/commands/psbt_creator.py", line 115, in create_psbt
File "cryptoadvance/specter/wallet.py", line 1714, in createpsbt
File "cryptoadvance/specter/rpc.py", line 459, in fn
File "cryptoadvance/specterext/spectrum/bridge_rpc.py", line 98, in multi
AttributeError: 'object' object has no attribute 'status_code'
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The error message needs to bubble up in the UI to the user
Desktop (please complete the following information):
This is necessary to implement a delete functionality.
Gathering some problems / errors of the status quo in this issue:
1) Loadwallet error
Implementing the getrawtransaction
method in Spectrum should make it possible to retrieve the whitepaper from the timechain with Spectrum.
getrawtransaction
is already implemented in the ElectrumX API:
https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-transaction-get
The extension-generation process is currently assuming you need encrypted userdata. That should get adjusted like this:
--encrypted-userdata
here: https://github.com/cryptoadvance/specter-desktop/blob/master/src/cryptoadvance/specter/cli/cli_ext.py#L39 (including adding it as a param to the method signature)ExtGen
instantiation and there ...Mind the different templating syntax. Those are "metatemplates" as they are creating templates which themself are later templates.
You can test it via using --tmpl-fs-source
and checkout the dummy-project somewhere. Also i've created a small section about https://docs.specter.solutions/desktop/extensions/intro/#virtualenv-management
spectrum
[2022-07-14 09:37:21,427] INFO in spectrum: RPC called getbalances wallet_name: specterbd9330750bc34d3b/myhot
spectrum
[2022-07-14 09:37:21,435] ERROR in spectrum: FAIL method getbalances wallet specterbd9330750bc34d3b/myhot exc unsupported operand type(s) for *: 'decimal.Decimal' and 'float'
spectrum
Traceback (most recent call last):
spectrum
File "/usr/src/app/src/cryptoadvance/spectrum/spectrum.py", line 323, in jsonrpc
spectrum
res = m(wallet, *args, **kwargs)
spectrum
File "/usr/src/app/src/cryptoadvance/spectrum/spectrum.py", line 57, in wrapper
spectrum
return f(*args, **kwargs)
spectrum
File "/usr/src/app/src/cryptoadvance/spectrum/spectrum.py", line 760, in getbalances
spectrum
"trusted": round(confirmed * 1e-8, 8),
spectrum
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'
spectrum
[2022-07-14 09:37:21,441] INFO in _internal: 10.244.0.122 - - [14/Jul/2022 09:37:21] "POST /wallet/specterbd9330750bc34d3b/myhot HTTP/1.1" 200 -
spectrum
[2022-07-14 09:37:28,839] INFO in healthz: ready?
The current error is:
cryptoadvance.spectrum.spectrum.RPCError: ('Method not found (utxoupdatepsbt)', -32601)
since the RPC call utxoupdatepsbt
doesn't exist / isn't handled in Spectrum.
BDK could be used here.
More background in this issue:
cryptoadvance/specter-desktop#2173
Could be integrated here on this screen. "Forget node" button only if there is a Spectrum node already configured.
With sth. like:
app.specter.node_manager.delete_node(node, app.specter)
Needs this issue to be done first:
#13
This is needed for the whitepaper functionality in Specter.
cryptoadvance/specter-desktop#2065
It'd not yet possible to connect to a electrum via Tor. We basically need something very similiar than specter.requests_session
:
def requests_session(self, force_tor=False):
requests_session = requests.Session()
if self.only_tor or force_tor:
proxy_url = self.proxy_url
proxy_parsed_url = urlparse(self.proxy_url)
proxy_url = proxy_parsed_url._replace(
netloc="{}:{}@{}".format(
str(random.randint(10000, 0x7FFFFFFF)),
"random",
proxy_parsed_url.netloc,
)
).geturl()
requests_session.proxies["http"] = proxy_url
requests_session.proxies["https"] = proxy_url
return requests_session
Connecting to a Websocket Server is quite a standard thing and done all over the place. There are good libraries doing that job and doing that job well (error handling/performance/testing/maintenance/security fixes). We're using a custom implementation in elsock.py
and i pledge for replacing it with an external library.
Lately i looked into https://github.com/jeffthibault/python-nostr (which has a very nice and clean codebase btw) and it's using https://github.com/websocket-client/websocket-client which seem to be a quite mature thing.
Update: The idea is stupid as the ElectrumConnection is not a websocket connection but a custom socket-protocol.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.