X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=protocol%2Fblock_test.go;h=4ee6479ea9b2321bbac6e17e1ac02c54b5eb29b0;hb=73164af54a2587ba479b8b133274e3ecd0559a68;hp=6fcece86a6c4e38894575175b2018a38dd77047b;hpb=54373c1a3efe0e373ec1605840a4363e4b246c46;p=bytom%2Fvapor.git diff --git a/protocol/block_test.go b/protocol/block_test.go index 6fcece86..4ee6479e 100644 --- a/protocol/block_test.go +++ b/protocol/block_test.go @@ -3,144 +3,94 @@ package protocol import ( "testing" - "github.com/vapor/config" + "github.com/vapor/database/storage" "github.com/vapor/protocol/bc" + "github.com/vapor/protocol/bc/types" "github.com/vapor/protocol/state" "github.com/vapor/testutil" ) -func TestCalcReorganizeNodes(t *testing.T) { - config.CommonConfig = config.DefaultConfig() - c := &Chain{index: state.NewBlockIndex()} - header := config.GenesisBlock().BlockHeader - initNode, err := state.NewBlockNode(&header, nil) - if err != nil { - t.Fatal(err) +type mStore struct { + blockHeaders map[bc.Hash]*types.BlockHeader +} + +func (s *mStore) BlockExist(hash *bc.Hash) bool { return false } +func (s *mStore) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil } +func (s *mStore) GetBlockHeader(hash *bc.Hash) (*types.BlockHeader, error) { + return s.blockHeaders[*hash], nil +} +func (s *mStore) GetStoreStatus() *BlockStoreState { return nil } +func (s *mStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil } +func (s *mStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error { return nil } +func (s *mStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil } +func (s *mStore) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil } +func (s *mStore) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil } +func (s *mStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil } +func (s *mStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil } +func (s *mStore) SaveBlockHeader(blockHeader *types.BlockHeader) error { + s.blockHeaders[blockHeader.Hash()] = blockHeader + return nil +} +func (s *mStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error { + return nil +} + +func TestCalcReorganizeChain(t *testing.T) { + c := &Chain{ + store: &mStore{ + blockHeaders: make(map[bc.Hash]*types.BlockHeader), + }, } - c.index.AddNode(initNode) - var wantAttachNodes []*state.BlockNode - var wantDetachNodes []*state.BlockNode + initBlockHeader := &types.BlockHeader{ + Height: 0, + Version: 1, + } + c.store.SaveBlockHeader(initBlockHeader) - mainChainNode := initNode + var wantAttachBlockHeaders []*types.BlockHeader + var wantDetachBlockHeaders []*types.BlockHeader + mainChainBlockHeader := initBlockHeader + newChainBlockHeader := initBlockHeader for i := 1; i <= 7; i++ { - header.Height = uint64(i) - mainChainNode, err = state.NewBlockNode(&header, mainChainNode) - if err != nil { - t.Fatal(err) + mainChainBlockHeader = &types.BlockHeader{ + PreviousBlockHash: mainChainBlockHeader.Hash(), + Height: uint64(i), } - wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...) - c.index.AddNode(mainChainNode) + wantDetachBlockHeaders = append([]*types.BlockHeader{mainChainBlockHeader}, wantDetachBlockHeaders...) + c.store.SaveBlockHeader(mainChainBlockHeader) } - c.bestNode = mainChainNode - c.index.SetMainChain(mainChainNode) - sideChainNode := initNode for i := 1; i <= 13; i++ { - header.Height = uint64(i) - sideChainNode, err = state.NewBlockNode(&header, sideChainNode) - if err != nil { - t.Fatal(err) + newChainBlockHeader = &types.BlockHeader{ + PreviousBlockHash: newChainBlockHeader.Hash(), + Height: uint64(i), + Version: 1, } - 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) + wantAttachBlockHeaders = append(wantAttachBlockHeaders, newChainBlockHeader) + c.store.SaveBlockHeader(newChainBlockHeader) } - if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) { - t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes) - } -} -func TestEdgeCalcReorganizeNodes(t *testing.T) { - config.CommonConfig = config.DefaultConfig() - header := config.GenesisBlock().BlockHeader - initNode, err := state.NewBlockNode(&header, nil) - if err != nil { - t.Fatal(err) + // normal + getAttachBlockHeaders, getDetachBlockHeaders, _ := c.calcReorganizeChain(newChainBlockHeader, mainChainBlockHeader) + if !testutil.DeepEqual(wantAttachBlockHeaders, getAttachBlockHeaders) { + t.Errorf("normal test: attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders) } - testNodes := []*state.BlockNode{initNode} - testNewNodes := []*state.BlockNode{initNode} - for i := uint64(1); i <= 5; i++ { - node := &state.BlockNode{ - Height: i, - Hash: bc.Hash{V0: uint64(i)}, - Parent: testNodes[i-1], - } - testNodes = append(testNodes, node) - - newNode := &state.BlockNode{ - Height: i, - Hash: bc.Hash{V1: uint64(i)}, - Parent: testNewNodes[i-1], - } - testNewNodes = append(testNewNodes, newNode) + if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) { + t.Errorf("normal test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders) } - cases := []struct { - mainChainNode *state.BlockNode - newNode *state.BlockNode - wantAttachNodes []*state.BlockNode - wantDetachNodes []*state.BlockNode - }{ - { - mainChainNode: testNodes[1], - newNode: testNodes[5], - wantAttachNodes: testNodes[2:], - wantDetachNodes: []*state.BlockNode{}, - }, - { - mainChainNode: testNodes[5], - newNode: testNodes[2], - wantAttachNodes: []*state.BlockNode{}, - wantDetachNodes: []*state.BlockNode{testNodes[5], testNodes[4], testNodes[3]}, - }, - { - mainChainNode: testNodes[2], - newNode: testNodes[2], - wantAttachNodes: []*state.BlockNode{}, - wantDetachNodes: []*state.BlockNode{}, - }, - { - mainChainNode: testNewNodes[3], - newNode: testNodes[2], - wantAttachNodes: testNodes[1:3], - wantDetachNodes: []*state.BlockNode{testNewNodes[3], testNewNodes[2], testNewNodes[1]}, - }, - { - mainChainNode: testNewNodes[2], - newNode: testNodes[3], - wantAttachNodes: testNodes[1:4], - wantDetachNodes: []*state.BlockNode{testNewNodes[2], testNewNodes[1]}, - }, - { - mainChainNode: testNodes[5], - newNode: testNewNodes[3], - wantAttachNodes: testNewNodes[1:4], - wantDetachNodes: []*state.BlockNode{testNodes[5], testNodes[4], testNodes[3], testNodes[2], testNodes[1]}, - }, + // detachBlockHeaders is empty + forkChainBlockHeader := wantAttachBlockHeaders[7] + wantAttachBlockHeaders = wantAttachBlockHeaders[8:] + wantDetachBlockHeaders = []*types.BlockHeader{} + getAttachBlockHeaders, getDetachBlockHeaders, _ = c.calcReorganizeChain(newChainBlockHeader, forkChainBlockHeader) + if !testutil.DeepEqual(wantAttachBlockHeaders, getAttachBlockHeaders) { + t.Errorf("detachBlockHeaders is empty test: attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders) } - for i, c := range cases { - chain := &Chain{index: state.NewBlockIndex()} - chain.index.AddNode(initNode) - for i := uint64(1); i <= c.mainChainNode.Height; i++ { - chain.index.AddNode(testNodes[i]) - } - chain.bestNode = c.mainChainNode - chain.index.SetMainChain(c.mainChainNode) - getAttachNodes, getDetachNodes := chain.calcReorganizeNodes(c.newNode) - - if !testutil.DeepEqual(c.wantAttachNodes, getAttachNodes) { - t.Errorf("test case %d, attach nodes want %v but get %v", i, c.wantAttachNodes, getAttachNodes) - } - - if !testutil.DeepEqual(c.wantDetachNodes, getDetachNodes) { - t.Errorf("test case %d, detach nodes want %v but get %v", i, c.wantDetachNodes, getDetachNodes) - } + if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) { + t.Errorf("detachBlockHeaders is empty test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders) } }