cleanup piym

This commit is contained in:
timlucmiptev 2020-12-07 10:59:30 +02:00 committed by ixv
parent 29f1357a4c
commit 0b426aaf61
3 changed files with 54 additions and 55 deletions

View File

@ -13,17 +13,17 @@
:: /sign-me :: /sign-me
:: ::
/- *btc, *btc-wallet-hook, bws=btc-wallet-store /- *btc, *btc-wallet-hook, bws=btc-wallet-store
/+ dbug, default-agent, bwsl=btc-wallet-store, bp=btc-provider /+ dbug, default-agent, bp=btc-provider
|% |%
++ defaults ++ defaults
|% |%
++ moon-limit 10 ++ fam-limit 10
-- --
+$ versioned-state +$ versioned-state
$% state-0 $% state-0
== ==
:: provider: maybe ship if provider is set :: provider: maybe ship if provider is set
:: moon-limit: how many addresses a ship and its moons can request in piym :: fam-limit: how many addresses a ship and its moons can request in piym
:: feybs: fee/byte in sats used for a given ship payee :: feybs: fee/byte in sats used for a given ship payee
:: ::
+$ state-0 +$ state-0
@ -31,12 +31,12 @@
provider=(unit [host=ship connected=?]) provider=(unit [host=ship connected=?])
=btc-state =btc-state
def-wallet=(unit xpub) def-wallet=(unit xpub)
moon-limit=@ud fam-limit=@ud
feybs=(map ship sats) feybs=(map ship sats)
=reqs =reqs
=piym =piym
=poym =poym
=piym-lock =pend-piym
== ==
:: ::
+$ card card:agent:gall +$ card card:agent:gall
@ -54,7 +54,7 @@
++ on-init ++ on-init
^- (quip card _this) ^- (quip card _this)
~& > '%btc-wallet-hook initialized' ~& > '%btc-wallet-hook initialized'
:_ this(moon-limit.state moon-limit:defaults) :_ this(fam-limit.state fam-limit:defaults)
:~ [%pass /r/[(scot %da now.bowl)] %agent [our.bowl %btc-wallet-store] %watch /requests] :~ [%pass /r/[(scot %da now.bowl)] %agent [our.bowl %btc-wallet-store] %watch /requests]
[%pass /u/[(scot %da now.bowl)] %agent [our.bowl %btc-wallet-store] %watch /updates] [%pass /u/[(scot %da now.bowl)] %agent [our.bowl %btc-wallet-store] %watch /updates]
== ==
@ -152,18 +152,25 @@
=> .(poym ~, feybs (~(put by feybs) payee.act feyb)) => .(poym ~, feybs (~(put by feybs) payee.act feyb))
:_ state :_ state
~[(poke-wallet-hook payee.act [%gen-pay-address value.act])] ~[(poke-wallet-hook payee.act [%gen-pay-address value.act])]
:: can't pay yourself; comets can't pay (could spam requests)
:: must have default wallet set
:: reuses payment address for ship if exists in piym
:: ::
%gen-pay-address %gen-pay-address
:: TODO: only run this if the moon limit isn't passed
~| "Can't pay ourselves; no comets" ~| "Can't pay ourselves; no comets"
?< =(src.bowl our.bowl) ?< =(src.bowl our.bowl)
?< ?=(%pawn (clan:title src.bowl)) ?< ?=(%pawn (clan:title src.bowl))
=^ cards state
(reuse-address src.bowl value.act)
?^ cards [cards state]
:: if no reuseable address, call store to generate
::
=+ f=(fam src.bowl)
=+ n=(~(gut by num-fam.piym) f 0)
?~ def-wallet ~|("btc-wallet-hook: no def-wallet set" !!) ?~ def-wallet ~|("btc-wallet-hook: no def-wallet set" !!)
=+ fam=(piym-key src.bowl) ?: (gte n fam-limit)
?~ fam ~|("More than {<fam-limit>} addresses for moons + planet" !!)
~& >>> "not enough moon space or payer has an unconfirmed tx in to us" :_ state(num-fam.piym (~(put by num-fam.piym) f +(n)))
`state
:_ state
:~ %- poke-wallet-store :~ %- poke-wallet-store
[%generate-address u.def-wallet %0 `[src.bowl value.act]] [%generate-address u.def-wallet %0 `[src.bowl value.act]]
== ==
@ -228,6 +235,9 @@
utxos.body.p.upd used.body.p.upd block.body.p.upd utxos.body.p.upd used.body.p.upd block.body.p.upd
== ==
== ==
::
%tx-info
`state
:: ::
%raw-tx %raw-tx
?~ poym `state ?~ poym `state
@ -261,10 +271,8 @@
:: ::
?~ peta.upd ~&(> address.upd `state) ?~ peta.upd ~&(> address.upd `state)
=/ [payer=ship value=sats] u.peta.upd =/ [payer=ship value=sats] u.peta.upd
:_ (update-piym xpub.upd address.upd payer value) :_ state(ps.piym (~(put by ps.piym) payer [xpub.upd address.upd payer value]))
:~ (poke-wallet-hook payer [%ret-pay-address address.upd payer value]) ~[(poke-wallet-hook payer [%ret-pay-address address.upd payer value])]
(poke-wallet-store [%add-piym address.upd peta.upd])
==
:: ::
%generate-txbu %generate-txbu
:: txbus can potentially use the same UTXO inputs, so if another payment :: txbus can potentially use the same UTXO inputs, so if another payment
@ -274,50 +282,36 @@
?~ provider ~&(>>> "provider not set" ~) ?~ provider ~&(>>> "provider not set" ~)
%+ turn txis.txbu.upd %+ turn txis.txbu.upd
|=(=txi:bws (get-raw-tx host.u.provider txid.utxo.txi)) |=(=txi:bws (get-raw-tx host.u.provider txid.utxo.txi))
::
%saw-piym
`state
:: ::
%scan-done %scan-done
?~ def-wallet ?~ def-wallet
`state(def-wallet `xpub.upd) `state(def-wallet `xpub.upd)
`state `state
== ==
:: +piym-key: returns (unit ship), the sponsor of a ship :: +reuse-address: if piym already has address for payer,
:: checks whether :: replace address and return to payer
:: - too many moons are already stored
:: - input ship is locked (i.e. broadcast a tx not in mempool yet)
:: ::
++ piym-key ++ reuse-address
|= payer=ship |= [payer=ship value=sats]
^- (unit ship) ^- (quip card _state)
?: (~(has by piym-lock) payer) =+ p=(~(get by ps.piym) payer)
~ ?~ p `state
=/ fam=ship =+ newp=u.p(value value)
?: =(%earl (clan:title payer)) :_ state(ps.piym (~(put by ps.piym) payer newp))
(sein:title our.bowl now.bowl payer) :~ %+ poke-wallet-hook payer
payer [%ret-pay-address address.newp payer value]
=+ ps=(~(get by piym) fam) ==
?~ ps `fam
?: (lte (lent u.ps) moon-limit.state)
`fam
~
:: +update-piym: store an expected incoming payment by ship
:: moons are stored with their sponsor
:: if ship already has a payment for the payer ship, replace
:: ::
++ update-piym :: +fam: planet parent if s is a moon
|= p=payment ::
|^ ^- _state ++ fam
=+ fam=(piym-key payer.p) |= s=ship
?~ fam ~!("Too many moons or payer is locked" !!) ^- ship
=+ ps=(~(get by piym) u.fam) ?. =(%earl (clan:title s)) s
?~ ps (insert u.fam ~[p]) (sein:title our.bowl now.bowl s)
=/ i=(unit @)
(find ~[payer.p] (turn u.ps |=([* py=ship *] py)))
?~ i (insert u.fam [p u.ps])
(insert u.fam (snap u.ps u.i p))
++ insert
|= [u.fam=ship ps=(list payment)]
state(piym (~(put by piym) u.fam ps))
--
:: +update-poym-txis: :: +update-poym-txis:
:: update outgoing payment with a rawtx, if the txid is in poym's txis :: update outgoing payment with a rawtx, if the txid is in poym's txis
:: ::

View File

@ -6,19 +6,22 @@
:: blockcount included so that we only request address info when :: blockcount included so that we only request address info when
:: there's a newer block, in the case of addresses we are cooking :: there's a newer block, in the case of addresses we are cooking
:: ::
:: payment: a payment expected from another ship :: payment: a payment expected from another ship
:: - address: address generated for this payment :: - address: address generated for this payment
:: piym: incoming payments. Stores all ship moons under their planet. :: piym: incoming payments. Stores all ship moons under their planet.
:: - num-fam: total payments (addresses) outstanding for ship and its moons
:: pend-piym: incoming payment txs that peer says they have broadcast
:: poym: outgoing payments. One at a time: new replaces old :: poym: outgoing payments. One at a time: new replaces old
:: ::
+$ btc-state [block=@ud fee=sats t=@da] +$ btc-state [block=@ud fee=sats t=@da]
+$ reqs (map req-id:bp req=request:bws) +$ reqs (map req-id:bp req=request:bws)
:: ::
+$ payment [=xpub =address payer=ship value=sats] +$ payment [=xpub =address payer=ship value=sats]
+$ piym (jar ship payment) +$ piym [ps=(map ship payment) num-fam=(map ship @ud)]
+$ pend-piym (map txid payment)
+$ poym (unit txbu:bws) +$ poym (unit txbu:bws)
+$ piym-lock (map ship txid)
:: req-pay-address: request a payment address from another ship :: req-pay-address: request a payment address from another ship
:: - target of action is local ship
:: gen-pay-address: generate a payment address from our ship to another :: gen-pay-address: generate a payment address from our ship to another
:: ret-pay-address: give an address to a payer who requested it :: ret-pay-address: give an address to a payer who requested it
:: broadcast-tx: broadcast a signed-psbt, associate with poym :: broadcast-tx: broadcast a signed-psbt, associate with poym

View File

@ -41,12 +41,14 @@
max-gap=@ud max-gap=@ud
confs=@ud confs=@ud
== ==
:: input: utxo for a transaction
:: feyb: fee per byte in sats :: feyb: fee per byte in sats
:: key: HD wallet path :: key: HD wallet path
:: txi/txo: input/output for a transaction being built :: txi/txo: input/output for a transaction being built
:: txbu: tx builder -- all information needed to make a transaction for signing :: txbu: tx builder -- all information needed to make a transaction for signing
:: peta: optional payment metadata :: peta: optional payment metadata
:: ::
+$ input [=utxo =chyg =idx]
+$ peta (unit [payer=ship value=sats]) +$ peta (unit [payer=ship value=sats])
+$ feyb sats +$ feyb sats
+$ key [=bipt =chyg =idx] +$ key [=bipt =chyg =idx]