Skip to content

Commit 44b4b0b

Browse files
authored
Merge pull request #64 from cloudstruct/feat/check-node
feat: check node at start
2 parents bc181b0 + 5b38db6 commit 44b4b0b

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

internal/config/config.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55
"github.com/Bitrue-exchange/libada-go"
6+
ouroboros "github.com/cloudstruct/go-ouroboros-network"
67
"github.com/kelseyhightower/envconfig"
78
"gopkg.in/yaml.v2"
89
"io/ioutil"
@@ -121,8 +122,29 @@ func (c *Config) populateNetworkMagic() error {
121122
}
122123

123124
func (c *Config) checkNode() error {
125+
// Connect to cardano-node
126+
errorChan := make(chan error)
127+
oOpts := &ouroboros.OuroborosOptions{
128+
NetworkMagic: uint32(c.Node.NetworkMagic),
129+
ErrorChan: errorChan,
130+
UseNodeToNodeProtocol: false,
131+
}
132+
oConn, err := ouroboros.New(oOpts)
133+
defer func() {
134+
// We have to close the channel to break out of the async error handler goroutine
135+
close(errorChan)
136+
// Close Ouroboros connection
137+
oConn.Close()
138+
}()
139+
if err != nil {
140+
return fmt.Errorf("failure creating Ouroboros connection: %s", err)
141+
}
142+
124143
if c.Node.Address != "" && c.Node.Port > 0 {
125-
// TODO: add some validation for node address/port
144+
// Connect to TCP port
145+
if err := oConn.Dial("tcp", fmt.Sprintf("%s:%d", c.Node.Address, c.Node.Port)); err != nil {
146+
return fmt.Errorf("failure connecting to node via TCP: %s", err)
147+
}
126148
} else if c.Node.SocketPath != "" {
127149
// Check that node socket path exists
128150
if _, err := os.Stat(c.Node.SocketPath); err != nil {
@@ -132,6 +154,9 @@ func (c *Config) checkNode() error {
132154
return fmt.Errorf("unknown error checking if node socket path exists: %s", err)
133155
}
134156
}
157+
if err := oConn.Dial("unix", c.Node.SocketPath); err != nil {
158+
return fmt.Errorf("failure connecting to node via UNIX socket: %s", err)
159+
}
135160
} else {
136161
return fmt.Errorf("you must specify either the UNIX socket path or the address/port for your cardano-node")
137162
}

0 commit comments

Comments
 (0)