-
Notifications
You must be signed in to change notification settings - Fork 211
/
generator.go
262 lines (240 loc) · 6.48 KB
/
generator.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path/filepath"
"sort"
"time"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spf13/afero"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/spacemeshos/go-spacemesh/bootstrap"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
)
const (
SchemaVersion = "https://spacemesh.io/bootstrap.schema.json.1.0"
confirmation = 7
timeout = 5 * time.Second
)
func PersistedFilename(epoch types.EpochID, suffix string) string {
return filepath.Join(dataDir, bootstrap.UpdateName(epoch, suffix))
}
type Generator struct {
logger log.Log
fs afero.Fs
client *http.Client
btcEndpoint string
smEndpoint string
}
type Opt func(*Generator)
func WithLogger(logger log.Log) Opt {
return func(g *Generator) {
g.logger = logger
}
}
func WithFilesystem(fs afero.Fs) Opt {
return func(g *Generator) {
g.fs = fs
}
}
func WithHttpClient(c *http.Client) Opt {
return func(g *Generator) {
g.client = c
}
}
func NewGenerator(btcEndpoint, smEndpoint string, opts ...Opt) *Generator {
g := &Generator{
logger: log.NewNop(),
fs: afero.NewOsFs(),
client: &http.Client{},
btcEndpoint: btcEndpoint,
smEndpoint: smEndpoint,
}
for _, opt := range opts {
opt(g)
}
return g
}
func (g *Generator) SmEndpoint() string {
return g.smEndpoint
}
func (g *Generator) Generate(
ctx context.Context,
targetEpoch types.EpochID,
genBeacon, genActiveSet bool,
) (string, error) {
if err := g.fs.MkdirAll(dataDir, 0o700); err != nil {
return "", fmt.Errorf("create persist dir %v: %w", dataDir, err)
}
var (
suffix string
beacon types.Beacon
activeSet []types.ATXID
err error
)
if genBeacon && genActiveSet {
suffix = bootstrap.SuffixBootstrap
} else if genBeacon {
suffix = bootstrap.SuffixBeacon
} else if genActiveSet {
suffix = bootstrap.SuffixActiveSet
} else {
g.logger.Fatal("nothing to do")
}
if genBeacon {
beacon, err = g.genBeacon(ctx, g.logger)
if err != nil {
return "", err
}
}
if genActiveSet {
activeSet, err = getActiveSet(ctx, g.smEndpoint, targetEpoch-1)
if err != nil {
return "", err
}
}
return g.GenUpdate(targetEpoch, beacon, activeSet, suffix)
}
// BitcoinResponse captures the only fields we care about from a bitcoin block.
type BitcoinResponse struct {
Height uint64 `json:"height"`
Hash string `json:"hash"`
}
func (g *Generator) genBeacon(ctx context.Context, logger log.Log) (types.Beacon, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
br, err := bitcoinHash(ctx, logger, g.client, g.btcEndpoint)
if err != nil {
return types.EmptyBeacon, err
}
decoded, err := hex.DecodeString(br.Hash)
if err != nil {
return types.EmptyBeacon, fmt.Errorf("decode bitcoin hash: %w", err)
}
// bitcoin hash started with leading zero. we want to grab 4 LSB
offset := len(decoded) - types.BeaconSize
beacon := types.BytesToBeacon(decoded[offset:])
return beacon, nil
}
func bitcoinHash(ctx context.Context, logger log.Log, client *http.Client, targetUrl string) (*BitcoinResponse, error) {
latest, err := queryBitcoin(ctx, client, targetUrl)
if err != nil {
return nil, err
}
logger.With().Info("latest bitcoin block height",
log.Uint64("height", latest.Height),
log.String("hash", latest.Hash),
)
height := latest.Height - confirmation
blockUrl := fmt.Sprintf("%s/blocks/%d", targetUrl, height)
confirmed, err := queryBitcoin(ctx, client, blockUrl)
if err != nil {
return nil, err
}
logger.With().Info("confirmed bitcoin block",
log.Uint64("height", confirmed.Height),
log.String("hash", confirmed.Hash),
)
return confirmed, nil
}
func queryBitcoin(ctx context.Context, client *http.Client, targetUrl string) (*BitcoinResponse, error) {
resource, err := url.Parse(targetUrl)
if err != nil {
return nil, fmt.Errorf("parse btc url: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, resource.String(), nil)
if err != nil {
return nil, fmt.Errorf("create http request: %w", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("http get latest bitcoin block: %w", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("bootstrap read response: %w", err)
}
var br BitcoinResponse
err = json.Unmarshal(data, &br)
if err != nil {
return nil, fmt.Errorf("unmarshal bitcoin response: %w", err)
}
return &br, nil
}
func getActiveSet(ctx context.Context, endpoint string, epoch types.EpochID) ([]types.ATXID, error) {
conn, err := grpc.NewClient(
endpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, fmt.Errorf("grpc dial %v: %w", endpoint, err)
}
defer conn.Close()
client := pb.NewMeshServiceClient(conn)
stream, err := client.EpochStream(ctx, &pb.EpochStreamRequest{Epoch: uint32(epoch)})
if err != nil {
return nil, fmt.Errorf("epoch stream %v: %w", endpoint, err)
}
activeSet := make([]types.ATXID, 0, 300_000)
for {
resp, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
activeSet = append(activeSet, types.ATXID(types.BytesToHash(resp.GetId().GetId())))
}
sort.Slice(activeSet, func(i, j int) bool {
return bytes.Compare(activeSet[i].Bytes(), activeSet[j].Bytes()) < 0
})
return activeSet, nil
}
func (g *Generator) GenUpdate(
epoch types.EpochID,
beacon types.Beacon,
activeSet []types.ATXID,
suffix string,
) (string, error) {
as := make([]string, 0, len(activeSet))
for _, atx := range activeSet {
as = append(as, hex.EncodeToString(atx.Bytes())) // no leading 0x
}
var update bootstrap.Update
update.Version = SchemaVersion
edata := bootstrap.EpochData{
ID: uint32(epoch),
Beacon: hex.EncodeToString(beacon.Bytes()), // no leading 0x
}
if len(activeSet) > 0 {
edata.ActiveSet = as
}
update.Data = bootstrap.InnerData{
Epoch: edata,
}
data, err := json.Marshal(update)
if err != nil {
return "", fmt.Errorf("marshal data: %w", err)
}
// make sure the data is valid
if err = bootstrap.ValidateSchema(data); err != nil {
return "", fmt.Errorf("invalid data: %w", err)
}
filename := PersistedFilename(epoch, suffix)
err = afero.WriteFile(g.fs, filename, data, 0o600)
if err != nil {
return "", fmt.Errorf("persist epoch update %v: %w", filename, err)
}
g.logger.With().Info("generated update",
log.String("filename", filename),
)
return filename, nil
}