Fix getPeerInfo for v0.20 (#2402)

This commit is contained in:
Ben Carman 2020-12-20 07:19:35 -06:00 committed by GitHub
parent d204056ad8
commit ba545760f9
6 changed files with 97 additions and 11 deletions

View File

@ -1,12 +1,11 @@
package org.bitcoins.commons.jsonmodels.bitcoind
import java.net.URI
import org.bitcoins.core.currency.Bitcoins
import org.bitcoins.core.number.{UInt32, UInt64}
import org.bitcoins.core.p2p.ServiceIdentifier
import org.bitcoins.core.wallet.fee.SatoshisPerKiloByte
import java.net.URI
import scala.concurrent.duration.FiniteDuration
sealed abstract class NetworkResult
@ -63,7 +62,24 @@ case class Network(
case class NetworkAddress(address: String, port: Int, score: Int)
extends NetworkResult
case class Peer(
sealed trait Peer extends NetworkResult {
def id: Int
def networkInfo: PeerNetworkInfo
def version: Int
def subver: String
def inbound: Boolean
def addnode: Boolean
def startingheight: Int
def synced_headers: Int
def synced_blocks: Int
def inflight: Vector[Int]
def whitelisted: Boolean
def bytessent_per_msg: Map[String, Int]
def bytesrecv_per_msg: Map[String, Int]
def minfeefilter: Option[SatoshisPerKiloByte]
}
case class PeerPreV20(
id: Int,
networkInfo: PeerNetworkInfo,
version: Int,
@ -79,7 +95,24 @@ case class Peer(
bytessent_per_msg: Map[String, Int],
bytesrecv_per_msg: Map[String, Int],
minfeefilter: Option[SatoshisPerKiloByte])
extends NetworkResult
extends Peer
case class PeerPostV20(
id: Int,
networkInfo: PeerNetworkInfo,
version: Int,
subver: String,
inbound: Boolean,
addnode: Boolean,
startingheight: Int,
synced_headers: Int,
synced_blocks: Int,
inflight: Vector[Int],
whitelisted: Boolean,
bytessent_per_msg: Map[String, Int],
bytesrecv_per_msg: Map[String, Int],
minfeefilter: Option[SatoshisPerKiloByte])
extends Peer
case class PeerNetworkInfo(
addr: URI,

View File

@ -199,7 +199,7 @@ object JsonSerializers {
implicit val peerNetworkInfoReads: Reads[PeerNetworkInfo] =
Json.reads[PeerNetworkInfo]
implicit val peerReads: Reads[Peer] = ((__ \ "id").read[Int] and
implicit val peerPreV20Reads: Reads[PeerPreV20] = ((__ \ "id").read[Int] and
__.read[PeerNetworkInfo] and
(__ \ "version").read[Int] and
(__ \ "subver").read[String] and
@ -213,7 +213,22 @@ object JsonSerializers {
(__ \ "whitelisted").read[Boolean] and
(__ \ "bytessent_per_msg").read[Map[String, Int]] and
(__ \ "bytesrecv_per_msg").read[Map[String, Int]] and
(__ \ "minfeefilter").readNullable[SatoshisPerKiloByte])(Peer)
(__ \ "minfeefilter").readNullable[SatoshisPerKiloByte])(PeerPreV20)
implicit val peerPostV20Reads: Reads[PeerPostV20] = ((__ \ "id").read[Int] and
__.read[PeerNetworkInfo] and
(__ \ "version").read[Int] and
(__ \ "subver").read[String] and
(__ \ "inbound").read[Boolean] and
(__ \ "addnode").read[Boolean] and
(__ \ "startingheight").read[Int] and
(__ \ "synced_headers").read[Int] and
(__ \ "synced_blocks").read[Int] and
(__ \ "inflight").read[Vector[Int]] and
(__ \ "whitelisted").read[Boolean] and
(__ \ "bytessent_per_msg").read[Map[String, Int]] and
(__ \ "bytesrecv_per_msg").read[Map[String, Int]] and
(__ \ "minfeefilter").readNullable[SatoshisPerKiloByte])(PeerPostV20)
implicit val nodeBanPostV20Reads: Reads[NodeBanPostV20] =
Json.reads[NodeBanPostV20]

View File

@ -1,10 +1,10 @@
package org.bitcoins.rpc.v18
import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.AddNodeArgument
import org.bitcoins.commons.jsonmodels.bitcoind.{
AddressInfoResultPostV18,
AddressInfoResultPreV18
}
import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.AddNodeArgument
import org.bitcoins.core.api.chain.db.BlockHeaderDbHelper
import org.bitcoins.core.protocol.blockchain.RegTestNetChainParams
import org.bitcoins.rpc.client.common.BitcoindVersion
@ -30,6 +30,18 @@ class BitcoindV18RpcClientTest extends BitcoindRpcTest {
behavior of "BitcoindV18RpcClient"
it should "be able to get peer info" in {
for {
(freshClient, otherFreshClient) <- clientPairF
infoList <- freshClient.getPeerInfo
} yield {
assert(infoList.length >= 0)
val info = infoList.head
assert(info.addnode)
assert(info.networkInfo.addr == otherFreshClient.getDaemon.uri)
}
}
it must "have our BitcoindRpcClient work with .hashCode() and equals" in {
for {
(client1, client2) <- clientPairF

View File

@ -35,7 +35,18 @@ class BitcoindV19RpcClientTest extends BitcoindRpcTest {
clientF.map { client =>
assert(client.version == BitcoindVersion.V19)
}
}
it should "be able to get peer info" in {
for {
(freshClient, otherFreshClient) <- clientPairF
infoList <- freshClient.getPeerInfo
} yield {
assert(infoList.length >= 0)
val info = infoList.head
assert(info.addnode)
assert(info.networkInfo.addr == otherFreshClient.getDaemon.uri)
}
}
it should "get a block filter given a block hash" in {

View File

@ -33,6 +33,18 @@ class BitcoindV20RpcClientTest extends BitcoindRpcTest {
}
}
it should "be able to get peer info" in {
for {
(freshClient, otherFreshClient) <- clientPairF
infoList <- freshClient.getPeerInfo
} yield {
assert(infoList.length >= 0)
val info = infoList.head
assert(info.addnode)
assert(info.networkInfo.addr == otherFreshClient.getDaemon.uri)
}
}
it should "get a block filter given a block hash" in {
for {
(client, _) <- clientPairF

View File

@ -1,7 +1,5 @@
package org.bitcoins.rpc.client.common
import java.net.URI
import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.{
AddNodeArgument,
SetBanCommand
@ -12,6 +10,7 @@ import org.bitcoins.core.protocol.blockchain.Block
import org.bitcoins.rpc.client.common.BitcoindVersion._
import play.api.libs.json.{JsBoolean, JsNumber, JsString}
import java.net.URI
import scala.concurrent.Future
/**
@ -62,7 +61,12 @@ trait P2PRpc { self: Client =>
}
def getPeerInfo: Future[Vector[Peer]] = {
bitcoindCall[Vector[Peer]]("getpeerinfo")
self.version match {
case V20 | Unknown =>
bitcoindCall[Vector[PeerPostV20]]("getpeerinfo")
case V16 | V17 | V18 | V19 | Experimental =>
bitcoindCall[Vector[PeerPreV20]]("getpeerinfo")
}
}
def listBanned: Future[Vector[NodeBan]] = {
@ -71,7 +75,6 @@ trait P2PRpc { self: Client =>
bitcoindCall[Vector[NodeBanPostV20]]("listbanned")
case V16 | V17 | V18 | V19 | Experimental =>
bitcoindCall[Vector[NodeBanPreV20]]("listbanned")
}
}