mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-14 10:06:40 +00:00

* [NOD-1191] Convert wire protocol to 100% protobuf * [NOD-1191] Simplify wire interface and remove redundant messages * [NOD-1191] Map all proto to wire conversions * [NOD-1203] Create netadapter outside of protocol manager * [NOD-1191] Fix nil errors * [NOD-1191] Fix comments * [NOD-1191] Add converter interface * [NOD-1191] Add missing GetBlockLocator message * [NOD-1191] Change message names that starts with 'get' to 'request' * [NOD-1191] Change message commands values * [NOD-1191] Remove redundant methods * [NOD-1191] Rename message constructors * [NOD-1191] Change message commands to use iota * [NOD-1191] Add missing outputs to protobuf conversion * [NOD-1191] Make block header a required field * [NOD-1191] Rename variables * [NOD-1212] Fix test names * [NOD-1191] Rename flow names * [NOD-1191] Fix infinite loop
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
package protowire
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/util/mstime"
|
|
"github.com/kaspanet/kaspad/wire"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (x *KaspadMessage_Block) toWireMessage() (wire.Message, error) {
|
|
return x.Block.toWireMessage()
|
|
}
|
|
|
|
func (x *KaspadMessage_Block) fromWireMessage(msgBlock *wire.MsgBlock) error {
|
|
x.Block = new(BlockMessage)
|
|
return x.Block.fromWireMessage(msgBlock)
|
|
}
|
|
|
|
func (x *BlockMessage) toWireMessage() (wire.Message, error) {
|
|
if len(x.Transactions) > wire.MaxTxPerBlock {
|
|
return nil, errors.Errorf("too many transactions to fit into a block "+
|
|
"[count %d, max %d]", len(x.Transactions), wire.MaxTxPerBlock)
|
|
}
|
|
|
|
protoBlockHeader := x.Header
|
|
if protoBlockHeader == nil {
|
|
return nil, errors.New("block header field cannot be nil")
|
|
}
|
|
|
|
parentHashes, err := protoHashesToWire(protoBlockHeader.ParentHashes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
hashMerkleRoot, err := protoBlockHeader.HashMerkleRoot.toWire()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
acceptedIDMerkleRoot, err := protoBlockHeader.AcceptedIDMerkleRoot.toWire()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
utxoCommitment, err := protoBlockHeader.UtxoCommitment.toWire()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
header := wire.BlockHeader{
|
|
Version: protoBlockHeader.Version,
|
|
ParentHashes: parentHashes,
|
|
HashMerkleRoot: hashMerkleRoot,
|
|
AcceptedIDMerkleRoot: acceptedIDMerkleRoot,
|
|
UTXOCommitment: utxoCommitment,
|
|
Timestamp: mstime.UnixMilliseconds(protoBlockHeader.Timestamp),
|
|
Bits: protoBlockHeader.Bits,
|
|
Nonce: protoBlockHeader.Nonce,
|
|
}
|
|
|
|
transactions := make([]*wire.MsgTx, len(x.Transactions))
|
|
for i, protoTx := range x.Transactions {
|
|
msgTx, err := protoTx.toWireMessage()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
transactions[i] = msgTx.(*wire.MsgTx)
|
|
}
|
|
|
|
return &wire.MsgBlock{
|
|
Header: header,
|
|
Transactions: transactions,
|
|
}, nil
|
|
}
|
|
|
|
func (x *BlockMessage) fromWireMessage(msgBlock *wire.MsgBlock) error {
|
|
if len(msgBlock.Transactions) > wire.MaxTxPerBlock {
|
|
return errors.Errorf("too many transactions to fit into a block "+
|
|
"[count %d, max %d]", len(msgBlock.Transactions), wire.MaxTxPerBlock)
|
|
}
|
|
|
|
header := msgBlock.Header
|
|
protoHeader := &BlockHeader{
|
|
Version: header.Version,
|
|
ParentHashes: wireHashesToProto(header.ParentHashes),
|
|
HashMerkleRoot: wireHashToProto(header.HashMerkleRoot),
|
|
AcceptedIDMerkleRoot: wireHashToProto(header.AcceptedIDMerkleRoot),
|
|
UtxoCommitment: wireHashToProto(header.UTXOCommitment),
|
|
Timestamp: header.Timestamp.UnixMilliseconds(),
|
|
Bits: header.Bits,
|
|
Nonce: header.Nonce,
|
|
}
|
|
protoTransactions := make([]*TransactionMessage, len(msgBlock.Transactions))
|
|
for i, tx := range msgBlock.Transactions {
|
|
protoTx := new(TransactionMessage)
|
|
protoTx.fromWireMessage(tx)
|
|
protoTransactions[i] = protoTx
|
|
}
|
|
*x = BlockMessage{
|
|
Header: protoHeader,
|
|
Transactions: protoTransactions,
|
|
}
|
|
return nil
|
|
}
|