Skip to content

Commit

Permalink
multi: move Input interface and related code
Browse files Browse the repository at this point in the history
This commit is a step to split the lnwallet package. It puts the Input
interface and implementations in a separate package along with all their
dependencies from lnwallet.
  • Loading branch information
joostjager committed Jan 31, 2019
1 parent 667474d commit 9e012ec
Show file tree
Hide file tree
Showing 66 changed files with 811 additions and 689 deletions.
98 changes: 49 additions & 49 deletions breacharbiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/sweep"
)

var (
Expand Down Expand Up @@ -100,7 +100,7 @@ type BreachConfig struct {
// Signer is used by the breach arbiter to generate sweep transactions,
// which move coins from previously open channels back to the user's
// wallet.
Signer lnwallet.Signer
Signer input.Signer

// Store is a persistent resource that maintains information regarding
// breached channels. This is used in conjunction with DB to recover
Expand Down Expand Up @@ -282,7 +282,7 @@ func convertToSecondLevelRevoke(bo *breachedOutput, breachInfo *retributionInfo,

// In this case, we'll modify the witness type of this output to
// actually prepare for a second level revoke.
bo.witnessType = lnwallet.HtlcSecondLevelRevoke
bo.witnessType = input.HtlcSecondLevelRevoke

// We'll also redirect the outpoint to this second level output, so the
// spending transaction updates it inputs accordingly.
Expand Down Expand Up @@ -346,8 +346,8 @@ func (b *breachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
breachedOutput := &breachInfo.breachedOutputs[i]

// If this isn't an HTLC output, then we can skip it.
if breachedOutput.witnessType != lnwallet.HtlcAcceptedRevoke &&
breachedOutput.witnessType != lnwallet.HtlcOfferedRevoke {
if breachedOutput.witnessType != input.HtlcAcceptedRevoke &&
breachedOutput.witnessType != input.HtlcOfferedRevoke {
continue
}

Expand Down Expand Up @@ -581,18 +581,18 @@ justiceTxBroadcast:
// Compute both the total value of funds being swept and the
// amount of funds that were revoked from the counter party.
var totalFunds, revokedFunds btcutil.Amount
for _, input := range breachInfo.breachedOutputs {
totalFunds += input.Amount()
for _, inp := range breachInfo.breachedOutputs {
totalFunds += inp.Amount()

// If the output being revoked is the remote commitment
// output or an offered HTLC output, it's amount
// contributes to the value of funds being revoked from
// the counter party.
switch input.WitnessType() {
case lnwallet.CommitmentRevoke:
revokedFunds += input.Amount()
case lnwallet.HtlcOfferedRevoke:
revokedFunds += input.Amount()
switch inp.WitnessType() {
case input.CommitmentRevoke:
revokedFunds += inp.Amount()
case input.HtlcOfferedRevoke:
revokedFunds += inp.Amount()
default:
}
}
Expand Down Expand Up @@ -755,21 +755,21 @@ func (b *breachArbiter) handleBreachHandoff(breachEvent *ContractBreachEvent) {
type breachedOutput struct {
amt btcutil.Amount
outpoint wire.OutPoint
witnessType lnwallet.WitnessType
signDesc lnwallet.SignDescriptor
witnessType input.WitnessType
signDesc input.SignDescriptor
confHeight uint32

secondLevelWitnessScript []byte

witnessFunc lnwallet.WitnessGenerator
witnessFunc input.WitnessGenerator
}

// makeBreachedOutput assembles a new breachedOutput that can be used by the
// breach arbiter to construct a justice or sweep transaction.
func makeBreachedOutput(outpoint *wire.OutPoint,
witnessType lnwallet.WitnessType,
witnessType input.WitnessType,
secondLevelScript []byte,
signDescriptor *lnwallet.SignDescriptor,
signDescriptor *input.SignDescriptor,
confHeight uint32) breachedOutput {

amount := signDescriptor.Output.Value
Expand Down Expand Up @@ -797,13 +797,13 @@ func (bo *breachedOutput) OutPoint() *wire.OutPoint {

// WitnessType returns the type of witness that must be generated to spend the
// breached output.
func (bo *breachedOutput) WitnessType() lnwallet.WitnessType {
func (bo *breachedOutput) WitnessType() input.WitnessType {
return bo.witnessType
}

// SignDesc returns the breached output's SignDescriptor, which is used during
// signing to compute the witness.
func (bo *breachedOutput) SignDesc() *lnwallet.SignDescriptor {
func (bo *breachedOutput) SignDesc() *input.SignDescriptor {
return &bo.signDesc
}

Expand All @@ -812,8 +812,8 @@ func (bo *breachedOutput) SignDesc() *lnwallet.SignDescriptor {
// generation function, which parameterized primarily by the witness type and
// sign descriptor. The method then returns the witness computed by invoking
// this function on the first and subsequent calls.
func (bo *breachedOutput) CraftInputScript(signer lnwallet.Signer, txn *wire.MsgTx,
hashCache *txscript.TxSigHashes, txinIdx int) (*lnwallet.InputScript, error) {
func (bo *breachedOutput) CraftInputScript(signer input.Signer, txn *wire.MsgTx,
hashCache *txscript.TxSigHashes, txinIdx int) (*input.Script, error) {

// First, we ensure that the witness generation function has been
// initialized for this breached output.
Expand Down Expand Up @@ -842,7 +842,7 @@ func (bo *breachedOutput) HeightHint() uint32 {

// Add compile-time constraint ensuring breachedOutput implements the Input
// interface.
var _ sweep.Input = (*breachedOutput)(nil)
var _ input.Input = (*breachedOutput)(nil)

// retributionInfo encapsulates all the data needed to sweep all the contested
// funds within a channel whose contract has been breached by the prior
Expand Down Expand Up @@ -883,7 +883,7 @@ func newRetributionInfo(chanPoint *wire.OutPoint,
if breachInfo.LocalOutputSignDesc != nil {
localOutput := makeBreachedOutput(
&breachInfo.LocalOutpoint,
lnwallet.CommitmentNoDelay,
input.CommitmentNoDelay,
// No second level script as this is a commitment
// output.
nil,
Expand All @@ -901,7 +901,7 @@ func newRetributionInfo(chanPoint *wire.OutPoint,
if breachInfo.RemoteOutputSignDesc != nil {
remoteOutput := makeBreachedOutput(
&breachInfo.RemoteOutpoint,
lnwallet.CommitmentRevoke,
input.CommitmentRevoke,
// No second level script as this is a commitment
// output.
nil,
Expand All @@ -919,11 +919,11 @@ func newRetributionInfo(chanPoint *wire.OutPoint,
// Using the breachedHtlc's incoming flag, determine the
// appropriate witness type that needs to be generated in order
// to sweep the HTLC output.
var htlcWitnessType lnwallet.WitnessType
var htlcWitnessType input.WitnessType
if breachedHtlc.IsIncoming {
htlcWitnessType = lnwallet.HtlcAcceptedRevoke
htlcWitnessType = input.HtlcAcceptedRevoke
} else {
htlcWitnessType = lnwallet.HtlcOfferedRevoke
htlcWitnessType = input.HtlcOfferedRevoke
}

htlcOutput := makeBreachedOutput(
Expand Down Expand Up @@ -956,13 +956,13 @@ func (b *breachArbiter) createJusticeTx(
// outputs, while simultaneously computing the estimated weight of the
// transaction.
var (
spendableOutputs []sweep.Input
weightEstimate lnwallet.TxWeightEstimator
spendableOutputs []input.Input
weightEstimate input.TxWeightEstimator
)

// Allocate enough space to potentially hold each of the breached
// outputs in the retribution info.
spendableOutputs = make([]sweep.Input, 0, len(r.breachedOutputs))
spendableOutputs = make([]input.Input, 0, len(r.breachedOutputs))

// The justice transaction we construct will be a segwit transaction
// that pays to a p2wkh output. Components such as the version,
Expand All @@ -975,38 +975,38 @@ func (b *breachArbiter) createJusticeTx(
// finally adding to our list of spendable outputs.
for i := range r.breachedOutputs {
// Grab locally scoped reference to breached output.
input := &r.breachedOutputs[i]
inp := &r.breachedOutputs[i]

// First, select the appropriate estimated witness weight for
// the give witness type of this breached output. If the witness
// type is unrecognized, we will omit it from the transaction.
var witnessWeight int
switch input.WitnessType() {
case lnwallet.CommitmentNoDelay:
witnessWeight = lnwallet.P2WKHWitnessSize
switch inp.WitnessType() {
case input.CommitmentNoDelay:
witnessWeight = input.P2WKHWitnessSize

case lnwallet.CommitmentRevoke:
witnessWeight = lnwallet.ToLocalPenaltyWitnessSize
case input.CommitmentRevoke:
witnessWeight = input.ToLocalPenaltyWitnessSize

case lnwallet.HtlcOfferedRevoke:
witnessWeight = lnwallet.OfferedHtlcPenaltyWitnessSize
case input.HtlcOfferedRevoke:
witnessWeight = input.OfferedHtlcPenaltyWitnessSize

case lnwallet.HtlcAcceptedRevoke:
witnessWeight = lnwallet.AcceptedHtlcPenaltyWitnessSize
case input.HtlcAcceptedRevoke:
witnessWeight = input.AcceptedHtlcPenaltyWitnessSize

case lnwallet.HtlcSecondLevelRevoke:
witnessWeight = lnwallet.ToLocalPenaltyWitnessSize
case input.HtlcSecondLevelRevoke:
witnessWeight = input.ToLocalPenaltyWitnessSize

default:
brarLog.Warnf("breached output in retribution info "+
"contains unexpected witness type: %v",
input.WitnessType())
inp.WitnessType())
continue
}
weightEstimate.AddWitnessInput(witnessWeight)

// Finally, append this input to our list of spendable outputs.
spendableOutputs = append(spendableOutputs, input)
spendableOutputs = append(spendableOutputs, inp)
}

txWeight := int64(weightEstimate.Weight())
Expand All @@ -1016,7 +1016,7 @@ func (b *breachArbiter) createJusticeTx(
// sweepSpendableOutputsTxn creates a signed transaction from a sequence of
// spendable outputs by sweeping the funds into a single p2wkh output.
func (b *breachArbiter) sweepSpendableOutputsTxn(txWeight int64,
inputs ...sweep.Input) (*wire.MsgTx, error) {
inputs ...input.Input) (*wire.MsgTx, error) {

// First, we obtain a new public key script from the wallet which we'll
// sweep the funds to.
Expand Down Expand Up @@ -1078,7 +1078,7 @@ func (b *breachArbiter) sweepSpendableOutputsTxn(txWeight int64,
// witness, and attaching it to the transaction. This function accepts
// an integer index representing the intended txin index, and the
// breached output from which it will spend.
addWitness := func(idx int, so sweep.Input) error {
addWitness := func(idx int, so input.Input) error {
// First, we construct a valid witness for this outpoint and
// transaction using the SpendableOutput's witness generation
// function.
Expand Down Expand Up @@ -1435,7 +1435,7 @@ func (bo *breachedOutput) Encode(w io.Writer) error {
return err
}

err := lnwallet.WriteSignDescriptor(w, &bo.signDesc)
err := input.WriteSignDescriptor(w, &bo.signDesc)
if err != nil {
return err
}
Expand Down Expand Up @@ -1466,7 +1466,7 @@ func (bo *breachedOutput) Decode(r io.Reader) error {
return err
}

if err := lnwallet.ReadSignDescriptor(r, &bo.signDesc); err != nil {
if err := input.ReadSignDescriptor(r, &bo.signDesc); err != nil {
return err
}

Expand All @@ -1479,7 +1479,7 @@ func (bo *breachedOutput) Decode(r io.Reader) error {
if _, err := io.ReadFull(r, scratch[:2]); err != nil {
return err
}
bo.witnessType = lnwallet.WitnessType(
bo.witnessType = input.WitnessType(
binary.BigEndian.Uint16(scratch[:2]),
)

Expand Down
25 changes: 13 additions & 12 deletions breacharbiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
Expand Down Expand Up @@ -94,8 +95,8 @@ var (
{
amt: btcutil.Amount(1e7),
outpoint: breachOutPoints[0],
witnessType: lnwallet.CommitmentNoDelay,
signDesc: lnwallet.SignDescriptor{
witnessType: input.CommitmentNoDelay,
signDesc: input.SignDescriptor{
SingleTweak: []byte{
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
Expand Down Expand Up @@ -138,8 +139,8 @@ var (
{
amt: btcutil.Amount(2e9),
outpoint: breachOutPoints[1],
witnessType: lnwallet.CommitmentRevoke,
signDesc: lnwallet.SignDescriptor{
witnessType: input.CommitmentRevoke,
signDesc: input.SignDescriptor{
SingleTweak: []byte{
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
Expand Down Expand Up @@ -182,8 +183,8 @@ var (
{
amt: btcutil.Amount(3e4),
outpoint: breachOutPoints[2],
witnessType: lnwallet.CommitmentDelayOutput,
signDesc: lnwallet.SignDescriptor{
witnessType: input.CommitmentDelayOutput,
signDesc: input.SignDescriptor{
SingleTweak: []byte{
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
Expand Down Expand Up @@ -1009,7 +1010,7 @@ func TestBreachHandoffSuccess(t *testing.T) {
ProcessACK: make(chan error, 1),
BreachRetribution: &lnwallet.BreachRetribution{
BreachTransaction: bobClose.CloseTx,
LocalOutputSignDesc: &lnwallet.SignDescriptor{
LocalOutputSignDesc: &input.SignDescriptor{
Output: &wire.TxOut{
PkScript: breachKeys[0],
},
Expand Down Expand Up @@ -1041,7 +1042,7 @@ func TestBreachHandoffSuccess(t *testing.T) {
ProcessACK: make(chan error, 1),
BreachRetribution: &lnwallet.BreachRetribution{
BreachTransaction: bobClose.CloseTx,
LocalOutputSignDesc: &lnwallet.SignDescriptor{
LocalOutputSignDesc: &input.SignDescriptor{
Output: &wire.TxOut{
PkScript: breachKeys[0],
},
Expand Down Expand Up @@ -1090,7 +1091,7 @@ func TestBreachHandoffFail(t *testing.T) {
ProcessACK: make(chan error, 1),
BreachRetribution: &lnwallet.BreachRetribution{
BreachTransaction: bobClose.CloseTx,
LocalOutputSignDesc: &lnwallet.SignDescriptor{
LocalOutputSignDesc: &input.SignDescriptor{
Output: &wire.TxOut{
PkScript: breachKeys[0],
},
Expand Down Expand Up @@ -1130,7 +1131,7 @@ func TestBreachHandoffFail(t *testing.T) {
ProcessACK: make(chan error, 1),
BreachRetribution: &lnwallet.BreachRetribution{
BreachTransaction: bobClose.CloseTx,
LocalOutputSignDesc: &lnwallet.SignDescriptor{
LocalOutputSignDesc: &input.SignDescriptor{
Output: &wire.TxOut{
PkScript: breachKeys[0],
},
Expand Down Expand Up @@ -1444,7 +1445,7 @@ func createInitChannels(revocationWindow int) (*lnwallet.LightningChannel, *lnwa
if err != nil {
return nil, nil, nil, err
}
bobCommitPoint := lnwallet.ComputeCommitmentPoint(bobFirstRevoke[:])
bobCommitPoint := input.ComputeCommitmentPoint(bobFirstRevoke[:])

aliceRoot, err := chainhash.NewHash(aliceKeyPriv.Serialize())
if err != nil {
Expand All @@ -1455,7 +1456,7 @@ func createInitChannels(revocationWindow int) (*lnwallet.LightningChannel, *lnwa
if err != nil {
return nil, nil, nil, err
}
aliceCommitPoint := lnwallet.ComputeCommitmentPoint(aliceFirstRevoke[:])
aliceCommitPoint := input.ComputeCommitmentPoint(aliceFirstRevoke[:])

aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(channelBal,
channelBal, &aliceCfg, &bobCfg, aliceCommitPoint, bobCommitPoint,
Expand Down
Loading

0 comments on commit 9e012ec

Please sign in to comment.