From 98c2d329e55ddbe8fafeb438b10d2f5e70ec8120 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 13 Jun 2019 17:33:06 -0700 Subject: [PATCH] watchtower/multi: embed TxPolicy in wtpolicy.Policy This commit splits out the parameters that shape the justice transaction into their own struct, which then embedded within the overarching wtpolicy.Policy which may have additional parameters describing operation of the session. This is done as a preliminary step to support comparison of sessions based on matching TxPolicy configurations. This prevents otherwise identical Policies from being counted as different if operational parameters like MaxUpdates differ, even if it has no material difference to the justice transaction. --- watchtower/lookout/justice_descriptor_test.go | 8 +- watchtower/lookout/lookout_test.go | 8 +- .../wtclient/backup_task_internal_test.go | 8 +- watchtower/wtclient/client_test.go | 88 ++++++++++++------- watchtower/wtpolicy/policy.go | 40 ++++++--- watchtower/wtserver/create_session.go | 12 +-- 6 files changed, 104 insertions(+), 60 deletions(-) diff --git a/watchtower/lookout/justice_descriptor_test.go b/watchtower/lookout/justice_descriptor_test.go index c4c4d35e..1f880c23 100644 --- a/watchtower/lookout/justice_descriptor_test.go +++ b/watchtower/lookout/justice_descriptor_test.go @@ -156,9 +156,11 @@ func testJusticeDescriptor(t *testing.T, blobType blob.Type) { // parameters that should be used in constructing the justice // transaction. policy := wtpolicy.Policy{ - BlobType: blobType, - SweepFeeRate: 2000, - RewardRate: 900000, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blobType, + SweepFeeRate: 2000, + RewardRate: 900000, + }, } sessionInfo := &wtdb.SessionInfo{ Policy: policy, diff --git a/watchtower/lookout/lookout_test.go b/watchtower/lookout/lookout_test.go index ba62a015..cf1c7960 100644 --- a/watchtower/lookout/lookout_test.go +++ b/watchtower/lookout/lookout_test.go @@ -96,7 +96,9 @@ func TestLookoutBreachMatching(t *testing.T) { sessionInfo1 := &wtdb.SessionInfo{ ID: makeArray33(1), Policy: wtpolicy.Policy{ - BlobType: rewardAndCommitType, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: rewardAndCommitType, + }, MaxUpdates: 10, }, RewardAddress: makeAddrSlice(22), @@ -104,7 +106,9 @@ func TestLookoutBreachMatching(t *testing.T) { sessionInfo2 := &wtdb.SessionInfo{ ID: makeArray33(2), Policy: wtpolicy.Policy{ - BlobType: rewardAndCommitType, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: rewardAndCommitType, + }, MaxUpdates: 10, }, RewardAddress: makeAddrSlice(22), diff --git a/watchtower/wtclient/backup_task_internal_test.go b/watchtower/wtclient/backup_task_internal_test.go index 77fc3fbd..5bc0dbb4 100644 --- a/watchtower/wtclient/backup_task_internal_test.go +++ b/watchtower/wtclient/backup_task_internal_test.go @@ -207,9 +207,11 @@ func genTaskTest( expRewardScript: rewardScript, session: &wtdb.ClientSessionBody{ Policy: wtpolicy.Policy{ - BlobType: blobType, - SweepFeeRate: sweepFeeRate, - RewardRate: 10000, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blobType, + SweepFeeRate: sweepFeeRate, + RewardRate: 10000, + }, }, RewardPkScript: rewardScript, }, diff --git a/watchtower/wtclient/client_test.go b/watchtower/wtclient/client_test.go index 00abc19d..c7c58977 100644 --- a/watchtower/wtclient/client_test.go +++ b/watchtower/wtclient/client_test.go @@ -785,9 +785,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 20000, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 20000, }, noRegisterChan0: true, }, @@ -817,9 +819,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 20000, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 20000, }, }, fn: func(h *testHarness) { @@ -850,9 +854,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 5, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 5, }, }, fn: func(h *testHarness) { @@ -884,9 +890,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 20000, - SweepFeeRate: 1000000, // high sweep fee creates dust + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1000000, // high sweep fee creates dust + }, + MaxUpdates: 20000, }, }, fn: func(h *testHarness) { @@ -913,9 +921,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 20000, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 20000, }, }, fn: func(h *testHarness) { @@ -993,9 +1003,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 5, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 5, }, }, fn: func(h *testHarness) { @@ -1049,9 +1061,11 @@ var clientTests = []clientTest{ localBalance: 10000001, // ensure (% amt != 0) remoteBalance: 20000001, // ensure (% amt != 0) policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 1000, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 1000, }, }, fn: func(h *testHarness) { @@ -1091,9 +1105,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 5, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 5, }, }, fn: func(h *testHarness) { @@ -1139,9 +1155,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 5, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 5, }, noAckCreateSession: true, }, @@ -1195,9 +1213,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 5, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 5, }, noAckCreateSession: true, }, @@ -1256,9 +1276,11 @@ var clientTests = []clientTest{ localBalance: localBalance, remoteBalance: remoteBalance, policy: wtpolicy.Policy{ - BlobType: blob.TypeDefault, - MaxUpdates: 5, - SweepFeeRate: 1, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: blob.TypeDefault, + SweepFeeRate: 1, + }, + MaxUpdates: 5, }, }, fn: func(h *testHarness) { diff --git a/watchtower/wtpolicy/policy.go b/watchtower/wtpolicy/policy.go index b21f73d3..cee4f87d 100644 --- a/watchtower/wtpolicy/policy.go +++ b/watchtower/wtpolicy/policy.go @@ -49,28 +49,28 @@ var ( // used by clients or servers. func DefaultPolicy() Policy { return Policy{ - BlobType: blob.TypeDefault, + TxPolicy: TxPolicy{ + BlobType: blob.TypeDefault, + RewardRate: DefaultRewardRate, + SweepFeeRate: lnwallet.SatPerKWeight( + DefaultSweepFeeRate, + ), + }, MaxUpdates: DefaultMaxUpdates, - RewardRate: DefaultRewardRate, - SweepFeeRate: lnwallet.SatPerKWeight( - DefaultSweepFeeRate, - ), } } -// Policy defines the negotiated parameters for a session between a client and -// server. The parameters specify the format of encrypted blobs sent to the -// tower, the reward schedule for the tower, and the number of encrypted blobs a -// client can send in one session. -type Policy struct { +// TxPolicy defines the negotiate parameters that determine the form of the +// justice transaction for a given breached state. Thus, for any given revoked +// state, an identical key will result in an identical justice transaction +// (barring signatures). The parameters specify the format of encrypted blobs +// sent to the tower, the reward schedule for the tower, and the number of +// encrypted blobs a client can send in one session. +type TxPolicy struct { // BlobType specifies the blob format that must be used by all updates sent // under the session key used to negotiate this session. BlobType blob.Type - // MaxUpdates is the maximum number of updates the watchtower will honor - // for this session. - MaxUpdates uint16 - // RewardBase is the fixed amount allocated to the tower when the // policy's blob type specifies a reward for the tower. This is taken // before adding the proportional reward. @@ -88,6 +88,18 @@ type Policy struct { SweepFeeRate lnwallet.SatPerKWeight } +// Policy defines the negotiated parameters for a session between a client and +// server. In addition to the TxPolicy that governs the shape of the justice +// transaction, the Policy also includes features which only affect the +// operation of the session. +type Policy struct { + TxPolicy + + // MaxUpdates is the maximum number of updates the watchtower will honor + // for this session. + MaxUpdates uint16 +} + // String returns a human-readable description of the current policy. func (p Policy) String() string { return fmt.Sprintf("(blob-type=%b max-updates=%d reward-rate=%d "+ diff --git a/watchtower/wtserver/create_session.go b/watchtower/wtserver/create_session.go index 5636b79d..c60c9b18 100644 --- a/watchtower/wtserver/create_session.go +++ b/watchtower/wtserver/create_session.go @@ -89,11 +89,13 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID, info := wtdb.SessionInfo{ ID: *id, Policy: wtpolicy.Policy{ - BlobType: req.BlobType, - MaxUpdates: req.MaxUpdates, - RewardBase: req.RewardBase, - RewardRate: req.RewardRate, - SweepFeeRate: req.SweepFeeRate, + TxPolicy: wtpolicy.TxPolicy{ + BlobType: req.BlobType, + RewardBase: req.RewardBase, + RewardRate: req.RewardRate, + SweepFeeRate: req.SweepFeeRate, + }, + MaxUpdates: req.MaxUpdates, }, RewardAddress: rewardScript, }