Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-v1.8] rpcserver: Modify getnetworkhashps -1 blocks logic. #3193

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions internal/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2599,27 +2599,15 @@ func handleGetNetworkHashPS(_ context.Context, s *Server, cmd interface{}) (inte
endHeight = best.Height
}

// Calculate the number of blocks per retarget interval based on the
// chain parameters.
params := s.cfg.ChainParams
blocksPerRetarget := int64(params.TargetTimespan / params.TargetTimePerBlock)

// Calculate the starting block height based on the passed number of
// blocks. When the passed value is negative, use the last block the
// difficulty changed as the starting height. Also make sure the
// Calculate the starting block height based on the passed number of blocks.
// When the passed value is negative, use the default. Also, make sure the
// starting height is not before the beginning of the chain.

numBlocks := int64(120)
if c.Blocks != nil {
if c.Blocks != nil && *c.Blocks >= 0 {
numBlocks = int64(*c.Blocks)
}

var startHeight int64
if numBlocks <= 0 {
startHeight = endHeight - ((endHeight % blocksPerRetarget) + 1)
} else {
startHeight = endHeight - numBlocks
}
startHeight := endHeight - numBlocks
if startHeight < 0 {
startHeight = 0
}
Expand Down
8 changes: 3 additions & 5 deletions internal/rpcserver/rpcserverhandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4567,11 +4567,9 @@ func TestHandleGetNetworkHashPS(t *testing.T) {
networkHashPSResult := int64(2014899978133500709)

testRPCServerHandler(t, []rpcTest{{
name: "handleGetNetworkHashPS: ok",
handler: handleGetNetworkHashPS,
cmd: &types.GetNetworkHashPSCmd{
Blocks: dcrjson.Int(0),
},
name: "handleGetNetworkHashPS: ok",
handler: handleGetNetworkHashPS,
cmd: &types.GetNetworkHashPSCmd{},
mockChain: mc(),
result: networkHashPSResult,
}, {
Expand Down
2 changes: 1 addition & 1 deletion internal/rpcserver/rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ var helpDescsEnUS = map[string]string{

// GetNetworkHashPSCmd help.
"getnetworkhashps--synopsis": "Returns the estimated network hashes per second for the block heights provided by the parameters.",
"getnetworkhashps-blocks": "The number of blocks, or -1 for blocks since last difficulty change",
"getnetworkhashps-blocks": "The number of blocks or -1 for the default number of blocks",
"getnetworkhashps-height": "Perform estimate ending with this height or -1 for current best chain block height",
"getnetworkhashps--result0": "Estimated hashes per second",

Expand Down