Skip to content

Commit 78a8041

Browse files
author
auxten
committed
organize all BPInfo in kms.BP
1 parent 9a66ab5 commit 78a8041

10 files changed

Lines changed: 78 additions & 62 deletions

File tree

cmd/thunderdbd/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func runClient() (err error) {
8787

8888
func clientRequest(connPool *rpc.SessionPool, reqType string, sql string) (err error) {
8989
log.SetLevel(log.DebugLevel)
90-
leaderNodeID := kms.BPNodeID
90+
leaderNodeID := kms.BP.NodeID
9191
var conn net.Conn
9292
var client *rpc.Client
9393

cmd/thunderdbd/initconf.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ var (
3535
// AllNodes holds all BP nodes and client node info
3636
AllNodes = []NodeInfo{
3737
{
38-
ID: kms.BPNodeID,
39-
Nonce: kms.BPNonce,
38+
ID: kms.BP.NodeID,
39+
Nonce: kms.BP.Nonce,
4040
PublicKey: nil,
4141
Addr: "127.0.0.1:2122",
4242
Role: kayak.Leader,
@@ -100,7 +100,7 @@ func initNodePeers(idx int, publicKeystorePath string) (nodes *[]NodeInfo, peers
100100

101101
for i, n := range AllNodes[:] {
102102
if n.Role == kayak.Leader || n.Role == kayak.Follower {
103-
AllNodes[i].PublicKey = kms.BPPublicKey
103+
AllNodes[i].PublicKey = kms.BP.PublicKey
104104
peers.Servers = append(peers.Servers, &kayak.Server{
105105
Role: n.Role,
106106
ID: n.ID,

cmd/thunderdbd/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ var (
5656
publicKeyStorePath string
5757

5858
// other
59-
noLogo bool
60-
showVersion bool
59+
noLogo bool
60+
showVersion bool
61+
integrationTest bool // run integration testing
6162

6263
// rpc server
6364
rpcServer *rpc.Server
@@ -72,6 +73,7 @@ const desc = `ThunderDB is a database`
7273
func init() {
7374
flag.BoolVar(&noLogo, "nologo", false, "Do not print logo")
7475
flag.BoolVar(&showVersion, "version", false, "Show version information and exit")
76+
flag.BoolVar(&integrationTest, "test", false, "Run integration test mode")
7577
flag.StringVar(&privateKeyPath, "private-key-path", "./private.key", "Path to private key file")
7678
flag.StringVar(&publicKeyStorePath, "public-keystore-path", "./public.keystore", "Path to public keystore file")
7779
flag.StringVar(&cpuProfile, "cpu-profile", "", "Path to file for CPU profiling information")
@@ -85,16 +87,18 @@ func init() {
8587
}
8688

8789
func initLogs() {
88-
log.SetLevel(log.DebugLevel)
89-
9090
log.Infof("%s starting, version %s, commit %s, branch %s", name, version, commit, branch)
9191
log.Infof("%s, target architecture is %s, operating system target is %s", runtime.Version(), runtime.GOARCH, runtime.GOOS)
9292
log.Infof("role: %s", conf.Role)
9393
}
9494

9595
func main() {
96+
log.SetLevel(log.DebugLevel)
9697
flag.Parse()
9798

99+
if integrationTest {
100+
}
101+
98102
// init log
99103
initLogs()
100104

consistent/consistent.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ func InitConsistent(storePath string, persistImpl Persistence, initBP bool) (c *
8181
// Load BlockProducer public key, set it in public key store
8282
// as all kms.BP stuff is initialized on kms init()
8383
BPNode = &proto.Node{
84-
ID: kms.BPNodeID,
84+
ID: kms.BP.NodeID,
8585
Addr: "",
86-
PublicKey: kms.BPPublicKey,
87-
Nonce: kms.BPNonce,
86+
PublicKey: kms.BP.PublicKey,
87+
Nonce: kms.BP.Nonce,
8888
}
8989
}
9090

crypto/kms/pubkeystore.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package kms
1818

1919
import (
20+
"bytes"
2021
"encoding/hex"
2122
"errors"
22-
2323
"sync"
2424

25-
"bytes"
26-
2725
log "github.com/sirupsen/logrus"
2826

2927
"github.com/coreos/bbolt"
@@ -40,6 +38,20 @@ type PublicKeyStore struct {
4038
bucket []byte
4139
}
4240

41+
// BPInfo hold all BP info fields
42+
type BPInfo struct {
43+
// PublicKeyStr is the public key of Block Producer
44+
PublicKeyStr string
45+
// PublicKey point to BlockProducer public key
46+
PublicKey *asymmetric.PublicKey
47+
// NodeID is the node id of Block Producer
48+
NodeID proto.NodeID
49+
// RawNodeID
50+
RawNodeID proto.RawNodeID
51+
// Nonce is the nonce, SEE: cmd/idminer for more
52+
Nonce mine.Uint256
53+
}
54+
4355
const (
4456
// kmsBucketName is the boltdb bucket name
4557
kmsBucketName = "kms"
@@ -57,37 +69,33 @@ var (
5769
var (
5870
//TODO(auxten): maybe each BP uses distinct key pair is safer
5971

60-
// BPPublicKeyStr is the public key of Block Producer
61-
BPPublicKeyStr = "02c1db96f2ba7e1cb4e9822d12de0f63f" +
62-
"b666feb828c7f509e81fab9bd7a34039c"
63-
// BPNodeID is the node id of Block Producer
64-
// {{14396347928 0 0 6148914694092305796} 45 00000000000589366268c274fdc11ec8bdb17e668d2f619555a2e9c1a29c91d8}
65-
BPNodeID = proto.NodeID("00000000000589366268c274fdc11ec8bdb17e668d2f619555a2e9c1a29c91d8")
66-
// BPRawNodeID hold the binary hash version of BPNodeID, will be initialized
67-
// at init()
68-
BPRawNodeID proto.RawNodeID
69-
// BPNonce is the nonce, SEE: cmd/idminer for more
70-
BPNonce = mine.Uint256{
71-
14396347928,
72-
0,
73-
0,
74-
6148914694092305796,
72+
// BP hold the initial BP info
73+
BP = &BPInfo{
74+
PublicKeyStr: "02c1db96f2ba7e1cb4e9822d12de0f63fb666feb828c7f509e81fab9bd7a34039c",
75+
PublicKey: nil,
76+
// {{14396347928 0 0 6148914694092305796} 45 00000000000589366268c274fdc11ec8bdb17e668d2f619555a2e9c1a29c91d8}
77+
NodeID: proto.NodeID("00000000000589366268c274fdc11ec8bdb17e668d2f619555a2e9c1a29c91d8"),
78+
RawNodeID: proto.RawNodeID{},
79+
Nonce: mine.Uint256{
80+
14396347928,
81+
0,
82+
0,
83+
6148914694092305796,
84+
},
7585
}
76-
// BPPublicKey point to BlockProducer public key
77-
BPPublicKey *asymmetric.PublicKey
7886
)
7987

8088
func init() {
81-
err := hash.Decode(&BPRawNodeID.Hash, string(BPNodeID))
89+
err := hash.Decode(&BP.RawNodeID.Hash, string(BP.NodeID))
8290
if err != nil {
83-
log.Fatalf("BPNodeID error: %s", err)
91+
log.Fatalf("BP.NodeID error: %s", err)
8492
}
8593

86-
publicKeyBytes, err := hex.DecodeString(BPPublicKeyStr)
94+
publicKeyBytes, err := hex.DecodeString(BP.PublicKeyStr)
8795
if err != nil {
8896
log.Fatalf("hex decode BPPublicKeyStr error: %s", err)
8997
}
90-
BPPublicKey, err = asymmetric.ParsePubKey(publicKeyBytes)
98+
BP.PublicKey, err = asymmetric.ParsePubKey(publicKeyBytes)
9199
if err != nil {
92100
log.Fatalf("parse publicKeyBytes error: %s", err)
93101
}
@@ -109,6 +117,9 @@ var (
109117
// InitPublicKeyStore opens a db file, if not exist, creates it.
110118
// and creates a bucket if not exist
111119
func InitPublicKeyStore(dbPath string, initNode *proto.Node) (err error) {
120+
//testFlag := flag.Lookup("test")
121+
//log.Debugf("%#v %#v", testFlag, testFlag.Value)
122+
112123
var bdb *bolt.DB
113124
bdb, err = bolt.Open(dbPath, 0600, nil)
114125
if err != nil {

crypto/kms/pubkeystore_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ func TestDB(t *testing.T) {
5555
PublicKey: pubKey2,
5656
Nonce: cpuminer.Uint256{},
5757
}
58-
publicKeyBytes, _ := hex.DecodeString(BPPublicKeyStr)
59-
BPPublicKey, _ = asymmetric.ParsePubKey(publicKeyBytes)
58+
publicKeyBytes, _ := hex.DecodeString(BP.PublicKeyStr)
59+
BP.PublicKey, _ = asymmetric.ParsePubKey(publicKeyBytes)
6060
BPNode := &proto.Node{
61-
ID: BPNodeID,
61+
ID: BP.NodeID,
6262
Addr: "",
63-
PublicKey: BPPublicKey,
64-
Nonce: BPNonce,
63+
PublicKey: BP.PublicKey,
64+
Nonce: BP.Nonce,
6565
}
6666

6767
Convey("Init db", t, func() {
@@ -71,12 +71,12 @@ func TestDB(t *testing.T) {
7171
InitPublicKeyStore(dbFile, BPNode)
7272
So(pks.bucket, ShouldNotBeNil)
7373

74-
nodeInfo, err := GetNodeInfo(BPNodeID)
74+
nodeInfo, err := GetNodeInfo(BP.NodeID)
7575
log.Debugf("nodeInfo %v", nodeInfo)
76-
pubk, err := GetPublicKey(BPNodeID)
76+
pubk, err := GetPublicKey(BP.NodeID)
7777
So(pubk, ShouldNotBeNil)
7878
So(err, ShouldBeNil)
79-
So(pubk.IsEqual(BPPublicKey), ShouldBeTrue)
79+
So(pubk.IsEqual(BP.PublicKey), ShouldBeTrue)
8080

8181
pubk, err = GetPublicKey(proto.NodeID("not exist"))
8282
So(pubk, ShouldBeNil)
@@ -91,13 +91,13 @@ func TestDB(t *testing.T) {
9191
err = setNode(node2)
9292
So(err, ShouldBeNil)
9393

94-
err = SetPublicKey(BPNodeID, BPNonce, BPPublicKey)
94+
err = SetPublicKey(BP.NodeID, BP.Nonce, BP.PublicKey)
9595
So(err, ShouldBeNil)
9696

97-
err = SetPublicKey(BPNodeID, cpuminer.Uint256{}, BPPublicKey)
97+
err = SetPublicKey(BP.NodeID, cpuminer.Uint256{}, BP.PublicKey)
9898
So(err, ShouldEqual, ErrNodeIDKeyNonceNotMatch)
9999

100-
err = SetPublicKey(proto.NodeID("0"+BPNodeID), BPNonce, BPPublicKey)
100+
err = SetPublicKey(proto.NodeID("0"+BP.NodeID), BP.Nonce, BP.PublicKey)
101101
So(err, ShouldEqual, ErrNotValidNodeID)
102102

103103
pubk, err = GetPublicKey(proto.NodeID("node1"))
@@ -115,7 +115,7 @@ func TestDB(t *testing.T) {
115115
So(IDs, ShouldHaveLength, 3)
116116
So(IDs, ShouldContain, proto.NodeID("node1"))
117117
So(IDs, ShouldContain, proto.NodeID("node2"))
118-
So(IDs, ShouldContain, BPNodeID)
118+
So(IDs, ShouldContain, BP.NodeID)
119119

120120
err = DelNode(proto.NodeID("node2"))
121121
So(err, ShouldBeNil)

route/dns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func IsBPNodeID(id *proto.RawNodeID) bool {
6565
if id == nil {
6666
return false
6767
}
68-
return id.IsEqual(&kms.BPRawNodeID.Hash)
68+
return id.IsEqual(&kms.BP.RawNodeID.Hash)
6969
}
7070

7171
// InitResolveCache init Resolver.cache by a new map

rpc/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ type Client struct {
3838
}
3939

4040
var (
41-
YamuxConfig *yamux.Config
41+
// YamuxConfig holds the default Yamux config
42+
YamuxConfig *yamux.Config
43+
// DefaultDialer holds the default dialer of SessionPool
4244
DefaultDialer = dialToNode
4345
)
4446

@@ -98,9 +100,8 @@ func DialToNode(nodeID proto.NodeID, pool *SessionPool) (conn net.Conn, err erro
98100
log.Errorf("open new session failed", err)
99101
}
100102
return
101-
} else {
102-
return pool.Get(nodeID)
103103
}
104+
return pool.Get(nodeID)
104105
}
105106

106107
// dialToNode connects to the node with nodeID
@@ -114,7 +115,7 @@ func dialToNode(nodeID proto.NodeID) (conn net.Conn, err error) {
114115
}
115116

116117
if route.IsBPNodeID(rawNodeID) {
117-
nodePublicKey = kms.BPPublicKey
118+
nodePublicKey = kms.BP.PublicKey
118119
} else {
119120
nodePublicKey, err = kms.GetPublicKey(nodeID)
120121
if err != nil {

rpc/client_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ func TestDialToNode(t *testing.T) {
7474
defer os.Remove(publicKeyStore)
7575
defer os.Remove(privateKey)
7676
kms.InitLocalKeyStore()
77-
c, err := DialToNode(kms.BPNodeID, nil)
77+
c, err := DialToNode(kms.BP.NodeID, nil)
7878
So(c, ShouldBeNil)
7979
So(err, ShouldNotBeNil)
8080

81-
publicKeyBytes, _ := hex.DecodeString(kms.BPPublicKeyStr)
82-
kms.BPPublicKey, _ = asymmetric.ParsePubKey(publicKeyBytes)
81+
publicKeyBytes, _ := hex.DecodeString(kms.BP.PublicKeyStr)
82+
kms.BP.PublicKey, _ = asymmetric.ParsePubKey(publicKeyBytes)
8383
BPNode := &proto.Node{
84-
ID: kms.BPNodeID,
84+
ID: kms.BP.NodeID,
8585
Addr: "",
86-
PublicKey: kms.BPPublicKey,
87-
Nonce: kms.BPNonce,
86+
PublicKey: kms.BP.PublicKey,
87+
Nonce: kms.BP.Nonce,
8888
}
8989

9090
kms.InitPublicKeyStore(publicKeyStore, BPNode)
@@ -94,14 +94,14 @@ func TestDialToNode(t *testing.T) {
9494

9595
kms.InitLocalKeyPair(privateKey, []byte(pass))
9696
route.InitResolver()
97-
c, err = DialToNode(kms.BPNodeID, nil)
97+
c, err = DialToNode(kms.BP.NodeID, nil)
9898
So(c, ShouldBeNil)
9999
So(err, ShouldNotBeNil)
100100

101101
l, _ := net.Listen("tcp", "127.0.0.1:0")
102102

103-
route.SetNodeAddr(&kms.BPRawNodeID, l.Addr().String())
104-
c, err = DialToNode(kms.BPNodeID, nil)
103+
route.SetNodeAddr(&kms.BP.RawNodeID, l.Addr().String())
104+
c, err = DialToNode(kms.BP.NodeID, nil)
105105
So(c, ShouldNotBeNil)
106106
So(err, ShouldBeNil)
107107
})

rpc/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func TestEncPingFindValue(t *testing.T) {
256256
Convey("test FindValue", t, func() {
257257
So(nodeIDList, ShouldContain, string(node1.ID))
258258
So(nodeIDList, ShouldContain, string(node2.ID))
259-
So(nodeIDList, ShouldContain, string(kms.BPNodeID))
259+
So(nodeIDList, ShouldContain, string(kms.BP.NodeID))
260260
})
261261
client.Close()
262262
server.Stop()

0 commit comments

Comments
 (0)