-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathobserver.go
More file actions
98 lines (88 loc) · 2.68 KB
/
observer.go
File metadata and controls
98 lines (88 loc) · 2.68 KB
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
/*
* Copyright 2019 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package worker
import (
"github.com/CovenantSQL/CovenantSQL/crypto"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/kms"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/types"
"github.com/CovenantSQL/CovenantSQL/utils/log"
)
// ObserverFetchBlock handles observer fetch block logic.
func (rpc *DBMSRPCService) ObserverFetchBlock(req *ObserverFetchBlockReq, resp *ObserverFetchBlockResp) (err error) {
subscriberID := req.GetNodeID().ToNodeID()
resp.Block, resp.Count, err = rpc.dbms.observerFetchBlock(req.DatabaseID, subscriberID, req.Count)
return
}
func (dbms *DBMS) observerFetchBlock(dbID proto.DatabaseID, nodeID proto.NodeID, count int32) (
block *types.Block, realCount int32, err error) {
var (
pubKey *asymmetric.PublicKey
addr proto.AccountAddress
height int32
)
// node parameters
pubKey, err = kms.GetPublicKey(nodeID)
if err != nil {
log.WithFields(log.Fields{
"databaseID": dbID,
"nodeID": nodeID,
}).WithError(err).Warning("get public key failed in observerFetchBlock")
return
}
addr, err = crypto.PubKeyHash(pubKey)
if err != nil {
log.WithFields(log.Fields{
"databaseID": dbID,
"nodeID": nodeID,
}).WithError(err).Warning("generate addr failed in observerFetchBlock")
return
}
defer func() {
lf := log.WithFields(log.Fields{
"dbID": dbID,
"nodeID": nodeID,
"addr": addr.String(),
"count": count,
})
if err != nil {
lf.WithError(err).Debug("observer fetch block")
} else {
if block != nil {
lf = lf.WithField("block", block.BlockHash())
}
lf.WithField("height", height).Debug("observer fetch block")
}
}()
// check permission
err = dbms.checkPermission(addr, dbID, types.ReadQuery, nil)
if err != nil {
log.WithFields(log.Fields{
"databaseID": dbID,
"addr": addr,
}).WithError(err).Warning("permission deny")
return
}
rawDB, ok := dbms.dbMap.Load(dbID)
if !ok {
err = ErrNotExists
return
}
db := rawDB.(*Database)
block, realCount, height, err = db.chain.FetchBlockByCount(count)
return
}