Skip to content

Commit ef7c39d

Browse files
authored
Merge pull request #174 from cloudstruct/feat/more-functional-options
feat: functional options for mini-protocols
2 parents b5d11e6 + b02aad7 commit ef7c39d

File tree

12 files changed

+253
-20
lines changed

12 files changed

+253
-20
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ Test local-tx-submission (only works in node-to-client mode).
9090
$ ./go-ouroboros-network ... local-tx-submission ...
9191
```
9292

93+
Test following the chain tip in the `preview` network.
94+
95+
```
96+
$ ./go-ouroboros-network -preview -address preview-node.world.dev.cardano.org:30002 -ntn chain-sync -tip
97+
```
98+
9399
### Stopping the local `cardano-node` instance
94100

95101
```

cmd/go-ouroboros-network/chainsync.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ var eraIntersect = map[int]map[string][]interface{}{
7979
}
8080

8181
func buildChainSyncConfig() chainsync.Config {
82-
return chainsync.Config{
83-
RollBackwardFunc: chainSyncRollBackwardHandler,
84-
RollForwardFunc: chainSyncRollForwardHandler,
85-
}
82+
return chainsync.NewConfig(
83+
chainsync.WithRollBackwardFunc(chainSyncRollBackwardHandler),
84+
chainsync.WithRollForwardFunc(chainSyncRollForwardHandler),
85+
)
8686
}
8787

8888
func buildBlockFetchConfig() blockfetch.Config {
89-
return blockfetch.Config{
90-
StartBatchFunc: blockFetchStartBatchHandler,
91-
NoBlocksFunc: blockFetchNoBlocksHandler,
92-
BlockFunc: blockFetchBlockHandler,
93-
BatchDoneFunc: blockFetchBatchDoneHandler,
94-
}
89+
return blockfetch.NewConfig(
90+
blockfetch.WithStartBatchFunc(blockFetchStartBatchHandler),
91+
blockfetch.WithNoBlocksFunc(blockFetchNoBlocksHandler),
92+
blockfetch.WithBlockFunc(blockFetchBlockHandler),
93+
blockfetch.WithBatchDoneFunc(blockFetchBatchDoneHandler),
94+
)
9595
}
9696

9797
func testChainSync(f *globalFlags) {

cmd/go-ouroboros-network/localtxsubmission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func newLocalTxSubmissionFlags() *localTxSubmissionFlags {
2828
}
2929

3030
func buildLocalTxSubmissionConfig() localtxsubmission.Config {
31-
return localtxsubmission.Config{}
31+
return localtxsubmission.NewConfig()
3232
}
3333

3434
func testLocalTxSubmission(f *globalFlags) {

cmd/go-ouroboros-network/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func newQueryFlags() *queryFlags {
2020
}
2121

2222
func buildLocalStateQueryConfig() localstatequery.Config {
23-
return localstatequery.Config{}
23+
return localstatequery.NewConfig()
2424
}
2525

2626
func testQuery(f *globalFlags) {

ouroboros.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,18 @@ func (o *Ouroboros) setupConnection() error {
167167
// Perform handshake
168168
var handshakeVersion uint16
169169
var handshakeFullDuplex bool
170-
handshakeConfig := &handshake.Config{
171-
ProtocolVersions: protoVersions,
172-
NetworkMagic: o.networkMagic,
173-
ClientFullDuplex: o.fullDuplex,
174-
FinishedFunc: func(version uint16, fullDuplex bool) error {
170+
handshakeConfig := handshake.NewConfig(
171+
handshake.WithProtocolVersions(protoVersions),
172+
handshake.WithNetworkMagic(o.networkMagic),
173+
handshake.WithClientFullDuplex(o.fullDuplex),
174+
handshake.WithFinishedFunc(func(version uint16, fullDuplex bool) error {
175175
handshakeVersion = version
176176
handshakeFullDuplex = fullDuplex
177177
close(o.handshakeFinishedChan)
178178
return nil
179-
},
180-
}
181-
o.Handshake = handshake.New(protoOptions, handshakeConfig)
179+
}),
180+
)
181+
o.Handshake = handshake.New(protoOptions, &handshakeConfig)
182182
if o.server {
183183
o.Handshake.Server.Start()
184184
} else {

protocol/blockfetch/blockfetch.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,38 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *BlockFetch {
8686
}
8787
return b
8888
}
89+
90+
type BlockFetchOptionFunc func(*Config)
91+
92+
func NewConfig(options ...BlockFetchOptionFunc) Config {
93+
c := Config{}
94+
// Apply provided options functions
95+
for _, option := range options {
96+
option(&c)
97+
}
98+
return c
99+
}
100+
101+
func WithStartBatchFunc(startBatchFunc StartBatchFunc) BlockFetchOptionFunc {
102+
return func(c *Config) {
103+
c.StartBatchFunc = startBatchFunc
104+
}
105+
}
106+
107+
func WithNoBlocksFunc(noBlocksFunc NoBlocksFunc) BlockFetchOptionFunc {
108+
return func(c *Config) {
109+
c.NoBlocksFunc = noBlocksFunc
110+
}
111+
}
112+
113+
func WithBlockFunc(blockFunc BlockFunc) BlockFetchOptionFunc {
114+
return func(c *Config) {
115+
c.BlockFunc = blockFunc
116+
}
117+
}
118+
119+
func WithBatchDoneFunc(BatchDoneFunc BatchDoneFunc) BlockFetchOptionFunc {
120+
return func(c *Config) {
121+
c.BatchDoneFunc = BatchDoneFunc
122+
}
123+
}

protocol/chainsync/chainsync.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,26 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *ChainSync {
106106
}
107107
return c
108108
}
109+
110+
type ChainSyncOptionFunc func(*Config)
111+
112+
func NewConfig(options ...ChainSyncOptionFunc) Config {
113+
c := Config{}
114+
// Apply provided options functions
115+
for _, option := range options {
116+
option(&c)
117+
}
118+
return c
119+
}
120+
121+
func WithRollBackwardFunc(rollBackwardFunc RollBackwardFunc) ChainSyncOptionFunc {
122+
return func(c *Config) {
123+
c.RollBackwardFunc = rollBackwardFunc
124+
}
125+
}
126+
127+
func WithRollForwardFunc(rollForwardFunc RollForwardFunc) ChainSyncOptionFunc {
128+
return func(c *Config) {
129+
c.RollForwardFunc = rollForwardFunc
130+
}
131+
}

protocol/handshake/handshake.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,38 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *Handshake {
7070
}
7171
return h
7272
}
73+
74+
type HandshakeOptionFunc func(*Config)
75+
76+
func NewConfig(options ...HandshakeOptionFunc) Config {
77+
c := Config{}
78+
// Apply provided options functions
79+
for _, option := range options {
80+
option(&c)
81+
}
82+
return c
83+
}
84+
85+
func WithProtocolVersions(versions []uint16) HandshakeOptionFunc {
86+
return func(c *Config) {
87+
c.ProtocolVersions = versions
88+
}
89+
}
90+
91+
func WithNetworkMagic(networkMagic uint32) HandshakeOptionFunc {
92+
return func(c *Config) {
93+
c.NetworkMagic = networkMagic
94+
}
95+
}
96+
97+
func WithClientFullDuplex(fullDuplex bool) HandshakeOptionFunc {
98+
return func(c *Config) {
99+
c.ClientFullDuplex = fullDuplex
100+
}
101+
}
102+
103+
func WithFinishedFunc(finishedFunc FinishedFunc) HandshakeOptionFunc {
104+
return func(c *Config) {
105+
c.FinishedFunc = finishedFunc
106+
}
107+
}

protocol/keepalive/keepalive.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,32 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *KeepAlive {
6969
}
7070
return k
7171
}
72+
73+
type KeepAliveOptionFunc func(*Config)
74+
75+
func NewConfig(options ...KeepAliveOptionFunc) Config {
76+
c := Config{}
77+
// Apply provided options functions
78+
for _, option := range options {
79+
option(&c)
80+
}
81+
return c
82+
}
83+
84+
func WithKeepAliveFunc(keepAliveFunc KeepAliveFunc) KeepAliveOptionFunc {
85+
return func(c *Config) {
86+
c.KeepAliveFunc = keepAliveFunc
87+
}
88+
}
89+
90+
func WithKeepAliveResponseFunc(keepAliveResponseFunc KeepAliveResponseFunc) KeepAliveOptionFunc {
91+
return func(c *Config) {
92+
c.KeepAliveResponseFunc = keepAliveResponseFunc
93+
}
94+
}
95+
96+
func WithDoneFunc(doneFunc DoneFunc) KeepAliveOptionFunc {
97+
return func(c *Config) {
98+
c.DoneFunc = doneFunc
99+
}
100+
}

protocol/localstatequery/localstatequery.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,44 @@ func New(protoOptions protocol.ProtocolOptions, cfg *Config) *LocalStateQuery {
111111
}
112112
return l
113113
}
114+
115+
type LocalStateQueryOptionFunc func(*Config)
116+
117+
func NewConfig(options ...LocalStateQueryOptionFunc) Config {
118+
c := Config{}
119+
// Apply provided options functions
120+
for _, option := range options {
121+
option(&c)
122+
}
123+
return c
124+
}
125+
126+
func WithAcquireFunc(acquireFunc AcquireFunc) LocalStateQueryOptionFunc {
127+
return func(c *Config) {
128+
c.AcquireFunc = acquireFunc
129+
}
130+
}
131+
132+
func WithQueryFunc(queryFunc QueryFunc) LocalStateQueryOptionFunc {
133+
return func(c *Config) {
134+
c.QueryFunc = queryFunc
135+
}
136+
}
137+
138+
func WithReleaseFunc(releaseFunc ReleaseFunc) LocalStateQueryOptionFunc {
139+
return func(c *Config) {
140+
c.ReleaseFunc = releaseFunc
141+
}
142+
}
143+
144+
func WithReAcquireFunc(reAcquireFunc ReAcquireFunc) LocalStateQueryOptionFunc {
145+
return func(c *Config) {
146+
c.ReAcquireFunc = reAcquireFunc
147+
}
148+
}
149+
150+
func WithDoneFunc(doneFunc DoneFunc) LocalStateQueryOptionFunc {
151+
return func(c *Config) {
152+
c.DoneFunc = doneFunc
153+
}
154+
}

0 commit comments

Comments
 (0)