Skip to content

Commit 5ed7991

Browse files
authored
Merge pull request #45 from cloudstruct/feature/txsubmission-protocol
Implement TxSubmission protocol (part 1)
2 parents 396dee8 + 616e072 commit 5ed7991

File tree

5 files changed

+363
-2
lines changed

5 files changed

+363
-2
lines changed

ouroboros.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/cloudstruct/go-ouroboros-network/protocol/keepalive"
1010
"github.com/cloudstruct/go-ouroboros-network/protocol/localstatequery"
1111
"github.com/cloudstruct/go-ouroboros-network/protocol/localtxsubmission"
12+
"github.com/cloudstruct/go-ouroboros-network/protocol/txsubmission"
1213
"net"
1314
)
1415

@@ -34,6 +35,8 @@ type Ouroboros struct {
3435
localTxSubmissionCallbackConfig *localtxsubmission.CallbackConfig
3536
LocalStateQuery *localstatequery.LocalStateQuery
3637
localStateQueryCallbackConfig *localstatequery.CallbackConfig
38+
TxSubmission *txsubmission.TxSubmission
39+
txSubmissionCallbackConfig *txsubmission.CallbackConfig
3740
}
3841

3942
type OuroborosOptions struct {
@@ -134,6 +137,7 @@ func (o *Ouroboros) setupConnection() error {
134137
protoOptions.Mode = protocol.ProtocolModeNodeToNode
135138
o.ChainSync = chainsync.New(protoOptions, o.chainSyncCallbackConfig)
136139
o.BlockFetch = blockfetch.New(protoOptions, o.blockFetchCallbackConfig)
140+
o.TxSubmission = txsubmission.New(protoOptions, o.txSubmissionCallbackConfig)
137141
if versionNtN.EnableKeepAliveProtocol {
138142
o.KeepAlive = keepalive.New(protoOptions, o.keepAliveCallbackConfig)
139143
if o.sendKeepAlives {

protocol/protocol.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ func (p *Protocol) getNewState(msg Message) (State, error) {
207207
matchFound := false
208208
for _, transition := range p.config.StateMap[p.state].Transitions {
209209
if transition.MsgType == msg.Type() {
210+
if transition.MatchFunc != nil {
211+
// Skip item if match function returns false
212+
if !transition.MatchFunc(msg) {
213+
continue
214+
}
215+
}
210216
newState = transition.NewState
211217
matchFound = true
212218
break

protocol/state.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ func (s State) String() string {
2323
}
2424

2525
type StateTransition struct {
26-
MsgType uint8
27-
NewState State
26+
MsgType uint8
27+
NewState State
28+
MatchFunc StateTransitionMatchFunc
2829
}
2930

31+
type StateTransitionMatchFunc func(Message) bool
32+
3033
type StateMapEntry struct {
3134
Agency uint
3235
Transitions []StateTransition

protocol/txsubmission/messages.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)