/
hosts.go
90 lines (82 loc) · 2.33 KB
/
hosts.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
package server
import (
"crypto/rsa"
pb "github.com/PomeloCloud/BFTRaft4go/proto/server"
"github.com/PomeloCloud/BFTRaft4go/utils"
"github.com/dgraph-io/badger"
"github.com/golang/protobuf/proto"
"github.com/patrickmn/go-cache"
"log"
"strconv"
)
type NodeIterator struct {
prefix []byte
data *badger.Iterator
server *BFTRaftServer
}
func (s *BFTRaftServer) GetHost(txn *badger.Txn, nodeId uint64) *pb.Host {
cacheKey := strconv.Itoa(int(nodeId))
if cacheNode, cachedFound := s.Hosts.Get(cacheKey); cachedFound {
return cacheNode.(*pb.Host)
}
if item, err := txn.Get(append(ComposeKeyPrefix(HOST_LIST_GROUP, HOST_LIST), utils.U64Bytes(nodeId)...)); err == nil {
data := ItemValue(item)
if data == nil {
return nil
}
node := pb.Host{}
proto.Unmarshal(*data, &node)
s.Hosts.Set(cacheKey, &node, cache.DefaultExpiration)
return &node
} else {
log.Println("cannot get host", nodeId, "on", s.Id, ":", err)
return nil
}
}
func (s *BFTRaftServer) GetHostNTXN(nodeId uint64) *pb.Host {
node := &pb.Host{}
s.DB.View(func(txn *badger.Txn) error {
node = s.GetHost(txn, nodeId)
return nil
})
return node
}
func (s *BFTRaftServer) GetHostPublicKey(nodeId uint64) *rsa.PublicKey {
cacheKey := strconv.Itoa(int(nodeId))
if cachedKey, cacheFound := s.NodePublicKeys.Get(cacheKey); cacheFound {
return cachedKey.(*rsa.PublicKey)
}
node := s.GetHostNTXN(nodeId)
if node == nil {
log.Println("cannot get node for get it's public key")
return nil
}
if key, err := utils.ParsePublicKey(node.PublicKey); err == nil {
s.NodePublicKeys.Set(cacheKey, key, cache.DefaultExpiration)
return key
} else {
return nil
}
}
func (s *BFTRaftServer) SaveHost(txn *badger.Txn, node *pb.Host) error {
if data, err := proto.Marshal(node); err == nil {
dbKey := append(ComposeKeyPrefix(HOST_LIST_GROUP, HOST_LIST), utils.U64Bytes(node.Id)...)
return txn.Set(dbKey, data, 0x00)
} else {
return err
}
}
func (s *BFTRaftServer) SaveHostNTXN(node *pb.Host) error {
return s.DB.Update(func(txn *badger.Txn) error {
return s.SaveHost(txn, node)
})
}
func (s *BFTRaftServer) VerifyCommandSign(cmd *pb.CommandRequest) bool {
signData := utils.ExecCommandSignData(cmd)
publicKey := s.GetHostPublicKey(cmd.ClientId)
if publicKey == nil {
return false
} else {
return utils.VerifySign(publicKey, cmd.Signature, signData) == nil
}
}