Add epoch boundary root map (#4993)

* Add to struct

* Add implementations

* Tests
This commit is contained in:
terence tsao 2020-03-03 20:07:34 +01:00 committed by GitHub
parent ba6b8c9321
commit 699e7efc61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 3 deletions

View File

@ -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",

View 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)
}

View 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")
}
}

View File

@ -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),
}
}