From: Poseidon Date: Wed, 9 Jun 2021 02:09:58 +0000 (+0800) Subject: refine_casper_code (#1954) X-Git-Tag: bytom2-prerelease~1^2~120 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=85c4e5a5bca2306770fe921b178b88f2893e62eb;p=bytom%2Fbytom.git refine_casper_code (#1954) * refine_casper_code * opt code --- diff --git a/protocol/apply_block.go b/protocol/apply_block.go index 7ea67283..715318c4 100644 --- a/protocol/apply_block.go +++ b/protocol/apply_block.go @@ -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 { diff --git a/protocol/auth_verification.go b/protocol/auth_verification.go index 25d87f69..104eab6a 100644 --- a/protocol/auth_verification.go +++ b/protocol/auth_verification.go @@ -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 diff --git a/protocol/casper.go b/protocol/casper.go index 39016d6c..3b733f78 100644 --- a/protocol/casper.go +++ b/protocol/casper.go @@ -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)