Skip to content

Commit 289e2db

Browse files
committed
itest+lntemp: add ChainKitClient RPCs and refactor testChainKit
1 parent a0385a5 commit 289e2db

File tree

3 files changed

+85
-31
lines changed

3 files changed

+85
-31
lines changed

docs/release-notes/release-notes-0.16.0.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ implemented, please refer to
290290
details. Along the way, several
291291
PRs([6776](https://github.com/lightningnetwork/lnd/pull/6776),
292292
[6822](https://github.com/lightningnetwork/lnd/pull/6822),
293-
[7172](https://github.com/lightningnetwork/lnd/pull/7172)) have been made to
293+
[7172](https://github.com/lightningnetwork/lnd/pull/7172),
294+
[7245](https://github.com/lightningnetwork/lnd/pull/7245)) have been made to
294295
refactor the itest for code health and maintenance.
295296

296297
# Contributors (Alphabetical Order)

lntemp/rpc/chain_kit.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package rpc
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
7+
)
8+
9+
// =====================
10+
// ChainKitClient related RPCs.
11+
// =====================
12+
13+
// GetBestBlock makes an RPC call to chain kit client's GetBestBlock and
14+
// asserts.
15+
func (h *HarnessRPC) GetBestBlock(
16+
req *chainrpc.GetBestBlockRequest) *chainrpc.GetBestBlockResponse {
17+
18+
if req == nil {
19+
req = &chainrpc.GetBestBlockRequest{}
20+
}
21+
22+
ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
23+
defer cancel()
24+
25+
resp, err := h.ChainKit.GetBestBlock(ctxt, req)
26+
h.NoError(err, "GetBestBlock")
27+
28+
return resp
29+
}
30+
31+
// GetBlock makes an RPC call to chain kit client's GetBlock and asserts.
32+
func (h *HarnessRPC) GetBlock(
33+
req *chainrpc.GetBlockRequest) *chainrpc.GetBlockResponse {
34+
35+
if req == nil {
36+
req = &chainrpc.GetBlockRequest{}
37+
}
38+
39+
ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
40+
defer cancel()
41+
42+
resp, err := h.ChainKit.GetBlock(ctxt, req)
43+
h.NoError(err, "GetBlock")
44+
45+
return resp
46+
}
47+
48+
// GetBlockHash makes an RPC call to chain kit client's GetBlockHash and
49+
// asserts.
50+
func (h *HarnessRPC) GetBlockHash(
51+
req *chainrpc.GetBlockHashRequest) *chainrpc.GetBlockHashResponse {
52+
53+
if req == nil {
54+
req = &chainrpc.GetBlockHashRequest{}
55+
}
56+
57+
ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
58+
defer cancel()
59+
60+
resp, err := h.ChainKit.GetBlockHash(ctxt, req)
61+
h.NoError(err, "GetBlockHash")
62+
63+
return resp
64+
}

lntest/itest/lnd_onchain_test.go

+19-30
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,28 @@ import (
2424

2525
// testChainKit tests ChainKit RPC endpoints.
2626
func testChainKit(ht *lntemp.HarnessTest) {
27-
ctx := context.Background()
28-
29-
// Test functions registered as test cases spin up separate nodes during
30-
// execution. By calling sub-test functions as seen below we avoid the
31-
// need to start separate nodes.
32-
testChainKitGetBlock(ctx, ht)
33-
testChainKitGetBlockHash(ctx, ht)
27+
// Test functions registered as test cases spin up separate nodes
28+
// during execution. By calling sub-test functions as seen below we
29+
// avoid the need to start separate nodes.
30+
testChainKitGetBlock(ht)
31+
testChainKitGetBlockHash(ht)
3432
}
3533

3634
// testChainKitGetBlock ensures that given a block hash, the RPC endpoint
3735
// returns the correct target block.
38-
func testChainKitGetBlock(ctx context.Context, ht *lntemp.HarnessTest) {
36+
func testChainKitGetBlock(ht *lntemp.HarnessTest) {
3937
// Get best block hash.
40-
bestBlockRes, err := ht.Alice.RPC.ChainKit.GetBestBlock(
41-
ctx, &chainrpc.GetBestBlockRequest{},
42-
)
43-
require.NoError(ht, err)
38+
bestBlockRes := ht.Alice.RPC.GetBestBlock(nil)
39+
4440
var bestBlockHash chainhash.Hash
45-
err = bestBlockHash.SetBytes(bestBlockRes.BlockHash)
41+
err := bestBlockHash.SetBytes(bestBlockRes.BlockHash)
4642
require.NoError(ht, err)
4743

4844
// Retrieve the best block by hash.
49-
getBlockRes, err := ht.Alice.RPC.ChainKit.GetBlock(
50-
ctx, &chainrpc.GetBlockRequest{
51-
BlockHash: bestBlockHash.CloneBytes(),
52-
},
53-
)
54-
require.NoError(ht, err)
45+
getBlockReq := &chainrpc.GetBlockRequest{
46+
BlockHash: bestBlockHash[:],
47+
}
48+
getBlockRes := ht.Alice.RPC.GetBlock(getBlockReq)
5549

5650
// Deserialize the block which was retrieved by hash.
5751
msgBlock := &wire.MsgBlock{}
@@ -67,20 +61,15 @@ func testChainKitGetBlock(ctx context.Context, ht *lntemp.HarnessTest) {
6761

6862
// testChainKitGetBlockHash ensures that given a block height, the RPC endpoint
6963
// returns the correct target block hash.
70-
func testChainKitGetBlockHash(ctx context.Context, ht *lntemp.HarnessTest) {
64+
func testChainKitGetBlockHash(ht *lntemp.HarnessTest) {
7165
// Get best block hash.
72-
bestBlockRes, err := ht.Alice.RPC.ChainKit.GetBestBlock(
73-
ctx, &chainrpc.GetBestBlockRequest{},
74-
)
75-
require.NoError(ht, err)
66+
bestBlockRes := ht.Alice.RPC.GetBestBlock(nil)
7667

7768
// Retrieve the block hash at best block height.
78-
getBlockHashRes, err := ht.Alice.RPC.ChainKit.GetBlockHash(
79-
ctx, &chainrpc.GetBlockHashRequest{
80-
BlockHeight: int64(bestBlockRes.BlockHeight),
81-
},
82-
)
83-
require.NoError(ht, err)
69+
req := &chainrpc.GetBlockHashRequest{
70+
BlockHeight: int64(bestBlockRes.BlockHeight),
71+
}
72+
getBlockHashRes := ht.Alice.RPC.GetBlockHash(req)
8473

8574
// Ensure best block hash is the same as retrieved block hash.
8675
expected := bestBlockRes.BlockHash

0 commit comments

Comments
 (0)