Skip to content

Commit 81846ce

Browse files
committed
staticaddr: withdrawal changes to be squashed
1 parent b9eb942 commit 81846ce

File tree

7 files changed

+117
-111
lines changed

7 files changed

+117
-111
lines changed

loopdb/sqlc/migrations/000010_static_address_deposits.up.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ CREATE TABLE IF NOT EXISTS deposits (
2626
-- expiry_sweep_txid is the transaction id of the expiry sweep.
2727
expiry_sweep_txid BLOB,
2828

29-
-- withdrawal_sweep_address is the address that will be used to sweep the
30-
-- deposit cooperatively with the server before it has expired.
31-
withdrawal_sweep_address TEXT
29+
-- finalized_withdrawal_tx is the coop signed tx that will be used to sweep
30+
-- the deposit back to the clients wallet. It will be republished on block
31+
-- arrival and after daemon restarts.
32+
finalized_withdrawal_tx TEXT
3233
);
3334

3435
-- deposit_updates contains all the updates to a deposit.

loopdb/sqlc/models.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/static_address_deposits.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ INSERT INTO deposits (
77
confirmation_height,
88
timeout_sweep_pk_script,
99
expiry_sweep_txid,
10-
withdrawal_sweep_address
10+
finalized_withdrawal_tx
1111
) VALUES (
1212
$1,
1313
$2,
@@ -26,7 +26,7 @@ SET
2626
out_index = $3,
2727
confirmation_height = $4,
2828
expiry_sweep_txid = $5,
29-
withdrawal_sweep_address = $6
29+
finalized_withdrawal_tx = $6
3030
WHERE
3131
deposits.deposit_id = $1;
3232

loopdb/sqlc/static_address_deposits.sql.go

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staticaddr/deposit/deposit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ type Deposit struct {
5252
// ExpirySweepTxid is the transaction id of the expiry sweep.
5353
ExpirySweepTxid chainhash.Hash
5454

55-
// WithdrawalSweepAddress is the address that is used to
56-
// cooperatively sweep the deposit to before it is expired.
57-
WithdrawalSweepAddress string
55+
// FinalizedWithdrawalTx is the coop signed withdrawal transaction. It
56+
// is republished on new block arrivals and on client restarts.
57+
FinalizedWithdrawalTx *wire.MsgTx
5858

5959
sync.Mutex
6060
}

staticaddr/deposit/sql_store.go

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package deposit
22

33
import (
4+
"bytes"
45
"context"
56
"database/sql"
7+
"encoding/hex"
68

79
"github.com/btcsuite/btcd/btcutil"
810
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -39,10 +41,6 @@ func (s *SqlStore) CreateDeposit(ctx context.Context, deposit *Deposit) error {
3941
Amount: int64(deposit.Value),
4042
ConfirmationHeight: deposit.ConfirmationHeight,
4143
TimeoutSweepPkScript: deposit.TimeOutSweepPkScript,
42-
WithdrawalSweepAddress: sql.NullString{
43-
String: deposit.WithdrawalSweepAddress,
44-
Valid: deposit.WithdrawalSweepAddress != "",
45-
},
4644
}
4745

4846
updateArgs := sqlc.InsertDepositUpdateParams{
@@ -81,18 +79,34 @@ func (s *SqlStore) UpdateDeposit(ctx context.Context, deposit *Deposit) error {
8179
}
8280
)
8381

82+
var finalizedWithdrawalTx string
83+
if deposit.FinalizedWithdrawalTx != nil {
84+
var buffer bytes.Buffer
85+
err := deposit.FinalizedWithdrawalTx.Serialize(
86+
&buffer,
87+
)
88+
if err != nil {
89+
return err
90+
}
91+
92+
finalizedWithdrawalTx = hex.EncodeToString(buffer.Bytes())
93+
}
94+
8495
updateArgs := sqlc.UpdateDepositParams{
8596
DepositID: deposit.ID[:],
8697
TxHash: txHash,
8798
OutIndex: outIndex.Int32,
8899
ConfirmationHeight: confirmationHeight.Int64,
89-
ExpirySweepTxid: deposit.ExpirySweepTxid[:],
90-
WithdrawalSweepAddress: sql.NullString{
91-
String: deposit.WithdrawalSweepAddress,
92-
Valid: deposit.WithdrawalSweepAddress != "",
100+
FinalizedWithdrawalTx: sql.NullString{
101+
String: finalizedWithdrawalTx,
102+
Valid: finalizedWithdrawalTx != "",
93103
},
94104
}
95105

106+
if deposit.ExpirySweepTxid != (chainhash.Hash{}) {
107+
updateArgs.ExpirySweepTxid = deposit.ExpirySweepTxid[:]
108+
}
109+
96110
return s.baseDB.ExecTx(ctx, &loopdb.SqliteTxOptions{},
97111
func(q *sqlc.Queries) error {
98112
err := q.UpdateDeposit(ctx, updateArgs)
@@ -200,17 +214,31 @@ func (s *SqlStore) toDeposit(row sqlc.Deposit,
200214
expirySweepTxid = *hash
201215
}
202216

217+
var finalizedWithdrawalTx *wire.MsgTx
218+
if row.FinalizedWithdrawalTx.Valid {
219+
finalizedWithdrawalTx = &wire.MsgTx{}
220+
tx, err := hex.DecodeString(row.FinalizedWithdrawalTx.String)
221+
if err != nil {
222+
return nil, err
223+
}
224+
225+
err = finalizedWithdrawalTx.Deserialize(bytes.NewReader(tx))
226+
if err != nil {
227+
return nil, err
228+
}
229+
}
230+
203231
return &Deposit{
204232
ID: id,
205233
state: fsm.StateType(lastUpdate.UpdateState),
206234
OutPoint: wire.OutPoint{
207235
Hash: *txHash,
208236
Index: uint32(row.OutIndex),
209237
},
210-
Value: btcutil.Amount(row.Amount),
211-
ConfirmationHeight: row.ConfirmationHeight,
212-
TimeOutSweepPkScript: row.TimeoutSweepPkScript,
213-
ExpirySweepTxid: expirySweepTxid,
214-
WithdrawalSweepAddress: row.WithdrawalSweepAddress.String,
238+
Value: btcutil.Amount(row.Amount),
239+
ConfirmationHeight: row.ConfirmationHeight,
240+
TimeOutSweepPkScript: row.TimeoutSweepPkScript,
241+
ExpirySweepTxid: expirySweepTxid,
242+
FinalizedWithdrawalTx: finalizedWithdrawalTx,
215243
}, nil
216244
}

0 commit comments

Comments
 (0)