Skip to content

Commit

Permalink
blockchain.go: fixed order of logic and spin case in CheckCanonical
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalank committed Jul 21, 2022
1 parent 81633a8 commit 4447c75
Showing 1 changed file with 80 additions and 29 deletions.
109 changes: 80 additions & 29 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2186,34 +2186,35 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool, setHead
// Process block using the parent state as reference point
substart := time.Now()

// Process our block and retrieve external blocks.
receipts, logs, usedGas, externalBlocks, err := bc.processor.Process(block, statedb, bc.vmConfig)
if err != nil {
bc.reportBlock(block, receipts, err)
atomic.StoreUint32(&followupInterrupt, 1)
bc.futureBlocks.Remove(block.Hash())
return it.index, err
}

order, err := bc.Engine().GetDifficultyOrder(block.Header())
if err != nil {
return it.index, err
}

if types.QuaiNetworkContext < params.ZONE {
err := bc.CheckSubordinateHeader(block.Header())
if order < types.QuaiNetworkContext {
err := bc.CheckDominantBlock(block)
if err != nil {
return it.index, err
}
}

fmt.Println("Running CheckCanonical and PCRC for block", block.Header().Number, block.Header().Location, block.Header().Hash())

_, err = bc.PCRC(block.Header(), order)
if err != nil {
if err.Error() == "slice is not synced" {
fmt.Println("adding to future blocks", block.Header().Hash())
if err := bc.addFutureBlock(block); err != nil {
return it.index, err
}
return it.index, nil
} else {
bc.futureBlocks.Remove(block.Hash())
if types.QuaiNetworkContext < params.ZONE {
err := bc.CheckSubordinateHeader(block.Header())
if err != nil {
return it.index, err
}
}

fmt.Println("Running CheckCanonical and PCRC for block", block.Header().Number, block.Header().Location, block.Header().Hash())
if order < types.QuaiNetworkContext {
err = bc.CheckCanonical(block.Header(), order)
if err != nil {
Expand All @@ -2229,13 +2230,18 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool, setHead
}
}

// Process our block and retrieve external blocks.
receipts, logs, usedGas, externalBlocks, err := bc.processor.Process(block, statedb, bc.vmConfig)
_, err = bc.PCRC(block.Header(), order)
if err != nil {
bc.reportBlock(block, receipts, err)
atomic.StoreUint32(&followupInterrupt, 1)
bc.futureBlocks.Remove(block.Hash())
return it.index, err
if err.Error() == "slice is not synced" {
fmt.Println("adding to future blocks", block.Header().Hash())
if err := bc.addFutureBlock(block); err != nil {
return it.index, err
}
return it.index, nil
} else {
bc.futureBlocks.Remove(block.Hash())
return it.index, err
}
}
// Update the metrics touched during block processing
accountReadTimer.Update(statedb.AccountReads) // Account reads are complete, we can mark them
Expand Down Expand Up @@ -3523,15 +3529,34 @@ func (bc *BlockChain) PreviousCanonicalCoincidentOnPath(header *types.Header, sl
// CheckCanonical retrieves whether or not the block to be imported is canonical. Will rollback our chain until the
// dominant block is canonical.
func (bc *BlockChain) CheckCanonical(header *types.Header, order int) error {
status := bc.domClient.GetBlockStatus(context.Background(), header)
// If the header is cononical break else keep looking
switch status {
case quaiclient.CanonStatTy:
return nil
case quaiclient.UnknownStatTy:
return errors.New("dominant chain not synced")
default:
return errors.New("dominant chain not synced")
lastUncleHeader := &types.Header{}
for {
status := bc.domClient.GetBlockStatus(context.Background(), header)
// If the header is cononical break else keep looking
switch status {
case quaiclient.CanonStatTy:
if (lastUncleHeader != &types.Header{}) {
bc.ReOrgRollBack(lastUncleHeader, []*types.Header{}, []*types.Header{})
}
return nil
default:
lastUncleHeader = header
}

if header.Number[types.QuaiNetworkContext].Cmp(big.NewInt(0)) == 0 {
return nil
}

terminalHeader, err := bc.Engine().PreviousCoincidentOnPath(bc, header, header.Location, order, types.QuaiNetworkContext, true)
if err != nil {
return err
}

if terminalHeader.Number[types.QuaiNetworkContext].Cmp(big.NewInt(0)) == 0 {
return nil
}

header = terminalHeader
}
}

Expand Down Expand Up @@ -3574,6 +3599,32 @@ func (bc *BlockChain) CheckSubordinateHeader(header *types.Header) error {
return nil
}

// CheckDominantBlock sends the block to the dominant chain.
func (bc *BlockChain) CheckDominantBlock(block *types.Block) error {
if bc.domClient == nil {
return errors.New("dom client is nil")
}

status := bc.GetBlockStatus(block.Header())
if status == WriteStatus(quaiclient.UnknownStatTy) {
extBlock, err := bc.GetExternalBlockByHashAndContext(block.Header().Hash(), types.QuaiNetworkContext-1)
if err != nil {
return err
}

if extBlock == nil {
fmt.Println("ext block is nil for", block.Header().Hash())
return nil
}
block := types.NewBlockWithHeader(extBlock.Header()).WithBody(extBlock.Transactions(), extBlock.Uncles())
sealed := block.WithSeal(block.Header())
fmt.Println("sending dominant block", block.Header().Number, block.Hash())
go bc.domClient.SendMinedBlock(context.Background(), sealed, true, true)
}

return nil
}

// calcHLCRNetDifficulty calculates the net difficulty from previous prime.
// The netDifficulties parameter inputs the nets of instantaneous difficulties from the terminus block.
// By correctly summing the net difficulties we have obtained the proper array to be compared in HLCR.
Expand Down

0 comments on commit 4447c75

Please sign in to comment.