2023-11-21 18:44:38 +00:00
package storage
import (
"path"
2023-12-06 20:20:34 +00:00
"github.com/pkg/errors"
2023-11-21 18:44:38 +00:00
"github.com/prysmaticlabs/prysm/v4/beacon-chain/db/filesystem"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/node"
"github.com/prysmaticlabs/prysm/v4/cmd"
2023-12-06 20:20:34 +00:00
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
2023-11-21 18:44:38 +00:00
"github.com/urfave/cli/v2"
)
var (
2023-12-06 20:20:34 +00:00
// BlobStoragePathFlag defines a flag to start the beacon chain from a give genesis state file.
BlobStoragePathFlag = & cli . PathFlag {
2023-11-21 18:44:38 +00:00
Name : "blob-path" ,
Usage : "Location for blob storage. Default location will be a 'blobs' directory next to the beacon db." ,
}
2023-12-06 20:20:34 +00:00
BlobRetentionEpochFlag = & cli . Uint64Flag {
Name : "blob-retention-epochs" ,
Usage : "Override the default blob retention period (measured in epochs). The node will exit with an error at startup if the value is less than the default of 4096 epochs." ,
2023-12-16 11:37:44 +00:00
Value : uint64 ( params . BeaconConfig ( ) . MinEpochsForBlobsSidecarsRequest ) ,
2023-12-06 20:20:34 +00:00
Aliases : [ ] string { "extend-blob-retention-epoch" } ,
}
2023-11-21 18:44:38 +00:00
)
// BeaconNodeOptions sets configuration values on the node.BeaconNode value at node startup.
// Note: we can't get the right context from cli.Context, because the beacon node setup code uses this context to
// create a cancellable context. If we switch to using App.RunContext, we can set up this cancellation in the cmd
// package instead, and allow the functional options to tap into context cancellation.
2023-12-06 20:20:34 +00:00
func BeaconNodeOptions ( c * cli . Context ) ( [ ] node . Option , error ) {
e , err := blobRetentionEpoch ( c )
if err != nil {
return nil , err
}
bs , err := filesystem . NewBlobStorage ( blobStoragePath ( c ) , filesystem . WithBlobRetentionEpochs ( e ) )
2023-11-21 18:44:38 +00:00
if err != nil {
return nil , err
}
2023-12-06 20:20:34 +00:00
return [ ] node . Option { node . WithBlobStorage ( bs ) , node . WithBlobRetentionEpochs ( e ) } , nil
2023-11-21 18:44:38 +00:00
}
2023-11-25 01:57:22 +00:00
func blobStoragePath ( c * cli . Context ) string {
2023-12-06 20:20:34 +00:00
blobsPath := c . Path ( BlobStoragePathFlag . Name )
2023-11-25 01:57:22 +00:00
if blobsPath == "" {
// append a "blobs" subdir to the end of the data dir path
blobsPath = path . Join ( c . String ( cmd . DataDirFlag . Name ) , "blobs" )
}
return blobsPath
}
2023-12-06 20:20:34 +00:00
var errInvalidBlobRetentionEpochs = errors . New ( "value is smaller than spec minimum" )
// blobRetentionEpoch returns the spec deffault MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUEST
// or a user-specified flag overriding this value. If a user-specified override is
// smaller than the spec default, an error will be returned.
func blobRetentionEpoch ( cliCtx * cli . Context ) ( primitives . Epoch , error ) {
2023-12-16 11:37:44 +00:00
spec := params . BeaconConfig ( ) . MinEpochsForBlobsSidecarsRequest
2023-12-06 20:20:34 +00:00
if ! cliCtx . IsSet ( BlobRetentionEpochFlag . Name ) {
return spec , nil
}
re := primitives . Epoch ( cliCtx . Uint64 ( BlobRetentionEpochFlag . Name ) )
// Validate the epoch value against the spec default.
2023-12-16 11:37:44 +00:00
if re < params . BeaconConfig ( ) . MinEpochsForBlobsSidecarsRequest {
2023-12-06 20:20:34 +00:00
return spec , errors . Wrapf ( errInvalidBlobRetentionEpochs , "%s=%d, spec=%d" , BlobRetentionEpochFlag . Name , re , spec )
}
return re , nil
}