mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-24 20:37:17 +00:00
Add epoch boundary root map (#4993)
* Add to struct * Add implementations * Tests
This commit is contained in:
parent
ba6b8c9321
commit
699e7efc61
@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"epoch_boundary_root.go",
|
||||
"replay.go",
|
||||
"service.go",
|
||||
],
|
||||
@ -23,7 +24,10 @@ go_library(
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["replay_test.go"],
|
||||
srcs = [
|
||||
"epoch_boundary_root_test.go",
|
||||
"replay_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//beacon-chain/core/blocks:go_default_library",
|
||||
|
24
beacon-chain/state/stategen/epoch_boundary_root.go
Normal file
24
beacon-chain/state/stategen/epoch_boundary_root.go
Normal file
@ -0,0 +1,24 @@
|
||||
package stategen
|
||||
|
||||
// This sets an epoch boundary slot to root mapping.
|
||||
// The slot is the key and the root is the value.
|
||||
func (s *State) setEpochBoundaryRoot(slot uint64, root [32]byte) {
|
||||
s.epochBoundaryLock.Lock()
|
||||
defer s.epochBoundaryLock.Unlock()
|
||||
s.epochBoundarySlotToRoot[slot] = root
|
||||
}
|
||||
|
||||
// This reads epoch boundary slot to root mapping.
|
||||
func (s *State) epochBoundaryRoot(slot uint64) ([32]byte, bool) {
|
||||
s.epochBoundaryLock.RLock()
|
||||
defer s.epochBoundaryLock.RUnlock()
|
||||
r, ok := s.epochBoundarySlotToRoot[slot]
|
||||
return r, ok
|
||||
}
|
||||
|
||||
// This deletes an entry of epoch boundary slot to root mapping.
|
||||
func (s *State) deleteEpochBoundaryRoot(slot uint64) {
|
||||
s.epochBoundaryLock.Lock()
|
||||
defer s.epochBoundaryLock.Unlock()
|
||||
delete(s.epochBoundarySlotToRoot, slot)
|
||||
}
|
32
beacon-chain/state/stategen/epoch_boundary_root_test.go
Normal file
32
beacon-chain/state/stategen/epoch_boundary_root_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package stategen
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEpochBoundaryRoot_CanSetGetDelete(t *testing.T) {
|
||||
s := &State{
|
||||
epochBoundarySlotToRoot: make(map[uint64][32]byte),
|
||||
}
|
||||
slot := uint64(100)
|
||||
r := [32]byte{'A'}
|
||||
|
||||
_, exists := s.epochBoundaryRoot(slot)
|
||||
if exists {
|
||||
t.Fatal("should not be cached")
|
||||
}
|
||||
|
||||
s.setEpochBoundaryRoot(slot, r)
|
||||
|
||||
rReceived, exists := s.epochBoundaryRoot(slot)
|
||||
if !exists {
|
||||
t.Fatal("should be cached")
|
||||
}
|
||||
if rReceived != r {
|
||||
t.Error("did not cache right value")
|
||||
}
|
||||
|
||||
s.deleteEpochBoundaryRoot(100)
|
||||
_, exists = s.epochBoundaryRoot(slot)
|
||||
if exists {
|
||||
t.Fatal("should not be cached")
|
||||
}
|
||||
}
|
@ -1,18 +1,23 @@
|
||||
package stategen
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
||||
)
|
||||
|
||||
// State represents a management object that handles the internal
|
||||
// logic of maintaining both hot and cold states in DB.
|
||||
type State struct {
|
||||
beaconDB db.NoHeadAccessDatabase
|
||||
beaconDB db.NoHeadAccessDatabase
|
||||
epochBoundarySlotToRoot map[uint64][32]byte
|
||||
epochBoundaryLock sync.RWMutex
|
||||
}
|
||||
|
||||
// New returns a new state management object.
|
||||
func New(db db.NoHeadAccessDatabase) *State {
|
||||
return &State{
|
||||
beaconDB: db,
|
||||
beaconDB: db,
|
||||
epochBoundarySlotToRoot: make(map[uint64][32]byte),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user