From d5abecbba9ff3537675aac2861e71de9fe24147e Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 22 Jul 2022 08:18:18 +0800 Subject: [PATCH] lntemp/rpc: add new package `lntemp/rpc` to manage rpc clients This commit starts the construction of the new package `lntemp`. This package serves as the scaffolding while we migrate our old itest into the new one. Once the migration is finished, the package will be put back to `lntest`. --- lntemp/rpc/chain_notifier.go | 5 + lntemp/rpc/harness_rpc.go | 94 +++++++++++++++++ lntemp/rpc/invoices.go | 5 + lntemp/rpc/lnd.go | 190 ++++++++++++++++++++++++++++++++++ lntemp/rpc/peers.go | 5 + lntemp/rpc/router.go | 5 + lntemp/rpc/signer.go | 5 + lntemp/rpc/state.go | 19 ++++ lntemp/rpc/wallet_kit.go | 24 +++++ lntemp/rpc/wallet_unlocker.go | 25 +++++ lntemp/rpc/watchtower.go | 5 + 11 files changed, 382 insertions(+) create mode 100644 lntemp/rpc/chain_notifier.go create mode 100644 lntemp/rpc/harness_rpc.go create mode 100644 lntemp/rpc/invoices.go create mode 100644 lntemp/rpc/lnd.go create mode 100644 lntemp/rpc/peers.go create mode 100644 lntemp/rpc/router.go create mode 100644 lntemp/rpc/signer.go create mode 100644 lntemp/rpc/state.go create mode 100644 lntemp/rpc/wallet_kit.go create mode 100644 lntemp/rpc/wallet_unlocker.go create mode 100644 lntemp/rpc/watchtower.go diff --git a/lntemp/rpc/chain_notifier.go b/lntemp/rpc/chain_notifier.go new file mode 100644 index 000000000..3e969f197 --- /dev/null +++ b/lntemp/rpc/chain_notifier.go @@ -0,0 +1,5 @@ +package rpc + +// ===================== +// ChainClient related RPCs. +// ===================== diff --git a/lntemp/rpc/harness_rpc.go b/lntemp/rpc/harness_rpc.go new file mode 100644 index 000000000..ca566035e --- /dev/null +++ b/lntemp/rpc/harness_rpc.go @@ -0,0 +1,94 @@ +package rpc + +import ( + "context" + "testing" + + "github.com/btcsuite/btcd/wire" + "github.com/lightningnetwork/lnd/lnrpc" + "github.com/lightningnetwork/lnd/lnrpc/chainrpc" + "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" + "github.com/lightningnetwork/lnd/lnrpc/peersrpc" + "github.com/lightningnetwork/lnd/lnrpc/routerrpc" + "github.com/lightningnetwork/lnd/lnrpc/signrpc" + "github.com/lightningnetwork/lnd/lnrpc/walletrpc" + "github.com/lightningnetwork/lnd/lnrpc/watchtowerrpc" + "github.com/lightningnetwork/lnd/lnrpc/wtclientrpc" + "github.com/lightningnetwork/lnd/lntest" + "github.com/stretchr/testify/require" + "google.golang.org/grpc" +) + +const ( + // TODO(yy): remove once temp tests is finished. + DefaultTimeout = lntest.DefaultTimeout +) + +// HarnessRPC wraps all lnd's RPC clients into a single struct for easier +// access. +type HarnessRPC struct { + *testing.T + + LN lnrpc.LightningClient + WalletUnlocker lnrpc.WalletUnlockerClient + Invoice invoicesrpc.InvoicesClient + Signer signrpc.SignerClient + Router routerrpc.RouterClient + WalletKit walletrpc.WalletKitClient + Watchtower watchtowerrpc.WatchtowerClient + WatchtowerClient wtclientrpc.WatchtowerClientClient + State lnrpc.StateClient + ChainClient chainrpc.ChainNotifierClient + Peer peersrpc.PeersClient + + // Name is the HarnessNode's name. + Name string + + // runCtx is a context with cancel method. It's used to signal when the + // node needs to quit, and used as the parent context when spawning + // children contexts for RPC requests. + runCtx context.Context + cancel context.CancelFunc +} + +// NewHarnessRPC creates a new HarnessRPC with its own context inherted from +// the pass context. +func NewHarnessRPC(ctxt context.Context, t *testing.T, c *grpc.ClientConn, + name string) *HarnessRPC { + + h := &HarnessRPC{ + T: t, + LN: lnrpc.NewLightningClient(c), + Invoice: invoicesrpc.NewInvoicesClient(c), + Router: routerrpc.NewRouterClient(c), + WalletKit: walletrpc.NewWalletKitClient(c), + WalletUnlocker: lnrpc.NewWalletUnlockerClient(c), + Watchtower: watchtowerrpc.NewWatchtowerClient(c), + WatchtowerClient: wtclientrpc.NewWatchtowerClientClient(c), + Signer: signrpc.NewSignerClient(c), + State: lnrpc.NewStateClient(c), + ChainClient: chainrpc.NewChainNotifierClient(c), + Peer: peersrpc.NewPeersClient(c), + Name: name, + } + + // Inherit parent context. + h.runCtx, h.cancel = context.WithCancel(ctxt) + return h +} + +// MakeOutpoint returns the outpoint of the channel's funding transaction. +func (h *HarnessRPC) MakeOutpoint(cp *lnrpc.ChannelPoint) wire.OutPoint { + fundingTxID, err := lnrpc.GetChanPointFundingTxid(cp) + require.NoErrorf(h, err, "failed to make chanPoint", h.Name) + + return wire.OutPoint{ + Hash: *fundingTxID, + Index: cp.OutputIndex, + } +} + +// NoError is a helper method to format the error message used in calling RPCs. +func (h *HarnessRPC) NoError(err error, operation string) { + require.NoErrorf(h, err, "%s: failed to call %s", h.Name, operation) +} diff --git a/lntemp/rpc/invoices.go b/lntemp/rpc/invoices.go new file mode 100644 index 000000000..0e6d6fe10 --- /dev/null +++ b/lntemp/rpc/invoices.go @@ -0,0 +1,5 @@ +package rpc + +// ===================== +// InvoiceClient related RPCs. +// ===================== diff --git a/lntemp/rpc/lnd.go b/lntemp/rpc/lnd.go new file mode 100644 index 000000000..e99cc6ace --- /dev/null +++ b/lntemp/rpc/lnd.go @@ -0,0 +1,190 @@ +package rpc + +import ( + "context" + + "github.com/lightningnetwork/lnd/lnrpc" +) + +// ===================== +// LightningClient related RPCs. +// ===================== + +// NewAddress makes a RPC call to NewAddress and asserts. +func (h *HarnessRPC) NewAddress( + req *lnrpc.NewAddressRequest) *lnrpc.NewAddressResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.LN.NewAddress(ctxt, req) + h.NoError(err, "NewAddress") + + return resp +} + +// WalletBalance makes a RPC call to WalletBalance and asserts. +func (h *HarnessRPC) WalletBalance() *lnrpc.WalletBalanceResponse { + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + req := &lnrpc.WalletBalanceRequest{} + resp, err := h.LN.WalletBalance(ctxt, req) + h.NoError(err, "WalletBalance") + + return resp +} + +// ListPeers makes a RPC call to the node's ListPeers and asserts. +func (h *HarnessRPC) ListPeers() *lnrpc.ListPeersResponse { + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.LN.ListPeers(ctxt, &lnrpc.ListPeersRequest{}) + h.NoError(err, "ListPeers") + + return resp +} + +// DisconnectPeer calls the DisconnectPeer RPC on a given node with a specified +// public key string and asserts there's no error. +func (h *HarnessRPC) DisconnectPeer( + pubkey string) *lnrpc.DisconnectPeerResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + req := &lnrpc.DisconnectPeerRequest{PubKey: pubkey} + + resp, err := h.LN.DisconnectPeer(ctxt, req) + h.NoError(err, "DisconnectPeer") + + return resp +} + +// DeleteAllPayments makes a RPC call to the node's DeleteAllPayments and +// asserts. +func (h *HarnessRPC) DeleteAllPayments() { + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + req := &lnrpc.DeleteAllPaymentsRequest{} + _, err := h.LN.DeleteAllPayments(ctxt, req) + h.NoError(err, "DeleteAllPayments") +} + +// GetInfo calls the GetInfo RPC on a given node and asserts there's no error. +func (h *HarnessRPC) GetInfo() *lnrpc.GetInfoResponse { + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + info, err := h.LN.GetInfo(ctxt, &lnrpc.GetInfoRequest{}) + h.NoError(err, "GetInfo") + + return info +} + +// ConnectPeer makes a RPC call to ConnectPeer and asserts there's no error. +func (h *HarnessRPC) ConnectPeer( + req *lnrpc.ConnectPeerRequest) *lnrpc.ConnectPeerResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.LN.ConnectPeer(ctxt, req) + h.NoError(err, "ConnectPeer") + + return resp +} + +// ListChannels list the channels for the given node and asserts it's +// successful. +func (h *HarnessRPC) ListChannels( + req *lnrpc.ListChannelsRequest) *lnrpc.ListChannelsResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.LN.ListChannels(ctxt, req) + h.NoError(err, "ListChannels") + + return resp +} + +// PendingChannels makes a RPC request to PendingChannels and asserts there's +// no error. +func (h *HarnessRPC) PendingChannels() *lnrpc.PendingChannelsResponse { + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + pendingChansRequest := &lnrpc.PendingChannelsRequest{} + resp, err := h.LN.PendingChannels(ctxt, pendingChansRequest) + h.NoError(err, "PendingChannels") + + return resp +} + +// ClosedChannels makes a RPC call to node's ClosedChannels and asserts. +func (h *HarnessRPC) ClosedChannels( + req *lnrpc.ClosedChannelsRequest) *lnrpc.ClosedChannelsResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.LN.ClosedChannels(ctxt, req) + h.NoError(err, "ClosedChannels") + + return resp +} + +// ListPayments lists the node's payments and asserts. +func (h *HarnessRPC) ListPayments( + req *lnrpc.ListPaymentsRequest) *lnrpc.ListPaymentsResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.LN.ListPayments(ctxt, req) + h.NoError(err, "ListPayments") + + return resp +} + +// ListInvoices list the node's invoice using the request and asserts. +func (h *HarnessRPC) ListInvoices( + req *lnrpc.ListInvoiceRequest) *lnrpc.ListInvoiceResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.LN.ListInvoices(ctxt, req) + h.NoError(err, "ListInvoice") + + return resp +} + +// DescribeGraph makes a RPC call to the node's DescribeGraph and asserts. It +// takes a bool to indicate whether we want to include private edges or not. +func (h *HarnessRPC) DescribeGraph( + req *lnrpc.ChannelGraphRequest) *lnrpc.ChannelGraph { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.LN.DescribeGraph(ctxt, req) + h.NoError(err, "DescribeGraph") + + return resp +} + +// ChannelBalance gets the channel balance and asserts. +func (h *HarnessRPC) ChannelBalance() *lnrpc.ChannelBalanceResponse { + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + req := &lnrpc.ChannelBalanceRequest{} + resp, err := h.LN.ChannelBalance(ctxt, req) + h.NoError(err, "ChannelBalance") + + return resp +} diff --git a/lntemp/rpc/peers.go b/lntemp/rpc/peers.go new file mode 100644 index 000000000..2c1578dc2 --- /dev/null +++ b/lntemp/rpc/peers.go @@ -0,0 +1,5 @@ +package rpc + +// ===================== +// PeerClient related RPCs. +// ===================== diff --git a/lntemp/rpc/router.go b/lntemp/rpc/router.go new file mode 100644 index 000000000..26a9ba6b7 --- /dev/null +++ b/lntemp/rpc/router.go @@ -0,0 +1,5 @@ +package rpc + +// ===================== +// RouterClient related RPCs. +// ===================== diff --git a/lntemp/rpc/signer.go b/lntemp/rpc/signer.go new file mode 100644 index 000000000..4a7bea403 --- /dev/null +++ b/lntemp/rpc/signer.go @@ -0,0 +1,5 @@ +package rpc + +// ===================== +// Signer related RPCs. +// ===================== diff --git a/lntemp/rpc/state.go b/lntemp/rpc/state.go new file mode 100644 index 000000000..7a25f60f1 --- /dev/null +++ b/lntemp/rpc/state.go @@ -0,0 +1,19 @@ +package rpc + +import ( + "github.com/lightningnetwork/lnd/lnrpc" +) + +// ===================== +// StateClient related RPCs. +// ===================== + +// SubscribeState makes a rpc call to StateClient and asserts there's no error. +func (h *HarnessRPC) SubscribeState() lnrpc.State_SubscribeStateClient { + client, err := h.State.SubscribeState( + h.runCtx, &lnrpc.SubscribeStateRequest{}, + ) + h.NoError(err, "SubscribeState") + + return client +} diff --git a/lntemp/rpc/wallet_kit.go b/lntemp/rpc/wallet_kit.go new file mode 100644 index 000000000..d7b700f0f --- /dev/null +++ b/lntemp/rpc/wallet_kit.go @@ -0,0 +1,24 @@ +package rpc + +import ( + "context" + + "github.com/lightningnetwork/lnd/lnrpc/walletrpc" +) + +// ===================== +// WalletKitClient related RPCs. +// ===================== + +// FinalizePsbt makes a RPC call to node's ListUnspent and asserts. +func (h *HarnessRPC) ListUnspent( + req *walletrpc.ListUnspentRequest) *walletrpc.ListUnspentResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.WalletKit.ListUnspent(ctxt, req) + h.NoError(err, "ListUnspent") + + return resp +} diff --git a/lntemp/rpc/wallet_unlocker.go b/lntemp/rpc/wallet_unlocker.go new file mode 100644 index 000000000..d1713c263 --- /dev/null +++ b/lntemp/rpc/wallet_unlocker.go @@ -0,0 +1,25 @@ +package rpc + +import ( + "context" + + "github.com/lightningnetwork/lnd/lnrpc" +) + +// ===================== +// WalletUnlockerClient related RPCs. +// ===================== + +// UnlockWallet makes a RPC request to WalletUnlocker and asserts there's no +// error. +func (h *HarnessRPC) UnlockWallet( + req *lnrpc.UnlockWalletRequest) *lnrpc.UnlockWalletResponse { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.WalletUnlocker.UnlockWallet(ctxt, req) + h.NoError(err, "UnlockWallet") + + return resp +} diff --git a/lntemp/rpc/watchtower.go b/lntemp/rpc/watchtower.go new file mode 100644 index 000000000..c18734d94 --- /dev/null +++ b/lntemp/rpc/watchtower.go @@ -0,0 +1,5 @@ +package rpc + +// ===================== +// WatchtowerClient and WatchtowerClientClient related RPCs. +// =====================