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

rpc implement setban, listbanned, clearbanned #85

Merged
merged 1 commit into from
Aug 15, 2022
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
54 changes: 54 additions & 0 deletions btcjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func NewAddNodeCmd(addr string, subCmd AddNodeSubCmd) *AddNodeCmd {
}
}

// ClearBannedCmd defines the clearbanned JSON-RPC command.
type ClearBannedCmd struct{}

// NewClearBannedCmd returns a new instance which can be used to issue an clearbanned
// JSON-RPC command.
func NewClearBannedCmd() *ClearBannedCmd {
return &ClearBannedCmd{}
}

// TransactionInput represents the inputs to a transaction. Specifically a
// transaction hash and output number pair.
type TransactionInput struct {
Expand Down Expand Up @@ -757,6 +766,15 @@ func NewInvalidateBlockCmd(blockHash string) *InvalidateBlockCmd {
}
}

// ListBannedCmd defines the listbanned JSON-RPC command.
type ListBannedCmd struct{}

// NewListBannedCmd returns a new instance which can be used to issue a listbanned
// JSON-RPC command.
func NewListBannedCmd() *ListBannedCmd {
return &ListBannedCmd{}
}

// PingCmd defines the ping JSON-RPC command.
type PingCmd struct{}

Expand Down Expand Up @@ -903,6 +921,39 @@ func NewBitcoindSendRawTransactionCmd(hexTx string, maxFeeRate int32) *SendRawTr
}
}

// SetBanSubCmd defines the type used in the setban JSON-RPC command for the
// sub command field.
type SetBanSubCmd string

const (
// SBAdd indicates the specified host should be added as a persistent
// peer.
SBAdd SetBanSubCmd = "add"

// SBRemove indicates the specified peer should be removed.
SBRemove SetBanSubCmd = "remove"
)

// SetBanCmd defines the setban JSON-RPC command.
type SetBanCmd struct {
Addr string
SubCmd SetBanSubCmd `jsonrpcusage:"\"add|remove\""`
BanTime *int `jsonrpcdefault:"0"`
Absolute *bool `jsonrpcdefault:"false"`
}

// NewSetBanCmd returns a new instance which can be used to issue an setban
// JSON-RPC command.
func NewSetBanCmd(addr string, subCmd SetBanSubCmd, banTime *int,
absolute *bool) *SetBanCmd {
return &SetBanCmd{
Addr: addr,
SubCmd: subCmd,
BanTime: banTime,
Absolute: absolute,
}
}

// SetGenerateCmd defines the setgenerate JSON-RPC command.
type SetGenerateCmd struct {
Generate bool
Expand Down Expand Up @@ -1080,6 +1131,9 @@ func init() {
MustRegisterCmd("getnetworkhashps", (*GetNetworkHashPSCmd)(nil), flags)
MustRegisterCmd("getnodeaddresses", (*GetNodeAddressesCmd)(nil), flags)
MustRegisterCmd("getpeerinfo", (*GetPeerInfoCmd)(nil), flags)
MustRegisterCmd("listbanned", (*ListBannedCmd)(nil), flags)
MustRegisterCmd("setban", (*SetBanCmd)(nil), flags)
MustRegisterCmd("clearbanned", (*ClearBannedCmd)(nil), flags)
MustRegisterCmd("getrawmempool", (*GetRawMempoolCmd)(nil), flags)
MustRegisterCmd("getrawtransaction", (*GetRawTransactionCmd)(nil), flags)
MustRegisterCmd("gettxout", (*GetTxOutCmd)(nil), flags)
Expand Down
9 changes: 9 additions & 0 deletions btcjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,15 @@ type InfoChainResult struct {
Errors string `json:"errors"`
}

// ListBannedResult models the data returned from the listbanned command.
type ListBannedResult struct {
Address string `json:"address"`
BanCreated int64 `json:"ban_created"`
BannedUntil int64 `json:"banned_until"`
BanDuration int64 `json:"ban_duration"`
TimeRemaining int64 `json:"time_remaining"`
}

// TxRawResult models the data from the getrawtransaction command.
type TxRawResult struct {
Hex string `json:"hex"`
Expand Down
52 changes: 52 additions & 0 deletions rpcadapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"sync/atomic"
"time"

"github.com/lbryio/lbcd/blockchain"
"github.com/lbryio/lbcd/chaincfg/chainhash"
Expand Down Expand Up @@ -181,6 +182,57 @@ func (cm *rpcConnManager) ConnectedPeers() []rpcserverPeer {
return peers
}

// BannedPeers returns a map consisting of all banned host with banned period.
//
// This function is safe for concurrent access and is part of the
// rpcserverConnManager interface implementation.
func (cm *rpcConnManager) BannedPeers() map[string]bannedPeriod {
replyChan := make(chan map[string]bannedPeriod)
cm.server.query <- listBannedPeersMsg{reply: replyChan}
return <-replyChan
}

// SetBan removes the peer associated with the provided address from the
// list of persistent peers.
//
// This function is safe for concurrent access and is part of the
// rpcserverConnManager interface implementation.
func (cm *rpcConnManager) SetBan(addr string, since, until time.Time) error {
replyChan := make(chan error)
cm.server.query <- setBanMsg{
addr: addr,
since: since,
until: until,
reply: replyChan,
}
return <-replyChan
}

// RemoveBan removes a host from banned list.
//
// This function is safe for concurrent access and is part of the
// rpcserverConnManager interface implementation.
func (cm *rpcConnManager) RemoveBan(addr string) error {
replyChan := make(chan error)
cm.server.query <- removeBanMsg{
addr: addr,
reply: replyChan,
}
return <-replyChan
}

// ClearBanned removes all banned host with banned period.
//
// This function is safe for concurrent access and is part of the
// rpcserverConnManager interface implementation.
func (cm *rpcConnManager) ClearBanned() error {
replyChan := make(chan error)
cm.server.query <- clearBannedMsg{
reply: replyChan,
}
return <-replyChan
}

// PersistentPeers returns an array consisting of all the added persistent
// peers.
//
Expand Down
69 changes: 69 additions & 0 deletions rpcclient/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,75 @@ func (c *Client) GetPeerInfo() ([]btcjson.GetPeerInfoResult, error) {
return c.GetPeerInfoAsync().Receive()
}

// FutureListBannedResult is a future promise to deliver the result of a
// ListBannedAsync RPC invocation (or an applicable error).
type FutureListBannedResult chan *Response

// Receive waits for the Response promised by the future and returns data about
// each connected network peer.
func (r FutureListBannedResult) Receive() ([]btcjson.ListBannedResult, error) {
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}

// Unmarshal result as an array of ListBanned result objects.
var bannedPeers []btcjson.ListBannedResult
err = json.Unmarshal(res, &bannedPeers)
if err != nil {
return nil, err
}

return bannedPeers, nil
}

// SetBanCommand enumerates the available commands that the SetBanCommand function
// accepts.
type SetBanCommand string

// Constants used to indicate the command for the SetBanCommand function.
const (
// SBAdd indicates the specified host should be added as a banned
// peer.
SBAdd SetBanCommand = "add"

// SBRemove indicates the specified peer should be removed.
SBRemove SetBanCommand = "remove"
)

// String returns the SetBanCommand in human-readable form.
func (cmd SetBanCommand) String() string {
return string(cmd)
}

// FutureSetBanResult is a future promise to deliver the result of an
// SetBanAsync RPC invocation (or an applicable error).
type FutureSetBanResult chan *Response

// Receive waits for the Response promised by the future and returns an error if
// any occurred when performing the specified command.
func (r FutureSetBanResult) Receive() error {
_, err := ReceiveFuture(r)
return err
}

// SetBanAsync returns an instance of a type that can be used to get the result
// of the RPC at some future time by invoking the Receive function on the
// returned instance.
func (c *Client) SetBanAsync(addr string, command string, banTime *int,
absolute *bool) FutureSetBanResult {
cmd := btcjson.NewSetBanCmd(addr, btcjson.SetBanSubCmd(command), banTime,
absolute)
return c.SendCmd(cmd)
}

// SetBan attempts to perform the passed command on the passed persistent peer.
// For example, it can be used to add or a remove a banned peer.
func (c *Client) SetBan(addr string, command string, banTime *int,
absolute *bool) error {
return c.SetBanAsync(addr, command, banTime, absolute).Receive()
}

// FutureGetNetTotalsResult is a future promise to deliver the result of a
// GetNetTotalsAsync RPC invocation (or an applicable error).
type FutureGetNetTotalsResult chan *Response
Expand Down
Loading