|
| 1 | +package txsubmission |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/cloudstruct/go-ouroboros-network/protocol" |
| 6 | + "github.com/cloudstruct/go-ouroboros-network/utils" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + MESSAGE_TYPE_REQUEST_TX_IDS = 0 |
| 11 | + MESSAGE_TYPE_REPLY_TX_IDS = 1 |
| 12 | + MESSAGE_TYPE_REQUEST_TXS = 2 |
| 13 | + MESSAGE_TYPE_REPLY_TXS = 3 |
| 14 | + MESSAGE_TYPE_DONE = 4 |
| 15 | + MESSAGE_TYPE_HELLO = 6 |
| 16 | +) |
| 17 | + |
| 18 | +func NewMsgFromCbor(msgType uint, data []byte) (protocol.Message, error) { |
| 19 | + var ret protocol.Message |
| 20 | + switch msgType { |
| 21 | + case MESSAGE_TYPE_REQUEST_TX_IDS: |
| 22 | + ret = &MsgRequestTxIds{} |
| 23 | + case MESSAGE_TYPE_REPLY_TX_IDS: |
| 24 | + ret = &MsgReplyTxIds{} |
| 25 | + case MESSAGE_TYPE_REQUEST_TXS: |
| 26 | + ret = &MsgRequestTxs{} |
| 27 | + case MESSAGE_TYPE_REPLY_TXS: |
| 28 | + ret = &MsgReplyTxs{} |
| 29 | + case MESSAGE_TYPE_DONE: |
| 30 | + ret = &MsgDone{} |
| 31 | + case MESSAGE_TYPE_HELLO: |
| 32 | + ret = &MsgHello{} |
| 33 | + } |
| 34 | + if _, err := utils.CborDecode(data, ret); err != nil { |
| 35 | + return nil, fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err) |
| 36 | + } |
| 37 | + if ret != nil { |
| 38 | + // Store the raw message CBOR |
| 39 | + ret.SetCbor(data) |
| 40 | + } |
| 41 | + return ret, nil |
| 42 | +} |
| 43 | + |
| 44 | +type MsgRequestTxIds struct { |
| 45 | + protocol.MessageBase |
| 46 | + Blocking bool |
| 47 | + Ack uint16 |
| 48 | + Req uint16 |
| 49 | +} |
| 50 | + |
| 51 | +func NewMsgRequestTxIds(blocking bool, ack uint16, req uint16) *MsgRequestTxIds { |
| 52 | + m := &MsgRequestTxIds{ |
| 53 | + MessageBase: protocol.MessageBase{ |
| 54 | + MessageType: MESSAGE_TYPE_REQUEST_TX_IDS, |
| 55 | + }, |
| 56 | + Blocking: blocking, |
| 57 | + Ack: ack, |
| 58 | + Req: req, |
| 59 | + } |
| 60 | + return m |
| 61 | +} |
| 62 | + |
| 63 | +type MsgReplyTxIds struct { |
| 64 | + protocol.MessageBase |
| 65 | + TxIds []txIdAndSize |
| 66 | +} |
| 67 | + |
| 68 | +func NewMsgReplyTxIds() *MsgReplyTxIds { |
| 69 | + m := &MsgReplyTxIds{ |
| 70 | + MessageBase: protocol.MessageBase{ |
| 71 | + MessageType: MESSAGE_TYPE_REPLY_TX_IDS, |
| 72 | + }, |
| 73 | + } |
| 74 | + return m |
| 75 | +} |
| 76 | + |
| 77 | +type MsgRequestTxs struct { |
| 78 | + protocol.MessageBase |
| 79 | + TxIds []txId |
| 80 | +} |
| 81 | + |
| 82 | +func NewMsgRequestTxs() *MsgRequestTxs { |
| 83 | + m := &MsgRequestTxs{ |
| 84 | + MessageBase: protocol.MessageBase{ |
| 85 | + MessageType: MESSAGE_TYPE_REQUEST_TXS, |
| 86 | + }, |
| 87 | + } |
| 88 | + return m |
| 89 | +} |
| 90 | + |
| 91 | +type MsgReplyTxs struct { |
| 92 | + protocol.MessageBase |
| 93 | + Txs []txBody |
| 94 | +} |
| 95 | + |
| 96 | +func NewMsgReplyTxs() *MsgReplyTxs { |
| 97 | + m := &MsgReplyTxs{ |
| 98 | + MessageBase: protocol.MessageBase{ |
| 99 | + MessageType: MESSAGE_TYPE_REPLY_TXS, |
| 100 | + }, |
| 101 | + } |
| 102 | + return m |
| 103 | +} |
| 104 | + |
| 105 | +type MsgDone struct { |
| 106 | + protocol.MessageBase |
| 107 | +} |
| 108 | + |
| 109 | +func NewMsgDone() *MsgDone { |
| 110 | + m := &MsgDone{ |
| 111 | + MessageBase: protocol.MessageBase{ |
| 112 | + MessageType: MESSAGE_TYPE_DONE, |
| 113 | + }, |
| 114 | + } |
| 115 | + return m |
| 116 | +} |
| 117 | + |
| 118 | +type MsgHello struct { |
| 119 | + protocol.MessageBase |
| 120 | +} |
| 121 | + |
| 122 | +func NewMsgHello() *MsgHello { |
| 123 | + m := &MsgHello{ |
| 124 | + MessageBase: protocol.MessageBase{ |
| 125 | + MessageType: MESSAGE_TYPE_HELLO, |
| 126 | + }, |
| 127 | + } |
| 128 | + return m |
| 129 | +} |
| 130 | + |
| 131 | +type txId struct { |
| 132 | + EraId uint16 |
| 133 | + TxId [32]byte |
| 134 | +} |
| 135 | + |
| 136 | +type txBody struct { |
| 137 | + EraId uint16 |
| 138 | + TxBody []byte |
| 139 | +} |
| 140 | + |
| 141 | +type txIdAndSize struct { |
| 142 | + TxId txId |
| 143 | + Size uint32 |
| 144 | +} |
0 commit comments