From 289e2db2ee92046b52990de4c59365f6e21d659b Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 9 Dec 2022 08:42:47 +0800 Subject: [PATCH] itest+lntemp: add `ChainKitClient` RPCs and refactor `testChainKit` --- docs/release-notes/release-notes-0.16.0.md | 3 +- lntemp/rpc/chain_kit.go | 64 ++++++++++++++++++++++ lntest/itest/lnd_onchain_test.go | 49 +++++++---------- 3 files changed, 85 insertions(+), 31 deletions(-) create mode 100644 lntemp/rpc/chain_kit.go diff --git a/docs/release-notes/release-notes-0.16.0.md b/docs/release-notes/release-notes-0.16.0.md index b01399301..642fdf24b 100644 --- a/docs/release-notes/release-notes-0.16.0.md +++ b/docs/release-notes/release-notes-0.16.0.md @@ -290,7 +290,8 @@ implemented, please refer to details. Along the way, several PRs([6776](https://github.com/lightningnetwork/lnd/pull/6776), [6822](https://github.com/lightningnetwork/lnd/pull/6822), -[7172](https://github.com/lightningnetwork/lnd/pull/7172)) have been made to +[7172](https://github.com/lightningnetwork/lnd/pull/7172), +[7245](https://github.com/lightningnetwork/lnd/pull/7245)) have been made to refactor the itest for code health and maintenance. # Contributors (Alphabetical Order) diff --git a/lntemp/rpc/chain_kit.go b/lntemp/rpc/chain_kit.go new file mode 100644 index 000000000..c668b3a0f --- /dev/null +++ b/lntemp/rpc/chain_kit.go @@ -0,0 +1,64 @@ +package rpc + +import ( + "context" + + "github.com/lightningnetwork/lnd/lnrpc/chainrpc" +) + +// ===================== +// ChainKitClient related RPCs. +// ===================== + +// GetBestBlock makes an RPC call to chain kit client's GetBestBlock and +// asserts. +func (h *HarnessRPC) GetBestBlock( + req *chainrpc.GetBestBlockRequest) *chainrpc.GetBestBlockResponse { + + if req == nil { + req = &chainrpc.GetBestBlockRequest{} + } + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.ChainKit.GetBestBlock(ctxt, req) + h.NoError(err, "GetBestBlock") + + return resp +} + +// GetBlock makes an RPC call to chain kit client's GetBlock and asserts. +func (h *HarnessRPC) GetBlock( + req *chainrpc.GetBlockRequest) *chainrpc.GetBlockResponse { + + if req == nil { + req = &chainrpc.GetBlockRequest{} + } + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.ChainKit.GetBlock(ctxt, req) + h.NoError(err, "GetBlock") + + return resp +} + +// GetBlockHash makes an RPC call to chain kit client's GetBlockHash and +// asserts. +func (h *HarnessRPC) GetBlockHash( + req *chainrpc.GetBlockHashRequest) *chainrpc.GetBlockHashResponse { + + if req == nil { + req = &chainrpc.GetBlockHashRequest{} + } + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + resp, err := h.ChainKit.GetBlockHash(ctxt, req) + h.NoError(err, "GetBlockHash") + + return resp +} diff --git a/lntest/itest/lnd_onchain_test.go b/lntest/itest/lnd_onchain_test.go index 31af68363..4f94b7e0f 100644 --- a/lntest/itest/lnd_onchain_test.go +++ b/lntest/itest/lnd_onchain_test.go @@ -24,34 +24,28 @@ import ( // testChainKit tests ChainKit RPC endpoints. func testChainKit(ht *lntemp.HarnessTest) { - ctx := context.Background() - - // Test functions registered as test cases spin up separate nodes during - // execution. By calling sub-test functions as seen below we avoid the - // need to start separate nodes. - testChainKitGetBlock(ctx, ht) - testChainKitGetBlockHash(ctx, ht) + // Test functions registered as test cases spin up separate nodes + // during execution. By calling sub-test functions as seen below we + // avoid the need to start separate nodes. + testChainKitGetBlock(ht) + testChainKitGetBlockHash(ht) } // testChainKitGetBlock ensures that given a block hash, the RPC endpoint // returns the correct target block. -func testChainKitGetBlock(ctx context.Context, ht *lntemp.HarnessTest) { +func testChainKitGetBlock(ht *lntemp.HarnessTest) { // Get best block hash. - bestBlockRes, err := ht.Alice.RPC.ChainKit.GetBestBlock( - ctx, &chainrpc.GetBestBlockRequest{}, - ) - require.NoError(ht, err) + bestBlockRes := ht.Alice.RPC.GetBestBlock(nil) + var bestBlockHash chainhash.Hash - err = bestBlockHash.SetBytes(bestBlockRes.BlockHash) + err := bestBlockHash.SetBytes(bestBlockRes.BlockHash) require.NoError(ht, err) // Retrieve the best block by hash. - getBlockRes, err := ht.Alice.RPC.ChainKit.GetBlock( - ctx, &chainrpc.GetBlockRequest{ - BlockHash: bestBlockHash.CloneBytes(), - }, - ) - require.NoError(ht, err) + getBlockReq := &chainrpc.GetBlockRequest{ + BlockHash: bestBlockHash[:], + } + getBlockRes := ht.Alice.RPC.GetBlock(getBlockReq) // Deserialize the block which was retrieved by hash. msgBlock := &wire.MsgBlock{} @@ -67,20 +61,15 @@ func testChainKitGetBlock(ctx context.Context, ht *lntemp.HarnessTest) { // testChainKitGetBlockHash ensures that given a block height, the RPC endpoint // returns the correct target block hash. -func testChainKitGetBlockHash(ctx context.Context, ht *lntemp.HarnessTest) { +func testChainKitGetBlockHash(ht *lntemp.HarnessTest) { // Get best block hash. - bestBlockRes, err := ht.Alice.RPC.ChainKit.GetBestBlock( - ctx, &chainrpc.GetBestBlockRequest{}, - ) - require.NoError(ht, err) + bestBlockRes := ht.Alice.RPC.GetBestBlock(nil) // Retrieve the block hash at best block height. - getBlockHashRes, err := ht.Alice.RPC.ChainKit.GetBlockHash( - ctx, &chainrpc.GetBlockHashRequest{ - BlockHeight: int64(bestBlockRes.BlockHeight), - }, - ) - require.NoError(ht, err) + req := &chainrpc.GetBlockHashRequest{ + BlockHeight: int64(bestBlockRes.BlockHeight), + } + getBlockHashRes := ht.Alice.RPC.GetBlockHash(req) // Ensure best block hash is the same as retrieved block hash. expected := bestBlockRes.BlockHash