-
Notifications
You must be signed in to change notification settings - Fork 46
Add WatchtowerClient interface #235
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
Open
Jontified
wants to merge
3
commits into
lightninglabs:master
Choose a base branch
from
Jontified:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
package lndclient | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// WatchtowerClientClient implements the watchtower client interface | ||
// https://lightning.engineering/api-docs/category/watchtowerclient-service/ | ||
// NOTE: For the macaroon code to work correctly this needs to be named | ||
// WatchtowerClientClient since we use reflection to set macaroon permissions. | ||
type WatchtowerClientClient interface { | ||
ServiceClient[wtclientrpc.WatchtowerClientClient] | ||
|
||
// AddTower adds a new watchtower reachable at the given address and | ||
// considers it for new sessions. If the watchtower already exists, then | ||
// any new addresses included will be considered when dialing it for | ||
// session negotiations and backups. | ||
AddTower(ctx context.Context, pubkey []byte, address string) error | ||
|
||
// DeactivateTower sets the given tower's status to inactive so that it | ||
// is not considered for session negotiation. Its sessions will also not | ||
// be used while the tower is inactive. | ||
DeactivateTower(ctx context.Context, pubkey []byte) (string, error) | ||
|
||
// GetTowerInfo gets information about a watchtower that corresponds to | ||
// the given pubkey. The `includeSessions` flag controls whether session | ||
// information is included. The `excludeExhaustedSessions` controls | ||
// whether exhausted sessions are included in the response. | ||
GetTowerInfo(ctx context.Context, pubkey []byte, includeSessions, | ||
excludeExhaustedSessions bool) (*wtclientrpc.Tower, error) | ||
|
||
// ListTowers gets information about all registered watchtowers. The | ||
// `includeSessions` and `excludeExhaustedSessions` flags serve the same | ||
// function as in the `GetTowerInfo` method. | ||
ListTowers(ctx context.Context, includeSessions, | ||
excludeExhaustedSessions bool) ([]*wtclientrpc.Tower, error) | ||
|
||
// Policy returns the active watchtower client policy configuration. | ||
Policy(ctx context.Context, policyType wtclientrpc.PolicyType) ( | ||
*wtclientrpc.PolicyResponse, error) | ||
|
||
// RemoveTower removes a watchtower from being considered for future | ||
// session negotiations and from being used for any subsequent backups | ||
// until it's added again. If an address is provided, then this RPC only | ||
// serves as a way of removing the address from the watchtower instead. | ||
RemoveTower(ctx context.Context, pubkey []byte, address string) error | ||
|
||
// Stats returns the in-memory statistics of the client since startup. | ||
Stats(ctx context.Context) (*wtclientrpc.StatsResponse, error) | ||
|
||
// TerminateSession terminates the given session and marks it to not be | ||
// used for backups anymore. Returns a human readable status string. | ||
TerminateSession(ctx context.Context, sessionId []byte) (string, error) | ||
} | ||
|
||
type wtClient struct { | ||
client wtclientrpc.WatchtowerClientClient | ||
wtClientMac serializedMacaroon | ||
timeout time.Duration | ||
} | ||
|
||
// A compile time check to ensure that wtClientClient implements the | ||
// WtclientClient interface. | ||
var _ WatchtowerClientClient = (*wtClient)(nil) | ||
|
||
// newClientClient creates a new watchtower client interface. | ||
func newWtClient(conn grpc.ClientConnInterface, | ||
wtClientMac serializedMacaroon, timeout time.Duration) *wtClient { | ||
|
||
return &wtClient{ | ||
client: wtclientrpc.NewWatchtowerClientClient(conn), | ||
wtClientMac: wtClientMac, | ||
timeout: timeout, | ||
} | ||
} | ||
|
||
// RawClientWithMacAuth returns a context with the proper macaroon | ||
// authentication, the default RPC timeout, and the raw client. | ||
func (m *wtClient) RawClientWithMacAuth( | ||
parentCtx context.Context) (context.Context, time.Duration, | ||
wtclientrpc.WatchtowerClientClient) { | ||
|
||
return m.wtClientMac.WithMacaroonAuth(parentCtx), m.timeout, m.client | ||
} | ||
|
||
// AddTower adds a new watchtower reachable at the given address and | ||
// considers it for new sessions. If the watchtower already exists, then | ||
// any new addresses included will be considered when dialing it for | ||
// session negotiations and backups. | ||
func (m *wtClient) AddTower(ctx context.Context, pubkey []byte, | ||
address string) error { | ||
|
||
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout) | ||
defer cancel() | ||
|
||
rpcReq := &wtclientrpc.AddTowerRequest{ | ||
Pubkey: pubkey, | ||
Address: address, | ||
} | ||
|
||
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx) | ||
_, err := m.client.AddTower(rpcCtx, rpcReq) | ||
|
||
return err | ||
} | ||
|
||
// DeactivateTower sets the given tower's status to inactive so that it | ||
// is not considered for session negotiation. Its sessions will also not | ||
// be used while the tower is inactive. | ||
func (m *wtClient) DeactivateTower(ctx context.Context, pubkey []byte) ( | ||
string, error) { | ||
|
||
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout) | ||
defer cancel() | ||
|
||
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx) | ||
resp, err := m.client.DeactivateTower(rpcCtx, | ||
&wtclientrpc.DeactivateTowerRequest{ | ||
Pubkey: pubkey, | ||
}, | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return resp.Status, nil | ||
} | ||
|
||
// GetTowerInfo gets information about a watchtower that corresponds to | ||
// the given pubkey. The `includeSessions` flag controls whether session | ||
// information is included. The `excludeExhaustedSessions` controls | ||
// whether exhausted sessions are included in the response. | ||
func (m *wtClient) GetTowerInfo(ctx context.Context, pubkey []byte, | ||
includeSessions, excludeExhaustedSessions bool) (*wtclientrpc.Tower, | ||
error) { | ||
|
||
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout) | ||
defer cancel() | ||
|
||
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx) | ||
return m.client.GetTowerInfo(rpcCtx, | ||
&wtclientrpc.GetTowerInfoRequest{ | ||
Pubkey: pubkey, | ||
IncludeSessions: includeSessions, | ||
ExcludeExhaustedSessions: excludeExhaustedSessions, | ||
}, | ||
) | ||
} | ||
|
||
// ListTowers gets information about all registered watchtowers. The | ||
// `includeSessions` and `excludeExhaustedSessions` flags serve the same | ||
// function as in the `GetTowerInfo` method. | ||
func (m *wtClient) ListTowers(ctx context.Context, includeSessions, | ||
excludeExhaustedSessions bool) ([]*wtclientrpc.Tower, error) { | ||
|
||
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout) | ||
defer cancel() | ||
|
||
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx) | ||
resp, err := m.client.ListTowers(rpcCtx, &wtclientrpc.ListTowersRequest{ | ||
IncludeSessions: includeSessions, | ||
ExcludeExhaustedSessions: excludeExhaustedSessions, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp.Towers, nil | ||
} | ||
|
||
// Policy returns the active watchtower client policy configuration. | ||
func (m *wtClient) Policy(ctx context.Context, | ||
policyType wtclientrpc.PolicyType) (*wtclientrpc.PolicyResponse, error) { | ||
|
||
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout) | ||
defer cancel() | ||
|
||
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx) | ||
return m.client.Policy(rpcCtx, &wtclientrpc.PolicyRequest{ | ||
PolicyType: policyType, | ||
}) | ||
} | ||
|
||
// RemoveTower removes a watchtower from being considered for future | ||
// session negotiations and from being used for any subsequent backups | ||
// until it's added again. If an address is provided, then this RPC only | ||
// serves as a way of removing the address from the watchtower instead. | ||
func (m *wtClient) RemoveTower(ctx context.Context, pubkey []byte, | ||
address string) error { | ||
|
||
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout) | ||
defer cancel() | ||
|
||
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx) | ||
_, err := m.client.RemoveTower(rpcCtx, &wtclientrpc.RemoveTowerRequest{ | ||
Pubkey: pubkey, | ||
Address: address, | ||
}) | ||
|
||
return err | ||
} | ||
|
||
// Stats returns the in-memory statistics of the client since startup. | ||
func (m *wtClient) Stats(ctx context.Context) (*wtclientrpc.StatsResponse, error) { | ||
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout) | ||
defer cancel() | ||
|
||
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx) | ||
return m.client.Stats(rpcCtx, &wtclientrpc.StatsRequest{}) | ||
} | ||
|
||
// TerminateSession terminates the given session and marks it to not be used | ||
// for backups anymore. Returns a human readable status string. | ||
func (m *wtClient) TerminateSession(ctx context.Context, | ||
sessionId []byte) (string, error) { | ||
|
||
rpcCtx, cancel := context.WithTimeout(ctx, m.timeout) | ||
defer cancel() | ||
|
||
rpcCtx = m.wtClientMac.WithMacaroonAuth(rpcCtx) | ||
resp, err := m.client.TerminateSession(rpcCtx, | ||
&wtclientrpc.TerminateSessionRequest{ | ||
SessionId: sessionId, | ||
}, | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return resp.Status, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.