From 155e31175aeaa3685c57383e386c6e62c46318ef Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Thu, 13 Dec 2018 18:15:03 +0800 Subject: db: cache compaction chain tip in db (#369) * Replace JSON with RLP in levelDB implementation. * Make sure blocks to sync following compaction chain tip --- core/test/blocks-generator_test.go | 2 -- core/test/utils.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'core/test') diff --git a/core/test/blocks-generator_test.go b/core/test/blocks-generator_test.go index bd7a5a2..78b609b 100644 --- a/core/test/blocks-generator_test.go +++ b/core/test/blocks-generator_test.go @@ -18,7 +18,6 @@ package test import ( - "fmt" "sort" "testing" "time" @@ -318,7 +317,6 @@ func (s *BlocksGeneratorTestSuite) TestConcateBlocksFromRounds() { totalAckCount += len(b.Acks) } // At least all blocks can ack some non-parent block. - fmt.Println(totalAckCount, totalBlockCount) req.True(totalAckCount/totalBlockCount >= 2) } diff --git a/core/test/utils.go b/core/test/utils.go index 56c5eac..6abd0b5 100644 --- a/core/test/utils.go +++ b/core/test/utils.go @@ -18,6 +18,7 @@ package test import ( + "errors" "fmt" "math" "net" @@ -26,6 +27,7 @@ import ( "github.com/dexon-foundation/dexon-consensus/common" "github.com/dexon-foundation/dexon-consensus/core/crypto" "github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa" + "github.com/dexon-foundation/dexon-consensus/core/db" "github.com/dexon-foundation/dexon-consensus/core/types" typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg" "github.com/dexon-foundation/dexon/rlp" @@ -170,3 +172,32 @@ func cloneBlockRandomnessResult(rand *types.BlockRandomnessResult) ( } return } + +var ( + // ErrCompactionChainTipBlockNotExists raised when the hash of compaction + // chain tip doesn't match a block in database. + ErrCompactionChainTipBlockNotExists = errors.New( + "compaction chain tip block not exists") + // ErrEmptyCompactionChainTipInfo raised when a compaction chain tip info + // is empty. + ErrEmptyCompactionChainTipInfo = errors.New( + "empty compaction chain tip info") + // ErrMismatchBlockHash raise when the hash for that block mismatched. + ErrMismatchBlockHash = errors.New("mismatched block hash") +) + +// VerifyDB check if a database is valid after test. +func VerifyDB(db db.Database) error { + hash, height := db.GetCompactionChainTipInfo() + if (hash == common.Hash{}) || height == 0 { + return ErrEmptyCompactionChainTipInfo + } + b, err := db.GetBlock(hash) + if err != nil { + return err + } + if b.Hash != hash { + return ErrMismatchBlockHash + } + return nil +} -- cgit v1.2.3