Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: check dust outputs on account withdrawals and closures #150

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions account/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ func (m *Manager) DepositAccount(ctx context.Context,
return nil, nil, err
}
if account.State != StateOpen {
return nil, nil, fmt.Errorf("account must be in %v to be"+
return nil, nil, fmt.Errorf("account must be in %v to be "+
"modified", StateOpen)
}

Expand Down Expand Up @@ -1044,7 +1044,7 @@ func (m *Manager) WithdrawAccount(ctx context.Context,
return nil, nil, err
}
if account.State != StateOpen {
return nil, nil, fmt.Errorf("account must be in %v to be"+
return nil, nil, fmt.Errorf("account must be in %v to be "+
"modified", StateOpen)
}

Expand Down Expand Up @@ -1700,6 +1700,13 @@ func sanityCheckAccountSpendTx(tx *wire.MsgTx, account *Account,
return err
}

// None of the outputs should be dust.
for _, output := range tx.TxOut {
if txrules.IsDustOutput(output, txrules.DefaultRelayFeePerKb) {
return fmt.Errorf("dust output %x", output.PkScript)
}
}

// CheckTransactionSanity doesn't have enough context to attempt fee
// calculation, but we do.
//
Expand Down
24 changes: 18 additions & 6 deletions account/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,14 +857,27 @@ func TestAccountWithdrawal(t *testing.T) {
defer h.stop()

const bestHeight = 100
const feeRate = chainfee.FeePerKwFloor

account := h.openAccount(
maxAccountValue, bestHeight+maxAccountExpiry, bestHeight,
)

// With our account created, we'll start our withdrawal by creating the
// outputs we'll withdraw our funds to. We'll create three outputs, one
// of each supported output type. Each output will have 1/4 of the
// account's value.
// With our account created, we'll start with an invalid withdrawal to a
// dust output, which should fail.
dustOutput := &wire.TxOut{Value: 0, PkScript: p2wsh}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

_, _, err := h.manager.WithdrawAccount(
context.Background(), account.TraderKey.PubKey,
[]*wire.TxOut{dustOutput}, feeRate, bestHeight,
)
if err == nil || !strings.Contains(err.Error(), "dust output") {
t.Fatalf("expected dust output error, got: %v", err)
}

// We'll now attempt a withdrawal that should succeed. We'll start by
// creating the outputs we'll withdraw our funds to. We'll create three
// outputs, one of each supported output type. Each output will have 1/4
// of the account's value.
valuePerOutput := account.Value / 4
outputs := []*wire.TxOut{
{
Expand All @@ -884,14 +897,13 @@ func TestAccountWithdrawal(t *testing.T) {
// We'll use the lowest fee rate possible, which should yield a
// transaction fee of 260 satoshis when taking into account the outputs
// we'll be withdrawing to.
const feeRate = chainfee.FeePerKwFloor
const expectedFee btcutil.Amount = 260

// Attempt the withdrawal.
//
// If successful, we'll follow with a series of assertions to ensure it
// was performed correctly.
_, _, err := h.manager.WithdrawAccount(
_, _, err = h.manager.WithdrawAccount(
context.Background(), account.TraderKey.PubKey, outputs,
feeRate, bestHeight,
)
Expand Down