Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Rpc api 2 - #996

Merged
AdamISZ merged 3 commits into
JoinMarket-Org:masterfrom
abhishek0405:rpc-api-2
Oct 10, 2021
Merged

AdamISZ merged 3 commits into
JoinMarket-Org:masterfrom
abhishek0405:rpc-api-2

Conversation

@abhishek0405

Copy link
Copy Markdown
Contributor

Extension of rpc-api-1, backend API for the Web based Joinmarket wallet interface

@AdamISZ

AdamISZ commented Aug 23, 2021

Copy link
Copy Markdown
Member

Thanks for doing this; and don't worry about the conflict (rebase needed), we can do that later.

@AdamISZ AdamISZ mentioned this pull request Aug 25, 2021
Comment thread scripts/jmwalletd.py Resolved
Comment thread scripts/jmwalletd.py Resolved
Comment thread scripts/jmwalletd.py Resolved
Comment thread docs/JSON-RPC-API-using-jmwalletd.md Resolved
Comment thread docs/JSON-RPC-API-using-jmwalletd.md Resolved
Comment thread jmclient/jmclient/client_protocol.py Resolved
Comment thread jmclient/jmclient/taker_utils.py Resolved
@AdamISZ

AdamISZ commented Sep 8, 2021

Copy link
Copy Markdown
Member

Little update on status for anyone following: most core functions are working: create wallet, load wallet, send direct, deposit funds, send coinjoin, start and stop maker, display wallet details including utxos.

(Here by "working" i mean both the front-end at https://github.com/shobhitaa/Joinmarket-SOB and the backend here)

Still some rough edges to clean up around wallet state management, and handling how wallet state updates on the backend are communicated to the front end. People can probably start testing it on signet soon (feel free to start already if you're patient and interested).

@abhiShandy

Copy link
Copy Markdown

On downloading this branch and attempting to install the backend, I found a few issues:

  1. I had to pip install "klein" and "jwt"
  2. I am facing an OpenSSL error: OpenSSL.SSL.Error: [('system library', 'fopen', 'No such file or directory'), ('BIO routines', 'file_ctrl', 'system lib'), ('SSL routines', 'SSL_CTX_use_certificate_file', 'system lib')]

@abhishek0405

Copy link
Copy Markdown
Contributor Author

On downloading this branch and attempting to install the backend, I found a few issues:

  1. I had to pip install "klein" and "jwt"
  2. I am facing an OpenSSL error: OpenSSL.SSL.Error: [('system library', 'fopen', 'No such file or directory'), ('BIO routines', 'file_ctrl', 'system lib'), ('SSL routines', 'SSL_CTX_use_certificate_file', 'system lib')]

I think (2) will be fixed once you create a self signed certificate. Since the server is TLS, you need a key.pem, cert.pem . These must be present in the scripts/ folder.

@abhiShandy

Copy link
Copy Markdown

Thanks! That fixed it.

@AdamISZ

AdamISZ commented Sep 16, 2021

Copy link
Copy Markdown
Member

After those two minor fixes we have something that's close to viable at least for testing, so I'll try to do the rebase now (today), since master has diverged quite far ahead at this point.

Comment thread jmclient/jmclient/wallet_service.py Resolved
@AdamISZ

AdamISZ commented Sep 17, 2021 •

Copy link
Copy Markdown
Member

Rebased on master as of 828cec2 and just cleaned up some imports in f30009d.

Happy to report that the most basic tests on regtest are working fairly smoothly; was able to fund wallet and show updates to balance by returning to main wallet display.

Screenshot from 2021-09-17 13-50-30

(Obviously we have previously done a fair bit of testing of sending, receiving, coinjoining and running maker so tons more testing to do, especially after rebase which could throw up new errors with code base changes).

(Also we'll want a test module to test each of the RPC methods, and make sure we've got test suite passing).

@AdamISZ

AdamISZ commented Sep 17, 2021

Copy link
Copy Markdown
Member

I had to pip install "klein" and "jwt"

Add these dependencies to jmclient/setup.py for simplicity (though some users won't use it it's no big deal).

@AaronDewes

Copy link
Copy Markdown

Not sure if it's something you'd be willing to do, but having an OpenAPI definition for this would be very useful.

@AdamISZ

AdamISZ commented Sep 22, 2021

Copy link
Copy Markdown
Member

Not sure if it's something you'd be willing to do, but having an OpenAPI definition for this would be very useful.

Thanks. Something like this is needed for sure. (I was just thinking about versioning today, but this is a nice link thanks, also there are a ton of other things before we get there, testing and a subscription element of the API. Still working very actively on this. Note the doc I wrote early on, it is at least a start).

@AdamISZ

AdamISZ commented Sep 23, 2021 •

Copy link
Copy Markdown
Member

See the commit comment for f00a201 but, to expand:

  • A client can now subscribe to notifications over a websocket. So far I only implemented the skeleton: any active connection will get sent transaction notifications, but it needs fleshing out, and I haven't yet thought about subscribing to other events (natural: the client can subscribe to changes in state other than unconfirmed/confirmed wallet transactions, like: the backend sends a list of filtered offers for choice in the coinjoin protocol, and can send updates reflecting wallet unlocked/locked/coinjoin in process/new maker coinjoin happened/maker started/maker stopped; these can replace the current polling loop).
  • As well as fleshing out what the notifications to be sent are, we need to add in the jwt authentication mechanism for the websocket.
  • The basic structure of testing is in place, however, and I think this will help a lot moving forward. As well as a very basic test of 'start websocket, make sure a notification is received', there is also, in jmclient/test/test_wallet_rpc.py, the structure that will allow us to test the contents of all received RPC responses; see class TrialTestWRPC_DisplayWallet for how it works.
  • Added pinned versions of dependencies Klein (pre-existing) and autobahn (new, for websockets).
  • Notice that the main script for running the RPC is now just a "setup, run daemon" structure as some other scripts, because we move the functionality all into the main package jmclient. This will allow us to re-use that main daemon class in other places, and it also makes testing work better.

According to my cursory test, the functionality that was already in place in our jm-web-client client, is still there, when you run python scripts/jmwalletd.py, as before.

So you can see there is still tons to do, in particular:

  • Make the transaction subscription properly fleshed out. Currently it subscribes to transaction events via JMWalletDaemon.wallet_service.register_callbacks using callback type "all", which is explained in the documentation of jmclient.WalletService.register_callbacks (the point is that it is not a 'callback once and stop' type callback, but a ping for every related-to-wallet transaction that shows up). We are free to add specific callbacks to listen for specific tx confirmations. We need to make the websocket-server code in jmclient/websocket_server.py do something more sensible when these transactions are fired.

  • Add a test to jmclient/test/test_wallet_rpc.py for every one of the routes in the API. Most will be fairly boilerplate like the one for /display (display wallet), though some will be tricky.

  • Detailed documentation for each HTTP route and each subscription on the websocket.

  • Come up with an event that can be served over the websocket that will serve the role of "tell the client what offers are provided for their taker coinjoin", and have them send a secondary request that basically says "yes, I do want to do the current coinjoin".

  • Add in the jwt authentication mechanism on the websocket.

  • I forgot to add, better to do it sooner rather than later: add a version to the routes (e.g .../v1/..)

@AdamISZ

AdamISZ commented Sep 24, 2021

Copy link
Copy Markdown
Member

Not sure if it's something you'd be willing to do, but having an OpenAPI definition for this would be very useful.

Spent quite a bit of time looking into it. Unfortunately all the tools available are hooked into Flask specifically, and there was some specific reason (which I might revisit) as to why this setup in twisted worked with Klein but not Flask.
I did also try out the SwaggerEditor tool which looks like at least it could be a sensible way to generate docs, even if not automated, but there are a lot of details to figure out. I think my time is better spent elsewhere for now, but if someone wants to figure that part out for us that would of course be terrific. Meanwhile one of the next few commits will update the existing *.md doc to document the API as is, accurately.

@AdamISZ

AdamISZ commented Sep 24, 2021

Copy link
Copy Markdown
Member

After 18191ef docs more accurately reflect what exists in the API.

  • I removed the SNICKER for now as there's not much interest and it's just more workload.
  • The successful return vals are documented but the error responses are not yet; I also need to fix up some of the error HTTP return codes.
  • A few of the variables here are a bit unclear, even if they "work". So we're not quite at the stage where we can fix/commit to the API syntax, quite.

Next job will be: make the websocket transaction notification do what is intended, and test it with a client.

@PulpCattel PulpCattel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nits found while skimming through the code (I'm halfway through it), I'll try to keep testing this as time allows.

Comment thread jmclient/jmclient/wallet_utils.py Resolved
Comment thread jmclient/jmclient/wallet_utils.py Resolved
Comment thread jmclient/jmclient/wallet_utils.py Resolved
Comment thread jmclient/jmclient/wallet_utils.py Resolved
Comment thread docs/JSON-RPC-API-using-jmwalletd.md Resolved
Comment thread jmclient/jmclient/snicker_receiver.py Resolved
Comment thread jmclient/jmclient/websocketserver.py Resolved
Comment thread jmclient/jmclient/websocketserver.py Resolved
Comment thread jmclient/jmclient/websocketserver.py Resolved
Comment thread docs/JSON-RPC-API-using-jmwalletd.md Resolved
@PulpCattel

Copy link
Copy Markdown
Member

Yeah makes sense, you have addressed most of the stuff anyway, I'll reopen the relevant discussions if something is lost in the process.

@AdamISZ

AdamISZ commented Oct 5, 2021 •

Copy link
Copy Markdown
Member

Squashed the last 27 commits in 9f334ab to make things a bit more manageable, I think there was only one outstanding issue from the earlier comment round.

@AdamISZ

AdamISZ commented Oct 5, 2021

Copy link
Copy Markdown
Member

Shall I add a GET and POST for any config var based on values "section" and "parameter=x"? That seems like the simplest way to address the config issue (perhaps there should be a secondary GET that returns the entire config as json, it might take a moment to convert configparser->json or it might be trivial).

@PulpCattel PulpCattel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I add a GET and POST for any config var based on values "section" and "parameter=x"? That seems like the simplest way to address the config issue (perhaps there should be a secondary GET that returns the entire config as json, it might take a moment to convert configparser->json or it might be trivial).

Both points seem reasonable.

Comment thread jmclient/jmclient/yieldgenerator.py Resolved
Comment thread jmclient/jmclient/wallet_rpc.py Resolved
Comment thread jmclient/jmclient/wallet_rpc.py Resolved
@AdamISZ

AdamISZ commented Oct 7, 2021

Copy link
Copy Markdown
Member

Added config get/set in 2893c7e and tested with POSTMAN that i could read/write/read OK with those routes. I'll avoid the work of figuring out how to do "get the entire config", for now. This is theoretically enough to be working with (one very important practical element: to do a coinjoin you might want to set the transaction fee, which you can now do by setting POLICY/tx_fees in the client, otherwise you would have had to edit the config file).
As noted briefly in the API docs, this of course only affects the in-memory config var, which is not synced with what's on file at the moment unfortunately. Reloading will revert to the file. That's a separate issue from this RPC API though.

I am mostly ready to merge now. I'll spend a few hours looking at the websocket and seeing if I can improve it (the repeated tx notifications don't bother me though, I think I know the reason). And of course it's super-bare bones with only the tx notification and nothing else, but in principle that's enough for someone writing client side code to do something approximating to what they need.

If there's some other function that absolutely must be in the first version, let me know. Advanced features like payjoin will wait for a later update.

@kristapsk

Copy link
Copy Markdown
Member

I am mostly ready to merge now.

@AdamISZ I think you forgot this review comment of yourself, call to display_rescan_message_and_system_exit should not be removed for other clients. https://github.com/JoinMarket-Org/joinmarket-clientserver/pull/996/files#diff-fdd873116e791aacd2db174034fa598cca422b8ef835c2e4a1a630861107f471R717 .

@AdamISZ

AdamISZ commented Oct 7, 2021

Copy link
Copy Markdown
Member

I am mostly ready to merge now.

@AdamISZ I think you forgot this review comment of yourself, call to display_rescan_message_and_system_exit should not be removed for other clients. https://github.com/JoinMarket-Org/joinmarket-clientserver/pull/996/files#diff-fdd873116e791aacd2db174034fa598cca422b8ef835c2e4a1a630861107f471R717 .

Yes, I did. Thanks!

@AdamISZ

AdamISZ commented Oct 7, 2021

Copy link
Copy Markdown
Member

@PulpCattel i think i've finished cleaning up the bits and pieces now. The last one adds a coinjoin_state event to the websocket, making use of the new "coinjoin_state" management inside the JMWalletDaemon. It's explained in the docs/ .md

@PulpCattel PulpCattel left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried the new notification and it works, it seems that this one is only sent once, while the transaction one is still sent 4 times (but as you said, no big deal).

The maker service starts and stops correctly in benign cases, but it seems to reach a weird state when, e.g., we pass it invalid values.

In this case a min size too high.

2021-10-07 23:56:07,519 [INFO]  JoinMarket 0.9.2
2021-10-07 23:56:09,520 [INFO]  minsize (50000000000) > maxsize (67451966)
2021-10-07 23:56:09,521 [INFO]  Not a fidelity bond wallet, not announcing fidelity bond
2021-10-07 23:56:09,521 [ERROR]  Failed to create offers.
2021-10-07 23:56:09,521 [INFO]  offerlist=[]
2021-10-07 23:56:09,522 [INFO]  Shutting down YieldGenerator service.
client fac is  <jmclient.client_protocol.JMClientProtocolFactory object at 0x7b79dc203080>
2021-10-07 23:56:12,813 [INFO]  joined: #joinmarket-pit-sig irc-eu-1.darkscience.net
2021-10-07 23:56:12,814 [INFO]  Could not connect to *ALL* servers yet, waiting up to 60 more seconds.
2021-10-07 23:56:13,222 [INFO]  joined: #joinmarket-pit-sig guybrush.hackint.org
2021-10-07 23:56:13,223 [INFO]  All IRC servers connected, starting execution.

When it reaches this state, it's not possible to stop it via maker-stop (not even after fixing the other comment), the command goes through (and JoinMarket thinks it's stopped) but the service actually keeps running.

p.s. Could be only IRC connections? I didn't try to be picked for a CoinJoin while in this state.

p.p.s. Tested configset and configget and both seems to work, I was able to get a setting, set it to a different value, get it again and assert it's equal to the value I just set.

Comment thread docs/JSON-RPC-API-using-jmwalletd.md Resolved
Comment thread docs/JSON-RPC-API-using-jmwalletd.md Resolved
Comment thread jmclient/jmclient/wallet_rpc.py Resolved
@AdamISZ

AdamISZ commented Oct 8, 2021

Copy link
Copy Markdown
Member

while the transaction one is still sent 4 times (but as you said, no big deal).

This is in the weeds and not interesting, but the reason is that the underlying bitcoin wallet gets a new transaction event multiple times if there are multiple input/outputs that involve the wallet, plus it also gets a separate event for confirmed and unconfirmed; with our normal callbacks we "swallow" the event the first time a specific txid is seen (i.e. once such a callback is called, it is removed from the list, so is guaranteed to be called once), but for the special "all" type callback, we are calling back every time we see a record of a transaction that involves the wallet in any way at all, and we don't delete that callback from the list, it's supposed to keep going continuously. I'm sure it would not be super hard to change the code to make sure that such callbacks are not repeatedly called on the same transaction, but it's not enough of a priority to be worth testing and so on.

@AdamISZ

AdamISZ commented Oct 8, 2021

Copy link
Copy Markdown
Member

2021-10-07 23:56:09,520 [INFO] minsize (50000000000) > maxsize (67451966)

That's a good catch. I'll catch the assertion error in create_my_orders and set the maker's aborted to True, which will get picked up by the YGService's monitoring loop and it should shut down gracefully. Will test that now.

(Unfortunately though, there may be other cases like this dotted around)

@AdamISZ

AdamISZ commented Oct 8, 2021

Copy link
Copy Markdown
Member

When it reaches this state, it's not possible to stop it via maker-stop (not even after fixing the other comment), the command goes through (and JoinMarket thinks it's stopped) but the service actually keeps running.

The changes in 3af86d5 should have fixed this.

I tested by : maker start/stop/start/stop with valid arguments, then tried to start once more with invalid minsize, it did as expected (started then immediately shut down the service), then I did start again, and it started up correctly.

(There is still an unpleasant leftover: I believe, though didn't check in detail, that the IRC bot/nick created for the version of the maker which was invalid, is left dangling. I can fix that later; it's an edge case, and a couple of nicks hanging around, only if the maker is ill configured, until the process shuts down is not a disaster).

Can't guarantee there might be other ways to trigger Exceptions that cause an issue, but of note is that the Maker bots have been refined over a long period of time to basically not ever crash based on external input.

@AdamISZ

AdamISZ commented Oct 8, 2021

Copy link
Copy Markdown
Member

OK done a fair bit of testing of all the main functions using postman. Anybody else seeing any outstanding issues?

@AdamISZ
AdamISZ force-pushed the rpc-api-2 branch 2 times, most recently from bdc5ea7 to 66b5273 Compare October 10, 2021 09:44
AdamISZ and others added 2 commits October 10, 2021 10:50
Uses Klein to provide HTTP server support.
Adds cookie based auth to requests (made JWT token
based in later commits).
Basic routes are: /unlock, /lock, /display,
/create of wallet.
Encapsulates WalletDaemon as a Service
Add snicker receiver service start, stop
Adds yg/maker function as stoppable service.

Adds a JMShutdown command to
the AMP protocol, allowing a clean shutdown
of a long running bot (e.g. maker) by shutting
down its message channel connections, without
shutting down the entire process.

Adds payment(direct send) request, first draft
The /utxos route is the equivalent of the showutxos
wallet tool method.
The heartbeat route /session allows a client to
make sure the backend is still running and in the
expected state (but see later commits for the
coinjoin state update via the websocket).

Fixes to sendpayment, the maker service,
the create wallet function.
sendpayment fix
Also substantially improved and made
functional the coinjoin route, a schedule
is now created and a complete taker-side
coinjoin is now possible.
@AdamISZ

AdamISZ commented Oct 10, 2021

Copy link
Copy Markdown
Member

Squashed to three commits ready for merge.

1. Moves the JMWalletDaemon service class into
the jmclient package (see the wallet_rpc.py module).
2. Adds dependencies "klein" and "autobahn" to the
jmclient package, as well as "pyjwt".
3. Adds another module websocketserver.py, using
autobahn, to allow the JMWalletDaemon service to
serve subscriptions over a websocket, for e.g.
transaction notifications.
4. Adds tests both for the websocket connection
and for the JSON-RPC HTTP connection.

JmwalletdWebSocketServerFactory.sendTxNotification
sends the json-ified transaction details using
jmbitcoin.human_readable_transaction (as is currently
used in our CLI), along with the txid.
Also adds a coinjoin state update event sent via
the websocket (switch from taker/maker/none).
Require authentication to connect to websocket.

5. Add OpenApi definition of API in yaml;
also auto-create human-readable API docs in markdown.
6. Add fidelity bond function to API
7. Add config read/write route to API
8. Remove snicker rpc calls temporarily
9. Updates to docoinjoin: corrects taker_finished
   for this custom case, does not shut down at end.
10. Address detailed review comments of @PulpCattel.
@AdamISZ

AdamISZ commented Oct 10, 2021

Copy link
Copy Markdown
Member

Did lots of testing last couple of days and cleaned off a few edge cases. Merging now; obviously it's not perfect but this should be viable for someone wishing to write client code, as a first functional version. Thanks to everyone for helping out.

@AdamISZ
AdamISZ merged commit ff10262 into JoinMarket-Org:master Oct 10, 2021
kristapsk added a commit that referenced this pull request Nov 25, 2021
840189c Fix SNICKER daemon startup (Kristaps Kaupe)

Pull request description:

  Fixes #1067.

  Bug was introduced with 7e73e4c (part of #996) when return value of `start_daemon_on_port()` was changed from `int` to `tuple`.

Top commit has no ACKs.

Tree-SHA512: 76bfbbf89b66d39d277bac4aed515cd251fe5f40e6eaafc2596c2e095a60931f1cddd2b147e4fd51d07c99c6ff8c94bbc2fe661a618da0220ff4924128833031
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants