Skip to content

Commit

Permalink
fix linter II
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Sep 12, 2024
1 parent f541a20 commit 76b5b44
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 40 deletions.
5 changes: 2 additions & 3 deletions bridgesync/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ var bridgeMigrations = &migrate.MemoryMigrationSource{
}

func RunMigrations(dbPath string) error {
migs := append(
return db.RunMigrations(dbPath, &migrate.MemoryMigrationSource{Migrations: append(
bridgeMigrations.Migrations,
treeMigrations.Migrations.Migrations...,
)
return db.RunMigrations(dbPath, &migrate.MemoryMigrationSource{Migrations: migs})
)})
}
60 changes: 34 additions & 26 deletions bridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,34 +120,36 @@ func newProcessor(dbPath, loggerPrefix string) (*processor, error) {
func (p *processor) GetBridges(
ctx context.Context, fromBlock, toBlock uint64,
) ([]Bridge, error) {
tx, err := p.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
bridgePtrs := []*Bridge{}
bridgesIface, err := p.getTypeFromBlockToBlock(ctx, fromBlock, toBlock, "bridge", bridgePtrs)
if err != nil {
return nil, err
}
defer func() {
if err := tx.Rollback(); err != nil {
log.Warnf("error rolling back tx: %v", err)
}
}()

if err = p.isBlockProcessed(tx, toBlock); err != nil {
return nil, err
}

bridges := []*Bridge{}
err = meddler.QueryAll(tx, &bridges, `
SELECT * FROM bridge
WHERE block_num >= $1 AND block_num <= $2;
`, fromBlock, toBlock)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
bridges, ok := bridgesIface.([]Bridge)
if !ok {
return nil, errors.New("failed to convert from []*Bridge to []Bridge")
}
return db.SlicePtrsToSlice(bridges).([]Bridge), err
return bridges, nil
}

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
Expand All @@ -162,15 +164,18 @@ func (p *processor) GetClaims(
return nil, err
}

claims := []*Claim{}
err = meddler.QueryAll(tx, &claims, `
SELECT * FROM claim
err = meddler.QueryAll(tx, typeToQuery, `
SELECT * FROM `+table+`
WHERE block_num >= $1 AND block_num <= $2;
`, fromBlock, toBlock)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
return nil, err
}
return db.SlicePtrsToSlice(claims).([]Claim), err

return db.SlicePtrsToSlice(typeToQuery), nil
}

func (p *processor) isBlockProcessed(tx db.DBer, blockNum uint64) error {
Expand Down Expand Up @@ -249,7 +254,10 @@ func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error {
return err
}
for _, e := range block.Events {
event := e.(Event)
event, ok := e.(Event)
if !ok {
return errors.New("failed to convert sync.Block.Event to Event")
}
if event.Bridge != nil {
if err = p.exitTree.AddLeaf(tx, block.Num, event.Pos, types.Leaf{
Index: event.Bridge.DepositCount,
Expand Down
18 changes: 12 additions & 6 deletions bridgesync/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ var (
type processAction interface {
method() string
desc() string
execute(t *testing.T) //nolint:thelper
execute(t *testing.T)
}

// GetClaims
Expand Down Expand Up @@ -470,7 +470,7 @@ func (a *getLastProcessedBlockAction) desc() string {
return a.description
}

func (a *getLastProcessedBlockAction) execute(t *testing.T) { //nolint:thelper
func (a *getLastProcessedBlockAction) execute(t *testing.T) {
t.Helper()

actualLastProcessedBlock, actualErr := a.p.GetLastProcessedBlock(a.ctx)
Expand All @@ -495,7 +495,7 @@ func (a *reorgAction) desc() string {
return a.description
}

func (a *reorgAction) execute(t *testing.T) { //nolint:thelper
func (a *reorgAction) execute(t *testing.T) {
t.Helper()

actualErr := a.p.Reorg(context.Background(), a.firstReorgedBlock)
Expand All @@ -519,7 +519,7 @@ func (a *processBlockAction) desc() string {
return a.description
}

func (a *processBlockAction) execute(t *testing.T) { //nolint:thelper
func (a *processBlockAction) execute(t *testing.T) {
t.Helper()

actualErr := a.p.ProcessBlock(context.Background(), a.block)
Expand All @@ -529,7 +529,10 @@ func (a *processBlockAction) execute(t *testing.T) { //nolint:thelper
func eventsToBridges(events []interface{}) []Bridge {
bridges := []Bridge{}
for _, event := range events {
e := event.(Event)
e, ok := event.(Event)
if !ok {
panic("should be ok")
}
if e.Bridge != nil {
bridges = append(bridges, *e.Bridge)
}
Expand All @@ -540,7 +543,10 @@ func eventsToBridges(events []interface{}) []Bridge {
func eventsToClaims(events []interface{}) []Claim {
claims := []Claim{}
for _, event := range events {
e := event.(Event)
e, ok := event.(Event)
if !ok {
panic("should be ok")
}
if e.Claim != nil {
claims = append(claims, *e.Claim)
}
Expand Down
4 changes: 4 additions & 0 deletions db/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
_ "github.com/mattn/go-sqlite3"
)

const (
UniqueConstrain = 1555
)

// NewSQLiteDB creates a new SQLite DB
func NewSQLiteDB(dbPath string) (*sql.DB, error) {
initMeddler()
Expand Down
4 changes: 2 additions & 2 deletions tree/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func MigrationsWithPrefix(prefix string) []*migrate.Migration {
return []*migrate.Migration{
{
Id: prefix + "tree001",
Up: []string{strings.Replace(mig001splitted[1], dbPrefixReplacer, prefix, -1)},
Down: []string{strings.Replace(mig001splitted[0], dbPrefixReplacer, prefix, -1)},
Up: []string{strings.ReplaceAll(mig001splitted[1], dbPrefixReplacer, prefix)},
Down: []string{strings.ReplaceAll(mig001splitted[0], dbPrefixReplacer, prefix)},
},
}
}
4 changes: 1 addition & 3 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/common"
"github.com/mattn/go-sqlite3"
"github.com/russross/meddler"
"golang.org/x/crypto/sha3"
)
Expand Down Expand Up @@ -157,8 +156,7 @@ func (t *Tree) storeNodes(tx *db.Tx, nodes []types.TreeNode) error {
for i := 0; i < len(nodes); i++ {
if err := meddler.Insert(tx, t.rhtTable, &nodes[i]); err != nil {
if sqliteErr, ok := db.SQLiteErr(err); ok {
uniqueConstraint := sqlite3.ErrNoExtended(1555) // 1555 is the error code for primary key violation
if sqliteErr.ExtendedCode == uniqueConstraint {
if sqliteErr.ExtendedCode == db.UniqueConstrain {
// ignore repeated entries. This is likely to happen due to not
// cleaning RHT when reorg
continue
Expand Down

0 comments on commit 76b5b44

Please sign in to comment.