• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版3b10a18ba05f0c8495fc76f9f46491a57c76311a (tree)
时间2020-02-19 10:51:27
作者ipqhjjybj <250657661@qq.c...>
CommiterGitHub

Log Message

add deleteBlock and then fix some tests (#491)

* add deleteBlock and then fix some tests

* add new test

* delete line

* fix one error

* change test case

* add s to word

* s

* update test cases

* update package and delete one same case

Co-authored-by: Paladz <yzhu101@uottawa.ca>

更改概述

差异

--- a/database/store.go
+++ b/database/store.go
@@ -156,6 +156,44 @@ func GetConsensusResult(db dbm.DB, seq uint64) (*state.ConsensusResult, error) {
156156 return consensusResult, nil
157157 }
158158
159+// DeleteBlock delete a new block in the protocol.
160+func (s *Store) DeleteBlock(block *types.Block) error {
161+ blockHash := block.Hash()
162+ blockHashes, err := s.GetBlockHashesByHeight(block.Height)
163+ if err != nil {
164+ return err
165+ }
166+
167+ for i := 0; i < len(blockHashes); i++ {
168+ if blockHashes[i].String() == blockHash.String() {
169+ blockHashes = append(blockHashes[0:i], blockHashes[i+1:len(blockHashes)]...)
170+ break
171+ }
172+ }
173+
174+ batch := s.db.NewBatch()
175+ if len(blockHashes) == 0 {
176+ batch.Delete(calcBlockHashesPrefix(block.Height))
177+ } else {
178+ binaryBlockHashes, err := json.Marshal(blockHashes)
179+ if err != nil {
180+ return errors.Wrap(err, "Marshal block hashes")
181+ }
182+
183+ batch.Set(calcBlockHashesPrefix(block.Height), binaryBlockHashes)
184+ }
185+
186+ batch.Delete(calcBlockHeaderKey(&blockHash))
187+ batch.Delete(calcBlockTransactionsKey(&blockHash))
188+ batch.Delete(calcTxStatusKey(&blockHash))
189+ batch.Write()
190+
191+ s.cache.removeBlockHashes(block.Height)
192+ s.cache.removeBlockHeader(&block.BlockHeader)
193+
194+ return nil
195+}
196+
159197 // NewStore creates and returns a new Store object.
160198 func NewStore(db dbm.DB) *Store {
161199 fillBlockHeaderFn := func(hash *bc.Hash) (*types.BlockHeader, error) {
--- a/database/store_test.go
+++ b/database/store_test.go
@@ -4,6 +4,8 @@ import (
44 "os"
55 "testing"
66
7+ "github.com/stretchr/testify/require"
8+
79 "github.com/bytom/vapor/consensus"
810 dbm "github.com/bytom/vapor/database/leveldb"
911 "github.com/bytom/vapor/database/storage"
@@ -290,3 +292,227 @@ func TestSaveBlockHeader(t *testing.T) {
290292 }
291293 }
292294 }
295+
296+func TestDeleteBlock(t *testing.T) {
297+ cases := []struct {
298+ initBlocks []*types.BlockHeader
299+ deleteBlock *types.BlockHeader
300+ wantBlocks []*types.BlockHeader
301+ }{
302+ {
303+ initBlocks: []*types.BlockHeader{},
304+ deleteBlock: &types.BlockHeader{
305+ Version: uint64(1),
306+ Height: uint64(1),
307+ Timestamp: uint64(1528945000),
308+ },
309+ wantBlocks: []*types.BlockHeader{},
310+ },
311+ {
312+ initBlocks: []*types.BlockHeader{
313+ {
314+ Version: uint64(1),
315+ Height: uint64(1),
316+ Timestamp: uint64(1528945000),
317+ },
318+ {
319+ Version: uint64(1),
320+ Height: uint64(1),
321+ Timestamp: uint64(1528945005),
322+ },
323+ {
324+ Version: uint64(1),
325+ Height: uint64(1),
326+ Timestamp: uint64(1528945010),
327+ },
328+ },
329+ deleteBlock: &types.BlockHeader{
330+ Version: uint64(1),
331+ Height: uint64(1),
332+ Timestamp: uint64(1528945000),
333+ },
334+ wantBlocks: []*types.BlockHeader{
335+ {
336+ Version: uint64(1),
337+ Height: uint64(1),
338+ Timestamp: uint64(1528945005),
339+ },
340+ {
341+ Version: uint64(1),
342+ Height: uint64(1),
343+ Timestamp: uint64(1528945010),
344+ },
345+ },
346+ },
347+ {
348+ initBlocks: []*types.BlockHeader{
349+ {
350+ Version: uint64(1),
351+ Height: uint64(1),
352+ Timestamp: uint64(1528945000),
353+ },
354+ {
355+ Version: uint64(1),
356+ Height: uint64(1),
357+ Timestamp: uint64(1528945005),
358+ },
359+ {
360+ Version: uint64(1),
361+ Height: uint64(1),
362+ Timestamp: uint64(1528945010),
363+ },
364+ },
365+ deleteBlock: &types.BlockHeader{
366+ Version: uint64(1),
367+ Height: uint64(1),
368+ Timestamp: uint64(1528945005),
369+ },
370+ wantBlocks: []*types.BlockHeader{
371+ {
372+ Version: uint64(1),
373+ Height: uint64(1),
374+ Timestamp: uint64(1528945000),
375+ },
376+ {
377+ Version: uint64(1),
378+ Height: uint64(1),
379+ Timestamp: uint64(1528945010),
380+ },
381+ },
382+ },
383+ {
384+ initBlocks: []*types.BlockHeader{
385+ {
386+ Version: uint64(1),
387+ Height: uint64(1),
388+ Timestamp: uint64(1528945000),
389+ },
390+ {
391+ Version: uint64(1),
392+ Height: uint64(1),
393+ Timestamp: uint64(1528945005),
394+ },
395+ {
396+ Version: uint64(1),
397+ Height: uint64(1),
398+ Timestamp: uint64(1528945010),
399+ },
400+ },
401+ deleteBlock: &types.BlockHeader{
402+ Version: uint64(1),
403+ Height: uint64(1),
404+ Timestamp: uint64(1528945010),
405+ },
406+ wantBlocks: []*types.BlockHeader{
407+ {
408+ Version: uint64(1),
409+ Height: uint64(1),
410+ Timestamp: uint64(1528945000),
411+ },
412+ {
413+ Version: uint64(1),
414+ Height: uint64(1),
415+ Timestamp: uint64(1528945005),
416+ },
417+ },
418+ },
419+ {
420+ initBlocks: []*types.BlockHeader{},
421+ deleteBlock: &types.BlockHeader{
422+ Version: uint64(1),
423+ Height: uint64(1),
424+ Timestamp: uint64(1528945030),
425+ },
426+ wantBlocks: []*types.BlockHeader{},
427+ },
428+ {
429+ initBlocks: []*types.BlockHeader{
430+ {
431+ Version: uint64(1),
432+ Height: uint64(1),
433+ Timestamp: uint64(1528945000),
434+ },
435+ },
436+ deleteBlock: &types.BlockHeader{
437+ Version: uint64(1),
438+ Height: uint64(1),
439+ Timestamp: uint64(1528945030),
440+ },
441+ wantBlocks: []*types.BlockHeader{
442+ {
443+ Version: uint64(1),
444+ Height: uint64(1),
445+ Timestamp: uint64(1528945000),
446+ },
447+ },
448+ },
449+ }
450+
451+ for _, c := range cases {
452+ verifyStatus := &bc.TransactionStatus{
453+ VerifyStatus: []*bc.TxVerifyResult{
454+ {StatusFail: false},
455+ },
456+ }
457+ deleteBlock := &types.Block{
458+ BlockHeader: types.BlockHeader{
459+ Version: c.deleteBlock.Version,
460+ Height: c.deleteBlock.Height,
461+ Timestamp: c.deleteBlock.Timestamp,
462+ },
463+ }
464+
465+ dbA := dbm.NewDB("dbu", "leveldb", "tempA")
466+ dbB := dbm.NewDB("dbc", "leveldb", "tempB")
467+
468+ storeA := NewStore(dbA)
469+ storeB := NewStore(dbB)
470+
471+ for i := 0; i < len(c.initBlocks); i++ {
472+ block := &types.Block{
473+ BlockHeader: types.BlockHeader{
474+ Version: c.initBlocks[i].Version,
475+ Height: c.initBlocks[i].Height,
476+ Timestamp: c.initBlocks[i].Timestamp,
477+ },
478+ }
479+ if err := storeA.SaveBlock(block, verifyStatus); err != nil {
480+ t.Fatal(err)
481+ }
482+ }
483+
484+ if err := storeA.DeleteBlock(deleteBlock); err != nil {
485+ t.Fatal(err)
486+ }
487+
488+ for i := 0; i < len(c.wantBlocks); i++ {
489+ block := &types.Block{
490+ BlockHeader: types.BlockHeader{
491+ Version: c.wantBlocks[i].Version,
492+ Height: c.wantBlocks[i].Height,
493+ Timestamp: c.wantBlocks[i].Timestamp,
494+ },
495+ }
496+ if err := storeB.SaveBlock(block, verifyStatus); err != nil {
497+ t.Fatal(err)
498+ }
499+ }
500+
501+ iterA := dbA.Iterator()
502+ iterB := dbB.Iterator()
503+
504+ for iterA.Next() && iterB.Next() {
505+ require.Equal(t, iterA.Key(), iterB.Key())
506+ require.Equal(t, iterA.Value(), iterB.Value())
507+ }
508+
509+ if iterA.Next() || iterB.Next() {
510+ t.Fatalf("why iterator is not finished")
511+ }
512+
513+ dbA.Close()
514+ os.RemoveAll("tempA")
515+ dbB.Close()
516+ os.RemoveAll("tempB")
517+ }
518+}
--- a/protocol/block_test.go
+++ b/protocol/block_test.go
@@ -23,9 +23,10 @@ func (s *mStore) GetStoreStatus() *BlockStoreState {
2323 func (s *mStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
2424 func (s *mStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error { return nil }
2525 func (s *mStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
26-func (s *mStore) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil }
26+func (s *mStore) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil }
2727 func (s *mStore) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil }
2828 func (s *mStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
29+func (s *mStore) DeleteBlock(*types.Block) error { return nil }
2930 func (s *mStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
3031 func (s *mStore) SaveBlockHeader(blockHeader *types.BlockHeader) error {
3132 s.blockHeaders[blockHeader.Hash()] = blockHeader
--- a/protocol/consensus_node_manager_test.go
+++ b/protocol/consensus_node_manager_test.go
@@ -299,7 +299,7 @@ func TestGetConsensusNodes(t *testing.T) {
299299 Height: 1202,
300300 PreviousBlockHash: testutil.MustDecodeHash("a5be1d1177eb027327baedb869f902f74850476d0b9432a30391a3165d3af7cc"),
301301 },
302- // fork chain, fork height in 1198, rollback 1200, 1199, append 1199, 1200
302+ // fork chain, fork height in 1198, rollback 1200, 1199, append 1199, 1200
303303 {
304304 Height: 1199,
305305 PreviousBlockHash: testutil.MustDecodeHash("ef24de31371b4d34363011b6c8b065b1acaad9264d9abae2253d584e0d3a8739"),
@@ -741,6 +741,10 @@ func (s *dummyStore) SaveBlock(block *types.Block, _ *bc.TransactionStatus) erro
741741 return nil
742742 }
743743
744+func (s *dummyStore) DeleteBlock(block *types.Block) error {
745+ return nil
746+}
747+
744748 func (s *dummyStore) SaveBlockHeader(header *types.BlockHeader) error {
745749 hash := header.Hash()
746750 s.blockHeaders[hash.String()] = header
--- a/protocol/store.go
+++ b/protocol/store.go
@@ -27,6 +27,7 @@ type Store interface {
2727 GetMainChainHash(uint64) (*bc.Hash, error)
2828 GetBlockHashesByHeight(uint64) ([]*bc.Hash, error)
2929
30+ DeleteBlock(*types.Block) error
3031 SaveBlock(*types.Block, *bc.TransactionStatus) error
3132 SaveBlockHeader(*types.BlockHeader) error
3233 SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error
--- a/protocol/txpool_test.go
+++ b/protocol/txpool_test.go
@@ -122,6 +122,7 @@ func (s *mockStore) GetConsensusResult(uint64) (*state.ConsensusResult, error)
122122 func (s *mockStore) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil }
123123 func (s *mockStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
124124 func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
125+func (s *mockStore) DeleteBlock(*types.Block) error { return nil }
125126 func (s *mockStore) SaveBlockHeader(*types.BlockHeader) error { return nil }
126127 func (s *mockStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
127128 return nil
@@ -672,6 +673,7 @@ func (s *mockStore1) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error)
672673 func (s *mockStore1) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil }
673674 func (s *mockStore1) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil }
674675 func (s *mockStore1) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
676+func (s *mockStore1) DeleteBlock(*types.Block) error { return nil }
675677 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
676678 func (s *mockStore1) SaveBlockHeader(*types.BlockHeader) error { return nil }
677679 func (s *mockStore1) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
Show on old repository browser