Skip to content

Commit

Permalink
core/consensus.go PreviousCononicalCoincidentOnPath was added as a me…
Browse files Browse the repository at this point in the history
…thod to allow subs to check doms on trace
  • Loading branch information
kiltsonfire committed Jul 12, 2022
1 parent bd52a86 commit 25b0228
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
25 changes: 25 additions & 0 deletions consensus/blake3/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,31 @@ func (blake3 *Blake3) PreviousCoincidentOnPath(chain consensus.ChainHeaderReader
}
}

// PreviousCononicalCoincidentOnPath searches the path for a cononical block of specified order in the specified slice
// *slice - The zone location which defines the slice in which we are validating
// *order - The order of the conincidence that is desired
// *path - Search among ancestors of this path in the specified slice
func (blake3 *Blake3) PreviousCononicalCoincidentOnPath(chain consensus.ChainHeaderReader, header *types.Header, slice []byte, order, path int, fullSliceEqual bool) (*types.Header, *types.Header, error) {
prevTerminalHeader := header
for {
terminalHeader, err := blake3.PreviousCoincidentOnPath(chain, prevTerminalHeader, slice, order, path, fullSliceEqual)
if err != nil {
return nil, nil, err
}
// Check to see if the terminal header is cononical in the dom
if chain.GetCanonicalHash(terminalHeader.Number[order].Uint64()) == terminalHeader.Hash() {

// Only return the prevTerminalHeader if we have run more than once because if not it is our head
if prevTerminalHeader == header {
return terminalHeader, nil, nil
} else {
return terminalHeader, prevTerminalHeader, nil
}
}
prevTerminalHeader = terminalHeader
}
}

// TraceBranches utilizes a passed in header for initializing a trace of all external blocks.
// The function will sue PrimeTraceBranch and RegionTraceBranch for the two different types of traces that need to occur.
// Depending on the difficultyContext, originalContext, and originalLocation the trace will know when and where to stop.
Expand Down
3 changes: 3 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type ChainHeaderReader interface {

// CheckLocationRange checks to make sure the range of r and z are valid
CheckLocationRange(location []byte) error

// GetCanonicalHash returns the canonical hash for a given block number
GetCanonicalHash(number uint64) common.Hash
}

// ChainReader defines a small collection of methods needed to access the local
Expand Down
33 changes: 3 additions & 30 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3350,44 +3350,17 @@ func (bc *BlockChain) PCRC(header *types.Header) (common.Hash, error) {
// If there are many invalid subordinate heads, aggregate the valid subordinate blocks (NewSubs) off of the valid dominant chain (RTR / PTP / PTR)
// and include them for processing in the manager.
func (bc *BlockChain) reorgTwistToCommonAncestor(subHead *types.Header, domHead *types.Header, slice []byte, order int, path int) error {
num := bc.hc.GetBlockNumber(subHead.Hash())

if num != nil {
// get all the external blocks on the subordinate chain path until common point
extBlocks, err := bc.GetExternalBlockTraceSet(subHead.Hash(), domHead, path)
if err != nil {
return err
}
hashes := make([]common.Hash, 0)
for _, extBlock := range extBlocks {
hashes = append(hashes, extBlock.Hash())
}
// Remove non-cononical blocks from subordinate chains.
nilHeader := &types.Header{}
bc.reOrgFeed.Send(ReOrgRollup{ReOrgHeader: nilHeader, OldChainHeaders: []*types.Header{nilHeader}, NewChainHeaders: []*types.Header{domHead}, NewSubs: hashes, NewSubContext: path})
return nil
}
hash := bc.hc.GetCanonicalHash(subHead.Number[path].Uint64())

prev := subHead
for {
prevHeader, err := bc.Engine().PreviousCoincidentOnPath(bc, prev, slice, order, path, true)
if err != nil {
return err
}
num = bc.hc.GetBlockNumber(prevHeader.Hash())
hash = bc.hc.GetCanonicalHash(prevHeader.Number[path].Uint64())

if num != nil {
// get all the external blocks on the subordinate chain path until common point
extBlocks, err := bc.GetExternalBlockTraceSet(prevHeader.Hash(), domHead, path)
if err != nil {
return err
}
hashes := make([]common.Hash, 0)
for _, extBlock := range extBlocks {
hashes = append(hashes, extBlock.Hash())
}
// Remove non-cononical blocks from subordinate chains.
bc.reOrgFeed.Send(ReOrgRollup{ReOrgHeader: prev, OldChainHeaders: []*types.Header{prev}, NewChainHeaders: []*types.Header{domHead}, NewSubs: hashes, NewSubContext: path})
if hash == prevHeader.Hash() {
return nil
}
prev = prevHeader
Expand Down

0 comments on commit 25b0228

Please sign in to comment.