Skip to content

Commit

Permalink
Separate JD database (#1472)
Browse files Browse the repository at this point in the history
* wait for funding on testnet

* wait for funding on testnet

* separate JD database

* name JD database differently

* changeset
  • Loading branch information
skudasov authored Dec 11, 2024
1 parent d3bffaf commit 11410d9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
2 changes: 2 additions & 0 deletions framework/.changeset/v0.3.7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Separate JobDistributor database
- Wait when fund nodes on testnets
36 changes: 27 additions & 9 deletions framework/components/jd/jd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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{
Expand All @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions framework/components/jd/jd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
28 changes: 20 additions & 8 deletions framework/components/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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")
Expand Down Expand Up @@ -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",
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion framework/components/simple_node_set/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion framework/examples/myproject_cll/jd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 11410d9

Please sign in to comment.