From: paladz <453256728@qq.com> Date: Fri, 13 Apr 2018 05:21:50 +0000 (+0800) Subject: add unit test for calcReorganizeNodes X-Git-Tag: v1.0.5-alpha~335^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=fd0f38045e5d4107929ce137db54908047c0e8fa;p=bytom%2Fbytom-spv.git add unit test for calcReorganizeNodes --- diff --git a/protocol/block.go b/protocol/block.go index 1a79413b..3edd556c 100644 --- a/protocol/block.go +++ b/protocol/block.go @@ -13,7 +13,6 @@ import ( var ( // ErrBadBlock is returned when a block is invalid. ErrBadBlock = errors.New("invalid block") - // ErrBadStateRoot is returned when the computed assets merkle root // disagrees with the one declared in a block header. ErrBadStateRoot = errors.New("invalid state merkle root") @@ -38,6 +37,24 @@ func (c *Chain) GetBlockByHeight(height uint64) (*types.Block, error) { return c.store.GetBlock(&node.Hash) } +func (c *Chain) calcReorganizeNodes(node *state.BlockNode) ([]*state.BlockNode, []*state.BlockNode) { + var attachNodes []*state.BlockNode + var detachNodes []*state.BlockNode + + attachIter := node + for c.index.NodeByHeight(attachIter.Height) != attachIter { + attachNodes = append([]*state.BlockNode{attachIter}, attachNodes...) + attachIter = attachIter.Parent + } + + detachIter := c.bestNode + for detachIter != attachIter { + detachNodes = append(detachNodes, detachIter) + detachIter = detachIter.Parent + } + return attachNodes, detachNodes +} + func (c *Chain) connectBlock(block *types.Block) (err error) { bcBlock := types.MapBlock(block) if bcBlock.TransactionStatus, err = c.store.GetTransactionStatus(&bcBlock.ID); err != nil { @@ -63,24 +80,6 @@ func (c *Chain) connectBlock(block *types.Block) (err error) { return nil } -func (c *Chain) calcReorganizeNodes(node *state.BlockNode) ([]*state.BlockNode, []*state.BlockNode) { - var attachNodes []*state.BlockNode - var detachNodes []*state.BlockNode - - attachIter := node - for c.index.NodeByHeight(attachIter.Height) != attachIter { - attachNodes = append([]*state.BlockNode{attachIter}, attachNodes...) - attachIter = attachIter.Parent - } - - detachIter := c.bestNode - for detachIter != attachIter { - detachNodes = append(detachNodes, detachIter) - detachIter = detachIter.Parent - } - return attachNodes, detachNodes -} - func (c *Chain) reorganizeChain(node *state.BlockNode) error { attachNodes, detachNodes := c.calcReorganizeNodes(node) utxoView := state.NewUtxoViewpoint() diff --git a/protocol/block_test.go b/protocol/block_test.go new file mode 100644 index 00000000..e321e932 --- /dev/null +++ b/protocol/block_test.go @@ -0,0 +1,56 @@ +package protocol + +import ( + "testing" + + "github.com/bytom/config" + "github.com/bytom/protocol/state" + "github.com/bytom/testutil" +) + +func TestCalcReorganizeNodes(t *testing.T) { + c := &Chain{index: state.NewBlockIndex()} + header := config.GenerateGenesisBlock().BlockHeader + initNode, err := state.NewBlockNode(&header, nil) + if err != nil { + t.Fatal(err) + } + + c.index.AddNode(initNode) + var wantAttachNodes []*state.BlockNode + var wantDetachNodes []*state.BlockNode + + mainChainNode := initNode + for i := 1; i <= 7; i++ { + header.Height = uint64(i) + header.Nonce = 0 + mainChainNode, err = state.NewBlockNode(&header, mainChainNode) + if err != nil { + t.Fatal(err) + } + wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...) + c.index.AddNode(mainChainNode) + } + c.bestNode = mainChainNode + c.index.SetMainChain(mainChainNode) + + sideChainNode := initNode + for i := 1; i <= 13; i++ { + header.Height = uint64(i) + header.Nonce = 1 + sideChainNode, err = state.NewBlockNode(&header, sideChainNode) + if err != nil { + t.Fatal(err) + } + wantAttachNodes = append(wantAttachNodes, sideChainNode) + c.index.AddNode(sideChainNode) + } + + getAttachNodes, getDetachNodes := c.calcReorganizeNodes(sideChainNode) + if !testutil.DeepEqual(wantAttachNodes, getAttachNodes) { + t.Errorf("attach nodes want %v but get %v", wantAttachNodes, getAttachNodes) + } + if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) { + t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes) + } +}