-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoneroger.go
134 lines (124 loc) · 3.53 KB
/
moneroger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Package moneroger provides high-level management of Monero services,
// coordinating both the Monero daemon (monerod) and wallet RPC service
// in a single unified interface.
package moneroger
import (
"context"
monerowalletrpc "github.com/opd-ai/moneroger/monero-wallet-rpc"
"github.com/opd-ai/moneroger/monerod"
"github.com/opd-ai/moneroger/util"
)
// Moneroger coordinates Monero daemon and wallet RPC services.
// It manages the lifecycle of both services ensuring proper startup
// and shutdown order.
//
// Fields:
// - monerod: The Monero daemon instance
// - monerowalletrpc: The wallet RPC service instance
//
// The Moneroger instance maintains references to both services
// and handles their coordination. It ensures the daemon is available
// before starting the wallet service, and handles graceful shutdown
// in the correct order.
type Moneroger struct {
monerod monerod.MoneroDaemon
monerowalletrpc monerowalletrpc.WalletRPC
}
// NewMoneroger creates a new instance managing both Monero services.
//
// Parameters:
// - config: Configuration settings for both services including:
// DataDir: Base directory for blockchain and wallet data
// WalletFile: Path to wallet file
// MoneroPort: Daemon RPC port
// WalletPort: Wallet RPC port
// TestNet: Network selection flag
//
// Returns:
// - *Moneroger: Configured manager instance
// - error: Any error during setup
//
// The function:
// 1. Starts the Monero daemon
// 2. Starts the wallet RPC service
// 3. Returns a manager coordinating both services
//
// Errors:
// - Daemon startup failures
// - Wallet service startup failures
// - Configuration validation errors
//
// Related:
// - monerod.NewMoneroDaemon
// - monerowalletrpc.NewWalletRPC
// - util.Config
func NewMoneroger(config util.Config) (*Moneroger, error) {
ctx := context.Background()
// Start Monero daemon
daemon, err := monerod.NewMoneroDaemon(ctx, config)
if err != nil {
return nil, err
}
// Start wallet RPC service
wallet, err := monerowalletrpc.NewWalletRPC(ctx, config, daemon)
if err != nil {
return nil, err
}
return &Moneroger{
monerod: *daemon,
monerowalletrpc: *wallet,
}, nil
}
// start initializes both Monero services in the correct order.
// This is an internal method used by NewMoneroger.
//
// Parameters:
// - ctx: Context for cancellation and timeout control
//
// Returns:
// - error: Any error during startup sequence
//
// The method:
// 1. Starts the Monero daemon
// 2. Waits for daemon availability
// 3. Starts the wallet RPC service
//
// Related:
// - MoneroDaemon.Start
// - WalletRPC.Start
func (m *Moneroger) Start(ctx context.Context) error {
if err := m.monerod.Start(ctx); err != nil {
return err
}
return m.monerowalletrpc.Start(ctx)
}
// Shutdown gracefully stops both Monero services in the correct order.
//
// Parameters:
// - ctx: Context for cancellation and timeout control
//
// Returns:
// - error: Any error during shutdown sequence
//
// The method:
// 1. Stops the wallet RPC service first
// 2. Stops the Monero daemon after
//
// This order ensures proper cleanup and prevents wallet
// errors due to daemon unavailability.
//
// Related:
// - WalletRPC.Shutdown
// - MoneroDaemon.Shutdown
func (m *Moneroger) Shutdown(ctx context.Context) error {
if err := m.monerowalletrpc.Shutdown(ctx); err != nil {
return err
}
return m.monerod.Shutdown(ctx)
}
func (m *Moneroger) MoneroDaemonPID() string {
return m.monerod.PID()
}
func (m *Moneroger) RPCWalletPID() string {
return m.monerowalletrpc.PID()
}