-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add sync mode p2p CLI flag * Return struct instead of interface * Change flag usage string * Remove SnapServer interface * Revert makefile * Minor fixes * Update docs
- Loading branch information
Showing
10 changed files
with
248 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package p2p | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"sync/atomic" | ||
|
||
"github.com/NethermindEth/juno/blockchain" | ||
"github.com/NethermindEth/juno/sync" | ||
"github.com/NethermindEth/juno/utils" | ||
"github.com/libp2p/go-libp2p/core/host" | ||
) | ||
|
||
type Downloader struct { | ||
isFeeder bool | ||
mode atomic.Uint32 | ||
baseSyncer *SyncService | ||
snapSyncer *SnapSyncer | ||
log utils.SimpleLogger | ||
} | ||
|
||
func NewDownloader(isFeeder bool, syncMode SyncMode, p2pHost host.Host, network *utils.Network, bc *blockchain.Blockchain, log utils.SimpleLogger) *Downloader { | ||
dl := &Downloader{ | ||
isFeeder: isFeeder, | ||
log: log, | ||
} | ||
|
||
dl.baseSyncer = newSyncService(bc, p2pHost, network, log) | ||
|
||
var snapSyncer *SnapSyncer | ||
if syncMode == SnapSync { | ||
snapSyncer = NewSnapSyncer(dl.baseSyncer.Client(), bc, log) | ||
} | ||
dl.snapSyncer = snapSyncer | ||
|
||
// TODO: when syncing becomes more mature, we need a way to dynamically determine which sync mode to use | ||
// For now, we will use the sync mode that is passed in the constructor | ||
dl.mode.Store(uint32(syncMode)) | ||
|
||
return dl | ||
} | ||
|
||
func (d *Downloader) Start(ctx context.Context) error { | ||
// Feeder node doesn't sync using P2P | ||
if d.isFeeder { | ||
return nil | ||
} | ||
|
||
d.log.Infow("Downloader start", "mode", d.getMode()) | ||
if d.getMode() == SnapSync { | ||
// TODO: a hack, remove this | ||
if os.Getenv("JUNO_P2P_NO_SYNC") == "" { | ||
err := d.snapSyncer.Run(ctx) | ||
if err != nil { | ||
d.log.Errorw("Snapsyncer failed to start") | ||
return err | ||
} | ||
} else { | ||
d.log.Infow("Syncing is disabled") | ||
return nil | ||
} | ||
} | ||
|
||
d.baseSyncer.Start(ctx) | ||
return nil | ||
} | ||
|
||
func (d *Downloader) getMode() SyncMode { | ||
return SyncMode(d.mode.Load()) | ||
} | ||
|
||
func (d *Downloader) WithListener(l sync.EventListener) { | ||
d.baseSyncer.WithListener(l) | ||
} |
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,74 @@ | ||
package p2p | ||
|
||
import ( | ||
"encoding" | ||
"fmt" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
// The following are necessary for Cobra and Viper, respectively, to unmarshal | ||
// CLI/config parameters properly. | ||
var ( | ||
_ pflag.Value = (*SyncMode)(nil) | ||
_ encoding.TextUnmarshaler = (*SyncMode)(nil) | ||
) | ||
|
||
// SyncMode represents the synchronisation mode of the downloader. | ||
// It is a uint32 as it is used with atomic operations. | ||
type SyncMode uint32 | ||
|
||
const ( | ||
FullSync SyncMode = iota // Synchronize by downloading blocks and applying them to the chain sequentially | ||
SnapSync // Download the chain and the state via snap protocol | ||
) | ||
|
||
func (s SyncMode) IsValid() bool { | ||
return s == FullSync || s == SnapSync | ||
} | ||
|
||
func (s SyncMode) String() string { | ||
switch s { | ||
case FullSync: | ||
return "full" | ||
case SnapSync: | ||
return "snap" | ||
default: | ||
return "unknown" | ||
} | ||
} | ||
|
||
func (s SyncMode) Type() string { | ||
return "SyncMode" | ||
} | ||
|
||
func (s SyncMode) MarshalYAML() (interface{}, error) { | ||
return s.String(), nil | ||
} | ||
|
||
func (s *SyncMode) Set(mode string) error { | ||
switch mode { | ||
case "full": | ||
*s = FullSync | ||
case "snap": | ||
*s = SnapSync | ||
default: | ||
return fmt.Errorf("unknown sync mode %q, want \"full\" or \"snap\"", mode) | ||
} | ||
return nil | ||
} | ||
|
||
func (s SyncMode) MarshalText() ([]byte, error) { | ||
switch s { | ||
case FullSync: | ||
return []byte("full"), nil | ||
case SnapSync: | ||
return []byte("snap"), nil | ||
default: | ||
return nil, fmt.Errorf("unknown sync mode %d", s) | ||
} | ||
} | ||
|
||
func (s *SyncMode) UnmarshalText(text []byte) error { | ||
return s.Set(string(text)) | ||
} |
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
Oops, something went wrong.