From d060e40eda3622870dc338f1f6fc83a3d8224264 Mon Sep 17 00:00:00 2001 From: Potuz Date: Mon, 6 Mar 2023 11:01:43 -0300 Subject: [PATCH 1/5] Use Epoch boundary cache to retrieve balances --- beacon-chain/state/stategen/getter.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/beacon-chain/state/stategen/getter.go b/beacon-chain/state/stategen/getter.go index f04f3c3cc485..f299b9861473 100644 --- a/beacon-chain/state/stategen/getter.go +++ b/beacon-chain/state/stategen/getter.go @@ -68,10 +68,19 @@ func (s *State) StateByRoot(ctx context.Context, blockRoot [32]byte) (state.Beac // ActiveNonSlashedBalancesByRoot retrieves the effective balances of all active and non-slashed validators at the // state with a given root func (s *State) ActiveNonSlashedBalancesByRoot(ctx context.Context, blockRoot [32]byte) ([]uint64, error) { - st, err := s.StateByRoot(ctx, blockRoot) + var st state.BeaconState + cachedInfo, ok, err := s.epochBoundaryStateCache.getByBlockRoot(blockRoot) if err != nil { return nil, err } + if ok { + st = cachedInfo.state + } else { + st, err = s.StateByRoot(ctx, blockRoot) + if err != nil { + return nil, err + } + } if st == nil || st.IsNil() { return nil, errNilState } From 01aa9d45bf7ca6a04c900e1860b8c32d539cdf0c Mon Sep 17 00:00:00 2001 From: Potuz Date: Mon, 6 Mar 2023 11:36:51 -0300 Subject: [PATCH 2/5] save boundary states before inserting to forkchoice --- beacon-chain/blockchain/process_block.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 6adfe257283c..52cec5cb6bd1 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -447,6 +447,12 @@ func (s *Service) onBlockBatch(ctx context.Context, blks []interfaces.ReadOnlySi } } } + // Save boundary states that will be useful for forkchoice + for r, st := range boundaries { + if err := s.cfg.StateGen.SaveState(ctx, r, st); err != nil { + return err + } + } // Insert all nodes but the last one to forkchoice if err := s.cfg.ForkChoiceStore.InsertChain(ctx, pendingNodes); err != nil { return errors.Wrap(err, "could not insert batch to forkchoice") @@ -463,11 +469,6 @@ func (s *Service) onBlockBatch(ctx context.Context, blks []interfaces.ReadOnlySi } } - for r, st := range boundaries { - if err := s.cfg.StateGen.SaveState(ctx, r, st); err != nil { - return err - } - } // Also saves the last post state which to be used as pre state for the next batch. lastB := blks[len(blks)-1] if err := s.cfg.StateGen.SaveState(ctx, lastBR, preState); err != nil { From cf7e454d18e1718f8b7df5c577a40ae0fad56157 Mon Sep 17 00:00:00 2001 From: Potuz Date: Mon, 6 Mar 2023 12:51:31 -0300 Subject: [PATCH 3/5] move up last block save --- beacon-chain/blockchain/process_block.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 52cec5cb6bd1..a44554b8e1ee 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -453,15 +453,15 @@ func (s *Service) onBlockBatch(ctx context.Context, blks []interfaces.ReadOnlySi return err } } - // Insert all nodes but the last one to forkchoice - if err := s.cfg.ForkChoiceStore.InsertChain(ctx, pendingNodes); err != nil { - return errors.Wrap(err, "could not insert batch to forkchoice") - } // Insert the last block to forkchoice lastBR := blockRoots[len(blks)-1] if err := s.cfg.ForkChoiceStore.InsertNode(ctx, preState, lastBR); err != nil { return errors.Wrap(err, "could not insert last block in batch to forkchoice") } + // Insert all nodes but the last one to forkchoice + if err := s.cfg.ForkChoiceStore.InsertChain(ctx, pendingNodes); err != nil { + return errors.Wrap(err, "could not insert batch to forkchoice") + } // Set their optimistic status if isValidPayload { if err := s.cfg.ForkChoiceStore.SetOptimisticToValid(ctx, lastBR); err != nil { From aa3d67573283e4b51543040245f6f837544c33f6 Mon Sep 17 00:00:00 2001 From: Potuz Date: Mon, 6 Mar 2023 12:56:39 -0300 Subject: [PATCH 4/5] remove boundary checks on balances --- beacon-chain/state/stategen/getter.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/beacon-chain/state/stategen/getter.go b/beacon-chain/state/stategen/getter.go index f299b9861473..f04f3c3cc485 100644 --- a/beacon-chain/state/stategen/getter.go +++ b/beacon-chain/state/stategen/getter.go @@ -68,19 +68,10 @@ func (s *State) StateByRoot(ctx context.Context, blockRoot [32]byte) (state.Beac // ActiveNonSlashedBalancesByRoot retrieves the effective balances of all active and non-slashed validators at the // state with a given root func (s *State) ActiveNonSlashedBalancesByRoot(ctx context.Context, blockRoot [32]byte) ([]uint64, error) { - var st state.BeaconState - cachedInfo, ok, err := s.epochBoundaryStateCache.getByBlockRoot(blockRoot) + st, err := s.StateByRoot(ctx, blockRoot) if err != nil { return nil, err } - if ok { - st = cachedInfo.state - } else { - st, err = s.StateByRoot(ctx, blockRoot) - if err != nil { - return nil, err - } - } if st == nil || st.IsNil() { return nil, errNilState } From 877d591eb5ed83b00d5a5a67b2e8cc599bcf83e6 Mon Sep 17 00:00:00 2001 From: Potuz Date: Mon, 6 Mar 2023 13:10:38 -0300 Subject: [PATCH 5/5] fix ordering --- beacon-chain/blockchain/process_block.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index a44554b8e1ee..fd1ac2d00eff 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -453,27 +453,26 @@ func (s *Service) onBlockBatch(ctx context.Context, blks []interfaces.ReadOnlySi return err } } - // Insert the last block to forkchoice + // Also saves the last post state which to be used as pre state for the next batch. lastBR := blockRoots[len(blks)-1] - if err := s.cfg.ForkChoiceStore.InsertNode(ctx, preState, lastBR); err != nil { - return errors.Wrap(err, "could not insert last block in batch to forkchoice") + if err := s.cfg.StateGen.SaveState(ctx, lastBR, preState); err != nil { + return err } // Insert all nodes but the last one to forkchoice if err := s.cfg.ForkChoiceStore.InsertChain(ctx, pendingNodes); err != nil { return errors.Wrap(err, "could not insert batch to forkchoice") } + // Insert the last block to forkchoice + if err := s.cfg.ForkChoiceStore.InsertNode(ctx, preState, lastBR); err != nil { + return errors.Wrap(err, "could not insert last block in batch to forkchoice") + } // Set their optimistic status if isValidPayload { if err := s.cfg.ForkChoiceStore.SetOptimisticToValid(ctx, lastBR); err != nil { return errors.Wrap(err, "could not set optimistic block to valid") } } - - // Also saves the last post state which to be used as pre state for the next batch. lastB := blks[len(blks)-1] - if err := s.cfg.StateGen.SaveState(ctx, lastBR, preState); err != nil { - return err - } arg := ¬ifyForkchoiceUpdateArg{ headState: preState, headRoot: lastBR,