-
Notifications
You must be signed in to change notification settings - Fork 12
/
root.go
327 lines (271 loc) · 9.15 KB
/
root.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
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
package server
import (
"context"
"fmt"
"net"
"os"
"time"
// kwil-db
txpb "github.com/kwilteam/kwil-db/api/protobuf/tx/v1"
"github.com/kwilteam/kwil-db/internal/app/kwild/config"
"github.com/kwilteam/kwil-db/internal/controller/grpc/healthsvc/v0"
txSvc "github.com/kwilteam/kwil-db/internal/controller/grpc/txsvc/v1"
"github.com/kwilteam/kwil-db/internal/pkg/healthcheck"
simple_checker "github.com/kwilteam/kwil-db/internal/pkg/healthcheck/simple-checker"
"github.com/kwilteam/kwil-db/pkg/abci"
"github.com/kwilteam/kwil-db/pkg/balances"
"github.com/kwilteam/kwil-db/pkg/engine"
"github.com/kwilteam/kwil-db/pkg/grpc/gateway"
"github.com/kwilteam/kwil-db/pkg/grpc/gateway/middleware/cors"
grpc "github.com/kwilteam/kwil-db/pkg/grpc/server"
"github.com/kwilteam/kwil-db/pkg/log"
"github.com/kwilteam/kwil-db/pkg/modules/datasets"
"github.com/kwilteam/kwil-db/pkg/modules/validators"
"github.com/kwilteam/kwil-db/pkg/sql"
vmgr "github.com/kwilteam/kwil-db/pkg/validators"
// CometBFT
cmtcfg "github.com/cometbft/cometbft/config"
cmtflags "github.com/cometbft/cometbft/libs/cli/flags"
cmtlog "github.com/cometbft/cometbft/libs/log"
nm "github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/proxy"
cmtlocal "github.com/cometbft/cometbft/rpc/client/local"
"github.com/spf13/viper"
"google.golang.org/grpc/health/grpc_health_v1"
)
// BuildKwildServer builds the kwild server
func BuildKwildServer(ctx context.Context) (svr *Server, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic while building kwild: %v", r)
}
}()
cfg, err := config.LoadKwildConfig()
if err != nil {
return nil, err
}
logger := log.New(cfg.Log)
logger = *logger.Named("kwild")
deps := &coreDependencies{
ctx: ctx,
cfg: cfg,
log: logger,
opener: newSqliteOpener(cfg.SqliteFilePath),
}
return buildServer(deps), nil
}
type nodeWrapper struct {
n *nm.Node
}
func (nw *nodeWrapper) Start() error {
return nw.n.Start()
}
func (nw *nodeWrapper) Stop() error {
if err := nw.n.Stop(); err != nil {
return err
}
nw.n.Wait()
return nil
}
var _ (startStopper) = (*nodeWrapper)(nil)
func buildServer(d *coreDependencies) *Server {
// engine
e := buildEngine(d)
// account store
accs := buildAccountRepository(d)
// datasets module
datasetsModule := buildDatasetsModule(d, e, accs)
// validator updater and store
vstore := buildValidatorManager(d)
// validator module
validatorModule := buildValidatorModule(d, accs, vstore)
abciApp := buildAbci(d, datasetsModule, validatorModule, nil)
cometBftNode, err := newCometNode(abciApp, d.cfg)
if err != nil {
failBuild(err, "failed to create cometbft node")
}
cometBftClient := buildCometBftClient(cometBftNode)
// tx service
txsvc := buildTxSvc(d, datasetsModule, accs, &wrappedCometBFTClient{cometBftClient})
// grpc server
grpcServer := buildGrpcServer(d, txsvc)
return &Server{
grpcServer: grpcServer,
gateway: buildGatewayServer(d),
cometBftNode: &nodeWrapper{cometBftNode},
}
}
// coreDependencies holds dependencies that are widely used
type coreDependencies struct {
ctx context.Context
cfg *config.KwildConfig
log log.Logger
opener sql.Opener
}
func buildAbci(d *coreDependencies, datasetsModule abci.DatasetsModule, validatorModule abci.ValidatorModule,
atomicCommitter abci.AtomicCommitter) *abci.AbciApp {
return abci.NewAbciApp(
datasetsModule,
validatorModule,
atomicCommitter,
abci.WithLogger(*d.log.Named("abci")),
)
}
func buildTxSvc(d *coreDependencies, txsvc txSvc.EngineReader, accs txSvc.AccountReader,
cometBftClient txSvc.BlockchainBroadcaster) *txSvc.Service {
return txSvc.NewService(txsvc, accs, cometBftClient,
txSvc.WithLogger(*d.log.Named("tx-service")),
)
}
func buildDatasetsModule(d *coreDependencies, eng datasets.Engine, accs datasets.AccountStore) *datasets.DatasetModule {
feeMultiplier := 1
if d.cfg.WithoutGasCosts {
feeMultiplier = 0
}
return datasets.NewDatasetModule(eng, accs,
datasets.WithLogger(*d.log.Named("dataset-module")),
datasets.WithFeeMultiplier(int64(feeMultiplier)),
)
}
func buildEngine(d *coreDependencies) *engine.Engine {
extensions, err := connectExtensions(d.ctx, d.cfg.ExtensionEndpoints)
if err != nil {
failBuild(err, "failed to connect to extensions")
}
e, err := engine.Open(d.ctx, d.opener,
engine.WithLogger(*d.log.Named("engine")),
engine.WithExtensions(adaptExtensions(extensions)),
)
if err != nil {
failBuild(err, "failed to open engine")
}
return e
}
func buildAccountRepository(d *coreDependencies) *balances.AccountStore {
db, err := d.opener.Open("accounts_db", *d.log.Named("account-store"))
if err != nil {
failBuild(err, "failed to open accounts db")
}
b, err := balances.NewAccountStore(d.ctx, db,
balances.WithLogger(*d.log.Named("accountStore")),
balances.WithNonces(!d.cfg.WithoutNonces),
balances.WithGasCosts(!d.cfg.WithoutGasCosts),
)
if err != nil {
failBuild(err, "failed to build account store")
}
return b
}
func buildValidatorManager(d *coreDependencies) *vmgr.ValidatorMgr {
db, err := d.opener.Open("validator_db", *d.log.Named("validator-store"))
if err != nil {
failBuild(err, "failed to open validator db")
}
v, err := vmgr.NewValidatorMgr(d.ctx, db,
vmgr.WithLogger(*d.log.Named("validatorStore")),
)
if err != nil {
failBuild(err, "failed to build validator store")
}
return v
}
func buildValidatorModule(d *coreDependencies, accs datasets.AccountStore,
vals validators.ValidatorMgr) *validators.ValidatorModule {
return validators.NewValidatorModule(vals, accs, abci.Addresser,
validators.WithLogger(*d.log.Named("validator-module")))
}
func buildGrpcServer(d *coreDependencies, txsvc txpb.TxServiceServer) *grpc.Server {
lis, err := net.Listen("tcp", d.cfg.GrpcListenAddress)
if err != nil {
failBuild(err, "failed to build grpc server")
}
grpcServer := grpc.New(*d.log.Named("grpc-server"), lis)
txpb.RegisterTxServiceServer(grpcServer, txsvc)
grpc_health_v1.RegisterHealthServer(grpcServer, buildHealthSvc(d))
return grpcServer
}
func buildHealthSvc(d *coreDependencies) *healthsvc.Server {
// health service
registrar := healthcheck.NewRegistrar(*d.log.Named("healthcheck"))
registrar.RegisterAsyncCheck(10*time.Second, 3*time.Second, healthcheck.Check{
Name: "dummy",
Check: func(ctx context.Context) error {
// error make this check fail, nil will make it succeed
return nil
},
})
ck := registrar.BuildChecker(simple_checker.New(d.log))
return healthsvc.NewServer(ck)
}
func buildGatewayServer(d *coreDependencies) *gateway.GatewayServer {
gw, err := gateway.NewGateway(d.ctx, d.cfg.HttpListenAddress,
gateway.WithLogger(d.log),
gateway.WithMiddleware(cors.MCors([]string{})),
gateway.WithGrpcService(d.cfg.GrpcListenAddress, txpb.RegisterTxServiceHandlerFromEndpoint),
)
if err != nil {
failBuild(err, "failed to build gateway server")
}
return gw
}
func buildCometBftClient(cometBftNode *nm.Node) *cmtlocal.Local {
return cmtlocal.New(cometBftNode)
}
// TODO: clean this up --> @jchappelow
// it seems some of this should be handled in ABCI package if we do not provide it as a package
func newCometNode(app *abci.AbciApp, cfg *config.KwildConfig) (*nm.Node, error) {
config := cmtcfg.DefaultConfig()
CometHomeDir := os.Getenv("COMET_BFT_HOME")
fmt.Printf("Home Directory: %v", CometHomeDir)
config.SetRoot(CometHomeDir)
viper.SetConfigFile(fmt.Sprintf("%s/%s", CometHomeDir, "config/config.toml"))
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("reading config: %v", err)
}
if err := viper.Unmarshal(config); err != nil {
return nil, fmt.Errorf("decoding config: %v", err)
}
if err := config.ValidateBasic(); err != nil {
return nil, fmt.Errorf("invalid configuration data: %v", err)
}
pv := privval.LoadFilePV(
config.PrivValidatorKeyFile(),
config.PrivValidatorStateFile(),
)
fmt.Println("PrivateKey: ", pv.Key.PrivKey)
fmt.Println("PrivateValidator: ", string(pv.Key.PrivKey.Bytes()))
nodeKey, err := p2p.LoadNodeKey(config.NodeKeyFile())
if err != nil {
return nil, fmt.Errorf("failed to load node's key: %v", err)
}
logger := cmtlog.NewTMLogger(cmtlog.NewSyncWriter(os.Stdout))
logger, err = cmtflags.ParseLogLevel(config.LogLevel, logger, cfg.Log.Level)
if err != nil {
return nil, fmt.Errorf("failed to parse log level: %v", err)
}
// TODO: Move this to Application init and maybe use some kind of kvstore to store the validators info
fmt.Println("Pre RPC Config: ", config.RPC.ListenAddress, " ", cfg.BcRpcUrl)
cfg.BcRpcUrl = config.RPC.ListenAddress
fmt.Println("Post RPC Config: ", config.RPC.ListenAddress, " ", cfg.BcRpcUrl)
node, err := nm.NewNode(
config,
pv,
nodeKey,
proxy.NewLocalClientCreator(app), // TODO: NewUnsyncedLocalClientCreator(app) is other option which doesn't take a lock on all the connections to the app
nm.DefaultGenesisDocProviderFunc(config),
nm.DefaultDBProvider,
nm.DefaultMetricsProvider(config.Instrumentation),
logger,
)
if err != nil {
return nil, fmt.Errorf("creating node: %v", err)
}
return node, nil
}
func failBuild(err error, msg string) {
if err != nil {
panic(fmt.Sprintf("%s: %s", msg, err.Error()))
}
}