prevent direct access to backend state struct

This commit is contained in:
Felipe Andrade 2023-05-09 02:57:36 -07:00
parent 6302eddef8
commit 32064105c8

@ -203,23 +203,23 @@ func NewConsensusPoller(bg *BackendGroup, opts ...ConsensusOpt) *ConsensusPoller
// UpdateBackend refreshes the consensus state of a single backend
func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) {
bs := cp.backendState[be]
if time.Now().Before(bs.bannedUntil) {
log.Debug("skipping backend banned", "backend", be.Name, "bannedUntil", bs.bannedUntil)
_, _, _, _, bannedUntil := cp.getBackendState(be)
if time.Now().Before(bannedUntil) {
log.Debug("skipping backend banned", "backend", be.Name, "bannedUntil", bannedUntil)
return
}
// if backend it not online or not in a health state we'll only resume checkin it after ban
if !be.Online() || !be.IsHealthy() {
log.Warn("backend banned - not online or not healthy", "backend", be.Name, "bannedUntil", bs.bannedUntil)
bs.bannedUntil = time.Now().Add(cp.banPeriod)
log.Warn("backend banned - not online or not healthy", "backend", be.Name, "bannedUntil", bannedUntil)
bannedUntil = time.Now().Add(cp.banPeriod)
}
// if backend it not in sync we'll check again after ban
inSync, err := cp.isInSync(ctx, be)
if err != nil || !inSync {
log.Warn("backend banned - not in sync", "backend", be.Name, "bannedUntil", bs.bannedUntil)
bs.bannedUntil = time.Now().Add(cp.banPeriod)
log.Warn("backend banned - not in sync", "backend", be.Name, "bannedUntil", bannedUntil)
bannedUntil = time.Now().Add(cp.banPeriod)
}
// if backend exhausted rate limit we'll skip it for now
@ -246,7 +246,11 @@ func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) {
if changed {
RecordBackendLatestBlock(be, latestBlockNumber)
log.Debug("backend state updated", "name", be.Name, "state", bs)
log.Debug("backend state updated",
"name", be.Name,
"peerCount", peerCount,
"latestBlockNumber", latestBlockNumber,
"latestBlockHash", latestBlockHash)
}
}
@ -258,7 +262,7 @@ func (cp *ConsensusPoller) UpdateBackendGroupConsensus(ctx context.Context) {
currentConsensusBlockNumber := cp.GetConsensusBlockNumber()
for _, be := range cp.backendGroup.Backends {
peerCount, backendLatestBlockNumber, backendLatestBlockHash, lastUpdate := cp.getBackendState(be)
peerCount, backendLatestBlockNumber, backendLatestBlockHash, lastUpdate, _ := cp.getBackendState(be)
if !be.skipPeerCountCheck && peerCount < cp.minPeerCount {
continue
@ -306,10 +310,10 @@ func (cp *ConsensusPoller) UpdateBackendGroupConsensus(ctx context.Context) {
- with minimum peer count
- updated recently
*/
bs := cp.backendState[be]
notUpdated := bs.lastUpdate.Add(cp.maxUpdateThreshold).Before(time.Now())
isBanned := time.Now().Before(bs.bannedUntil)
notEnoughPeers := !be.skipPeerCountCheck && bs.peerCount < cp.minPeerCount
peerCount, _, _, lastUpdate, bannedUntil := cp.getBackendState(be)
notUpdated := lastUpdate.Add(cp.maxUpdateThreshold).Before(time.Now())
isBanned := time.Now().Before(bannedUntil)
notEnoughPeers := !be.skipPeerCountCheck && peerCount < cp.minPeerCount
if !be.IsHealthy() || be.IsRateLimited() || !be.Online() || notUpdated || isBanned || notEnoughPeers {
filteredBackendsNames = append(filteredBackendsNames, be.Name)
continue
@ -432,13 +436,14 @@ func (cp *ConsensusPoller) isInSync(ctx context.Context, be *Backend) (result bo
return res, nil
}
func (cp *ConsensusPoller) getBackendState(be *Backend) (peerCount uint64, blockNumber hexutil.Uint64, blockHash string, lastUpdate time.Time) {
func (cp *ConsensusPoller) getBackendState(be *Backend) (peerCount uint64, blockNumber hexutil.Uint64, blockHash string, lastUpdate time.Time, bannedUntil time.Time) {
bs := cp.backendState[be]
bs.backendStateMux.Lock()
peerCount = bs.peerCount
blockNumber = bs.latestBlockNumber
blockHash = bs.latestBlockHash
lastUpdate = bs.lastUpdate
bannedUntil = bs.bannedUntil
bs.backendStateMux.Unlock()
return
}