Skip to content

Commit

Permalink
add block finalizer unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bysomeone committed May 8, 2024
1 parent bf2d521 commit 945997c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
10 changes: 4 additions & 6 deletions blockchain/blockfinalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ type finalizer struct {
lock sync.RWMutex
}

func newFinalizer(chain *BlockChain) *finalizer {

f := &finalizer{chain: chain}
func (f *finalizer) Init(chain *BlockChain) {

f.chain = chain
raw, err := chain.blockStore.db.Get(snowChoiceKey)

if err == nil {
Expand All @@ -36,8 +35,6 @@ func newFinalizer(chain *BlockChain) *finalizer {
chainlog.Debug("newFinalizer", "enableHeight", f.choice.Height, "gapHeight", chain.cfg.BlockFinalizeGapHeight)
go f.waitFinalizeStartBlock(f.choice.Height)
}

return f
}

func (f *finalizer) healthCheck(finalizedHeight int64) {
Expand Down Expand Up @@ -68,7 +65,8 @@ const defaultFinalizeGapHeight = 128

func (f *finalizer) waitFinalizeStartBlock(beginHeight int64) {

for f.chain.blockStore.Height() < beginHeight+f.chain.cfg.BlockFinalizeGapHeight {
waitHeight := f.chain.cfg.BlockFinalizeGapHeight
for f.chain.blockStore.Height() < beginHeight+waitHeight {
time.Sleep(time.Second * 5)
}

Expand Down
58 changes: 58 additions & 0 deletions blockchain/blockfinalize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package blockchain

import (
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/types"
"github.com/stretchr/testify/require"
"os"
"testing"
)

func newTestChain(t *testing.T) (*BlockChain, string) {

chain := InitEnv()
dir, err := os.MkdirTemp("", "finalize")
require.Nil(t, err)
blockStoreDB := dbm.NewDB("blockchain", "leveldb", dir, 64)
chain.blockStore = NewBlockStore(chain, blockStoreDB, nil)
node := newPreGenBlockNode()
node.parent = nil
chain.bestChain = newChainView(node)
return chain, dir
}

func TestFinalizer(t *testing.T) {

chain, dir := newTestChain(t)
defer os.RemoveAll(dir)

f := &finalizer{}
f.Init(chain)

hash := []byte("testhash")
choice := &types.SnowChoice{Height: 1, Hash: hash}
msg := queue.NewMessage(0, "test", 0, choice)

f.snowmanPreferBlock(msg)
f.snowmanAcceptBlock(msg)
height, hash1 := f.getLastFinalized()
require.Equal(t, 0, int(height))
require.Equal(t, 0, len(hash1))
node := &blockNode{
parent: chain.bestChain.Tip(),
height: 1,
hash: hash,
}
chain.bestChain.SetTip(node)
f.snowmanAcceptBlock(msg)
height, hash1 = f.getLastFinalized()
require.Equal(t, 1, int(height))
require.Equal(t, hash, hash1)

f.snowmanLastChoice(msg)
msg1, err := chain.client.Wait(msg)
require.Nil(t, err)
require.Equal(t, msg1.Data, choice)
f.Init(chain)
}
4 changes: 2 additions & 2 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ func (chain *BlockChain) SetQueueClient(client queue.Client) {
// 获取当前最大chunk连续高度
chain.maxSerialChunkNum = chain.blockStore.GetMaxSerialChunkNum()

chain.finalizer = newFinalizer(chain)

chain.finalizer = &finalizer{}
chain.finalizer.Init(chain)
//recv 消息的处理,共识模块需要获取lastblock从数据库中
chain.recvwg.Add(1)
//初始化blockchian模块
Expand Down

0 comments on commit 945997c

Please sign in to comment.