-
Notifications
You must be signed in to change notification settings - Fork 672
/
keystore_server.go
70 lines (57 loc) · 1.76 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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gkeystore
import (
"context"
"google.golang.org/grpc"
"github.com/hashicorp/go-plugin"
"github.com/ava-labs/avalanchego/api/keystore"
"github.com/ava-labs/avalanchego/api/keystore/gkeystore/gkeystoreproto"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/rpcdb"
"github.com/ava-labs/avalanchego/database/rpcdb/rpcdbproto"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
)
var _ gkeystoreproto.KeystoreServer = &Server{}
// Server is a snow.Keystore that is managed over RPC.
type Server struct {
gkeystoreproto.UnimplementedKeystoreServer
ks keystore.BlockchainKeystore
broker *plugin.GRPCBroker
}
// NewServer returns a keystore connected to a remote keystore
func NewServer(ks keystore.BlockchainKeystore, broker *plugin.GRPCBroker) *Server {
return &Server{
ks: ks,
broker: broker,
}
}
func (s *Server) GetDatabase(
_ context.Context,
req *gkeystoreproto.GetDatabaseRequest,
) (*gkeystoreproto.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
dbBrokerID := s.broker.NextId()
go s.broker.AcceptAndServe(dbBrokerID, func(opts []grpc.ServerOption) *grpc.Server {
server := grpc.NewServer(opts...)
closer.closer.Add(server)
db := rpcdb.NewServer(&closer)
rpcdbproto.RegisterDatabaseServer(server, db)
return server
})
return &gkeystoreproto.GetDatabaseResponse{DbServer: dbBrokerID}, nil
}
type dbCloser struct {
database.Database
closer grpcutils.ServerCloser
}
func (db *dbCloser) Close() error {
err := db.Database.Close()
db.closer.Stop()
return err
}