github.com/turingchain2020/turingchain@v1.1.21/blockchain/export_block_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package blockchain_test 6 7 import ( 8 "os" 9 "testing" 10 "time" 11 12 dbm "github.com/turingchain2020/turingchain/common/db" 13 _ "github.com/turingchain2020/turingchain/system" 14 "github.com/turingchain2020/turingchain/types" 15 "github.com/turingchain2020/turingchain/util/testnode" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func TestExportBlockProc(t *testing.T) { 21 mock33 := testnode.New("", nil) 22 blockchain := mock33.GetBlockChain() 23 chainlog.Debug("TestExportBlockProc begin --------------------") 24 25 //构造1030个区块 26 curheight := blockchain.GetBlockHeight() 27 addblockheight := curheight + 1030 28 var err error 29 cfg := mock33.GetClient().GetConfig() 30 for { 31 _, err = addMainTx(cfg, mock33.GetGenesisKey(), mock33.GetAPI()) 32 require.NoError(t, err) 33 34 curheight = blockchain.GetBlockHeight() 35 _, err = blockchain.GetBlock(curheight) 36 require.NoError(t, err) 37 if curheight >= addblockheight { 38 break 39 } 40 time.Sleep(sendTxWait) 41 } 42 43 //导出block信息,startheight从1开始 44 title := mock33.GetCfg().Title 45 driver := mock33.GetCfg().BlockChain.Driver 46 dbPath := "" 47 48 //异常测试 49 err = blockchain.ExportBlock(title, dbPath, 10000) 50 assert.Equal(t, err, types.ErrInvalidParam) 51 52 err = blockchain.ExportBlock(title, dbPath, 0) 53 require.NoError(t, err) 54 //读取文件头信息 55 db := dbm.NewDB(title, driver, dbPath, 4) 56 headertitle, err := db.Get([]byte("F:header:")) 57 require.NoError(t, err) 58 59 var fileHeader types.FileHeader 60 err = types.Decode(headertitle, &fileHeader) 61 require.NoError(t, err) 62 assert.Equal(t, driver, fileHeader.Driver) 63 assert.Equal(t, int64(0), fileHeader.GetStartHeight()) 64 assert.Equal(t, title, fileHeader.GetTitle()) 65 db.Close() 66 mock33.Close() 67 68 testImportBlockProc(t) 69 70 //异常测试 71 err = blockchain.ExportBlock(title, dbPath, -1) 72 assert.Equal(t, err, types.ErrInvalidParam) 73 74 err = blockchain.ExportBlock("test", dbPath, 0) 75 assert.Equal(t, err, types.ErrInvalidParam) 76 77 chainlog.Debug("TestExportBlock end --------------------") 78 } 79 80 func testImportBlockProc(t *testing.T) { 81 mock33 := testnode.New("", nil) 82 defer mock33.Close() 83 blockchain := mock33.GetBlockChain() 84 chainlog.Debug("TestImportBlockProc begin --------------------") 85 title := mock33.GetCfg().Title 86 dbPath := "" 87 driver := mock33.GetCfg().BlockChain.Driver 88 89 //读取文件头信息 90 db := dbm.NewDB(title, driver, dbPath, 4) 91 var endBlock types.EndBlock 92 endHeight, err := db.Get([]byte("F:endblock:")) 93 require.NoError(t, err) 94 err = types.Decode(endHeight, &endBlock) 95 require.NoError(t, err) 96 db.Close() 97 98 err = blockchain.ImportBlock(title, dbPath) 99 require.NoError(t, err) 100 curHeader, err := blockchain.ProcGetLastHeaderMsg() 101 require.NoError(t, err) 102 assert.Equal(t, curHeader.Height, endBlock.GetHeight()) 103 assert.Equal(t, curHeader.GetHash(), endBlock.GetHash()) 104 file := title + ".db" 105 os.RemoveAll(file) 106 107 chainlog.Debug("TestImportBlockProc end --------------------") 108 }