aboutsummaryrefslogtreecommitdiffstats
path: root/core/db/memory_test.go
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2018-12-13 18:15:03 +0800
committerGitHub <noreply@github.com>2018-12-13 18:15:03 +0800
commit155e31175aeaa3685c57383e386c6e62c46318ef (patch)
tree58333399ca2e08bc856e3c23ecca7eefe3c5db5e /core/db/memory_test.go
parent0ead4a7c012af9ddaa4a934729e216539d2caeb1 (diff)
downloaddexon-consensus-155e31175aeaa3685c57383e386c6e62c46318ef.tar
dexon-consensus-155e31175aeaa3685c57383e386c6e62c46318ef.tar.gz
dexon-consensus-155e31175aeaa3685c57383e386c6e62c46318ef.tar.bz2
dexon-consensus-155e31175aeaa3685c57383e386c6e62c46318ef.tar.lz
dexon-consensus-155e31175aeaa3685c57383e386c6e62c46318ef.tar.xz
dexon-consensus-155e31175aeaa3685c57383e386c6e62c46318ef.tar.zst
dexon-consensus-155e31175aeaa3685c57383e386c6e62c46318ef.zip
db: cache compaction chain tip in db (#369)
* Replace JSON with RLP in levelDB implementation. * Make sure blocks to sync following compaction chain tip
Diffstat (limited to 'core/db/memory_test.go')
-rw-r--r--core/db/memory_test.go46
1 files changed, 31 insertions, 15 deletions
diff --git a/core/db/memory_test.go b/core/db/memory_test.go
index 8fee582..7d18c3a 100644
--- a/core/db/memory_test.go
+++ b/core/db/memory_test.go
@@ -72,55 +72,55 @@ func (s *MemBackedDBTestSuite) TestSaveAndLoad() {
// Make sure the file pointed by 'dbPath' doesn't exist.
_, err := os.Stat(dbPath)
- s.Require().NotNil(err)
+ s.Require().Error(err)
dbInst, err := NewMemBackedDB(dbPath)
- s.Require().Nil(err)
+ s.Require().NoError(err)
s.Require().NotNil(dbInst)
defer func() {
if dbInst != nil {
- s.Nil(os.Remove(dbPath))
+ s.NoError(os.Remove(dbPath))
dbInst = nil
}
}()
- s.Nil(dbInst.PutBlock(*s.b00))
- s.Nil(dbInst.PutBlock(*s.b01))
- s.Nil(dbInst.PutBlock(*s.b02))
- s.Nil(dbInst.Close())
+ s.NoError(dbInst.PutBlock(*s.b00))
+ s.NoError(dbInst.PutBlock(*s.b01))
+ s.NoError(dbInst.PutBlock(*s.b02))
+ s.NoError(dbInst.Close())
// Load the json file back to check if all inserted blocks
// exists.
dbInst, err = NewMemBackedDB(dbPath)
- s.Require().Nil(err)
+ s.Require().NoError(err)
s.Require().NotNil(dbInst)
s.True(dbInst.HasBlock(s.b00.Hash))
s.True(dbInst.HasBlock(s.b01.Hash))
s.True(dbInst.HasBlock(s.b02.Hash))
- s.Nil(dbInst.Close())
+ s.NoError(dbInst.Close())
}
func (s *MemBackedDBTestSuite) TestIteration() {
// Make sure the file pointed by 'dbPath' doesn't exist.
dbInst, err := NewMemBackedDB()
- s.Require().Nil(err)
+ s.Require().NoError(err)
s.Require().NotNil(dbInst)
// Setup database.
- s.Nil(dbInst.PutBlock(*s.b00))
- s.Nil(dbInst.PutBlock(*s.b01))
- s.Nil(dbInst.PutBlock(*s.b02))
+ s.NoError(dbInst.PutBlock(*s.b00))
+ s.NoError(dbInst.PutBlock(*s.b01))
+ s.NoError(dbInst.PutBlock(*s.b02))
// Check if we can iterate all 3 blocks.
iter, err := dbInst.GetAllBlocks()
- s.Require().Nil(err)
+ s.Require().NoError(err)
touched := common.Hashes{}
for {
b, err := iter.NextBlock()
if err == ErrIterationFinished {
break
}
- s.Require().Nil(err)
+ s.Require().NoError(err)
touched = append(touched, b.Hash)
}
s.Len(touched, 3)
@@ -129,6 +129,22 @@ func (s *MemBackedDBTestSuite) TestIteration() {
s.Contains(touched, s.b02.Hash)
}
+func (s *MemBackedDBTestSuite) TestCompactionChainTipInfo() {
+ dbInst, err := NewMemBackedDB()
+ s.Require().NoError(err)
+ s.Require().NotNil(dbInst)
+ // Save some tip info.
+ hash := common.NewRandomHash()
+ s.Require().NoError(dbInst.PutCompactionChainTipInfo(hash, 123))
+ // Get it back to check.
+ hashBack, height := dbInst.GetCompactionChainTipInfo()
+ s.Require().Equal(hash, hashBack)
+ s.Require().Equal(height, uint64(123))
+ // Unable to put compaction chain tip info with lower height.
+ err = dbInst.PutCompactionChainTipInfo(hash, 122)
+ s.Require().IsType(err, ErrInvalidCompactionChainTipHeight)
+}
+
func TestMemBackedDB(t *testing.T) {
suite.Run(t, new(MemBackedDBTestSuite))
}