github.com/lbryio/lbcd@v0.22.119/rpcadapters.go (about) 1 // Copyright (c) 2017 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "sync/atomic" 9 "time" 10 11 "github.com/lbryio/lbcd/blockchain" 12 "github.com/lbryio/lbcd/chaincfg/chainhash" 13 "github.com/lbryio/lbcd/mempool" 14 "github.com/lbryio/lbcd/netsync" 15 "github.com/lbryio/lbcd/peer" 16 "github.com/lbryio/lbcd/wire" 17 btcutil "github.com/lbryio/lbcutil" 18 ) 19 20 // rpcPeer provides a peer for use with the RPC server and implements the 21 // rpcserverPeer interface. 22 type rpcPeer serverPeer 23 24 // Ensure rpcPeer implements the rpcserverPeer interface. 25 var _ rpcserverPeer = (*rpcPeer)(nil) 26 27 // ToPeer returns the underlying peer instance. 28 // 29 // This function is safe for concurrent access and is part of the rpcserverPeer 30 // interface implementation. 31 func (p *rpcPeer) ToPeer() *peer.Peer { 32 if p == nil { 33 return nil 34 } 35 return (*serverPeer)(p).Peer 36 } 37 38 // IsTxRelayDisabled returns whether or not the peer has disabled transaction 39 // relay. 40 // 41 // This function is safe for concurrent access and is part of the rpcserverPeer 42 // interface implementation. 43 func (p *rpcPeer) IsTxRelayDisabled() bool { 44 return (*serverPeer)(p).disableRelayTx 45 } 46 47 // BanScore returns the current integer value that represents how close the peer 48 // is to being banned. 49 // 50 // This function is safe for concurrent access and is part of the rpcserverPeer 51 // interface implementation. 52 func (p *rpcPeer) BanScore() uint32 { 53 return (*serverPeer)(p).banScore.Int() 54 } 55 56 // FeeFilter returns the requested current minimum fee rate for which 57 // transactions should be announced. 58 // 59 // This function is safe for concurrent access and is part of the rpcserverPeer 60 // interface implementation. 61 func (p *rpcPeer) FeeFilter() int64 { 62 return atomic.LoadInt64(&(*serverPeer)(p).feeFilter) 63 } 64 65 // rpcConnManager provides a connection manager for use with the RPC server and 66 // implements the rpcserverConnManager interface. 67 type rpcConnManager struct { 68 server *server 69 } 70 71 // Ensure rpcConnManager implements the rpcserverConnManager interface. 72 var _ rpcserverConnManager = &rpcConnManager{} 73 74 // Connect adds the provided address as a new outbound peer. The permanent flag 75 // indicates whether or not to make the peer persistent and reconnect if the 76 // connection is lost. Attempting to connect to an already existing peer will 77 // return an error. 78 // 79 // This function is safe for concurrent access and is part of the 80 // rpcserverConnManager interface implementation. 81 func (cm *rpcConnManager) Connect(addr string, permanent bool) error { 82 replyChan := make(chan error) 83 cm.server.query <- connectNodeMsg{ 84 addr: addr, 85 permanent: permanent, 86 reply: replyChan, 87 } 88 return <-replyChan 89 } 90 91 // RemoveByID removes the peer associated with the provided id from the list of 92 // persistent peers. Attempting to remove an id that does not exist will return 93 // an error. 94 // 95 // This function is safe for concurrent access and is part of the 96 // rpcserverConnManager interface implementation. 97 func (cm *rpcConnManager) RemoveByID(id int32) error { 98 replyChan := make(chan error) 99 cm.server.query <- removeNodeMsg{ 100 cmp: func(sp *serverPeer) bool { return sp.ID() == id }, 101 reply: replyChan, 102 } 103 return <-replyChan 104 } 105 106 // RemoveByAddr removes the peer associated with the provided address from the 107 // list of persistent peers. Attempting to remove an address that does not 108 // exist will return an error. 109 // 110 // This function is safe for concurrent access and is part of the 111 // rpcserverConnManager interface implementation. 112 func (cm *rpcConnManager) RemoveByAddr(addr string) error { 113 replyChan := make(chan error) 114 cm.server.query <- removeNodeMsg{ 115 cmp: func(sp *serverPeer) bool { return sp.Addr() == addr }, 116 reply: replyChan, 117 } 118 return <-replyChan 119 } 120 121 // DisconnectByID disconnects the peer associated with the provided id. This 122 // applies to both inbound and outbound peers. Attempting to remove an id that 123 // does not exist will return an error. 124 // 125 // This function is safe for concurrent access and is part of the 126 // rpcserverConnManager interface implementation. 127 func (cm *rpcConnManager) DisconnectByID(id int32) error { 128 replyChan := make(chan error) 129 cm.server.query <- disconnectNodeMsg{ 130 cmp: func(sp *serverPeer) bool { return sp.ID() == id }, 131 reply: replyChan, 132 } 133 return <-replyChan 134 } 135 136 // DisconnectByAddr disconnects the peer associated with the provided address. 137 // This applies to both inbound and outbound peers. Attempting to remove an 138 // address that does not exist will return an error. 139 // 140 // This function is safe for concurrent access and is part of the 141 // rpcserverConnManager interface implementation. 142 func (cm *rpcConnManager) DisconnectByAddr(addr string) error { 143 replyChan := make(chan error) 144 cm.server.query <- disconnectNodeMsg{ 145 cmp: func(sp *serverPeer) bool { return sp.Addr() == addr }, 146 reply: replyChan, 147 } 148 return <-replyChan 149 } 150 151 // ConnectedCount returns the number of currently connected peers. 152 // 153 // This function is safe for concurrent access and is part of the 154 // rpcserverConnManager interface implementation. 155 func (cm *rpcConnManager) ConnectedCount() int32 { 156 return cm.server.ConnectedCount() 157 } 158 159 // NetTotals returns the sum of all bytes received and sent across the network 160 // for all peers. 161 // 162 // This function is safe for concurrent access and is part of the 163 // rpcserverConnManager interface implementation. 164 func (cm *rpcConnManager) NetTotals() (uint64, uint64) { 165 return cm.server.NetTotals() 166 } 167 168 // ConnectedPeers returns an array consisting of all connected peers. 169 // 170 // This function is safe for concurrent access and is part of the 171 // rpcserverConnManager interface implementation. 172 func (cm *rpcConnManager) ConnectedPeers() []rpcserverPeer { 173 replyChan := make(chan []*serverPeer) 174 cm.server.query <- getPeersMsg{reply: replyChan} 175 serverPeers := <-replyChan 176 177 // Convert to RPC server peers. 178 peers := make([]rpcserverPeer, 0, len(serverPeers)) 179 for _, sp := range serverPeers { 180 peers = append(peers, (*rpcPeer)(sp)) 181 } 182 return peers 183 } 184 185 // BannedPeers returns a map consisting of all banned host with banned period. 186 // 187 // This function is safe for concurrent access and is part of the 188 // rpcserverConnManager interface implementation. 189 func (cm *rpcConnManager) BannedPeers() map[string]bannedPeriod { 190 replyChan := make(chan map[string]bannedPeriod) 191 cm.server.query <- listBannedPeersMsg{reply: replyChan} 192 return <-replyChan 193 } 194 195 // SetBan removes the peer associated with the provided address from the 196 // list of persistent peers. 197 // 198 // This function is safe for concurrent access and is part of the 199 // rpcserverConnManager interface implementation. 200 func (cm *rpcConnManager) SetBan(addr string, since, until time.Time) error { 201 replyChan := make(chan error) 202 cm.server.query <- setBanMsg{ 203 addr: addr, 204 since: since, 205 until: until, 206 reply: replyChan, 207 } 208 return <-replyChan 209 } 210 211 // RemoveBan removes a host from banned list. 212 // 213 // This function is safe for concurrent access and is part of the 214 // rpcserverConnManager interface implementation. 215 func (cm *rpcConnManager) RemoveBan(addr string) error { 216 replyChan := make(chan error) 217 cm.server.query <- removeBanMsg{ 218 addr: addr, 219 reply: replyChan, 220 } 221 return <-replyChan 222 } 223 224 // ClearBanned removes all banned host with banned period. 225 // 226 // This function is safe for concurrent access and is part of the 227 // rpcserverConnManager interface implementation. 228 func (cm *rpcConnManager) ClearBanned() error { 229 replyChan := make(chan error) 230 cm.server.query <- clearBannedMsg{ 231 reply: replyChan, 232 } 233 return <-replyChan 234 } 235 236 // PersistentPeers returns an array consisting of all the added persistent 237 // peers. 238 // 239 // This function is safe for concurrent access and is part of the 240 // rpcserverConnManager interface implementation. 241 func (cm *rpcConnManager) PersistentPeers() []rpcserverPeer { 242 replyChan := make(chan []*serverPeer) 243 cm.server.query <- getAddedNodesMsg{reply: replyChan} 244 serverPeers := <-replyChan 245 246 // Convert to generic peers. 247 peers := make([]rpcserverPeer, 0, len(serverPeers)) 248 for _, sp := range serverPeers { 249 peers = append(peers, (*rpcPeer)(sp)) 250 } 251 return peers 252 } 253 254 // BroadcastMessage sends the provided message to all currently connected peers. 255 // 256 // This function is safe for concurrent access and is part of the 257 // rpcserverConnManager interface implementation. 258 func (cm *rpcConnManager) BroadcastMessage(msg wire.Message) { 259 cm.server.BroadcastMessage(msg) 260 } 261 262 // AddRebroadcastInventory adds the provided inventory to the list of 263 // inventories to be rebroadcast at random intervals until they show up in a 264 // block. 265 // 266 // This function is safe for concurrent access and is part of the 267 // rpcserverConnManager interface implementation. 268 func (cm *rpcConnManager) AddRebroadcastInventory(iv *wire.InvVect, data interface{}) { 269 cm.server.AddRebroadcastInventory(iv, data) 270 } 271 272 // RelayTransactions generates and relays inventory vectors for all of the 273 // passed transactions to all connected peers. 274 func (cm *rpcConnManager) RelayTransactions(txns []*mempool.TxDesc) { 275 cm.server.relayTransactions(txns) 276 } 277 278 // NodeAddresses returns an array consisting node addresses which can 279 // potentially be used to find new nodes in the network. 280 // 281 // This function is safe for concurrent access and is part of the 282 // rpcserverConnManager interface implementation. 283 func (cm *rpcConnManager) NodeAddresses() []*wire.NetAddress { 284 return cm.server.addrManager.AddressCache() 285 } 286 287 // rpcSyncMgr provides a block manager for use with the RPC server and 288 // implements the rpcserverSyncManager interface. 289 type rpcSyncMgr struct { 290 server *server 291 syncMgr *netsync.SyncManager 292 } 293 294 // Ensure rpcSyncMgr implements the rpcserverSyncManager interface. 295 var _ rpcserverSyncManager = (*rpcSyncMgr)(nil) 296 297 // IsCurrent returns whether or not the sync manager believes the chain is 298 // current as compared to the rest of the network. 299 // 300 // This function is safe for concurrent access and is part of the 301 // rpcserverSyncManager interface implementation. 302 func (b *rpcSyncMgr) IsCurrent() bool { 303 return b.syncMgr.IsCurrent() 304 } 305 306 // SubmitBlock submits the provided block to the network after processing it 307 // locally. 308 // 309 // This function is safe for concurrent access and is part of the 310 // rpcserverSyncManager interface implementation. 311 func (b *rpcSyncMgr) SubmitBlock(block *btcutil.Block, flags blockchain.BehaviorFlags) (bool, error) { 312 return b.syncMgr.ProcessBlock(block, flags) 313 } 314 315 // Pause pauses the sync manager until the returned channel is closed. 316 // 317 // This function is safe for concurrent access and is part of the 318 // rpcserverSyncManager interface implementation. 319 func (b *rpcSyncMgr) Pause() chan<- struct{} { 320 return b.syncMgr.Pause() 321 } 322 323 // SyncPeerID returns the peer that is currently the peer being used to sync 324 // from. 325 // 326 // This function is safe for concurrent access and is part of the 327 // rpcserverSyncManager interface implementation. 328 func (b *rpcSyncMgr) SyncPeerID() int32 { 329 return b.syncMgr.SyncPeerID() 330 } 331 332 // LocateBlocks returns the hashes of the blocks after the first known block in 333 // the provided locators until the provided stop hash or the current tip is 334 // reached, up to a max of wire.MaxBlockHeadersPerMsg hashes. 335 // 336 // This function is safe for concurrent access and is part of the 337 // rpcserverSyncManager interface implementation. 338 func (b *rpcSyncMgr) LocateHeaders(locators []*chainhash.Hash, hashStop *chainhash.Hash) []wire.BlockHeader { 339 return b.server.chain.LocateHeaders(locators, hashStop) 340 }