-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathenclave.go
More file actions
397 lines (330 loc) · 16.3 KB
/
Copy pathenclave.go
File metadata and controls
397 lines (330 loc) · 16.3 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package enclave
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
"time"
"github.com/ten-protocol/go-ten/go/ethadapter/contractlib"
"github.com/ten-protocol/go-ten/go/enclave/evm"
"github.com/ten-protocol/go-ten/go/common/compression"
"github.com/ten-protocol/go-ten/go/enclave/crypto"
enclaveconfig "github.com/ten-protocol/go-ten/go/enclave/config"
"github.com/ten-protocol/go-ten/go/enclave/evm/ethchainadapter"
"github.com/ten-protocol/go-ten/go/enclave/gas"
"github.com/ten-protocol/go-ten/go/enclave/storage"
"github.com/ten-protocol/go-ten/go/enclave/system"
"github.com/ten-protocol/go-ten/go/enclave/components"
"github.com/ten-protocol/go-ten/go/responses"
"github.com/ten-protocol/go-ten/go/enclave/genesis"
"github.com/ten-protocol/go-ten/go/common/errutil"
"github.com/ten-protocol/go-ten/go/common"
"github.com/ten-protocol/go-ten/go/common/gethencoding"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/stopcontrol"
"github.com/ten-protocol/go-ten/go/common/tracers"
"github.com/ten-protocol/go-ten/go/enclave/crosschain"
"github.com/ten-protocol/go-ten/go/enclave/events"
_ "github.com/ten-protocol/go-ten/go/common/tracers/native" // make sure the tracers are loaded
gethcommon "github.com/ethereum/go-ethereum/common"
gethlog "github.com/ethereum/go-ethereum/log"
gethrpc "github.com/ten-protocol/go-ten/lib/gethfork/rpc"
)
type enclaveImpl struct {
initAPI common.EnclaveInit
adminAPI common.EnclaveAdmin
rpcAPI common.EnclaveClientRPC
processingCtx context.Context // context for async data processing tasks that should not be canceled if RPC caller context is canceled
stopControl *stopcontrol.StopControl
shutdownFunc func() // function to call when stopping the enclave (e.g. sys exit for docker but not for tests)
}
// NewEnclave creates and initializes all the services of the enclave.
//
// `genesisJSON` is the configuration for the corresponding L1's genesis block. This is used to validate the blocks
// received from the L1 node if `validateBlocks` is set to true.
func NewEnclave(config *enclaveconfig.EnclaveConfig, genesis *genesis.Genesis, contractRegistryLib contractlib.ContractRegistryLib, logger gethlog.Logger) common.Enclave {
jsonConfig, _ := json.MarshalIndent(config, "", " ")
logger.Info("Creating enclave service with following config", log.CfgKey, string(jsonConfig))
chainConfig := ethchainadapter.ChainParams(big.NewInt(config.TenChainID))
// Initialise the database
cachingService := storage.NewCacheService(logger, config.UseInMemoryDB)
storage := storage.NewStorageFromConfig(config, cachingService, chainConfig, logger)
err := storage.DeleteDirtyBlocks(context.Background())
if err != nil {
logger.Crit("failed to clean dirty blocks", log.ErrKey, err)
}
// attempt to fetch the enclave key from the database
// the enclave key is part of the attestation and identifies the current enclave
// if this is the first time the enclave starts, it has to generate a new key
enclaveKeyService := crypto.NewEnclaveAttestedKeyService(logger)
err = loadOrCreateEnclaveKey(storage, enclaveKeyService, logger)
if err != nil {
logger.Crit("Failed to load or create enclave key", log.ErrKey, err)
}
sharedSecretService := crypto.NewSharedSecretService(logger)
sharedSecret, err := storage.FetchSecret(context.Background())
if err != nil && !errors.Is(err, errutil.ErrNotFound) {
logger.Crit("Failed to fetch secret", "err", err)
}
if sharedSecret != nil {
sharedSecretService.SetSharedSecret(sharedSecret)
} else if len(config.SharedSecret) != 0 {
// if the shared secret is configured explicitly, use it
// this is a breaking glass functionality to allow recovery after an extreme event
var configSharedSecret [crypto.SharedSecretLenInBytes]byte
copy(configSharedSecret[:], gethcommon.Hex2BytesFixed(config.SharedSecret, crypto.SharedSecretLenInBytes))
sharedSecretService.SetSharedSecret((*crypto.SharedEnclaveSecret)(&configSharedSecret))
logger.Info("Started node with configured shared secret")
}
daEncryptionService := crypto.NewDAEncryptionService(sharedSecretService, logger)
rpcKeyService := crypto.NewRPCKeyService(sharedSecretService, logger)
crossChainProcessors := crosschain.New(&config.MessageBusAddress, &config.L1BridgeAddress, storage, logger)
// initialise system contracts
scb := system.NewSystemContractCallbacks(storage, &config.SystemContractOwner, logger)
err = scb.Load(crossChainProcessors.Local)
if err != nil && !errors.Is(err, errutil.ErrNotFound) {
logger.Crit("failed to load system contracts", log.ErrKey, err)
}
l1ChainCfg := common.GetL1ChainConfig(uint64(config.L1ChainID))
dataCompressionService := compression.NewBrotliDataCompressionService(int64(config.DecompressionLimit))
gasPricer := components.NewGasPricer(logger, config, storage)
gasOracle := gas.NewGasOracle(l1ChainCfg, storage, logger)
blockProcessor := components.NewBlockProcessor(storage, crossChainProcessors, gasOracle, logger)
// start the mempool in validate only. Based on the config, it might become sequencer
evmEntropyService := crypto.NewEvmEntropyService(sharedSecretService, logger)
gethEncodingService := gethencoding.NewGethEncodingService(storage, cachingService, evmEntropyService, logger)
batchRegistry := components.NewBatchRegistry(storage, config, gethEncodingService, logger)
chainContext := evm.NewTenChainContext(storage, gethEncodingService, config, chainConfig, logger)
visibilityReader := evm.NewContractVisibilityReader(logger)
evmFacade := evm.NewEVMExecutor(chainContext, chainConfig, config, config.GasLocalExecutionCapFlag, storage, gethEncodingService, visibilityReader, logger)
tenChain := components.NewChain(storage, config, evmFacade, gethEncodingService, chainConfig, genesis, logger, batchRegistry)
// note: this has to happen after the sync of executed batches as the mempool needs to know the current state
mempool, err := components.NewTxPool(batchRegistry.EthChain(), config, tenChain, storage, batchRegistry, blockProcessor, gasOracle, gasPricer, config.MinGasPrice, true, logger)
if err != nil {
logger.Crit("unable to init eth tx pool", log.ErrKey, err)
}
batchExecutor := components.NewBatchExecutor(storage, batchRegistry, evmFacade, config, gethEncodingService, crossChainProcessors, genesis, gasOracle, chainConfig, scb, evmEntropyService, mempool, dataCompressionService, gasPricer, logger)
subscriptionManager := events.NewSubscriptionManager(storage, batchRegistry, config.TenChainID, logger)
// todo (#1474) - make sure the enclave cannot be started in production with WillAttest=false
attestationProvider := components.NewAttestationProvider(enclaveKeyService, config.WillAttest, logger)
processingCtx, cancelProcessingCtx := context.WithCancel(context.Background())
// signal to stop the enclave
stopControl := stopcontrol.New()
// shutdownFunc is called after services attempt a graceful shutdown.
// For tests, we don't want the process to exit, but in docker we do
shutdownFunc := func() {
cancelProcessingCtx()
logger.Info("enclave shutdown complete")
}
if config.WillAttest { // using attestation as a signal that this is a production enclave
shutdownFunc = func() {
cancelProcessingCtx()
time.Sleep(1 * time.Second)
fmt.Println("enclave shutdown complete")
os.Exit(0)
}
}
// these services are directly exposed as the API of the Enclave
initAPI := NewEnclaveInitAPI(config, storage, logger, blockProcessor, enclaveKeyService, attestationProvider, sharedSecretService, daEncryptionService, rpcKeyService)
adminAPI := NewEnclaveAdminAPI(config, storage, logger, blockProcessor, batchRegistry, batchExecutor, gethEncodingService, stopControl, subscriptionManager, enclaveKeyService, mempool, chainConfig, attestationProvider, sharedSecretService, daEncryptionService, contractRegistryLib, gasOracle, gasPricer)
rpcAPI := NewEnclaveRPCAPI(config, storage, tenChain, logger, blockProcessor, batchRegistry, gethEncodingService, cachingService, mempool, chainConfig, crossChainProcessors, scb, subscriptionManager, genesis, gasOracle, gasPricer, rpcKeyService, evmFacade)
logger.Info("Enclave service created successfully.", log.EnclaveIDKey, enclaveKeyService.EnclaveID())
return &enclaveImpl{
initAPI: initAPI,
adminAPI: adminAPI,
rpcAPI: rpcAPI,
processingCtx: processingCtx,
stopControl: stopControl,
shutdownFunc: shutdownFunc,
}
}
// Status is only implemented by the RPC wrapper
func (e *enclaveImpl) Status(ctx context.Context) (common.Status, common.SystemError) {
return e.adminAPI.Status(ctx)
}
func (e *enclaveImpl) Attestation(ctx context.Context) (*common.AttestationReport, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.initAPI.Attestation(ctx)
}
// GenerateSecret - the genesis enclave is responsible with generating the secret entropy
func (e *enclaveImpl) GenerateSecret(ctx context.Context) (common.EncryptedSharedEnclaveSecret, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.initAPI.GenerateSecret(ctx)
}
// InitEnclave - initialise an enclave with a seed received by another enclave
func (e *enclaveImpl) InitEnclave(ctx context.Context, s common.EncryptedSharedEnclaveSecret) common.SystemError {
return e.initAPI.InitEnclave(ctx, s)
}
func (e *enclaveImpl) EnclaveID(ctx context.Context) (common.EnclaveID, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return common.EnclaveID{}, systemError
}
return e.initAPI.EnclaveID(ctx)
}
func (e *enclaveImpl) RPCEncryptionKey(ctx context.Context) ([]byte, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.initAPI.RPCEncryptionKey(ctx)
}
func (e *enclaveImpl) DebugTraceTransaction(ctx context.Context, txHash gethcommon.Hash, config *tracers.TraceConfig) (json.RawMessage, common.SystemError) {
return e.rpcAPI.DebugTraceTransaction(ctx, txHash, config)
}
func (e *enclaveImpl) GetTotalContractCount(ctx context.Context) (*big.Int, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.rpcAPI.GetTotalContractCount(ctx)
}
func (e *enclaveImpl) EnclavePublicConfig(ctx context.Context) (*common.EnclavePublicConfig, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.rpcAPI.EnclavePublicConfig(ctx)
}
func (e *enclaveImpl) FetchSequencerAttestations(ctx context.Context) ([]*common.AttestationReport, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.rpcAPI.FetchSequencerAttestations(ctx)
}
func (e *enclaveImpl) EncryptedRPC(ctx context.Context, encryptedParams common.EncryptedRequest) (*responses.EnclaveResponse, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.rpcAPI.EncryptedRPC(ctx, encryptedParams)
}
func (e *enclaveImpl) GetCode(ctx context.Context, address gethcommon.Address, blockNrOrHash gethrpc.BlockNumberOrHash) ([]byte, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.rpcAPI.GetCode(ctx, address, blockNrOrHash)
}
func (e *enclaveImpl) Subscribe(ctx context.Context, id gethrpc.ID, encryptedSubscription common.EncryptedParamsLogSubscription) common.SystemError {
return e.rpcAPI.Subscribe(ctx, id, encryptedSubscription)
}
func (e *enclaveImpl) Unsubscribe(id gethrpc.ID) common.SystemError {
if systemError := checkStopping(e.stopControl); systemError != nil {
return systemError
}
return e.rpcAPI.Unsubscribe(id)
}
func (e *enclaveImpl) MakeActive() common.SystemError {
if systemError := checkStopping(e.stopControl); systemError != nil {
return systemError
}
return e.adminAPI.MakeActive()
}
func (e *enclaveImpl) ExportCrossChainData(ctx context.Context, fromSeqNo uint64, toSeqNo uint64) (*common.ExtCrossChainBundle, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.adminAPI.ExportCrossChainData(ctx, fromSeqNo, toSeqNo)
}
func (e *enclaveImpl) BackupSharedSecret(ctx context.Context) ([]byte, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.adminAPI.BackupSharedSecret(ctx)
}
func (e *enclaveImpl) GetBatch(ctx context.Context, hash common.L2BatchHash) (*common.ExtBatch, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.adminAPI.GetBatch(ctx, hash)
}
func (e *enclaveImpl) GetBatchBySeqNo(ctx context.Context, seqNo uint64) (*common.ExtBatch, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.adminAPI.GetBatchBySeqNo(ctx, seqNo)
}
func (e *enclaveImpl) GetRollupData(ctx context.Context, hash common.L2RollupHash) (*common.PublicRollupMetadata, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.adminAPI.GetRollupData(ctx, hash)
}
func (e *enclaveImpl) StreamL2Updates() (chan common.StreamL2UpdatesResponse, func()) {
return e.adminAPI.StreamL2Updates()
}
// SubmitL1Block is used to update the enclave with an additional L1 block.
func (e *enclaveImpl) SubmitL1Block(ctx context.Context, processed *common.ProcessedL1Data) (*common.BlockSubmissionResponse, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
// pass a background context here as the data processing should not be canceled by the caller disconnecting
return e.adminAPI.SubmitL1Block(e.processingCtx, processed)
}
func (e *enclaveImpl) SubmitBatch(ctx context.Context, extBatch *common.ExtBatch) common.SystemError {
if systemError := checkStopping(e.stopControl); systemError != nil {
return systemError
}
// pass a background context here as the data processing should not be canceled by the caller disconnecting
return e.adminAPI.SubmitBatch(e.processingCtx, extBatch)
}
func (e *enclaveImpl) CreateBatch(ctx context.Context, skipBatchIfEmpty bool) common.SystemError {
if systemError := checkStopping(e.stopControl); systemError != nil {
return systemError
}
return e.adminAPI.CreateBatch(ctx, skipBatchIfEmpty)
}
func (e *enclaveImpl) CreateRollup(ctx context.Context, fromSeqNo uint64) (*common.CreateRollupResult, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.adminAPI.CreateRollup(ctx, fromSeqNo)
}
// HealthCheck returns whether the enclave is deemed healthy
func (e *enclaveImpl) HealthCheck(ctx context.Context) (bool, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return false, systemError
}
return e.adminAPI.HealthCheck(ctx)
}
// StopClient is only implemented by the RPC wrapper
func (e *enclaveImpl) StopClient() common.SystemError {
return e.adminAPI.StopClient()
}
func (e *enclaveImpl) Stop() common.SystemError {
defer e.shutdownFunc()
err := e.adminAPI.Stop()
if err != nil {
return responses.ToInternalError(err)
}
return nil
}
func checkStopping(s *stopcontrol.StopControl) common.SystemError {
if s.IsStopping() {
return responses.ToInternalError(fmt.Errorf("enclave is stopping"))
}
return nil
}
func loadOrCreateEnclaveKey(storage storage.Storage, enclaveKeyService *crypto.EnclaveAttestedKeyService, logger gethlog.Logger) error {
enclaveKey, err := storage.GetEnclaveKey(context.Background())
if err != nil && !errors.Is(err, errutil.ErrNotFound) {
return fmt.Errorf("failed to load enclave key: %w", err)
}
if enclaveKey != nil {
enclaveKeyService.SetEnclaveKey(enclaveKey)
return nil
}
// enclave key not found - new key should be generated
logger.Info("Generating new enclave key")
enclaveKey, err = enclaveKeyService.GenerateEnclaveKey()
if err != nil {
return fmt.Errorf("failed to generate enclave key: %w", err)
}
err = storage.StoreEnclaveKey(context.Background(), enclaveKey)
if err != nil {
return fmt.Errorf("failed to store enclave key: %w", err)
}
enclaveKeyService.SetEnclaveKey(enclaveKey)
return nil
}