Skip to content

Commit

Permalink
Caplin: Remove cache files completely (#13340)
Browse files Browse the repository at this point in the history
* It was a pre-optimization and it is now not needed
* simpler code
* less headaches for concurrency
  • Loading branch information
Giulio2002 authored Jan 8, 2025
1 parent e3cb56e commit cc41076
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
17 changes: 11 additions & 6 deletions cl/beacon/handler/block_production.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,16 @@ func (a *ApiHandler) GetEthV3ValidatorBlock(
)
}

// make a simple copy to the current head state
baseState, err := a.forkchoiceStore.GetStateAtBlockRoot(
baseBlockRoot,
true,
) // we start the block production from this state
var baseState *state.CachingBeaconState
if err := a.syncedData.ViewHeadState(func(headState *state.CachingBeaconState) error {
baseState, err = headState.Copy()
if err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}

if err != nil {
return nil, err
Expand Down Expand Up @@ -1177,7 +1182,7 @@ func (a *ApiHandler) storeBlockAndBlobs(
return err
}

if err := a.forkchoiceStore.OnBlock(ctx, block, true, false, false); err != nil {
if err := a.forkchoiceStore.OnBlock(ctx, block, true, true, false); err != nil {
return err
}
finalizedBlockRoot := a.forkchoiceStore.FinalizedCheckpoint().Root
Expand Down
6 changes: 6 additions & 0 deletions cl/cltypes/solid/hash_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func (h *hashList) CopyTo(t IterableSSZ[libcommon.Hash]) {
tu.MerkleTree = &merkle_tree.MerkleTree{}
}
h.MerkleTree.CopyInto(tu.MerkleTree)
// make the leaf function on the new buffer
tu.MerkleTree.SetComputeLeafFn(func(idx int, out []byte) {
copy(out, tu.u[idx*length.Hash:])
})
} else {
tu.MerkleTree = nil
}
copy(tu.u, h.u)
}
Expand Down
5 changes: 5 additions & 0 deletions cl/cltypes/solid/uint64slice_byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (arr *byteBasedUint64Slice) CopyTo(target *byteBasedUint64Slice) {
target.MerkleTree = &merkle_tree.MerkleTree{}
}
arr.MerkleTree.CopyInto(target.MerkleTree)
target.SetComputeLeafFn(func(idx int, out []byte) {
copy(out, target.u[idx*length.Hash:])
})
} else {
target.MerkleTree = nil
}

target.u = target.u[:len(arr.u)]
Expand Down
5 changes: 5 additions & 0 deletions cl/cltypes/solid/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func (v *ValidatorSet) CopyTo(t *ValidatorSet) {
t.MerkleTree = &merkle_tree.MerkleTree{}
}
v.MerkleTree.CopyInto(t.MerkleTree)
t.MerkleTree.SetComputeLeafFn(func(idx int, out []byte) {
copy(out, t.buffer[idx*validatorSize:])
})
} else {
t.MerkleTree = nil
}
// skip copying (unsupported for phase0)
t.phase0Data = make([]Phase0Data, t.l)
Expand Down
6 changes: 5 additions & 1 deletion cl/merkle_tree/merkle_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (m *MerkleTree) Initialize(leavesCount, maxTreeCacheDepth int, computeLeaf
m.dirtyLeaves = make([]atomic.Bool, leavesCount)
}

func (m *MerkleTree) SetComputeLeafFn(computeLeaf func(idx int, out []byte)) {
m.computeLeaf = computeLeaf
}

func (m *MerkleTree) MarkLeafAsDirty(idx int) {
m.mu.RLock()
defer m.mu.RUnlock()
Expand Down Expand Up @@ -207,7 +211,7 @@ func (m *MerkleTree) CopyInto(other *MerkleTree) {
m.mu.RLock()
defer m.mu.RUnlock()
defer other.mu.Unlock()
other.computeLeaf = m.computeLeaf
//other.computeLeaf = m.computeLeaf
if len(other.layers) > len(m.layers) {
// reset the internal layers
for i := len(m.layers); i < len(other.layers); i++ {
Expand Down
2 changes: 1 addition & 1 deletion cl/phase1/core/state/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (b *CachingBeaconState) initCaches() error {
func (b *CachingBeaconState) InitBeaconState() error {
b.totalActiveBalanceCache = nil
b._refreshActiveBalancesIfNeeded()

b.previousStateRoot = common.Hash{}
b.publicKeyIndicies = make(map[[48]byte]uint64)
b.ForEachValidator(func(validator solid.Validator, i, total int) bool {
b.publicKeyIndicies[validator.PublicKey()] = uint64(i)
Expand Down

0 comments on commit cc41076

Please sign in to comment.