Skip to content

Commit 4ec8da3

Browse files
committed
staticaddr: withdrawal changes to be squashed
1 parent 904ccca commit 4ec8da3

File tree

7 files changed

+104
-109
lines changed

7 files changed

+104
-109
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 BLOB
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 & 23 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: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package deposit
22

33
import (
4+
"bytes"
45
"context"
56
"database/sql"
67

@@ -39,10 +40,6 @@ func (s *SqlStore) CreateDeposit(ctx context.Context, deposit *Deposit) error {
3940
Amount: int64(deposit.Value),
4041
ConfirmationHeight: deposit.ConfirmationHeight,
4142
TimeoutSweepPkScript: deposit.TimeOutSweepPkScript,
42-
WithdrawalSweepAddress: sql.NullString{
43-
String: deposit.WithdrawalSweepAddress,
44-
Valid: deposit.WithdrawalSweepAddress != "",
45-
},
4643
}
4744

4845
updateArgs := sqlc.InsertDepositUpdateParams{
@@ -81,16 +78,26 @@ func (s *SqlStore) UpdateDeposit(ctx context.Context, deposit *Deposit) error {
8178
}
8279
)
8380

81+
var finalizedWithdrawalTx []byte
82+
if deposit.FinalizedWithdrawalTx != nil {
83+
var buffer bytes.Buffer
84+
err := deposit.FinalizedWithdrawalTx.Serialize(
85+
&buffer,
86+
)
87+
if err != nil {
88+
return err
89+
}
90+
91+
finalizedWithdrawalTx = buffer.Bytes()
92+
}
93+
8494
updateArgs := sqlc.UpdateDepositParams{
85-
DepositID: deposit.ID[:],
86-
TxHash: txHash,
87-
OutIndex: outIndex.Int32,
88-
ConfirmationHeight: confirmationHeight.Int64,
89-
ExpirySweepTxid: deposit.ExpirySweepTxid[:],
90-
WithdrawalSweepAddress: sql.NullString{
91-
String: deposit.WithdrawalSweepAddress,
92-
Valid: deposit.WithdrawalSweepAddress != "",
93-
},
95+
DepositID: deposit.ID[:],
96+
TxHash: txHash,
97+
OutIndex: outIndex.Int32,
98+
ConfirmationHeight: confirmationHeight.Int64,
99+
ExpirySweepTxid: deposit.ExpirySweepTxid[:],
100+
FinalizedWithdrawalTx: finalizedWithdrawalTx,
94101
}
95102

96103
return s.baseDB.ExecTx(ctx, &loopdb.SqliteTxOptions{},
@@ -200,17 +207,28 @@ func (s *SqlStore) toDeposit(row sqlc.Deposit,
200207
expirySweepTxid = *hash
201208
}
202209

210+
var finalizedWithdrawalTx *wire.MsgTx
211+
if row.FinalizedWithdrawalTx != nil {
212+
finalizedWithdrawalTx = &wire.MsgTx{}
213+
err := finalizedWithdrawalTx.Deserialize(bytes.NewReader(
214+
row.FinalizedWithdrawalTx,
215+
))
216+
if err != nil {
217+
return nil, err
218+
}
219+
}
220+
203221
return &Deposit{
204222
ID: id,
205223
state: fsm.StateType(lastUpdate.UpdateState),
206224
OutPoint: wire.OutPoint{
207225
Hash: *txHash,
208226
Index: uint32(row.OutIndex),
209227
},
210-
Value: btcutil.Amount(row.Amount),
211-
ConfirmationHeight: row.ConfirmationHeight,
212-
TimeOutSweepPkScript: row.TimeoutSweepPkScript,
213-
ExpirySweepTxid: expirySweepTxid,
214-
WithdrawalSweepAddress: row.WithdrawalSweepAddress.String,
228+
Value: btcutil.Amount(row.Amount),
229+
ConfirmationHeight: row.ConfirmationHeight,
230+
TimeOutSweepPkScript: row.TimeoutSweepPkScript,
231+
ExpirySweepTxid: expirySweepTxid,
232+
FinalizedWithdrawalTx: finalizedWithdrawalTx,
215233
}, nil
216234
}

0 commit comments

Comments
 (0)