Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
dcrjson: getblocksubsidy support. (decred#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
dajohi authored and alexlyp committed May 23, 2017
1 parent 3428a40 commit 2a2ab09
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
16 changes: 16 additions & 0 deletions dcrjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ func NewGetBlockHeaderCmd(hash string, verbose *bool) *GetBlockHeaderCmd {
}
}

// GetBlockSubsidyCmd defines the getblocksubsidy JSON-RPC command.
type GetBlockSubsidyCmd struct {
Height int64
Voters uint16
}

// NewGetBlockSubsidyCmd returns a new instance which can be used to issue a
// getblocksubsidy JSON-RPC command.
func NewGetBlockSubsidyCmd(height int64, voters uint16) *GetBlockSubsidyCmd {
return &GetBlockSubsidyCmd{
Height: height,
Voters: voters,
}
}

// TemplateRequest is a request object as defined in BIP22
// (https://en.bitcoin.it/wiki/BIP_0022), it is optionally provided as an
// pointer argument to GetBlockTemplateCmd.
Expand Down Expand Up @@ -775,6 +790,7 @@ func init() {
MustRegisterCmd("getblockcount", (*GetBlockCountCmd)(nil), flags)
MustRegisterCmd("getblockhash", (*GetBlockHashCmd)(nil), flags)
MustRegisterCmd("getblockheader", (*GetBlockHeaderCmd)(nil), flags)
MustRegisterCmd("getblocksubsidy", (*GetBlockSubsidyCmd)(nil), flags)
MustRegisterCmd("getblocktemplate", (*GetBlockTemplateCmd)(nil), flags)
MustRegisterCmd("getchaintips", (*GetChainTipsCmd)(nil), flags)
MustRegisterCmd("getconnectioncount", (*GetConnectionCountCmd)(nil), flags)
Expand Down
14 changes: 14 additions & 0 deletions dcrjson/chainsvrcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ func TestChainSvrCmds(t *testing.T) {
Verbose: dcrjson.Bool(true),
},
},
{
name: "getblocksubsidy",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("getblocksubsidy", 123, 256)
},
staticCmd: func() interface{} {
return dcrjson.NewGetBlockSubsidyCmd(123, 256)
},
marshalled: `{"jsonrpc":"1.0","method":"getblocksubsidy","params":[123,256],"id":1}`,
unmarshalled: &dcrjson.GetBlockSubsidyCmd{
Height: 123,
Voters: 256,
},
},
{
name: "getblocktemplate",
newCmd: func() (interface{}, error) {
Expand Down
9 changes: 9 additions & 0 deletions dcrjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ type GetBlockChainInfoResult struct {
ChainWork string `json:"chainwork"`
}

// GetBlockSubsidyResult models the data returned from the getblocksubsidy
// command.
type GetBlockSubsidyResult struct {
Developer int64 `json:"developer"`
PoS int64 `json:"pos"`
PoW int64 `json:"pow"`
Total int64 `json:"total"`
}

// GetBlockTemplateResultTx models the transactions field of the
// getblocktemplate command.
type GetBlockTemplateResultTx struct {
Expand Down
31 changes: 31 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
"getblockcount": handleGetBlockCount,
"getblockhash": handleGetBlockHash,
"getblockheader": handleGetBlockHeader,
"getblocksubsidy": handleGetBlockSubsidy,
"getcoinsupply": handleGetCoinSupply,
"getconnectioncount": handleGetConnectionCount,
"getcurrentnet": handleGetCurrentNet,
Expand Down Expand Up @@ -2128,6 +2129,36 @@ func handleGetBlockHeader(s *rpcServer, cmd interface{}, closeChan <-chan struct

}

// handleGetBlockSubsidy implements the getblocksubsidy command.
func handleGetBlockSubsidy(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*dcrjson.GetBlockSubsidyCmd)

height := c.Height
voters := c.Voters

cache := s.chain.FetchSubsidyCache()
if cache == nil {
return nil, rpcInternalError("empty subsidy cache", "")
}

dev := blockchain.CalcBlockTaxSubsidy(cache, height, voters,
s.server.chainParams)
pos := blockchain.CalcStakeVoteSubsidy(cache, height,
s.server.chainParams) * int64(voters)
pow := blockchain.CalcBlockWorkSubsidy(cache, height, voters,
s.server.chainParams)
total := dev + pos + pow

rep := dcrjson.GetBlockSubsidyResult{
Developer: dev,
PoS: pos,
PoW: pow,
Total: total,
}

return rep, nil
}

// encodeTemplateID encodes the passed details into an ID that can be used to
// uniquely identify a block template.
func encodeTemplateID(prevHash *chainhash.Hash, lastGenerated time.Time) string {
Expand Down
12 changes: 12 additions & 0 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ var helpDescsEnUS = map[string]string{
"getblockheaderverboseresult-stakeroot": "The merkle root of the stake transaction tree",
"getblockheaderverboseresult-stakeversion": "The stake version of the block",

// GetBlockSubsidyCmd help.
"getblocksubsidy--synopsis": "Returns information regarding subsidy amounts.",
"getblocksubsidy-height": "The block height",
"getblocksubsidy-voters": "The number of voters",

// GetBlockSubsidyResult help.
"getblocksubsidyresult-developer": "The developer subsidy",
"getblocksubsidyresult-pos": "The Proof-of-Stake subsidy",
"getblocksubsidyresult-pow": "The Proof-of-Work subsidy",
"getblocksubsidyresult-total": "The total subsidy",

// TemplateRequest help.
"templaterequest-mode": "This is 'template', 'proposal', or omitted",
"templaterequest-capabilities": "List of capabilities",
Expand Down Expand Up @@ -897,6 +908,7 @@ var rpcResultTypes = map[string][]interface{}{
"getblockcount": {(*int64)(nil)},
"getblockhash": {(*string)(nil)},
"getblockheader": {(*string)(nil), (*dcrjson.GetBlockHeaderVerboseResult)(nil)},
"getblocksubsidy": {(*dcrjson.GetBlockSubsidyResult)(nil)},
"getblocktemplate": {(*dcrjson.GetBlockTemplateResult)(nil), (*string)(nil), nil},
"getconnectioncount": {(*int32)(nil)},
"getcurrentnet": {(*uint32)(nil)},
Expand Down

0 comments on commit 2a2ab09

Please sign in to comment.