Skip to content

Commit

Permalink
wip: 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 7bfea6b commit 0108886
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 15 deletions.
3 changes: 1 addition & 2 deletions mod/state-transition/pkg/core/state_processor_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package core

import (
"context"
"fmt"

engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
"github.com/berachain/beacon-kit/mod/errors"
Expand Down Expand Up @@ -140,7 +139,7 @@ func (sp *StateProcessor[

lph, err := st.GetLatestExecutionPayloadHeader()
if err != nil {
return fmt.Errorf("failed retrieving latest payload header: %w", err)
return err
}

// Check chain canonicity
Expand Down
2 changes: 2 additions & 0 deletions mod/state-transition/pkg/core/state_processor_randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ func (sp *StateProcessor[
}

epoch := sp.cs.SlotToEpoch(slot)

mix, err := st.GetRandaoMixAtIndex(
epoch.Unwrap() % sp.cs.EpochsPerHistoricalVector(),
)
if err != nil {
return err
}

return st.UpdateRandaoMixAtIndex(
(epoch.Unwrap()+1)%sp.cs.EpochsPerHistoricalVector(),
mix,
Expand Down
2 changes: 1 addition & 1 deletion mod/state-transition/pkg/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (sp *StateProcessor[
}
eth1Data, err := st.GetEth1Data()
if err != nil {
return err
return fmt.Errorf("failed retrieving eth1 data, %w", err)
}
depositCount := min(
sp.cs.MaxDepositsPerBlock(),
Expand Down
14 changes: 11 additions & 3 deletions mod/storage/pkg/beacondb/eth1.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func (kv *KVStore[
]) GetLatestExecutionPayloadHeader() (
ExecutionPayloadHeaderT, error,
) {
forkVersion, err := kv.latestExecutionPayloadVersion.Get(kv.ctx)
v, err := kv.latestExecutionPayloadVersion.Get(kv.ctx)
switch {
case err == nil:
kv.latestExecutionPayloadCodec.SetActiveForkVersion(forkVersion)
kv.latestExecutionPayloadCodec.SetActiveForkVersion(v)
return kv.latestExecutionPayloadHeader.Get(kv.ctx)
case errors.Is(err, collections.ErrNotFound):
var t ExecutionPayloadHeaderT
Expand Down Expand Up @@ -98,7 +98,15 @@ func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT,
]) GetEth1Data() (Eth1DataT, error) {
return kv.eth1Data.Get(kv.ctx)
d, err := kv.eth1Data.Get(kv.ctx)
switch {
case err == nil:
return d, nil
case errors.Is(err, collections.ErrNotFound):
return d, ErrNotFound
default:
return d, err
}
}

// SetEth1Data sets the eth1 data in the beacon state.
Expand Down
16 changes: 15 additions & 1 deletion mod/storage/pkg/beacondb/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

package beacondb

import (
"errors"

"cosmossdk.io/collections"
)

// SetFork sets the fork version for the given epoch.
func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
Expand All @@ -35,5 +41,13 @@ func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT,
]) GetFork() (ForkT, error) {
return kv.fork.Get(kv.ctx)
f, err := kv.fork.Get(kv.ctx)
switch {
case err == nil:
return f, nil
case errors.Is(err, collections.ErrNotFound):
return f, ErrNotFound
default:
return f, err
}
}
25 changes: 21 additions & 4 deletions mod/storage/pkg/beacondb/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

package beacondb

import "github.com/berachain/beacon-kit/mod/primitives/pkg/common"
import (
"errors"

"cosmossdk.io/collections"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
)

// UpdateBlockRootAtIndex sets a block root in the BeaconStore.
func (kv *KVStore[
Expand All @@ -41,10 +46,14 @@ func (kv *KVStore[
index uint64,
) (common.Root, error) {
bz, err := kv.blockRoots.Get(kv.ctx, index)
if err != nil {
switch {
case err == nil:
return common.Root(bz), nil
case errors.Is(err, collections.ErrNotFound):
return common.Root{}, ErrNotFound
default:
return common.Root{}, err
}
return common.Root(bz), nil
}

// SetLatestBlockHeader sets the latest block header in the BeaconStore.
Expand All @@ -64,7 +73,15 @@ func (kv *KVStore[
]) GetLatestBlockHeader() (
BeaconBlockHeaderT, error,
) {
return kv.latestBlockHeader.Get(kv.ctx)
h, err := kv.latestBlockHeader.Get(kv.ctx)
switch {
case err == nil:
return h, nil
case errors.Is(err, collections.ErrNotFound):
return h, ErrNotFound
default:
return h, err
}
}

// UpdateStateRootAtIndex updates the state root at the given slot.
Expand Down
26 changes: 22 additions & 4 deletions mod/storage/pkg/beacondb/randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@

package beacondb

import "github.com/berachain/beacon-kit/mod/primitives/pkg/common"
import (
"errors"
"fmt"

"cosmossdk.io/collections"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
)

// UpdateRandaoMixAtIndex sets the current RANDAO mix in the store.
func (kv *KVStore[
Expand All @@ -41,8 +47,20 @@ func (kv *KVStore[
index uint64,
) (common.Bytes32, error) {
bz, err := kv.randaoMix.Get(kv.ctx, index)
if err != nil {
return common.Bytes32{}, err
switch {
case err == nil:
return common.Bytes32(bz), nil
case errors.Is(err, collections.ErrNotFound):
return common.Bytes32{}, fmt.Errorf(
"failed retrieving randao mix at index %d: %w",
index,
ErrNotFound,
)
default:
return common.Bytes32{}, fmt.Errorf(
"failed retrieving randao mix at index %d: %w",
index,
err,
)
}
return common.Bytes32(bz), nil
}

0 comments on commit 0108886

Please sign in to comment.