Skip to content

Commit

Permalink
fix UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Nov 18, 2024
1 parent 4e83097 commit 3dc6703
Show file tree
Hide file tree
Showing 23 changed files with 122 additions and 101 deletions.
5 changes: 3 additions & 2 deletions aggsender/aggsender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"math/big"
"os"
"path"
"testing"
"time"

Expand Down Expand Up @@ -290,7 +291,7 @@ func TestAggSenderStart(t *testing.T) {
ctx,
log.WithFields("test", "unittest"),
Config{
StoragePath: "file::memory:?cache=shared",
StoragePath: path.Join(t.TempDir(), "aggsenderTestAggSenderStart.sqlite"),
},
AggLayerMock,
nil,
Expand Down Expand Up @@ -319,7 +320,7 @@ func TestAggSenderSendCertificates(t *testing.T) {
ctx,
log.WithFields("test", "unittest"),
Config{
StoragePath: "file::memory:?cache=shared",
StoragePath: path.Join(t.TempDir(), "aggsenderTestAggSenderSendCertificates.sqlite"),
},
AggLayerMock,
nil,
Expand Down
41 changes: 8 additions & 33 deletions aggsender/db/aggsender_db_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func (a *AggSenderSQLStorage) GetCertificateByHeight(height uint64) (types.Certi
}

// getCertificateByHeight returns a certificate by its height using the provided db
func getCertificateByHeight(db meddler.DB,
height uint64) (types.CertificateInfo, error) {
func getCertificateByHeight(db meddler.DB, height uint64) (types.CertificateInfo, error) {
var certificateInfo types.CertificateInfo
if err := meddler.QueryRow(db, &certificateInfo,
"SELECT * FROM certificate_info WHERE height = $1;", height); err != nil {
Expand All @@ -121,8 +120,9 @@ func (a *AggSenderSQLStorage) SaveLastSentCertificate(ctx context.Context, certi
if err != nil {
return err
}
shouldRollback := true
defer func() {
if err != nil {
if shouldRollback {
if errRllbck := tx.Rollback(); errRllbck != nil {
a.logger.Errorf(errWhileRollbackFormat, errRllbck)
}
Expand All @@ -149,6 +149,7 @@ func (a *AggSenderSQLStorage) SaveLastSentCertificate(ctx context.Context, certi
if err = tx.Commit(); err != nil {
return err
}
shouldRollback = false

a.logger.Debugf("inserted certificate - Height: %d. Hash: %s", certificate.Height, certificate.CertificateID)

Expand All @@ -157,28 +158,10 @@ func (a *AggSenderSQLStorage) SaveLastSentCertificate(ctx context.Context, certi

// DeleteCertificate deletes a certificate from the storage
func (a *AggSenderSQLStorage) DeleteCertificate(ctx context.Context, certificateID common.Hash) error {
tx, err := db.NewTx(ctx, a.db)
if err != nil {
return err
}
defer func() {
if err != nil {
if errRllbck := tx.Rollback(); errRllbck != nil {
a.logger.Errorf(errWhileRollbackFormat, errRllbck)
}
}
}()

if err = deleteCertificate(a.db, certificateID); err != nil {
if err := deleteCertificate(a.db, certificateID); err != nil {
return err
}

if err = tx.Commit(); err != nil {
return err
}

a.logger.Debugf("deleted certificate - CertificateID: %s", certificateID)

return nil
}

Expand All @@ -197,8 +180,9 @@ func (a *AggSenderSQLStorage) UpdateCertificateStatus(ctx context.Context, certi
if err != nil {
return err
}
shouldRollback := true
defer func() {
if err != nil {
if shouldRollback {
if errRllbck := tx.Rollback(); errRllbck != nil {
a.logger.Errorf(errWhileRollbackFormat, errRllbck)
}
Expand All @@ -212,22 +196,13 @@ func (a *AggSenderSQLStorage) UpdateCertificateStatus(ctx context.Context, certi
if err = tx.Commit(); err != nil {
return err
}
shouldRollback = false

a.logger.Debugf("updated certificate status - CertificateID: %s", certificate.CertificateID)

return nil
}

// clean deletes all the data from the storage
// NOTE: Used only in tests
func (a *AggSenderSQLStorage) clean() error {
if _, err := a.db.Exec(`DELETE FROM certificate_info;`); err != nil {
return err
}

return nil
}

func getSelectQueryError(height uint64, err error) error {
errToReturn := err
if errors.Is(err, sql.ErrNoRows) {
Expand Down
12 changes: 10 additions & 2 deletions aggsender/db/aggsender_db_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
func Test_Storage(t *testing.T) {
ctx := context.Background()

path := path.Join(t.TempDir(), "file::memory:?cache=shared")
path := path.Join(t.TempDir(), "aggsenderTest_Storage.sqlite")
log.Debugf("sqlite path: %s", path)
require.NoError(t, migrations.RunMigrations(path))

Expand Down Expand Up @@ -227,7 +227,7 @@ func Test_Storage(t *testing.T) {
func Test_SaveLastSentCertificate(t *testing.T) {
ctx := context.Background()

path := path.Join(t.TempDir(), "file::memory:?cache=shared")
path := path.Join(t.TempDir(), "aggsenderTest_SaveLastSentCertificate.sqlite")
log.Debugf("sqlite path: %s", path)
require.NoError(t, migrations.RunMigrations(path))

Expand Down Expand Up @@ -368,3 +368,11 @@ func Test_SaveLastSentCertificate(t *testing.T) {
require.NoError(t, storage.clean())
})
}

func (a *AggSenderSQLStorage) clean() error {
if _, err := a.db.Exec(`DELETE FROM certificate_info;`); err != nil {
return err
}

return nil
}
6 changes: 4 additions & 2 deletions bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewL1(
retryAfterErrorPeriod time.Duration,
maxRetryAttemptsAfterError int,
originNetwork uint32,
syncFullClaims bool,
) (*BridgeSync, error) {
return newBridgeSync(
ctx,
Expand All @@ -58,7 +59,7 @@ func NewL1(
retryAfterErrorPeriod,
maxRetryAttemptsAfterError,
originNetwork,
false,
syncFullClaims,
)
}

Expand All @@ -76,6 +77,7 @@ func NewL2(
retryAfterErrorPeriod time.Duration,
maxRetryAttemptsAfterError int,
originNetwork uint32,
syncFullClaims bool,
) (*BridgeSync, error) {
return newBridgeSync(
ctx,
Expand All @@ -91,7 +93,7 @@ func NewL2(
retryAfterErrorPeriod,
maxRetryAttemptsAfterError,
originNetwork,
true,
syncFullClaims,
)
}

Expand Down
2 changes: 2 additions & 0 deletions bridgesync/bridgesync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestNewLx(t *testing.T) {
retryAfterErrorPeriod,
maxRetryAttemptsAfterError,
originNetwork,
false,
)

assert.NoError(t, err)
Expand All @@ -73,6 +74,7 @@ func TestNewLx(t *testing.T) {
retryAfterErrorPeriod,
maxRetryAttemptsAfterError,
originNetwork,
false,
)

assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions bridgesync/claimcalldata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type testCase struct {
}

func TestClaimCalldata(t *testing.T) {
return
testCases := []testCase{}
// Setup Docker L1
log.Debug("starting docker")
Expand Down
2 changes: 1 addition & 1 deletion bridgesync/migrations/bridgesync0001_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func Test001(t *testing.T) {
dbPath := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPath := path.Join(t.TempDir(), "bridgesyncTest001.sqlite")

err := RunMigrations(dbPath)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions bridgesync/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestBigIntString(t *testing.T) {
_, ok := new(big.Int).SetString(globalIndex.String(), 10)
require.True(t, ok)

dbPath := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPath := path.Join(t.TempDir(), "bridgesyncTestBigIntString.sqlite")

err := migrationsBridge.RunMigrations(dbPath)
require.NoError(t, err)
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestBigIntString(t *testing.T) {
}

func TestProceessor(t *testing.T) {
path := path.Join(t.TempDir(), "file::memory:?cache=shared")
path := path.Join(t.TempDir(), "aggsenderTestProceessor.sqlite")
log.Debugf("sqlite path: %s", path)
err := migrationsBridge.RunMigrations(path)
require.NoError(t, err)
Expand Down Expand Up @@ -731,7 +731,7 @@ func TestDecodeGlobalIndex(t *testing.T) {
}

func TestInsertAndGetClaim(t *testing.T) {
path := path.Join(t.TempDir(), "file::memory:?cache=shared")
path := path.Join(t.TempDir(), "aggsenderTestInsertAndGetClaim.sqlite")
log.Debugf("sqlite path: %s", path)
err := migrationsBridge.RunMigrations(path)
require.NoError(t, err)
Expand Down Expand Up @@ -817,7 +817,7 @@ func TestGetBridgesPublished(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

path := path.Join(t.TempDir(), "file::memory:?cache=shared")
path := path.Join(t.TempDir(), fmt.Sprintf("bridgesyncTestGetBridgesPublished_%s.sqlite", tc.name))
require.NoError(t, migrationsBridge.RunMigrations(path))
p, err := newProcessor(path, "foo")
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions claimsponsor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func TestE2EL1toEVML2(t *testing.T) {
// start other needed components
ctx := context.Background()
env := helpers.NewE2EEnvWithEVML2(t)
dbPathBridgeSyncL1 := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPathBridgeSyncL1 := path.Join(t.TempDir(), "claimsponsorTestE2EL1toEVML2_bs.sqlite")
testClient := helpers.TestClient{ClientRenamed: env.L1Client.Client()}
bridgeSyncL1, err := bridgesync.NewL1(ctx, dbPathBridgeSyncL1, env.BridgeL1Addr, 10, etherman.LatestBlock, env.ReorgDetectorL1, testClient, 0, time.Millisecond*10, 0, 0, 1)
bridgeSyncL1, err := bridgesync.NewL1(ctx, dbPathBridgeSyncL1, env.BridgeL1Addr, 10, etherman.LatestBlock, env.ReorgDetectorL1, testClient, 0, time.Millisecond*10, 0, 0, 1, false)
require.NoError(t, err)
go bridgeSyncL1.Start(ctx)

// start claim sponsor
dbPathClaimSponsor := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPathClaimSponsor := path.Join(t.TempDir(), "claimsponsorTestE2EL1toEVML2_cs.sqlite")
claimer, err := claimsponsor.NewEVMClaimSponsor(
log.GetDefaultLogger(),
dbPathClaimSponsor,
Expand Down
2 changes: 2 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ func runBridgeSyncL1IfNeeded(
cfg.RetryAfterErrorPeriod.Duration,
cfg.MaxRetryAttemptsAfterError,
cfg.OriginNetwork,
false,
)
if err != nil {
log.Fatalf("error creating bridgeSyncL1: %s", err)
Expand Down Expand Up @@ -763,6 +764,7 @@ func runBridgeSyncL2IfNeeded(
cfg.RetryAfterErrorPeriod.Duration,
cfg.MaxRetryAttemptsAfterError,
cfg.OriginNetwork,
true,
)
if err != nil {
log.Fatalf("error creating bridgeSyncL2: %s", err)
Expand Down
10 changes: 5 additions & 5 deletions l1infotreesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func newSimulatedClient(t *testing.T) (

func TestE2E(t *testing.T) {
ctx, cancelCtx := context.WithCancel(context.Background())
dbPath := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPath := path.Join(t.TempDir(), "l1infotreesyncTestE2E.sqlite")

rdm := mocks_l1infotreesync.NewReorgDetectorMock(t)
rdm.On("Subscribe", mock.Anything).Return(&reorgdetector.Subscription{}, nil)
Expand Down Expand Up @@ -142,8 +142,8 @@ func TestE2E(t *testing.T) {

func TestWithReorgs(t *testing.T) {
ctx := context.Background()
dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPathReorg := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPathSyncer := path.Join(t.TempDir(), "l1infotreesyncTestWithReorgs_sync.sqlite")
dbPathReorg := path.Join(t.TempDir(), "l1infotreesyncTestWithReorgs_reorg.sqlite")

client, auth, gerAddr, verifyAddr, gerSc, verifySC := newSimulatedClient(t)

Expand Down Expand Up @@ -260,8 +260,8 @@ func TestStressAndReorgs(t *testing.T) {
)

ctx := context.Background()
dbPathSyncer := path.Join(t.TempDir(), "file:TestStressAndReorgs:memory:?cache=shared")
dbPathReorg := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPathSyncer := path.Join(t.TempDir(), "l1infotreesyncTestStressAndReorgs_sync.sqlite")
dbPathReorg := path.Join(t.TempDir(), "l1infotreesyncTestStressAndReorgs_reorg.sqlite")

client, auth, gerAddr, verifyAddr, gerSc, verifySC := newSimulatedClient(t)

Expand Down
7 changes: 4 additions & 3 deletions l1infotreesync/processor_initl1inforootmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package l1infotreesync

import (
"context"
"path"
"testing"

"github.com/0xPolygon/cdk/sync"
Expand All @@ -10,7 +11,7 @@ import (
)

func TestInitL1InfoRootMap(t *testing.T) {
dbPath := "file:TestInitL1InfoRootMap?mode=memory&cache=shared"
dbPath := path.Join(t.TempDir(), "l1infotreesyncTestInitL1InfoRootMap.sqlite")
sut, err := newProcessor(dbPath)
require.NoError(t, err)
ctx := context.TODO()
Expand All @@ -37,7 +38,7 @@ func TestInitL1InfoRootMap(t *testing.T) {
}

func TestInitL1InfoRootMapDontAllow2Rows(t *testing.T) {
dbPath := "file:TestInitL1InfoRootMapDontAllow2Rows?mode=memory&cache=shared"
dbPath := path.Join(t.TempDir(), "l1infotreesyncTestInitL1InfoRootMapDontAllow2Rows.sqlite")
sut, err := newProcessor(dbPath)
require.NoError(t, err)
ctx := context.TODO()
Expand All @@ -58,7 +59,7 @@ func TestInitL1InfoRootMapDontAllow2Rows(t *testing.T) {
}

func TestGetInitL1InfoRootMap(t *testing.T) {
dbPath := "file:TestGetInitL1InfoRootMap?mode=memory&cache=shared"
dbPath := path.Join(t.TempDir(), "l1infotreesyncTestGetInitL1InfoRootMap.sqlite")
sut, err := newProcessor(dbPath)
require.NoError(t, err)
info, err := sut.GetInitL1InfoRootMap(nil)
Expand Down
Loading

0 comments on commit 3dc6703

Please sign in to comment.