Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 committed Nov 17, 2024
1 parent 86f12c9 commit 1d4a7d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
14 changes: 6 additions & 8 deletions cl/phase1/forkchoice/fork_graph/fork_graph_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/golang/snappy"
"github.com/spf13/afero"
"golang.org/x/sync/semaphore"

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/log/v3"
Expand Down Expand Up @@ -133,7 +132,7 @@ type forkGraphDisk struct {
rcfg beacon_router_configuration.RouterConfiguration
emitter *beaconevents.EventEmitter

busyWritingToDisk *semaphore.Weighted
stateDumpLock sync.Mutex
}

// Initialize fork graph with a new state
Expand All @@ -155,12 +154,11 @@ func NewForkGraphDisk(anchorState *state.CachingBeaconState, aferoFs afero.Fs, r
// current state data
currentState: anchorState,
// configuration
beaconCfg: anchorState.BeaconConfig(),
genesisTime: anchorState.GenesisTime(),
anchorSlot: anchorState.Slot(),
rcfg: rcfg,
emitter: emitter,
busyWritingToDisk: semaphore.NewWeighted(1),
beaconCfg: anchorState.BeaconConfig(),
genesisTime: anchorState.GenesisTime(),
anchorSlot: anchorState.Slot(),
rcfg: rcfg,
emitter: emitter,
}
f.lowestAvailableBlock.Store(anchorState.Slot())
f.headers.Store(libcommon.Hash(anchorRoot), &anchorHeader)
Expand Down
13 changes: 4 additions & 9 deletions cl/phase1/forkchoice/fork_graph/fork_graph_disk_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package fork_graph

import (
"context"
"encoding/binary"
"fmt"
"io"
Expand All @@ -41,10 +40,8 @@ func getBeaconStateCacheFilename(blockRoot libcommon.Hash) string {

func (f *forkGraphDisk) readBeaconStateFromDisk(blockRoot libcommon.Hash) (bs *state.CachingBeaconState, err error) {
var file afero.File
if err := f.busyWritingToDisk.Acquire(context.TODO(), 1); err != nil {
return nil, err
}
defer f.busyWritingToDisk.Release(1)
f.stateDumpLock.Lock()
defer f.stateDumpLock.Unlock()

file, err = f.fs.Open(getBeaconStateFilename(blockRoot))
if err != nil {
Expand Down Expand Up @@ -103,17 +100,15 @@ func (f *forkGraphDisk) DumpBeaconStateOnDisk(blockRoot libcommon.Hash, bs *stat
if !forced && bs.Slot()%dumpSlotFrequency != 0 {
return
}
if err := f.busyWritingToDisk.Acquire(context.TODO(), 1); err != nil {
return err
}
f.stateDumpLock.Lock()
// Truncate and then grow the buffer to the size of the state.
f.sszBuffer, err = bs.EncodeSSZ(f.sszBuffer[:0])
if err != nil {
return
}

go func() {
defer f.busyWritingToDisk.Release(1)
defer f.stateDumpLock.Unlock()

var dumpedFile afero.File
dumpedFile, err = f.fs.OpenFile(getBeaconStateFilename(blockRoot), os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0o755)
Expand Down

0 comments on commit 1d4a7d9

Please sign in to comment.