Skip to content

Commit

Permalink
Merge pull request #281 from blinklabs-io/feat/protocol-auto-start
Browse files Browse the repository at this point in the history
feat: auto-start protocols with option to disable
  • Loading branch information
agaffney authored May 23, 2023
2 parents 9c83d3a + 2c01208 commit a878eeb
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 15 deletions.
3 changes: 1 addition & 2 deletions cmd/block-fetch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"os"

"github.com/blinklabs-io/gouroboros"
ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/cmd/common"
"github.com/blinklabs-io/gouroboros/ledger"
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
Expand Down Expand Up @@ -60,7 +60,6 @@ func main() {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
o.BlockFetch().Client.Start()

blockHash, err := hex.DecodeString(f.hash)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions cmd/gouroboros/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ func testChainSync(f *globalFlags) {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
oConn.ChainSync().Client.Start()
if f.ntnProto {
oConn.BlockFetch().Client.Start()
}

var point common.Point
if chainSyncFlags.tip {
Expand Down
6 changes: 3 additions & 3 deletions cmd/gouroboros/localtxsubmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"

ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/gouroboros/protocol/localtxsubmission"
"io/ioutil"
"os"
)

type localTxSubmissionFlags struct {
Expand Down Expand Up @@ -78,7 +79,6 @@ func testLocalTxSubmission(f *globalFlags) {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
o.LocalTxSubmission().Client.Start()

var txBytes []byte
if localTxSubmissionFlags.txFile != "" {
Expand Down
1 change: 0 additions & 1 deletion cmd/gouroboros/mem_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func testMemUsage(f *globalFlags) {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
o.ChainSync().Client.Start()

tip, err := o.ChainSync().Client.GetCurrentTip()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/gouroboros/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package main
import (
"flag"
"fmt"
"os"

ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/protocol/localstatequery"
"os"
)

type queryFlags struct {
Expand Down Expand Up @@ -70,7 +71,6 @@ func testQuery(f *globalFlags) {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
o.LocalStateQuery().Client.Start()

switch queryFlags.flagset.Args()[0] {
case "current-era":
Expand Down
1 change: 0 additions & 1 deletion cmd/peer-sharing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func main() {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
o.PeerSharing().Client.Start()

peers, err := o.PeerSharing().Client.GetPeers(10)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/tx-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"os"

"github.com/blinklabs-io/gouroboros"
ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/cmd/common"

Expand Down Expand Up @@ -51,7 +51,6 @@ func main() {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
o.LocalTxMonitor().Client.Start()

capacity, size, numberOfTxs, err := o.LocalTxMonitor().Client.GetSizes()
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Connection struct {
onceClose sync.Once
sendKeepAlives bool
delayMuxerStart bool
delayProtocolStart bool
fullDuplex bool
// Mini-protocols
blockFetch *blockfetch.BlockFetch
Expand Down Expand Up @@ -331,6 +332,25 @@ func (c *Connection) setupConnection() error {
if versionNtN.EnablePeerSharingProtocol {
c.peerSharing = peersharing.New(protoOptions, c.peerSharingConfig)
}
// Start protocols
if !c.delayProtocolStart {
if handshakeFullDuplex || !c.server {
c.blockFetch.Client.Start()
c.chainSync.Client.Start()
c.txSubmission.Client.Start()
if c.peerSharing != nil {
c.peerSharing.Client.Start()
}
}
if handshakeFullDuplex || c.server {
c.blockFetch.Server.Start()
c.chainSync.Server.Start()
c.txSubmission.Server.Start()
if c.peerSharing != nil {
c.peerSharing.Server.Start()
}
}
}
} else {
versionNtC := GetProtocolVersionNtC(handshakeVersion)
protoOptions.Mode = protocol.ProtocolModeNodeToClient
Expand All @@ -342,6 +362,29 @@ func (c *Connection) setupConnection() error {
if versionNtC.EnableLocalTxMonitorProtocol {
c.localTxMonitor = localtxmonitor.New(protoOptions, c.localTxMonitorConfig)
}
// Start protocols
if !c.delayProtocolStart {
if handshakeFullDuplex || !c.server {
c.chainSync.Client.Start()
c.localTxSubmission.Client.Start()
if c.localStateQuery != nil {
c.localStateQuery.Client.Start()
}
if c.localTxMonitor != nil {
c.localTxMonitor.Client.Start()
}
}
if handshakeFullDuplex || c.server {
c.chainSync.Server.Start()
c.localTxSubmission.Server.Start()
if c.localStateQuery != nil {
c.localStateQuery.Server.Start()
}
if c.localTxMonitor != nil {
c.localTxMonitor.Server.Start()
}
}
}
}
// Start muxer
diffusionMode := muxer.DiffusionModeInitiator
Expand Down
9 changes: 9 additions & 0 deletions connection_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ func WithDelayMuxerStart(delayMuxerStart bool) ConnectionOptionFunc {
}
}

// WithDelayProtocolStart specifies whether to delay the start of the relevant mini-protocols. This is useful
// if you are maintaining lots of connections and want to reduce resource overhead by only starting particular
// protocols
func WithDelayProtocolStart(delayProtocolStart bool) ConnectionOptionFunc {
return func(c *Connection) {
c.delayProtocolStart = delayProtocolStart
}
}

// WithFullDuplex specifies whether to enable full-duplex mode when acting as a client
func WithFullDuplex(fullDuplex bool) ConnectionOptionFunc {
return func(c *Connection) {
Expand Down

0 comments on commit a878eeb

Please sign in to comment.