OSDN Git Service

refine_casper_code (#1954)
authorPoseidon <shenao.78@163.com>
Wed, 9 Jun 2021 02:09:58 +0000 (10:09 +0800)
committerGitHub <noreply@github.com>
Wed, 9 Jun 2021 02:09:58 +0000 (10:09 +0800)
* refine_casper_code

* opt code

protocol/apply_block.go
protocol/auth_verification.go
protocol/casper.go

index 7ea6728..715318c 100644 (file)
@@ -32,8 +32,7 @@ func (c *Casper) ApplyBlock(block *types.Block) (*applyBlockReply, error) {
        defer c.mu.Unlock()
 
        if _, err := c.tree.nodeByHash(block.Hash()); err == nil {
-               _, bestHash := c.bestChain()
-               return &applyBlockReply{bestHash: bestHash}, nil
+               return &applyBlockReply{bestHash: c.bestChain()}, nil
        }
 
        target, err := c.applyBlockToCheckpoint(block)
@@ -60,13 +59,11 @@ func (c *Casper) ApplyBlock(block *types.Block) (*applyBlockReply, error) {
                return nil, err
        }
 
-       _, bestHash := c.bestChain()
-       reply := &applyBlockReply{verification: verification, bestHash: bestHash}
-       return reply, c.saveCheckpoints(affectedCheckpoints)
+       return &applyBlockReply{verification: verification, bestHash: c.bestChain()}, c.saveCheckpoints(affectedCheckpoints)
 }
 
 func (c *Casper) applyBlockToCheckpoint(block *types.Block) (*state.Checkpoint, error) {
-       node, err := c.checkpointByHash(block.PreviousBlockHash)
+       node, err := c.checkpointNodeByHash(block.PreviousBlockHash)
        if err != nil {
                return nil, err
        }
@@ -99,18 +96,18 @@ func (c *Casper) applyBlockToCheckpoint(block *types.Block) (*state.Checkpoint,
        return checkpoint, nil
 }
 
-func (c *Casper) checkpointByHash(blockHash bc.Hash) (*treeNode, error) {
+func (c *Casper) checkpointNodeByHash(blockHash bc.Hash) (*treeNode, error) {
        node, err := c.tree.nodeByHash(blockHash)
        if err != nil {
                logrus.WithField("err", err).Error("fail find checkpoint, start to reorganize checkpoint")
 
-               return c.reorganizeCheckpoint(blockHash)
+               return c.replayCheckpoint(blockHash)
        }
 
        return node, nil
 }
 
-func (c *Casper) reorganizeCheckpoint(hash bc.Hash) (*treeNode, error) {
+func (c *Casper) replayCheckpoint(hash bc.Hash) (*treeNode, error) {
        prevHash := hash
        var attachBlocks []*types.Block
        for {
@@ -247,7 +244,7 @@ func (c *Casper) myVerification(target *state.Checkpoint, validators map[string]
                return nil, nil
        }
 
-       if source := c.lastJustifiedCheckpointOfBranch(target); source != nil {
+       if source := c.lastJustifiedCheckpoint(target); source != nil {
                v := &Verification{
                        SourceHash:   source.Hash,
                        TargetHash:   target.Hash,
@@ -341,7 +338,7 @@ func processVote(output *types.TxOutput, checkpoint *state.Checkpoint) error {
        return nil
 }
 
-func (c *Casper) lastJustifiedCheckpointOfBranch(branch *state.Checkpoint) *state.Checkpoint {
+func (c *Casper) lastJustifiedCheckpoint(branch *state.Checkpoint) *state.Checkpoint {
        parent := branch.Parent
        for parent != nil {
                switch parent.Status {
index 25d87f6..104eab6 100644 (file)
@@ -48,12 +48,11 @@ func (c *Casper) AuthVerification(v *Verification) error {
                return nil
        }
 
-       _, oldBestHash := c.bestChain()
        if err := c.authVerification(v, targetNode.checkpoint, validators); err != nil {
                return err
        }
 
-       return c.tryRollback(oldBestHash)
+       return c.tryRollback(c.bestChain())
 }
 
 func (c *Casper) authVerification(v *Verification, target *state.Checkpoint, validators map[string]*state.Validator) error {
@@ -131,7 +130,7 @@ func (c *Casper) setFinalized(checkpoint *state.Checkpoint) {
 }
 
 func (c *Casper) tryRollback(oldBestHash bc.Hash) error {
-       if _, newBestHash := c.bestChain(); oldBestHash != newBestHash {
+       if newBestHash := c.bestChain(); oldBestHash != newBestHash {
                msg := &rollbackMsg{bestHash: newBestHash}
                c.rollbackCh <- msg
                return <-msg.reply
index 39016d6..3b733f7 100644 (file)
@@ -91,7 +91,7 @@ func (c *Casper) Validators(blockHash *bc.Hash) (map[string]*state.Validator, er
 }
 
 func (c *Casper) parentCheckpoint(blockHash *bc.Hash) (*state.Checkpoint, error) {
-       hash, err := c.prevCheckpointHash(blockHash)
+       hash, err := c.parentCheckpointHash(blockHash)
        if err != nil {
                return nil, err
        }
@@ -100,7 +100,7 @@ func (c *Casper) parentCheckpoint(blockHash *bc.Hash) (*state.Checkpoint, error)
 }
 
 func (c *Casper) parentCheckpointByPrevHash(prevBlockHash *bc.Hash) (*state.Checkpoint, error) {
-       hash, err := c.prevCheckpointHashByPrevHash(prevBlockHash)
+       hash, err := c.parentCheckpointHashByPrevHash(prevBlockHash)
        if err != nil {
                return nil, err
        }
@@ -131,11 +131,11 @@ func (c *Casper) EvilValidators() []*EvilValidator {
        return validators
 }
 
-func (c *Casper) bestChain() (uint64, bc.Hash) {
+func (c *Casper) bestChain() bc.Hash {
        // root is init justified
        root := c.tree.checkpoint
-       bestHeight, bestHash, _ := chainOfMaxJustifiedHeight(c.tree, root.Height)
-       return bestHeight, bestHash
+       _, bestHash, _ := chainOfMaxJustifiedHeight(c.tree, root.Height)
+       return bestHash
 }
 
 func lastJustified(node *treeNode) (uint64, bc.Hash) {
@@ -168,7 +168,7 @@ func chainOfMaxJustifiedHeight(node *treeNode, justifiedHeight uint64) (uint64,
        return bestHeight, bestHash, maxJustifiedHeight
 }
 
-func (c *Casper) prevCheckpointHash(blockHash *bc.Hash) (*bc.Hash, error) {
+func (c *Casper) parentCheckpointHash(blockHash *bc.Hash) (*bc.Hash, error) {
        if data, ok := c.prevCheckpointCache.Get(*blockHash); ok {
                return data.(*bc.Hash), nil
        }
@@ -178,7 +178,7 @@ func (c *Casper) prevCheckpointHash(blockHash *bc.Hash) (*bc.Hash, error) {
                return nil, err
        }
 
-       result, err := c.prevCheckpointHashByPrevHash(&block.PreviousBlockHash)
+       result, err := c.parentCheckpointHashByPrevHash(&block.PreviousBlockHash)
        if err != nil {
                return nil, err
        }
@@ -187,7 +187,7 @@ func (c *Casper) prevCheckpointHash(blockHash *bc.Hash) (*bc.Hash, error) {
        return result, nil
 }
 
-func (c *Casper) prevCheckpointHashByPrevHash(prevBlockHash *bc.Hash) (*bc.Hash, error) {
+func (c *Casper) parentCheckpointHashByPrevHash(prevBlockHash *bc.Hash) (*bc.Hash, error) {
        prevHash := prevBlockHash
        for {
                prevBlock, err := c.store.GetBlockHeader(prevHash)