-
Notifications
You must be signed in to change notification settings - Fork 12
/
engines.go
278 lines (230 loc) · 10 KB
/
engines.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
package protocol
import (
"os"
"path/filepath"
"github.com/google/uuid"
"github.com/iotaledger/hive.go/ds/reactive"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/ioutils"
"github.com/iotaledger/hive.go/runtime/module"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/runtime/workerpool"
"github.com/iotaledger/iota-core/pkg/protocol/engine"
"github.com/iotaledger/iota-core/pkg/protocol/engine/accounts/accountsledger"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
"github.com/iotaledger/iota-core/pkg/protocol/engine/eviction"
"github.com/iotaledger/iota-core/pkg/storage"
"github.com/iotaledger/iota-core/pkg/storage/utils"
iotago "github.com/iotaledger/iota.go/v4"
)
// Engines is a subcomponent of the protocol that exposes the engines that are managed by the protocol.
type Engines struct {
// Main contains the main engine.
Main reactive.Variable[*engine.Engine]
// protocol contains a reference to the Protocol instance that this component belongs to.
protocol *Protocol
// worker contains the worker pool that is used to process changes to the engine instances asynchronously.
worker *workerpool.WorkerPool
// directory contains the directory that is used to store the engine instances on disk.
directory *utils.Directory
// ReactiveModule embeds a reactive module that provides default API for logging and lifecycle management.
*module.ReactiveModule
}
// newEngines creates a new Engines instance.
func newEngines(protocol *Protocol) *Engines {
e := &Engines{
Main: reactive.NewVariable[*engine.Engine](),
ReactiveModule: protocol.NewReactiveSubModule("Engines"),
protocol: protocol,
worker: protocol.Workers.CreatePool("Engines", workerpool.WithWorkerCount(1)),
directory: utils.NewDirectory(protocol.Options.BaseDirectory),
}
protocol.Constructed.OnTrigger(func() {
shutdown := lo.Batch(
e.syncMainEngineFromMainChain(),
e.syncMainEngineInfoFile(),
e.injectEngineInstances(),
)
e.Shutdown.OnTrigger(func() {
shutdown()
e.Stopped.Trigger()
})
e.Initialized.Trigger()
})
e.Constructed.Trigger()
return e
}
// ForkAtSlot creates a new engine instance that forks from the main engine at the given slot.
func (e *Engines) ForkAtSlot(slot iotago.SlotIndex) (*engine.Engine, error) {
newEngineAlias := lo.PanicOnErr(uuid.NewUUID()).String()
errorHandler := func(err error) {
e.protocol.LogError("engine error", "err", err, "name", newEngineAlias[0:8])
}
// copy raw data on disk.
newStorage, err := storage.Clone(e.Main.Get().Storage, e.directory.Path(newEngineAlias), DatabaseVersion, errorHandler, e.protocol.Options.StorageOptions...)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to copy storage from active engine instance (%s) to new engine instance (%s)", e.Main.Get().Storage.Directory(), e.directory.Path(newEngineAlias))
}
// remove commitments that after forking point.
latestCommitment := newStorage.Settings().LatestCommitment()
if err = newStorage.Commitments().Rollback(slot, latestCommitment.Slot()); err != nil {
return nil, ierrors.Wrap(err, "failed to rollback commitments")
}
// create temporary components and rollback their permanent state, which will be reflected on disk.
evictionState := eviction.NewState(newStorage.Settings(), newStorage.RootBlocks)
evictionState.Initialize(latestCommitment.Slot())
blockCache := blocks.New(evictionState, newStorage.Settings().APIProvider())
accountsManager := accountsledger.New(newStorage.Settings().APIProvider(), blockCache.Block, newStorage.AccountDiffs, newStorage.Accounts())
accountsManager.SetLatestCommittedSlot(latestCommitment.Slot())
if err = accountsManager.Rollback(slot); err != nil {
return nil, ierrors.Wrap(err, "failed to rollback accounts manager")
}
if err = evictionState.Rollback(newStorage.Settings().LatestFinalizedSlot(), slot); err != nil {
return nil, ierrors.Wrap(err, "failed to rollback eviction state")
}
if err = newStorage.Ledger().Rollback(slot); err != nil {
return nil, err
}
targetCommitment, err := newStorage.Commitments().Load(slot)
if err != nil {
return nil, ierrors.Wrapf(err, "error while retrieving commitment for target index %d", slot)
}
if err = newStorage.Settings().Rollback(targetCommitment); err != nil {
return nil, err
}
if err = newStorage.Rollback(slot); err != nil {
return nil, err
}
candidateEngine := e.loadEngineInstanceWithStorage(newEngineAlias, newStorage)
// rollback attestations already on created engine instance, because this action modifies the in-memory storage.
if err = candidateEngine.Attestations.Rollback(slot); err != nil {
return nil, ierrors.Wrap(err, "error while rolling back attestations storage on candidate engine")
}
return candidateEngine, nil
}
// loadMainEngine loads the main engine from disk or creates a new one if no engine exists.
func (e *Engines) loadMainEngine(snapshotPath string) (*engine.Engine, error) {
info := &engineInfo{}
if err := ioutils.ReadJSONFromFile(e.infoFilePath(), info); err != nil && !ierrors.Is(err, os.ErrNotExist) {
return nil, ierrors.Errorf("unable to read engine info file: %w", err)
}
e.Main.Compute(func(mainEngine *engine.Engine) *engine.Engine {
// load previous engine as main engine if it exists.
if len(info.Name) > 0 {
if exists, isDirectory, err := ioutils.PathExists(e.directory.Path(info.Name)); err == nil && exists && isDirectory {
return e.loadEngineInstanceFromSnapshot(info.Name, snapshotPath)
}
}
// load new engine if no previous engine exists.
return e.loadEngineInstanceFromSnapshot(lo.PanicOnErr(uuid.NewUUID()).String(), snapshotPath)
})
// cleanup candidates
if err := e.cleanupCandidates(); err != nil {
return nil, err
}
return e.Main.Get(), nil
}
// cleanupCandidates removes all engine instances that are not the main engine.
func (e *Engines) cleanupCandidates() error {
activeDir := filepath.Base(e.Main.Get().Storage.Directory())
dirs, err := e.directory.SubDirs()
if err != nil {
return ierrors.Wrapf(err, "unable to list subdirectories of %s", e.directory.Path())
}
for _, dir := range dirs {
if dir == activeDir {
continue
}
if err := e.directory.RemoveSubdir(dir); err != nil {
return ierrors.Wrapf(err, "unable to remove subdirectory %s", dir)
}
}
return nil
}
// infoFilePath returns the path to the engine info file.
func (e *Engines) infoFilePath() string {
return e.directory.Path(engineInfoFile)
}
// loadEngineInstanceFromSnapshot loads an engine instance from a snapshot.
func (e *Engines) loadEngineInstanceFromSnapshot(engineAlias string, snapshotPath string) *engine.Engine {
errorHandler := func(err error) {
e.protocol.LogError("engine error", "err", err, "name", engineAlias[0:8])
}
return e.loadEngineInstanceWithStorage(engineAlias, storage.Create(e.directory.Path(engineAlias), DatabaseVersion, errorHandler, e.protocol.Options.StorageOptions...), engine.WithSnapshotPath(snapshotPath))
}
// loadEngineInstanceWithStorage loads an engine instance with the given storage.
func (e *Engines) loadEngineInstanceWithStorage(engineAlias string, storage *storage.Storage, engineOptions ...options.Option[engine.Engine]) *engine.Engine {
return engine.New(
e.protocol.Logger,
e.protocol.Workers.CreateGroup(engineAlias),
storage,
e.protocol.Options.PreSolidFilterProvider,
e.protocol.Options.PostSolidFilterProvider,
e.protocol.Options.BlockDAGProvider,
e.protocol.Options.BookerProvider,
e.protocol.Options.ClockProvider,
e.protocol.Options.BlockGadgetProvider,
e.protocol.Options.SlotGadgetProvider,
e.protocol.Options.SybilProtectionProvider,
e.protocol.Options.NotarizationProvider,
e.protocol.Options.AttestationProvider,
e.protocol.Options.LedgerProvider,
e.protocol.Options.SchedulerProvider,
e.protocol.Options.TipManagerProvider,
e.protocol.Options.TipSelectionProvider,
e.protocol.Options.RetainerProvider,
e.protocol.Options.UpgradeOrchestratorProvider,
e.protocol.Options.SyncManagerProvider,
append(e.protocol.Options.EngineOptions, engineOptions...)...,
)
}
// syncMainEngineFromMainChain syncs the main engine from the main chain.
func (e *Engines) syncMainEngineFromMainChain() (shutdown func()) {
return e.protocol.Chains.Main.WithNonEmptyValue(func(mainChain *Chain) (shutdown func()) {
return e.Main.DeriveValueFrom(reactive.NewDerivedVariable(func(currentMainEngine *engine.Engine, newMainEngine *engine.Engine) *engine.Engine {
return lo.Cond(newMainEngine == nil, currentMainEngine, newMainEngine)
}, mainChain.Engine))
})
}
// syncMainEngineInfoFile syncs the engine info file with the main engine.
func (e *Engines) syncMainEngineInfoFile() (shutdown func()) {
return e.Main.OnUpdate(func(_ *engine.Engine, mainEngine *engine.Engine) {
if mainEngine != nil {
if err := ioutils.WriteJSONToFile(e.infoFilePath(), &engineInfo{Name: filepath.Base(mainEngine.Storage.Directory())}, 0o644); err != nil {
e.LogError("unable to write engine info file", "err", err)
}
}
})
}
// injectEngineInstances injects engine instances into the chains (when requested).
func (e *Engines) injectEngineInstances() (shutdown func()) {
return e.protocol.Chains.WithElements(func(chain *Chain) (shutdown func()) {
return chain.StartEngine.OnUpdate(func(_ bool, startEngine bool) {
e.worker.Submit(func() {
if !startEngine {
chain.Engine.Set(nil)
return
}
if newEngine, err := func() (*engine.Engine, error) {
if e.Main.Get() == nil {
return e.loadMainEngine(e.protocol.Options.SnapshotPath)
}
return e.ForkAtSlot(chain.ForkingPoint.Get().Slot() - 1)
}(); err != nil {
e.LogError("failed to create new engine instance", "err", err)
} else {
e.protocol.Network.OnShutdown(func() { newEngine.Shutdown.Trigger() })
chain.Engine.Set(newEngine)
}
})
})
})
}
// engineInfoFile is the name of the engine info file.
const engineInfoFile = "info"
// engineInfo is the structure of the engine info file.
type engineInfo struct {
// Name contains the name of the engine.
Name string `json:"name"`
}