2023-09-24 06:42:53 +00:00
|
|
|
package chaindata
|
2023-09-01 11:54:39 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-22 09:10:39 +00:00
|
|
|
"errors"
|
2023-09-01 11:54:39 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2023-09-24 06:42:53 +00:00
|
|
|
type Paths struct {
|
|
|
|
ChainData, Ancient string
|
2023-09-22 09:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List of names of chaindata fixtures accessible via ChainDataPaths
|
|
|
|
var FixtureChains = []string{
|
|
|
|
"small", "small2",
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsFixture(chain string) bool {
|
|
|
|
has := false
|
|
|
|
for _, fixture := range FixtureChains {
|
|
|
|
if chain == fixture {
|
|
|
|
has = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2023-09-24 06:42:53 +00:00
|
|
|
// GetFixture returns the absolute paths to fixture chaindata for the given name.
|
|
|
|
func GetFixture(chain string) (*Paths, error) {
|
2023-09-22 09:10:39 +00:00
|
|
|
if !IsFixture(chain) {
|
|
|
|
return nil, errors.New("no fixture named " + chain)
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:54:39 +00:00
|
|
|
_, thisPath, _, ok := runtime.Caller(0)
|
|
|
|
if !ok {
|
2023-09-22 09:10:39 +00:00
|
|
|
return nil, errors.New("could not get function source path")
|
2023-09-01 11:54:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 06:42:53 +00:00
|
|
|
chaindataPath := filepath.Join(filepath.Dir(thisPath), "_data", chain)
|
2023-09-01 11:54:39 +00:00
|
|
|
ancientdataPath := filepath.Join(chaindataPath, "ancient")
|
|
|
|
|
|
|
|
if _, err := os.Stat(chaindataPath); err != nil {
|
2023-09-24 06:42:53 +00:00
|
|
|
return nil, errors.New("cannot access chaindata at " + chaindataPath)
|
2023-09-01 11:54:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 06:42:53 +00:00
|
|
|
return &Paths{chaindataPath, ancientdataPath}, nil
|
2023-09-01 11:54:39 +00:00
|
|
|
}
|