watchtower/wtclient/backup_task: make sweep pkscript independent of session

This commit changes when the sweep pkscript
is assigned in the construction of the justice
transaction. Currently, the sweep pkscript is
assigned when the task is bound to a session.
However, we will moving to an assignment where
a unique sweep pkscript is used per channel to
prevent address inflation. Hence, this commit
makes the sweep pkscript a state dependent
variable, since it can be known at the time the
channel id is assigned.
This commit is contained in:
Conner Fromknecht 2019-02-06 18:20:30 -08:00
parent fe9f703b5b
commit a00fc148c8
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
2 changed files with 11 additions and 11 deletions

@ -43,6 +43,7 @@ type backupTask struct {
toLocalInput input.Input
toRemoteInput input.Input
totalAmt btcutil.Amount
sweepPkScript []byte
// session-dependent variables
@ -53,7 +54,8 @@ type backupTask struct {
// newBackupTask initializes a new backupTask and populates all state-dependent
// variables.
func newBackupTask(chanID *lnwire.ChannelID,
breachInfo *lnwallet.BreachRetribution) *backupTask {
breachInfo *lnwallet.BreachRetribution,
sweepPkScript []byte) *backupTask {
// Parse the non-dust outputs from the breach transaction,
// simultaneously computing the total amount contained in the inputs
@ -100,6 +102,7 @@ func newBackupTask(chanID *lnwire.ChannelID,
toLocalInput: toLocalInput,
toRemoteInput: toRemoteInput,
totalAmt: btcutil.Amount(totalAmt),
sweepPkScript: sweepPkScript,
}
}
@ -122,8 +125,7 @@ func (t *backupTask) inputs() map[wire.OutPoint]input.Input {
// SessionInfo's policy. If no error is returned, the task has been bound to the
// session and can be queued to upload to the tower. Otherwise, the bind failed
// and should be rescheduled with a different session.
func (t *backupTask) bindSession(session *wtdb.SessionInfo,
sweepPkScript []byte) error {
func (t *backupTask) bindSession(session *wtdb.SessionInfo) error {
// First we'll begin by deriving a weight estimate for the justice
// transaction. The final weight can be different depending on whether
@ -152,7 +154,7 @@ func (t *backupTask) bindSession(session *wtdb.SessionInfo,
// in the current session's policy.
outputs, err := session.Policy.ComputeJusticeTxOuts(
t.totalAmt, int64(weightEstimate.Weight()),
sweepPkScript, session.RewardAddress,
t.sweepPkScript, session.RewardAddress,
)
if err != nil {
return err
@ -170,7 +172,7 @@ func (t *backupTask) bindSession(session *wtdb.SessionInfo,
// session-dependent variables, and signs the resulting transaction. The
// required pieces from signatures, witness scripts, etc are then packaged into
// a JusticeKit and encrypted using the breach transaction's key.
func (t *backupTask) craftSessionPayload(sweepPkScript []byte,
func (t *backupTask) craftSessionPayload(
signer input.Signer) (wtdb.BreachHint, []byte, error) {
var hint wtdb.BreachHint
@ -179,7 +181,7 @@ func (t *backupTask) craftSessionPayload(sweepPkScript []byte,
// to-local script, and the remote CSV delay.
keyRing := t.breachInfo.KeyRing
justiceKit := &blob.JusticeKit{
SweepAddress: sweepPkScript,
SweepAddress: t.sweepPkScript,
RevocationPubKey: toBlobPubKey(keyRing.RevocationKey),
LocalDelayPubKey: toBlobPubKey(keyRing.DelayKey),
CSVDelay: t.breachInfo.RemoteDelay,

@ -397,7 +397,7 @@ func TestBackupTask(t *testing.T) {
func testBackupTask(t *testing.T, test backupTaskTest) {
// Create a new backupTask from the channel id and breach info.
task := newBackupTask(&test.chanID, test.breachInfo)
task := newBackupTask(&test.chanID, test.breachInfo, test.expSweepScript)
// Assert that all parameters set during initialization are properly
// populated.
@ -452,7 +452,7 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
// Now, bind the session to the task. If successful, this locks in the
// session's negotiated parameters and allows the backup task to derive
// the final free variables in the justice transaction.
err := task.bindSession(test.session, test.expSweepScript)
err := task.bindSession(test.session)
if err != test.bindErr {
t.Fatalf("expected: %v when binding session, got: %v",
test.bindErr, err)
@ -509,9 +509,7 @@ func testBackupTask(t *testing.T, test backupTaskTest) {
// Now, we'll construct, sign, and encrypt the blob containing the parts
// needed to reconstruct the justice transaction.
hint, encBlob, err := task.craftSessionPayload(
test.expSweepScript, test.signer,
)
hint, encBlob, err := task.craftSessionPayload(test.signer)
if err != nil {
t.Fatalf("unable to craft session payload: %v", err)
}