/
protocol_server.go
171 lines (146 loc) · 4.43 KB
/
protocol_server.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
package cmd
import (
"context"
"fmt"
"io"
"net"
"os"
"path/filepath"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
memorySigner "github.com/oasisprotocol/oasis-core/go/common/crypto/signature/signers/memory"
"github.com/oasisprotocol/oasis-core/go/common/identity"
"github.com/oasisprotocol/oasis-core/go/common/logging"
genesisTestHelpers "github.com/oasisprotocol/oasis-core/go/genesis/tests"
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/background"
"github.com/oasisprotocol/oasis-core/go/storage/api"
"github.com/oasisprotocol/oasis-core/go/storage/database"
badgerNodedb "github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/badger"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/interop/fixtures"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/node"
)
const (
cfgServerSocket = "socket"
cfgServerDataDir = "datadir"
cfgServerFixture = "fixture"
)
var (
protoServerFlags = flag.NewFlagSet("", flag.ContinueOnError)
protoServerCmd = &cobra.Command{
Use: "proto-server",
Short: "run simple gRPC server implementing the storage service",
Run: doProtoServer,
}
logger = logging.GetLogger("cmd/protocol_server")
)
func doProtoServer(cmd *cobra.Command, args []string) {
svcMgr := background.NewServiceManager(logger)
defer svcMgr.Cleanup()
dataDir := viper.GetString(cfgServerDataDir)
if dataDir == "" {
fmt.Println("no data directory specified")
return
}
// Initialize logging.
logFile := filepath.Join(dataDir, "protocol_server.log")
w, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
if err != nil {
fmt.Printf("failed to open log file: %v\n", err)
return
}
logWriter := io.MultiWriter(os.Stdout, w)
if err = logging.Initialize(logWriter, logging.FmtJSON, logging.LevelWarn, nil); err != nil {
fmt.Printf("failed to initialize logging: %v\n", err)
return
}
genesisTestHelpers.SetTestChainContext()
// Generate dummy identity.
ident, err := identity.LoadOrGenerate(dataDir, memorySigner.NewFactory(), false)
if err != nil {
logger.Error("failed to generate identity",
"err", err,
)
return
}
// Initialize a dummy storage backend.
storageCfg := api.Config{
Backend: database.BackendNameBadgerDB,
DB: dataDir,
Signer: ident.NodeSigner,
ApplyLockLRUSlots: 1,
InsecureSkipChecks: false,
MaxCacheSize: 16 * 1024 * 1024,
}
if fixtureName := viper.GetString(cfgServerFixture); fixtureName != "" {
ctx := context.Background()
ndbCfg := storageCfg.ToNodeDB()
var ndb api.NodeDB
ndb, err = badgerNodedb.New(ndbCfg)
if err != nil {
logger.Error("failed to initialize node db",
"err", err,
)
return
}
var fixture fixtures.Fixture
fixture, err = fixtures.GetFixture(fixtureName)
if err != nil {
logger.Error("failed getting fixture",
"err", err,
"fixture", fixture.Name(),
)
return
}
var root *node.Root
root, err = fixture.Populate(ctx, ndb)
if err != nil {
logger.Error("failed to populate fixture",
"err", err,
"fixture", fixture.Name(),
)
return
}
fmt.Printf("Fixture: %s, populated root hash: %s\n", fixtureName, root.Hash)
ndb.Close()
}
backend, err := database.New(&storageCfg)
if err != nil {
logger.Error("failed to initialize storage backend",
"err", err,
)
return
}
// Initialize the JSON-RPC listener/service.
ln, err := net.ListenUnix("unix", &net.UnixAddr{Name: viper.GetString(cfgServerSocket)})
if err != nil {
logger.Error("failed to listen on socket",
"err", err,
)
return
}
rpcSvr := newDbRPCService(ln, backend)
svcMgr.Register(rpcSvr)
// Start the JSON-RPC server.
if err := rpcSvr.Start(); err != nil {
logger.Error("failed to start JSON-RPC server",
"err", err,
)
return
}
logger.Info("initialization complete: ready to serve")
// Wait for the services to catch on fire or otherwise
// terminate.
svcMgr.Wait()
}
// Register registers the grpc-server sub-command and all of it's children.
func RegisterProtoServer(parentCmd *cobra.Command) {
protoServerCmd.Flags().AddFlagSet(protoServerFlags)
parentCmd.AddCommand(protoServerCmd)
}
func init() {
protoServerFlags.String(cfgServerSocket, "storage.sock", "path to storage protocol server socket")
protoServerFlags.String(cfgServerDataDir, "", "path to data directory")
protoServerFlags.String(cfgServerFixture, "", "fixture for initializing initial state")
_ = viper.BindPFlags(protoServerFlags)
}