Skip to content

Commit

Permalink
Check HeadState First (#4830)
Browse files Browse the repository at this point in the history
* easy optimization
* Merge refs/heads/master into easyOptimization
* cache miss
  • Loading branch information
nisdas committed Feb 11, 2020
1 parent 8c90e38 commit 601f93a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
19 changes: 19 additions & 0 deletions beacon-chain/blockchain/process_attestation_helpers.go
@@ -1,6 +1,7 @@
package blockchain

import (
"bytes"
"context"
"fmt"

Expand Down Expand Up @@ -28,6 +29,24 @@ func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (*sta
return cachedState, nil
}

headRoot, err := s.HeadRoot(ctx)
if err != nil {
return nil, errors.Wrapf(err, "could not get head root")
}
if bytes.Equal(headRoot, c.Root) {
st, err := s.HeadState(ctx)
if err != nil {
return nil, errors.Wrapf(err, "could not get head state")
}
if err := s.checkpointState.AddCheckpointState(&cache.CheckpointState{
Checkpoint: c,
State: st.Copy(),
}); err != nil {
return nil, errors.Wrap(err, "could not saved checkpoint state to cache")
}
return st, nil
}

baseState, err := s.beaconDB.State(ctx, bytesutil.ToBytes32(c.Root))
if err != nil {
return nil, errors.Wrapf(err, "could not get pre state for slot %d", helpers.StartSlot(c.Epoch))
Expand Down
7 changes: 7 additions & 0 deletions beacon-chain/blockchain/process_block_helpers.go
Expand Up @@ -73,6 +73,13 @@ func (s *Service) verifyBlkPreState(ctx context.Context, b *ethpb.BeaconBlock) (
}
return preState.Copy(), nil
}
headRoot, err := s.HeadRoot(ctx)
if err != nil {
return nil, errors.Wrapf(err, "could not get head root")
}
if bytes.Equal(headRoot, b.ParentRoot) {
return s.HeadState(ctx)
}

preState, err := s.beaconDB.State(ctx, bytesutil.ToBytes32(b.ParentRoot))
if err != nil {
Expand Down

0 comments on commit 601f93a

Please sign in to comment.