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
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package protowire
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/wire"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (x *KaspadMessage_Transaction) toWireMessage() (wire.Message, error) {
|
|
return x.Transaction.toWireMessage()
|
|
}
|
|
|
|
func (x *KaspadMessage_Transaction) fromWireMessage(msgTx *wire.MsgTx) error {
|
|
x.Transaction = new(TransactionMessage)
|
|
x.Transaction.fromWireMessage(msgTx)
|
|
return nil
|
|
}
|
|
|
|
func (x *TransactionMessage) toWireMessage() (wire.Message, error) {
|
|
inputs := make([]*wire.TxIn, len(x.Inputs))
|
|
for i, protoInput := range x.Inputs {
|
|
prevTxID, err := protoInput.PreviousOutpoint.TransactionID.toWire()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
outpoint := wire.NewOutpoint(prevTxID, protoInput.PreviousOutpoint.Index)
|
|
inputs[i] = wire.NewTxIn(outpoint, protoInput.SignatureScript)
|
|
}
|
|
|
|
outputs := make([]*wire.TxOut, len(x.Outputs))
|
|
for i, protoOutput := range x.Outputs {
|
|
outputs[i] = &wire.TxOut{
|
|
Value: protoOutput.Value,
|
|
ScriptPubKey: protoOutput.ScriptPubKey,
|
|
}
|
|
}
|
|
|
|
if x.SubnetworkID == nil {
|
|
return nil, errors.New("transaction subnetwork field cannot be nil")
|
|
}
|
|
|
|
subnetworkID, err := x.SubnetworkID.toWire()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
payloadHash, err := x.PayloadHash.toWire()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &wire.MsgTx{
|
|
Version: x.Version,
|
|
TxIn: inputs,
|
|
TxOut: outputs,
|
|
LockTime: x.LockTime,
|
|
SubnetworkID: *subnetworkID,
|
|
Gas: x.Gas,
|
|
PayloadHash: payloadHash,
|
|
Payload: x.Payload,
|
|
}, nil
|
|
}
|
|
|
|
func (x *TransactionMessage) fromWireMessage(msgTx *wire.MsgTx) {
|
|
protoInputs := make([]*TransactionInput, len(msgTx.TxIn))
|
|
for i, input := range msgTx.TxIn {
|
|
protoInputs[i] = &TransactionInput{
|
|
PreviousOutpoint: &Outpoint{
|
|
TransactionID: wireTransactionIDToProto(&input.PreviousOutpoint.TxID),
|
|
Index: input.PreviousOutpoint.Index,
|
|
},
|
|
SignatureScript: input.SignatureScript,
|
|
Sequence: input.Sequence,
|
|
}
|
|
}
|
|
|
|
protoOutputs := make([]*TransactionOutput, len(msgTx.TxOut))
|
|
for i, output := range msgTx.TxOut {
|
|
protoOutputs[i] = &TransactionOutput{
|
|
Value: output.Value,
|
|
ScriptPubKey: output.ScriptPubKey,
|
|
}
|
|
}
|
|
|
|
*x = TransactionMessage{
|
|
Version: msgTx.Version,
|
|
Inputs: protoInputs,
|
|
Outputs: protoOutputs,
|
|
LockTime: msgTx.LockTime,
|
|
SubnetworkID: wireSubnetworkIDToProto(&msgTx.SubnetworkID),
|
|
Gas: msgTx.Gas,
|
|
PayloadHash: wireHashToProto(msgTx.PayloadHash),
|
|
Payload: msgTx.Payload,
|
|
}
|
|
}
|