From 1f2b35b8fad75be470e6e85747acc5c462b79aca Mon Sep 17 00:00:00 2001 From: Arnau Date: Thu, 12 Sep 2024 17:56:21 +0200 Subject: [PATCH] fix UTs and lint --- bridgesync/claimcalldata_test.go | 2 ++ bridgesync/processor.go | 61 +++++++++++++++++++------------- bridgesync/processor_test.go | 8 +++-- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/bridgesync/claimcalldata_test.go b/bridgesync/claimcalldata_test.go index d788c2c7..1319835b 100644 --- a/bridgesync/claimcalldata_test.go +++ b/bridgesync/claimcalldata_test.go @@ -28,6 +28,7 @@ type testCase struct { func TestClaimCalldata(t *testing.T) { testCases := []testCase{} // Setup Docker L1 + log.Debug("starting docker") ctx := context.Background() msg, err := exec.Command("bash", "-l", "-c", "docker compose up -d").CombinedOutput() require.NoError(t, err, string(msg)) @@ -36,6 +37,7 @@ func TestClaimCalldata(t *testing.T) { msg, err = exec.Command("bash", "-l", "-c", "docker compose down").CombinedOutput() require.NoError(t, err, string(msg)) }() + log.Debug("docker started") client, err := ethclient.Dial("http://localhost:8545") require.NoError(t, err) privateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80") diff --git a/bridgesync/processor.go b/bridgesync/processor.go index 942ec9b1..b34416a0 100644 --- a/bridgesync/processor.go +++ b/bridgesync/processor.go @@ -5,6 +5,7 @@ import ( "database/sql" "encoding/binary" "errors" + "fmt" "math/big" "github.com/0xPolygon/cdk/bridgesync/migrations" @@ -120,11 +121,24 @@ func newProcessor(dbPath, loggerPrefix string) (*processor, error) { func (p *processor) GetBridges( ctx context.Context, fromBlock, toBlock uint64, ) ([]Bridge, error) { - bridgePtrs := []*Bridge{} - bridgesIface, err := p.getTypeFromBlockToBlock(ctx, fromBlock, toBlock, "bridge", bridgePtrs) + tx, err := p.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) + if err != nil { + return nil, err + } + defer func() { + if err := tx.Rollback(); err != nil { + log.Warnf("error rolling back tx: %v", err) + } + }() + rows, err := p.queryBlockRange(tx, fromBlock, toBlock, "bridge") if err != nil { return nil, err } + bridgePtrs := []*Bridge{} + if err = meddler.ScanAll(rows, &bridgePtrs); err != nil { + return nil, err + } + bridgesIface := db.SlicePtrsToSlice(bridgePtrs) bridges, ok := bridgesIface.([]Bridge) if !ok { return nil, errors.New("failed to convert from []*Bridge to []Bridge") @@ -135,21 +149,6 @@ func (p *processor) GetBridges( func (p *processor) GetClaims( ctx context.Context, fromBlock, toBlock uint64, ) ([]Claim, error) { - claimPtrs := []*Claim{} - claimsIface, err := p.getTypeFromBlockToBlock(ctx, fromBlock, toBlock, "claim", claimPtrs) - if err != nil { - return nil, err - } - claims, ok := claimsIface.([]Claim) - if !ok { - return nil, errors.New("failed to convert from []*Claim to []Claim") - } - return claims, nil -} - -func (p *processor) getTypeFromBlockToBlock( - ctx context.Context, fromBlock, toBlock uint64, table string, typeToQuery interface{}, -) (interface{}, error) { tx, err := p.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) if err != nil { return nil, err @@ -159,23 +158,37 @@ func (p *processor) getTypeFromBlockToBlock( log.Warnf("error rolling back tx: %v", err) } }() - - if err = p.isBlockProcessed(tx, toBlock); err != nil { + rows, err := p.queryBlockRange(tx, fromBlock, toBlock, "claim") + if err != nil { + return nil, err + } + claimPtrs := []*Claim{} + if err = meddler.ScanAll(rows, &claimPtrs); err != nil { return nil, err } + claimsIface := db.SlicePtrsToSlice(claimPtrs) + claims, ok := claimsIface.([]Claim) + if !ok { + return nil, errors.New("failed to convert from []*Claim to []Claim") + } + return claims, nil +} - err = meddler.QueryAll(tx, typeToQuery, ` - SELECT * FROM `+table+` +func (p *processor) queryBlockRange(tx db.DBer, fromBlock, toBlock uint64, table string) (*sql.Rows, error) { + if err := p.isBlockProcessed(tx, toBlock); err != nil { + return nil, err + } + rows, err := tx.Query(fmt.Sprintf(` + SELECT * FROM %s WHERE block_num >= $1 AND block_num <= $2; - `, fromBlock, toBlock) + `, table), fromBlock, toBlock) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } return nil, err } - - return db.SlicePtrsToSlice(typeToQuery), nil + return rows, nil } func (p *processor) isBlockProcessed(tx db.DBer, blockNum uint64) error { diff --git a/bridgesync/processor_test.go b/bridgesync/processor_test.go index 5578cee5..2ff03c76 100644 --- a/bridgesync/processor_test.go +++ b/bridgesync/processor_test.go @@ -420,10 +420,11 @@ func (a *getClaims) desc() string { return a.description } -func (a *getClaims) execute(t *testing.T) { //nolint:thelper +func (a *getClaims) execute(t *testing.T) { + t.Helper() actualEvents, actualErr := a.p.GetClaims(a.ctx, a.fromBlock, a.toBlock) - require.Equal(t, a.expectedClaims, actualEvents) require.Equal(t, a.expectedErr, actualErr) + require.Equal(t, a.expectedClaims, actualEvents) } // GetBridges @@ -446,7 +447,8 @@ func (a *getBridges) desc() string { return a.description } -func (a *getBridges) execute(t *testing.T) { //nolint:thelper +func (a *getBridges) execute(t *testing.T) { + t.Helper() actualEvents, actualErr := a.p.GetBridges(a.ctx, a.fromBlock, a.toBlock) require.Equal(t, a.expectedBridges, actualEvents) require.Equal(t, a.expectedErr, actualErr)