FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CovenantSQL/client/helper_test.go at develop · gridl/CovenantSQL · GitHub
gridl
/
CovenantSQL
Public
forked from
CovenantSQL/CovenantSQL
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
CovenantSQL
/
client
/
helper_test.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
315 lines (269 loc) · 7.66 KB
Breadcrumbs
CovenantSQL
/
client
/
helper_test.go
Copy path
File metadata and controls
315 lines (269 loc) · 7.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2018 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
client
import
(
"database/sql"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sync/atomic"
pi
"github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces"
"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/consistent"
"github.com/CovenantSQL/CovenantSQL/crypto"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/crypto/kms"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/route"
rpc
"github.com/CovenantSQL/CovenantSQL/rpc/mux"
"github.com/CovenantSQL/CovenantSQL/types"
"github.com/CovenantSQL/CovenantSQL/utils"
"github.com/CovenantSQL/CovenantSQL/utils/log"
"github.com/CovenantSQL/CovenantSQL/worker"
)
const
(
// PubKeyStorePath defines public cache store.
PubKeyStorePath
=
"./public.keystore"
)
var
(
rootHash
=
hash.
Hash
{}
stubNextNonce
pi.
AccountNonce
=
1
)
// fake BPDB service.
type
stubBPService
struct
{}
func
(
s
*
stubBPService
)
QueryAccountTokenBalance
(
req
*
types.
QueryAccountTokenBalanceReq
,
resp
*
types.
QueryAccountTokenBalanceResp
) (
err
error
) {
resp
.
OK
=
req
.
TokenType
.
Listed
()
return
}
func
(
s
*
stubBPService
)
QuerySQLChainProfile
(
req
*
types.
QuerySQLChainProfileReq
,
resp
*
types.
QuerySQLChainProfileResp
) (
err
error
) {
var
nodeID
proto.
NodeID
if
nodeID
,
err
=
kms
.
GetLocalNodeID
();
err
!=
nil
{
return
}
resp
.
Profile
=
types.
SQLChainProfile
{
Miners
: []
*
types.
MinerInfo
{
{
NodeID
:
nodeID
,
},
},
}
return
}
func
(
s
*
stubBPService
)
NextAccountNonce
(
_
*
types.
NextAccountNonceReq
,
resp
*
types.
NextAccountNonceResp
) (
err
error
) {
resp
.
Nonce
=
stubNextNonce
return
}
func
(
s
*
stubBPService
)
AddTx
(
req
*
types.
AddTxReq
,
resp
*
types.
AddTxResp
) (
err
error
) {
return
}
func
(
s
*
stubBPService
)
QueryTxState
(
req
*
types.
QueryTxStateReq
,
resp
*
types.
QueryTxStateResp
) (
err
error
,
) {
resp
.
State
=
pi
.
TransactionStateConfirmed
return
}
func
startTestService
() (
stopTestService
func
(),
tempDir
string
,
err
error
) {
var
server
*
rpc.
Server
var
cleanup
func
()
if
cleanup
,
tempDir
,
server
,
err
=
initNode
();
err
!=
nil
{
return
}
var
rootDir
string
if
rootDir
,
err
=
ioutil
.
TempDir
(
""
,
"dbms_test_"
);
err
!=
nil
{
return
}
cfg
:=
&
worker.
DBMSConfig
{
RootDir
:
rootDir
,
Server
:
server
,
MaxReqTimeGap
:
worker
.
DefaultMaxReqTimeGap
,
}
var
dbms
*
worker.
DBMS
if
dbms
,
err
=
worker
.
NewDBMS
(
cfg
);
err
!=
nil
{
return
}
stopTestService
=
func
() {
if
dbms
!=
nil
{
dbms
.
Shutdown
()
}
cleanup
()
// cleanup session pool
rpc
.
GetSessionPoolInstance
().
Close
()
}
// init
if
err
=
dbms
.
Init
();
err
!=
nil
{
return
}
// add database
var
req
*
types.
UpdateService
var
res
types.
UpdateServiceResponse
var
peers
*
proto.
Peers
var
block
*
types.
Block
dbID
:=
proto
.
DatabaseID
(
"db"
)
// create sqlchain block
block
,
err
=
types
.
CreateRandomBlock
(
rootHash
,
true
)
// get database peers
if
peers
,
err
=
genPeers
(
1
);
err
!=
nil
{
return
}
// build create database request
req
=
new
(types.
UpdateService
)
req
.
Header
.
Op
=
types
.
CreateDB
req
.
Header
.
Instance
=
types.
ServiceInstance
{
DatabaseID
:
dbID
,
Peers
:
peers
,
ResourceMeta
: types.
ResourceMeta
{
IsolationLevel
:
int
(
sql
.
LevelReadUncommitted
),
},
GenesisBlock
:
block
,
}
if
req
.
Header
.
Signee
,
err
=
kms
.
GetLocalPublicKey
();
err
!=
nil
{
return
}
var
privateKey
*
asymmetric.
PrivateKey
if
privateKey
,
err
=
kms
.
GetLocalPrivateKey
();
err
!=
nil
{
return
}
if
err
=
req
.
Sign
(
privateKey
);
err
!=
nil
{
return
}
// send create database request
if
err
=
testRequest
(
route
.
DBSDeploy
,
req
,
&
res
);
err
!=
nil
{
return
}
// update private key permission in dbms for query
addr
,
err
:=
crypto
.
PubKeyHash
(
privateKey
.
PubKey
())
if
err
!=
nil
{
return
}
permStat
:=
&
types.
PermStat
{
Permission
:
types
.
UserPermissionFromRole
(
types
.
Admin
),
Status
:
types
.
Normal
,
}
err
=
dbms
.
UpdatePermission
(
dbID
,
proto
.
AccountAddress
(
addr
),
permStat
)
if
err
!=
nil
{
return
}
return
}
func
initNode
() (
cleanupFunc
func
(),
tempDir
string
,
server
*
rpc.
Server
,
err
error
) {
if
tempDir
,
err
=
ioutil
.
TempDir
(
""
,
"db_test_"
);
err
!=
nil
{
return
}
log
.
WithField
(
"d"
,
tempDir
).
Debug
(
"created temp dir"
)
// init conf
_
,
testFile
,
_
,
_
:=
runtime
.
Caller
(
0
)
pubKeyStoreFile
:=
filepath
.
Join
(
tempDir
,
PubKeyStorePath
+
"_dht"
)
utils
.
RemoveAll
(
pubKeyStoreFile
+
"*"
)
clientPubKeyStoreFile
:=
filepath
.
Join
(
tempDir
,
PubKeyStorePath
+
"_c"
)
utils
.
RemoveAll
(
clientPubKeyStoreFile
+
"*"
)
dupConfFile
:=
filepath
.
Join
(
tempDir
,
"config.yaml"
)
confFile
:=
filepath
.
Join
(
filepath
.
Dir
(
testFile
),
"../test/node_standalone/config.yaml"
)
if
err
=
utils
.
DupConf
(
confFile
,
dupConfFile
);
err
!=
nil
{
return
}
privateKeyPath
:=
filepath
.
Join
(
filepath
.
Dir
(
testFile
),
"../test/node_standalone/private.key"
)
conf
.
GConf
,
_
=
conf
.
LoadConfig
(
dupConfFile
)
log
.
Debugf
(
"GConf: %#v"
,
conf
.
GConf
)
_
,
err
=
utils
.
CopyFile
(
privateKeyPath
,
conf
.
GConf
.
PrivateKeyFile
)
if
err
!=
nil
{
log
.
WithFields
(log.
Fields
{
"from"
:
privateKeyPath
,
"to"
:
conf
.
GConf
.
PrivateKeyFile
,
}).
WithError
(
err
).
Fatal
(
"copy private key failed"
)
return
}
// reset the once
route
.
Once
.
Reset
()
route
.
InitKMS
(
clientPubKeyStoreFile
)
var
dht
*
route.
DHTService
// init dht
dht
,
err
=
route
.
NewDHTService
(
pubKeyStoreFile
,
new
(consistent.
KMSStorage
),
true
)
if
err
!=
nil
{
return
}
// init rpc
if
server
,
err
=
rpc
.
NewServerWithService
(rpc.
ServiceMap
{
route
.
DHTRPCName
:
dht
});
err
!=
nil
{
return
}
// register fake chain service
if
err
=
server
.
RegisterService
(
route
.
BlockProducerRPCName
,
&
stubBPService
{});
err
!=
nil
{
return
}
// init private key
masterKey
:=
[]
byte
(
""
)
if
err
=
server
.
InitRPCServer
(
conf
.
GConf
.
ListenAddr
,
privateKeyPath
,
masterKey
);
err
!=
nil
{
return
}
// start server
go
server
.
Serve
()
// fake database init already processed
atomic
.
StoreUint32
(
&
driverInitialized
,
1
)
cleanupFunc
=
func
() {
os
.
RemoveAll
(
tempDir
)
server
.
Listener
.
Close
()
server
.
Stop
()
// restore database init state
atomic
.
StoreUint32
(
&
driverInitialized
,
0
)
kms
.
ResetLocalKeyStore
()
}
return
}
func
testRequest
(
method
route.
RemoteFunc
,
req
interface
{},
response
interface
{}) (
err
error
) {
// get node id
var
nodeID
proto.
NodeID
if
nodeID
,
err
=
kms
.
GetLocalNodeID
();
err
!=
nil
{
return
}
return
rpc
.
NewCaller
().
CallNode
(
nodeID
,
method
.
String
(),
req
,
response
)
}
func
getKeys
() (
privKey
*
asymmetric.
PrivateKey
,
pubKey
*
asymmetric.
PublicKey
,
err
error
) {
// get public key
if
pubKey
,
err
=
kms
.
GetLocalPublicKey
();
err
!=
nil
{
return
}
// get private key
if
privKey
,
err
=
kms
.
GetLocalPrivateKey
();
err
!=
nil
{
return
}
return
}
func
genPeers
(
term
uint64
) (
peers
*
proto.
Peers
,
err
error
) {
// get node id
var
nodeID
proto.
NodeID
if
nodeID
,
err
=
kms
.
GetLocalNodeID
();
err
!=
nil
{
return
}
// get private/public key
var
privateKey
*
asymmetric.
PrivateKey
if
privateKey
,
_
,
err
=
getKeys
();
err
!=
nil
{
return
}
// generate peers and sign
peers
=
&
proto.
Peers
{
PeersHeader
: proto.
PeersHeader
{
Term
:
term
,
Leader
:
nodeID
,
Servers
: []proto.
NodeID
{
nodeID
},
},
}
err
=
peers
.
Sign
(
privateKey
)
return
}
Back
|
FazBrowse Home
|
New Git URL