Skip to content

Commit

Permalink
feat(typing): use the unwrap (berachain#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocnc authored Aug 9, 2024
1 parent 208682a commit d606a9b
Show file tree
Hide file tree
Showing 26 changed files with 72 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,15 @@ package chainspec

import "github.com/berachain/beacon-kit/mod/chain-spec/pkg/chain"

type BartioChainSpec[
DomainTypeT ~[4]byte,
EpochT ~uint64,
ExecutionAddressT ~[20]byte,
SlotT ~uint64,
CometBFTConfigT any,
] struct {
// BGTContractAddress
BGTContractAddress ExecutionAddressT `mapstructure:"bgt-contract-address"`
type BeraChainSpec struct {
// SpecData is the underlying data structure for chain-specific parameters.
chain.SpecData[
DomainTypeT,
EpochT,
ExecutionAddressT,
SlotT,
CometBFTConfigT,
chain.Spec[
[4]byte,
uint64,
[20]byte,
uint64,
any,
]
// BGTContractAddress
BGTContractAddress [20]byte `mapstructure:"bgt-contract-address"`
}
10 changes: 6 additions & 4 deletions examples/berad/pkg/state-transition/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package transition
import (
"bytes"

chainspec "github.com/berachain/beacon-kit/examples/berad/pkg/chain-spec"
"github.com/berachain/beacon-kit/mod/errors"
gethprimitives "github.com/berachain/beacon-kit/mod/geth-primitives"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
Expand Down Expand Up @@ -78,7 +79,7 @@ type StateProcessor[
},
] struct {
// cs is the chain specification for the beacon chain.
cs common.ChainSpec
cs chainspec.BeraChainSpec
// signer is the BLS signer used for cryptographic operations.
signer crypto.BLSSigner
// executionEngine is the engine responsible for executing transactions.
Expand Down Expand Up @@ -130,7 +131,7 @@ func NewStateProcessor[
ToExecutionAddress() (gethprimitives.ExecutionAddress, error)
},
](
cs common.ChainSpec,
cs chainspec.BeraChainSpec,
executionEngine ExecutionEngine[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalsT,
],
Expand Down Expand Up @@ -206,7 +207,8 @@ func (sp *StateProcessor[
}

// Process the Epoch Boundary.
if uint64(stateSlot+1)%sp.cs.SlotsPerEpoch() == 0 {
boundary := (stateSlot.Unwrap() + 1) == 0
if boundary {
if epochValidatorUpdates, err =
sp.processEpoch(st); err != nil {
return nil, err
Expand Down Expand Up @@ -460,7 +462,7 @@ func (sp *StateProcessor[
return err
}

if sp.cs.SlotToEpoch(slot) == math.U64(constants.GenesisEpoch) {
if sp.cs.SlotToEpoch(slot.Unwrap()) == constants.GenesisEpoch {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (sp *StateProcessor[
// When we are verifying a payload we expect that it was produced by
// the proposer for the slot that it is for.
expectedMix, err := st.GetRandaoMixAtIndex(
uint64(sp.cs.SlotToEpoch(slot)) % sp.cs.EpochsPerHistoricalVector())
sp.cs.SlotToEpoch(slot.Unwrap()) % sp.cs.EpochsPerHistoricalVector())
if err != nil {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions examples/berad/pkg/state-transition/state_processor_randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/constants"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto/sha256"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/berachain/beacon-kit/mod/primitives/pkg/version"
"github.com/go-faster/xor"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func (sp *StateProcessor[
return err
}

epoch := sp.cs.SlotToEpoch(slot)
epoch := sp.cs.SlotToEpoch(slot.Unwrap())
body := blk.GetBody()

var fd ForkDataT
Expand All @@ -67,7 +68,7 @@ func (sp *StateProcessor[

if !skipVerification {
signingRoot := fd.ComputeRandaoSigningRoot(
sp.cs.DomainTypeRandao(), epoch,
sp.cs.DomainTypeRandao(), math.U64(epoch),
)
reveal := body.GetRandaoReveal()
if err = sp.signer.VerifySignature(
Expand All @@ -80,14 +81,14 @@ func (sp *StateProcessor[
}

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

return st.UpdateRandaoMixAtIndex(
uint64(epoch)%sp.cs.EpochsPerHistoricalVector(),
epoch%sp.cs.EpochsPerHistoricalVector(),
sp.buildRandaoMix(prevMix, body.GetRandaoReveal()),
)
}
Expand All @@ -106,15 +107,15 @@ func (sp *StateProcessor[
return err
}

epoch := sp.cs.SlotToEpoch(slot)
epoch := sp.cs.SlotToEpoch(slot.Unwrap())
mix, err := st.GetRandaoMixAtIndex(
uint64(epoch) % sp.cs.EpochsPerHistoricalVector(),
epoch % sp.cs.EpochsPerHistoricalVector(),
)
if err != nil {
return err
}
return st.UpdateRandaoMixAtIndex(
uint64(epoch+1)%sp.cs.EpochsPerHistoricalVector(),
(epoch+1)%sp.cs.EpochsPerHistoricalVector(),
mix,
)
}
Expand Down
14 changes: 7 additions & 7 deletions examples/berad/pkg/state-transition/state_processor_slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (sp *StateProcessor[
return err
}

index := (uint64(sp.cs.SlotToEpoch(slot)) + 1) % sp.cs.EpochsPerSlashingsVector()
index := (sp.cs.SlotToEpoch(slot.Unwrap()) + 1) % sp.cs.EpochsPerSlashingsVector()
return st.UpdateSlashingAtIndex(index, 0)
}

Expand Down Expand Up @@ -92,8 +92,8 @@ func (sp *StateProcessor[
}

adjustedTotalSlashingBalance := min(
uint64(totalSlashings)*sp.cs.ProportionalSlashingMultiplier(),
uint64(totalBalance),
totalSlashings.Unwrap()*sp.cs.ProportionalSlashingMultiplier(),
totalBalance.Unwrap(),
)

vals, err := st.GetValidators()
Expand All @@ -108,16 +108,16 @@ func (sp *StateProcessor[
}

//nolint:mnd // this is in the spec
slashableEpoch := (uint64(sp.cs.SlotToEpoch(slot)) + sp.cs.EpochsPerSlashingsVector()) / 2
slashableEpoch := (sp.cs.SlotToEpoch(slot.Unwrap()) + sp.cs.EpochsPerSlashingsVector()) / 2

// Iterate through the validators and slash if needed.
for _, val := range vals {
if val.IsSlashed() &&
(slashableEpoch == uint64(val.GetWithdrawableEpoch())) {
(slashableEpoch == val.GetWithdrawableEpoch().Unwrap()) {
if err = sp.processSlash(
st, val,
adjustedTotalSlashingBalance,
uint64(totalBalance),
totalBalance.Unwrap(),
); err != nil {
return err
}
Expand All @@ -139,7 +139,7 @@ func (sp *StateProcessor[
) error {
// Calculate the penalty.
increment := sp.cs.EffectiveBalanceIncrement()
balDivIncrement := uint64(val.GetEffectiveBalance()) / increment
balDivIncrement := val.GetEffectiveBalance().Unwrap() / increment
penaltyNumerator := balDivIncrement * adjustedTotalSlashingBalance
penalty := penaltyNumerator / totalBalance * increment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (sp *StateProcessor[
) error {
var (
genesisValidatorsRoot common.Root
epoch math.Epoch
epoch uint64
err error
)

Expand All @@ -167,7 +167,7 @@ func (sp *StateProcessor[
}

// Get the current epoch.
epoch = sp.cs.SlotToEpoch(slot)
epoch = sp.cs.SlotToEpoch(slot.Unwrap())

// Verify that the message was signed correctly.
var d ForkDataT
Expand Down
2 changes: 1 addition & 1 deletion mod/beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *Service[
var blk BeaconBlockT
// Create a new block.
parentBlockRoot, err := st.GetBlockRootAtIndex(
uint64(requestedSlot-1) % s.chainSpec.SlotsPerHistoricalRoot(),
(requestedSlot.Unwrap() - 1) % s.chainSpec.SlotsPerHistoricalRoot(),
)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion mod/consensus-types/pkg/types/fork_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (fd *ForkData) ComputeRandaoSigningRoot(
epoch math.Epoch,
) common.Root {
return ComputeSigningRootUInt64(
uint64(epoch),
epoch.Unwrap(),
fd.ComputeDomain(domainType),
)
}
2 changes: 1 addition & 1 deletion mod/da/pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *Store[BeaconBlockBodyT]) IsDataAvailable(
) bool {
for _, commitment := range body.GetBlobKzgCommitments() {
// Check if the block data is available in the IndexDB
blockData, err := s.IndexDB.Has(uint64(slot), commitment[:])
blockData, err := s.IndexDB.Has(slot.Unwrap(), commitment[:])
if err != nil || !blockData {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion mod/execution/pkg/deposit/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (dc *WrappedBeaconDepositContract[
logs, err := dc.FilterDeposit(
&bind.FilterOpts{
Context: ctx,
Start: uint64(blkNum),
Start: blkNum.Unwrap(),
End: (*uint64)(&blkNum),
},
)
Expand Down
2 changes: 1 addition & 1 deletion mod/execution/pkg/deposit/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ func (m *metrics) markFailedToGetBlockLogs(blockNum math.U64) {
m.sink.IncrementCounter(
"beacon_kit.execution.deposit.failed_to_get_block_logs",
"block_num",
strconv.FormatUint(uint64(blockNum), 10),
strconv.FormatUint(blockNum.Unwrap(), 10),
)
}
6 changes: 3 additions & 3 deletions mod/execution/pkg/deposit/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func BuildPruneRangeFn[
}
index := deposits[len(deposits)-1].GetIndex()

end := min(index, math.U64(cs.MaxDepositsPerBlock()))
end := min(index.Unwrap(), cs.MaxDepositsPerBlock())
if index < math.U64(cs.MaxDepositsPerBlock()) {
return 0, end.Unwrap()
return 0, end
}

return uint64(index - math.U64(cs.MaxDepositsPerBlock())), end.Unwrap()
return index.Unwrap() - cs.MaxDepositsPerBlock(), end
}
}
2 changes: 1 addition & 1 deletion mod/payload/pkg/attributes/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (f *Factory[

// Get the previous randao mix.
if prevRandao, err = st.GetRandaoMixAtIndex(
uint64(epoch) % f.chainSpec.EpochsPerHistoricalVector(),
epoch.Unwrap() % f.chainSpec.EpochsPerHistoricalVector(),
); err != nil {
return attributes, err
}
Expand Down
4 changes: 3 additions & 1 deletion mod/runtime/pkg/cosmos/baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func (app *BaseApp) InitChain(
}

if res == nil {
return nil, errors.New("application init chain handler returned a nil response")
return nil, errors.New(
"application init chain handler returned a nil response",
)
}

if len(req.Validators) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion mod/state-transition/pkg/core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (s *StateDB[
return nil, err
}

epoch := math.Epoch(uint64(slot) / s.cs.SlotsPerEpoch())
epoch := math.Epoch(slot.Unwrap() / s.cs.SlotsPerEpoch())

withdrawalIndex, err := s.GetNextWithdrawalIndex()
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion mod/state-transition/pkg/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ func (sp *StateProcessor[
}

// Process the Epoch Boundary.
if uint64(stateSlot+1)%sp.cs.SlotsPerEpoch() == 0 {
boundary := (stateSlot.Unwrap()+1)%sp.cs.SlotsPerEpoch() == 0
if boundary {
if epochValidatorUpdates, err =
sp.processEpoch(st); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion mod/state-transition/pkg/core/state_processor_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (sp *StateProcessor[
// When we are verifying a payload we expect that it was produced by
// the proposer for the slot that it is for.
expectedMix, err := st.GetRandaoMixAtIndex(
uint64(sp.cs.SlotToEpoch(slot)) % sp.cs.EpochsPerHistoricalVector())
sp.cs.SlotToEpoch(slot).Unwrap() % sp.cs.EpochsPerHistoricalVector())
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions mod/state-transition/pkg/core/state_processor_randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ func (sp *StateProcessor[
}

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

return st.UpdateRandaoMixAtIndex(
uint64(epoch)%sp.cs.EpochsPerHistoricalVector(),
epoch.Unwrap()%sp.cs.EpochsPerHistoricalVector(),
sp.buildRandaoMix(prevMix, body.GetRandaoReveal()),
)
}
Expand All @@ -108,13 +108,13 @@ func (sp *StateProcessor[

epoch := sp.cs.SlotToEpoch(slot)
mix, err := st.GetRandaoMixAtIndex(
uint64(epoch) % sp.cs.EpochsPerHistoricalVector(),
epoch.Unwrap() % sp.cs.EpochsPerHistoricalVector(),
)
if err != nil {
return err
}
return st.UpdateRandaoMixAtIndex(
uint64(epoch+1)%sp.cs.EpochsPerHistoricalVector(),
(epoch.Unwrap()+1)%sp.cs.EpochsPerHistoricalVector(),
mix,
)
}
Expand Down
Loading

0 comments on commit d606a9b

Please sign in to comment.