Skip to content

Commit

Permalink
light: fix early erroring (#6905)
Browse files Browse the repository at this point in the history
Co-authored-by: Callum Waters <cmwaters19@gmail.com>
  • Loading branch information
tnasu and cmwaters committed Jan 27, 2022
1 parent f27f755 commit f47fcb2
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions light/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func NewClientFromTrustedStore(
}

// Validate the number of witnesses.
if len(c.witnesses) < 1 {
if len(c.witnesses) == 0 {
return nil, ErrNoWitnesses
}

Expand Down Expand Up @@ -1023,14 +1023,14 @@ func (c *Client) lightBlockFromPrimary(ctx context.Context, height int64) (*type

case provider.ErrNoResponse, provider.ErrLightBlockNotFound, provider.ErrHeightTooHigh:
// we find a new witness to replace the primary
c.logger.Debug("error from light block request from primary, replacing...",
c.logger.Info("error from light block request from primary, replacing...",
"error", err, "height", height, "primary", c.primary)
return c.findNewPrimary(ctx, height, false)

default:
// The light client has most likely received either provider.ErrUnreliableProvider or provider.ErrBadLightBlock
// These errors mean that the light client should drop the primary and try with another provider instead
c.logger.Error("error from light block request from primary, removing...",
c.logger.Info("error from light block request from primary, removing...",
"error", err, "height", height, "primary", c.primary)
return c.findNewPrimary(ctx, height, true)
}
Expand Down Expand Up @@ -1068,7 +1068,7 @@ func (c *Client) findNewPrimary(ctx context.Context, height int64, remove bool)
c.providerMutex.Lock()
defer c.providerMutex.Unlock()

if len(c.witnesses) <= 1 {
if len(c.witnesses) == 0 {
return nil, ErrNoWitnesses
}

Expand Down Expand Up @@ -1139,6 +1139,11 @@ func (c *Client) findNewPrimary(ctx context.Context, height int64, remove bool)
}
}

// remove witnesses marked as bad. Removal is done in descending order
if err := c.removeWitnesses(witnessesToRemove); err != nil {
c.logger.Error("failed to remove witnesses", "err", err, "witnessesToRemove", witnessesToRemove)
}

return nil, lastError
}

Expand All @@ -1151,7 +1156,7 @@ func (c *Client) compareFirstHeaderWithWitnesses(ctx context.Context, h *types.S
c.providerMutex.Lock()
defer c.providerMutex.Unlock()

if len(c.witnesses) < 1 {
if len(c.witnesses) == 0 {
return ErrNoWitnesses
}

Expand Down Expand Up @@ -1193,7 +1198,7 @@ and remove witness. Otherwise, use the different primary`, e.WitnessIndex), "wit

// remove witnesses that have misbehaved
if err := c.removeWitnesses(witnessesToRemove); err != nil {
return err
c.logger.Error("failed to remove witnesses", "err", err, "witnessesToRemove", witnessesToRemove)
}

return nil
Expand Down

0 comments on commit f47fcb2

Please sign in to comment.