forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
keystore_server.go
72 lines (58 loc) · 1.75 KB
/
keystore_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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gkeystore
import (
"context"
"google.golang.org/grpc"
"github.com/MetalBlockchain/metalgo/api/keystore"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/database/rpcdb"
"github.com/MetalBlockchain/metalgo/vms/rpcchainvm/grpcutils"
keystorepb "github.com/MetalBlockchain/metalgo/proto/pb/keystore"
rpcdbpb "github.com/MetalBlockchain/metalgo/proto/pb/rpcdb"
)
var _ keystorepb.KeystoreServer = (*Server)(nil)
// Server is a snow.Keystore that is managed over RPC.
type Server struct {
keystorepb.UnsafeKeystoreServer
ks keystore.BlockchainKeystore
}
// NewServer returns a keystore connected to a remote keystore
func NewServer(ks keystore.BlockchainKeystore) *Server {
return &Server{
ks: ks,
}
}
func (s *Server) GetDatabase(
_ context.Context,
req *keystorepb.GetDatabaseRequest,
) (*keystorepb.GetDatabaseResponse, error) {
db, err := s.ks.GetRawDatabase(req.Username, req.Password)
if err != nil {
return nil, err
}
closer := dbCloser{Database: db}
// start the db server
serverListener, err := grpcutils.NewListener()
if err != nil {
return nil, err
}
serverAddr := serverListener.Addr().String()
go grpcutils.Serve(serverListener, func(opts []grpc.ServerOption) *grpc.Server {
server := grpcutils.NewDefaultServer(opts)
closer.closer.Add(server)
db := rpcdb.NewServer(&closer)
rpcdbpb.RegisterDatabaseServer(server, db)
return server
})
return &keystorepb.GetDatabaseResponse{ServerAddr: serverAddr}, nil
}
type dbCloser struct {
database.Database
closer grpcutils.ServerCloser
}
func (db *dbCloser) Close() error {
err := db.Database.Close()
db.closer.Stop()
return err
}