/
xchainmg_test.go
executable file
·461 lines (444 loc) · 14.5 KB
/
xchainmg_test.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package xchaincore
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"io/ioutil"
"math/big"
"net"
"os"
"os/exec"
"testing"
"time"
"github.com/golang/protobuf/proto"
log "github.com/xuperchain/log15"
"github.com/xuperchain/xuperunion/common/config"
"github.com/xuperchain/xuperunion/global"
"github.com/xuperchain/xuperunion/p2pv2"
xuper_p2p "github.com/xuperchain/xuperunion/p2pv2/pb"
"github.com/xuperchain/xuperunion/pb"
"github.com/xuperchain/xuperunion/contract/kernel"
crypto_client "github.com/xuperchain/xuperunion/crypto/client"
"github.com/xuperchain/xuperunion/ledger"
"github.com/xuperchain/xuperunion/utxo"
)
const BobAddress = "dpzuVdosQrF2kmzumhVeFQZa1aYcdgFpN"
const BobPubkey = `{"Curvname":"P-256","X":74695617477160058757747208220371236837474210247114418775262229497812962582435,"Y":51348715319124770392993866417088542497927816017012182211244120852620959209571}`
const BobPrivateKey = `{"Curvname":"P-256","X":74695617477160058757747208220371236837474210247114418775262229497812962582435,"Y":51348715319124770392993866417088542497927816017012182211244120852620959209571,"D":29079635126530934056640915735344231956621504557963207107451663058887647996601}`
const DefaultKvEngine = "leveldb"
var baseDir = os.Getenv("XCHAIN_ROOT")
func Init(t *testing.T) *XChainMG {
logger := log.New("module", "xchain")
logger.SetHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
cfg := config.NewNodeConfig()
l, _ := net.Listen("tcp", ":0")
port := l.Addr().(*net.TCPAddr).Port
l.Close()
t.Log("port: ", port)
cfg.P2pV2.Port = int32(port)
p2pV2Service, p2pV2Err := p2pv2.NewP2PServerV2(cfg.P2pV2, nil)
t.Log("cfg: ", cfg.P2pV2)
if p2pV2Err != nil {
t.Error("new p2pv2 server error ", p2pV2Err.Error())
}
xcmg := &XChainMG{}
cfg.Datapath = "../core/data/blockchain"
if err := xcmg.Init(logger, cfg, p2pV2Service); err != nil {
t.Error("XChainMG init error ", err.Error())
}
return xcmg
}
func TestXChainMgBasic(t *testing.T) {
/*
c := exec.Command("sh", "-c", "../xchain-cli netUrl gen")
_, cmdErr := c.Output()
if cmdErr != nil {
t.Error("netUrl gen error ", cmdErr.Error())
}
*/
InitCreateBlockChain(t)
xcmg := Init(t)
defer func() {
if xcmg != nil {
xcmg.Stop()
defer os.RemoveAll(fmt.Sprintf("%s/core/data", baseDir))
}
}()
if xcmg == nil {
t.Error("create XChainMG error")
}
// test for Get
rootXCore := xcmg.Get("xuper")
if rootXCore == nil {
t.Error("expect not nil, but got ", rootXCore)
}
xcore := xcmg.Get("Dog")
if xcore != nil {
t.Error("expect nil, but got ", xcore)
}
// test for GetAll
bcs := xcmg.GetAll()
if len(bcs) != 1 {
t.Error("expect size but got ", len(bcs), "expect xuper xchain but got ", bcs[0])
}
// test for Start
xcmg.Start()
// test for CreateBlockChain
// create exist chain
xcore2, xcoreErr := xcmg.CreateBlockChain("xuper", []byte("todo"))
if xcoreErr != ErrBlockChainIsExist {
t.Error("expect ErrBlockChainIsExist, but got ", xcoreErr)
} else {
t.Log("xcore2: ", xcore2)
}
// create non exist chain
rootJs, _ := ioutil.ReadFile(fmt.Sprintf("%s/core/data/config", baseDir) + "/xuper.json")
xcore2, xcoreErr = xcmg.CreateBlockChain("dog", rootJs)
if xcoreErr != nil {
t.Error("create non exist chain error ", xcoreErr.Error())
} else {
defer os.RemoveAll(fmt.Sprintf("%s/core/data/blockchain/dog", baseDir))
t.Log("dog chain ", xcore2)
}
status := rootXCore.Status()
t.Log("xuper chain status ", status)
txReq := &pb.TxData{}
txReq.Bcname = "xuper"
txReq.FromAddr = BobAddress
txReq.FromPubkey = BobPubkey
txReq.FromScrkey = BobPrivateKey
txReq.Nonce = "nonce"
txReq.Timestamp = time.Now().UnixNano()
// tx_req.Desc = []byte("")
txReq.Account = []*pb.TxDataAccount{
{Address: BobAddress, Amount: "1"},
}
txReq.Header = &pb.Header{}
hd := &global.XContext{Timer: global.NewXTimer()}
txStatus := rootXCore.GenerateTx(txReq, hd)
t.Log("tx status ", txStatus)
ret, balErr := rootXCore.GetBalance(BobAddress)
if balErr != nil {
t.Error("get balance error ", balErr.Error())
} else {
t.Log("address ", BobAddress, " balance ", ret)
}
// test for GetFrozenBalance
ret, balErr = rootXCore.GetFrozenBalance(BobAddress)
if balErr != nil {
t.Error("get frozen balance error ", balErr.Error())
} else {
t.Log("address ", BobAddress, " frozen balance ", ret)
}
// test for GetConsType
consType := rootXCore.GetConsType()
if consType != "single" {
t.Error("expect consType is single, but got ", consType)
}
// test for GetDposCandidates
strArr, _ := rootXCore.GetDposCandidates()
if len(strArr) != 0 {
t.Error("expect 0 candidates, but got ", len(strArr))
}
// test for GetDposNominateRecords
strArr2, _ := rootXCore.GetDposNominateRecords(BobAddress)
if len(strArr2) != 0 {
t.Error("expect 0 moninate records, but got ", len(strArr2))
}
strArr3, _ := rootXCore.GetDposNominatedRecords(BobAddress)
if len(strArr3) != 0 {
t.Error("expect 0 moninated records, but got ", len(strArr3))
}
strArr4, _ := rootXCore.GetDposVoteRecords(BobAddress)
if len(strArr4) != 0 {
t.Error("expect 0 vote records, but got ", len(strArr4))
}
strArr5, _ := rootXCore.GetDposVotedRecords(BobAddress)
if len(strArr5) != 0 {
t.Error("expect 0 voted records, but got ", len(strArr4))
}
// test for GetCheckResults
proposers, _ := rootXCore.GetCheckResults(0)
if len(proposers) != 0 {
t.Error("expect 0 proposers, but got ", len(proposers))
}
// test for PostTx -> txStatus
reply, state := rootXCore.PostTx(txStatus, hd)
t.Log("postTx reply ", reply)
t.Log("postTx status ", state)
// test for QueryTx -> txStatus
result := rootXCore.QueryTx(txStatus)
t.Log("query non exist tx result ", result)
// query ok tx
ecdsaPk, pkErr := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if pkErr != nil {
t.Error("generate key error ", pkErr.Error())
}
t.Log("tip blockid ", rootXCore.Ledger.GetMeta().TipBlockid)
t.Log("tx ", txStatus.Tx)
block, formatBlockErr := rootXCore.Ledger.FormatBlock([]*pb.Transaction{txStatus.Tx},
[]byte(BobAddress),
ecdsaPk,
223456789,
0,
0,
rootXCore.Ledger.GetMeta().TipBlockid,
big.NewInt(1))
if formatBlockErr != nil {
t.Error("format block error", formatBlockErr.Error())
}
confirmStatus := rootXCore.Ledger.ConfirmBlock(block, false)
if !confirmStatus.Succ {
t.Error("confirm block error ", confirmStatus)
}
playErr := rootXCore.Utxovm.Play(block.Blockid)
if playErr != nil {
t.Error("utxo play error ", playErr.Error())
}
rootXCore.nodeMode = config.NodeModeFastSync
globalBlock := &pb.Block{
Header: &pb.Header{},
Bcname: "xuper",
Blockid: block.Blockid,
Status: pb.Block_TRUNK,
Block: block,
}
result = rootXCore.QueryTx(txStatus)
t.Log("query exist tx result ", result)
// test for GetNodeMode
nodeMode := rootXCore.GetNodeMode()
if nodeMode != config.NodeModeFastSync {
t.Error("expect FAST_SYNC, but got ", nodeMode)
}
// test for SendBlock
// sendblock exist block
sendBlockErr := rootXCore.SendBlock(globalBlock, hd)
if sendBlockErr != nil && sendBlockErr != ErrBlockExist {
t.Error("send block error ", sendBlockErr.Error())
}
// sendblock non exist block but trunk block
txReq.Nonce = "nonce1"
txReq.Timestamp = time.Now().UnixNano()
txStatus = rootXCore.GenerateTx(txReq, &global.XContext{Timer: global.NewXTimer()})
block, formatBlockErr = rootXCore.Ledger.FormatBlock([]*pb.Transaction{txStatus.Tx},
[]byte(BobAddress),
ecdsaPk,
223456789,
0,
0,
rootXCore.Ledger.GetMeta().TipBlockid,
big.NewInt(1))
if formatBlockErr != nil {
t.Error("format block error", formatBlockErr.Error())
}
globalBlock = &pb.Block{
Header: &pb.Header{},
Bcname: "xuper",
Blockid: block.Blockid,
Status: pb.Block_TRUNK,
Block: block,
}
sendBlockErr = rootXCore.SendBlock(globalBlock, &global.XContext{Timer: global.NewXTimer()})
if sendBlockErr != nil {
t.Error("send block error ", sendBlockErr.Error())
return
}
status = rootXCore.Status()
t.Log("xuper chain status ", status)
// sendblock non exist block and no trunk block
txReq.Nonce = "nonce2"
txReq.Timestamp = time.Now().UnixNano()
txStatus = rootXCore.GenerateTx(txReq, &global.XContext{Timer: global.NewXTimer()})
block, formatBlockErr = rootXCore.Ledger.FormatBlock([]*pb.Transaction{txStatus.Tx},
[]byte(BobAddress),
ecdsaPk,
223456789,
0,
0,
rootXCore.Ledger.GetMeta().TipBlockid,
big.NewInt(1))
if formatBlockErr != nil {
t.Error("format block error", formatBlockErr.Error())
}
globalBlock = &pb.Block{
Header: &pb.Header{},
Bcname: "xuper",
Blockid: block.Blockid,
Status: pb.Block_TRUNK,
Block: block,
}
// save in pending table
saveErr := rootXCore.Ledger.SavePendingBlock(globalBlock)
if saveErr != nil {
t.Error("save error ", saveErr.Error())
}
txReq.Nonce = "nonce3"
txReq.Timestamp = time.Now().UnixNano()
txStatus = rootXCore.GenerateTx(txReq, &global.XContext{Timer: global.NewXTimer()})
block2, formatBlockErr2 := rootXCore.Ledger.FormatBlock([]*pb.Transaction{txStatus.Tx},
[]byte(BobAddress),
ecdsaPk,
223456789,
0,
0,
globalBlock.Block.Blockid,
big.NewInt(1))
if formatBlockErr2 != nil {
t.Error("format block error", formatBlockErr.Error())
}
globalBlock = &pb.Block{
Header: &pb.Header{},
Bcname: "xuper",
Blockid: block2.Blockid,
Status: pb.Block_TRUNK,
Block: block2,
}
sendBlockErr = rootXCore.SendBlock(globalBlock, &global.XContext{Timer: global.NewXTimer()})
if sendBlockErr != nil {
t.Error("send block error ", sendBlockErr.Error())
}
// test for GetBlock
// get exist block
blockID := &pb.BlockID{
Header: &pb.Header{},
Bcname: "xuper",
Blockid: globalBlock.Blockid,
NeedContent: true,
}
existBlock := rootXCore.GetBlock(blockID)
if existBlock == nil {
t.Error("expect no nil, but got ", existBlock)
}
// get non exist block
blockID = &pb.BlockID{
Header: &pb.Header{},
Bcname: "dog",
Blockid: []byte("123456"),
NeedContent: true,
}
existBlock = rootXCore.GetBlock(blockID)
if existBlock.Status != pb.Block_NOEXIST {
t.Error("expect NOEXIST but got ", existBlock.Status)
}
// test for GetBlockChainStatus
blkStatus := &pb.BCStatus{
Header: &pb.Header{},
Bcname: "xuper",
}
out := rootXCore.GetBlockChainStatus(blkStatus)
t.Log("out: ", out)
out2 := rootXCore.ConfirmTipBlockChainStatus(blkStatus)
t.Log("out: ", out2)
// test for countGetBlockChainStatus
res, _ := xuper_p2p.NewXuperMessage(xuper_p2p.XuperMsgVersion2, "xuper", "123456789",
xuper_p2p.XuperMessage_GET_BLOCK_RES, nil, xuper_p2p.XuperMessage_CHECK_SUM_ERROR)
countGetBlockChainStatus([]*xuper_p2p.XuperMessage{res})
// test for countConfirmBlockRes
t.Log("state value ", countConfirmBlockRes([]*xuper_p2p.XuperMessage{res}))
t.Log("is accepted: ", rootXCore.syncConfirm(blkStatus))
res2, _ := rootXCore.syncForOnce()
t.Log("sync for once", res2)
rootXCore.doMiner()
// test fot BroadCastGetBlock
rootXCore.BroadCastGetBlock(blockID)
// test for xchainmg_net.go
// assemble POSTTX,SENDBLOCK,BATCHPOSTTX,
msgInfo, _ := proto.Marshal(txStatus)
t.Log("msg info ", msgInfo)
msgTmp, _ := xuper_p2p.NewXuperMessage(xuper_p2p.XuperMsgVersion1, "xuper", "123456", xuper_p2p.XuperMessage_POSTTX,
msgInfo, xuper_p2p.XuperMessage_NONE)
xcmg.msgChan <- msgTmp
//batch BatchPostTx
batchTxs := &pb.BatchTxs{
Header: &pb.Header{},
Txs: []*pb.TxStatus{txStatus},
}
t.Log("batchTxs: ", batchTxs)
msgInfos, _ := proto.Marshal(batchTxs)
msgsTmp, _ := xuper_p2p.NewXuperMessage(xuper_p2p.XuperMsgVersion1, "xuper", "123457", xuper_p2p.XuperMessage_BATCHPOSTTX,
msgInfos, xuper_p2p.XuperMessage_NONE)
xcmg.msgChan <- msgsTmp
sendBlockMsgInfo, _ := proto.Marshal(globalBlock)
sendBlockMsgTmp, _ := xuper_p2p.NewXuperMessage(xuper_p2p.XuperMsgVersion1, "xuper", "123458", xuper_p2p.XuperMessage_SENDBLOCK,
sendBlockMsgInfo, xuper_p2p.XuperMessage_NONE)
xcmg.msgChan <- sendBlockMsgTmp
<-time.After(3 * time.Second)
ctx := context.Background()
xcmg.handleGetBlock(ctx, sendBlockMsgTmp)
blkStatusMsg, _ := proto.Marshal(blkStatus)
blkStatusTmp, _ := xuper_p2p.NewXuperMessage(xuper_p2p.XuperMsgVersion1, "xuper", "123459", xuper_p2p.XuperMessage_GET_BLOCKCHAINSTATUS,
blkStatusMsg, xuper_p2p.XuperMessage_NONE)
xcmg.msgChan <- blkStatusTmp
xcmg.handleGetBlockChainStatus(ctx, blkStatusTmp)
xcmg.handleConfirmBlockChainStatus(ctx, blkStatusTmp)
xcmg.Stop()
}
func InitCreateBlockChain(t *testing.T) {
//defer os.RemoveAll(workSpace)
workSpace := fmt.Sprintf("%s/core/data/blockchain/xuper", baseDir)
os.RemoveAll(fmt.Sprintf("%s/core/data", baseDir))
cmd := fmt.Sprintf("cp -rf %s/data %s/core/", baseDir, baseDir)
fmt.Println(cmd)
c := exec.Command("sh", "-c", cmd)
_, cmdErr := c.Output()
if cmdErr != nil {
// t.Error("cp error ", cmdErr.Error())
}
os.RemoveAll(fmt.Sprintf("%s/core/data/blockchain/xuper/", baseDir))
ledger, err := ledger.NewLedger(workSpace, nil, nil, DefaultKvEngine, crypto_client.CryptoTypeDefault)
if err != nil {
t.Fatal(err)
}
defer ledger.Close()
kl := &kernel.Kernel{}
kLogger := log.New("module", "kernel")
kLogger.SetHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
kl.Init(workSpace, kLogger, nil, "xuper")
kl.SetNewChainWhiteList(map[string]bool{BobAddress: true})
utxoVM, _ := utxo.MakeUtxoVM("xuper", ledger, workSpace, "", "", []byte(""), nil, 5000, 60, 500, nil, false, DefaultKvEngine, crypto_client.CryptoTypeDefault)
utxoVM.RegisterVM("kernel", kl, global.VMPrivRing0)
//创建链的时候分配财富
tx, err2 := utxoVM.GenerateRootTx([]byte(`
{
"version" : "1"
, "consensus" : {
"type" : "single",
"miner" : "dpzuVdosQrF2kmzumhVeFQZa1aYcdgFpN"
}
, "predistribution":[
{
"address" : "dpzuVdosQrF2kmzumhVeFQZa1aYcdgFpN"
, "quota" : "100000000000000000000"
}
]
, "maxblocksize" : "128"
, "period" : "3000"
, "award" : "428100000000"
, "decimals" : "8"
, "award_decay": {
"height_gap": 31536000,
"ratio": 1
}
}
`))
if err2 != nil {
t.Fatal(err2)
}
defer utxoVM.Close()
block, _ := ledger.FormatRootBlock([]*pb.Transaction{tx})
confirmStatus := ledger.ConfirmBlock(block, true)
if !confirmStatus.Succ {
t.Fatal("confirm block fail")
}
playErr := utxoVM.Play(block.Blockid)
if playErr != nil {
t.Fatal(playErr)
}
c = exec.Command("sh", "-c", fmt.Sprintf("cp %s/core/data/config/xuper.json %s/core/data/blockchain/xuper/", baseDir, baseDir))
_, cmdErr = c.Output()
if cmdErr != nil {
t.Error("cp error ", cmdErr.Error())
}
}