-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprivate_network.go
151 lines (124 loc) · 3.37 KB
/
private_network.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package privatebtc
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
"golang.org/x/exp/slices"
)
// NodeService is a service for creating Bitcoin Nodes.
type NodeService interface {
CreateNodes(
ctx context.Context,
nodeRequests []CreateNodeRequest,
) ([]NodeHandler, error)
}
// PrivateNetwork is a Bitcoin private network.
type PrivateNetwork struct {
logger *slog.Logger
nodeService NodeService
rpcClientFactory RPCClientFactory
nodes Nodes
nodeRequests []CreateNodeRequest
timeout *time.Duration
walletName *string
rpcUser string
rpcPassword string
}
// Default Bitcoin Core ports for regtest.
const (
RPCRegtestDefaultPort = "18443"
P2PRegtestDefaultPort = "18444"
)
// NewPrivateNetwork creates a new private network with the given number of nodes.
func NewPrivateNetwork(
nodeService NodeService,
rpcClientFactory RPCClientFactory,
nodes int,
opts ...Option,
) (*PrivateNetwork, error) {
options := defaultOptions()
for i := range opts {
opts[i].apply(options)
}
rpcAuth, err := newRPCAuth(options.rpcUser, options.rpcPass)
if err != nil {
return nil, fmt.Errorf("new rpc auth: %w", err)
}
nodeRequests := make([]CreateNodeRequest, nodes)
for i := range nodeRequests {
nodeRequests[i] = CreateNodeRequest{
RPCAuth: rpcAuth,
FallbackFee: options.fallbackFee,
}
}
return &PrivateNetwork{
logger: slog.New(options.handler),
nodeService: nodeService,
rpcClientFactory: rpcClientFactory,
nodes: nil,
nodeRequests: nodeRequests,
timeout: options.timeout,
walletName: options.walletName,
rpcUser: options.rpcUser,
rpcPassword: options.rpcPass,
}, nil
}
// Start creates the private network nodes and connects them.
func (n *PrivateNetwork) Start(ctx context.Context) error {
n.logger.Info("⌛ Creating nodes")
nodes, err := n.nodeService.CreateNodes(ctx, n.nodeRequests)
if err != nil {
return fmt.Errorf("create nodes: %w", err)
}
n.logger.Info("🐳✅ Successfully created nodes")
n.nodes = make([]Node, len(nodes))
for i, nodeHandler := range nodes {
rpcClient, err := n.rpcClientFactory.NewRPCClient(
nodeHandler.HostRPCPort(),
n.rpcUser,
n.rpcPassword,
)
if err != nil {
return fmt.Errorf("new rpc client: %w", err)
}
if n.walletName != nil {
if err := rpcClient.CreateWallet(ctx, *n.walletName); err != nil {
return fmt.Errorf("create wallet: %w", err)
}
}
n.nodes[i] = Node{
id: i,
name: fmt.Sprintf("Node %d", i),
rpcClient: rpcClient,
nodeHandler: nodeHandler,
pn: n,
}
}
n.logger.Info("🔗⌛ Connecting nodes")
if err := connectNodes(ctx, n.nodes); err != nil {
return fmt.Errorf("connect nodes: %w", err)
}
n.logger.Info("🔗✅ Successfully connected nodes")
return nil
}
// Nodes returns a copy of the nodes in the private network.
func (n *PrivateNetwork) Nodes() Nodes {
return slices.Clone(n.nodes)
}
// CreateNodeRequest is used to create a node.
type CreateNodeRequest struct {
RPCAuth string
FallbackFee float64
}
// Close terminates all nodes in the private network.
func (n *PrivateNetwork) Close() error {
var errs error
for i := range n.nodes {
if err := n.nodes[i].nodeHandler.Close(); err != nil {
errs = errors.Join(errs, fmt.Errorf("terminate node %d: %w", i, err))
}
}
return errs
}