7 "github.com/bytom/bytom/config"
8 dbm "github.com/bytom/bytom/database/leveldb"
9 "github.com/bytom/bytom/database/storage"
10 "github.com/bytom/bytom/protocol"
11 "github.com/bytom/bytom/protocol/bc"
12 "github.com/bytom/bytom/protocol/bc/types"
13 "github.com/bytom/bytom/protocol/state"
14 "github.com/bytom/bytom/testutil"
17 func TestLoadBlockIndex(t *testing.T) {
18 defer os.RemoveAll("temp")
19 testDB := dbm.NewDB("testdb", "leveldb", "temp")
20 store := NewStore(testDB)
22 block := config.GenesisBlock()
23 if err := store.SaveBlock(block); err != nil {
27 for block.Height <= 128 {
28 preHash := block.Hash()
29 block.PreviousBlockHash = preHash
31 if err := store.SaveBlock(block); err != nil {
35 if block.Height%32 != 0 {
39 for i := uint64(0); i < block.Height/32; i++ {
40 if err := store.SaveBlock(block); err != nil {
46 if _, err := store.LoadBlockIndex(128); err != nil {
51 func TestLoadBlockIndexBestHeight(t *testing.T) {
53 blockBestHeight uint64
54 stateBestHeight uint64
70 defer os.RemoveAll("temp")
71 testDB := dbm.NewDB("testdb", "leveldb", "temp")
72 store := NewStore(testDB)
73 var savedBlocks []types.Block
75 for _, c := range cases {
76 block := config.GenesisBlock()
77 for i := uint64(0); i < c.blockBestHeight; i++ {
78 if err := store.SaveBlock(block); err != nil {
82 savedBlocks = append(savedBlocks, *block)
83 block.PreviousBlockHash = block.Hash()
87 index, err := store.LoadBlockIndex(c.stateBestHeight)
92 for _, block := range savedBlocks {
93 blockHash := block.Hash()
94 if existIndex := index.BlockExist(&blockHash); (block.Height <= c.stateBestHeight) != existIndex {
95 t.Errorf("Error in load block index")
101 func TestLoadBlockIndexEquals(t *testing.T) {
102 defer os.RemoveAll("temp")
103 testDB := dbm.NewDB("testdb", "leveldb", "temp")
104 store := NewStore(testDB)
106 block := config.GenesisBlock()
107 expectBlockIndex := state.NewBlockIndex()
108 var parent *state.BlockNode
110 for block.Height <= 100 {
111 if err := store.SaveBlock(block); err != nil {
115 if block.Height != 0 {
116 parent = expectBlockIndex.GetNode(&block.PreviousBlockHash)
119 node, err := state.NewBlockNode(&block.BlockHeader, parent)
124 expectBlockIndex.AddNode(node)
125 block.PreviousBlockHash = block.Hash()
129 index, err := store.LoadBlockIndex(100)
134 if !testutil.DeepEqual(expectBlockIndex, index) {
135 t.Errorf("got block index:%v, expect block index:%v", index, expectBlockIndex)
139 func TestSaveChainStatus(t *testing.T) {
140 defer os.RemoveAll("temp")
141 testDB := dbm.NewDB("testdb", "leveldb", "temp")
142 store := NewStore(testDB)
144 node := &state.BlockNode{Height: 100, Hash: bc.Hash{V0: 0, V1: 1, V2: 2, V3: 3}}
145 view := &state.UtxoViewpoint{
146 Entries: map[bc.Hash]*storage.UtxoEntry{
147 bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: false, BlockHeight: 100, Spent: false},
148 bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: true, BlockHeight: 100, Spent: true},
149 bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: false, BlockHeight: 100, Spent: true},
153 contractView := state.NewContractViewpoint()
154 if err := store.SaveChainStatus(node, view, contractView, 0, &bc.Hash{}); err != nil {
158 expectStatus := &protocol.BlockStoreState{Height: node.Height, Hash: &node.Hash, FinalizedHeight: 0, FinalizedHash: &bc.Hash{}}
159 if !testutil.DeepEqual(store.GetStoreStatus(), expectStatus) {
160 t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectStatus)
163 for hash, utxo := range view.Entries {
164 if utxo.Spent && !utxo.IsCoinBase {
168 gotUtxo, err := store.GetUtxo(&hash)
173 if !testutil.DeepEqual(utxo, gotUtxo) {
174 t.Errorf("got utxo entry:%v, expect utxo entry:%v", gotUtxo, utxo)
179 func TestSaveBlock(t *testing.T) {
180 testDB := dbm.NewDB("testdb", "leveldb", "temp")
186 store := NewStore(testDB)
187 block := config.GenesisBlock()
188 if err := store.SaveBlock(block); err != nil {
192 blockHash := block.Hash()
193 gotBlock, err := store.GetBlock(&blockHash)
198 gotBlock.Transactions[0].Tx.SerializedSize = 0
199 gotBlock.Transactions[0].SerializedSize = 0
200 if !testutil.DeepEqual(block, gotBlock) {
201 t.Errorf("got block:%v, expect block:%v", gotBlock, block)
204 data := store.db.Get(CalcBlockHeaderIndexKey(block.Height, &blockHash))
205 gotBlockHeader := types.BlockHeader{}
206 if err := gotBlockHeader.UnmarshalText(data); err != nil {
210 if !testutil.DeepEqual(block.BlockHeader, gotBlockHeader) {
211 t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader)