From 57968162ecc649ad554ec1954f09763b2c230294 Mon Sep 17 00:00:00 2001 From: timlucmiptev Date: Thu, 19 Nov 2020 13:44:12 +0200 Subject: [PATCH] return txbu from sut --- WALLET.scratch.md | 8 ++++++-- app/btc-wallet-hook.hoon | 5 +++-- app/btc-wallet-store.hoon | 12 ++++++++++- lib/btc-wallet-store.hoon | 42 +++++++++++++++++++++++++-------------- sur/btc-wallet-hook.hoon | 7 +------ sur/btc-wallet-store.hoon | 16 ++++++++++++--- 6 files changed, 61 insertions(+), 29 deletions(-) diff --git a/WALLET.scratch.md b/WALLET.scratch.md index 3fd20d7ffa..d8a2c7907e 100644 --- a/WALLET.scratch.md +++ b/WALLET.scratch.md @@ -75,6 +75,7 @@ Uses `btc-wallet-hook`, with max-gap=3 ``` ### utxo selection +Creates dummy inputs and outputs. Builds a TX with them. ``` =btc -build-file %/lib/btc/hoon =bwsl -build-file %/lib/btc-wallet-store/hoon @@ -87,9 +88,12 @@ Uses `btc-wallet-hook`, with max-gap=3 =eny1 0v3uc.iuebi.5qilc.l8d87.c1k6n.7iksq.nkobs.8s5he.raq40.9ff0b.5tj3u.kjtg7.aq59e.hatv7.oioam.mlsr4.pqqcd.cnbjn.pnpi2.1m5rt.k4scg =eny2 0v1gt.mc4ca.lfs0m.q1dal.lqobu.mmlbd.2umnp.lj9dr.4pf4s.pvclr.dps96.4a6i8.rt6n9.krp0r.11kqu.ckqe4.1tmat.gr754.463aj.a4b41.jj7qg =inputs ~[[u(value val0) %0 0] [u(value val1) %0 2] [u(value val2) %0 1] [u(value val3) %1 2]] +=outputs ~[[[%bech32 'bc1q59u5epktervh6fxqay2dlph0wxu9hjnx6v8n66'] value=200.100] [[%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm'] value=200.000]] +=w *walt:bwsl +=w w(bipt %bip84) -(~(single-random-draw sut:bwsl [*walt:bwsl eny1 400.100 100 2]) inputs) -(~(single-random-draw sut:bwsl [*walt:bwsl eny2 400.100 100 2]) inputs) +(~(single-random-draw sut:bwsl [w eny1 100 outputs]) inputs) +(~(single-random-draw sut:bwsl [w eny2 100 outputs]) inputs) ``` Above tests w 2 outputs, total fees with 2 inputs of 27.500. Gives: diff --git a/app/btc-wallet-hook.hoon b/app/btc-wallet-hook.hoon index 3fd6a76569..61350c9722 100644 --- a/app/btc-wallet-hook.hoon +++ b/app/btc-wallet-hook.hoon @@ -139,9 +139,10 @@ ~[(poke-wallet-store / [%generate-address u.def-wallet %0 `[src.bowl value.act]])] :: %pay-address + :: we get passed address/payer/value + :: TODO: ask wallet to generate txbu for poym :: TODO: update poym :: send tx request out for poym - :: TODO: ask wallet to generate txbu for poym `state :: %force-retry @@ -201,7 +202,7 @@ ^- (quip card _state) ?- -.upd %generate-address - :: TODO if there's meta, add to piym + :: if no meta (payer/value), just prints address :: moons go in a jar with parent as the key :: this will let me implement moon rate-limiting :: diff --git a/app/btc-wallet-store.hoon b/app/btc-wallet-store.hoon index b78cdb261a..aefd5610f0 100644 --- a/app/btc-wallet-store.hoon +++ b/app/btc-wallet-store.hoon @@ -115,11 +115,21 @@ %generate-address =/ uw=(unit walt) (~(get by walts) xpub.act) ?~ uw - ~|("btc-wallet-store, %generate-address: non-existent wallet" !!) + ~|("btc-wallet-store: non-existent xpub" !!) =/ [a=address:btc w=walt] ~(gen-address wad u.uw chyg.act) :_ state(walts (~(put by walts) xpub.act w)) ~[(send-update [%generate-address a meta.act])] + :: + %generate-txbu + =/ uw=(unit walt) (~(get by walts) xpub.act) + ?~ uw ~&(>>> "btc-wallet-store: non-existent xpub" `state) + =/ r=(unit [=vbytes:btc =txbu]) + %~ select-utxos sut + [u.uw +>.act] + ?~ r ~&(>>> "btc-wallet-store: insufficient balance" `state) + :_ state + ~[(send-update [%generate-txbu xpub.act u.r])] == :: wallet scan algorithm: :: Initiate a batch for each chyg, with max-gap idxs in it diff --git a/lib/btc-wallet-store.hoon b/lib/btc-wallet-store.hoon index 26f6872561..8e451fad76 100644 --- a/lib/btc-wallet-store.hoon +++ b/lib/btc-wallet-store.hoon @@ -90,18 +90,22 @@ -- -- :: sut: door to select utxos -:: targ: target output amount, not including fees -:: feyb: fee in sats per byte -:: outs: number of outputs :: ++ sut -|_ [w=walt eny=@uvJ targ=sats feyb=sats outs=@] +|_ [w=walt eny=@uvJ =feyb txos=(list txo)] ++ meta-weight 10 ++ output-weight 31 + :: + ++ target-value + ^- sats + %+ roll (turn txos |=(=txo value.txo)) + |=([a=sats b=sats] (add a b)) + :: ++ base-weight ^- vbytes %+ add meta-weight - (mul outs output-weight) + (mul (lent txos) output-weight) + :: ++ input-weight ^- vbytes ?. ?=(%bip84 bipt.w) @@ -121,13 +125,22 @@ =/ cost (mul input-weight feyb) ?: (lte val cost) 0 (sub val cost) - :: Uses naive random selection. Should switch to branch-and-bound later. + :: Uses naive random selection. Should switch to branch-and-bound later. + :: + ++ inputs-to-txbu + |= is=(list input) ^- txbu + :_ txos + %+ turn is + |=(i=input [utxo.i ~ [bipt.w chyg.i idx.i]]) :: ++ select-utxos - |^ ^- (unit [=vbytes (list input)]) - %- single-random-draw - %- zing - (turn ~(val by wach.w) to-inputs) + |^ ^- (unit [=vbytes =txbu]) + =/ uis=(unit (list input)) + %- single-random-draw + %- zing + (turn ~(val by wach.w) to-inputs) + ?~ uis ~ + `[(total-vbytes u.uis) (inputs-to-txbu u.uis)] ++ to-inputs |= =addi ^- (list input) %+ turn ~(tap in utxos.addi) @@ -139,18 +152,17 @@ :: ++ single-random-draw |= is=(list input) - ^- (unit [=vbytes (list input)]) + ^- (unit (list input)) =/ rng ~(. og eny) - =/ target (add targ (mul feyb base-weight)) :: add base fees to target + =/ target (add target-value (mul feyb base-weight)) :: add base fees to target =| [select=(list input) total=sats:btc] |- ?: =(~ is) ~ =^ n rng (rads:rng (lent is)) =/ i=input (snag n is) =/ net-val (net-value value.utxo.i) - =? select (gth net-val 0) [i select] :: select if net-value > 0 + =? select (gth net-val 0) [i select] :: select if net-value > 0 =/ new-total (add total net-val) - ?: (gte new-total target) - `[(total-vbytes select) select] + ?: (gte new-total target) `select %= $ is (oust [n 1] is) total new-total diff --git a/sur/btc-wallet-hook.hoon b/sur/btc-wallet-hook.hoon index bd844fdcf6..0905f976a9 100644 --- a/sur/btc-wallet-hook.hoon +++ b/sur/btc-wallet-hook.hoon @@ -6,7 +6,6 @@ :: :: payment: a payment expected from another ship :: - address: address generated for this payment -:: txbu: tx builder -- all information needed to make a transaction for signing :: piym: incoming payments. Stores all ship moons inside :: poym: outgoing payments :: piym-watch/poym-watch: @@ -17,13 +16,9 @@ +$ pend-txbu (map req-id:bp ship) :: +$ payment [=address payer=ship value=sats] -+$ key [=bipt =chyg:bws =idx:bws] -+$ txin [=utxo raw-tx=(unit byts) =key] -+$ txout [=address value=sats] -+$ txbu [txins=(list txin) txouts=(list txout)] :: +$ piym (jar ship payment) -+$ poym (map ship txbu) ++$ poym (map ship txbu:bws) +$ piym-watch (map address ship) +$ poym-watch (map address ship) :: diff --git a/sur/btc-wallet-store.hoon b/sur/btc-wallet-store.hoon index 851bcd8001..bad8c5d354 100644 --- a/sur/btc-wallet-store.hoon +++ b/sur/btc-wallet-store.hoon @@ -39,11 +39,19 @@ max-gap=@ud confs=@ud == -:: input: utxo for a transaction +:: input: utxo for a transaction:: +:: feyb: fee per byte in sats +:: key: HD wallet path +:: txi/txo: input/output for a transaction being built +:: txbu: tx builder -- all information needed to make a transaction for signing :: +$ input [=utxo =chyg =idx] -:: todo: Set of indices; empty it out until none are left--means scanning of that batch is done -:: start: index this batch started scanning from ++$ feyb sats ++$ key [=bipt =chyg =idx] ++$ txi [=utxo raw-tx=(unit buffer) =key] ++$ txo [=address value=sats] ++$ txbu [txis=(list txi) txos=(list txo)] +:: TODO: document :: +$ batch [todo=(set idx) endpoint=idx has-used=?] +$ scans (map [xpub chyg] batch) @@ -58,10 +66,12 @@ $% [%add-wallet =xpub scan-to=(unit scon) max-gap=(unit @ud) confs=(unit @ud)] [%address-info =xpub =chyg =idx utxos=(set utxo) used=? blockcount=@ud] [%generate-address =xpub =chyg meta=(unit [payer=ship value=sats])] + [%generate-txbu =xpub txos=(list txo) feyb=sats] == :: +$ update $% [%generate-address =address meta=(unit [payer=ship value=sats])] + [%generate-txbu =xpub =vbytes =txbu] [%scan-done =xpub] == ::