github.com/turingchain2020/turingchain@v1.1.21/blockchain/blocktable_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 "testing" 9 "time" 10 11 "github.com/turingchain2020/turingchain/blockchain" 12 "github.com/turingchain2020/turingchain/common/merkle" 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 TestBlockTable(t *testing.T) { 21 cfg := testnode.GetDefaultConfig() 22 cfg.GetModuleConfig().BlockChain.RollbackBlock = 5 23 mock33 := testnode.NewWithConfig(cfg, nil) 24 defer mock33.Close() 25 cfg = mock33.GetClient().GetConfig() 26 blockchain := mock33.GetBlockChain() 27 chainlog.Info("TestBlockTable begin --------------------") 28 29 //构造十个区块 30 curheight := blockchain.GetBlockHeight() 31 addblockheight := curheight + 10 32 33 _, err := blockchain.GetBlock(curheight) 34 if err != nil { 35 require.NoError(t, err) 36 } 37 38 for { 39 _, err = addSingleParaTx(cfg, mock33.GetGenesisKey(), mock33.GetAPI(), "user.p.hyb.none") 40 require.NoError(t, err) 41 42 _, _, err = addGroupParaTx(cfg, mock33.GetGenesisKey(), mock33.GetAPI(), "user.p.hyb.", false) 43 require.NoError(t, err) 44 45 curheight = blockchain.GetBlockHeight() 46 _, err = blockchain.GetBlock(curheight) 47 require.NoError(t, err) 48 if curheight >= addblockheight { 49 break 50 } 51 time.Sleep(sendTxWait) 52 } 53 time.Sleep(sendTxWait * 2) 54 testBlockTable(cfg, t, blockchain) 55 //del测试 56 blockchain.Rollback() 57 testBlockTable(cfg, t, blockchain) 58 } 59 60 func testBlockTable(cfg *types.TuringchainConfig, t *testing.T, blockchain *blockchain.BlockChain) { 61 curheight := blockchain.GetBlockHeight() 62 63 //通过当前高度获取header 64 header, err := blockchain.GetStore().GetBlockHeaderByHeight(curheight) 65 require.NoError(t, err) 66 67 //通过当前高度获取block 68 block, err := blockchain.GetStore().LoadBlock(curheight, nil) 69 require.NoError(t, err) 70 71 assert.Equal(t, header.GetHash(), block.Block.Hash(cfg)) 72 assert.Equal(t, header.GetHash(), block.Block.MainHash) 73 assert.Equal(t, curheight, block.Block.MainHeight) 74 75 //通过当前高度+hash获取block 76 block1, err := blockchain.GetStore().LoadBlockByHash(header.GetHash()) 77 require.NoError(t, err) 78 assert.Equal(t, header.GetHash(), block1.Block.Hash(cfg)) 79 assert.Equal(t, header.GetHash(), block1.Block.MainHash) 80 assert.Equal(t, curheight, block1.Block.MainHeight) 81 82 //获取当前高度上的所有平行链title 83 replyparaTxs, err := blockchain.LoadParaTxByHeight(curheight, "", 0, 0) 84 require.NoError(t, err) 85 for _, paratx := range replyparaTxs.Items { 86 assert.Equal(t, paratx.Height, curheight) 87 assert.Equal(t, paratx.Hash, header.GetHash()) 88 if paratx.Title != "user.p.hyb." && paratx.Title != "user.p.fzm." { 89 t.Error("testBlockTable:Title:fail!") 90 } 91 } 92 93 var req types.ReqHeightByTitle 94 req.Height = -1 95 req.Title = "user.p.hyb." 96 req.Count = 0 97 req.Direction = 0 98 99 //获取平行链title="user.p.hyb."对应的区块高度 100 paraTxs, err := blockchain.LoadParaTxByTitle(&req) 101 require.NoError(t, err) 102 assert.Equal(t, paraTxs.Title, "user.p.hyb.") 103 104 //获取平行链title对应的区块高度,向前翻 105 startheight := curheight - 2 106 req.Height = startheight 107 paraTxs, err = blockchain.LoadParaTxByTitle(&req) 108 require.NoError(t, err) 109 count := 0 110 var req1 types.ReqParaTxByHeight 111 req1.Title = req.Title 112 assert.Equal(t, paraTxs.Title, "user.p.hyb.") 113 114 for _, paratx := range paraTxs.Items { 115 count++ 116 req1.Items = append(req1.Items, paratx.Height) 117 } 118 assert.Equal(t, int64(count), startheight-1) 119 120 t.Log(paraTxs) 121 t.Log(req1) 122 //通过title+heightList获取对应平行链的交易信息 123 paraChainTxs, err := blockchain.GetParaTxByHeight(&req1) 124 require.NoError(t, err) 125 for index, pChainTx := range paraChainTxs.Items { 126 assert.Equal(t, pChainTx.Header.Height, paraTxs.Items[index].Height) 127 assert.Equal(t, pChainTx.Header.Hash, paraTxs.Items[index].Hash) 128 blockheight := pChainTx.Header.Height 129 //子roothash的proof证明验证 130 var hashes [][]byte 131 for _, tx := range pChainTx.GetTxDetails() { 132 if cfg.IsFork(blockheight, "ForkRootHash") { 133 hashes = append(hashes, tx.GetTx().FullHash()) 134 } else { 135 hashes = append(hashes, tx.GetTx().Hash()) 136 } 137 } 138 childHash := merkle.GetMerkleRoot(hashes) 139 root := merkle.GetMerkleRootFromBranch(pChainTx.GetProofs(), childHash, pChainTx.Index) 140 assert.Equal(t, childHash, root) 141 assert.Equal(t, childHash, pChainTx.ChildHash) 142 count-- 143 } 144 assert.Equal(t, count, 0) 145 146 //获取平行链title对应的区块高度,向后翻 147 req.Direction = 1 148 paraTxs, err = blockchain.LoadParaTxByTitle(&req) 149 require.NoError(t, err) 150 assert.Equal(t, paraTxs.Title, "user.p.hyb.") 151 count = len(paraTxs.Items) 152 153 if count < 2 { 154 t.Error("testBlockTable:Title:fail!") 155 } 156 //异常测试 157 _, err = blockchain.LoadParaTxByTitle(nil) 158 assert.Equal(t, types.ErrInvalidParam, err) 159 160 req.Count = 100000 161 _, err = blockchain.LoadParaTxByTitle(nil) 162 assert.Equal(t, types.ErrInvalidParam, err) 163 164 req.Count = 0 165 req.Direction = 3 166 _, err = blockchain.LoadParaTxByTitle(nil) 167 assert.Equal(t, types.ErrInvalidParam, err) 168 169 req.Count = 0 170 req.Direction = 0 171 req.Title = "" 172 _, err = blockchain.LoadParaTxByTitle(nil) 173 assert.Equal(t, types.ErrInvalidParam, err) 174 175 req.Count = 0 176 req.Direction = 0 177 req.Title = "user.write" 178 _, err = blockchain.LoadParaTxByTitle(nil) 179 assert.Equal(t, types.ErrInvalidParam, err) 180 181 //GetParaTxByHeight入参检测test 182 _, err = blockchain.GetParaTxByHeight(nil) 183 assert.Equal(t, types.ErrInvalidParam, err) 184 185 var reqPara types.ReqParaTxByHeight 186 reqPara.Title = "user.write" 187 reqPara.Items = append(reqPara.Items, 1) 188 _, err = blockchain.GetParaTxByHeight(&reqPara) 189 assert.Equal(t, types.ErrInvalidParam, err) 190 191 reqPara.Title = "user.p.hyb." 192 reqPara.Items = append(reqPara.Items, 2) 193 reqPara.Items[0] = -1 194 _, err = blockchain.GetParaTxByHeight(&reqPara) 195 assert.Equal(t, types.ErrInvalidParam, err) 196 197 for i := 0; i < 10002; i++ { 198 reqPara.Items = append(reqPara.Items, int64(i)) 199 } 200 _, err = blockchain.GetParaTxByHeight(&reqPara) 201 assert.Equal(t, types.ErrInvalidParam, err) 202 }