-
Notifications
You must be signed in to change notification settings - Fork 33
/
enclave_container.go
81 lines (64 loc) · 2.59 KB
/
enclave_container.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
package container
import (
"context"
"fmt"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/obscuronet/go-obscuro/go/common"
"github.com/obscuronet/go-obscuro/go/common/log"
"github.com/obscuronet/go-obscuro/go/enclave"
"github.com/obscuronet/go-obscuro/go/config"
"github.com/obscuronet/go-obscuro/go/ethadapter/mgmtcontractlib"
obscuroGenesis "github.com/obscuronet/go-obscuro/go/enclave/genesis"
)
// todo (#1056) - replace with the genesis.json of Obscuro's L1 network.
const hardcodedGenesisJSON = "TODO - REPLACE ME"
type EnclaveContainer struct {
Enclave common.Enclave
RPCServer *enclave.RPCServer
Logger gethlog.Logger
}
func (e *EnclaveContainer) Start() error {
err := e.RPCServer.StartServer()
if err != nil {
return err
}
e.Logger.Info("obscuro enclave RPC service started.")
return nil
}
func (e *EnclaveContainer) Stop() error {
_, err := e.RPCServer.Stop(context.Background(), nil)
if err != nil {
e.Logger.Error("Unable to cleanly stop enclave", log.ErrKey, err)
return err
}
return nil
}
// NewEnclaveContainerFromConfig wires up the components of the Enclave and its RPC server. Manages their lifecycle/monitors their status
func NewEnclaveContainerFromConfig(config *config.EnclaveConfig) *EnclaveContainer {
// todo - improve this wiring, perhaps setup DB etc. at this level and inject into enclave
// (at that point the WithLogger constructor could be a full DI constructor like the HostContainer tries, for testability)
logger := log.New(log.EnclaveCmp, config.LogLevel, config.LogPath, log.NodeIDKey, config.HostID)
// todo - this is for debugging purposes only, should be remove in the future
fmt.Printf("Building enclave container with config: %+v\n", config)
logger.Info(fmt.Sprintf("Building enclave container with config: %+v", config))
return NewEnclaveContainerWithLogger(config, logger)
}
// NewEnclaveContainerWithLogger is useful for testing etc.
func NewEnclaveContainerWithLogger(config *config.EnclaveConfig, logger gethlog.Logger) *EnclaveContainer {
contractAddr := config.ManagementContractAddress
mgmtContractLib := mgmtcontractlib.NewMgmtContractLib(&contractAddr, logger)
if config.ValidateL1Blocks {
config.GenesisJSON = []byte(hardcodedGenesisJSON)
}
genesis, err := obscuroGenesis.New(config.ObscuroGenesis)
if err != nil {
logger.Crit("unable to parse obscuro genesis", log.ErrKey, err)
}
encl := enclave.NewEnclave(config, genesis, mgmtContractLib, logger)
rpcServer := enclave.NewEnclaveRPCServer(config.Address, encl, logger)
return &EnclaveContainer{
Enclave: encl,
RPCServer: rpcServer,
Logger: logger,
}
}