From 5a1c705165aafe69c905bc3576fefc79cd4ee766 Mon Sep 17 00:00:00 2001 From: Donald Adu-Poku Date: Sun, 26 Jul 2020 00:39:30 +0000 Subject: [PATCH] rpcserver: add verifychain & getdifficulty tests. This adds rpc tests for the verifychain and getdifficulty rpcs. --- internal/rpcserver/rpcserverhandlers_test.go | 76 +++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/internal/rpcserver/rpcserverhandlers_test.go b/internal/rpcserver/rpcserverhandlers_test.go index 25559891..d00d9867 100644 --- a/internal/rpcserver/rpcserverhandlers_test.go +++ b/internal/rpcserver/rpcserverhandlers_test.go @@ -111,6 +111,7 @@ type testRPCChain struct { blockByHash *dcrutil.Block blockByHashErr error blockByHeight *dcrutil.Block + blockByHeightErr error blockHashByHeight *chainhash.Hash blockHashByHeightErr error blockHeightByHash int64 @@ -170,7 +171,7 @@ func (c *testRPCChain) BlockByHash(hash *chainhash.Hash) (*dcrutil.Block, error) // BlockByHeight returns a mocked block at the given height. func (c *testRPCChain) BlockByHeight(height int64) (*dcrutil.Block, error) { - return c.blockByHeight, nil + return c.blockByHeight, c.blockByHeightErr } // BlockHashByHeight returns a mocked hash of the block at the given height. @@ -3167,6 +3168,22 @@ func TestHandleGetCurrentNet(t *testing.T) { }}) } +func TestHandleGetDifficulty(t *testing.T) { + t.Parallel() + + testRPCServerHandler(t, []rpcTest{{ + name: "handleGetDifficulty: ok", + handler: handleGetDifficulty, + mockChain: func() *testRPCChain { + chain := defaultMockRPCChain() + chain.bestSnapshot.Bits = defaultChainParams.PowLimitBits + return chain + }(), + cmd: &types.GetDifficultyCmd{}, + result: float64(1.0), + }}) +} + func TestHandleGetInfo(t *testing.T) { t.Parallel() @@ -3899,6 +3916,63 @@ func TestHandleValidateAddress(t *testing.T) { }}) } +func TestHandleVerifyChain(t *testing.T) { + t.Parallel() + + hash := mustParseHash("00000000000000000c07ff0178ad600db22c18f8f47fa423fa144b2ca919475d") + + testRPCServerHandler(t, []rpcTest{{ + name: "handleVerifyChain: ok", + handler: handleVerifyChain, + cmd: &types.VerifyChainCmd{}, + result: true, + }, { + name: "handleVerifyChain: ok with depth=50", + handler: handleVerifyChain, + cmd: &types.VerifyChainCmd{ + CheckDepth: dcrjson.Int64(50), + }, + mockChain: func() *testRPCChain { + chain := defaultMockRPCChain() + chain.bestSnapshot = &blockchain.BestState{ + Height: 1, + Hash: *hash, + } + return chain + }(), + result: true, + }, { + name: "handleVerifyChain: invalid block with checklevel=1", + handler: handleVerifyChain, + cmd: &types.VerifyChainCmd{ + CheckLevel: dcrjson.Int64(1), + CheckDepth: dcrjson.Int64(50), + }, + mockSanityChecker: func() *testSanityChecker { + checker := defaultMockSanityChecker() + checker.checkBlockSanityErr = errors.New("invalid block") + return checker + }(), + result: false, + }, { + name: "handleVerifyChain: no block exists", + handler: handleVerifyChain, + cmd: &types.VerifyChainCmd{ + CheckDepth: dcrjson.Int64(50), + }, + mockChain: func() *testRPCChain { + chain := defaultMockRPCChain() + chain.bestSnapshot = &blockchain.BestState{ + Height: 100, + Hash: *hash, + } + chain.blockByHeightErr = errors.New("no block exists") + return chain + }(), + result: false, + }}) +} + func testRPCServerHandler(t *testing.T, tests []rpcTest) { t.Helper()