sweep

package
v0.0.0-...-49584ae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2019 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRemoteSpend is returned in case an output that we try to sweep is
	// confirmed in a tx of the remote party.
	ErrRemoteSpend = errors.New("remote party swept utxo")

	// ErrTooManyAttempts is returned in case sweeping an output has failed
	// for the configured max number of attempts.
	ErrTooManyAttempts = errors.New("sweep failed after max attempts")

	// DefaultMaxSweepAttempts specifies the default maximum number of times
	// an input is included in a publish attempt before giving up and
	// returning an error to the caller.
	DefaultMaxSweepAttempts = 10
)
View Source
var (
	// DefaultBatchWindowDuration specifies duration of the sweep batch
	// window. The sweep is held back during the batch window to allow more
	// inputs to be added and thereby lower the fee per input.
	DefaultBatchWindowDuration = 30 * time.Second
)
View Source
var (
	// DefaultMaxInputsPerTx specifies the default maximum number of inputs
	// allowed in a single sweep tx. If more need to be swept, multiple txes
	// are created and published.
	DefaultMaxInputsPerTx = 100
)

Functions

func DefaultNextAttemptDeltaFunc

func DefaultNextAttemptDeltaFunc(attempts int) int32

DefaultNextAttemptDeltaFunc is the default calculation for next sweep attempt scheduling. It implements exponential back-off with some randomness. This is to prevent a stuck tx (for example because fee is too low and can't be bumped in pfcd) from blocking all other retried inputs in the same tx.

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until UseLogger is called.

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type BaseInput

type BaseInput struct {
	// contains filtered or unexported fields
}

BaseInput contains all the information needed to sweep a basic output (CSV/CLTV/no time lock)

func MakeBaseInput

func MakeBaseInput(outpoint *wire.OutPoint, witnessType lnwallet.WitnessType,
	signDescriptor *lnwallet.SignDescriptor, heightHint uint32) BaseInput

MakeBaseInput assembles a new BaseInput that can be used to construct a sweep transaction.

func (*BaseInput) BlocksToMaturity

func (bi *BaseInput) BlocksToMaturity() uint32

BlocksToMaturity returns the relative timelock, as a number of blocks, that must be built on top of the confirmation height before the output can be spent. For non-CSV locked inputs this is always zero.

func (*BaseInput) BuildWitness

func (bi *BaseInput) BuildWitness(signer lnwallet.Signer, txn *wire.MsgTx,
	hashCache *txscript.TxSigHashes, txinIdx int) ([][]byte, error)

BuildWitness computes a valid witness that allows us to spend from the breached output. It does so by generating the witness generation function, which is parameterized primarily by the witness type and sign descriptor. The method then returns the witness computed by invoking this function.

func (*BaseInput) HeightHint

func (i *BaseInput) HeightHint() uint32

HeightHint returns the minimum height at which a confirmed spending tx can occur.

func (*BaseInput) OutPoint

func (i *BaseInput) OutPoint() *wire.OutPoint

OutPoint returns the breached output's identifier that is to be included as a transaction input.

func (*BaseInput) SignDesc

func (i *BaseInput) SignDesc() *lnwallet.SignDescriptor

SignDesc returns the breached output's SignDescriptor, which is used during signing to compute the witness.

func (*BaseInput) WitnessType

func (i *BaseInput) WitnessType() lnwallet.WitnessType

WitnessType returns the type of witness that must be generated to spend the breached output.

type HtlcSucceedInput

type HtlcSucceedInput struct {
	// contains filtered or unexported fields
}

HtlcSucceedInput constitutes a sweep input that needs a pre-image. The input is expected to reside on the commitment tx of the remote party and should not be a second level tx output.

func MakeHtlcSucceedInput

func MakeHtlcSucceedInput(outpoint *wire.OutPoint,
	signDescriptor *lnwallet.SignDescriptor,
	preimage []byte, heightHint uint32) HtlcSucceedInput

MakeHtlcSucceedInput assembles a new redeem input that can be used to construct a sweep transaction.

func (*HtlcSucceedInput) BlocksToMaturity

func (h *HtlcSucceedInput) BlocksToMaturity() uint32

BlocksToMaturity returns the relative timelock, as a number of blocks, that must be built on top of the confirmation height before the output can be spent.

func (*HtlcSucceedInput) BuildWitness

func (h *HtlcSucceedInput) BuildWitness(signer lnwallet.Signer, txn *wire.MsgTx,
	hashCache *txscript.TxSigHashes, txinIdx int) ([][]byte, error)

BuildWitness computes a valid witness that allows us to spend from the breached output. For HtlcSpendInput it will need to make the preimage part of the witness.

func (*HtlcSucceedInput) HeightHint

func (i *HtlcSucceedInput) HeightHint() uint32

HeightHint returns the minimum height at which a confirmed spending tx can occur.

func (*HtlcSucceedInput) OutPoint

func (i *HtlcSucceedInput) OutPoint() *wire.OutPoint

OutPoint returns the breached output's identifier that is to be included as a transaction input.

func (*HtlcSucceedInput) SignDesc

func (i *HtlcSucceedInput) SignDesc() *lnwallet.SignDescriptor

SignDesc returns the breached output's SignDescriptor, which is used during signing to compute the witness.

func (*HtlcSucceedInput) WitnessType

func (i *HtlcSucceedInput) WitnessType() lnwallet.WitnessType

WitnessType returns the type of witness that must be generated to spend the breached output.

type Input

type Input interface {
	// Outpoint returns the reference to the output being spent, used to
	// construct the corresponding transaction input.
	OutPoint() *wire.OutPoint

	// WitnessType returns an enum specifying the type of witness that must
	// be generated in order to spend this output.
	WitnessType() lnwallet.WitnessType

	// SignDesc returns a reference to a spendable output's sign
	// descriptor, which is used during signing to compute a valid witness
	// that spends this output.
	SignDesc() *lnwallet.SignDescriptor

	// BuildWitness returns a valid witness allowing this output to be
	// spent, the witness should be attached to the transaction at the
	// location determined by the given `txinIdx`.
	BuildWitness(signer lnwallet.Signer, txn *wire.MsgTx,
		hashCache *txscript.TxSigHashes,
		txinIdx int) ([][]byte, error)

	// BlocksToMaturity returns the relative timelock, as a number of
	// blocks, that must be built on top of the confirmation height before
	// the output can be spent. For non-CSV locked inputs this is always
	// zero.
	BlocksToMaturity() uint32

	// HeightHint returns the minimum height at which a confirmed spending
	// tx can occur.
	HeightHint() uint32
}

Input contains all data needed to construct a sweep tx input.

type MockNotifier

type MockNotifier struct {
	// contains filtered or unexported fields
}

MockNotifier simulates the chain notifier for test purposes. This type is exported because it is used in nursery tests.

func NewMockNotifier

func NewMockNotifier(t *testing.T) *MockNotifier

NewMockNotifier instantiates a new mock notifier.

func (*MockNotifier) ConfirmTx

func (m *MockNotifier) ConfirmTx(txid *chainhash.Hash, height uint32) error

ConfirmTx simulates a tx confirming.

func (*MockNotifier) NotifyEpoch

func (m *MockNotifier) NotifyEpoch(height int32)

NotifyEpoch simulates a new epoch arriving.

func (*MockNotifier) RegisterBlockEpochNtfn

func (m *MockNotifier) RegisterBlockEpochNtfn(
	bestBlock *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error)

RegisterBlockEpochNtfn registers a block notification.

func (*MockNotifier) RegisterConfirmationsNtfn

func (m *MockNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
	_ []byte, numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent,
	error)

RegisterConfirmationsNtfn registers for tx confirm notifications.

func (*MockNotifier) RegisterSpendNtfn

func (m *MockNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
	_ []byte, heightHint uint32) (*chainntnfs.SpendEvent, error)

RegisterSpendNtfn registers for spend notifications.

func (*MockNotifier) SpendOutpoint

func (m *MockNotifier) SpendOutpoint(outpoint wire.OutPoint,
	spendingTx wire.MsgTx)

SpendOutpoint simulates a utxo being spent.

func (*MockNotifier) Start

func (m *MockNotifier) Start() error

Start the notifier.

func (*MockNotifier) Stop

func (m *MockNotifier) Stop() error

Stop the notifier.

type MockSweeperStore

type MockSweeperStore struct {
	// contains filtered or unexported fields
}

MockSweeperStore is a mock implementation of sweeper store. This type is exported, because it is currently used in nursery tests too.

func NewMockSweeperStore

func NewMockSweeperStore() *MockSweeperStore

NewMockSweeperStore returns a new instance.

func (*MockSweeperStore) GetLastPublishedTx

func (s *MockSweeperStore) GetLastPublishedTx() (*wire.MsgTx, error)

GetLastPublishedTx returns the last tx that we called NotifyPublishTx for.

func (*MockSweeperStore) IsOurTx

func (s *MockSweeperStore) IsOurTx(hash chainhash.Hash) (bool, error)

IsOurTx determines whether a tx is published by us, based on its hash.

func (*MockSweeperStore) NotifyPublishTx

func (s *MockSweeperStore) NotifyPublishTx(tx *wire.MsgTx) error

NotifyPublishTx signals that we are about to publish a tx.

type Result

type Result struct {
	// Err is the final result of the sweep. It is nil when the input is
	// swept successfully by us. ErrRemoteSpend is returned when another
	// party took the input.
	Err error

	// Tx is the transaction that spent the input.
	Tx *wire.MsgTx
}

Result is the struct that is pushed through the result channel. Callers can use this to be informed of the final sweep result. In case of a remote spend, Err will be ErrRemoteSpend.

type SweeperStore

type SweeperStore interface {
	// IsOurTx determines whether a tx is published by us, based on its
	// hash.
	IsOurTx(hash chainhash.Hash) (bool, error)

	// NotifyPublishTx signals that we are about to publish a tx.
	NotifyPublishTx(*wire.MsgTx) error

	// GetLastPublishedTx returns the last tx that we called NotifyPublishTx
	// for.
	GetLastPublishedTx() (*wire.MsgTx, error)
}

SweeperStore stores published txes.

func NewSweeperStore

func NewSweeperStore(db *channeldb.DB, chainHash *chainhash.Hash) (
	SweeperStore, error)

NewSweeperStore returns a new store instance.

type UtxoSweeper

type UtxoSweeper struct {
	// contains filtered or unexported fields
}

UtxoSweeper is responsible for sweeping outputs back into the wallet

func New

func New(cfg *UtxoSweeperConfig) *UtxoSweeper

New returns a new Sweeper instance.

func (*UtxoSweeper) CreateSweepTx

func (s *UtxoSweeper) CreateSweepTx(inputs []Input, confTarget uint32,
	currentBlockHeight uint32) (*wire.MsgTx, error)

CreateSweepTx accepts a list of inputs and signs and generates a txn that spends from them. This method also makes an accurate fee estimate before generating the required witnesses.

The created transaction has a single output sending all the funds back to the source wallet, after accounting for the fee estimate.

The value of currentBlockHeight argument will be set as the tx locktime. This function assumes that all CLTV inputs will be unlocked after currentBlockHeight. Reasons not to use the maximum of all actual CLTV expiry values of the inputs:

- Make handling re-orgs easier. - Thwart future possible fee sniping attempts. - Make us blend in with the picfightcoind wallet.

func (*UtxoSweeper) Start

func (s *UtxoSweeper) Start() error

Start starts the process of constructing and publish sweep txes.

func (*UtxoSweeper) Stop

func (s *UtxoSweeper) Stop() error

Stop stops sweeper from listening to block epochs and constructing sweep txes.

func (*UtxoSweeper) SweepInput

func (s *UtxoSweeper) SweepInput(input Input) (chan Result, error)

SweepInput sweeps inputs back into the wallet. The inputs will be batched and swept after the batch time window ends.

NOTE: Extreme care needs to be taken that input isn't changed externally. Because it is an interface and we don't know what is exactly behind it, we cannot make a local copy in sweeper.

type UtxoSweeperConfig

type UtxoSweeperConfig struct {
	// GenSweepScript generates a P2WKH script belonging to the wallet where
	// funds can be swept.
	GenSweepScript func() ([]byte, error)

	// Estimator is used when crafting sweep transactions to estimate the
	// necessary fee relative to the expected size of the sweep
	// transaction.
	Estimator lnwallet.FeeEstimator

	// PublishTransaction facilitates the process of broadcasting a signed
	// transaction to the appropriate network.
	PublishTransaction func(*wire.MsgTx) error

	// NewBatchTimer creates a channel that will be sent on when a certain
	// time window has passed. During this time window, new inputs can still
	// be added to the sweep tx that is about to be generated.
	NewBatchTimer func() <-chan time.Time

	// Notifier is an instance of a chain notifier we'll use to watch for
	// certain on-chain events.
	Notifier chainntnfs.ChainNotifier

	// ChainIO is used  to determine the current block height.
	ChainIO lnwallet.BlockChainIO

	// Store stores the published sweeper txes.
	Store SweeperStore

	// Signer is used by the sweeper to generate valid witnesses at the
	// time the incubated outputs need to be spent.
	Signer lnwallet.Signer

	// SweepTxConfTarget assigns a confirmation target for sweep txes on
	// which the fee calculation will be based.
	SweepTxConfTarget uint32

	// MaxInputsPerTx specifies the default maximum number of inputs allowed
	// in a single sweep tx. If more need to be swept, multiple txes are
	// created and published.
	MaxInputsPerTx int

	// MaxSweepAttempts specifies the maximum number of times an input is
	// included in a publish attempt before giving up and returning an error
	// to the caller.
	MaxSweepAttempts int

	// NextAttemptDeltaFunc returns given the number of already attempted
	// sweeps, how many blocks to wait before retrying to sweep.
	NextAttemptDeltaFunc func(int) int32
}

UtxoSweeperConfig contains dependencies of UtxoSweeper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL