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)
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
}
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 {
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,
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 {
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 {
}
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
}
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
}
}
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
}
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) {
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
}
return nil, err
}
- result, err := c.prevCheckpointHashByPrevHash(&block.PreviousBlockHash)
+ result, err := c.parentCheckpointHashByPrevHash(&block.PreviousBlockHash)
if err != nil {
return nil, err
}
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)