Skip to content

Commit 06345cd

Browse files
committed
feat: functional options for protocol configs
Fixes #112
1 parent 49ba4a0 commit 06345cd

File tree

11 files changed

+230
-162
lines changed

11 files changed

+230
-162
lines changed

cmd/go-ouroboros-network/chainsync.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ var eraIntersect = map[int]map[string][]interface{}{
7676
},
7777
}
7878

79-
func buildChainSyncCallbackConfig() *chainsync.ChainSyncCallbackConfig {
80-
return &chainsync.ChainSyncCallbackConfig{
79+
func buildChainSyncConfig() chainsync.Config {
80+
return chainsync.Config{
8181
AwaitReplyFunc: chainSyncAwaitReplyHandler,
8282
RollBackwardFunc: chainSyncRollBackwardHandler,
8383
RollForwardFunc: chainSyncRollForwardHandler,
@@ -87,8 +87,8 @@ func buildChainSyncCallbackConfig() *chainsync.ChainSyncCallbackConfig {
8787
}
8888
}
8989

90-
func buildBlockFetchCallbackConfig() *blockfetch.BlockFetchCallbackConfig {
91-
return &blockfetch.BlockFetchCallbackConfig{
90+
func buildBlockFetchConfig() blockfetch.Config {
91+
return blockfetch.Config{
9292
StartBatchFunc: blockFetchStartBatchHandler,
9393
NoBlocksFunc: blockFetchNoBlocksHandler,
9494
BlockFunc: blockFetchBlockHandler,
@@ -124,13 +124,15 @@ func testChainSync(f *globalFlags) {
124124
ouroboros.WithErrorChan(errorChan),
125125
ouroboros.WithNodeToNode(f.ntnProto),
126126
ouroboros.WithKeepAlive(true),
127+
ouroboros.WithBlockFetchConfig(buildBlockFetchConfig()),
128+
ouroboros.WithChainSyncConfig(buildChainSyncConfig()),
127129
)
128130
if err != nil {
129131
fmt.Printf("ERROR: %s\n", err)
130132
os.Exit(1)
131133
}
132-
o.ChainSync.Start(buildChainSyncCallbackConfig())
133-
o.BlockFetch.Start(buildBlockFetchCallbackConfig())
134+
o.ChainSync.Start()
135+
o.BlockFetch.Start()
134136

135137
syncState.oConn = o
136138
syncState.readyForNextBlockChan = make(chan bool)

cmd/go-ouroboros-network/localtxsubmission.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func newLocalTxSubmissionFlags() *localTxSubmissionFlags {
3131
return f
3232
}
3333

34-
func buildLocalTxSubmissionCallbackConfig() *localtxsubmission.CallbackConfig {
35-
return &localtxsubmission.CallbackConfig{
34+
func buildLocalTxSubmissionConfig() localtxsubmission.Config {
35+
return localtxsubmission.Config{
3636
AcceptTxFunc: localTxSubmissionAcceptTxHandler,
3737
RejectTxFunc: localTxSubmissionRejectTxHandler,
3838
}
@@ -63,12 +63,13 @@ func testLocalTxSubmission(f *globalFlags) {
6363
ouroboros.WithErrorChan(errorChan),
6464
ouroboros.WithNodeToNode(f.ntnProto),
6565
ouroboros.WithKeepAlive(true),
66+
ouroboros.WithLocalTxSubmissionConfig(buildLocalTxSubmissionConfig()),
6667
)
6768
if err != nil {
6869
fmt.Printf("ERROR: %s\n", err)
6970
os.Exit(1)
7071
}
71-
o.LocalTxSubmission.Start(buildLocalTxSubmissionCallbackConfig())
72+
o.LocalTxSubmission.Start()
7273

7374
txData, err := ioutil.ReadFile(localTxSubmissionFlags.txFile)
7475
if err != nil {

options.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package ouroboros
22

33
import (
4+
"github.com/cloudstruct/go-ouroboros-network/protocol/blockfetch"
5+
"github.com/cloudstruct/go-ouroboros-network/protocol/chainsync"
6+
"github.com/cloudstruct/go-ouroboros-network/protocol/keepalive"
7+
"github.com/cloudstruct/go-ouroboros-network/protocol/localstatequery"
8+
"github.com/cloudstruct/go-ouroboros-network/protocol/localtxsubmission"
9+
"github.com/cloudstruct/go-ouroboros-network/protocol/txsubmission"
410
"net"
511
)
612

@@ -53,3 +59,39 @@ func WithFullDuplex(fullDuplex bool) OuroborosOptionFunc {
5359
o.fullDuplex = fullDuplex
5460
}
5561
}
62+
63+
func WithBlockFetchConfig(cfg blockfetch.Config) OuroborosOptionFunc {
64+
return func(o *Ouroboros) {
65+
o.blockFetchConfig = &cfg
66+
}
67+
}
68+
69+
func WithChainSyncConfig(cfg chainsync.Config) OuroborosOptionFunc {
70+
return func(o *Ouroboros) {
71+
o.chainSyncConfig = &cfg
72+
}
73+
}
74+
75+
func WithKeepAliveConfig(cfg keepalive.Config) OuroborosOptionFunc {
76+
return func(o *Ouroboros) {
77+
o.keepAliveConfig = &cfg
78+
}
79+
}
80+
81+
func WithLocalStateQueryConfig(cfg localstatequery.Config) OuroborosOptionFunc {
82+
return func(o *Ouroboros) {
83+
o.localStateQueryConfig = &cfg
84+
}
85+
}
86+
87+
func WithLocalTxSubmissionConfig(cfg localtxsubmission.Config) OuroborosOptionFunc {
88+
return func(o *Ouroboros) {
89+
o.localTxSubmissionConfig = &cfg
90+
}
91+
}
92+
93+
func WithTxSubmissionConfig(cfg txsubmission.Config) OuroborosOptionFunc {
94+
return func(o *Ouroboros) {
95+
o.txSubmissionConfig = &cfg
96+
}
97+
}

ouroboros.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ type Ouroboros struct {
2727
delayMuxerStart bool
2828
fullDuplex bool
2929
// Mini-protocols
30-
Handshake *handshake.Handshake
31-
ChainSync *chainsync.ChainSync
32-
BlockFetch *blockfetch.BlockFetch
33-
KeepAlive *keepalive.KeepAlive
34-
LocalTxSubmission *localtxsubmission.LocalTxSubmission
35-
LocalStateQuery *localstatequery.LocalStateQuery
36-
TxSubmission *txsubmission.TxSubmission
30+
Handshake *handshake.Handshake
31+
ChainSync *chainsync.ChainSync
32+
chainSyncConfig *chainsync.Config
33+
BlockFetch *blockfetch.BlockFetch
34+
blockFetchConfig *blockfetch.Config
35+
KeepAlive *keepalive.KeepAlive
36+
keepAliveConfig *keepalive.Config
37+
LocalTxSubmission *localtxsubmission.LocalTxSubmission
38+
localTxSubmissionConfig *localtxsubmission.Config
39+
LocalStateQuery *localstatequery.LocalStateQuery
40+
localStateQueryConfig *localstatequery.Config
41+
TxSubmission *txsubmission.TxSubmission
42+
txSubmissionConfig *txsubmission.Config
3743
}
3844

3945
func New(options ...OuroborosOptionFunc) (*Ouroboros, error) {
@@ -118,11 +124,16 @@ func (o *Ouroboros) setupConnection() error {
118124
protoOptions.Role = protocol.ProtocolRoleClient
119125
}
120126
// Perform handshake
121-
o.Handshake = handshake.New(protoOptions, protoVersions)
127+
handshakeConfig := &handshake.Config{
128+
ProtocolVersions: protoVersions,
129+
NetworkMagic: o.networkMagic,
130+
ClientFullDuplex: o.fullDuplex,
131+
}
132+
o.Handshake = handshake.New(protoOptions, handshakeConfig)
122133
o.Handshake.Start()
123134
// TODO: figure out better way to signify automatic handshaking and returning the chosen version
124135
if !o.server {
125-
err := o.Handshake.ProposeVersions(protoVersions, o.networkMagic, o.fullDuplex)
136+
err := o.Handshake.ProposeVersions()
126137
if err != nil {
127138
return err
128139
}
@@ -151,22 +162,22 @@ func (o *Ouroboros) setupConnection() error {
151162
if o.useNodeToNodeProto {
152163
versionNtN := GetProtocolVersionNtN(o.Handshake.Version)
153164
protoOptions.Mode = protocol.ProtocolModeNodeToNode
154-
o.ChainSync = chainsync.New(protoOptions)
155-
o.BlockFetch = blockfetch.New(protoOptions)
156-
o.TxSubmission = txsubmission.New(protoOptions)
165+
o.ChainSync = chainsync.New(protoOptions, o.chainSyncConfig)
166+
o.BlockFetch = blockfetch.New(protoOptions, o.blockFetchConfig)
167+
o.TxSubmission = txsubmission.New(protoOptions, o.txSubmissionConfig)
157168
if versionNtN.EnableKeepAliveProtocol {
158-
o.KeepAlive = keepalive.New(protoOptions)
169+
o.KeepAlive = keepalive.New(protoOptions, o.keepAliveConfig)
159170
if o.sendKeepAlives {
160-
o.KeepAlive.Start(nil)
171+
o.KeepAlive.Start()
161172
}
162173
}
163174
} else {
164175
versionNtC := GetProtocolVersionNtC(o.Handshake.Version)
165176
protoOptions.Mode = protocol.ProtocolModeNodeToClient
166-
o.ChainSync = chainsync.New(protoOptions)
167-
o.LocalTxSubmission = localtxsubmission.New(protoOptions)
177+
o.ChainSync = chainsync.New(protoOptions, o.chainSyncConfig)
178+
o.LocalTxSubmission = localtxsubmission.New(protoOptions, o.localTxSubmissionConfig)
168179
if versionNtC.EnableLocalQueryProtocol {
169-
o.LocalStateQuery = localstatequery.New(protoOptions)
180+
o.LocalStateQuery = localstatequery.New(protoOptions, o.localStateQueryConfig)
170181
}
171182
}
172183
// Start muxer

protocol/blockfetch/blockfetch.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,26 @@ var StateMap = protocol.StateMap{
6666

6767
type BlockFetch struct {
6868
*protocol.Protocol
69-
callbackConfig *BlockFetchCallbackConfig
69+
config *Config
7070
}
7171

72-
type BlockFetchCallbackConfig struct {
73-
StartBatchFunc BlockFetchStartBatchFunc
74-
NoBlocksFunc BlockFetchNoBlocksFunc
75-
BlockFunc BlockFetchBlockFunc
76-
BatchDoneFunc BlockFetchBatchDoneFunc
72+
type Config struct {
73+
StartBatchFunc StartBatchFunc
74+
NoBlocksFunc NoBlocksFunc
75+
BlockFunc BlockFunc
76+
BatchDoneFunc BatchDoneFunc
7777
}
7878

7979
// Callback function types
80-
type BlockFetchStartBatchFunc func() error
81-
type BlockFetchNoBlocksFunc func() error
82-
type BlockFetchBlockFunc func(uint, interface{}) error
83-
type BlockFetchBatchDoneFunc func() error
84-
85-
func New(options protocol.ProtocolOptions) *BlockFetch {
86-
b := &BlockFetch{}
80+
type StartBatchFunc func() error
81+
type NoBlocksFunc func() error
82+
type BlockFunc func(uint, interface{}) error
83+
type BatchDoneFunc func() error
84+
85+
func New(options protocol.ProtocolOptions, cfg *Config) *BlockFetch {
86+
b := &BlockFetch{
87+
config: cfg,
88+
}
8789
protoConfig := protocol.ProtocolConfig{
8890
Name: PROTOCOL_NAME,
8991
ProtocolId: PROTOCOL_ID,
@@ -100,8 +102,7 @@ func New(options protocol.ProtocolOptions) *BlockFetch {
100102
return b
101103
}
102104

103-
func (b *BlockFetch) Start(callbackConfig *BlockFetchCallbackConfig) {
104-
b.callbackConfig = callbackConfig
105+
func (b *BlockFetch) Start() {
105106
b.Protocol.Start()
106107
}
107108

@@ -133,23 +134,23 @@ func (b *BlockFetch) ClientDone() error {
133134
}
134135

135136
func (b *BlockFetch) handleStartBatch() error {
136-
if b.callbackConfig.StartBatchFunc == nil {
137+
if b.config.StartBatchFunc == nil {
137138
return fmt.Errorf("received block-fetch StartBatch message but no callback function is defined")
138139
}
139140
// Call the user callback function
140-
return b.callbackConfig.StartBatchFunc()
141+
return b.config.StartBatchFunc()
141142
}
142143

143144
func (b *BlockFetch) handleNoBlocks() error {
144-
if b.callbackConfig.NoBlocksFunc == nil {
145+
if b.config.NoBlocksFunc == nil {
145146
return fmt.Errorf("received block-fetch NoBlocks message but no callback function is defined")
146147
}
147148
// Call the user callback function
148-
return b.callbackConfig.NoBlocksFunc()
149+
return b.config.NoBlocksFunc()
149150
}
150151

151152
func (b *BlockFetch) handleBlock(msgGeneric protocol.Message) error {
152-
if b.callbackConfig.BlockFunc == nil {
153+
if b.config.BlockFunc == nil {
153154
return fmt.Errorf("received block-fetch Block message but no callback function is defined")
154155
}
155156
msg := msgGeneric.(*MsgBlock)
@@ -163,13 +164,13 @@ func (b *BlockFetch) handleBlock(msgGeneric protocol.Message) error {
163164
return err
164165
}
165166
// Call the user callback function
166-
return b.callbackConfig.BlockFunc(wrappedBlock.Type, blk)
167+
return b.config.BlockFunc(wrappedBlock.Type, blk)
167168
}
168169

169170
func (b *BlockFetch) handleBatchDone() error {
170-
if b.callbackConfig.BatchDoneFunc == nil {
171+
if b.config.BatchDoneFunc == nil {
171172
return fmt.Errorf("received block-fetch BatchDone message but no callback function is defined")
172173
}
173174
// Call the user callback function
174-
return b.callbackConfig.BatchDoneFunc()
175+
return b.config.BatchDoneFunc()
175176
}

0 commit comments

Comments
 (0)