From 11410d9e0d3cb06115b2c257cf2247fbd1966ee6 Mon Sep 17 00:00:00 2001 From: Sergey Kudasov Date: Wed, 11 Dec 2024 23:50:39 +0100 Subject: [PATCH] Separate JD database (#1472) * wait for funding on testnet * wait for funding on testnet * separate JD database * name JD database differently * changeset --- framework/.changeset/v0.3.7.md | 2 ++ framework/components/jd/jd.go | 36 +++++++++++++++----- framework/components/jd/jd_test.go | 8 ----- framework/components/postgres/postgres.go | 28 ++++++++++----- framework/components/simple_node_set/fund.go | 4 ++- framework/examples/myproject_cll/jd_test.go | 1 - 6 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 framework/.changeset/v0.3.7.md diff --git a/framework/.changeset/v0.3.7.md b/framework/.changeset/v0.3.7.md new file mode 100644 index 000000000..e8c781236 --- /dev/null +++ b/framework/.changeset/v0.3.7.md @@ -0,0 +1,2 @@ +- Separate JobDistributor database +- Wait when fund nodes on testnets \ No newline at end of file diff --git a/framework/components/jd/jd.go b/framework/components/jd/jd.go index a84d1a14f..3dc37e156 100644 --- a/framework/components/jd/jd.go +++ b/framework/components/jd/jd.go @@ -6,6 +6,7 @@ import ( "github.com/docker/docker/api/types/container" "github.com/docker/go-connections/nat" "github.com/smartcontractkit/chainlink-testing-framework/framework" + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres" tc "github.com/testcontainers/testcontainers-go" tcwait "github.com/testcontainers/testcontainers-go/wait" "os" @@ -19,14 +20,14 @@ const ( ) type Input struct { - Image string `toml:"image"` - GRPCPort string `toml:"grpc_port"` - WSRPCPort string `toml:"wsrpc_port"` - DBURL string `toml:"db_url"` - CSAEncryptionKey string `toml:"csa_encryption_key"` - DockerFilePath string `toml:"docker_file"` - DockerContext string `toml:"docker_ctx"` - Out *Output `toml:"out"` + Image string `toml:"image"` + GRPCPort string `toml:"grpc_port"` + WSRPCPort string `toml:"wsrpc_port"` + CSAEncryptionKey string `toml:"csa_encryption_key"` + DockerFilePath string `toml:"docker_file"` + DockerContext string `toml:"docker_ctx"` + DBInput *postgres.Input `toml:"db"` + Out *Output `toml:"out"` } type Output struct { @@ -49,6 +50,16 @@ func defaults(in *Input) { } } +func defaultJDDB() *postgres.Input { + return &postgres.Input{ + Image: "postgres:12", + Port: 14000, + Name: "jd-db", + VolumeName: "jd", + JDDatabase: true, + } +} + func NewJD(in *Input) (*Output, error) { if in.Out != nil && in.Out.UseCache { return in.Out, nil @@ -59,6 +70,13 @@ func NewJD(in *Input) (*Output, error) { if jdImg != "" { in.Image = jdImg } + if in.DBInput == nil { + in.DBInput = defaultJDDB() + } + pgOut, err := postgres.NewPostgreSQL(in.DBInput) + if err != nil { + return nil, err + } containerName := framework.DefaultTCName("jd") bindPort := fmt.Sprintf("%s/tcp", in.GRPCPort) req := tc.ContainerRequest{ @@ -74,7 +92,7 @@ func NewJD(in *Input) (*Output, error) { h.PortBindings = framework.MapTheSamePort(bindPort) }, Env: map[string]string{ - "DATABASE_URL": in.DBURL, + "DATABASE_URL": pgOut.JDDockerInternalURL, "PORT": in.GRPCPort, "NODE_RPC_PORT": in.WSRPCPort, "CSA_KEY_ENCRYPTION_SECRET": in.CSAEncryptionKey, diff --git a/framework/components/jd/jd_test.go b/framework/components/jd/jd_test.go index 5b2d31570..2f3848345 100644 --- a/framework/components/jd/jd_test.go +++ b/framework/components/jd/jd_test.go @@ -3,7 +3,6 @@ package jd_test import ( "github.com/smartcontractkit/chainlink-testing-framework/framework" "github.com/smartcontractkit/chainlink-testing-framework/framework/components/jd" - "github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres" "github.com/stretchr/testify/require" "os" "sync" @@ -17,14 +16,7 @@ import ( func TestJD(t *testing.T) { err := framework.DefaultNetwork(&sync.Once{}) require.NoError(t, err) - pgOut, err := postgres.NewPostgreSQL(&postgres.Input{ - Image: "postgres:12.0", - Port: 14402, - VolumeName: "c", - }) - require.NoError(t, err) _, err = jd.NewJD(&jd.Input{ - DBURL: pgOut.JDDockerInternalURL, Image: os.Getenv("CTF_JD_IMAGE"), }) require.NoError(t, err) diff --git a/framework/components/postgres/postgres.go b/framework/components/postgres/postgres.go index 80cc3f239..46ac97557 100644 --- a/framework/components/postgres/postgres.go +++ b/framework/components/postgres/postgres.go @@ -25,8 +25,10 @@ const ( type Input struct { Image string `toml:"image" validate:"required"` Port int `toml:"port"` + Name string `toml:"name"` VolumeName string `toml:"volume_name"` Databases int `toml:"databases"` + JDDatabase bool `toml:"jd_database"` PullImage bool `toml:"pull_image"` Out *Output `toml:"out"` } @@ -43,13 +45,20 @@ func NewPostgreSQL(in *Input) (*Output, error) { ctx := context.Background() bindPort := fmt.Sprintf("%s/tcp", Port) - containerName := framework.DefaultTCName("ns-postgresql") + var containerName string + if in.Name != "" { + containerName = framework.DefaultTCName(in.Name) + } else { + containerName = framework.DefaultTCName("ns-postgresql") + } var sqlCommands []string for i := 0; i <= in.Databases; i++ { sqlCommands = append(sqlCommands, fmt.Sprintf("CREATE DATABASE db_%d;", i)) } - sqlCommands = append(sqlCommands, "CREATE DATABASE jd;") + if in.JDDatabase { + sqlCommands = append(sqlCommands, "CREATE DATABASE jd;") + } sqlCommands = append(sqlCommands, "ALTER USER chainlink WITH SUPERUSER;") initSQL := strings.Join(sqlCommands, "\n") initFile, err := os.CreateTemp("", "init-*.sql") @@ -129,7 +138,7 @@ func NewPostgreSQL(in *Input) (*Output, error) { if err != nil { return nil, err } - return &Output{ + o := &Output{ ContainerName: containerName, DockerInternalURL: fmt.Sprintf( "postgresql://%s:%s@%s:%s/%s?sslmode=disable", @@ -147,21 +156,24 @@ func NewPostgreSQL(in *Input) (*Output, error) { portToExpose, Database, ), - JDDockerInternalURL: fmt.Sprintf( + } + if in.JDDatabase { + o.JDDockerInternalURL = fmt.Sprintf( "postgresql://%s:%s@%s:%s/%s?sslmode=disable", User, Password, containerName, Port, "jd", - ), - JDUrl: fmt.Sprintf( + ) + o.JDUrl = fmt.Sprintf( "postgresql://%s:%s@%s:%d/%s?sslmode=disable", User, Password, host, portToExpose, "jd", - ), - }, nil + ) + } + return o, nil } diff --git a/framework/components/simple_node_set/fund.go b/framework/components/simple_node_set/fund.go index 09773009a..b1f4e47e8 100644 --- a/framework/components/simple_node_set/fund.go +++ b/framework/components/simple_node_set/fund.go @@ -5,6 +5,7 @@ import ( "crypto/ecdsa" "errors" "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -57,7 +58,8 @@ func SendETH(client *ethclient.Client, privateKeyHex string, toAddress string, a return fmt.Errorf("failed to send transaction: %v", err) } framework.L.Info().Msgf("Transaction sent: %s", signedTx.Hash().Hex()) - return nil + _, err = bind.WaitMined(context.Background(), client, signedTx) + return err } // FundNodes funds Chainlink nodes with N ETH each diff --git a/framework/examples/myproject_cll/jd_test.go b/framework/examples/myproject_cll/jd_test.go index d661fe2eb..ce3cfa860 100644 --- a/framework/examples/myproject_cll/jd_test.go +++ b/framework/examples/myproject_cll/jd_test.go @@ -23,7 +23,6 @@ func TestJDAndNodeSet(t *testing.T) { require.NoError(t, err) out, err := ns.NewSharedDBNodeSet(in.NodeSet, bc) require.NoError(t, err) - in.JD.DBURL = out.DBOut.JDDockerInternalURL _, err = jd.NewJD(in.JD) require.NoError(t, err)