From c933100d80540c927e84d98917a667aa2e576a87 Mon Sep 17 00:00:00 2001 From: timlucmiptev Date: Mon, 9 Nov 2020 14:29:30 +0200 Subject: [PATCH] support for req-id --- BTC.scratch.md | 8 ++--- app/btc-provider.hoon | 66 +++++++++++++++++++++++----------------- app/btc-wallet-hook.hoon | 5 ++- sur/btc-provider.hoon | 8 +++-- sur/btc-wallet-hook.hoon | 5 ++- 5 files changed, 53 insertions(+), 39 deletions(-) diff --git a/BTC.scratch.md b/BTC.scratch.md index 9060db63f..098b1aac5 100644 --- a/BTC.scratch.md +++ b/BTC.scratch.md @@ -4,12 +4,12 @@ The below requires norsyr's fix to `decompress-point` in order to work. ## Set Credentials and Ping Servers (don't need password if all run on localhost) ``` -=rpc-pass 'c7dc0698a2e3a5d66096152b34c273b8fbcc821e9d49f2ac706e38307d3441c5' +=rpc-pass 'c55932944eee4de272a22678b9592a25782283e76361fbe671369616e18b1f46' :btc-provider|command [%set-credentials [rpc-url='http://localhost:8332' rpc-user='__cookie__' rpc-pass] [rpc-url='http://localhost:50002']] -:btc-provider|action [%address-info [%bech32 'bc1q59u5epktervh6fxqay2dlph0wxu9hjnx6v8n66']] -:btc-provider|action [%address-info [%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm']] -:btc-provider|action [%address-info [%bech32 'bc1qglkc9zfcn04vcc88nn0ljtxcpu5uxfznc3829k']] +:btc-provider|action ['addr0' %address-info [%bech32 'bc1q59u5epktervh6fxqay2dlph0wxu9hjnx6v8n66']] +:btc-provider|action ['addr1' %address-info [%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm']] +:btc-provider|action ['addr2' %address-info [%bech32 'bc1qglkc9zfcn04vcc88nn0ljtxcpu5uxfznc3829k']] :: first is an address w balance :: second has no balance but is used :: third is unused diff --git a/app/btc-provider.hoon b/app/btc-provider.hoon index d6c9f34a8..2043b2d43 100644 --- a/app/btc-provider.hoon +++ b/app/btc-provider.hoon @@ -73,7 +73,7 @@ :_ this :~ :* %pass /ping/[(scot %da now.bowl)] %agent [our.bowl %btc-provider] %poke - %btc-provider-action !>([%ping ~]) + %btc-provider-action !>([%blank-id %ping ~]) == (start-ping-timer:hc ~s30) == @@ -107,13 +107,13 @@ ++ handle-action |= act=action ^- (quip card _state) - ?. ?|(connected.host-info =(-.act %ping)) + ?. ?|(connected.host-info =(-.body.act %ping)) ~& >>> "Not connected to RPC" - [~[(send-update [%| [%not-connected 500]])] state] + [~[(send-update [%| %not-connected 500])] state] =/ ract=action:rpc - ?- -.act + ?- -.body.act %address-info - [%erpc %get-address-utxos address.act] + [%erpc %get-address-utxos address.body.act] :: %ping [%brpc %get-block-count ~] @@ -125,16 +125,21 @@ =/ req=request:http (gen-request host-info ract) [%pass (mk-wire act ract) %arvo %i %request req out] -:: wire structure: /action-tas/rpc-action-tas/(address, if rpc-action %erpc)/now +:: wire structure: /action-tas/rpc-action-tas/req-id/(address, if rpc-action %erpc)/now :: ++ mk-wire |= [act=action ract=action:rpc] ^- wire =/ addr=path ?:(?=(%erpc -.ract) /(address-to-cord:elib address.ract) /) - %- zing ~[/[-.act]/[-.ract] addr /[(scot %da now.bowl)]] -:: TODO: write up how some responses can trigger a req-card to get more info (i.e. used -:: that happens when multiple RPC calls have to be collated into one response + %- zing + :~ /[-.body.act]/[-.ract]/[req-id.act] + addr + /[(scot %da now.bowl)] + == +:: Handles HTTP responses from RPC servers. Parses for errors, then handles response. +:: For actions that require collating multiple RPC calls, uses req-card to call out +:: to RPC again if more information is required. :: ++ handle-rpc-response |= [=wire response=client-response:iris] @@ -155,29 +160,33 @@ :: no error, switch on wire to handle RPC data :: ?+ wire ~|("Unexpected HTTP response" !!) - [%address-info %erpc @ *] - =/ addr=address:btc (address-from-cord:elib +>-.wire) - =/ eresp (parse-response:electrum-rpc:elib rpc-resp) - :_ state - ^- (list card) - :~ ?- -.eresp - %get-address-utxos - ?: =(0 ~(wyt in utxos.eresp)) - (req-card [%address-info addr] [%erpc %get-address-history addr]) - (send-update [%& %address-info addr utxos.eresp %.y]) - :: - %get-address-history - %- send-update - :* %& %address-info addr *(set utxo:btc) - ?:(=(0 ~(wyt in txs.eresp)) %.n %.y) - == - == - == + [%address-info %erpc @ @ *] + [(handle-address-info wire rpc-resp) state] :: [%ping %brpc *] `state(connected.host-info %.y) == :: +++ handle-address-info + |= [=wire rpc-resp=response:rpc:jstd] + ^- (list card) + =/ req-id=@t +>-.wire + =/ addr=address:btc (address-from-cord:elib +>+<.wire) + =/ eresp (parse-response:electrum-rpc:elib rpc-resp) + :~ ?- -.eresp + %get-address-utxos + ?: =(0 ~(wyt in utxos.eresp)) + (req-card [req-id %address-info addr] [%erpc %get-address-history addr]) + (send-update [%& req-id %address-info addr utxos.eresp %.y]) + :: + %get-address-history + %- send-update + :* %& req-id %address-info addr *(set utxo:btc) + ?:(=(0 ~(wyt in txs.eresp)) %.n %.y) + == + == + == +:: ++ connection-error |= status=@ud ^- [(unit error) _state] @@ -195,7 +204,8 @@ == :: ++ send-update - |= =update ^- card + |= =update + ^- card ~& >> "send-update: {}" [%give %fact [/clients]~ %btc-provider-update !>(update)] :: diff --git a/app/btc-wallet-hook.hoon b/app/btc-wallet-hook.hoon index 623822e87..627882cc5 100644 --- a/app/btc-wallet-hook.hoon +++ b/app/btc-wallet-hook.hoon @@ -12,7 +12,7 @@ :: Sends updates to: :: none :: -/- *btc, *btc-wallet-hook, bws=btc-wallet-store +/- *btc, *btc-wallet-hook, bws=btc-wallet-store, bp=btc-provider /+ shoe, dbug, default-agent, bwsl=btc-wallet-store |% +$ versioned-state @@ -147,4 +147,7 @@ :* %pass /[(scot %da now.bowl)] %agent [host %btc-provider] %poke %btc-provider-action !>([%get-address-info a]) == +++ mk-req-id + |= hash=@ux ^- req-id:bp + (scot %ux hash) -- diff --git a/sur/btc-provider.hoon b/sur/btc-provider.hoon index 543c73ee2..168415c57 100644 --- a/sur/btc-provider.hoon +++ b/sur/btc-provider.hoon @@ -4,11 +4,13 @@ +$ electrum-credentials [rpc-url=@t] +$ credentials [bc=btc-credentials ec=electrum-credentials] +$ host-info [creds=credentials connected=? clients=(set ship)] -+$ action ++$ action [req-id=@t body=action-body] ++$ action-body $% [%address-info =address] [%ping ~] == -+$ result ++$ result [req-id=@t body=result-body] ++$ result-body $% [%address-info a=address utxos=(set utxo) used=?] == +$ error @@ -18,7 +20,7 @@ [%http-error status=@ud] [%rpc-error ~] == -+$ update (each result error) :: sub updates to /clients path (connection etc.) ++$ update (each result error) :: +$ command $% [%set-credentials creds=credentials] diff --git a/sur/btc-wallet-hook.hoon b/sur/btc-wallet-hook.hoon index f695b3271..80a8407bc 100644 --- a/sur/btc-wallet-hook.hoon +++ b/sur/btc-wallet-hook.hoon @@ -1,12 +1,11 @@ -/- *btc, bws=btc-wallet-store +/- *btc, bws=btc-wallet-store, bp=btc-provider |% :: req-id: hash of [xpub chyg idx] :: addo: wallet index to send result of request to :: back: lookup table of req-id -> addo :: -+$ req-id @ux +$ addo [=xpub =chyg:bws =idx:bws] -+$ back (map req-id addo) ++$ back (map req-id:bp addo) +$ action $% [%set-provider provider=ship] ==