Skip to content

Commit

Permalink
Merge pull request #32 from base-org/setting-for-json-validation-only
Browse files Browse the repository at this point in the history
chore: allow validation service to process subset of formats
  • Loading branch information
danyalprout authored Jul 10, 2024
2 parents 988d545 + ec43e60 commit a1681ab
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
6 changes: 3 additions & 3 deletions archiver/service/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (a *Archiver) backfillBlobs(ctx context.Context, latest *v1.BeaconBlockHead
a.log.Crit("failed to read backfill_processes", "err", err)
}
backfillProcesses[common.Hash(latest.Root)] = storage.BackfillProcess{Start: *latest, Current: *latest}
a.dataStoreClient.WriteBackfillProcesses(ctx, backfillProcesses)
_ = a.dataStoreClient.WriteBackfillProcesses(ctx, backfillProcesses)

backfillLoop := func(start *v1.BeaconBlockHeader, current *v1.BeaconBlockHeader) {
curr, alreadyExists, err := current, false, error(nil)
Expand All @@ -219,7 +219,7 @@ func (a *Archiver) backfillBlobs(ctx context.Context, latest *v1.BeaconBlockHead
"startSlot", start.Header.Message.Slot,
)
delete(backfillProcesses, common.Hash(start.Root))
a.dataStoreClient.WriteBackfillProcesses(ctx, backfillProcesses)
_ = a.dataStoreClient.WriteBackfillProcesses(ctx, backfillProcesses)
}()

for !alreadyExists {
Expand All @@ -246,7 +246,7 @@ func (a *Archiver) backfillBlobs(ctx context.Context, latest *v1.BeaconBlockHead
count++
if count%10 == 0 {
backfillProcesses[common.Hash(start.Root)] = storage.BackfillProcess{Start: *start, Current: *curr}
a.dataStoreClient.WriteBackfillProcesses(ctx, backfillProcesses)
_ = a.dataStoreClient.WriteBackfillProcesses(ctx, backfillProcesses)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion validator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ func Main() cliapp.LifecycleAction {
beaconClient := service.NewBlobSidecarClient(cfg.BeaconConfig.BeaconURL)
blobClient := service.NewBlobSidecarClient(cfg.BlobConfig.BeaconURL)

return service.NewValidator(l, headerClient, beaconClient, blobClient, closeApp, cfg.NumBlocks), nil
return service.NewValidator(l, headerClient, beaconClient, blobClient, closeApp, cfg), nil
}
}
27 changes: 22 additions & 5 deletions validator/flags/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
)

type ValidatorConfig struct {
LogConfig oplog.CLIConfig
BeaconConfig common.BeaconConfig
BlobConfig common.BeaconConfig
NumBlocks int
LogConfig oplog.CLIConfig
BeaconConfig common.BeaconConfig
BlobConfig common.BeaconConfig
ValidateFormats []string
NumBlocks int
}

func (c ValidatorConfig) Check() error {
Expand All @@ -29,6 +30,21 @@ func (c ValidatorConfig) Check() error {
return fmt.Errorf("number of blocks must be greater than 0")
}

if len(c.ValidateFormats) == 0 || len(c.ValidateFormats) > 2 {
return fmt.Errorf("no formats to validate, please specify formats [json,ssz]")
}

seen := make(map[string]struct{})
for _, format := range c.ValidateFormats {
if format != "json" && format != "ssz" {
return fmt.Errorf("invalid format %v, please specify formats [json,ssz]", format)
}
if _, ok := seen[format]; ok {
return fmt.Errorf("duplicate format %v", format)
}
seen[format] = struct{}{}
}

return nil
}

Expand All @@ -45,6 +61,7 @@ func ReadConfig(cliCtx *cli.Context) ValidatorConfig {
BeaconURL: cliCtx.String(BlobApiClientUrlFlag.Name),
BeaconClientTimeout: timeout,
},
NumBlocks: cliCtx.Int(NumBlocksClientFlag.Name),
NumBlocks: cliCtx.Int(NumBlocksClientFlag.Name),
ValidateFormats: cliCtx.StringSlice(ValidateFormatsFlag.Name),
}
}
9 changes: 8 additions & 1 deletion validator/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ var (
Required: true,
EnvVars: opservice.PrefixEnvVar(EnvVarPrefix, "NUM_BLOCKS"),
}
ValidateFormatsFlag = &cli.StringSliceFlag{
Name: "validate-formats",
Usage: "The formats to validate [json, ssz]",
Value: cli.NewStringSlice("json", "ssz"),
Required: true,
EnvVars: opservice.PrefixEnvVar(EnvVarPrefix, "VALIDATE_FORMATS"),
}
)

func init() {
Flags = append(Flags, oplog.CLIFlags(EnvVarPrefix)...)
Flags = append(Flags, BeaconClientTimeoutFlag, L1BeaconClientUrlFlag, BlobApiClientUrlFlag, NumBlocksClientFlag)
Flags = append(Flags, BeaconClientTimeoutFlag, L1BeaconClientUrlFlag, BlobApiClientUrlFlag, NumBlocksClientFlag, ValidateFormatsFlag)
}

// Flags contains the list of configuration options available to the binary.
Expand Down
20 changes: 15 additions & 5 deletions validator/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
v1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/base-org/blob-archiver/common/storage"
"github.com/base-org/blob-archiver/validator/flags"
"github.com/ethereum-optimism/optimism/op-service/retry"
"github.com/ethereum/go-ethereum/log"
)
Expand All @@ -29,14 +30,21 @@ const (
retryAttempts = 10
)

func NewValidator(l log.Logger, headerClient client.BeaconBlockHeadersProvider, beaconAPI BlobSidecarClient, blobAPI BlobSidecarClient, app context.CancelCauseFunc, numBlocks int) *ValidatorService {
var (
formatSettingToHeader = map[string]Format{
"json": FormatJson,
"ssz": FormatSSZ,
}
)

func NewValidator(l log.Logger, headerClient client.BeaconBlockHeadersProvider, beaconAPI BlobSidecarClient, blobAPI BlobSidecarClient, app context.CancelCauseFunc, cfg flags.ValidatorConfig) *ValidatorService {
return &ValidatorService{
log: l,
headerClient: headerClient,
beaconAPI: beaconAPI,
blobAPI: blobAPI,
closeApp: app,
numBlocks: numBlocks,
cfg: cfg,
}
}

Expand All @@ -47,7 +55,7 @@ type ValidatorService struct {
beaconAPI BlobSidecarClient
blobAPI BlobSidecarClient
closeApp context.CancelCauseFunc
numBlocks int
cfg flags.ValidatorConfig
}

// Start starts the validator service. This will fetch the current range of blocks to validate and start the validation
Expand All @@ -64,7 +72,7 @@ func (a *ValidatorService) Start(ctx context.Context) error {
}

end := header.Data.Header.Message.Slot - finalizedL1Offset
start := end - phase0.Slot(a.numBlocks)
start := end - phase0.Slot(a.cfg.NumBlocks)

go a.checkBlobs(ctx, start, end)

Expand Down Expand Up @@ -126,7 +134,9 @@ func (a *ValidatorService) checkBlobs(ctx context.Context, start phase0.Slot, en
var result CheckBlobResult

for slot := start; slot <= end; slot++ {
for _, format := range []Format{FormatJson, FormatSSZ} {
for _, setting := range a.cfg.ValidateFormats {
format := formatSettingToHeader[setting]

id := strconv.FormatUint(uint64(slot), 10)

l := a.log.New("format", format, "slot", slot)
Expand Down
8 changes: 5 additions & 3 deletions validator/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/base-org/blob-archiver/common/beacon/beacontest"
"github.com/base-org/blob-archiver/common/blobtest"
"github.com/base-org/blob-archiver/common/storage"
"github.com/base-org/blob-archiver/validator/flags"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -71,9 +72,10 @@ func setup(t *testing.T) (*ValidatorService, *beacontest.StubBeaconClient, *stub
data: make(map[string]response),
}

numBlocks := 600

return NewValidator(l, headerClient, beacon, blob, cancel, numBlocks), headerClient, beacon, blob
return NewValidator(l, headerClient, beacon, blob, cancel, flags.ValidatorConfig{
ValidateFormats: []string{"json", "ssz"},
NumBlocks: 600,
}), headerClient, beacon, blob
}

func TestValidatorService_OnFetchError(t *testing.T) {
Expand Down

0 comments on commit a1681ab

Please sign in to comment.