Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Dec 16, 2024
1 parent 47faaff commit 68a4e3a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
8 changes: 4 additions & 4 deletions internal/bus/chainsubscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (s *chainSubscriber) applyChainUpdate(tx sql.ChainUpdateTx, cau chain.Apply
cau.ForEachFileContractElement(func(fce types.FileContractElement, created bool, rev *types.FileContractElement, resolved, valid bool) {
if err != nil {
return
} else if known, lookupErr := tx.IsKnownContract(fce.ID); err != nil {
} else if known, lookupErr := tx.IsKnownContract(fce.ID); lookupErr != nil {
err = lookupErr
return
} else if !known {
Expand All @@ -217,7 +217,7 @@ func (s *chainSubscriber) applyChainUpdate(tx sql.ChainUpdateTx, cau chain.Apply
cau.ForEachV2FileContractElement(func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType) {
if err != nil {
return
} else if known, lookupErr := tx.IsKnownContract(fce.ID); err != nil {
} else if known, lookupErr := tx.IsKnownContract(fce.ID); lookupErr != nil {
err = lookupErr
return
} else if !known {
Expand Down Expand Up @@ -268,7 +268,7 @@ func (s *chainSubscriber) revertChainUpdate(tx sql.ChainUpdateTx, cru chain.Reve
cru.ForEachFileContractElement(func(fce types.FileContractElement, created bool, rev *types.FileContractElement, resolved, _ bool) {
if err != nil {
return
} else if known, lookupErr := tx.IsKnownContract(fce.ID); err != nil {
} else if known, lookupErr := tx.IsKnownContract(fce.ID); lookupErr != nil {
err = lookupErr
return
} else if !known {
Expand All @@ -285,7 +285,7 @@ func (s *chainSubscriber) revertChainUpdate(tx sql.ChainUpdateTx, cru chain.Reve
cru.ForEachV2FileContractElement(func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType) {
if err != nil {
return
} else if known, lookupErr := tx.IsKnownContract(fce.ID); err != nil {
} else if known, lookupErr := tx.IsKnownContract(fce.ID); lookupErr != nil {
err = lookupErr
return
} else if !known {
Expand Down
9 changes: 4 additions & 5 deletions stores/sql/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,9 @@ func FileContractElement(ctx context.Context, tx sql.Tx, fcid types.FileContract
}

func IsKnownContract(ctx context.Context, tx sql.Tx, fcid types.FileContractID) (known bool, _ error) {
err := tx.QueryRow(ctx, "SELECT 1 FROM contracts WHERE fcid = ?", FileContractID(fcid)).Scan(&known)
if errors.Is(err, dsql.ErrNoRows) {
return false, nil
} else if err != nil {
err := tx.QueryRow(ctx, "SELECT EXISTS (SELECT 1 FROM contracts WHERE fcid = ?)", FileContractID(fcid)).
Scan(&known)
if err != nil {
return false, err
}
return known, nil
Expand All @@ -221,7 +220,7 @@ func RecordContractRenewal(ctx context.Context, tx sql.Tx, oldFCID, newFCID type
}
_, err = tx.Exec(ctx, "UPDATE contracts SET contracts.renewed_from = ? WHERE contracts.fcid = ?", FileContractID(oldFCID), FileContractID(newFCID))
if err != nil {
return fmt.Errorf("failed to update renewed_to of new contract: %w", err)
return fmt.Errorf("failed to update renewed_from of new contract: %w", err)
}
return nil
}
Expand Down
4 changes: 0 additions & 4 deletions stores/sql/mysql/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ func (c chainUpdateTx) FileContractElement(fcid types.FileContractID) (types.V2F
}

func (c chainUpdateTx) IsKnownContract(fcid types.FileContractID) (bool, error) {
if c.known == nil {
c.known = make(map[types.FileContractID]bool)
}

if relevant, ok := c.known[fcid]; ok {
return relevant, nil
}
Expand Down
7 changes: 4 additions & 3 deletions stores/sql/mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,10 @@ func (tx *MainDatabaseTx) Peers(ctx context.Context) ([]syncer.PeerInfo, error)

func (tx *MainDatabaseTx) ProcessChainUpdate(ctx context.Context, fn func(ssql.ChainUpdateTx) error) error {
return fn(&chainUpdateTx{
ctx: ctx,
tx: tx,
l: tx.log.Named("ProcessChainUpdate"),
ctx: ctx,
known: make(map[types.FileContractID]bool),
tx: tx,
l: tx.log.Named("ProcessChainUpdate"),
})
}

Expand Down
4 changes: 0 additions & 4 deletions stores/sql/sqlite/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,6 @@ func (c chainUpdateTx) FileContractElement(fcid types.FileContractID) (types.V2F
}

func (c chainUpdateTx) IsKnownContract(fcid types.FileContractID) (bool, error) {
if c.known == nil {
c.known = make(map[types.FileContractID]bool)
}

if relevant, ok := c.known[fcid]; ok {
return relevant, nil
}
Expand Down
7 changes: 4 additions & 3 deletions stores/sql/sqlite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,10 @@ func (tx *MainDatabaseTx) Peers(ctx context.Context) ([]syncer.PeerInfo, error)

func (tx *MainDatabaseTx) ProcessChainUpdate(ctx context.Context, fn func(ssql.ChainUpdateTx) error) (err error) {
return fn(&chainUpdateTx{
ctx: ctx,
tx: tx,
l: tx.log.Named("ProcessChainUpdate"),
ctx: ctx,
known: make(map[types.FileContractID]bool),
tx: tx,
l: tx.log.Named("ProcessChainUpdate"),
})
}

Expand Down

0 comments on commit 68a4e3a

Please sign in to comment.