Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): add connection options - port/key/network #119

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions pkg/peerprotocol/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type Connection struct {
chiaConfig *config.ChiaConfig

networkID string
peerIP *net.IP
peerPort uint16
peerKeyPair *tls.Certificate
Expand All @@ -33,15 +34,8 @@ type PeerResponseHandlerFunc func(*protocols.Message, error)

// NewConnection creates a new connection object with the specified peer
func NewConnection(ip *net.IP, options ...ConnectionOptionFunc) (*Connection, error) {
cfg, err := config.GetChiaConfig()
if err != nil {
return nil, err
}

c := &Connection{
chiaConfig: cfg,
peerIP: ip,
peerPort: cfg.FullNode.Port,
peerIP: ip,
}

for _, fn := range options {
Expand All @@ -53,21 +47,50 @@ func NewConnection(ip *net.IP, options ...ConnectionOptionFunc) (*Connection, er
}
}

err = c.loadKeyPair()
if err != nil {
return nil, err
if c.peerPort == 0 {
if err := c.loadChiaConfig(); err != nil {
return nil, err
}
c.peerPort = c.chiaConfig.FullNode.Port
}

if c.peerKeyPair == nil {
if err := c.loadChiaConfig(); err != nil {
return nil, err
}
if err := c.loadConfigKeyPair(); err != nil {
return nil, err
}
}

if len(c.networkID) == 0 {
if err := c.loadChiaConfig(); err != nil {
return nil, err
}
c.networkID = c.chiaConfig.SelectedNetwork
}

// Generate the websocket dialer
err = c.generateDialer()
if err != nil {
if err := c.generateDialer(); err != nil {
return nil, err
}

return c, nil
}

func (c *Connection) loadKeyPair() error {
func (c *Connection) loadChiaConfig() error {
if c.chiaConfig != nil {
return nil
}
cfg, err := config.GetChiaConfig()
if err != nil {
return err
}
c.chiaConfig = cfg
return nil
}

func (c *Connection) loadConfigKeyPair() error {
var err error

c.peerKeyPair, err = c.chiaConfig.FullNode.SSL.LoadPublicKeyPair(c.chiaConfig.ChiaRoot)
Expand Down Expand Up @@ -122,7 +145,7 @@ func (c *Connection) Close() {
func (c *Connection) Handshake() error {
// Handshake
handshake := &protocols.Handshake{
NetworkID: c.chiaConfig.SelectedNetwork,
NetworkID: c.networkID,
ProtocolVersion: protocols.ProtocolVersion,
SoftwareVersion: "2.0.0",
ServerPort: c.peerPort,
Expand Down
25 changes: 25 additions & 0 deletions pkg/peerprotocol/connectionoptions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package peerprotocol

import (
"crypto/tls"
"time"
)

Expand All @@ -14,3 +15,27 @@ func WithHandshakeTimeout(timeout time.Duration) ConnectionOptionFunc {
return nil
}
}

// WithPeerPort sets the port for the peer
func WithPeerPort(port uint16) ConnectionOptionFunc {
return func(c *Connection) error {
c.peerPort = port
return nil
}
}

// WithPeerKeyPair sets the keypair for the peer
func WithPeerKeyPair(keypair tls.Certificate) ConnectionOptionFunc {
return func(c *Connection) error {
c.peerKeyPair = &keypair
return nil
}
}

// WithNetworkID sets the network ID for the peer
func WithNetworkID(networkID string) ConnectionOptionFunc {
return func(c *Connection) error {
c.networkID = networkID
return nil
}
}
1 change: 0 additions & 1 deletion pkg/protocols/fullnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ type RequestBlock struct {
type RespondBlock struct {
Block types.FullBlock `streamable:""`
}