Skip to content

Commit

Permalink
core/services: flag duplicate service names (#10843)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Oct 4, 2023
1 parent 346c942 commit 936ec09
Show file tree
Hide file tree
Showing 28 changed files with 140 additions and 130 deletions.
4 changes: 2 additions & 2 deletions common/headtracker/head_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"golang.org/x/exp/maps"

htrktypes "github.com/smartcontractkit/chainlink/v2/common/headtracker/types"
"github.com/smartcontractkit/chainlink/v2/common/types"
"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

Expand Down Expand Up @@ -157,7 +157,7 @@ func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) HealthReport() map[string]error {
report := map[string]error{
ht.Name(): ht.StartStopOnce.Healthy(),
}
maps.Copy(report, ht.headListener.HealthReport())
services.CopyHealth(report, ht.headListener.HealthReport())
return report
}

Expand Down
9 changes: 4 additions & 5 deletions common/txmgr/txmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/google/uuid"
"github.com/pkg/errors"
"golang.org/x/exp/maps"

feetypes "github.com/smartcontractkit/chainlink/v2/common/fee/types"
txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types"
Expand Down Expand Up @@ -263,13 +262,13 @@ func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) HealthRepo

// only query if txm started properly
b.IfStarted(func() {
maps.Copy(report, b.broadcaster.HealthReport())
maps.Copy(report, b.confirmer.HealthReport())
maps.Copy(report, b.txAttemptBuilder.HealthReport())
services.CopyHealth(report, b.broadcaster.HealthReport())
services.CopyHealth(report, b.confirmer.HealthReport())
services.CopyHealth(report, b.txAttemptBuilder.HealthReport())
})

if b.txConfig.ForwardersEnabled() {
maps.Copy(report, b.fwdMgr.HealthReport())
services.CopyHealth(report, b.fwdMgr.HealthReport())
}
return report
}
Expand Down
11 changes: 5 additions & 6 deletions core/chains/evm/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"go.uber.org/multierr"
"golang.org/x/exp/maps"

"github.com/smartcontractkit/sqlx"

Expand Down Expand Up @@ -380,13 +379,13 @@ func (c *chain) HealthReport() map[string]error {
report := map[string]error{
c.Name(): c.StartStopOnce.Healthy(),
}
maps.Copy(report, c.txm.HealthReport())
maps.Copy(report, c.headBroadcaster.HealthReport())
maps.Copy(report, c.headTracker.HealthReport())
maps.Copy(report, c.logBroadcaster.HealthReport())
services.CopyHealth(report, c.txm.HealthReport())
services.CopyHealth(report, c.headBroadcaster.HealthReport())
services.CopyHealth(report, c.headTracker.HealthReport())
services.CopyHealth(report, c.logBroadcaster.HealthReport())

if c.balanceMonitor != nil {
maps.Copy(report, c.balanceMonitor.HealthReport())
services.CopyHealth(report, c.balanceMonitor.HealthReport())
}

return report
Expand Down
6 changes: 2 additions & 4 deletions core/chains/evm/gas/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"golang.org/x/exp/maps"

commonfee "github.com/smartcontractkit/chainlink/v2/common/fee"
feetypes "github.com/smartcontractkit/chainlink/v2/common/fee/types"
commontypes "github.com/smartcontractkit/chainlink/v2/common/types"
Expand Down Expand Up @@ -215,9 +213,9 @@ func (e *WrappedEvmEstimator) Ready() error {

func (e *WrappedEvmEstimator) HealthReport() map[string]error {
report := map[string]error{e.Name(): e.StartStopOnce.Healthy()}
maps.Copy(report, e.EvmEstimator.HealthReport())
services.CopyHealth(report, e.EvmEstimator.HealthReport())
if e.l1Oracle != nil {
maps.Copy(report, e.l1Oracle.HealthReport())
services.CopyHealth(report, e.l1Oracle.HealthReport())
}

return report
Expand Down
17 changes: 8 additions & 9 deletions core/chains/evm/headtracker/head_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"testing"
"time"

"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/store/models"

"github.com/ethereum/go-ethereum"
gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
Expand All @@ -24,16 +16,23 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

commonmocks "github.com/smartcontractkit/chainlink/v2/common/types/mocks"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker"
httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

Expand Down Expand Up @@ -402,7 +401,7 @@ func TestHeadTracker_Start_LoadsLatestChain(t *testing.T) {

gomega.NewWithT(t).Eventually(func() bool {
report := ht.headTracker.HealthReport()
maps.Copy(report, ht.headBroadcaster.HealthReport())
services.CopyHealth(report, ht.headBroadcaster.HealthReport())
return !slices.ContainsFunc(maps.Values(report), func(e error) bool { return e != nil })
}, 5*time.Second, testutils.TestInterval).Should(gomega.Equal(true))

Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/monitor/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewBalanceMonitor(ethClient evmclient.Client, ethKeyStore keystore.Eth, log
chainId := ethClient.ConfiguredChainID()
bm := &balanceMonitor{
utils.StartStopOnce{},
logger,
logger.Named("BalanceMonitor"),
ethClient,
chainId,
chainId.String(),
Expand Down
3 changes: 1 addition & 2 deletions core/chains/solana/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/gagliardetto/solana-go/rpc"
"github.com/pkg/errors"
"go.uber.org/multierr"
"golang.org/x/exp/maps"

"github.com/smartcontractkit/chainlink-relay/pkg/logger"
"github.com/smartcontractkit/chainlink-relay/pkg/loop"
Expand Down Expand Up @@ -387,7 +386,7 @@ func (c *chain) Ready() error {

func (c *chain) HealthReport() map[string]error {
report := map[string]error{c.Name(): c.StartStopOnce.Healthy()}
maps.Copy(report, c.txm.HealthReport())
services.CopyHealth(report, c.txm.HealthReport())
return report
}

Expand Down
5 changes: 2 additions & 3 deletions core/chains/starknet/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (

"github.com/pkg/errors"
"go.uber.org/multierr"
"golang.org/x/exp/maps"

"github.com/smartcontractkit/chainlink-relay/pkg/logger"
"github.com/smartcontractkit/chainlink-relay/pkg/loop"
relaytypes "github.com/smartcontractkit/chainlink-relay/pkg/types"

starkChain "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/chain"
starkchain "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/chain"
"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/config"
Expand All @@ -23,6 +21,7 @@ import (

"github.com/smartcontractkit/chainlink/v2/core/chains"
"github.com/smartcontractkit/chainlink/v2/core/chains/internal"
"github.com/smartcontractkit/chainlink/v2/core/services"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
Expand Down Expand Up @@ -164,7 +163,7 @@ func (c *chain) Ready() error {

func (c *chain) HealthReport() map[string]error {
report := map[string]error{c.Name(): c.StartStopOnce.Healthy()}
maps.Copy(report, c.txm.HealthReport())
services.CopyHealth(report, c.txm.HealthReport())
return report
}

Expand Down
54 changes: 30 additions & 24 deletions core/services/chainlink/relayer_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *RelayerFactory) NewEVM(ctx context.Context, config EVMFactoryConfig) (m
}
legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(evmRelayExtenders)
for _, ext := range evmRelayExtenders.Slice() {
relayID := relay.ID{Network: relay.EVM, ChainID: relay.ChainID(ext.Chain().ID().String())}
relayID := relay.ID{Network: relay.EVM, ChainID: ext.Chain().ID().String()}
chain, err2 := legacyChains.Get(relayID.ChainID)
if err2 != nil {
return nil, err2
Expand All @@ -68,7 +68,7 @@ func (r *RelayerFactory) NewEVM(ctx context.Context, config EVMFactoryConfig) (m
CSAETHKeystore: config.CSAETHKeystore,
EventBroadcaster: ccOpts.EventBroadcaster,
}
relayer, err2 := evmrelay.NewRelayer(ccOpts.Logger, chain, relayerOpts)
relayer, err2 := evmrelay.NewRelayer(ccOpts.Logger.Named(relayID.ChainID), chain, relayerOpts)
if err2 != nil {
err = errors.Join(err, err2)
continue
Expand Down Expand Up @@ -97,19 +97,21 @@ func (r *RelayerFactory) NewSolana(ks keystore.Solana, chainCfgs solana.SolanaCo
// create one relayer per chain id
for _, chainCfg := range chainCfgs {

relayId := relay.ID{Network: relay.Solana, ChainID: relay.ChainID(*chainCfg.ChainID)}
_, alreadyExists := unique[relayId.Name()]
relayID := relay.ID{Network: relay.Solana, ChainID: *chainCfg.ChainID}
_, alreadyExists := unique[relayID.Name()]
if alreadyExists {
return nil, fmt.Errorf("duplicate chain definitions for %s", relayId.Name())
return nil, fmt.Errorf("duplicate chain definitions for %s", relayID.Name())
}
unique[relayId.Name()] = struct{}{}
unique[relayID.Name()] = struct{}{}

// skip disabled chains from further processing
if !chainCfg.IsEnabled() {
solLggr.Warnw("Skipping disabled chain", "id", chainCfg.ChainID)
continue
}

lggr := solLggr.Named(relayID.ChainID)

if cmdName := env.SolanaPluginCmd.Get(); cmdName != "" {

// setup the solana relayer to be a LOOP
Expand All @@ -122,27 +124,27 @@ func (r *RelayerFactory) NewSolana(ks keystore.Solana, chainCfgs solana.SolanaCo
}

solCmdFn, err := plugins.NewCmdFactory(r.Register, plugins.CmdConfig{
ID: relayId.Name(),
ID: relayID.Name(),
Cmd: cmdName,
})
if err != nil {
return nil, fmt.Errorf("failed to create Solana LOOP command: %w", err)
}

solanaRelayers[relayId] = loop.NewRelayerService(solLggr, r.GRPCOpts, solCmdFn, string(cfgTOML), signer)
solanaRelayers[relayID] = loop.NewRelayerService(lggr, r.GRPCOpts, solCmdFn, string(cfgTOML), signer)

} else {
// fallback to embedded chain
opts := solana.ChainOpts{
Logger: solLggr,
Logger: lggr,
KeyStore: signer,
}

chain, err := solana.NewChain(chainCfg, opts)
if err != nil {
return nil, err
}
solanaRelayers[relayId] = relay.NewRelayerServerAdapter(pkgsolana.NewRelayer(solLggr, chain), chain)
solanaRelayers[relayID] = relay.NewRelayerServerAdapter(pkgsolana.NewRelayer(lggr, chain), chain)
}
}
return solanaRelayers, nil
Expand All @@ -166,19 +168,21 @@ func (r *RelayerFactory) NewStarkNet(ks keystore.StarkNet, chainCfgs starknet.St
unique := make(map[string]struct{})
// create one relayer per chain id
for _, chainCfg := range chainCfgs {
relayId := relay.ID{Network: relay.StarkNet, ChainID: relay.ChainID(*chainCfg.ChainID)}
_, alreadyExists := unique[relayId.Name()]
relayID := relay.ID{Network: relay.StarkNet, ChainID: *chainCfg.ChainID}
_, alreadyExists := unique[relayID.Name()]
if alreadyExists {
return nil, fmt.Errorf("duplicate chain definitions for %s", relayId.Name())
return nil, fmt.Errorf("duplicate chain definitions for %s", relayID.Name())
}
unique[relayId.Name()] = struct{}{}
unique[relayID.Name()] = struct{}{}

// skip disabled chains from further processing
if !chainCfg.IsEnabled() {
starkLggr.Warnw("Skipping disabled chain", "id", chainCfg.ChainID)
continue
}

lggr := starkLggr.Named(relayID.ChainID)

if cmdName := env.StarknetPluginCmd.Get(); cmdName != "" {
// setup the starknet relayer to be a LOOP
cfgTOML, err := toml.Marshal(struct {
Expand All @@ -189,19 +193,19 @@ func (r *RelayerFactory) NewStarkNet(ks keystore.StarkNet, chainCfgs starknet.St
}

starknetCmdFn, err := plugins.NewCmdFactory(r.Register, plugins.CmdConfig{
ID: relayId.Name(),
ID: relayID.Name(),
Cmd: cmdName,
})
if err != nil {
return nil, fmt.Errorf("failed to create StarkNet LOOP command: %w", err)
}
// the starknet relayer service has a delicate keystore dependency. the value that is passed to NewRelayerService must
// be compatible with instantiating a starknet transaction manager KeystoreAdapter within the LOOPp executable.
starknetRelayers[relayId] = loop.NewRelayerService(starkLggr, r.GRPCOpts, starknetCmdFn, string(cfgTOML), loopKs)
starknetRelayers[relayID] = loop.NewRelayerService(lggr, r.GRPCOpts, starknetCmdFn, string(cfgTOML), loopKs)
} else {
// fallback to embedded chain
opts := starknet.ChainOpts{
Logger: starkLggr,
Logger: lggr,
KeyStore: loopKs,
}

Expand All @@ -210,7 +214,7 @@ func (r *RelayerFactory) NewStarkNet(ks keystore.StarkNet, chainCfgs starknet.St
return nil, err
}

starknetRelayers[relayId] = relay.NewRelayerServerAdapter(pkgstarknet.NewRelayer(starkLggr, chain), chain)
starknetRelayers[relayID] = relay.NewRelayerServerAdapter(pkgstarknet.NewRelayer(lggr, chain), chain)
}
}
return starknetRelayers, nil
Expand Down Expand Up @@ -257,28 +261,30 @@ func (r *RelayerFactory) NewCosmos(ctx context.Context, config CosmosFactoryConf
relayers := make(map[relay.ID]cosmos.LoopRelayerChainer)

var (
lggr = r.Logger.Named("Cosmos")
loopKs = &keystore.CosmosLoopKeystore{Cosmos: config.Keystore}
cosmosLggr = r.Logger.Named("Cosmos")
loopKs = &keystore.CosmosLoopKeystore{Cosmos: config.Keystore}
)

// create one relayer per chain id
for _, chainCfg := range config.CosmosConfigs {
relayId := relay.ID{Network: relay.Cosmos, ChainID: relay.ChainID(*chainCfg.ChainID)}
relayID := relay.ID{Network: relay.Cosmos, ChainID: *chainCfg.ChainID}

lggr := cosmosLggr.Named(relayID.ChainID)

opts := cosmos.ChainOpts{
QueryConfig: config.QConfig,
Logger: lggr.Named(relayId.ChainID),
Logger: lggr,
DB: config.DB,
KeyStore: loopKs,
EventBroadcaster: config.EventBroadcaster,
}

chain, err := cosmos.NewChain(chainCfg, opts)
if err != nil {
return nil, fmt.Errorf("failed to load Cosmos chain %q: %w", relayId, err)
return nil, fmt.Errorf("failed to load Cosmos chain %q: %w", relayID, err)
}

relayers[relayId] = cosmos.NewLoopRelayerChain(pkgcosmos.NewRelayer(lggr, chain), chain)
relayers[relayID] = cosmos.NewLoopRelayerChain(pkgcosmos.NewRelayer(lggr, chain), chain)

}
return relayers, nil
Expand Down
Loading

0 comments on commit 936ec09

Please sign in to comment.