From f235c06a82430ca796614115e22a6d4ebdf63173 Mon Sep 17 00:00:00 2001 From: Mikelle Date: Wed, 1 May 2024 01:42:10 +0200 Subject: [PATCH] fix: move blocksPerWindow set up to separated func --- p2p/pkg/depositmanager/deposit.go | 39 +++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/p2p/pkg/depositmanager/deposit.go b/p2p/pkg/depositmanager/deposit.go index 0623a63ad..6d53f69e7 100644 --- a/p2p/pkg/depositmanager/deposit.go +++ b/p2p/pkg/depositmanager/deposit.go @@ -126,7 +126,13 @@ func (dm *DepositManager) Start(ctx context.Context) <-chan struct{} { case <-egCtx.Done(): return nil case bidderReg := <-dm.bidderRegs: - effectiveStake := new(big.Int).Div(new(big.Int).Set(bidderReg.DepositedAmount), new(big.Int).SetUint64(dm.blocksPerWindow.Load())) + blocksPerWindow, err := dm.checkAndSetBlocksPerWindow() + if err != nil { + dm.logger.Error("failed to get blocks per window", "error", err) + return err + } + + effectiveStake := new(big.Int).Div(new(big.Int).Set(bidderReg.DepositedAmount), new(big.Int).SetUint64(blocksPerWindow)) if err := dm.store.SetBalance(bidderReg.Bidder, bidderReg.WindowNumber, effectiveStake); err != nil { return err } @@ -147,17 +153,12 @@ func (dm *DepositManager) Start(ctx context.Context) <-chan struct{} { } func (dm *DepositManager) CheckAndDeductDeposit(ctx context.Context, address common.Address, bidAmountStr string, blockNumber int64) (*big.Int, error) { - if dm.blocksPerWindow.Load() == 0 { - blocksPerWindow, err := dm.blockTracker.GetBlocksPerWindow() - if err != nil { - dm.logger.Error("getting blocks per window", "error", err) - return nil, status.Errorf(codes.Internal, "failed to get blocks per window: %v", err) - } - dm.blocksPerWindow.Store(blocksPerWindow.Uint64()) + blocksPerWindow, err := dm.checkAndSetBlocksPerWindow() + if err != nil { + dm.logger.Error("failed to get blocks per window", "error", err) + return nil, status.Error(codes.Internal, err.Error()) } - blocksPerWindow := dm.blocksPerWindow.Load() - bidAmount, ok := new(big.Int).SetString(bidAmountStr, 10) if !ok { dm.logger.Error("parsing bid amount", "amount", bidAmountStr) @@ -223,3 +224,21 @@ func (dm *DepositManager) CheckAndDeductDeposit(ctx context.Context, address com func (dm *DepositManager) RefundDeposit(address common.Address, deductedAmount *big.Int, blockNumber int64) error { return dm.store.RefundBalanceForBlock(address, deductedAmount, blockNumber) } + +func (dm *DepositManager) checkAndSetBlocksPerWindow() (uint64, error) { + bpwCache := dm.blocksPerWindow.Load() + + if bpwCache != 0 { + return bpwCache, nil + } + + blocksPerWindow, err := dm.blockTracker.GetBlocksPerWindow() + if err != nil { + return 0, fmt.Errorf("failed to get blocks per window: %w", err) + } + + blocksPerWindowUint64 := blocksPerWindow.Uint64() + dm.blocksPerWindow.Store(blocksPerWindowUint64) + + return blocksPerWindowUint64, nil +}