WIP: piym and history handling

This commit is contained in:
timlucmiptev 2020-12-06 13:46:32 +02:00 committed by ixv
parent b9f63c20ef
commit 893521176b
5 changed files with 86 additions and 38 deletions

View File

@ -25,7 +25,6 @@
:: provider: maybe ship if provider is set
:: moon-limit: how many addresses a ship and its moons can request in piym
:: feybs: fee/byte in sats used for a given ship payee
:: piym/poym-watch: listen to btc-wallet-store for address updates; update payment info
::
+$ state-0
$: %0
@ -36,7 +35,8 @@
feybs=(map ship sats)
=reqs
=piym
poym=(unit txbu:bws)
=poym
=piym-lock
==
::
+$ card card:agent:gall
@ -138,11 +138,12 @@
`state(def-wallet `(snag 0 xs))
::
%req-pay-address
:: TODO: add whitelisting here instead of comet block
:: overwrites any payment being built currently
:: can't pay yourself; comets can't pay (could spam requests)
:: forwards poke to payee if payee isn't us
:: wire is /payer/value/timestamp
:: deletes poym since we'll be making a new outgoing payment
:: lets us set fee per byte and recall it once we get a payment address back
:: wire is /payer/value/timestamp
::
~| "Can't pay ourselves; no comets"
?< =(src.bowl payee.act)
@ -153,10 +154,15 @@
~[(poke-wallet-hook payee.act [%gen-pay-address value.act])]
::
%gen-pay-address
:: TODO: only run this if the moon limit isn't passed
~| "Can't pay ourselves; no comets"
?< =(src.bowl our.bowl)
?< ?=(%pawn (clan:title src.bowl))
?~ def-wallet ~|("btc-wallet-hook: no def-wallet set" !!)
=+ fam=(piym-key src.bowl)
?~ fam
~& >>> "not enough moon space or payer has an unconfirmed tx in to us"
`state
:_ state
:~ %- poke-wallet-store
[%generate-address u.def-wallet %0 `[src.bowl value.act]]
@ -171,6 +177,9 @@
:~ %- poke-wallet-store
[%generate-txbu u.def-wallet `src.bowl feyb ~[[address.act value.act]]]
==
::
%broadcast-tx
`state
::
%clear-poym
`state(poym ~)
@ -248,12 +257,14 @@
^- (quip card _state)
?- -.upd
%generate-address
:: if no meta (payer/value), just prints address
:: if no peta (payer/value), just prints address
::
?~ meta.upd ~&(> address.upd `state)
=/ [payer=ship value=sats] u.meta.upd
:_ (update-piym address.upd u.meta.upd)
~[(poke-wallet-hook payer [%ret-pay-address address.upd payer value])]
?~ peta.upd ~&(> address.upd `state)
=/ [payer=ship value=sats] u.peta.upd
:_ (update-piym 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
:: txbus can potentially use the same UTXO inputs, so if another payment
@ -269,6 +280,25 @@
`state(def-wallet `xpub.upd)
`state
==
:: +piym-key: returns (unit ship), the sponsor of a ship
:: checks whether
:: - too many moons are already stored
:: - input ship is locked (i.e. broadcast a tx not in mempool yet)
::
++ piym-key
|= payer=ship
^- (unit ship)
?: (~(has by piym-lock) payer)
~
=/ fam=ship
?: =(%earl (clan:title payer))
(sein:title our.bowl now.bowl payer)
payer
=+ 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
@ -276,21 +306,17 @@
++ update-piym
|= p=payment
|^ ^- _state
=/ fam=ship
?: =(%earl (clan:title payer.p))
(sein:title our.bowl now.bowl payer.p)
payer.p
=+ ps=(~(get by piym) fam)
?~ ps (insert fam ~[p])
~| "btc-wallet-hook: too many address requests from moons"
?> (lte (lent u.ps) moon-limit.state)
=+ fam=(piym-key payer.p)
?~ fam ~!("Too many moons or payer is locked" !!)
=+ ps=(~(get by piym) u.fam)
?~ ps (insert u.fam ~[p])
=/ i=(unit @)
(find ~[payer.p] (turn u.ps |=([* py=ship *] py)))
?~ i (insert fam [p u.ps])
(insert fam (snap u.ps u.i p))
?~ i (insert u.fam [p u.ps])
(insert u.fam (snap u.ps u.i p))
++ insert
|= [fam=ship ps=(list payment)]
state(piym (~(put by piym) fam ps))
|= [u.fam=ship ps=(list payment)]
state(piym (~(put by piym) u.fam ps))
--
:: +update-poym-txis:
:: update outgoing payment with a rawtx, if the txid is in poym's txis

View File

@ -219,8 +219,8 @@
:- (weld cards0 cards1)
state(scans (insert-batches xpub batch0 batch1))
:: +update-address: watch the address passed; update wallet if it's used
:: - if address is unused, send %address-info request to monitor it
:: - if address doesn't have enough confs, send %address-info request to monitor it
:: - if address is unused, send %address-info request
:: - if address doesn't have enough confs, send %address-info request
:: - if this idx was the last in todo.scans, do run-scan to see whether scan is done
:: - updates wallet-store state to have last-block
::
@ -276,7 +276,7 @@
:: sends a request for info on the new address (watches it)
::
++ generate-address
|= [=xpub =chyg meta=(unit [payer=ship value=sats])]
|= [=xpub =chyg =peta]
=+ uw=(~(get by walts) xpub)
?~ uw
~|("btc-wallet-store: non-existent xpub" !!)
@ -285,7 +285,7 @@
=/ [addr=address:btc =idx w=walt]
~(gen-address wad u.uw chyg)
:_ state(walts (~(put by walts) xpub w))
:~ (send-update [%generate-address addr meta])
:~ (send-update [%generate-address addr peta])
%+ send-request ~[/requests]
:* %address-info last-block
addr xpub chyg idx

View File

@ -1,7 +1,7 @@
::
::
/- *btc-wallet-store
/+ bip32, btc
/+ bip32, btc, bp=btc-provider
=, secp:crypto
=+ ecc=secp256k1
|%
@ -177,9 +177,11 @@
|= =addi ^- (list input)
%+ turn ~(tap in utxos.addi)
|=(=utxo:btc [utxo chyg.addi idx.addi])
::
++ inputs-to-txbu
|= is=(list input) ^- txbu
:* payee
:* (gen-req-id:bp eny)
payee
(total-vbytes is)
%+ turn is
|=(i=input [utxo.i ~ [bipt.w chyg.i idx.i]])

View File

@ -9,20 +9,19 @@
:: payment: a payment expected from another ship
:: - address: address generated for this payment
:: piym: incoming payments. Stores all ship moons under their planet.
:: piym-watch/poym-watch:
:: let us link an address back to its incoming/outgoing payment
:: checked when address updates come from btc-wallet-store
:: poym: outgoing payments. One at a time: new replaces old
::
+$ btc-state [block=@ud fee=sats t=@da]
+$ reqs (map req-id:bp req=request:bws)
::
+$ payment [=address payer=ship value=sats]
::
+$ piym (jar ship payment)
+$ piym-watch (map address ship)
+$ poym-watch (map address ship)
+$ poym (unit txbu:bws)
+$ piym-lock (map ship txid)
:: req-pay-address: request a payment address from another ship
:: gen-pay-address: generate a payment address from our ship to another
:: ret-pay-address: give an address to a payer who requested it
:: broadcast-tx: broadcast a signed-psbt, associate with poym
::
+$ action
$% [%set-provider provider=ship]
@ -30,6 +29,7 @@
[%req-pay-address payee=ship value=sats feyb=(unit sats)]
[%gen-pay-address value=sats]
[%ret-pay-address =address payer=ship value=sats]
[%broadcast-tx =req-id:bp signed-psbt=cord]
[%clear-poym ~]
[%force-retry ~]
==

View File

@ -2,7 +2,7 @@
:: m / purpose' / coin_type' / account' / change / address_index
:: change can be 0 or 1
::
/- *btc
/- *btc, bp=btc-provider
/+ bip32
|%
++ max-index (dec (pow 2 32))
@ -44,17 +44,35 @@
:: 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
:: txbu: tx builder -- all information needed to make a transaction for signing
:: peta: optional payment metadata
::
+$ input [=utxo =chyg =idx]
+$ feyb sats
+$ key [=bipt =chyg =idx]
+$ txi [=utxo ur=(unit rawtx) =key]
+$ txo [=address value=sats]
+$ txbu [payee=(unit ship) =vbytes txis=(list txi) txos=(list txo)]
+$ txbu [=req-id:bp payee=(unit ship) =vbytes txis=(list txi) txos=(list txo)]
+$ peta (unit [payer=ship value=sats])
:: hest: an entry in the history log
:: raw: pending history elements
:: - hearsay: %.y if a peer told us about this element and we haven't observed it yet
:: yum: confirmed history elements
::
+$ hest
$: =txid
recvd=@da
inputs=(list [=utxo s=(unit ship)])
outputs=(list [=output s=(unit ship)])
==
+$ raw (list [hearsay=? block=@ud =hest])
+$ yum ((mop @ud hest) gth)
+$ history [w=walt =raw =yum]
:: state/watch variables:
:: scanning addresses and monitoring generated addresses
:: batch: indexes to scan for a given chyg
:: scans: all scans underway (batches)
:: piym-watch: any address we've been told has an incoming payment promised
::
+$ batch [todo=(set idx) endpoint=idx has-used=?]
+$ scans (map [xpub chyg] batch)
@ -68,13 +86,15 @@
+$ action
$% [%add-wallet =xpub scan-to=(unit scon) max-gap=(unit @ud) confs=(unit @ud)]
[%address-info =xpub =chyg =idx utxos=(set utxo) used=? block=@ud]
[%generate-address =xpub =chyg meta=(unit [payer=ship value=sats])]
[%generate-address =xpub =chyg =peta]
[%generate-txbu =xpub payee=(unit ship) feyb=sats txos=(list txo)]
[%add-hest =hest]
==
::
+$ update
$% [%generate-address =address meta=(unit [payer=ship value=sats])]
$% [%generate-address =address =peta]
[%generate-txbu =xpub =txbu]
[%saw-piym s=ship =txid]
[%scan-done =xpub]
==
:: last-block: most recent block this address was checked