diff --git a/txmanager/pgstorage.go b/txmanager/pgstorage.go index 4e48363..1cc0846 100644 --- a/txmanager/pgstorage.go +++ b/txmanager/pgstorage.go @@ -8,6 +8,7 @@ import ( "time" txmTypes "github.com/0xPolygon/agglayer/txmanager/types" + "github.com/0xPolygonHermez/zkevm-node/db" "github.com/ethereum/go-ethereum/common" "github.com/jackc/pgconn" "github.com/jackc/pgx/v4" @@ -25,6 +26,18 @@ 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 + } + + return &PostgresStorage{ + db, + }, nil +} + // Add persist a monitored tx func (s *PostgresStorage) Add(ctx context.Context, mTx txmTypes.MonitoredTx, dbTx pgx.Tx) error { conn := s.dbConn(dbTx) diff --git a/txmanager/pgstorage_test.go b/txmanager/pgstorage_test.go index 651218b..066304b 100644 --- a/txmanager/pgstorage_test.go +++ b/txmanager/pgstorage_test.go @@ -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" @@ -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") @@ -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") @@ -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" @@ -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) @@ -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)