-
Notifications
You must be signed in to change notification settings - Fork 211
/
sync.go
279 lines (235 loc) · 7.09 KB
/
sync.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"cloud.google.com/go/storage"
"context"
"crypto/tls"
"fmt"
"github.com/spacemeshos/go-spacemesh/activation"
cmdp "github.com/spacemeshos/go-spacemesh/cmd"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/database"
"github.com/spacemeshos/go-spacemesh/filesystem"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/mesh"
"github.com/spacemeshos/go-spacemesh/miner"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/sync"
"github.com/spacemeshos/go-spacemesh/timesync"
"github.com/spf13/cobra"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"io/ioutil"
"net/http"
"os"
"time"
)
// Sync cmd
var Cmd = &cobra.Command{
Use: "sync",
Short: "start sync",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Starting sync")
syncApp := NewSyncApp()
log.Info("Right after NewSyncApp %v", syncApp.Config.DataDir)
defer syncApp.Cleanup()
syncApp.Initialize(cmd)
syncApp.Start(cmd, args)
},
}
//////////////////////////////
var expectedLayers int
var bucket string
var version string
var remote bool
func init() {
//path to remote storage
Cmd.PersistentFlags().StringVarP(&bucket, "storage-path", "z", "spacemesh-sync-data", "Specify storage bucket name")
//expected layers
Cmd.PersistentFlags().IntVar(&expectedLayers, "expected-layers", 101, "expected number of layers")
//fetch from remote
Cmd.PersistentFlags().BoolVar(&remote, "remote-data", false, "fetch from remote")
//request timeout
Cmd.PersistentFlags().StringVarP(&version, "version", "v", "FullBlocks/", "data version")
cmdp.AddCommands(Cmd)
}
type SyncApp struct {
*cmdp.BaseApp
sync *sync.Syncer
clock *timesync.Ticker
}
func NewSyncApp() *SyncApp {
return &SyncApp{BaseApp: cmdp.NewBaseApp()}
}
func (app *SyncApp) Cleanup() {
err := os.RemoveAll(app.Config.DataDir)
if err != nil {
app.sync.Error("failed to cleanup sync: %v", err)
}
}
type MockBlockBuilder struct {
txs []*types.Transaction
}
func (m *MockBlockBuilder) ValidateAndAddTxToPool(tx *types.Transaction) error {
m.txs = append(m.txs, tx)
return nil
}
func (app *SyncApp) Start(cmd *cobra.Command, args []string) {
// start p2p services
lg := log.New("sync_test", "", "")
lg.Info("------------ Start sync test -----------")
lg.Info("data folder: ", app.Config.DataDir)
lg.Info("storage path: ", bucket)
lg.Info("download from remote storage: ", remote)
lg.Info("expected layers: ", expectedLayers)
lg.Info("request timeout: ", app.Config.SyncRequestTimeout)
lg.Info("data version: ", version)
lg.Info("layers per epoch: ", app.Config.LayersPerEpoch)
lg.Info("hdist: ", app.Config.Hdist)
path := app.Config.DataDir + version
lg.Info("local db path: ", path)
swarm, err := p2p.New(cmdp.Ctx, app.Config.P2P, lg.WithName("p2p"), app.Config.DataDir)
if err != nil {
panic("something got fudged while creating p2p service ")
}
conf := sync.Configuration{
Concurrency: 4,
AtxsLimit: 200,
LayerSize: int(app.Config.LayerAvgSize),
RequestTimeout: time.Duration(app.Config.SyncRequestTimeout) * time.Millisecond,
SyncInterval: 2 * 60 * time.Millisecond,
Hdist: app.Config.Hdist,
}
if remote {
if err := GetData(app.Config.DataDir, version, lg); err != nil {
lg.Error("could not download data for test", err)
return
}
}
poetDbStore, err := database.NewLDBDatabase(path+"poet", 0, 0, lg.WithName("poetDbStore"))
if err != nil {
lg.Error("error: ", err)
return
}
poetDb := activation.NewPoetDb(poetDbStore, lg.WithName("poetDb").WithOptions(log.Nop))
mshdb, err := mesh.NewPersistentMeshDB(path, 5, lg.WithOptions(log.Nop))
if err != nil {
lg.Error("error: ", err)
return
}
atxdbStore, err := database.NewLDBDatabase(path+"atx", 0, 0, lg)
if err != nil {
lg.Error("error: ", err)
return
}
atxdb := activation.NewActivationDb(atxdbStore, &sync.MockIStore{}, mshdb, uint16(app.Config.LayersPerEpoch), &sync.ValidatorMock{}, lg.WithName("atxDB").WithOptions(log.Nop))
txpool := miner.NewTxMemPool()
atxpool := miner.NewAtxMemPool()
var msh *mesh.Mesh
if mshdb.PersistentData() {
lg.Info("persistent data found ")
msh = mesh.NewRecoveredMesh(mshdb, atxdb, sync.ConfigTst(), &sync.MeshValidatorMock{}, txpool, atxpool, &sync.MockState{}, lg)
} else {
lg.Info("no persistent data found ")
msh = mesh.NewMesh(mshdb, atxdb, sync.ConfigTst(), &sync.MeshValidatorMock{}, txpool, atxpool, &sync.MockState{}, lg)
}
msh.SetBlockBuilder(&MockBlockBuilder{})
defer msh.Close()
msh.AddBlock(mesh.GenesisBlock)
clock := sync.MockClock{}
clock.Layer = types.LayerID(expectedLayers + 1)
lg.Info("current layer %v", clock.GetCurrentLayer())
app.sync = sync.NewSync(swarm, msh, txpool, atxpool, sync.BlockEligibilityValidatorMock{}, poetDb, conf, &clock, lg.WithName("sync"))
if err = swarm.Start(); err != nil {
log.Panic("error starting p2p err=%v", err)
}
i := 0
for ; ; i++ {
if lyr, err2 := msh.GetLayer(types.LayerID(i)); err2 != nil || lyr == nil {
lg.Info("loaded %v layers from disk %v", i-1, err2)
break
} else {
lg.Info("loaded layer %v from disk ", i)
msh.ValidateLayer(lyr)
}
}
sleep := time.Duration(10) * time.Second
lg.Info("wait %v sec", sleep)
app.sync.Start()
for app.sync.ProcessedLayer() < types.LayerID(expectedLayers) {
app.sync.ForceSync()
lg.Info("sleep for %v sec", 30)
time.Sleep(30 * time.Second)
}
lg.Info("%v verified layers %v", app.BaseApp.Config.P2P.NodeID, app.sync.ProcessedLayer())
lg.Event().Info("sync done")
for {
lg.Info("keep busy sleep for %v sec", 60)
time.Sleep(60 * time.Second)
}
}
//download data from remote storage
func GetData(path, prefix string, lg log.Log) error {
dirs := []string{"poet", "atx", "nipst", "blocks", "ids", "layers", "transactions", "validity", "unappliedTxs"}
for _, dir := range dirs {
if err := filesystem.ExistOrCreate(path + prefix + "/" + dir); err != nil {
return err
}
}
c := http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 10,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS11,
InsecureSkipVerify: true,
},
},
Timeout: 2 * time.Second,
}
ctx := context.TODO()
client, err := storage.NewClient(ctx, option.WithoutAuthentication(), option.WithHTTPClient(&c))
if err != nil {
panic(err)
}
it := client.Bucket(bucket).Objects(ctx, &storage.Query{
Prefix: prefix,
})
count := 0
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
rc, err := client.Bucket(bucket).Object(attrs.Name).NewReader(ctx)
if err != nil {
return err
}
defer rc.Close()
data, err := ioutil.ReadAll(rc)
if err != nil {
return err
}
//skip main folder
if attrs.Name == version {
continue
}
lg.Info("downloading: %v to %v", attrs.Name, path+attrs.Name)
err = ioutil.WriteFile(path+attrs.Name, data, 0644)
if err != nil {
lg.Info("%v", err)
return err
}
count++
}
lg.Info("Done downloading: %v files", count)
return nil
}
func main() {
if err := Cmd.Execute(); err != nil {
log.Info("error ", err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}