Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
PostgressDB connection created twice (#123)
Browse files Browse the repository at this point in the history
* fix

* tests fix

* fix TestHistoryHashSlice

* fix TestMonitoredTx_HistoryStringSlice
  • Loading branch information
goran-ethernal authored Apr 15, 2024
1 parent 13e5a96 commit bbee677
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
5 changes: 1 addition & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ func start(cliCtx *cli.Context) error {
}

// Prepare EthTxMan client
ethTxManagerStorage, err := txmanager.NewPostgresStorage(c.DB)
if err != nil {
return err
}
ethTxManagerStorage := txmanager.NewPostgresStorage(pg)
etm := txmanager.New(c.EthTxManager, &ethMan, ethTxManagerStorage, &ethMan)

// Create opentelemetry metric provider
Expand Down
7 changes: 6 additions & 1 deletion txmanager/pgstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ type PostgresStorage struct {

// NewPostgresStorage creates a new instance of storage that use
// postgres to store data
func NewPostgresStorage(dbCfg db.Config) (*PostgresStorage, error) {
func NewPostgresStorage(db *pgxpool.Pool) *PostgresStorage {
return &PostgresStorage{db}
}

// NewPostgresStorageWithCfg creates a new instance of storage that use based on provided config
func NewPostgresStorageWithCfg(dbCfg db.Config) (*PostgresStorage, error) {
db, err := db.NewSQLDB(dbCfg)
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions txmanager/pgstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newStateDBConfig(t *testing.T) db.Config {

func TestAddGetAndUpdate(t *testing.T) {
dbCfg := newStateDBConfig(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

owner := "owner"
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestAddGetAndUpdate(t *testing.T) {

func TestAddAndGetByStatus(t *testing.T) {
dbCfg := newStateDBConfig(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

to := common.HexToAddress("0x2")
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestAddAndGetByStatus(t *testing.T) {

func TestAddAndGetBySenderAndStatus(t *testing.T) {
dbCfg := newStateDBConfig(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

from := common.HexToAddress("0x1")
Expand Down Expand Up @@ -280,7 +280,7 @@ func TestAddAndGetBySenderAndStatus(t *testing.T) {

func TestAddRepeated(t *testing.T) {
dbCfg := newStateDBConfig(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

owner := "owner"
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestAddRepeated(t *testing.T) {

func TestGetNotFound(t *testing.T) {
dbCfg := newStateDBConfig(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

_, err = storage.Get(context.Background(), "not found owner", "not found id", nil)
Expand All @@ -329,7 +329,7 @@ func TestGetNotFound(t *testing.T) {

func TestGetByStatusNoRows(t *testing.T) {
dbCfg := newStateDBConfig(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

mTxs, err := storage.GetByStatus(context.Background(), nil, []txmTypes.MonitoredTxStatus{}, nil)
Expand Down
14 changes: 7 additions & 7 deletions txmanager/txmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var defaultEthTxmanagerConfigForTests = config.EthTxManagerConfig{
func TestTxGetMined(t *testing.T) {
dbCfg := newStateDBConfig(t)
etherman := mocks.NewEthermanMock(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

ethTxManagerClient := New(defaultEthTxmanagerConfigForTests, etherman, storage, etherman)
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestTxGetMinedAfterReviewed(t *testing.T) {
dbCfg := newStateDBConfig(t)

etherman := mocks.NewEthermanMock(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

ethTxManagerClient := New(defaultEthTxmanagerConfigForTests, etherman, storage, etherman)
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestTxGetMinedAfterReviewed(t *testing.T) {
func TestExecutionReverted(t *testing.T) {
dbCfg := newStateDBConfig(t)
etherman := mocks.NewEthermanMock(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

ethTxManagerClient := New(defaultEthTxmanagerConfigForTests, etherman, storage, etherman)
Expand Down Expand Up @@ -533,7 +533,7 @@ func TestGasPriceMarginAndLimit(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
dbCfg := newStateDBConfig(t)
etherman := mocks.NewEthermanMock(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

var cfg = config.EthTxManagerConfig{
Expand Down Expand Up @@ -618,7 +618,7 @@ func TestGasOffset(t *testing.T) {
dbCfg := newStateDBConfig(t)

etherman := mocks.NewEthermanMock(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

var cfg = config.EthTxManagerConfig{
Expand Down Expand Up @@ -673,7 +673,7 @@ func TestGasOffset(t *testing.T) {
func TestFailedToEstimateTxWithForcedGasGetMined(t *testing.T) {
dbCfg := newStateDBConfig(t)
etherman := mocks.NewEthermanMock(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

// set forced gas
Expand Down Expand Up @@ -793,7 +793,7 @@ func TestTxRetry_MaxRetriesReached(t *testing.T) {
dbCfg := newStateDBConfig(t)

etherman := mocks.NewEthermanMock(t)
storage, err := NewPostgresStorage(dbCfg)
storage, err := NewPostgresStorageWithCfg(dbCfg)
require.NoError(t, err)

config := config.EthTxManagerConfig{
Expand Down
21 changes: 8 additions & 13 deletions txmanager/types/monitoretx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ func TestMonitoredTx_HistoryStringSlice(t *testing.T) {
},
}

expected := []string{
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000002",
"0x0000000000000000000000000000000000000000000000000000000000000003",
}
result := mTx.HistoryStringSlice()
assert.Equal(t, len(mTx.History), len(result))

assert.Equal(t, expected, result)
for _, hash := range result {
assert.True(t, mTx.History[common.HexToHash(hash)])
}
}

func TestHistoryHashSlice(t *testing.T) {
Expand All @@ -84,15 +82,12 @@ func TestHistoryHashSlice(t *testing.T) {
},
}

expected := []common.Hash{
common.HexToHash("0x1"),
common.HexToHash("0x2"),
common.HexToHash("0x3"),
}

result := mTx.HistoryHashSlice()
assert.Equal(t, len(mTx.History), len(result))

assert.Equal(t, expected, result)
for _, hash := range result {
assert.True(t, mTx.History[hash])
}
}

func TestMonitoredTx_BlockNumberU64Ptr(t *testing.T) {
Expand Down

0 comments on commit bbee677

Please sign in to comment.