From 0a91bed43e9c87768b86bd100c0b36c764dd6481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Wed, 11 Dec 2024 15:44:08 +0900 Subject: [PATCH] web,cmd: Generalize chains API Make it easier to add new LOOPs by reducing the amount of boilerplate --- core/cmd/app.go | 12 +- core/cmd/chains_commands.go | 60 +- core/cmd/cosmos_chains_commands.go | 48 -- core/cmd/cosmos_chains_commands_test.go | 33 - core/cmd/evm_chains_commands.go | 48 -- core/cmd/evm_chains_commands_test.go | 38 -- core/cmd/nodes_commands.go | 4 +- core/cmd/solana_chains_commands.go | 48 -- core/cmd/solana_chains_commands_test.go | 32 - core/cmd/starknet_chains_commands.go | 48 -- .../chainlink/relayer_chain_interoperators.go | 9 +- core/web/chains_controller.go | 51 +- core/web/chains_controller_test.go | 585 ++++++++++++++++++ core/web/cosmos_chains_controller.go | 17 - core/web/cosmos_chains_controller_test.go | 193 ------ core/web/evm_chains_controller.go | 19 - core/web/evm_chains_controller_test.go | 215 ------- core/web/evm_nodes_controller.go | 3 + core/web/presenters/chain.go | 30 + core/web/presenters/cosmos_chain.go | 19 - core/web/presenters/evm_chain.go | 19 - core/web/presenters/solana_chain.go | 19 - core/web/presenters/starknet_chain.go | 19 - core/web/router.go | 23 +- core/web/solana_chains_controller.go | 17 - core/web/solana_chains_controller_test.go | 216 ------- core/web/starknet_chains_controller.go | 17 - 27 files changed, 723 insertions(+), 1119 deletions(-) delete mode 100644 core/cmd/cosmos_chains_commands.go delete mode 100644 core/cmd/cosmos_chains_commands_test.go delete mode 100644 core/cmd/evm_chains_commands.go delete mode 100644 core/cmd/evm_chains_commands_test.go delete mode 100644 core/cmd/solana_chains_commands.go delete mode 100644 core/cmd/solana_chains_commands_test.go delete mode 100644 core/cmd/starknet_chains_commands.go create mode 100644 core/web/chains_controller_test.go delete mode 100644 core/web/cosmos_chains_controller.go delete mode 100644 core/web/cosmos_chains_controller_test.go delete mode 100644 core/web/evm_chains_controller.go delete mode 100644 core/web/evm_chains_controller_test.go delete mode 100644 core/web/solana_chains_controller.go delete mode 100644 core/web/solana_chains_controller_test.go delete mode 100644 core/web/starknet_chains_controller.go diff --git a/core/cmd/app.go b/core/cmd/app.go index ad944f0d0a6..0ec4d0f7c81 100644 --- a/core/cmd/app.go +++ b/core/cmd/app.go @@ -290,15 +290,9 @@ func NewApp(s *Shell) *cli.App { }, }, { - Name: "chains", - Usage: "Commands for handling chain configuration", - Subcommands: cli.Commands{ - chainCommand("EVM", EVMChainClient(s), cli.Int64Flag{Name: "id", Usage: "chain ID"}), - chainCommand("Cosmos", CosmosChainClient(s), cli.StringFlag{Name: "id", Usage: "chain ID"}), - chainCommand("Solana", SolanaChainClient(s), - cli.StringFlag{Name: "id", Usage: "chain ID, options: [mainnet, testnet, devnet, localnet]"}), - chainCommand("StarkNet", StarkNetChainClient(s), cli.StringFlag{Name: "id", Usage: "chain ID"}), - }, + Name: "chains", + Usage: "Commands for handling chain configuration", + Subcommands: initChainSubCmds(s), }, { Name: "nodes", diff --git a/core/cmd/chains_commands.go b/core/cmd/chains_commands.go index 6edb5afc5ba..9eb71de479a 100644 --- a/core/cmd/chains_commands.go +++ b/core/cmd/chains_commands.go @@ -2,8 +2,11 @@ package cmd import ( "fmt" + "strconv" "strings" + "github.com/smartcontractkit/chainlink/v2/core/services/relay" + "github.com/smartcontractkit/chainlink/v2/core/web/presenters" "github.com/urfave/cli" ) @@ -39,12 +42,12 @@ type chainClient[P TableRenderer] struct { path string } -// newChainClient returns a new ChainClient for a particular type of chains.Config. +// NewChainClient returns a new ChainClient for a particular type of chains.Config. // P is a TableRenderer corresponding to R, and P2 is the slice variant (type P2 []P). -func newChainClient[P TableRenderer](s *Shell, name string) ChainClient { - return &chainClient[P]{ +func NewChainClient(s *Shell, network string) ChainClient { + return &chainClient[ChainPresenter]{ Shell: s, - path: "/v2/chains/" + name, + path: "/v2/chains/" + network, } } @@ -53,3 +56,52 @@ func (cli *chainClient[P]) IndexChains(c *cli.Context) (err error) { var p P return cli.getPage(cli.path, c.Int("page"), &p) } + +// ChainPresenter implements TableRenderer for a ChainResource +type ChainPresenter struct { + presenters.ChainResource +} + +// ToRow presents the ChainResource as a slice of strings. +func (p *ChainPresenter) ToRow() []string { + return []string{p.GetID(), strconv.FormatBool(p.Enabled), p.Config} +} + +// RenderTable implements TableRenderer +// Just renders a single row +func (p ChainPresenter) RenderTable(rt RendererTable) error { + rows := [][]string{} + rows = append(rows, p.ToRow()) + + renderList(chainHeaders, rows, rt.Writer) + + return nil +} + +// ChainPresenters implements TableRenderer for a slice of ChainPresenters. +type ChainPresenters []ChainPresenter + +// RenderTable implements TableRenderer +func (ps ChainPresenters) RenderTable(rt RendererTable) error { + rows := [][]string{} + + for _, p := range ps { + rows = append(rows, p.ToRow()) + } + + renderList(chainHeaders, rows, rt.Writer) + + return nil +} + +func initChainSubCmds(s *Shell) []cli.Command { + var cmds []cli.Command + for network := range relay.SupportedNetworks { + if network == relay.NetworkDummy { + continue + } + cmds = append(cmds, chainCommand(network, NewChainClient(s, network), cli.StringFlag{Name: "id", Usage: "chain ID"})) + } + + return cmds +} diff --git a/core/cmd/cosmos_chains_commands.go b/core/cmd/cosmos_chains_commands.go deleted file mode 100644 index d58b1baa159..00000000000 --- a/core/cmd/cosmos_chains_commands.go +++ /dev/null @@ -1,48 +0,0 @@ -package cmd - -import ( - "strconv" - - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -// CosmosChainPresenter implements TableRenderer for a CosmosChainResource -type CosmosChainPresenter struct { - presenters.CosmosChainResource -} - -// ToRow presents the CosmosChainResource as a slice of strings. -func (p *CosmosChainPresenter) ToRow() []string { - return []string{p.GetID(), strconv.FormatBool(p.Enabled), p.Config} -} - -// RenderTable implements TableRenderer -// Just renders a single row -func (p CosmosChainPresenter) RenderTable(rt RendererTable) error { - rows := [][]string{} - rows = append(rows, p.ToRow()) - - renderList(chainHeaders, rows, rt.Writer) - - return nil -} - -// CosmosChainPresenters implements TableRenderer for a slice of CosmosChainPresenters. -type CosmosChainPresenters []CosmosChainPresenter - -// RenderTable implements TableRenderer -func (ps CosmosChainPresenters) RenderTable(rt RendererTable) error { - rows := [][]string{} - - for _, p := range ps { - rows = append(rows, p.ToRow()) - } - - renderList(chainHeaders, rows, rt.Writer) - - return nil -} - -func CosmosChainClient(s *Shell) ChainClient { - return newChainClient[CosmosChainPresenters](s, "cosmos") -} diff --git a/core/cmd/cosmos_chains_commands_test.go b/core/cmd/cosmos_chains_commands_test.go deleted file mode 100644 index a0d2052d836..00000000000 --- a/core/cmd/cosmos_chains_commands_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package cmd_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - coscfg "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/config" - - "github.com/smartcontractkit/chainlink/v2/core/cmd" - "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/cosmostest" -) - -func TestShell_IndexCosmosChains(t *testing.T) { - t.Parallel() - - chainID := cosmostest.RandomChainID() - chain := coscfg.TOMLConfig{ - ChainID: ptr(chainID), - Enabled: ptr(true), - } - app := cosmosStartNewApplication(t, &chain) - client, r := app.NewShellAndRenderer() - - require.Nil(t, cmd.CosmosChainClient(client).IndexChains(cltest.EmptyCLIContext())) - chains := *r.Renders[0].(*cmd.CosmosChainPresenters) - require.Len(t, chains, 1) - c := chains[0] - assert.Equal(t, chainID, c.ID) - assertTableRenders(t, r) -} diff --git a/core/cmd/evm_chains_commands.go b/core/cmd/evm_chains_commands.go deleted file mode 100644 index d4025cfca53..00000000000 --- a/core/cmd/evm_chains_commands.go +++ /dev/null @@ -1,48 +0,0 @@ -package cmd - -import ( - "strconv" - - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -// EVMChainPresenter implements TableRenderer for an EVMChainResource. -type EVMChainPresenter struct { - presenters.EVMChainResource -} - -// ToRow presents the EVMChainResource as a slice of strings. -func (p *EVMChainPresenter) ToRow() []string { - return []string{p.GetID(), strconv.FormatBool(p.Enabled), p.Config} -} - -// RenderTable implements TableRenderer -// Just renders a single row -func (p EVMChainPresenter) RenderTable(rt RendererTable) error { - rows := [][]string{} - rows = append(rows, p.ToRow()) - - renderList(chainHeaders, rows, rt.Writer) - - return nil -} - -// EVMChainPresenters implements TableRenderer for a slice of EVMChainPresenters. -type EVMChainPresenters []EVMChainPresenter - -// RenderTable implements TableRenderer -func (ps EVMChainPresenters) RenderTable(rt RendererTable) error { - rows := [][]string{} - - for _, p := range ps { - rows = append(rows, p.ToRow()) - } - - renderList(chainHeaders, rows, rt.Writer) - - return nil -} - -func EVMChainClient(s *Shell) ChainClient { - return newChainClient[EVMChainPresenters](s, "evm") -} diff --git a/core/cmd/evm_chains_commands_test.go b/core/cmd/evm_chains_commands_test.go deleted file mode 100644 index fa6d7bb519c..00000000000 --- a/core/cmd/evm_chains_commands_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package cmd_test - -import ( - "strconv" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - client2 "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" - "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" - "github.com/smartcontractkit/chainlink/v2/core/cmd" - "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" -) - -func newRandChainID() *big.Big { - return big.New(testutils.NewRandomEVMChainID()) -} - -func TestShell_IndexEVMChains(t *testing.T) { - t.Parallel() - - app := startNewApplicationV2(t, func(c *chainlink.Config, s *chainlink.Secrets) { - c.EVM[0].Enabled = ptr(true) - c.EVM[0].NonceAutoSync = ptr(false) - c.EVM[0].BalanceMonitor.Enabled = ptr(false) - }) - client, r := app.NewShellAndRenderer() - - require.Nil(t, cmd.EVMChainClient(client).IndexChains(cltest.EmptyCLIContext())) - chains := *r.Renders[0].(*cmd.EVMChainPresenters) - require.Len(t, chains, 1) - c := chains[0] - assert.Equal(t, strconv.Itoa(client2.NullClientChainID), c.ID) - assertTableRenders(t, r) -} diff --git a/core/cmd/nodes_commands.go b/core/cmd/nodes_commands.go index efee10bb156..63453f75e35 100644 --- a/core/cmd/nodes_commands.go +++ b/core/cmd/nodes_commands.go @@ -52,10 +52,10 @@ type nodeClient[P TableRenderer] struct { // newNodeClient returns a new NodeClient for a particular type of NodeStatus. // P is a TableRenderer for []types.NodeStatus. -func newNodeClient[P TableRenderer](s *Shell, name string) NodeClient { +func newNodeClient[P TableRenderer](s *Shell, network string) NodeClient { return &nodeClient[P]{ Shell: s, - path: "/v2/nodes/" + name, + path: "/v2/nodes/" + network, } } diff --git a/core/cmd/solana_chains_commands.go b/core/cmd/solana_chains_commands.go deleted file mode 100644 index aa2a07c0f8c..00000000000 --- a/core/cmd/solana_chains_commands.go +++ /dev/null @@ -1,48 +0,0 @@ -package cmd - -import ( - "strconv" - - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -// SolanaChainPresenter implements TableRenderer for a SolanaChainResource -type SolanaChainPresenter struct { - presenters.SolanaChainResource -} - -// ToRow presents the SolanaChainResource as a slice of strings. -func (p *SolanaChainPresenter) ToRow() []string { - return []string{p.GetID(), strconv.FormatBool(p.Enabled), p.Config} -} - -// RenderTable implements TableRenderer -// Just renders a single row -func (p SolanaChainPresenter) RenderTable(rt RendererTable) error { - rows := [][]string{} - rows = append(rows, p.ToRow()) - - renderList(chainHeaders, rows, rt.Writer) - - return nil -} - -// SolanaChainPresenters implements TableRenderer for a slice of SolanaChainPresenters. -type SolanaChainPresenters []SolanaChainPresenter - -// RenderTable implements TableRenderer -func (ps SolanaChainPresenters) RenderTable(rt RendererTable) error { - rows := [][]string{} - - for _, p := range ps { - rows = append(rows, p.ToRow()) - } - - renderList(chainHeaders, rows, rt.Writer) - - return nil -} - -func SolanaChainClient(s *Shell) ChainClient { - return newChainClient[SolanaChainPresenters](s, "solana") -} diff --git a/core/cmd/solana_chains_commands_test.go b/core/cmd/solana_chains_commands_test.go deleted file mode 100644 index e374ba11c65..00000000000 --- a/core/cmd/solana_chains_commands_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package cmd_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - solcfg "github.com/smartcontractkit/chainlink-solana/pkg/solana/config" - "github.com/smartcontractkit/chainlink/v2/core/cmd" - "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/solanatest" -) - -func TestShell_IndexSolanaChains(t *testing.T) { - t.Parallel() - - id := solanatest.RandomChainID() - cfg := solcfg.TOMLConfig{ - ChainID: &id, - Enabled: ptr(true), - } - app := solanaStartNewApplication(t, &cfg) - client, r := app.NewShellAndRenderer() - - require.Nil(t, cmd.SolanaChainClient(client).IndexChains(cltest.EmptyCLIContext())) - chains := *r.Renders[0].(*cmd.SolanaChainPresenters) - require.Len(t, chains, 1) - c := chains[0] - assert.Equal(t, id, c.ID) - assertTableRenders(t, r) -} diff --git a/core/cmd/starknet_chains_commands.go b/core/cmd/starknet_chains_commands.go deleted file mode 100644 index 5b20b37ae22..00000000000 --- a/core/cmd/starknet_chains_commands.go +++ /dev/null @@ -1,48 +0,0 @@ -package cmd - -import ( - "strconv" - - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -// StarkNetChainPresenter implements TableRenderer for a StarkNetChainResource -type StarkNetChainPresenter struct { - presenters.StarkNetChainResource -} - -// ToRow presents the StarkNetChainResource as a slice of strings. -func (p *StarkNetChainPresenter) ToRow() []string { - return []string{p.GetID(), strconv.FormatBool(p.Enabled), p.Config} -} - -// RenderTable implements TableRenderer -// Just renders a single row -func (p StarkNetChainPresenter) RenderTable(rt RendererTable) error { - rows := [][]string{} - rows = append(rows, p.ToRow()) - - renderList(chainHeaders, rows, rt.Writer) - - return nil -} - -// StarkNetChainPresenters implements TableRenderer for a slice of StarkNetChainPresenters. -type StarkNetChainPresenters []StarkNetChainPresenter - -// RenderTable implements TableRenderer -func (ps StarkNetChainPresenters) RenderTable(rt RendererTable) error { - rows := [][]string{} - - for _, p := range ps { - rows = append(rows, p.ToRow()) - } - - renderList(chainHeaders, rows, rt.Writer) - - return nil -} - -func StarkNetChainClient(s *Shell) ChainClient { - return newChainClient[StarkNetChainPresenters](s, "starknet") -} diff --git a/core/services/chainlink/relayer_chain_interoperators.go b/core/services/chainlink/relayer_chain_interoperators.go index 1be6e9337d1..2fc671bfe6e 100644 --- a/core/services/chainlink/relayer_chain_interoperators.go +++ b/core/services/chainlink/relayer_chain_interoperators.go @@ -14,6 +14,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/relay" + commonTypes "github.com/smartcontractkit/chainlink/v2/common/types" "github.com/smartcontractkit/chainlink/v2/core/chains" "github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm" "github.com/smartcontractkit/chainlink/v2/core/services" @@ -53,7 +54,7 @@ type LegacyChainer interface { type ChainStatuser interface { ChainStatus(ctx context.Context, id types.RelayID) (types.ChainStatus, error) - ChainStatuses(ctx context.Context, offset, limit int) ([]types.ChainStatus, int, error) + ChainStatuses(ctx context.Context, offset, limit int) ([]commonTypes.ChainStatusWithID, int, error) } // NodesStatuser is an interface for node configuration and state. @@ -261,9 +262,9 @@ func (rs *CoreRelayerChainInteroperators) ChainStatus(ctx context.Context, id ty return lr.GetChainStatus(ctx) } -func (rs *CoreRelayerChainInteroperators) ChainStatuses(ctx context.Context, offset, limit int) ([]types.ChainStatus, int, error) { +func (rs *CoreRelayerChainInteroperators) ChainStatuses(ctx context.Context, offset, limit int) ([]commonTypes.ChainStatusWithID, int, error) { var ( - stats []types.ChainStatus + stats []commonTypes.ChainStatusWithID totalErr error ) rs.mu.Lock() @@ -283,7 +284,7 @@ func (rs *CoreRelayerChainInteroperators) ChainStatuses(ctx context.Context, off totalErr = errors.Join(totalErr, err) continue } - stats = append(stats, stat) + stats = append(stats, commonTypes.ChainStatusWithID{ChainStatus: stat, RelayID: rid}) } if totalErr != nil { diff --git a/core/web/chains_controller.go b/core/web/chains_controller.go index 6bc5ee4daa3..a899b26b26c 100644 --- a/core/web/chains_controller.go +++ b/core/web/chains_controller.go @@ -5,13 +5,14 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/manyminds/api2go/jsonapi" "github.com/smartcontractkit/chainlink-common/pkg/types" + commonTypes "github.com/smartcontractkit/chainlink/v2/common/types" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/logger/audit" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" + "github.com/smartcontractkit/chainlink/v2/core/web/presenters" ) type ChainsController interface { @@ -21,16 +22,6 @@ type ChainsController interface { Show(*gin.Context) } -type chainsController[R jsonapi.EntityNamer] struct { - network string - resourceName string - chainStats chainlink.ChainStatuser - errNotEnabled error - newResource func(types.ChainStatus) R - lggr logger.Logger - auditLogger audit.AuditLogger -} - type errChainDisabled struct { name string tomlKey string @@ -40,11 +31,17 @@ func (e errChainDisabled) Error() string { return fmt.Sprintf("%s is disabled: Set %s=true to enable", e.name, e.tomlKey) } -func newChainsController[R jsonapi.EntityNamer](network string, chainStats chainlink.ChainsNodesStatuser, errNotEnabled error, - newResource func(types.ChainStatus) R, lggr logger.Logger, auditLogger audit.AuditLogger) *chainsController[R] { - return &chainsController[R]{ - network: network, - resourceName: network + "_chain", +type chainsController struct { + chainStats chainlink.RelayerChainInteroperators + errNotEnabled error + newResource func(commonTypes.ChainStatusWithID) presenters.ChainResource + lggr logger.Logger + auditLogger audit.AuditLogger +} + +func NewChainsController(chainStats chainlink.RelayerChainInteroperators, errNotEnabled error, + newResource func(commonTypes.ChainStatusWithID) presenters.ChainResource, lggr logger.Logger, auditLogger audit.AuditLogger) *chainsController { + return &chainsController{ chainStats: chainStats, errNotEnabled: errNotEnabled, newResource: newResource, @@ -53,37 +50,45 @@ func newChainsController[R jsonapi.EntityNamer](network string, chainStats chain } } -func (cc *chainsController[R]) Index(c *gin.Context, size, page, offset int) { +func (cc *chainsController) Index(c *gin.Context, size, page, offset int) { if cc.chainStats == nil { jsonAPIError(c, http.StatusBadRequest, cc.errNotEnabled) return } - chains, count, err := cc.chainStats.ChainStatuses(c.Request.Context(), offset, size) + + chainStats := cc.chainStats + + if network := c.Param("network"); network != "" { + chainStats = chainStats.List(chainlink.FilterRelayersByType(network)) + } + + chains, count, err := chainStats.ChainStatuses(c.Request.Context(), offset, size) if err != nil { jsonAPIError(c, http.StatusBadRequest, err) return } - var resources []R + var resources []presenters.ChainResource for _, chain := range chains { resources = append(resources, cc.newResource(chain)) } - paginatedResponse(c, cc.resourceName, size, page, resources, count, err) + paginatedResponse(c, "chain", size, page, resources, count, err) } -func (cc *chainsController[R]) Show(c *gin.Context) { +func (cc *chainsController) Show(c *gin.Context) { if cc.chainStats == nil { jsonAPIError(c, http.StatusBadRequest, cc.errNotEnabled) return } - relayID := types.RelayID{Network: cc.network, ChainID: c.Param("ID")} + relayID := types.RelayID{Network: c.Param("network"), ChainID: c.Param("ID")} chain, err := cc.chainStats.ChainStatus(c.Request.Context(), relayID) + status := commonTypes.ChainStatusWithID{ChainStatus: chain, RelayID: relayID} if err != nil { jsonAPIError(c, http.StatusBadRequest, err) return } - jsonAPIResponse(c, cc.newResource(chain), cc.resourceName) + jsonAPIResponse(c, cc.newResource(status), "chain") } diff --git a/core/web/chains_controller_test.go b/core/web/chains_controller_test.go new file mode 100644 index 00000000000..3e74ddafa20 --- /dev/null +++ b/core/web/chains_controller_test.go @@ -0,0 +1,585 @@ +package web_test + +import ( + "fmt" + "math/big" + "net/http" + "sort" + "testing" + "time" + + "github.com/manyminds/api2go/jsonapi" + "github.com/shopspring/decimal" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/exp/rand" + + commoncfg "github.com/smartcontractkit/chainlink-common/pkg/config" + commonTypes "github.com/smartcontractkit/chainlink-common/pkg/types" + coscfg "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/config" + "github.com/smartcontractkit/chainlink-solana/pkg/solana/config" + + evmcfg "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" + ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" + "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" + "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" + "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest" + "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/cosmostest" + "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" + "github.com/smartcontractkit/chainlink/v2/core/web" + "github.com/smartcontractkit/chainlink/v2/core/web/presenters" +) + +func Test_EVMChainsController_Show(t *testing.T) { + t.Parallel() + + validId := ubig.New(testutils.NewRandomEVMChainID()) + + testCases := []struct { + name string + inputId string + wantStatusCode int + want *evmcfg.EVMConfig + }{ + { + inputId: validId.String(), + name: "success", + want: &evmcfg.EVMConfig{ + ChainID: validId, + Enabled: ptr(true), + Chain: evmcfg.Defaults(nil, &evmcfg.Chain{ + GasEstimator: evmcfg.GasEstimator{ + EIP1559DynamicFees: ptr(true), + BlockHistory: evmcfg.BlockHistoryEstimator{ + BlockHistorySize: ptr[uint16](50), + }, + }, + RPCBlockQueryDelay: ptr[uint16](23), + MinIncomingConfirmations: ptr[uint32](12), + LinkContractAddress: ptr(types.EIP55AddressFromAddress(testutils.NewAddress())), + }), + }, + wantStatusCode: http.StatusOK, + }, + { + inputId: "invalidid", + name: "invalid id", + want: nil, + wantStatusCode: http.StatusBadRequest, + }, + { + inputId: "234", + name: "not found", + want: nil, + wantStatusCode: http.StatusBadRequest, + }, + } + + for _, testCase := range testCases { + tc := testCase + + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + controller := setupEVMChainsControllerTest(t, configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { + if tc.want != nil { + c.EVM = evmcfg.EVMConfigs{tc.want} + } + })) + + wantedResult := tc.want + resp, cleanup := controller.client.Get( + fmt.Sprintf("/v2/chains/evm/%s", tc.inputId), + ) + t.Cleanup(cleanup) + require.Equal(t, tc.wantStatusCode, resp.StatusCode) + + if wantedResult != nil { + resource1 := presenters.ChainResource{} + err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, resp), &resource1) + require.NoError(t, err) + + assert.Equal(t, resource1.ID, wantedResult.ChainID.String()) + toml, err := wantedResult.TOMLString() + require.NoError(t, err) + assert.Equal(t, toml, resource1.Config) + } + }) + } +} + +func Test_EVMChainsController_Index(t *testing.T) { + t.Parallel() + + // sort test chain ids to make expected comparison easy + chainIDs := []*big.Int{testutils.NewRandomEVMChainID(), testutils.NewRandomEVMChainID(), testutils.NewRandomEVMChainID()} + sort.Slice(chainIDs, func(i, j int) bool { + return chainIDs[i].String() < chainIDs[j].String() + }) + + configuredChains := evmcfg.EVMConfigs{ + {ChainID: ubig.New(chainIDs[0]), Chain: evmcfg.Defaults(nil)}, + { + ChainID: ubig.New(chainIDs[1]), + Chain: evmcfg.Defaults(nil, &evmcfg.Chain{ + RPCBlockQueryDelay: ptr[uint16](13), + GasEstimator: evmcfg.GasEstimator{ + EIP1559DynamicFees: ptr(true), + BlockHistory: evmcfg.BlockHistoryEstimator{ + BlockHistorySize: ptr[uint16](1), + }, + }, + MinIncomingConfirmations: ptr[uint32](120), + }), + }, + { + ChainID: ubig.New(chainIDs[2]), + Chain: evmcfg.Defaults(nil, &evmcfg.Chain{ + RPCBlockQueryDelay: ptr[uint16](5), + GasEstimator: evmcfg.GasEstimator{ + EIP1559DynamicFees: ptr(false), + BlockHistory: evmcfg.BlockHistoryEstimator{ + BlockHistorySize: ptr[uint16](2), + }, + }, + MinIncomingConfirmations: ptr[uint32](30), + }), + }, + } + + assert.Len(t, configuredChains, 3) + controller := setupEVMChainsControllerTest(t, configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { + c.EVM = append(c.EVM, configuredChains...) + })) + + badResp, cleanup := controller.client.Get("/v2/chains/evm?size=asd") + t.Cleanup(cleanup) + require.Equal(t, http.StatusUnprocessableEntity, badResp.StatusCode) + + resp, cleanup := controller.client.Get("/v2/chains/evm?size=3") + t.Cleanup(cleanup) + require.Equal(t, http.StatusOK, resp.StatusCode) + + body := cltest.ParseResponseBody(t, resp) + + metaCount, err := cltest.ParseJSONAPIResponseMetaCount(body) + require.NoError(t, err) + require.Equal(t, 1+len(configuredChains), metaCount) + + var links jsonapi.Links + + var gotChains []presenters.ChainResource + err = web.ParsePaginatedResponse(body, &gotChains, &links) + assert.NoError(t, err) + assert.NotEmpty(t, links["next"].Href) + assert.Empty(t, links["prev"].Href) + + assert.Len(t, links, 1) + // the difference in index value here seems to be due to the fact + // that cltest always has a default EVM chain, which is the off-by-one + // in the indices + assert.Equal(t, gotChains[2].ID, configuredChains[1].ChainID.String()) + toml, err := configuredChains[1].TOMLString() + require.NoError(t, err) + assert.Equal(t, toml, gotChains[2].Config) + + resp, cleanup = controller.client.Get(links["next"].Href) + t.Cleanup(cleanup) + require.Equal(t, http.StatusOK, resp.StatusCode) + + gotChains = []presenters.ChainResource{} + err = web.ParsePaginatedResponse(cltest.ParseResponseBody(t, resp), &gotChains, &links) + assert.NoError(t, err) + assert.Empty(t, links["next"].Href) + assert.NotEmpty(t, links["prev"].Href) + + assert.Len(t, links, 1) + assert.Equal(t, gotChains[0].ID, configuredChains[2].ChainID.String()) + toml, err = configuredChains[2].TOMLString() + require.NoError(t, err) + assert.Equal(t, toml, gotChains[0].Config) +} + +type TestEVMChainsController struct { + app *cltest.TestApplication + client cltest.HTTPClientCleaner +} + +func setupEVMChainsControllerTest(t *testing.T, cfg chainlink.GeneralConfig) *TestEVMChainsController { + // Using this instead of `NewApplicationEVMDisabled` since we need the chain set to be loaded in the app + // for the sake of the API endpoints to work properly + app := cltest.NewApplicationWithConfig(t, cfg) + ctx := testutils.Context(t) + require.NoError(t, app.Start(ctx)) + + client := app.NewHTTPClient(nil) + + return &TestEVMChainsController{ + app: app, + client: client, + } +} + +func ptr[T any](t T) *T { return &t } + +func Test_CosmosChainsController_Show(t *testing.T) { + t.Parallel() + + const validId = "Chainlink-12" + + testCases := []struct { + name string + inputId string + wantStatusCode int + want func(t *testing.T, app *cltest.TestApplication) *commonTypes.ChainStatus + }{ + { + inputId: validId, + name: "success", + want: func(t *testing.T, app *cltest.TestApplication) *commonTypes.ChainStatus { + return &commonTypes.ChainStatus{ + ID: validId, + Enabled: true, + Config: `ChainID = 'Chainlink-12' +Enabled = true +Bech32Prefix = 'wasm' +BlockRate = '6s' +BlocksUntilTxTimeout = 30 +ConfirmPollPeriod = '1s' +FallbackGasPrice = '9.999' +GasToken = 'ucosm' +GasLimitMultiplier = '1.55555' +MaxMsgsPerBatch = 100 +OCR2CachePollPeriod = '4s' +OCR2CacheTTL = '1m0s' +TxMsgTimeout = '10m0s' +Nodes = [] +`, + } + }, + wantStatusCode: http.StatusOK, + }, + { + inputId: "234", + name: "not found", + want: func(t *testing.T, app *cltest.TestApplication) *commonTypes.ChainStatus { + return nil + }, + wantStatusCode: http.StatusBadRequest, + }, + } + + for _, testCase := range testCases { + tc := testCase + + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + controller := setupCosmosChainsControllerTestV2(t, &coscfg.TOMLConfig{ + ChainID: ptr(validId), + Enabled: ptr(true), + Chain: coscfg.Chain{ + FallbackGasPrice: ptr(decimal.RequireFromString("9.999")), + GasLimitMultiplier: ptr(decimal.RequireFromString("1.55555")), + }}) + + wantedResult := tc.want(t, controller.app) + resp, cleanup := controller.client.Get( + fmt.Sprintf("/v2/chains/cosmos/%s", tc.inputId), + ) + t.Cleanup(cleanup) + require.Equal(t, tc.wantStatusCode, resp.StatusCode) + + if wantedResult != nil { + resource1 := presenters.ChainResource{} + err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, resp), &resource1) + require.NoError(t, err) + + assert.Equal(t, wantedResult.ID, resource1.ID) + assert.Equal(t, wantedResult.Config, resource1.Config) + } + }) + } +} + +func Test_CosmosChainsController_Index(t *testing.T) { + t.Parallel() + + chainA := &coscfg.TOMLConfig{ + ChainID: ptr("a" + cosmostest.RandomChainID()), + Enabled: ptr(true), + Chain: coscfg.Chain{ + FallbackGasPrice: ptr(decimal.RequireFromString("9.999")), + }, + } + + chainB := &coscfg.TOMLConfig{ + ChainID: ptr("b" + cosmostest.RandomChainID()), + Enabled: ptr(true), + Chain: coscfg.Chain{ + GasLimitMultiplier: ptr(decimal.RequireFromString("1.55555")), + }, + } + controller := setupCosmosChainsControllerTestV2(t, chainA, chainB) + + badResp, cleanup := controller.client.Get("/v2/chains/cosmos?size=asd") + t.Cleanup(cleanup) + require.Equal(t, http.StatusUnprocessableEntity, badResp.StatusCode) + + resp, cleanup := controller.client.Get("/v2/chains/cosmos?size=1") + t.Cleanup(cleanup) + require.Equal(t, http.StatusOK, resp.StatusCode) + + body := cltest.ParseResponseBody(t, resp) + + metaCount, err := cltest.ParseJSONAPIResponseMetaCount(body) + require.NoError(t, err) + require.Equal(t, 2, metaCount) + + var links jsonapi.Links + + var chains []presenters.ChainResource + err = web.ParsePaginatedResponse(body, &chains, &links) + assert.NoError(t, err) + assert.NotEmpty(t, links["next"].Href) + assert.Empty(t, links["prev"].Href) + + assert.Len(t, links, 1) + assert.Equal(t, *chainA.ChainID, chains[0].ID) + tomlA, err := chainA.TOMLString() + require.NoError(t, err) + assert.Equal(t, tomlA, chains[0].Config) + + resp, cleanup = controller.client.Get(links["next"].Href) + t.Cleanup(cleanup) + require.Equal(t, http.StatusOK, resp.StatusCode) + + chains = []presenters.ChainResource{} + err = web.ParsePaginatedResponse(cltest.ParseResponseBody(t, resp), &chains, &links) + assert.NoError(t, err) + assert.Empty(t, links["next"].Href) + assert.NotEmpty(t, links["prev"].Href) + + assert.Len(t, links, 1) + assert.Equal(t, *chainB.ChainID, chains[0].ID) + tomlB, err := chainB.TOMLString() + require.NoError(t, err) + assert.Equal(t, tomlB, chains[0].Config) +} + +type TestCosmosChainsController struct { + app *cltest.TestApplication + client cltest.HTTPClientCleaner +} + +func setupCosmosChainsControllerTestV2(t *testing.T, cfgs ...*coscfg.TOMLConfig) *TestCosmosChainsController { + for i := range cfgs { + cfgs[i].SetDefaults() + } + cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { + c.Cosmos = cfgs + c.EVM = nil + }) + app := cltest.NewApplicationWithConfig(t, cfg) + ctx := testutils.Context(t) + require.NoError(t, app.Start(ctx)) + + client := app.NewHTTPClient(nil) + + return &TestCosmosChainsController{ + app: app, + client: client, + } +} +func Test_SolanaChainsController_Show(t *testing.T) { + t.Parallel() + + const validId = "Chainlink-12" + + testCases := []struct { + name string + inputId string + wantStatusCode int + want func(t *testing.T, app *cltest.TestApplication) *commonTypes.ChainStatus + }{ + { + inputId: validId, + name: "success", + want: func(t *testing.T, app *cltest.TestApplication) *commonTypes.ChainStatus { + return &commonTypes.ChainStatus{ + ID: validId, + Enabled: true, + Config: `ChainID = 'Chainlink-12' +BalancePollPeriod = '5s' +ConfirmPollPeriod = '500ms' +OCR2CachePollPeriod = '1s' +OCR2CacheTTL = '1m0s' +TxTimeout = '1h0m0s' +TxRetryTimeout = '10s' +TxConfirmTimeout = '30s' +TxRetentionTimeout = '0s' +SkipPreflight = false +Commitment = 'confirmed' +MaxRetries = 0 +FeeEstimatorMode = 'fixed' +ComputeUnitPriceMax = 1000 +ComputeUnitPriceMin = 0 +ComputeUnitPriceDefault = 0 +FeeBumpPeriod = '3s' +BlockHistoryPollPeriod = '5s' +BlockHistorySize = 1 +ComputeUnitLimitDefault = 200000 +EstimateComputeUnitLimit = false +Nodes = [] + +[MultiNode] +Enabled = false +PollFailureThreshold = 5 +PollInterval = '15s' +SelectionMode = 'PriorityLevel' +SyncThreshold = 10 +NodeIsSyncingEnabled = false +LeaseDuration = '1m0s' +FinalizedBlockPollInterval = '5s' +EnforceRepeatableRead = true +DeathDeclarationDelay = '20s' +NodeNoNewHeadsThreshold = '20s' +NoNewFinalizedHeadsThreshold = '20s' +FinalityDepth = 0 +FinalityTagEnabled = true +FinalizedBlockOffset = 50 +`, + } + }, + wantStatusCode: http.StatusOK, + }, + { + inputId: "234", + name: "not found", + want: func(t *testing.T, app *cltest.TestApplication) *commonTypes.ChainStatus { + return nil + }, + wantStatusCode: http.StatusBadRequest, + }, + } + + for _, testCase := range testCases { + tc := testCase + + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + controller := setupSolanaChainsControllerTestV2(t, &config.TOMLConfig{ + ChainID: ptr(validId), + Chain: config.Chain{ + SkipPreflight: ptr(false), + TxTimeout: commoncfg.MustNewDuration(time.Hour), + }, + }) + + wantedResult := tc.want(t, controller.app) + resp, cleanup := controller.client.Get( + fmt.Sprintf("/v2/chains/solana/%s", tc.inputId), + ) + t.Cleanup(cleanup) + require.Equal(t, tc.wantStatusCode, resp.StatusCode) + + if wantedResult != nil { + resource1 := presenters.ChainResource{} + err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, resp), &resource1) + require.NoError(t, err) + + assert.Equal(t, wantedResult.ID, resource1.ID) + assert.Equal(t, wantedResult.Enabled, resource1.Enabled) + assert.Equal(t, wantedResult.Config, resource1.Config) + } + }) + } +} + +func Test_SolanaChainsController_Index(t *testing.T) { + t.Parallel() + + chainA := &config.TOMLConfig{ + ChainID: ptr(fmt.Sprintf("ChainlinktestA-%d", rand.Int31n(999999))), + Chain: config.Chain{ + TxTimeout: commoncfg.MustNewDuration(time.Hour), + }, + } + chainB := &config.TOMLConfig{ + ChainID: ptr(fmt.Sprintf("ChainlinktestB-%d", rand.Int31n(999999))), + Chain: config.Chain{ + SkipPreflight: ptr(false), + }, + } + controller := setupSolanaChainsControllerTestV2(t, chainA, chainB) + + badResp, cleanup := controller.client.Get("/v2/chains/solana?size=asd") + t.Cleanup(cleanup) + require.Equal(t, http.StatusUnprocessableEntity, badResp.StatusCode) + + resp, cleanup := controller.client.Get("/v2/chains/solana?size=1") + t.Cleanup(cleanup) + require.Equal(t, http.StatusOK, resp.StatusCode) + + body := cltest.ParseResponseBody(t, resp) + + metaCount, err := cltest.ParseJSONAPIResponseMetaCount(body) + require.NoError(t, err) + require.Equal(t, 2, metaCount) + + var links jsonapi.Links + + chains := []presenters.ChainResource{} + err = web.ParsePaginatedResponse(body, &chains, &links) + assert.NoError(t, err) + assert.NotEmpty(t, links["next"].Href) + assert.Empty(t, links["prev"].Href) + + assert.Len(t, links, 1) + assert.Equal(t, *chainA.ChainID, chains[0].ID) + tomlA, err := chainA.TOMLString() + require.NoError(t, err) + assert.Equal(t, tomlA, chains[0].Config) + + resp, cleanup = controller.client.Get(links["next"].Href) + t.Cleanup(cleanup) + require.Equal(t, http.StatusOK, resp.StatusCode) + + chains = []presenters.ChainResource{} + err = web.ParsePaginatedResponse(cltest.ParseResponseBody(t, resp), &chains, &links) + assert.NoError(t, err) + assert.Empty(t, links["next"].Href) + assert.NotEmpty(t, links["prev"].Href) + + assert.Len(t, links, 1) + assert.Equal(t, *chainB.ChainID, chains[0].ID) + tomlB, err := chainB.TOMLString() + require.NoError(t, err) + assert.Equal(t, tomlB, chains[0].Config) +} + +type TestSolanaChainsController struct { + app *cltest.TestApplication + client cltest.HTTPClientCleaner +} + +func setupSolanaChainsControllerTestV2(t *testing.T, cfgs ...*config.TOMLConfig) *TestSolanaChainsController { + for i := range cfgs { + cfgs[i].SetDefaults() + } + cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { + c.Solana = cfgs + c.EVM = nil + }) + app := cltest.NewApplicationWithConfig(t, cfg) + require.NoError(t, app.Start(testutils.Context(t))) + + client := app.NewHTTPClient(nil) + + return &TestSolanaChainsController{ + app: app, + client: client, + } +} diff --git a/core/web/cosmos_chains_controller.go b/core/web/cosmos_chains_controller.go deleted file mode 100644 index 27c3976ce39..00000000000 --- a/core/web/cosmos_chains_controller.go +++ /dev/null @@ -1,17 +0,0 @@ -package web - -import ( - "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" - "github.com/smartcontractkit/chainlink/v2/core/services/relay" - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -func NewCosmosChainsController(app chainlink.Application) ChainsController { - return newChainsController[presenters.CosmosChainResource]( - relay.NetworkCosmos, - app.GetRelayers().List(chainlink.FilterRelayersByType(relay.NetworkCosmos)), - ErrCosmosNotEnabled, - presenters.NewCosmosChainResource, - app.GetLogger(), - app.GetAuditLogger()) -} diff --git a/core/web/cosmos_chains_controller_test.go b/core/web/cosmos_chains_controller_test.go deleted file mode 100644 index 2d5eb42515a..00000000000 --- a/core/web/cosmos_chains_controller_test.go +++ /dev/null @@ -1,193 +0,0 @@ -package web_test - -import ( - "fmt" - "net/http" - "testing" - - "github.com/manyminds/api2go/jsonapi" - "github.com/shopspring/decimal" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/smartcontractkit/chainlink-common/pkg/types" - coscfg "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/config" - - "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/cosmostest" - "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" - "github.com/smartcontractkit/chainlink/v2/core/web" - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -func Test_CosmosChainsController_Show(t *testing.T) { - t.Parallel() - - const validId = "Chainlink-12" - - testCases := []struct { - name string - inputId string - wantStatusCode int - want func(t *testing.T, app *cltest.TestApplication) *types.ChainStatus - }{ - { - inputId: validId, - name: "success", - want: func(t *testing.T, app *cltest.TestApplication) *types.ChainStatus { - return &types.ChainStatus{ - ID: validId, - Enabled: true, - Config: `ChainID = 'Chainlink-12' -Enabled = true -Bech32Prefix = 'wasm' -BlockRate = '6s' -BlocksUntilTxTimeout = 30 -ConfirmPollPeriod = '1s' -FallbackGasPrice = '9.999' -GasToken = 'ucosm' -GasLimitMultiplier = '1.55555' -MaxMsgsPerBatch = 100 -OCR2CachePollPeriod = '4s' -OCR2CacheTTL = '1m0s' -TxMsgTimeout = '10m0s' -Nodes = [] -`, - } - }, - wantStatusCode: http.StatusOK, - }, - { - inputId: "234", - name: "not found", - want: func(t *testing.T, app *cltest.TestApplication) *types.ChainStatus { - return nil - }, - wantStatusCode: http.StatusBadRequest, - }, - } - - for _, testCase := range testCases { - tc := testCase - - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - - controller := setupCosmosChainsControllerTestV2(t, &coscfg.TOMLConfig{ - ChainID: ptr(validId), - Enabled: ptr(true), - Chain: coscfg.Chain{ - FallbackGasPrice: ptr(decimal.RequireFromString("9.999")), - GasLimitMultiplier: ptr(decimal.RequireFromString("1.55555")), - }}) - - wantedResult := tc.want(t, controller.app) - resp, cleanup := controller.client.Get( - fmt.Sprintf("/v2/chains/cosmos/%s", tc.inputId), - ) - t.Cleanup(cleanup) - require.Equal(t, tc.wantStatusCode, resp.StatusCode) - - if wantedResult != nil { - resource1 := presenters.CosmosChainResource{} - err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, resp), &resource1) - require.NoError(t, err) - - assert.Equal(t, wantedResult.ID, resource1.ID) - assert.Equal(t, wantedResult.Config, resource1.Config) - } - }) - } -} - -func Test_CosmosChainsController_Index(t *testing.T) { - t.Parallel() - - chainA := &coscfg.TOMLConfig{ - ChainID: ptr("a" + cosmostest.RandomChainID()), - Enabled: ptr(true), - Chain: coscfg.Chain{ - FallbackGasPrice: ptr(decimal.RequireFromString("9.999")), - }, - } - - chainB := &coscfg.TOMLConfig{ - ChainID: ptr("b" + cosmostest.RandomChainID()), - Enabled: ptr(true), - Chain: coscfg.Chain{ - GasLimitMultiplier: ptr(decimal.RequireFromString("1.55555")), - }, - } - controller := setupCosmosChainsControllerTestV2(t, chainA, chainB) - - badResp, cleanup := controller.client.Get("/v2/chains/cosmos?size=asd") - t.Cleanup(cleanup) - require.Equal(t, http.StatusUnprocessableEntity, badResp.StatusCode) - - resp, cleanup := controller.client.Get("/v2/chains/cosmos?size=1") - t.Cleanup(cleanup) - require.Equal(t, http.StatusOK, resp.StatusCode) - - body := cltest.ParseResponseBody(t, resp) - - metaCount, err := cltest.ParseJSONAPIResponseMetaCount(body) - require.NoError(t, err) - require.Equal(t, 2, metaCount) - - var links jsonapi.Links - - var chains []presenters.CosmosChainResource - err = web.ParsePaginatedResponse(body, &chains, &links) - assert.NoError(t, err) - assert.NotEmpty(t, links["next"].Href) - assert.Empty(t, links["prev"].Href) - - assert.Len(t, links, 1) - assert.Equal(t, *chainA.ChainID, chains[0].ID) - tomlA, err := chainA.TOMLString() - require.NoError(t, err) - assert.Equal(t, tomlA, chains[0].Config) - - resp, cleanup = controller.client.Get(links["next"].Href) - t.Cleanup(cleanup) - require.Equal(t, http.StatusOK, resp.StatusCode) - - chains = []presenters.CosmosChainResource{} - err = web.ParsePaginatedResponse(cltest.ParseResponseBody(t, resp), &chains, &links) - assert.NoError(t, err) - assert.Empty(t, links["next"].Href) - assert.NotEmpty(t, links["prev"].Href) - - assert.Len(t, links, 1) - assert.Equal(t, *chainB.ChainID, chains[0].ID) - tomlB, err := chainB.TOMLString() - require.NoError(t, err) - assert.Equal(t, tomlB, chains[0].Config) -} - -type TestCosmosChainsController struct { - app *cltest.TestApplication - client cltest.HTTPClientCleaner -} - -func setupCosmosChainsControllerTestV2(t *testing.T, cfgs ...*coscfg.TOMLConfig) *TestCosmosChainsController { - for i := range cfgs { - cfgs[i].SetDefaults() - } - cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { - c.Cosmos = cfgs - c.EVM = nil - }) - app := cltest.NewApplicationWithConfig(t, cfg) - ctx := testutils.Context(t) - require.NoError(t, app.Start(ctx)) - - client := app.NewHTTPClient(nil) - - return &TestCosmosChainsController{ - app: app, - client: client, - } -} diff --git a/core/web/evm_chains_controller.go b/core/web/evm_chains_controller.go deleted file mode 100644 index 9c887fa409c..00000000000 --- a/core/web/evm_chains_controller.go +++ /dev/null @@ -1,19 +0,0 @@ -package web - -import ( - "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" - "github.com/smartcontractkit/chainlink/v2/core/services/relay" - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -var ErrEVMNotEnabled = errChainDisabled{name: "EVM", tomlKey: "EVM.Enabled"} - -func NewEVMChainsController(app chainlink.Application) ChainsController { - return newChainsController[presenters.EVMChainResource]( - relay.NetworkEVM, - app.GetRelayers().List(chainlink.FilterRelayersByType(relay.NetworkEVM)), - ErrEVMNotEnabled, - presenters.NewEVMChainResource, - app.GetLogger(), - app.GetAuditLogger()) -} diff --git a/core/web/evm_chains_controller_test.go b/core/web/evm_chains_controller_test.go deleted file mode 100644 index ab8bf35e6cb..00000000000 --- a/core/web/evm_chains_controller_test.go +++ /dev/null @@ -1,215 +0,0 @@ -package web_test - -import ( - "fmt" - "math/big" - "net/http" - "sort" - "testing" - - "github.com/manyminds/api2go/jsonapi" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - evmcfg "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml" - "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" - ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" - "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest" - "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" - "github.com/smartcontractkit/chainlink/v2/core/web" - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -func Test_EVMChainsController_Show(t *testing.T) { - t.Parallel() - - validId := ubig.New(testutils.NewRandomEVMChainID()) - - testCases := []struct { - name string - inputId string - wantStatusCode int - want *evmcfg.EVMConfig - }{ - { - inputId: validId.String(), - name: "success", - want: &evmcfg.EVMConfig{ - ChainID: validId, - Enabled: ptr(true), - Chain: evmcfg.Defaults(nil, &evmcfg.Chain{ - GasEstimator: evmcfg.GasEstimator{ - EIP1559DynamicFees: ptr(true), - BlockHistory: evmcfg.BlockHistoryEstimator{ - BlockHistorySize: ptr[uint16](50), - }, - }, - RPCBlockQueryDelay: ptr[uint16](23), - MinIncomingConfirmations: ptr[uint32](12), - LinkContractAddress: ptr(types.EIP55AddressFromAddress(testutils.NewAddress())), - }), - }, - wantStatusCode: http.StatusOK, - }, - { - inputId: "invalidid", - name: "invalid id", - want: nil, - wantStatusCode: http.StatusBadRequest, - }, - { - inputId: "234", - name: "not found", - want: nil, - wantStatusCode: http.StatusBadRequest, - }, - } - - for _, testCase := range testCases { - tc := testCase - - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - - controller := setupEVMChainsControllerTest(t, configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { - if tc.want != nil { - c.EVM = evmcfg.EVMConfigs{tc.want} - } - })) - - wantedResult := tc.want - resp, cleanup := controller.client.Get( - fmt.Sprintf("/v2/chains/evm/%s", tc.inputId), - ) - t.Cleanup(cleanup) - require.Equal(t, tc.wantStatusCode, resp.StatusCode) - - if wantedResult != nil { - resource1 := presenters.EVMChainResource{} - err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, resp), &resource1) - require.NoError(t, err) - - assert.Equal(t, resource1.ID, wantedResult.ChainID.String()) - toml, err := wantedResult.TOMLString() - require.NoError(t, err) - assert.Equal(t, toml, resource1.Config) - } - }) - } -} - -func Test_EVMChainsController_Index(t *testing.T) { - t.Parallel() - - // sort test chain ids to make expected comparison easy - chainIDs := []*big.Int{testutils.NewRandomEVMChainID(), testutils.NewRandomEVMChainID(), testutils.NewRandomEVMChainID()} - sort.Slice(chainIDs, func(i, j int) bool { - return chainIDs[i].String() < chainIDs[j].String() - }) - - configuredChains := evmcfg.EVMConfigs{ - {ChainID: ubig.New(chainIDs[0]), Chain: evmcfg.Defaults(nil)}, - { - ChainID: ubig.New(chainIDs[1]), - Chain: evmcfg.Defaults(nil, &evmcfg.Chain{ - RPCBlockQueryDelay: ptr[uint16](13), - GasEstimator: evmcfg.GasEstimator{ - EIP1559DynamicFees: ptr(true), - BlockHistory: evmcfg.BlockHistoryEstimator{ - BlockHistorySize: ptr[uint16](1), - }, - }, - MinIncomingConfirmations: ptr[uint32](120), - }), - }, - { - ChainID: ubig.New(chainIDs[2]), - Chain: evmcfg.Defaults(nil, &evmcfg.Chain{ - RPCBlockQueryDelay: ptr[uint16](5), - GasEstimator: evmcfg.GasEstimator{ - EIP1559DynamicFees: ptr(false), - BlockHistory: evmcfg.BlockHistoryEstimator{ - BlockHistorySize: ptr[uint16](2), - }, - }, - MinIncomingConfirmations: ptr[uint32](30), - }), - }, - } - - assert.Len(t, configuredChains, 3) - controller := setupEVMChainsControllerTest(t, configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { - c.EVM = append(c.EVM, configuredChains...) - })) - - badResp, cleanup := controller.client.Get("/v2/chains/evm?size=asd") - t.Cleanup(cleanup) - require.Equal(t, http.StatusUnprocessableEntity, badResp.StatusCode) - - resp, cleanup := controller.client.Get("/v2/chains/evm?size=3") - t.Cleanup(cleanup) - require.Equal(t, http.StatusOK, resp.StatusCode) - - body := cltest.ParseResponseBody(t, resp) - - metaCount, err := cltest.ParseJSONAPIResponseMetaCount(body) - require.NoError(t, err) - require.Equal(t, 1+len(configuredChains), metaCount) - - var links jsonapi.Links - - var gotChains []presenters.EVMChainResource - err = web.ParsePaginatedResponse(body, &gotChains, &links) - assert.NoError(t, err) - assert.NotEmpty(t, links["next"].Href) - assert.Empty(t, links["prev"].Href) - - assert.Len(t, links, 1) - // the difference in index value here seems to be due to the fact - // that cltest always has a default EVM chain, which is the off-by-one - // in the indices - assert.Equal(t, gotChains[2].ID, configuredChains[1].ChainID.String()) - toml, err := configuredChains[1].TOMLString() - require.NoError(t, err) - assert.Equal(t, toml, gotChains[2].Config) - - resp, cleanup = controller.client.Get(links["next"].Href) - t.Cleanup(cleanup) - require.Equal(t, http.StatusOK, resp.StatusCode) - - gotChains = []presenters.EVMChainResource{} - err = web.ParsePaginatedResponse(cltest.ParseResponseBody(t, resp), &gotChains, &links) - assert.NoError(t, err) - assert.Empty(t, links["next"].Href) - assert.NotEmpty(t, links["prev"].Href) - - assert.Len(t, links, 1) - assert.Equal(t, gotChains[0].ID, configuredChains[2].ChainID.String()) - toml, err = configuredChains[2].TOMLString() - require.NoError(t, err) - assert.Equal(t, toml, gotChains[0].Config) -} - -type TestEVMChainsController struct { - app *cltest.TestApplication - client cltest.HTTPClientCleaner -} - -func setupEVMChainsControllerTest(t *testing.T, cfg chainlink.GeneralConfig) *TestEVMChainsController { - // Using this instead of `NewApplicationEVMDisabled` since we need the chain set to be loaded in the app - // for the sake of the API endpoints to work properly - app := cltest.NewApplicationWithConfig(t, cfg) - ctx := testutils.Context(t) - require.NoError(t, app.Start(ctx)) - - client := app.NewHTTPClient(nil) - - return &TestEVMChainsController{ - app: app, - client: client, - } -} - -func ptr[T any](t T) *T { return &t } diff --git a/core/web/evm_nodes_controller.go b/core/web/evm_nodes_controller.go index 8872f51d7e3..1f68acee21d 100644 --- a/core/web/evm_nodes_controller.go +++ b/core/web/evm_nodes_controller.go @@ -6,6 +6,9 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/web/presenters" ) +// ErrEVMNotEnabled is returned when COSMOS_ENABLED is not true. +var ErrEVMNotEnabled = errChainDisabled{name: "EVM", tomlKey: "EVM.Enabled"} + func NewEVMNodesController(app chainlink.Application) NodesController { scopedNodeStatuser := NewNetworkScopedNodeStatuser(app.GetRelayers(), relay.NetworkEVM) diff --git a/core/web/presenters/chain.go b/core/web/presenters/chain.go index 99cf9a1d252..703dd6dab2a 100644 --- a/core/web/presenters/chain.go +++ b/core/web/presenters/chain.go @@ -1,11 +1,30 @@ package presenters +import ( + "github.com/smartcontractkit/chainlink-common/pkg/types" + commonTypes "github.com/smartcontractkit/chainlink/v2/common/types" +) + type ChainResource struct { JAID Enabled bool `json:"enabled"` Config string `json:"config"` // TOML } +// GetName implements the api2go EntityNamer interface +func (r ChainResource) GetName() string { + return "chain" +} + +// NewChainResource returns a new ChainResource for chain. +func NewChainResource(chain commonTypes.ChainStatusWithID) ChainResource { + return ChainResource{ + JAID: NewPrefixedJAID(chain.RelayID.Network, chain.RelayID.ChainID), + Config: chain.Config, + Enabled: chain.Enabled, + } +} + type NodeResource struct { JAID ChainID string `json:"chainID"` @@ -13,3 +32,14 @@ type NodeResource struct { Config string `json:"config"` // TOML State string `json:"state"` } + +// NewNodeResource returns a new NodeResource for node. +func NewNodeResource(node types.NodeStatus) NodeResource { + return NodeResource{ + JAID: NewPrefixedJAID(node.Name, node.ChainID), + ChainID: node.ChainID, + Name: node.Name, + State: node.State, + Config: node.Config, + } +} diff --git a/core/web/presenters/cosmos_chain.go b/core/web/presenters/cosmos_chain.go index c2bc4b52b61..1f276a56e54 100644 --- a/core/web/presenters/cosmos_chain.go +++ b/core/web/presenters/cosmos_chain.go @@ -4,25 +4,6 @@ import ( "github.com/smartcontractkit/chainlink-common/pkg/types" ) -// CosmosChainResource is an Cosmos chain JSONAPI resource. -type CosmosChainResource struct { - ChainResource -} - -// GetName implements the api2go EntityNamer interface -func (r CosmosChainResource) GetName() string { - return "cosmos_chain" -} - -// NewCosmosChainResource returns a new CosmosChainResource for chain. -func NewCosmosChainResource(chain types.ChainStatus) CosmosChainResource { - return CosmosChainResource{ChainResource{ - JAID: NewJAID(chain.ID), - Config: chain.Config, - Enabled: chain.Enabled, - }} -} - // CosmosNodeResource is a Cosmos node JSONAPI resource. type CosmosNodeResource struct { NodeResource diff --git a/core/web/presenters/evm_chain.go b/core/web/presenters/evm_chain.go index adf399d4b01..cb8f597ee7e 100644 --- a/core/web/presenters/evm_chain.go +++ b/core/web/presenters/evm_chain.go @@ -2,25 +2,6 @@ package presenters import "github.com/smartcontractkit/chainlink-common/pkg/types" -// EVMChainResource is an EVM chain JSONAPI resource. -type EVMChainResource struct { - ChainResource -} - -// GetName implements the api2go EntityNamer interface -func (r EVMChainResource) GetName() string { - return "evm_chain" -} - -// NewEVMChainResource returns a new EVMChainResource for chain. -func NewEVMChainResource(chain types.ChainStatus) EVMChainResource { - return EVMChainResource{ChainResource{ - JAID: NewJAID(chain.ID), - Config: chain.Config, - Enabled: chain.Enabled, - }} -} - // EVMNodeResource is an EVM node JSONAPI resource. type EVMNodeResource struct { NodeResource diff --git a/core/web/presenters/solana_chain.go b/core/web/presenters/solana_chain.go index 798d98124a5..feefc5e8c7b 100644 --- a/core/web/presenters/solana_chain.go +++ b/core/web/presenters/solana_chain.go @@ -4,25 +4,6 @@ import ( "github.com/smartcontractkit/chainlink-common/pkg/types" ) -// SolanaChainResource is an Solana chain JSONAPI resource. -type SolanaChainResource struct { - ChainResource -} - -// GetName implements the api2go EntityNamer interface -func (r SolanaChainResource) GetName() string { - return "solana_chain" -} - -// NewSolanaChainResource returns a new SolanaChainResource for chain. -func NewSolanaChainResource(chain types.ChainStatus) SolanaChainResource { - return SolanaChainResource{ChainResource{ - JAID: NewJAID(chain.ID), - Config: chain.Config, - Enabled: chain.Enabled, - }} -} - // SolanaNodeResource is a Solana node JSONAPI resource. type SolanaNodeResource struct { NodeResource diff --git a/core/web/presenters/starknet_chain.go b/core/web/presenters/starknet_chain.go index addf798fe9f..6135356d767 100644 --- a/core/web/presenters/starknet_chain.go +++ b/core/web/presenters/starknet_chain.go @@ -4,25 +4,6 @@ import ( "github.com/smartcontractkit/chainlink-common/pkg/types" ) -// StarkNetChainResource is an StarkNet chain JSONAPI resource. -type StarkNetChainResource struct { - ChainResource -} - -// GetName implements the api2go EntityNamer interface -func (r StarkNetChainResource) GetName() string { - return "starknet_chain" -} - -// NewStarkNetChainResource returns a new StarkNetChainResource for chain. -func NewStarkNetChainResource(chain types.ChainStatus) StarkNetChainResource { - return StarkNetChainResource{ChainResource{ - JAID: NewJAID(chain.ID), - Config: chain.Config, - Enabled: chain.Enabled, - }} -} - // StarkNetNodeResource is a StarkNet node JSONAPI resource. type StarkNetNodeResource struct { NodeResource diff --git a/core/web/router.go b/core/web/router.go index 6e96b47981b..9f3e923be4d 100644 --- a/core/web/router.go +++ b/core/web/router.go @@ -40,6 +40,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" "github.com/smartcontractkit/chainlink/v2/core/web/auth" "github.com/smartcontractkit/chainlink/v2/core/web/loader" + "github.com/smartcontractkit/chainlink/v2/core/web/presenters" "github.com/smartcontractkit/chainlink/v2/core/web/resolver" "github.com/smartcontractkit/chainlink/v2/core/web/schema" ) @@ -390,18 +391,16 @@ func v2Routes(app chainlink.Application, r *gin.RouterGroup) { authv2.PATCH("/log", auth.RequiresAdminRole(lgc.Patch)) chains := authv2.Group("chains") - for _, chain := range []struct { - path string - cc ChainsController - }{ - {"evm", NewEVMChainsController(app)}, - {"solana", NewSolanaChainsController(app)}, - {"starknet", NewStarkNetChainsController(app)}, - {"cosmos", NewCosmosChainsController(app)}, - } { - chains.GET(chain.path, paginatedRequest(chain.cc.Index)) - chains.GET(chain.path+"/:ID", chain.cc.Show) - } + chainController := NewChainsController( + app.GetRelayers(), + errors.New("EVM"), // TODO: + presenters.NewChainResource, + app.GetLogger(), + app.GetAuditLogger(), + ) + chains.GET("", paginatedRequest(chainController.Index)) + chains.GET("/:network", paginatedRequest(chainController.Index)) + chains.GET("/:network/:ID", chainController.Show) nodes := authv2.Group("nodes") for _, chain := range []struct { diff --git a/core/web/solana_chains_controller.go b/core/web/solana_chains_controller.go deleted file mode 100644 index 56d44d600ca..00000000000 --- a/core/web/solana_chains_controller.go +++ /dev/null @@ -1,17 +0,0 @@ -package web - -import ( - "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" - "github.com/smartcontractkit/chainlink/v2/core/services/relay" - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -func NewSolanaChainsController(app chainlink.Application) ChainsController { - return newChainsController( - relay.NetworkSolana, - app.GetRelayers().List(chainlink.FilterRelayersByType(relay.NetworkSolana)), - ErrSolanaNotEnabled, - presenters.NewSolanaChainResource, - app.GetLogger(), - app.GetAuditLogger()) -} diff --git a/core/web/solana_chains_controller_test.go b/core/web/solana_chains_controller_test.go deleted file mode 100644 index fdc9bd16b9b..00000000000 --- a/core/web/solana_chains_controller_test.go +++ /dev/null @@ -1,216 +0,0 @@ -package web_test - -import ( - "fmt" - "math/rand" - "net/http" - "testing" - "time" - - "github.com/manyminds/api2go/jsonapi" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - commoncfg "github.com/smartcontractkit/chainlink-common/pkg/config" - "github.com/smartcontractkit/chainlink-common/pkg/types" - "github.com/smartcontractkit/chainlink-solana/pkg/solana/config" - - "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest" - "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" - "github.com/smartcontractkit/chainlink/v2/core/web" - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -func Test_SolanaChainsController_Show(t *testing.T) { - t.Parallel() - - const validId = "Chainlink-12" - - testCases := []struct { - name string - inputId string - wantStatusCode int - want func(t *testing.T, app *cltest.TestApplication) *types.ChainStatus - }{ - { - inputId: validId, - name: "success", - want: func(t *testing.T, app *cltest.TestApplication) *types.ChainStatus { - return &types.ChainStatus{ - ID: validId, - Enabled: true, - Config: `ChainID = 'Chainlink-12' -BalancePollPeriod = '5s' -ConfirmPollPeriod = '500ms' -OCR2CachePollPeriod = '1s' -OCR2CacheTTL = '1m0s' -TxTimeout = '1h0m0s' -TxRetryTimeout = '10s' -TxConfirmTimeout = '30s' -TxRetentionTimeout = '0s' -SkipPreflight = false -Commitment = 'confirmed' -MaxRetries = 0 -FeeEstimatorMode = 'fixed' -ComputeUnitPriceMax = 1000 -ComputeUnitPriceMin = 0 -ComputeUnitPriceDefault = 0 -FeeBumpPeriod = '3s' -BlockHistoryPollPeriod = '5s' -BlockHistorySize = 1 -ComputeUnitLimitDefault = 200000 -EstimateComputeUnitLimit = false -Nodes = [] - -[MultiNode] -Enabled = false -PollFailureThreshold = 5 -PollInterval = '15s' -SelectionMode = 'PriorityLevel' -SyncThreshold = 10 -NodeIsSyncingEnabled = false -LeaseDuration = '1m0s' -FinalizedBlockPollInterval = '5s' -EnforceRepeatableRead = true -DeathDeclarationDelay = '20s' -NodeNoNewHeadsThreshold = '20s' -NoNewFinalizedHeadsThreshold = '20s' -FinalityDepth = 0 -FinalityTagEnabled = true -FinalizedBlockOffset = 50 -`, - } - }, - wantStatusCode: http.StatusOK, - }, - { - inputId: "234", - name: "not found", - want: func(t *testing.T, app *cltest.TestApplication) *types.ChainStatus { - return nil - }, - wantStatusCode: http.StatusBadRequest, - }, - } - - for _, testCase := range testCases { - tc := testCase - - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - - controller := setupSolanaChainsControllerTestV2(t, &config.TOMLConfig{ - ChainID: ptr(validId), - Chain: config.Chain{ - SkipPreflight: ptr(false), - TxTimeout: commoncfg.MustNewDuration(time.Hour), - }, - }) - - wantedResult := tc.want(t, controller.app) - resp, cleanup := controller.client.Get( - fmt.Sprintf("/v2/chains/solana/%s", tc.inputId), - ) - t.Cleanup(cleanup) - require.Equal(t, tc.wantStatusCode, resp.StatusCode) - - if wantedResult != nil { - resource1 := presenters.SolanaChainResource{} - err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, resp), &resource1) - require.NoError(t, err) - - assert.Equal(t, wantedResult.ID, resource1.ID) - assert.Equal(t, wantedResult.Enabled, resource1.Enabled) - assert.Equal(t, wantedResult.Config, resource1.Config) - } - }) - } -} - -func Test_SolanaChainsController_Index(t *testing.T) { - t.Parallel() - - chainA := &config.TOMLConfig{ - ChainID: ptr(fmt.Sprintf("ChainlinktestA-%d", rand.Int31n(999999))), - Chain: config.Chain{ - TxTimeout: commoncfg.MustNewDuration(time.Hour), - }, - } - chainB := &config.TOMLConfig{ - ChainID: ptr(fmt.Sprintf("ChainlinktestB-%d", rand.Int31n(999999))), - Chain: config.Chain{ - SkipPreflight: ptr(false), - }, - } - controller := setupSolanaChainsControllerTestV2(t, chainA, chainB) - - badResp, cleanup := controller.client.Get("/v2/chains/solana?size=asd") - t.Cleanup(cleanup) - require.Equal(t, http.StatusUnprocessableEntity, badResp.StatusCode) - - resp, cleanup := controller.client.Get("/v2/chains/solana?size=1") - t.Cleanup(cleanup) - require.Equal(t, http.StatusOK, resp.StatusCode) - - body := cltest.ParseResponseBody(t, resp) - - metaCount, err := cltest.ParseJSONAPIResponseMetaCount(body) - require.NoError(t, err) - require.Equal(t, 2, metaCount) - - var links jsonapi.Links - - chains := []presenters.SolanaChainResource{} - err = web.ParsePaginatedResponse(body, &chains, &links) - assert.NoError(t, err) - assert.NotEmpty(t, links["next"].Href) - assert.Empty(t, links["prev"].Href) - - assert.Len(t, links, 1) - assert.Equal(t, *chainA.ChainID, chains[0].ID) - tomlA, err := chainA.TOMLString() - require.NoError(t, err) - assert.Equal(t, tomlA, chains[0].Config) - - resp, cleanup = controller.client.Get(links["next"].Href) - t.Cleanup(cleanup) - require.Equal(t, http.StatusOK, resp.StatusCode) - - chains = []presenters.SolanaChainResource{} - err = web.ParsePaginatedResponse(cltest.ParseResponseBody(t, resp), &chains, &links) - assert.NoError(t, err) - assert.Empty(t, links["next"].Href) - assert.NotEmpty(t, links["prev"].Href) - - assert.Len(t, links, 1) - assert.Equal(t, *chainB.ChainID, chains[0].ID) - tomlB, err := chainB.TOMLString() - require.NoError(t, err) - assert.Equal(t, tomlB, chains[0].Config) -} - -type TestSolanaChainsController struct { - app *cltest.TestApplication - client cltest.HTTPClientCleaner -} - -func setupSolanaChainsControllerTestV2(t *testing.T, cfgs ...*config.TOMLConfig) *TestSolanaChainsController { - for i := range cfgs { - cfgs[i].SetDefaults() - } - cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { - c.Solana = cfgs - c.EVM = nil - }) - app := cltest.NewApplicationWithConfig(t, cfg) - require.NoError(t, app.Start(testutils.Context(t))) - - client := app.NewHTTPClient(nil) - - return &TestSolanaChainsController{ - app: app, - client: client, - } -} diff --git a/core/web/starknet_chains_controller.go b/core/web/starknet_chains_controller.go deleted file mode 100644 index eb79ba3f962..00000000000 --- a/core/web/starknet_chains_controller.go +++ /dev/null @@ -1,17 +0,0 @@ -package web - -import ( - "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" - "github.com/smartcontractkit/chainlink/v2/core/services/relay" - "github.com/smartcontractkit/chainlink/v2/core/web/presenters" -) - -func NewStarkNetChainsController(app chainlink.Application) ChainsController { - return newChainsController( - relay.NetworkStarkNet, - app.GetRelayers().List(chainlink.FilterRelayersByType(relay.NetworkStarkNet)), - ErrStarkNetNotEnabled, - presenters.NewStarkNetChainResource, - app.GetLogger(), - app.GetAuditLogger()) -}