basic unit tests for saving blocks with extended comit

This commit is contained in:
William Banfield
2022-05-19 19:30:44 -04:00
parent 1744ed2741
commit fd2a7df517
2 changed files with 79 additions and 1 deletions
+1 -1
View File
@@ -578,7 +578,7 @@ func (bs *BlockStore) saveBlockToBatch(batch dbm.Batch, block *types.Block, bloc
pbec := seenCommit.ToProto()
extCommitBytes := mustEncode(pbec)
if err := batch.Set(extCommitKey(height), extCommitBytes); err != nil {
if err := batch.Set(blockCommitKey(height), extCommitBytes); err != nil {
return err
}
return nil
+78
View File
@@ -322,6 +322,84 @@ func TestSaveBlockWithExtendedCommitPanicOnAbsentExtension(t *testing.T) {
}
}
// TestLoadBlockExtendedCommit tests loading the extended commit for a previously
// saved block. The load method should return nil when only a commit was saved and
// return the extended commit otherwise.
func TestLoadBlockExtendedCommit(t *testing.T) {
for _, testCase := range []struct {
name string
saveExtended bool
expectResult bool
}{
{
name: "save commit",
saveExtended: false,
expectResult: false,
},
{
name: "save extended commit",
saveExtended: true,
expectResult: true,
},
} {
t.Run(testCase.name, func(t *testing.T) {
state, bs, cleanup, err := makeStateAndBlockStore(t.TempDir())
require.NoError(t, err)
defer cleanup()
block := factory.MakeBlock(state, bs.Height()+1, new(types.Commit))
seenCommit := makeTestExtCommit(block.Header.Height, tmtime.Now())
ps, err := block.MakePartSet(2)
require.NoError(t, err)
if testCase.saveExtended {
bs.SaveBlockWithExtendedCommit(block, ps, seenCommit)
} else {
bs.SaveBlock(block, ps, seenCommit.ToCommit())
}
res := bs.LoadBlockExtendedCommit(block.Height)
if testCase.expectResult {
require.Equal(t, seenCommit, res)
} else {
require.Nil(t, res)
}
})
}
}
// TestLoadBlockCommit tests loading the commit for a previously saved block.
// The load method should always return a commit.
func TestLoadBlockCommit(t *testing.T) {
for _, testCase := range []struct {
name string
saveExtended bool
}{
{
name: "save commit",
saveExtended: false,
},
{
name: "save extended commit",
saveExtended: true,
},
} {
t.Run(testCase.name, func(t *testing.T) {
state, bs, cleanup, err := makeStateAndBlockStore(t.TempDir())
require.NoError(t, err)
defer cleanup()
block := factory.MakeBlock(state, bs.Height()+1, new(types.Commit))
seenCommit := makeTestExtCommit(block.Header.Height, tmtime.Now())
ps, err := block.MakePartSet(2)
require.NoError(t, err)
if testCase.saveExtended {
bs.SaveBlockWithExtendedCommit(block, ps, seenCommit)
} else {
bs.SaveBlock(block, ps, seenCommit.ToCommit())
}
res := bs.LoadBlockCommit(block.Height)
require.Equal(t, seenCommit.ToCommit(), res)
})
}
}
func TestLoadBaseMeta(t *testing.T) {
cfg, err := config.ResetTestRoot(t.TempDir(), "blockchain_reactor_test")
require.NoError(t, err)