@@ -46,6 +46,8 @@ type Node struct {
4646 ouroboros * ouroborosPkg.Ouroboros
4747 shutdownFuncs []func (context.Context ) error
4848 config Config
49+ ctx context.Context
50+ cancel context.CancelFunc
4951 done chan struct {}
5052 shutdownOnce sync.Once
5153}
@@ -73,6 +75,7 @@ func (n *Node) Run() error {
7375 return err
7476 }
7577 }
78+ n .ctx , n .cancel = context .WithCancel (context .Background ())
7679 // Load database
7780 dbNeedsRecovery := false
7881 dbConfig := & database.Config {
@@ -149,9 +152,12 @@ func (n *Node) Run() error {
149152 }
150153 }
151154 // Start ledger
152- if err := n .ledgerState .Start (); err != nil {
155+ if err := n .ledgerState .Start (n . ctx ); err != nil {
153156 return fmt .Errorf ("failed to start ledger: %w" , err )
154157 }
158+ n .shutdownFuncs = append (n .shutdownFuncs , func (ctx context.Context ) error {
159+ return n .ledgerState .Close ()
160+ })
155161 // Initialize mempool
156162 n .mempool = mempool .NewMempool (mempool.MempoolConfig {
157163 MempoolCapacity : n .config .mempoolCapacity ,
@@ -192,6 +198,9 @@ func (n *Node) Run() error {
192198 if err := n .connManager .Start (); err != nil {
193199 return err
194200 }
201+ n .shutdownFuncs = append (n .shutdownFuncs , func (ctx context.Context ) error {
202+ return n .connManager .Stop (ctx )
203+ })
195204 // Configure peer governor
196205 n .peerGov = peergov .NewPeerGovernor (
197206 peergov.PeerGovernorConfig {
@@ -214,6 +223,10 @@ func (n *Node) Run() error {
214223 if err := n .peerGov .Start (); err != nil {
215224 return err
216225 }
226+ n .shutdownFuncs = append (n .shutdownFuncs , func (ctx context.Context ) error {
227+ n .peerGov .Stop ()
228+ return nil
229+ })
217230 // Configure UTxO RPC
218231 n .utxorpc = utxorpc .NewUtxorpc (
219232 utxorpc.UtxorpcConfig {
@@ -227,9 +240,16 @@ func (n *Node) Run() error {
227240 if err := n .utxorpc .Start (); err != nil {
228241 return err
229242 }
243+ n .shutdownFuncs = append (n .shutdownFuncs , func (ctx context.Context ) error {
244+ return n .utxorpc .Stop (ctx )
245+ })
230246
231247 // Wait for shutdown signal
232- <- n .done
248+ if n .ctx != nil {
249+ <- n .ctx .Done ()
250+ } else {
251+ <- n .done
252+ }
233253 return nil
234254}
235255
@@ -249,6 +269,9 @@ func (n *Node) shutdown() error {
249269 }
250270 ctx , cancel := context .WithTimeout (context .Background (), shutdownTimeout )
251271 defer cancel ()
272+ if n .cancel != nil {
273+ n .cancel ()
274+ }
252275
253276 var err error
254277
0 commit comments