-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
beacon-chain/cache: Convert tests to cache_test blackbox testing (#13920
) * beacon-chain/cache: convert to blackbox tests (package cache_test) * Move balanceCacheKey to its own file to satisify go fuzz build (cherry picked from commit 3233e64)
- Loading branch information
1 parent
f079db6
commit 5ddd5cb
Showing
7 changed files
with
89 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cache | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state" | ||
"github.com/prysmaticlabs/prysm/v5/config/params" | ||
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" | ||
) | ||
|
||
// Given input state `st`, balance key is constructed as: | ||
// (block_root in `st` at epoch_start_slot - 1) + current_epoch + validator_count | ||
func balanceCacheKey(st state.ReadOnlyBeaconState) (string, error) { | ||
slotsPerEpoch := params.BeaconConfig().SlotsPerEpoch | ||
currentEpoch := st.Slot().DivSlot(slotsPerEpoch) | ||
epochStartSlot, err := slotsPerEpoch.SafeMul(uint64(currentEpoch)) | ||
if err != nil { | ||
// impossible condition due to early division | ||
return "", fmt.Errorf("start slot calculation overflows: %w", err) | ||
} | ||
prevSlot := primitives.Slot(0) | ||
if epochStartSlot > 1 { | ||
prevSlot = epochStartSlot - 1 | ||
} | ||
r, err := st.BlockRootAtIndex(uint64(prevSlot % params.BeaconConfig().SlotsPerHistoricalRoot)) | ||
if err != nil { | ||
// impossible condition because index is always constrained within state | ||
return "", err | ||
} | ||
|
||
// Mix in current epoch | ||
b := make([]byte, 8) | ||
binary.LittleEndian.PutUint64(b, uint64(currentEpoch)) | ||
key := append(r, b...) | ||
|
||
// Mix in validator count | ||
b = make([]byte, 8) | ||
binary.LittleEndian.PutUint64(b, uint64(st.NumValidators())) | ||
key = append(key, b...) | ||
|
||
return string(key), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cache | ||
|
||
import ( | ||
lru "github.com/hashicorp/golang-lru" | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state" | ||
) | ||
|
||
func BalanceCacheKey(st state.ReadOnlyBeaconState) (string, error) { | ||
return balanceCacheKey(st) | ||
} | ||
|
||
func MaxCheckpointStateSize() int { | ||
return maxCheckpointStateSize | ||
} | ||
|
||
func (c *CheckpointStateCache) Cache() *lru.Cache { | ||
return c.cache | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters