Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Sep 12, 2024
1 parent f05383e commit 3d5a2ab
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 29 deletions.
13 changes: 10 additions & 3 deletions bridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ func (p *processor) GetBridges(
if err != nil {
return nil, err
}

defer tx.Rollback()
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
Expand All @@ -149,7 +152,11 @@ func (p *processor) GetClaims(
if err != nil {
return nil, err
}
defer tx.Rollback()
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
Expand Down
12 changes: 6 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)
execute(t *testing.T) //nolint:thelper
}

// GetClaims
Expand All @@ -420,7 +420,7 @@ func (a *getClaims) desc() string {
return a.description
}

func (a *getClaims) execute(t *testing.T) {
func (a *getClaims) execute(t *testing.T) { //nolint:thelper
actualEvents, actualErr := a.p.GetClaims(a.ctx, a.fromBlock, a.toBlock)
require.Equal(t, a.expectedClaims, actualEvents)
require.Equal(t, a.expectedErr, actualErr)
Expand All @@ -446,7 +446,7 @@ func (a *getBridges) desc() string {
return a.description
}

func (a *getBridges) execute(t *testing.T) {
func (a *getBridges) execute(t *testing.T) { //nolint:thelper
actualEvents, actualErr := a.p.GetBridges(a.ctx, a.fromBlock, a.toBlock)
require.Equal(t, a.expectedBridges, actualEvents)
require.Equal(t, a.expectedErr, actualErr)
Expand All @@ -470,7 +470,7 @@ func (a *getLastProcessedBlockAction) desc() string {
return a.description
}

func (a *getLastProcessedBlockAction) execute(t *testing.T) {
func (a *getLastProcessedBlockAction) execute(t *testing.T) { //nolint:thelper
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) {
func (a *reorgAction) execute(t *testing.T) { //nolint:thelper
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) {
func (a *processBlockAction) execute(t *testing.T) { //nolint:thelper
t.Helper()

actualErr := a.p.ProcessBlock(context.Background(), a.block)
Expand Down
29 changes: 19 additions & 10 deletions db/meddler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ func initMeddler() {
meddler.Register("hash", HashMeddler{})
}

func SQLiteErr(err error) (sqlite.Error, bool) {
if sqliteErr, ok := err.(sqlite.Error); ok {
func SQLiteErr(err error) (*sqlite.Error, bool) {
sqliteErr := &sqlite.Error{}
if ok := errors.As(err, sqliteErr); ok {
return sqliteErr, true
}
if driverErr, ok := meddler.DriverErr(err); ok {
if sqliteErr, ok := driverErr.(sqlite.Error); ok {
return sqliteErr, true
}
return sqliteErr, errors.As(driverErr, sqliteErr)
}
return sqlite.Error{}, false
return sqliteErr, false
}

// SliceToSlicePtrs converts any []Foo to []*Foo
Expand Down Expand Up @@ -68,15 +67,19 @@ func (b BigIntMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, e

// PostRead is called after a Scan operation for fields that have the BigIntMeddler
func (b BigIntMeddler) PostRead(fieldPtr, scanTarget interface{}) error {
ptr := scanTarget.(*string)
ptr, ok := scanTarget.(*string)
if !ok {
return errors.New("scanTarget is not *string")
}
if ptr == nil {
return fmt.Errorf("BigIntMeddler.PostRead: nil pointer")
}
field, ok := fieldPtr.(**big.Int)
if !ok {
return errors.New("fieldPtr is not *big.Int")
}
*field, ok = new(big.Int).SetString(*ptr, 10)
decimal := 10
*field, ok = new(big.Int).SetString(*ptr, decimal)
if !ok {
return fmt.Errorf("big.Int.SetString failed on \"%v\"", *ptr)
}
Expand Down Expand Up @@ -104,7 +107,10 @@ func (b MerkleProofMeddler) PreRead(fieldAddr interface{}) (scanTarget interface

// PostRead is called after a Scan operation for fields that have the ProofMeddler
func (b MerkleProofMeddler) PostRead(fieldPtr, scanTarget interface{}) error {
ptr := scanTarget.(*string)
ptr, ok := scanTarget.(*string)
if !ok {
return errors.New("scanTarget is not *string")
}
if ptr == nil {
return errors.New("ProofMeddler.PostRead: nil pointer")
}
Expand Down Expand Up @@ -147,7 +153,10 @@ func (b HashMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, err

// PostRead is called after a Scan operation for fields that have the ProofMeddler
func (b HashMeddler) PostRead(fieldPtr, scanTarget interface{}) error {
ptr := scanTarget.(*string)
ptr, ok := scanTarget.(*string)
if !ok {
return errors.New("scanTarget is not *string")
}
if ptr == nil {
return fmt.Errorf("HashMeddler.PostRead: nil pointer")
}
Expand Down
6 changes: 5 additions & 1 deletion l1infotreesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ func (p *processor) GetLatestInfoUntilBlock(ctx context.Context, blockNum uint64
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() {
if err := tx.Rollback(); err != nil {
log.Warnf("error rolling back tx: %v", err)
}
}()

lpb, err := p.getLastProcessedBlockWithTx(tx)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions lastgersync/evmdownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ func (d *downloader) Download(ctx context.Context, fromBlock uint64, downloadedC

func (d *downloader) getGERsFromIndex(ctx context.Context, fromL1InfoTreeIndex uint32) ([]Event, error) {
lastRoot, err := d.l1InfoTreesync.GetLastL1InfoTreeRoot(ctx)
if err == tree.ErrNotFound {
if errors.Is(err, tree.ErrNotFound) {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("error calling GetLastL1InfoTreeRoot: %v", err)
return nil, fmt.Errorf("error calling GetLastL1InfoTreeRoot: %w", err)
}

gers := []Event{}
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/aggoracle_e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func CommonSetup(t *testing.T) (
require.NoError(t, err)
// Syncer
dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared")
syncer, err := l1infotreesync.New(ctx, dbPathSyncer, gerL1Addr, common.Address{}, 10, etherman.LatestBlock, reorg, l1Client.Client(), time.Millisecond, 0, 100*time.Millisecond, 3)
maxRetries := 3
syncBlockChunkSize := 10
syncer, err := l1infotreesync.New(ctx, dbPathSyncer, gerL1Addr, common.Address{}, syncBlockChunkSize, etherman.LatestBlock, reorg, l1Client.Client(), time.Millisecond, 0, 100*time.Millisecond, maxRetries)

Check failure on line 118 in test/helpers/aggoracle_e2e.go

View workflow job for this annotation

GitHub Actions / test-unit (1.22.4, amd64)

cannot use syncBlockChunkSize (variable of type int) as uint64 value in argument to l1infotreesync.New

Check failure on line 118 in test/helpers/aggoracle_e2e.go

View workflow job for this annotation

GitHub Actions / lint

cannot use syncBlockChunkSize (variable of type int) as uint64 value in argument to l1infotreesync.New (typecheck)

Check failure on line 118 in test/helpers/aggoracle_e2e.go

View workflow job for this annotation

GitHub Actions / lint

cannot use syncBlockChunkSize (variable of type int) as uint64 value in argument to l1infotreesync.New) (typecheck)

Check failure on line 118 in test/helpers/aggoracle_e2e.go

View workflow job for this annotation

GitHub Actions / lint

cannot use syncBlockChunkSize (variable of type int) as uint64 value in argument to l1infotreesync.New) (typecheck)

Check failure on line 118 in test/helpers/aggoracle_e2e.go

View workflow job for this annotation

GitHub Actions / lint

cannot use syncBlockChunkSize (variable of type int) as uint64 value in argument to l1infotreesync.New) (typecheck)
require.NoError(t, err)
go syncer.Start(ctx)

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

import (
"database/sql"
"errors"
"fmt"

"github.com/0xPolygon/cdk/db"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (t *AppendOnlyTree) initCache(tx *db.Tx) error {
siblings := [types.DefaultHeight]common.Hash{}
lastRoot, err := t.getLastRootWithTx(tx)
if err != nil {
if err == ErrNotFound {
if errors.Is(err, ErrNotFound) {
t.lastIndex = -1
t.lastLeftCache = siblings
return nil
Expand All @@ -96,7 +97,7 @@ func (t *AppendOnlyTree) initCache(tx *db.Tx) error {
currentNode, err := t.getRHTNode(tx, currentNodeHash)
if err != nil {
return fmt.Errorf(
"error getting node %s from the RHT at height %d with root %s: %v",
"error getting node %s from the RHT at height %d with root %s: %w",
currentNodeHash.Hex(), h, lastRoot.Hash.Hex(), err,
)
}
Expand Down
8 changes: 5 additions & 3 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 @@ -153,10 +154,11 @@ func generateZeroHashes(height uint8) []common.Hash {
}

func (t *Tree) storeNodes(tx *db.Tx, nodes []types.TreeNode) error {
for _, node := range nodes {
if err := meddler.Insert(tx, t.rhtTable, &node); err != nil {
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 {
if sqliteErr.ExtendedCode == 1555 { // 1555 is the error code for primary key violation
uniqueConstraint := sqlite3.ErrNoExtended(1555) // 1555 is the error code for primary key violation
if sqliteErr.ExtendedCode == uniqueConstraint {
// ignore repeated entries. This is likely to happen due to not
// cleaning RHT when reorg
continue
Expand Down
3 changes: 2 additions & 1 deletion tree/updatabletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tree

import (
"database/sql"
"errors"

"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/tree/types"
Expand All @@ -26,7 +27,7 @@ func (t *UpdatableTree) UpsertLeaf(tx *db.Tx, blockNum, blockPosition uint64, le
var rootHash common.Hash
root, err := t.getLastRootWithTx(tx)
if err != nil {
if err == ErrNotFound {
if errors.Is(err, ErrNotFound) {
rootHash = t.zeroHashes[types.DefaultHeight]
} else {
return err
Expand Down

0 comments on commit 3d5a2ab

Please sign in to comment.