Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloading optimization and bugfix #1678

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,20 @@ func (sl *Slice) CollectNewlyConfirmedEtxs(block *types.WorkObject, location com
return newlyConfirmedEtxs, subRollup, nil
}

// Terminate the search if the slice to which the given block belongs was
// not activated yet, expansion number is validated on prime block, so only
// terminate on prime block, It is possible to make this check more
// optimized but this is performant enough and in some cases might have to
// go through few more region blocks than necessary
_, order, err := sl.Engine().CalcOrder(ancestor)
if err != nil {
return nil, nil, err
}
regions, zones := common.GetHierarchySizeForExpansionNumber(ancestor.ExpansionNumber())
if order == common.PRIME_CTX && (location.Region() > int(regions) || location.Zone() > int(zones)) {
return newlyConfirmedEtxs, subRollup, nil
}

// Terminate the search when we find a block produced by the same sub
if ancestor.Location().SubIndex(nodeLocation) == location.SubIndex(nodeLocation) {
return newlyConfirmedEtxs, subRollup, nil
Expand Down
77 changes: 37 additions & 40 deletions quai/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,48 +155,45 @@ func (h *handler) checkNextPrimeBlock() {
select {
case <-checkNextPrimeBlockTimer.C:
currentHeight := h.core.CurrentHeader().Number(h.nodeLocation.Context())
go func() {
defer func() {
if r := recover(); r != nil {
h.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
resultCh := h.p2pBackend.Request(h.nodeLocation, new(big.Int).Add(currentHeight, big.NewInt(1)), common.Hash{})
data := <-resultCh
// If we find a new hash for the requested block number we can check
// first if we already have the block in the database otherwise ask the
// peers for it
if data != nil {
blockHash, ok := data.(common.Hash)
if ok {
block := h.core.GetBlockByHash(blockHash)
// If the blockHash for the asked number is not present in the
// appended database we ask the peer for the block with this hash
if block == nil {
go func() {
defer func() {
if r := recover(); r != nil {
h.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
resultCh := h.p2pBackend.Request(h.nodeLocation, blockHash, &types.WorkObject{})
block := <-resultCh
if block != nil {
h.core.WriteBlock(block.(*types.WorkObject))
}
}()
}
}
}
}()
// Try to fetch the next 3 blocks
h.GetNextPrimeBlock(currentHeight)
h.GetNextPrimeBlock(new(big.Int).Add(currentHeight, big.NewInt(1)))
h.GetNextPrimeBlock(new(big.Int).Add(currentHeight, big.NewInt(2)))
case <-h.quitCh:
return
}
}
}

func (h *handler) GetNextPrimeBlock(number *big.Int) {
go func() {
defer func() {
if r := recover(); r != nil {
h.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
resultCh := h.p2pBackend.Request(h.nodeLocation, new(big.Int).Add(number, big.NewInt(1)), common.Hash{})
data := <-resultCh
// If we find a new hash for the requested block number we can check
// first if we already have the block in the database otherwise ask the
// peers for it
if data != nil {
blockHash, ok := data.(common.Hash)
if ok {
block := h.core.GetBlockByHash(blockHash)
// If the blockHash for the asked number is not present in the
// appended database we ask the peer for the block with this hash
if block == nil {
resultCh := h.p2pBackend.Request(h.nodeLocation, blockHash, &types.WorkObject{})
block := <-resultCh
if block != nil {
h.core.WriteBlock(block.(*types.WorkObject))
}
}
}
}
}()
}
Loading