Repository navigation
Expand file tree
/
Copy pathplugin.go
More file actions
196 lines (189 loc) · 6.04 KB
/
Copy pathplugin.go
File metadata and controls
196 lines (189 loc) · 6.04 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2026 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chainsync
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
"github.com/blinklabs-io/adder/internal/logging"
"github.com/blinklabs-io/adder/plugin"
ouroboros "github.com/blinklabs-io/gouroboros"
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
)
func init() {
plugin.Register(
plugin.PluginEntry{
Type: plugin.PluginTypeInput,
Name: "chainsync",
Description: "syncs blocks from a Cardano node using either NtC (node-to-client) or NtN (node-to-node)",
NewFromOptionsFunc: newFromOptions,
Options: []plugin.PluginOption{
{
Name: "kupo-url",
Type: plugin.PluginOptionTypeString,
DefaultValue: "",
CustomEnvVar: "KUPO_URL",
Description: "Kupo HTTP endpoint for resolving transaction inputs",
},
{
Name: "network",
Type: plugin.PluginOptionTypeString,
CustomEnvVar: "CARDANO_NETWORK",
Description: "specifies a well-known Cardano network name",
DefaultValue: "mainnet",
},
{
Name: "network-magic",
Type: plugin.PluginOptionTypeUint,
Description: "specifies the network magic value to use, overrides 'network'",
DefaultValue: uint(0),
},
{
Name: "address",
Type: plugin.PluginOptionTypeString,
Description: "specifies the TCP address of the node to connect to in the form 'host:port'",
DefaultValue: "",
},
{
Name: "socket-path",
Type: plugin.PluginOptionTypeString,
CustomEnvVar: "CARDANO_NODE_SOCKET_PATH",
Description: "specifies the path to the UNIX socket to connect to",
DefaultValue: "",
},
{
Name: "ntc-tcp",
Type: plugin.PluginOptionTypeBool,
Description: "use the NtC (node-to-client) protocol over TCP, for use when exposing a node's UNIX socket via socat or similar",
DefaultValue: false,
},
{
Name: "intersect-tip",
Type: plugin.PluginOptionTypeBool,
Description: "start syncing at the chain tip (defaults to chain genesis)",
DefaultValue: true,
},
{
Name: "intersect-point",
Type: plugin.PluginOptionTypeString,
Description: "start syncing at the specified chain point(s) in '<slot>.<hash>' format",
DefaultValue: "",
},
{
Name: "include-cbor",
Type: plugin.PluginOptionTypeBool,
Description: "include original CBOR for block/transaction in events",
DefaultValue: false,
},
{
Name: "auto-reconnect",
Type: plugin.PluginOptionTypeBool,
Description: "auto-reconnect if the connection is broken",
DefaultValue: true,
},
{
Name: "delay-confirmations",
Type: plugin.PluginOptionTypeUint,
Description: "number of confirmations required before emitting events",
DefaultValue: uint(0),
},
},
},
)
}
func newFromOptions(values plugin.Options) (plugin.ManagedPlugin, error) {
if endpoint := values.String("kupo-url"); endpoint != "" {
if err := plugin.ValidateHTTPURL(endpoint); err != nil {
return nil, fmt.Errorf("kupo-url: %w", err)
}
}
if values.String("network") != "" {
if _, ok := ouroboros.NetworkByName(values.String("network")); !ok {
return nil, errors.New("unknown network")
}
}
if values.String("address") != "" {
if err := plugin.ValidateHostPort(values.String("address")); err != nil {
return nil, fmt.Errorf("address: %w", err)
}
}
if values.String("network") == "" && values.String("address") == "" &&
values.String("socket-path") == "" {
return nil, errors.New("network, address, or socket-path is required")
}
//nolint:gosec // Options validates every uint as an unsigned 32-bit value.
nm := uint32(values.Uint("network-magic"))
opts := []ChainSyncOptionFunc{
WithLogger(
logging.GetLogger().With("plugin", "input.chainsync"),
),
WithNetwork(values.String("network")),
WithKupoUrl(values.String("kupo-url")),
WithNetworkMagic(nm),
WithAddress(values.String("address")),
WithSocketPath(values.String("socket-path")),
WithNtcTcp(values.Bool("ntc-tcp")),
WithIncludeCbor(values.Bool("include-cbor")),
WithAutoReconnect(values.Bool("auto-reconnect")),
WithDelayConfirmations(values.Uint("delay-confirmations")),
}
pointsSlice := plugin.SplitAndTrim(values.String("intersect-point"))
if len(pointsSlice) > 0 {
intersectPoints := make([]ocommon.Point, 0, len(pointsSlice))
for _, point := range pointsSlice {
intersectPointParts := strings.Split(point, ".")
if len(intersectPointParts) != 2 {
return nil, errors.New(
"invalid intersect point format: expected '<slot>.<hash>'",
)
}
intersectSlot, err := strconv.ParseUint(
intersectPointParts[0],
10,
64,
)
if err != nil {
return nil, errors.New(
"invalid intersect point format: slot must be a number",
)
}
intersectHashBytes, err := hex.DecodeString(intersectPointParts[1])
if err != nil || len(intersectHashBytes) != 32 {
return nil, errors.New(
"invalid intersect point format: hash must be 32 bytes of hex",
)
}
intersectPoints = append(
intersectPoints,
ocommon.Point{
Slot: intersectSlot,
Hash: intersectHashBytes[:],
},
)
}
opts = append(
opts,
WithIntersectPoints(intersectPoints),
)
} else {
opts = append(
opts,
WithIntersectTip(values.Bool("intersect-tip")),
)
}
p := New(opts...)
return p, nil
}