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

Commit

Permalink
rpcserver: Add existsmissedtickets RPC.
Browse files Browse the repository at this point in the history
This RPC works identically to the existsexpiredtickets RPC but checks
for missed rather than expired tickets.
  • Loading branch information
jrick committed May 17, 2017
1 parent a60c70e commit 39734da
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
17 changes: 17 additions & 0 deletions blockchain/stakeext.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ func (b *BlockChain) CheckLiveTickets(hashes []chainhash.Hash) []bool {
return existsSlice
}

// CheckMissedTickets returns a slice of bools representing whether each ticket
// hash has been missed in the live ticket treap of the best node.
//
// This function is safe for concurrent access.
func (b *BlockChain) CheckMissedTickets(hashes []chainhash.Hash) []bool {
b.chainLock.RLock()
sn := b.bestNode.stakeNode
b.chainLock.RUnlock()

existsSlice := make([]bool, len(hashes))
for i := range hashes {
existsSlice[i] = sn.ExistsMissedTicket(hashes[i])
}

return existsSlice
}

// CheckExpiredTicket returns whether or not a ticket was ever expired.
//
// This function is safe for concurrent access.
Expand Down
14 changes: 14 additions & 0 deletions dcrjson/dcrdcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ func NewExistsAddressesCmd(addresses []string) *ExistsAddressesCmd {
}
}

// ExistsMissedTicketsCmd defines the existsmissedtickets JSON-RPC command.
type ExistsMissedTicketsCmd struct {
TxHashBlob string
}

// NewExistsMissedTicketsCmd returns a new instance which can be used to issue an
// existsmissedtickets JSON-RPC command.
func NewExistsMissedTicketsCmd(txHashBlob string) *ExistsMissedTicketsCmd {
return &ExistsMissedTicketsCmd{
TxHashBlob: txHashBlob,
}
}

// ExistsExpiredTicketsCmd defines the existsexpiredtickets JSON-RPC command.
type ExistsExpiredTicketsCmd struct {
TxHashBlob string
Expand Down Expand Up @@ -281,6 +294,7 @@ func init() {
MustRegisterCmd("estimatestakediff", (*EstimateStakeDiffCmd)(nil), flags)
MustRegisterCmd("existsaddress", (*ExistsAddressCmd)(nil), flags)
MustRegisterCmd("existsaddresses", (*ExistsAddressesCmd)(nil), flags)
MustRegisterCmd("existsmissedtickets", (*ExistsMissedTicketsCmd)(nil), flags)
MustRegisterCmd("existsexpiredtickets", (*ExistsExpiredTicketsCmd)(nil), flags)
MustRegisterCmd("existsliveticket", (*ExistsLiveTicketCmd)(nil), flags)
MustRegisterCmd("existslivetickets", (*ExistsLiveTicketsCmd)(nil), flags)
Expand Down
27 changes: 27 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
"estimatestakediff": handleEstimateStakeDiff,
"existsaddress": handleExistsAddress,
"existsaddresses": handleExistsAddresses,
"existsmissedtickets": handleExistsMissedTickets,
"existsexpiredtickets": handleExistsExpiredTickets,
"existsliveticket": handleExistsLiveTicket,
"existslivetickets": handleExistsLiveTickets,
Expand Down Expand Up @@ -1605,6 +1606,32 @@ func handleExistsAddresses(s *rpcServer, cmd interface{}, closeChan <-chan struc
return hex.EncodeToString([]byte(set)), nil
}

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

hashes, err := dcrjson.DecodeConcatenatedHashes(c.TxHashBlob)
if err != nil {
return nil, err
}

exists := s.server.blockManager.chain.CheckMissedTickets(hashes)
if len(exists) != len(hashes) {
return nil, rpcInvalidError("Invalid missed ticket count "+
"got %v, want %v", len(exists), len(hashes))
}

// Convert the slice of bools into a compacted set of bit flags.
set := bitset.NewBytes(len(hashes))
for i := range exists {
if exists[i] {
set.Set(i)
}
}

return hex.EncodeToString([]byte(set)), nil
}

// handleExistsExpiredTickets implements the existsexpiredtickets command.
func handleExistsExpiredTickets(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*dcrjson.ExistsExpiredTicketsCmd)
Expand Down
6 changes: 6 additions & 0 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ var helpDescsEnUS = map[string]string{
"existsaddresses-addresses": "The addresses to check",
"existsaddresses--result0": "Bitset of bools showing if addresses exist or not",

// ExitsMissedTicketsCmd help.
"existsmissedtickets--synopsis": "Test for the existance of the provided tickets in the missed ticket map",
"existsmissedtickets-txhashblob": "Blob containing the hashes to check",
"existsmissedtickets--result0": "Bool blob showing if the ticket exists in the missed ticket database or not",

// ExistsExpiredTicketsCmd help.
"existsexpiredtickets--synopsis": "Test for the existance of the provided tickets in the expired ticket map",
"existsexpiredtickets-txhashblob": "Blob containing the hashes to check",
Expand Down Expand Up @@ -879,6 +884,7 @@ var rpcResultTypes = map[string][]interface{}{
"estimatestakediff": {(*dcrjson.EstimateStakeDiffResult)(nil)},
"existsaddress": {(*bool)(nil)},
"existsaddresses": {(*string)(nil)},
"existsmissedtickets": {(*string)(nil)},
"existsexpiredtickets": {(*string)(nil)},
"existsliveticket": {(*bool)(nil)},
"existslivetickets": {(*string)(nil)},
Expand Down

0 comments on commit 39734da

Please sign in to comment.