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 config.CommonConfig = config.DefaultConfig()
19 testDB := dbm.NewDB("testdb", "leveldb", "temp")
20 store := NewStore(testDB)
26 block := config.GenesisBlock()
27 txStatus := bc.NewTransactionStatus()
29 if err := store.SaveBlock(block, txStatus); err != nil {
33 for block.Height <= 128 {
34 preHash := block.Hash()
35 block.PreviousBlockHash = preHash
37 if err := store.SaveBlock(block, txStatus); err != nil {
41 if block.Height%32 != 0 {
45 for i := uint64(0); i < block.Height/32; i++ {
47 if err := store.SaveBlock(block, txStatus); err != nil {
53 if _, err := store.LoadBlockIndex(128); err != nil {
58 func TestLoadBlockIndexBestHeight(t *testing.T) {
60 blockBestHeight uint64
61 stateBestHeight uint64
77 testDB := dbm.NewDB("testdb", "leveldb", "temp")
82 store := NewStore(testDB)
83 var savedBlocks []types.Block
85 for _, c := range cases {
86 block := config.GenesisBlock()
87 txStatus := bc.NewTransactionStatus()
89 for i := uint64(0); i < c.blockBestHeight; i++ {
90 if err := store.SaveBlock(block, txStatus); err != nil {
94 savedBlocks = append(savedBlocks, *block)
95 block.PreviousBlockHash = block.Hash()
99 index, err := store.LoadBlockIndex(c.stateBestHeight)
104 for _, block := range savedBlocks {
105 blockHash := block.Hash()
106 if block.Height <= c.stateBestHeight != index.BlockExist(&blockHash) {
107 t.Errorf("Error in load block index")
113 func TestLoadBlockIndexEquals(t *testing.T) {
114 testDB := dbm.NewDB("testdb", "leveldb", "temp")
115 store := NewStore(testDB)
121 block := config.GenesisBlock()
122 txStatus := bc.NewTransactionStatus()
123 expectBlockIndex := state.NewBlockIndex()
124 var parent *state.BlockNode
126 for block.Height <= 100 {
127 if err := store.SaveBlock(block, txStatus); err != nil {
131 if block.Height != 0 {
132 parent = expectBlockIndex.GetNode(&block.PreviousBlockHash)
135 node, err := state.NewBlockNode(&block.BlockHeader, parent)
140 expectBlockIndex.AddNode(node)
141 block.PreviousBlockHash = block.Hash()
145 index, err := store.LoadBlockIndex(100)
150 if !testutil.DeepEqual(expectBlockIndex, index) {
151 t.Errorf("got block index:%v, expect block index:%v", index, expectBlockIndex)
155 func TestSaveChainStatus(t *testing.T) {
156 testDB := dbm.NewDB("testdb", "leveldb", "temp")
162 store := NewStore(testDB)
164 node := &state.BlockNode{Height: 100, Hash: bc.Hash{V0: 0, V1: 1, V2: 2, V3: 3}}
165 view := &state.UtxoViewpoint{
166 Entries: map[bc.Hash]*storage.UtxoEntry{
167 bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: false},
168 bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.CoinbaseUTXOType, BlockHeight: 100, Spent: true},
169 bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: true},
170 bc.Hash{V0: 1, V1: 1, V2: 3, V3: 5}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: false},
171 bc.Hash{V0: 1, V1: 1, V2: 3, V3: 6}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: true},
172 bc.Hash{V0: 1, V1: 3, V2: 3, V3: 7}: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 100, Spent: false},
173 bc.Hash{V0: 1, V1: 3, V2: 3, V3: 7}: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 100, Spent: true},
177 if err := store.SaveChainStatus(node, node, view, map[uint64]*state.VoteResult{}); err != nil {
181 expectStatus := &protocol.BlockStoreState{Height: node.Height, Hash: &node.Hash, IrreversibleHeight: node.Height, IrreversibleHash: &node.Hash}
182 if !testutil.DeepEqual(store.GetStoreStatus(), expectStatus) {
183 t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectStatus)
186 for hash, utxo := range view.Entries {
187 if (utxo.Type == storage.NormalUTXOType) && utxo.Spent {
190 if (utxo.Type == storage.CrosschainUTXOType) && (!utxo.Spent) {
193 if (utxo.Type == storage.VoteUTXOType) && (utxo.Spent) {
197 gotUtxo, err := store.GetUtxo(&hash)
202 if !testutil.DeepEqual(utxo, gotUtxo) {
203 t.Errorf("got utxo entry:%v, expect utxo entry:%v", gotUtxo, utxo)
208 func TestSaveBlock(t *testing.T) {
209 testDB := dbm.NewDB("testdb", "leveldb", "temp")
215 store := NewStore(testDB)
217 block := config.GenesisBlock()
218 status := &bc.TransactionStatus{VerifyStatus: []*bc.TxVerifyResult{{StatusFail: true}}}
219 if err := store.SaveBlock(block, status); err != nil {
223 blockHash := block.Hash()
224 gotBlock, err := store.GetBlock(&blockHash)
229 gotBlock.Transactions[0].Tx.SerializedSize = 0
230 gotBlock.Transactions[0].SerializedSize = 0
231 if !testutil.DeepEqual(block, gotBlock) {
232 t.Errorf("got block:%v, expect block:%v", gotBlock, block)
235 gotStatus, err := store.GetTransactionStatus(&blockHash)
240 if !testutil.DeepEqual(status, gotStatus) {
241 t.Errorf("got status:%v, expect status:%v", gotStatus, status)
244 data := store.db.Get(calcBlockHeaderKey(block.Height, &blockHash))
245 gotBlockHeader := types.BlockHeader{}
246 if err := gotBlockHeader.UnmarshalText(data); err != nil {
250 if !testutil.DeepEqual(block.BlockHeader, gotBlockHeader) {
251 t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader)