forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add progress of restore in eth_syncing (0xPolygon#306)
* Add initial logic for the sync progression * Add backup command * Export the JSON response correctly * Add import chain function * Remove debug code * Fix json tag in BackupResult * Add comments for blockStream * Properly close the listener channels, add simple test for the progression * Remove mock event channel limits * Add Initialize method in consensus * Add restore command * Add cancel by ctrl + c in restore command * Remove import flag from server command Remove import flag from server command * Simplify fetchAndSaveBackup * Remove restore command and add --restore flag in server * Revert setupConsensus method * Add comment for backup/restore function * Revert the auto generated codes that didn't need to update * Revert the auto generated codes that didn't need to update * Create helper/progress package * Add progression for restore * Add missing error handling in backup.go * Add missing error handling in backup.go Display log for file closing error * Fix error message * Remove redundant statement * Remove unused codes * Add comments * Improve codes based review and add comments for go-doc * Fix tests * Rename targetFrom and targetTo * Fix build error * Add Test_consumeCommonBlocks * Remove unused field * Fix wrong SystemService name * Fix block number for current progress in ProgressionWrapper * Change chunk size from num blocks to blocks size * Add unit test for Test_importBlocks * Add error check in system service * Add error check in CreateBackup * Use WriteBlock instead of WriteBlocks in restore * Fix lint error * Fix faild test * Fix build error * Fix error handling in CreateBackup * Remove batch writing blocks in restore due the interface change of blockchain * Fix lint error * Fix lint error Co-authored-by: Milos Zivkovic <[email protected]>
- Loading branch information
1 parent
44b9d7f
commit 75acfdb
Showing
16 changed files
with
251 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package progress | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/0xPolygon/polygon-edge/blockchain" | ||
) | ||
|
||
type ChainSyncType string | ||
|
||
const ( | ||
ChainSyncRestore ChainSyncType = "restore" | ||
ChainSyncBulk ChainSyncType = "bulk-sync" | ||
) | ||
|
||
// Progression defines the status of the sync | ||
// progression of the node | ||
type Progression struct { | ||
// SyncType is indicating the sync method | ||
SyncType ChainSyncType | ||
|
||
// StartingBlock is the initial block that the node is starting | ||
// the sync from. It is reset after every sync batch | ||
StartingBlock uint64 | ||
|
||
// CurrentBlock is the last written block from the sync batch | ||
CurrentBlock uint64 | ||
|
||
// HighestBlock is the target block in the sync batch | ||
HighestBlock uint64 | ||
} | ||
|
||
type ProgressionWrapper struct { | ||
// progression is a reference to the ongoing batch sync. | ||
// Nil if no batch sync is currently in progress | ||
progression *Progression | ||
|
||
// stopCh is the channel for receiving stop signals | ||
// in progression tracking | ||
stopCh chan struct{} | ||
|
||
lock sync.RWMutex | ||
|
||
syncType ChainSyncType | ||
} | ||
|
||
func NewProgressionWrapper(syncType ChainSyncType) *ProgressionWrapper { | ||
return &ProgressionWrapper{ | ||
progression: nil, | ||
stopCh: make(chan struct{}), | ||
syncType: syncType, | ||
} | ||
} | ||
|
||
// startProgression initializes the progression tracking | ||
func (pw *ProgressionWrapper) StartProgression( | ||
startingBlock uint64, | ||
subscription blockchain.Subscription, | ||
) { | ||
pw.lock.Lock() | ||
defer pw.lock.Unlock() | ||
|
||
pw.progression = &Progression{ | ||
SyncType: pw.syncType, | ||
StartingBlock: startingBlock, | ||
} | ||
|
||
go pw.RunUpdateLoop(subscription) | ||
} | ||
|
||
// runUpdateLoop starts the blockchain event monitoring loop and | ||
// updates the currently written block in the batch sync | ||
func (pw *ProgressionWrapper) RunUpdateLoop(subscription blockchain.Subscription) { | ||
eventCh := subscription.GetEventCh() | ||
|
||
for { | ||
select { | ||
case event := <-eventCh: | ||
if event.Type == blockchain.EventFork { | ||
continue | ||
} | ||
|
||
if len(event.NewChain) == 0 { | ||
continue | ||
} | ||
|
||
lastBlock := event.NewChain[len(event.NewChain)-1] | ||
pw.UpdateCurrentProgression(lastBlock.Number) | ||
case <-pw.stopCh: | ||
subscription.Close() | ||
|
||
return | ||
} | ||
} | ||
} | ||
|
||
// StopProgression stops the progression tracking | ||
func (pw *ProgressionWrapper) StopProgression() { | ||
pw.stopCh <- struct{}{} | ||
|
||
pw.lock.Lock() | ||
defer pw.lock.Unlock() | ||
|
||
pw.progression = nil | ||
} | ||
|
||
// UpdateCurrentProgression sets the currently written block in the bulk sync | ||
func (pw *ProgressionWrapper) UpdateCurrentProgression(currentBlock uint64) { | ||
pw.lock.Lock() | ||
defer pw.lock.Unlock() | ||
|
||
pw.progression.CurrentBlock = currentBlock | ||
} | ||
|
||
// UpdateHighestProgression sets the highest-known target block in the bulk sync | ||
func (pw *ProgressionWrapper) UpdateHighestProgression(highestBlock uint64) { | ||
pw.lock.Lock() | ||
defer pw.lock.Unlock() | ||
|
||
pw.progression.HighestBlock = highestBlock | ||
} | ||
|
||
// GetProgression returns the latest sync progression | ||
func (pw *ProgressionWrapper) GetProgression() *Progression { | ||
pw.lock.RLock() | ||
defer pw.lock.RUnlock() | ||
|
||
return pw.progression | ||
} |
Oops, something went wrong.