From 45609b1dbfba5d7083826e27f2c5862fcca24106 Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Thu, 20 Dec 2018 14:19:55 +0800 Subject: core: deliver finalized blocks upon receiving randomness results. (#376) --- core/db/level-db.go | 2 +- core/db/level-db_test.go | 11 ++++++++--- core/db/memory.go | 2 +- core/db/memory_test.go | 11 ++++++++--- 4 files changed, 18 insertions(+), 8 deletions(-) (limited to 'core/db') diff --git a/core/db/level-db.go b/core/db/level-db.go index 3b5994b..75c3037 100644 --- a/core/db/level-db.go +++ b/core/db/level-db.go @@ -163,7 +163,7 @@ func (lvl *LevelDBBackedDB) PutCompactionChainTipInfo( if err != nil { return err } - if info.Height >= height { + if info.Height+1 != height { return ErrInvalidCompactionChainTipHeight } if err = lvl.db.Put(compactionChainTipInfoKey, marshaled, nil); err != nil { diff --git a/core/db/level-db_test.go b/core/db/level-db_test.go index cf56b87..df971ee 100644 --- a/core/db/level-db_test.go +++ b/core/db/level-db_test.go @@ -141,14 +141,19 @@ func (s *LevelDBTestSuite) TestCompactionChainTipInfo() { }(dbName) // Save some tip info. hash := common.NewRandomHash() - s.Require().NoError(dbInst.PutCompactionChainTipInfo(hash, 123)) + s.Require().NoError(dbInst.PutCompactionChainTipInfo(hash, 1)) // Get it back to check. hashBack, height := dbInst.GetCompactionChainTipInfo() s.Require().Equal(hash, hashBack) - s.Require().Equal(height, uint64(123)) + s.Require().Equal(height, uint64(1)) // Unable to put compaction chain tip info with lower height. - err = dbInst.PutCompactionChainTipInfo(hash, 122) + err = dbInst.PutCompactionChainTipInfo(hash, 0) s.Require().IsType(err, ErrInvalidCompactionChainTipHeight) + // Unable to put compaction chain tip info with height not incremental by 1. + err = dbInst.PutCompactionChainTipInfo(hash, 3) + s.Require().IsType(err, ErrInvalidCompactionChainTipHeight) + // It's OK to put compaction chain tip info with height incremental by 1. + s.Require().NoError(dbInst.PutCompactionChainTipInfo(hash, 2)) } func (s *LevelDBTestSuite) TestDKGPrivateKey() { diff --git a/core/db/memory.go b/core/db/memory.go index 7393de9..4bc08e7 100644 --- a/core/db/memory.go +++ b/core/db/memory.go @@ -149,7 +149,7 @@ func (m *MemBackedDB) PutCompactionChainTipInfo( blockHash common.Hash, height uint64) error { m.compactionChainTipLock.Lock() defer m.compactionChainTipLock.Unlock() - if m.compactionChainTipHeight >= height { + if m.compactionChainTipHeight+1 != height { return ErrInvalidCompactionChainTipHeight } m.compactionChainTipHeight = height diff --git a/core/db/memory_test.go b/core/db/memory_test.go index 09f74bb..a1b5165 100644 --- a/core/db/memory_test.go +++ b/core/db/memory_test.go @@ -137,14 +137,19 @@ func (s *MemBackedDBTestSuite) TestCompactionChainTipInfo() { s.Require().NotNil(dbInst) // Save some tip info. hash := common.NewRandomHash() - s.Require().NoError(dbInst.PutCompactionChainTipInfo(hash, 123)) + s.Require().NoError(dbInst.PutCompactionChainTipInfo(hash, 1)) // Get it back to check. hashBack, height := dbInst.GetCompactionChainTipInfo() s.Require().Equal(hash, hashBack) - s.Require().Equal(height, uint64(123)) + s.Require().Equal(height, uint64(1)) // Unable to put compaction chain tip info with lower height. - err = dbInst.PutCompactionChainTipInfo(hash, 122) + err = dbInst.PutCompactionChainTipInfo(hash, 0) s.Require().IsType(err, ErrInvalidCompactionChainTipHeight) + // Unable to put compaction chain tip info with height not incremental by 1. + err = dbInst.PutCompactionChainTipInfo(hash, 3) + s.Require().IsType(err, ErrInvalidCompactionChainTipHeight) + // It's OK to put compaction chain tip info with height incremental by 1. + s.Require().NoError(dbInst.PutCompactionChainTipInfo(hash, 2)) } func (s *MemBackedDBTestSuite) TestDKGPrivateKey() { -- cgit v1.2.3