Skip to content

Commit

Permalink
config/server: clean-up chainName and reuse source config
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Oct 22, 2024
1 parent ff6d807 commit 5b7aa04
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 52 deletions.
18 changes: 18 additions & 0 deletions .changelog/766.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
config/server: clean-up chainName and reuse source config

Required config change:

Old:

```yaml
server:
chain_name: mainnet
```
New:
```yaml
server:
source:
chain_name: mainnet
```
24 changes: 12 additions & 12 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,22 @@ func NewService(cfg *config.ServerConfig) (*Service, error) {
if err != nil {
return nil, err
}
referenceSwaps := map[common.Runtime]config.ReferenceSwap{}

// Runtime clients.
runtimeClients := make(map[common.Runtime]nodeapi.RuntimeApiLite)
var networkConfig *sdkConfig.Network
if cfg.Source != nil {
referenceSwaps = cfg.Source.ReferenceSwaps()
networkConfig = cfg.Source.SDKNetwork()
apiRuntimes := []common.Runtime{common.RuntimeEmerald, common.RuntimeSapphire, common.RuntimePontusxTest, common.RuntimePontusxDev}
for _, runtime := range apiRuntimes {
client, err2 := source.NewRuntimeClient(ctx, cfg.Source, runtime)
if err2 != nil {
logger.Warn("unable to instantiate runtime client for api server", "runtime", runtime, "err", err2)
}
runtimeClients[runtime] = client
referenceSwaps := cfg.Source.ReferenceSwaps()
networkConfig = cfg.Source.SDKNetwork()
apiRuntimes := []common.Runtime{common.RuntimeEmerald, common.RuntimeSapphire, common.RuntimePontusxTest, common.RuntimePontusxDev}
for _, runtime := range apiRuntimes {
client, err2 := source.NewRuntimeClient(ctx, cfg.Source, runtime)
if err2 != nil {
logger.Warn("unable to instantiate runtime client for api server", "runtime", runtime, "err", err2)
}
runtimeClients[runtime] = client
}
client, err := storage.NewStorageClient(cfg.ChainName, backing, referenceSwaps, runtimeClients, networkConfig, logger)

client, err := storage.NewStorageClient(*cfg.Source, backing, referenceSwaps, runtimeClients, networkConfig, logger)
if err != nil {
return nil, err
}
Expand Down
59 changes: 46 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ func (sc *SourceConfig) Validate() error {
} else if sc.ChainName != "" && sc.CustomChain != nil {
return fmt.Errorf("source.chain_name and source.custom_chain specified, can only use one")
}
if sc.CustomChain != nil {
if sc.CustomChain.History == nil {
return fmt.Errorf("source.custom_chain.history not specified")
}
if sc.CustomChain.SDKNetwork == nil {
return fmt.Errorf("source.custom_chain.sdk_network not specified")
}
}
for archiveName, archiveConfig := range sc.Nodes {
if archiveConfig.DefaultNode == nil && archiveConfig.ConsensusNode == nil && len(archiveConfig.RuntimeNodes) == 0 {
return fmt.Errorf("source.nodes[%v] has none of .default, .consensus, or .runtimes", archiveName)
Expand Down Expand Up @@ -248,6 +256,37 @@ func (sc *SourceConfig) SDKParaTime(runtime common.Runtime) *sdkConfig.ParaTime
return sc.SDKNetwork().ParaTimes.All[string(runtime)]
}

func (sc *SourceConfig) ResolveRuntimeID(runtime common.Runtime) (string, error) {
// Default chain.
if sc.ChainName != "" {
network, exists := sdkConfig.DefaultNetworks.All[string(sc.ChainName)]
if !exists {
return "", fmt.Errorf("unknown default chain name %s", sc.ChainName)
}
rt, exists := network.ParaTimes.All[string(runtime)]
if !exists {
return "", fmt.Errorf("unknown runtime %s for default chain %s", runtime, sc.ChainName)
}
return rt.ID, nil
}

// Custom chain.
if sc.CustomChain == nil {
return "", fmt.Errorf("no custom chain specified")
}
if sc.CustomChain.SDKNetwork == nil {
return "", fmt.Errorf("no SDK network specified for custom chain")
}
if sc.CustomChain.SDKNetwork.ParaTimes.All == nil {
return "", fmt.Errorf("no runtimes specified for custom chain")
}
rt, exists := sc.CustomChain.SDKNetwork.ParaTimes.All[string(runtime)]
if !exists {
return "", fmt.Errorf("unknown runtime %s for custom chain", runtime)
}
return rt.ID, nil
}

type CacheConfig struct {
// CacheDir is the directory where the cache data is stored
CacheDir string `koanf:"cache_dir"`
Expand Down Expand Up @@ -539,10 +578,6 @@ func (cfg *AggregateStatsConfig) Validate() error {

// ServerConfig contains the API server configuration.
type ServerConfig struct {
// ChainName is the name of the chain (i.e. mainnet/testnet). Custom/local nets are not supported.
// This is only used for the runtime status endpoint.
ChainName common.ChainName `koanf:"chain_name"`

// Endpoint is the service endpoint from which to serve the API.
Endpoint string `koanf:"endpoint"`

Expand All @@ -561,16 +596,14 @@ func (cfg *ServerConfig) Validate() error {
if cfg.Storage == nil {
return fmt.Errorf("no storage config provided")
}
if cfg.ChainName == "" {
return fmt.Errorf("no chain name provided")
if cfg.Source == nil {
return fmt.Errorf("no source config provided")
}
if cfg.Source != nil {
if err := cfg.Source.Validate(); err != nil {
return err
}
if cfg.Source.Cache != nil {
return fmt.Errorf("server config should not have a cache configured")
}
if err := cfg.Source.Validate(); err != nil {
return err
}
if cfg.Source.Cache != nil {
return fmt.Errorf("server config should not have a cache configured")
}

return cfg.Storage.Validate(false /* requireMigrations */)
Expand Down
3 changes: 2 additions & 1 deletion config/docker-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ analysis:
migrations: file://storage/migrations

server:
chain_name: mainnet
source:
chain_name: mainnet
endpoint: 0.0.0.0:8008
storage:
endpoint: postgresql://rwuser:password@nexus-postgres:5432/indexer?sslmode=disable
Expand Down
1 change: 0 additions & 1 deletion config/local-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ analysis:
migrations: file://storage/migrations

server:
chain_name: mainnet
endpoint: localhost:8008
storage:
endpoint: postgresql://rwuser:password@localhost:5432/indexer?sslmode=disable
Expand Down
24 changes: 5 additions & 19 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
// StorageClient is a wrapper around a storage.TargetStorage
// with knowledge of network semantics.
type StorageClient struct {
chainName common.ChainName
sourceCfg config.SourceConfig
db storage.TargetStorage
referenceSwaps map[common.Runtime]config.ReferenceSwap
runtimeClients map[common.Runtime]nodeapi.RuntimeApiLite
Expand Down Expand Up @@ -90,20 +90,6 @@ func translateLayer(layer apiTypes.Layer) common.Layer {
}
}

// runtimeNameToID returns the runtime ID for the given network and runtime name.
func runtimeNameToID(chainName common.ChainName, name common.Runtime) (string, error) {
network, exists := oasisConfig.DefaultNetworks.All[string(chainName)]
if !exists {
return "", fmt.Errorf("unknown network: %s", chainName)
}
paratime, exists := network.ParaTimes.All[string(name)]
if !exists {
return "", fmt.Errorf("unknown runtime: %s", name)
}

return paratime.ID, nil
}

type rowsWithCount struct {
rows pgx.Rows
totalCount uint64
Expand All @@ -122,7 +108,7 @@ func runtimeFromCtx(ctx context.Context) common.Runtime {
}

// NewStorageClient creates a new storage client.
func NewStorageClient(chainName common.ChainName, db storage.TargetStorage, referenceSwaps map[common.Runtime]config.ReferenceSwap, runtimeClients map[common.Runtime]nodeapi.RuntimeApiLite, networkConfig *oasisConfig.Network, l *log.Logger) (*StorageClient, error) {
func NewStorageClient(sourceCfg config.SourceConfig, db storage.TargetStorage, referenceSwaps map[common.Runtime]config.ReferenceSwap, runtimeClients map[common.Runtime]nodeapi.RuntimeApiLite, networkConfig *oasisConfig.Network, l *log.Logger) (*StorageClient, error) {
blockCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1024 * 10,
MaxCost: 1024,
Expand All @@ -133,7 +119,7 @@ func NewStorageClient(chainName common.ChainName, db storage.TargetStorage, refe
l.Error("api client: failed to create block cache: %w", err)
return nil, err
}
return &StorageClient{chainName, db, referenceSwaps, runtimeClients, networkConfig, blockCache, l}, nil
return &StorageClient{sourceCfg, db, referenceSwaps, runtimeClients, networkConfig, blockCache, l}, nil
}

// Shutdown closes the backing TargetStorage.
Expand Down Expand Up @@ -2214,10 +2200,10 @@ func (c *StorageClient) RuntimeEVMNFTs(ctx context.Context, limit *uint64, offse
// RuntimeStatus returns runtime status information.
func (c *StorageClient) RuntimeStatus(ctx context.Context) (*RuntimeStatus, error) {
runtimeName := runtimeFromCtx(ctx)
runtimeID, err := runtimeNameToID(c.chainName, runtimeName)
runtimeID, err := c.sourceCfg.ResolveRuntimeID(runtimeName)
if err != nil {
// Return a generic error here and log the detailed error. This is most likely a misconfiguration of the server.
c.logger.Error("runtime name to ID failure", "runtime", runtimeName, "chain", c.chainName, "err", err)
c.logger.Error("runtime name to ID failure", "runtime", runtimeName, "err", err)
return nil, apiCommon.ErrBadRuntime
}

Expand Down
3 changes: 3 additions & 0 deletions storage/oasis/nodeapi/history/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type HistoryRuntimeApiLite struct {
}

func NewHistoryRuntimeApiLite(ctx context.Context, history *config.History, sdkPT *sdkConfig.ParaTime, nodes map[string]*config.ArchiveConfig, fastStartup bool, runtime common.Runtime) (*HistoryRuntimeApiLite, error) {
if history == nil {
return nil, fmt.Errorf("history config not provided")
}
apis := map[string]nodeapi.RuntimeApiLite{}
for _, record := range history.Records {
if archiveConfig, ok := nodes[record.ArchiveName]; ok {
Expand Down
5 changes: 4 additions & 1 deletion tests/e2e/config/e2e-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ analysis:
migrations: file:///storage/migrations

server:
chain_name: localnet # unused
source:
custom_chain:
history: {}
sdk_network: {}
endpoint: 0.0.0.0:8008
storage:
endpoint: postgresql://indexer:password@nexus-postgres:5432/indexer?sslmode=disable
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e_regression/damask/e2e_config_1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ analysis:
migrations: file://storage/migrations

server:
chain_name: mainnet
source:
chain_name: mainnet
endpoint: localhost:8008
storage:
endpoint: postgresql://api:password@localhost:5432/indexer?sslmode=disable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"balances": [
{
"balance": "0",
"token_decimals": 0,
"token_symbol": ""
"token_decimals": 18,
"token_symbol": "ROSE"
}
],
"evm_balances": [],
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e_regression/eden/e2e_config_1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ analysis:
migrations: file://storage/migrations

server:
chain_name: mainnet
source:
chain_name: mainnet
endpoint: localhost:8008
storage:
endpoint: postgresql://api:password@localhost:5432/indexer?sslmode=disable
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e_regression/edenfast/e2e_config_1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ analysis:
migrations: file://storage/migrations

server:
chain_name: mainnet
source:
chain_name: mainnet
endpoint: localhost:8008
storage:
endpoint: postgresql://api:password@localhost:5432/indexer?sslmode=disable
Expand Down

0 comments on commit 5b7aa04

Please sign in to comment.