• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版d4e0711c484ad296e529b6543f46bb99c937fb2b (tree)
时间2020-02-20 18:01:13
作者ipqhjjybj <250657661@qq.c...>
CommiterGitHub

Log Message

Rollback pr3 (#496)

* rollback chain

* fix test ci

* delete irrblock judge

* update rollblock

* delete test case

* fix bad delete

* change rollback

* delete one test

* add tip

* change rollback

* fix one err

* remove fmt

* remove one test

* rename

* rename variable

Co-authored-by: Poseidon <shenao.78@163.com>

更改概述

差异

--- a/database/store.go
+++ b/database/store.go
@@ -156,6 +156,18 @@ func GetConsensusResult(db dbm.DB, seq uint64) (*state.ConsensusResult, error) {
156156 return consensusResult, nil
157157 }
158158
159+// DeleteConsensusResult delete a consensusResult from cache and database
160+func (s *Store) DeleteConsensusResult(seq uint64) error {
161+ consensusResult, err := GetConsensusResult(s.db, seq)
162+ if err != nil {
163+ return err
164+ }
165+
166+ s.db.Delete(calcConsensusResultKey(seq))
167+ s.cache.removeConsensusResult(consensusResult)
168+ return nil
169+}
170+
159171 // DeleteBlock delete a new block in the protocol.
160172 func (s *Store) DeleteBlock(block *types.Block) error {
161173 blockHash := block.Hash()
--- a/node/node.go
+++ b/node/node.go
@@ -203,7 +203,7 @@ func Rollback(config *cfg.Config, targetHeight uint64) error {
203203 return err
204204 }
205205
206- return nil
206+ return chain.Rollback(targetHeight)
207207 }
208208
209209 func initNodeConfig(config *cfg.Config) error {
--- a/protocol/block.go
+++ b/protocol/block.go
@@ -172,6 +172,78 @@ func (c *Chain) detachBlock(detachBlockHeader *types.BlockHeader, consensusResul
172172 return block, nil
173173 }
174174
175+func (c *Chain) syncSubProtocols() error {
176+ for _, p := range c.subProtocols {
177+ if err := c.syncProtocolStatus(p); err != nil {
178+ return errors.Wrap(err, p.Name(), "sync sub protocol status")
179+ }
180+ }
181+ return nil
182+}
183+
184+// Rollback rollback the chain from one blockHeight to targetBlockHeight
185+// WARNING: we recommend to use this only in commond line
186+func (c *Chain) Rollback(targetHeight uint64) error {
187+ c.cond.L.Lock()
188+ defer c.cond.L.Unlock()
189+
190+ utxoView := state.NewUtxoViewpoint()
191+ consensusResult, err := c.getBestConsensusResult()
192+ if err != nil {
193+ return err
194+ }
195+
196+ if err = c.syncSubProtocols(); err != nil {
197+ return err
198+ }
199+
200+ targetBlockHeader, err := c.GetHeaderByHeight(targetHeight)
201+ if err != nil {
202+ return err
203+ }
204+
205+ _, deletedBlockHeaders, err := c.calcReorganizeChain(targetBlockHeader, c.bestBlockHeader)
206+ if err != nil {
207+ return err
208+ }
209+
210+ deletedBlocks := []*types.Block{}
211+ for _, deletedBlockHeader := range deletedBlockHeaders {
212+ block, err := c.detachBlock(deletedBlockHeader, consensusResult, utxoView)
213+ if err != nil {
214+ return err
215+ }
216+
217+ deletedBlocks = append(deletedBlocks, block)
218+ }
219+
220+ setIrrBlockHeader := c.lastIrrBlockHeader
221+ if c.lastIrrBlockHeader.Height > targetBlockHeader.Height {
222+ setIrrBlockHeader = targetBlockHeader
223+ }
224+
225+ startSeq := state.CalcVoteSeq(c.bestBlockHeader.Height)
226+
227+ if err = c.setState(targetBlockHeader, setIrrBlockHeader, nil, utxoView, []*state.ConsensusResult{consensusResult.Fork()}); err != nil {
228+ return err
229+ }
230+
231+ for _, block := range deletedBlocks {
232+ if err := c.store.DeleteBlock(block); err != nil {
233+ return err
234+ }
235+ }
236+
237+ endSeq := state.CalcVoteSeq(targetHeight)
238+ for nowSeq := startSeq; nowSeq > endSeq; nowSeq-- {
239+ if err := c.store.DeleteConsensusResult(nowSeq); err != nil {
240+ return err
241+ }
242+ }
243+
244+ return nil
245+}
246+
175247 func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
176248 attachBlockHeaders, detachBlockHeaders, err := c.calcReorganizeChain(blockHeader, c.bestBlockHeader)
177249 if err != nil {
@@ -185,10 +257,8 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
185257 return err
186258 }
187259
188- for _, p := range c.subProtocols {
189- if err := c.syncProtocolStatus(p); err != nil {
190- return errors.Wrap(err, p.Name(), "sync sub protocol status")
191- }
260+ if err = c.syncSubProtocols(); err != nil {
261+ return err
192262 }
193263
194264 txsToRestore := map[bc.Hash]*types.Tx{}
--- a/protocol/block_test.go
+++ b/protocol/block_test.go
@@ -26,6 +26,7 @@ func (s *mStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) {
2626 func (s *mStore) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil }
2727 func (s *mStore) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil }
2828 func (s *mStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
29+func (s *mStore) DeleteConsensusResult(seq uint64) error { return nil }
2930 func (s *mStore) DeleteBlock(*types.Block) error { return nil }
3031 func (s *mStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
3132 func (s *mStore) SaveBlockHeader(blockHeader *types.BlockHeader) error {
--- a/protocol/consensus_node_manager_test.go
+++ b/protocol/consensus_node_manager_test.go
@@ -735,6 +735,10 @@ func (s *dummyStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) {
735735 return nil, nil
736736 }
737737
738+func (s *dummyStore) DeleteConsensusResult(seq uint64) error {
739+ return nil
740+}
741+
738742 func (s *dummyStore) SaveBlock(block *types.Block, _ *bc.TransactionStatus) error {
739743 hash := block.Hash()
740744 s.blocks[hash.String()] = block
--- a/protocol/store.go
+++ b/protocol/store.go
@@ -27,6 +27,7 @@ type Store interface {
2727 GetMainChainHash(uint64) (*bc.Hash, error)
2828 GetBlockHashesByHeight(uint64) ([]*bc.Hash, error)
2929
30+ DeleteConsensusResult(uint64) error
3031 DeleteBlock(*types.Block) error
3132 SaveBlock(*types.Block, *bc.TransactionStatus) error
3233 SaveBlockHeader(*types.BlockHeader) error
--- a/protocol/txpool_test.go
+++ b/protocol/txpool_test.go
@@ -122,6 +122,7 @@ func (s *mockStore) GetConsensusResult(uint64) (*state.ConsensusResult, error)
122122 func (s *mockStore) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil }
123123 func (s *mockStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
124124 func (s *mockStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
125+func (s *mockStore) DeleteConsensusResult(seq uint64) error { return nil }
125126 func (s *mockStore) DeleteBlock(*types.Block) error { return nil }
126127 func (s *mockStore) SaveBlockHeader(*types.BlockHeader) error { return nil }
127128 func (s *mockStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
@@ -675,6 +676,7 @@ func (s *mockStore1) GetMainChainHash(uint64) (*bc.Hash, error)
675676 func (s *mockStore1) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
676677 func (s *mockStore1) DeleteBlock(*types.Block) error { return nil }
677678 func (s *mockStore1) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
679+func (s *mockStore1) DeleteConsensusResult(seq uint64) error { return nil }
678680 func (s *mockStore1) SaveBlockHeader(*types.BlockHeader) error { return nil }
679681 func (s *mockStore1) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
680682 return nil
Show on old repository browser