OSDN Git Service

fk
authorHAOYUatHZ <haoyu@protonmail.com>
Mon, 27 May 2019 07:47:39 +0000 (15:47 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Mon, 27 May 2019 07:47:39 +0000 (15:47 +0800)
database/store.go
protocol/block.go
protocol/protocol.go
protocol/store.go

index 7db4a92..b8046cf 100644 (file)
@@ -224,12 +224,16 @@ func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error {
 }
 
 // SaveChainStatus save the core's newest status && delete old status
-func (s *Store) SaveChainStatus(node, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, voteMap map[uint64]*state.VoteResult) error {
+func (s *Store) SaveChainStatus(node, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, mainchainOutMap map[bc.Hash]bool, voteMap map[uint64]*state.VoteResult) error {
        batch := s.db.NewBatch()
        if err := saveUtxoView(batch, view); err != nil {
                return err
        }
 
+       if err := saveMainchainOutputIDs(batch, mainchainOutMap); err != nil {
+               return err
+       }
+
        if err := saveVoteResult(batch, voteMap); err != nil {
                return err
        }
@@ -265,6 +269,19 @@ func (s *Store) SaveChainNodeStatus(bestNode, irreversibleNode *state.BlockNode)
        return nil
 }
 
+// saveVoteResult save mainchain output id in case of double spending
+func saveMainchainOutputIDs(batch dbm.Batch, mainchainOutMap map[bc.Hash]bool) error {
+       // for _, vote := range voteMap {
+       //      bytes, err := json.Marshal(vote)
+       //      if err != nil {
+       //              return err
+       //      }
+
+       //      batch.Set(calcVoteResultKey(vote.Seq), bytes)
+       // }
+       return nil
+}
+
 // saveVoteResult update the voting results generated by each irreversible block
 func saveVoteResult(batch dbm.Batch, voteMap map[uint64]*state.VoteResult) error {
        for _, vote := range voteMap {
index a89047c..e9dff87 100644 (file)
@@ -99,7 +99,8 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
                irreversibleNode = node
        }
 
-       if err := c.setState(node, irreversibleNode, utxoView, voteResultMap); err != nil {
+       mainchainOutMap := make(map[bc.Hash]bool)
+       if err := c.setState(node, irreversibleNode, utxoView, mainchainOutMap, voteResultMap); err != nil {
                return err
        }
 
@@ -112,9 +113,10 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
 func (c *Chain) reorganizeChain(node *state.BlockNode) error {
        attachNodes, detachNodes := c.calcReorganizeNodes(node)
        utxoView := state.NewUtxoViewpoint()
+       mainchainOutMap := make(map[bc.Hash]bool)
        voteResultMap := make(map[uint64]*state.VoteResult)
        irreversibleNode := c.bestIrreversibleNode
-       
+
        for _, detachNode := range detachNodes {
                b, err := c.store.GetBlock(&detachNode.Hash)
                if err != nil {
@@ -136,7 +138,7 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
                if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil {
                        return err
                }
-               
+
                if err := c.bbft.DetachBlock(voteResultMap, b); err != nil {
                        return err
                }
@@ -173,7 +175,7 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
                log.WithFields(log.Fields{"module": logModule, "height": node.Height, "hash": node.Hash.String()}).Debug("attach from mainchain")
        }
 
-       return c.setState(node, irreversibleNode, utxoView, voteResultMap)
+       return c.setState(node, irreversibleNode, utxoView, mainchainOutMap, voteResultMap)
 }
 
 // SaveBlock will validate and save block into storage
@@ -269,7 +271,7 @@ func (c *Chain) processBlock(block *types.Block) (bool, error) {
        if block.Height <= c.bestIrreversibleNode.Height {
                return false, errors.New("the height of block below the height of irreversible block")
        }
-       
+
        blockHash := block.Hash()
        if c.BlockExist(&blockHash) {
                log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Info("block has been processed")
index f3f4fc2..f35b85d 100644 (file)
@@ -81,7 +81,8 @@ func (c *Chain) initChainStatus() error {
        if err != nil {
                return err
        }
-       return c.store.SaveChainStatus(node, node, utxoView, map[uint64]*state.VoteResult{})
+       mainchainOutMap := make(map[bc.Hash]bool)
+       return c.store.SaveChainStatus(node, node, utxoView, mainchainOutMap, map[uint64]*state.VoteResult{})
 }
 
 // BestBlockHeight returns the current height of the blockchain.
@@ -110,8 +111,8 @@ func (c *Chain) InMainChain(hash bc.Hash) bool {
 }
 
 // This function must be called with mu lock in above level
-func (c *Chain) setState(node *state.BlockNode, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, voteMap map[uint64]*state.VoteResult) error {
-       if err := c.store.SaveChainStatus(node, irreversibleNode, view, voteMap); err != nil {
+func (c *Chain) setState(node *state.BlockNode, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, mainchainOutMap map[bc.Hash]bool, voteMap map[uint64]*state.VoteResult) error {
+       if err := c.store.SaveChainStatus(node, irreversibleNode, view, mainchainOutMap, voteMap); err != nil {
                return err
        }
 
index 48ac4e8..6580cde 100644 (file)
@@ -26,7 +26,7 @@ type Store interface {
 
        LoadBlockIndex(uint64) (*state.BlockIndex, error)
        SaveBlock(*types.Block, *bc.TransactionStatus) error
-       SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[uint64]*state.VoteResult) error
+       SaveChainStatus(*state.BlockNode, *state.BlockNode, *state.UtxoViewpoint, map[bc.Hash]bool, map[uint64]*state.VoteResult) error
        SaveChainNodeStatus(*state.BlockNode, *state.BlockNode) error
 }