7 "github.com/vapor/config"
8 dbm "github.com/vapor/database/leveldb"
9 "github.com/vapor/database/storage"
10 "github.com/vapor/protocol"
11 "github.com/vapor/protocol/bc"
12 "github.com/vapor/protocol/bc/types"
13 "github.com/vapor/protocol/state"
14 "github.com/vapor/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 txStatus := bc.NewTransactionStatus()
25 if err := store.SaveBlock(block, txStatus); err != nil {
29 for block.Height <= 128 {
30 preHash := block.Hash()
31 block.PreviousBlockHash = preHash
33 if err := store.SaveBlock(block, txStatus); err != nil {
37 if block.Height%32 != 0 {
41 for i := uint64(0); i < block.Height/32; i++ {
43 if err := store.SaveBlock(block, txStatus); err != nil {
49 if _, err := store.LoadBlockIndex(128); err != nil {
54 func TestLoadBlockIndexBestHeight(t *testing.T) {
56 blockBestHeight uint64
57 stateBestHeight uint64
73 defer os.RemoveAll("temp")
74 testDB := dbm.NewDB("testdb", "leveldb", "temp")
75 store := NewStore(testDB)
76 var savedBlocks []types.Block
78 for _, c := range cases {
79 block := config.GenesisBlock()
80 txStatus := bc.NewTransactionStatus()
82 for i := uint64(0); i < c.blockBestHeight; i++ {
83 if err := store.SaveBlock(block, txStatus); err != nil {
87 savedBlocks = append(savedBlocks, *block)
88 block.PreviousBlockHash = block.Hash()
92 index, err := store.LoadBlockIndex(c.stateBestHeight)
97 for _, block := range savedBlocks {
98 blockHash := block.Hash()
99 if block.Height <= c.stateBestHeight != index.BlockExist(&blockHash) {
100 t.Errorf("Error in load block index")
106 func TestLoadBlockIndexEquals(t *testing.T) {
107 defer os.RemoveAll("temp")
108 testDB := dbm.NewDB("testdb", "leveldb", "temp")
109 store := NewStore(testDB)
111 block := config.GenesisBlock()
112 txStatus := bc.NewTransactionStatus()
113 expectBlockIndex := state.NewBlockIndex()
114 var parent *state.BlockNode
116 for block.Height <= 100 {
117 if err := store.SaveBlock(block, txStatus); err != nil {
121 if block.Height != 0 {
122 parent = expectBlockIndex.GetNode(&block.PreviousBlockHash)
125 node, err := state.NewBlockNode(&block.BlockHeader, parent)
130 expectBlockIndex.AddNode(node)
131 block.PreviousBlockHash = block.Hash()
135 index, err := store.LoadBlockIndex(100)
140 if !testutil.DeepEqual(expectBlockIndex, index) {
141 t.Errorf("got block index:%v, expect block index:%v", index, expectBlockIndex)
144 func TestSaveChainStatus(t *testing.T) {
145 defer os.RemoveAll("temp")
146 testDB := dbm.NewDB("testdb", "leveldb", "temp")
147 store := NewStore(testDB)
149 node := &state.BlockNode{Height: 100, Hash: bc.Hash{V0: 0, V1: 1, V2: 2, V3: 3}}
150 view := &state.UtxoViewpoint{
151 Entries: map[bc.Hash]*storage.UtxoEntry{
152 bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: false, BlockHeight: 100, Spent: false},
153 bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: true, BlockHeight: 100, Spent: true},
154 bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: false, BlockHeight: 100, Spent: true},
158 if err := store.SaveChainStatus(node, view); err != nil {
162 expectStatus := &protocol.BlockStoreState{Height: node.Height, Hash: &node.Hash}
163 if !testutil.DeepEqual(store.GetStoreStatus(), expectStatus) {
164 t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectStatus)
167 for hash, utxo := range view.Entries {
168 if utxo.Spent && !utxo.IsCoinBase {
172 gotUtxo, err := store.GetUtxo(&hash)
177 if !testutil.DeepEqual(utxo, gotUtxo) {
178 t.Errorf("got utxo entry:%v, expect utxo entry:%v", gotUtxo, utxo)
183 func TestSaveBlock(t *testing.T) {
184 defer os.RemoveAll("temp")
185 testDB := dbm.NewDB("testdb", "leveldb", "temp")
186 store := NewStore(testDB)
188 block := config.GenesisBlock()
189 status := &bc.TransactionStatus{VerifyStatus: []*bc.TxVerifyResult{{StatusFail: true}}}
190 if err := store.SaveBlock(block, status); err != nil {
194 blockHash := block.Hash()
195 gotBlock, err := store.GetBlock(&blockHash)
200 gotBlock.Transactions[0].Tx.SerializedSize = 0
201 gotBlock.Transactions[0].SerializedSize = 0
202 if !testutil.DeepEqual(block, gotBlock) {
203 t.Errorf("got block:%v, expect block:%v", gotBlock, block)
206 gotStatus, err := store.GetTransactionStatus(&blockHash)
211 if !testutil.DeepEqual(status, gotStatus) {
212 t.Errorf("got status:%v, expect status:%v", gotStatus, status)
215 data := store.db.Get(calcBlockHeaderKey(block.Height, &blockHash))
216 gotBlockHeader := types.BlockHeader{}
217 if err := gotBlockHeader.UnmarshalText(data); err != nil {
221 if !testutil.DeepEqual(block.BlockHeader, gotBlockHeader) {
222 t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader)