Skip to content
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
2 changes: 1 addition & 1 deletion cmd/utils/app/reset-datadir.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func resetCliAction(cliCtx *cli.Context) (err error) {
}
defer unlock()

err = snapcfg.LoadPreverified(cliCtx.Context, PreverifiedFlag.Get(cliCtx), &dirs)
err = snapcfg.LoadPreverified(cliCtx.Context, PreverifiedFlag.Get(cliCtx), &dirs, chainName)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion db/downloader/downloadercfg/downloadercfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func LoadSnapshotsHashes(ctx context.Context, dirs datadir.Dirs, chainName strin
snapcfg.SetToml(chainName, haveToml, true)
} else {
// Fetch the snapshot hashes from the web
err := snapcfg.LoadRemotePreverifiedForChain(ctx, chainName)
err := snapcfg.LoadRemotePreverified(ctx, chainName)
if err != nil {
log.Root().Crit("Snapshot hashes for supported networks was not loaded. Please check your network connection and/or GitHub status here https://www.githubstatus.com/", "chain", chainName, "err", err)
return fmt.Errorf("failed to fetch remote snapshot hashes for chain %s", chainName)
Expand Down
12 changes: 6 additions & 6 deletions db/downloader/webseeds/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ func Verify(
if dataDir != "" {
dirs = datadir.Open(dataDir)
}
err = snapcfg.LoadPreverified(ctx, preverifiedFlagValue, &dirs)
if err != nil {
return
}
allPreverified := snapcfg.GetAllCurrentPreverified()
chains := selectChains(targetChain, snapcfg.EmbeddedWebseedsRaw)
if len(chains) == 0 {
err = errors.New("no matching chains")
Expand Down Expand Up @@ -78,6 +73,11 @@ func Verify(
for _, chain := range chains {
// Shift left?
//
err = snapcfg.LoadPreverified(ctx, preverifiedFlagValue, &dirs, chain)
if err != nil {
return
}
cfg, _ := snapcfg.KnownCfg(chain)
webseeds, _ := snapcfg.GetEmbeddedWebseeds(chain)
var baseUrl string
err := errors.New("no valid webseeds")
Expand All @@ -88,7 +88,7 @@ func Verify(
}
}
panicif.Err(err)
preverified := g.MapMustGet(allPreverified, chain)
preverified := cfg.Preverified
panicif.True(preverified.Local)
//
// end shift left?
Expand Down
6 changes: 3 additions & 3 deletions db/snapcfg/preverified.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (

// Loads preverified from locations other than just remote. Usually for utility commands that want
// to test different preverified sources.
func LoadPreverified(ctx context.Context, flagValue string, dirs *datadir.Dirs) (err error) {
func LoadPreverified(ctx context.Context, flagValue string, dirs *datadir.Dirs, chainName string) (err error) {
switch flagValue {
case "local":
panicif.Err(os.Setenv(RemotePreverifiedEnvKey, dirs.PreverifiedPath()))
fallthrough
case "remote":
err = LoadRemotePreverified(ctx)
err = LoadRemotePreverified(ctx, chainName)
if err != nil {
// TODO: Check if we should continue? What if we ask for a git revision and
// can't get it? What about a branch? Can we reset to the embedded snapshot hashes?
return fmt.Errorf("loading remote preverified snapshots: %w", err)
return fmt.Errorf("loading remote preverified snapshots for chain %q: %w", chainName, err)
}
case "embedded":
// Should already be loaded.
Expand Down
73 changes: 2 additions & 71 deletions db/snapcfg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"errors"
"fmt"
"io"
"maps"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -76,19 +75,6 @@ var registry = &preverifiedRegistry{
cached: make(map[string]*Cfg),
}

// All forces parsing of all remaining raw entries and returns a clone of the data map.
func (r *preverifiedRegistry) All() map[string]Preverified {
r.mu.Lock()
defer r.mu.Unlock()
for name, rawBytes := range r.raw {
if _, ok := r.data[name]; !ok {
r.data[name] = fromEmbeddedToml(rawBytes)
}
}
r.raw = nil
return maps.Clone(r.data)
}

func (r *preverifiedRegistry) Get(networkName string) (*Cfg, bool) {
r.mu.RLock()
if cfg, ok := r.cached[networkName]; ok {
Expand Down Expand Up @@ -147,16 +133,6 @@ func (r *preverifiedRegistry) Reset(data map[string]Preverified) {
r.mu.Unlock()
}

// ResetRaw replaces the raw bytes, clearing data and cached. Used after remote loading
// updates the module-level snapshot hash vars.
func (r *preverifiedRegistry) ResetRaw(raw map[string][]byte) {
r.mu.Lock()
r.raw = raw
r.data = make(map[string]Preverified)
r.cached = make(map[string]*Cfg)
r.mu.Unlock()
}

// Has checks whether a chain name exists in either raw or data without triggering parsing.
func (r *preverifiedRegistry) Has(networkName string) bool {
r.mu.RLock()
Expand Down Expand Up @@ -593,8 +569,8 @@ func fetchChainToml(ctx context.Context, source snapshothashes.SnapshotSource, b
return res, nil
}

// LoadRemotePreverifiedForChain fetches and loads snapshot hashes for a single chain only.
func LoadRemotePreverifiedForChain(ctx context.Context, chainName string) error {
// LoadRemotePreverified fetches and loads snapshot hashes for a single chain.
func LoadRemotePreverified(ctx context.Context, chainName string) error {
if s, ok := os.LookupEnv(RemotePreverifiedEnvKey); ok {
log.Info("Loading local preverified override file", "file", s)

Expand Down Expand Up @@ -630,46 +606,6 @@ func LoadRemotePreverifiedForChain(ctx context.Context, chainName string) error
return nil
}

func LoadRemotePreverified(ctx context.Context) (err error) {
if s, ok := os.LookupEnv(RemotePreverifiedEnvKey); ok {
log.Info("Loading local preverified override file", "file", s)

b, err := os.ReadFile(s)
if err != nil {
return fmt.Errorf("reading remote preverified override file: %w", err)
}
for _, sh := range snapshotHashPtrs {
*sh = bytes.Clone(b)
}
} else {
log.Info("Loading remote snapshot hashes")

err = snapshothashes.LoadSnapshots(ctx, snapshothashes.R2, snapshotGitBranch)
if err != nil {
log.Root().Warn("Failed to load snapshot hashes from R2; falling back to GitHub", "err", err)

// Fallback to GitHub if R2 fails
err = snapshothashes.LoadSnapshots(ctx, snapshothashes.Github, snapshotGitBranch)
if err != nil {
return err
}
}
}

// Re-load the preverified hashes
registry.ResetRaw(map[string][]byte{
networkname.Mainnet: snapshothashes.Mainnet,
networkname.Sepolia: snapshothashes.Sepolia,
networkname.Amoy: snapshothashes.Amoy,
networkname.BorMainnet: snapshothashes.BorMainnet,
networkname.Gnosis: snapshothashes.Gnosis,
networkname.Chiado: snapshothashes.Chiado,
networkname.Hoodi: snapshothashes.Hoodi,
networkname.Bloatnet: snapshothashes.Bloatnet,
})
return
}

func SetToml(networkName string, toml []byte, local bool) {
if registry.Has(networkName) {
registry.Set(networkName, Preverified{Local: local, Items: fromToml(toml)})
Expand All @@ -683,11 +619,6 @@ func GetToml(networkName string) []byte {
return nil
}

// Gets the current preverified for all chains.
func GetAllCurrentPreverified() map[string]Preverified {
return registry.All()
}

// Converts webseed value to URL. Mostly this is just stripping v1: for now, as nothing else is in
// active use.
func WebseedToUrl(s string) (_ string, err error) {
Expand Down
Loading