itest+lntemp: add ChainKitClient RPCs and refactor testChainKit

This commit is contained in:
yyforyongyu 2022-12-09 08:42:47 +08:00
parent a0385a535b
commit 289e2db2ee
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
3 changed files with 85 additions and 31 deletions

View File

@ -290,7 +290,8 @@ implemented, please refer to
details. Along the way, several details. Along the way, several
PRs([6776](https://github.com/lightningnetwork/lnd/pull/6776), PRs([6776](https://github.com/lightningnetwork/lnd/pull/6776),
[6822](https://github.com/lightningnetwork/lnd/pull/6822), [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. refactor the itest for code health and maintenance.
# Contributors (Alphabetical Order) # Contributors (Alphabetical Order)

64
lntemp/rpc/chain_kit.go Normal file
View File

@ -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
}

View File

@ -24,34 +24,28 @@ import (
// testChainKit tests ChainKit RPC endpoints. // testChainKit tests ChainKit RPC endpoints.
func testChainKit(ht *lntemp.HarnessTest) { 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
// Test functions registered as test cases spin up separate nodes during // avoid the need to start separate nodes.
// execution. By calling sub-test functions as seen below we avoid the testChainKitGetBlock(ht)
// need to start separate nodes. testChainKitGetBlockHash(ht)
testChainKitGetBlock(ctx, ht)
testChainKitGetBlockHash(ctx, ht)
} }
// testChainKitGetBlock ensures that given a block hash, the RPC endpoint // testChainKitGetBlock ensures that given a block hash, the RPC endpoint
// returns the correct target block. // returns the correct target block.
func testChainKitGetBlock(ctx context.Context, ht *lntemp.HarnessTest) { func testChainKitGetBlock(ht *lntemp.HarnessTest) {
// Get best block hash. // Get best block hash.
bestBlockRes, err := ht.Alice.RPC.ChainKit.GetBestBlock( bestBlockRes := ht.Alice.RPC.GetBestBlock(nil)
ctx, &chainrpc.GetBestBlockRequest{},
)
require.NoError(ht, err)
var bestBlockHash chainhash.Hash var bestBlockHash chainhash.Hash
err = bestBlockHash.SetBytes(bestBlockRes.BlockHash) err := bestBlockHash.SetBytes(bestBlockRes.BlockHash)
require.NoError(ht, err) require.NoError(ht, err)
// Retrieve the best block by hash. // Retrieve the best block by hash.
getBlockRes, err := ht.Alice.RPC.ChainKit.GetBlock( getBlockReq := &chainrpc.GetBlockRequest{
ctx, &chainrpc.GetBlockRequest{ BlockHash: bestBlockHash[:],
BlockHash: bestBlockHash.CloneBytes(), }
}, getBlockRes := ht.Alice.RPC.GetBlock(getBlockReq)
)
require.NoError(ht, err)
// Deserialize the block which was retrieved by hash. // Deserialize the block which was retrieved by hash.
msgBlock := &wire.MsgBlock{} 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 // testChainKitGetBlockHash ensures that given a block height, the RPC endpoint
// returns the correct target block hash. // returns the correct target block hash.
func testChainKitGetBlockHash(ctx context.Context, ht *lntemp.HarnessTest) { func testChainKitGetBlockHash(ht *lntemp.HarnessTest) {
// Get best block hash. // Get best block hash.
bestBlockRes, err := ht.Alice.RPC.ChainKit.GetBestBlock( bestBlockRes := ht.Alice.RPC.GetBestBlock(nil)
ctx, &chainrpc.GetBestBlockRequest{},
)
require.NoError(ht, err)
// Retrieve the block hash at best block height. // Retrieve the block hash at best block height.
getBlockHashRes, err := ht.Alice.RPC.ChainKit.GetBlockHash( req := &chainrpc.GetBlockHashRequest{
ctx, &chainrpc.GetBlockHashRequest{ BlockHeight: int64(bestBlockRes.BlockHeight),
BlockHeight: int64(bestBlockRes.BlockHeight), }
}, getBlockHashRes := ht.Alice.RPC.GetBlockHash(req)
)
require.NoError(ht, err)
// Ensure best block hash is the same as retrieved block hash. // Ensure best block hash is the same as retrieved block hash.
expected := bestBlockRes.BlockHash expected := bestBlockRes.BlockHash