mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-19 14:43:57 +01:00
parent
c27e1136e2
commit
780ac75bf0
@ -10,8 +10,11 @@ import scodec.bits.ByteVector
|
||||
* Represents a P2P network message
|
||||
*/
|
||||
sealed abstract class NetworkMessage extends NetworkElement {
|
||||
require(header.payloadSize.toInt == payload.bytes.length, s"Payload size is not what header says it is, " +
|
||||
s"header.payloadSize=${header.payloadSize.toInt} actual=${payload.bytes.length}")
|
||||
require(
|
||||
header.payloadSize.toInt == payload.bytes.length,
|
||||
s"Payload size is not what header says it is, " +
|
||||
s"header.payloadSize=${header.payloadSize.toInt} actual=${payload.bytes.length}"
|
||||
)
|
||||
def header: NetworkHeader
|
||||
def payload: NetworkPayload
|
||||
override def bytes: ByteVector = RawNetworkMessageSerializer.write(this)
|
||||
|
@ -8,13 +8,14 @@ trait RawNetworkMessageSerializer extends RawBitcoinSerializer[NetworkMessage] {
|
||||
|
||||
def read(bytes: ByteVector): NetworkMessage = {
|
||||
//first 24 bytes are the header
|
||||
val (headerBytes,payloadBytes) = bytes.splitAt(24)
|
||||
val (headerBytes, payloadBytes) = bytes.splitAt(24)
|
||||
val header = NetworkHeader.fromBytes(headerBytes)
|
||||
if (header.payloadSize.toInt > payloadBytes.length) {
|
||||
throw new RuntimeException(s"We do not have enough bytes for payload! Expected=${header.payloadSize.toInt} got=${payloadBytes.length}")
|
||||
throw new RuntimeException(
|
||||
s"We do not have enough bytes for payload! Expected=${header.payloadSize.toInt} got=${payloadBytes.length}")
|
||||
} else {
|
||||
val payload = NetworkPayload(header, payloadBytes)
|
||||
val n = NetworkMessage(header, payload)
|
||||
val n = NetworkMessage(header, payload)
|
||||
n
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,9 @@ trait RawNetworkHeaderSerializer
|
||||
* @return the native object for the MessageHeader
|
||||
*/
|
||||
def read(bytes: ByteVector): NetworkHeader = {
|
||||
require(bytes.length == 24, s"Got bytes.length=${bytes.length} when NetworkHeader expects 24 bytes")
|
||||
require(
|
||||
bytes.length == 24,
|
||||
s"Got bytes.length=${bytes.length} when NetworkHeader expects 24 bytes")
|
||||
val network = Networks.magicToNetwork(bytes.take(4))
|
||||
//.trim removes the null characters appended to the command name
|
||||
val commandName = bytes.slice(4, 16).toArray.map(_.toChar).mkString.trim
|
||||
|
@ -377,9 +377,8 @@ object P2PClient extends P2PLogger {
|
||||
val messageTry = Try(NetworkMessage(remainingBytes))
|
||||
messageTry match {
|
||||
case Success(message) =>
|
||||
val newRemainingBytes = remainingBytes.slice(
|
||||
message.bytes.length,
|
||||
remainingBytes.length)
|
||||
val newRemainingBytes =
|
||||
remainingBytes.slice(message.bytes.length, remainingBytes.length)
|
||||
logger.trace(
|
||||
s"Parsed a message=${message.header.commandName} from bytes, continuing with remainingBytes=${newRemainingBytes.length}")
|
||||
loop(newRemainingBytes, message :: accum)
|
||||
|
Loading…
Reference in New Issue
Block a user