Package com.privatechain.network.rpc
Class MessageCodec
java.lang.Object
com.privatechain.network.rpc.MessageCodec
Encodes and decodes wire-protocol messages exchanged between blockchain nodes.
MessageCodec is responsible for the serialization layer of the P2P
protocol. All messages are framed as UTF-8 JSON with a type discriminator
field. This makes the protocol human-readable and debuggable while meeting the
portability requirements of the private-blockchain library.
Message format
Every message follows a simple envelope:
{
"type": "BLOCK", // one of the MessageType enum values
"senderId": "nodeId...", // originating node ID
"payload": { ... } // type-specific JSON object
}
Supported message types
MessageCodec.MessageType.BLOCK— a newly mined or propagated blockMessageCodec.MessageType.TRANSACTION— a submitted transactionMessageCodec.MessageType.GET_STATUS— request for a peer's chain heightMessageCodec.MessageType.STATUS— response carrying a peer's chain heightMessageCodec.MessageType.GET_BLOCKS— request for blocks in a height rangeMessageCodec.MessageType.PING— heartbeat requestMessageCodec.MessageType.PONG— heartbeat response
Thread safety
Jackson's ObjectMapper is thread-safe after configuration; this class
is safe for concurrent use by multiple threads.
- Since:
- 1.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDiscriminator enum identifying the kind of P2P message.static final classImmutable envelope wrapping all P2P messages. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aMessageCodecwith a pre-configuredObjectMapper. -
Method Summary
Modifier and TypeMethodDescriptionblockMessage(String senderId, Block block) Creates aBLOCKmessage carrying the given block.decode(byte[] bytes) Decodes a UTF-8 JSON byte array into aMessageCodec.NetworkMessage.decodeFromString(String json) Decodes a JSON string into aMessageCodec.NetworkMessage.byte[]encode(MessageCodec.NetworkMessage message) Encodes aMessageCodec.NetworkMessageto a UTF-8 JSON byte array.Encodes aMessageCodec.NetworkMessageto a UTF-8 JSON string.extractBlock(MessageCodec.NetworkMessage message) Extracts and deserializes aBlockfrom the payload of aBLOCKmessage.intExtracts the chain height from the payload of aSTATUSmessage.Extracts and deserializes aTransactionfrom the payload of aTRANSACTIONmessage.getBlocksMessage(String senderId, int fromHeight, int toHeight) Creates aGET_BLOCKSmessage requesting blocks in a height range.getStatusMessage(String senderId) Creates aGET_STATUSmessage requesting a peer's current chain height.pingMessage(String senderId) Creates aPINGheartbeat request message.pongMessage(String senderId) Creates aPONGheartbeat response message.statusMessage(String senderId, int chainHeight) Creates aSTATUSmessage reporting this node's chain height.transactionMessage(String senderId, Transaction transaction) Creates aTRANSACTIONmessage carrying the given transaction.
-
Constructor Details
-
MessageCodec
public MessageCodec()Constructs aMessageCodecwith a pre-configuredObjectMapper.The mapper is configured to:
- Serialize
Instantvia theJavaTimeModule - Disable
SerializationFeature.WRITE_DATES_AS_TIMESTAMPSso timestamps appear as ISO-8601 strings
- Serialize
-
-
Method Details
-
encode
Encodes aMessageCodec.NetworkMessageto a UTF-8 JSON byte array.- Parameters:
message- the message to encode (non-null)- Returns:
- UTF-8 JSON bytes
- Throws:
IllegalArgumentException- if encoding failsNullPointerException- if message is null
-
encodeToString
Encodes aMessageCodec.NetworkMessageto a UTF-8 JSON string.- Parameters:
message- the message to encode (non-null)- Returns:
- JSON string
- Throws:
IllegalArgumentException- if encoding fails
-
decode
Decodes a UTF-8 JSON byte array into aMessageCodec.NetworkMessage.- Parameters:
bytes- the raw bytes to decode (non-null, non-empty)- Returns:
- decoded
MessageCodec.NetworkMessage - Throws:
IllegalArgumentException- if decoding fails or bytes are emptyNullPointerException- if bytes is null
-
decodeFromString
Decodes a JSON string into aMessageCodec.NetworkMessage.- Parameters:
json- the JSON string to decode (non-null, non-blank)- Returns:
- decoded
MessageCodec.NetworkMessage - Throws:
IllegalArgumentException- if decoding fails or JSON is blank
-
blockMessage
Creates aBLOCKmessage carrying the given block.- Parameters:
senderId- the originating node ID (non-null, non-blank)block- the block to propagate (non-null)- Returns:
- a
MessageCodec.NetworkMessageof typeMessageCodec.MessageType.BLOCK
-
transactionMessage
Creates aTRANSACTIONmessage carrying the given transaction.- Parameters:
senderId- the originating node ID (non-null, non-blank)transaction- the transaction to gossip (non-null)- Returns:
- a
MessageCodec.NetworkMessageof typeMessageCodec.MessageType.TRANSACTION
-
getStatusMessage
Creates aGET_STATUSmessage requesting a peer's current chain height.- Parameters:
senderId- the originating node ID (non-null, non-blank)- Returns:
- a
MessageCodec.NetworkMessageof typeMessageCodec.MessageType.GET_STATUS
-
statusMessage
Creates aSTATUSmessage reporting this node's chain height.- Parameters:
senderId- the originating node ID (non-null, non-blank)chainHeight- the current chain height- Returns:
- a
MessageCodec.NetworkMessageof typeMessageCodec.MessageType.STATUS
-
getBlocksMessage
Creates aGET_BLOCKSmessage requesting blocks in a height range.- Parameters:
senderId- the originating node IDfromHeight- inclusive start heighttoHeight- inclusive end height- Returns:
- a
MessageCodec.NetworkMessageof typeMessageCodec.MessageType.GET_BLOCKS
-
pingMessage
Creates aPINGheartbeat request message.- Parameters:
senderId- the originating node ID (non-null, non-blank)- Returns:
- a
MessageCodec.NetworkMessageof typeMessageCodec.MessageType.PING
-
pongMessage
Creates aPONGheartbeat response message.- Parameters:
senderId- the originating node ID (non-null, non-blank)- Returns:
- a
MessageCodec.NetworkMessageof typeMessageCodec.MessageType.PONG
-
extractBlock
Extracts and deserializes aBlockfrom the payload of aBLOCKmessage.- Parameters:
message- a message of typeMessageCodec.MessageType.BLOCK(non-null)- Returns:
- the deserialized block
- Throws:
IllegalArgumentException- if the message type is wrong or parsing fails
-
extractTransaction
Extracts and deserializes aTransactionfrom the payload of aTRANSACTIONmessage.- Parameters:
message- a message of typeMessageCodec.MessageType.TRANSACTION(non-null)- Returns:
- the deserialized transaction
- Throws:
IllegalArgumentException- if the message type is wrong or parsing fails
-
extractChainHeight
Extracts the chain height from the payload of aSTATUSmessage.- Parameters:
message- a message of typeMessageCodec.MessageType.STATUS(non-null)- Returns:
- the reported chain height
- Throws:
IllegalArgumentException- if parsing fails
-