Skip to content

Commit

Permalink
wip: yet some more fixing err not found in beacondb package
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Oct 11, 2024
1 parent 3afed77 commit ef0e1da
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 35 deletions.
4 changes: 1 addition & 3 deletions mod/state-transition/pkg/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
package core

import (
"fmt"

"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
Expand All @@ -47,7 +45,7 @@ func (sp *StateProcessor[
}
eth1Data, err := st.GetEth1Data()
if err != nil {
return fmt.Errorf("failed retrieving eth1 data, %w", err)
return err
}
depositCount := min(
sp.cs.MaxDepositsPerBlock(),
Expand Down
55 changes: 47 additions & 8 deletions mod/storage/pkg/beacondb/eth1.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,50 @@ func (kv *KVStore[
]) GetLatestExecutionPayloadHeader() (
ExecutionPayloadHeaderT, error,
) {
var h ExecutionPayloadHeaderT
v, err := kv.getLatestExecutionPayloadVersion()
if err != nil {
return h, fmt.Errorf(
"failed retrieving latest execution payload header: %w",
err,
)
}
kv.latestExecutionPayloadCodec.SetActiveForkVersion(v)
h, err = kv.latestExecutionPayloadHeader.Get(kv.ctx)
switch {
case err == nil:
return h, nil
case errors.Is(err, collections.ErrNotFound):
return h, fmt.Errorf(
"failed retrieving latest execution payload header: %w",
ErrNotFound,
)
default:
return h, fmt.Errorf(
"failed retrieving latest execution payload header: %w",
err,
)
}
}

func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT,
]) getLatestExecutionPayloadVersion() (uint32, error) {
v, err := kv.latestExecutionPayloadVersion.Get(kv.ctx)
switch {
case err == nil:
kv.latestExecutionPayloadCodec.SetActiveForkVersion(v)
return kv.latestExecutionPayloadHeader.Get(kv.ctx)
return v, nil
case errors.Is(err, collections.ErrNotFound):
var t ExecutionPayloadHeaderT
return t, ErrNotFound
return 0, fmt.Errorf(
"failed retrieving latest execution payload version: %w",
ErrNotFound,
)
default:
var t ExecutionPayloadHeaderT
return t, err
return 0, fmt.Errorf(
"failed retrieving latest execution payload version: %w",
err,
)
}
}

Expand Down Expand Up @@ -112,9 +145,15 @@ func (kv *KVStore[
case err == nil:
return d, nil
case errors.Is(err, collections.ErrNotFound):
return d, ErrNotFound
return d, fmt.Errorf(
"failed retrieving eth1 data: %w",
ErrNotFound,
)
default:
return d, err
return d, fmt.Errorf(
"failed retrieving eth1 data: %w",
err,
)
}
}

Expand Down
5 changes: 3 additions & 2 deletions mod/storage/pkg/beacondb/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package beacondb

import (
"errors"
"fmt"

"cosmossdk.io/collections"
)
Expand All @@ -46,8 +47,8 @@ func (kv *KVStore[
case err == nil:
return f, nil
case errors.Is(err, collections.ErrNotFound):
return f, ErrNotFound
return f, fmt.Errorf("failed retrieving fork: %w", ErrNotFound)
default:
return f, err
return f, fmt.Errorf("failed retrieving fork: %w", err)
}
}
41 changes: 34 additions & 7 deletions mod/storage/pkg/beacondb/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package beacondb

import (
"errors"
"fmt"

"cosmossdk.io/collections"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
Expand Down Expand Up @@ -50,9 +51,17 @@ func (kv *KVStore[
case err == nil:
return common.Root(bz), nil
case errors.Is(err, collections.ErrNotFound):
return common.Root{}, ErrNotFound
return common.Root{}, fmt.Errorf(
"failed retrieving block root at index %d: %w",
index,
ErrNotFound,
)
default:
return common.Root{}, err
return common.Root{}, fmt.Errorf(
"failed retrieving block root at index %d: %w",
index,
err,
)
}
}

Expand All @@ -78,9 +87,15 @@ func (kv *KVStore[
case err == nil:
return h, nil
case errors.Is(err, collections.ErrNotFound):
return h, ErrNotFound
return h, fmt.Errorf(
"failed retrieving latest block header: %w",
ErrNotFound,
)
default:
return h, err
return h, fmt.Errorf(
"failed retrieving latest block header: %w",
err,
)
}
}

Expand All @@ -103,8 +118,20 @@ func (kv *KVStore[
idx uint64,
) (common.Root, error) {
bz, err := kv.stateRoots.Get(kv.ctx, idx)
if err != nil {
return common.Root{}, err
switch {
case err == nil:
return common.Root(bz), nil
case errors.Is(err, collections.ErrNotFound):
return common.Root{}, fmt.Errorf(
"failed retrieving state root at index %d: %w",
idx,
ErrNotFound,
)
default:
return common.Root{}, fmt.Errorf(
"failed retrieving state root at index %d: %w",
idx,
err,
)
}
return common.Root(bz), nil
}
45 changes: 38 additions & 7 deletions mod/storage/pkg/beacondb/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
package beacondb

import (
"errors"
"fmt"

"cosmossdk.io/collections"
"cosmossdk.io/collections/indexes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
Expand Down Expand Up @@ -87,10 +91,22 @@ func (kv *KVStore[
kv.ctx,
pubkey[:],
)
if err != nil {
return 0, err
switch {
case err == nil:
return math.ValidatorIndex(idx), nil
case errors.Is(err, collections.ErrNotFound):
return 0, fmt.Errorf(
"failed retrieving validator index by pub key %s: %w",
pubkey,
ErrNotFound,
)
default:
return 0, fmt.Errorf(
"failed retrieving validator index by pub key %s: %w",
pubkey,
err,
)
}
return math.ValidatorIndex(idx), nil
}

// ValidatorIndexByCometBFTAddress returns the validator address by index.
Expand Down Expand Up @@ -217,7 +233,22 @@ func (kv *KVStore[
idx math.ValidatorIndex,
) (math.Gwei, error) {
balance, err := kv.balances.Get(kv.ctx, idx.Unwrap())
return math.Gwei(balance), err
switch {
case err == nil:
return math.Gwei(balance), nil
case errors.Is(err, collections.ErrNotFound):
return 0, fmt.Errorf(
"failed retrieving balance at index %d: %w",
idx,
ErrNotFound,
)
default:
return 0, fmt.Errorf(
"failed retrieving balance at index %d: %w",
idx,
err,
)
}
}

// SetBalance sets the balance of a validator.
Expand Down Expand Up @@ -268,13 +299,13 @@ func (kv *KVStore[
return 0, err
}

slot, err := kv.slot.Get(kv.ctx)
slot, err := kv.GetSlot()
if err != nil {
return 0, err
return 0, fmt.Errorf("failed retrieving total active balances: %w", err)
}

totalActiveBalances := math.Gwei(0)
epoch := math.Epoch(slot / slotsPerEpoch)
epoch := slot / math.Epoch(slotsPerEpoch)
return totalActiveBalances, indexes.ScanValues(
kv.ctx, kv.validators, iter, func(v ValidatorT,
) bool {
Expand Down
26 changes: 18 additions & 8 deletions mod/storage/pkg/beacondb/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package beacondb

import (
"fmt"

"cosmossdk.io/collections"
"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
Expand Down Expand Up @@ -55,12 +57,18 @@ func (kv *KVStore[
index uint64,
) (math.Gwei, error) {
amount, err := kv.slashings.Get(kv.ctx, index)
if errors.Is(err, collections.ErrNotFound) {
switch {
case err == nil:
return math.Gwei(amount), nil
case errors.Is(err, collections.ErrNotFound):
return 0, nil
} else if err != nil {
return 0, err
default:
return 0, fmt.Errorf(
"failed retrieving slashing at index %d: %w",
index,
err,
)
}
return math.Gwei(amount), nil
}

// SetSlashingAtIndex sets the slashing amount in the store.
Expand All @@ -80,12 +88,14 @@ func (kv *KVStore[
ForkT, ValidatorT, ValidatorsT,
]) GetTotalSlashing() (math.Gwei, error) {
total, err := kv.totalSlashing.Get(kv.ctx)
if errors.Is(err, collections.ErrNotFound) {
switch {
case err == nil:
return math.Gwei(total), nil
case errors.Is(err, collections.ErrNotFound):
return 0, nil
} else if err != nil {
return 0, err
default:
return 0, fmt.Errorf("failed retrieving total slashing: %w", err)
}
return math.Gwei(total), nil
}

// SetTotalSlashing sets the total slashing amount in the store.
Expand Down

0 comments on commit ef0e1da

Please sign in to comment.