Skip to content

Commit

Permalink
hack db into NewRelayer for PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann committed Jun 18, 2024
1 parent 051dba7 commit dbd5cf7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 100 deletions.
1 change: 1 addition & 0 deletions core/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (n ChainlinkAppFactory) NewApplication(ctx context.Context, cfg chainlink.G
CSAETHKeystore: keyStore,
ChainOpts: legacyevm.ChainOpts{AppConfig: cfg, MailMon: mailMon, DS: ds},
MercuryTransmitter: cfg.Mercury().Transmitter(),
DB: db.DB, // hack
}
// evm always enabled for backward compatibility
// TODO BCF-2510 this needs to change in order to clear the path for EVM extraction
Expand Down
4 changes: 4 additions & 0 deletions core/services/chainlink/relayer_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chainlink

import (
"context"
"database/sql"
"errors"
"fmt"

Expand Down Expand Up @@ -42,6 +43,8 @@ type EVMFactoryConfig struct {
legacyevm.ChainOpts
evmrelay.CSAETHKeystore
coreconfig.MercuryTransmitter
// hack to allow for the factory to be used in the context of the relayer
DB *sql.DB
}

func (r *RelayerFactory) NewEVM(ctx context.Context, config EVMFactoryConfig) (map[types.RelayID]evmrelay.LoopRelayAdapter, error) {
Expand Down Expand Up @@ -76,6 +79,7 @@ func (r *RelayerFactory) NewEVM(ctx context.Context, config EVMFactoryConfig) (m
MercuryPool: r.MercuryPool,
TransmitterConfig: config.MercuryTransmitter,
CapabilitiesRegistry: r.CapabilitiesRegistry,
DB: config.DB, //hack
}
relayer, err2 := evmrelay.NewRelayer(lggr.Named(relayID.ChainID), chain, relayerOpts)
if err2 != nil {
Expand Down
13 changes: 13 additions & 0 deletions core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evm

import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -30,6 +31,7 @@ import (
txmgrcommon "github.com/smartcontractkit/chainlink/v2/common/txmgr"
txm "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
evmtypes "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/chains/legacyevm"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
Expand All @@ -46,6 +48,7 @@ import (
reportcodecv3 "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/v3/reportcodec"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/wsrpc"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
evmdb "github.com/smartcontractkit/chainlink/v2/core/store/migrate/plugins/relayer/evm"
)

var (
Expand Down Expand Up @@ -99,6 +102,7 @@ type CSAETHKeystore interface {

type RelayerOpts struct {
DS sqlutil.DataSource
DB *sql.DB // hack for migrations. need a way to get this from the DS, or other move the migration logic out of here
CSAETHKeystore
MercuryPool wsrpc.Pool
TransmitterConfig mercury.TransmitterConfig
Expand Down Expand Up @@ -128,6 +132,15 @@ func NewRelayer(lggr logger.Logger, chain legacyevm.Chain, opts RelayerOpts) (*R
return nil, fmt.Errorf("cannot create evm relayer: %w", err)
}
lggr = lggr.Named("Relayer")
// run the migrations for the relayer
// TODO: this is a hack. migration require a sql.DB because that's what goose uses
err = evmdb.Migrate(context.Background(), opts.DB, evmdb.Cfg{
Schema: "evm_" + chain.ID().String(),
ChainID: ubig.New(chain.ID()),
})
if err != nil {
return nil, fmt.Errorf("failed to migrate evm relayer for chain %s: %w", chain.ID().String(), err)
}

mercuryORM := mercury.NewORM(opts.DS)
lloORM := llo.NewORM(opts.DS, chain.ID())
Expand Down
100 changes: 0 additions & 100 deletions core/store/migrate/plugins/relayer/evm/0002_initial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package evm
import (
"bytes"
_ "embed"
"fmt"
"os"
"testing"

_ "github.com/mattn/go-sqlite3"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest/heavyweight"

"github.com/pressly/goose/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -45,99 +41,3 @@ func Test_resolveup(t *testing.T) {
})
}
}

func Test_init_functional(t *testing.T) {
_, db := heavyweight.FullTestDBEmptyV2(t, nil)
defer db.Close()

// must load the initial state, derivied from the core migrations at v244
// because the evm migrations try to move try from the core schema to the evm schema
b, err := os.ReadFile("./testutils/evm_initial_state.sql")
require.NoError(t, err)

_, err = db.DB.Exec(string(b))
require.NoError(t, err, "failed to load initial state")

type args struct {
cfg Cfg
}
tests := []struct {
name string
args args
wantErr bool
wantTables []string
}{
{
name: "evm template",
args: args{
cfg: Cfg{
Schema: "evm_3266",
ChainID: big.NewI(int64(3266)),
},
},
wantTables: []string{
"forwarders",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err = Register0002(tt.args.cfg)
require.NoError(t, err)

// we need a table to store the goose version for this cfg
goose.SetTableName(fmt.Sprintf("goose_version_%s", tt.args.cfg.Schema))
// run the migrations from the embedded templates

tDir := t.TempDir()
_, err := generateMigrations(embeddedTmplFS, MigrationRootDir, tDir, tt.args.cfg)
require.NoError(t, err, "failed to generate migrations")
//goose.SetBaseFS(tDir)
err = goose.UpTo(db.DB, tDir, 2)
require.NoError(t, err, "failed to run migrations")

// test that the migrations were applied
goose.Status(db.DB, MigrationRootDir)

rows, err := db.DB.Query("SELECT schemaname, tablename FROM pg_catalog.pg_tables where schemaname = $1", tt.args.cfg.Schema)
require.NoError(t, err)
defer rows.Close()
var gotTables []string
for rows.Next() {
var schema, table string
err = rows.Scan(&schema, &table)
t.Logf("schema: %s, table: %s", schema, table)
require.NoError(t, err)
gotTables = append(gotTables, table)
}
// check the error from rows
err = rows.Err()
require.NoError(t, err)
assert.Equal(t, tt.wantTables, gotTables)

// check the goose version
v, err := goose.EnsureDBVersion(db.DB)
require.NoError(t, err)
assert.Equal(t, int64(2), v)

// run the down migrations
err = goose.DownTo(db.DB, tDir, 0)
require.NoError(t, err, "failed to run down migrations")
v, err = goose.EnsureDBVersion(db.DB)
require.NoError(t, err)
rows, err = db.DB.Query("SELECT schemaname, tablename FROM pg_catalog.pg_tables where schemaname = $1", tt.args.cfg.Schema)
require.NoError(t, err)
defer rows.Close()
var gotDownTables []string
for rows.Next() {
var schema, table string
err = rows.Scan(&schema, &table)
t.Logf("schema: %s, table: %s", schema, table)
require.NoError(t, err)
gotTables = append(gotTables, table)
}
assert.Len(t, gotDownTables, 0)
})
}
t.FailNow()
}

0 comments on commit dbd5cf7

Please sign in to comment.