Skip to content

Commit

Permalink
*: convert to uint64 (#2668)
Browse files Browse the repository at this point in the history
Convert `Duty.Slot` from int64 to uint64.

This is recommended according to ethereum consensus specs: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types

category: feature 
ticket: #2658
  • Loading branch information
xenowits authored Nov 6, 2023
1 parent be36a73 commit 1aa0bb4
Show file tree
Hide file tree
Showing 45 changed files with 331 additions and 340 deletions.
10 changes: 5 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func newTracker(ctx context.Context, life *lifecycle.Manager, deadlineFunc func(

// calculateTrackerDelay returns the slot to start tracking from. This mitigates noisy failed duties on
// startup due to downstream VC startup delays.
func calculateTrackerDelay(ctx context.Context, cl eth2wrap.Client, now time.Time) (int64, error) {
func calculateTrackerDelay(ctx context.Context, cl eth2wrap.Client, now time.Time) (uint64, error) {
const maxDelayTime = time.Second * 10 // We want to delay at most 10 seconds
const minDelaySlots = 2 // But we do not want to delay less than 2 slots

Expand All @@ -695,9 +695,9 @@ func calculateTrackerDelay(ctx context.Context, cl eth2wrap.Client, now time.Tim
return 0, err
}

currentSlot := int64(now.Sub(genesisTime) / slotDuration)
currentSlot := uint64(now.Sub(genesisTime) / slotDuration)

maxDelayTimeSlot := currentSlot + int64(maxDelayTime/slotDuration) + 1
maxDelayTimeSlot := currentSlot + uint64(maxDelayTime/slotDuration) + 1
minDelaySlot := currentSlot + minDelaySlots

if maxDelayTimeSlot < minDelaySlot {
Expand Down Expand Up @@ -1056,7 +1056,7 @@ func hex7(input []byte) string {
}

// slotFromTimestamp returns slot from the provided timestamp.
func slotFromTimestamp(ctx context.Context, eth2Cl eth2wrap.Client, timestamp time.Time) (int64, error) {
func slotFromTimestamp(ctx context.Context, eth2Cl eth2wrap.Client, timestamp time.Time) (uint64, error) {
genesis, err := eth2Cl.GenesisTime(ctx)
if err != nil {
return 0, err
Expand All @@ -1071,5 +1071,5 @@ func slotFromTimestamp(ctx context.Context, eth2Cl eth2wrap.Client, timestamp ti

delta := timestamp.Sub(genesis)

return int64(delta / slotDuration), nil
return uint64(delta / slotDuration), nil
}
2 changes: 1 addition & 1 deletion app/app_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestSlotFromTimestamp(t *testing.T) {
tests := []struct {
name string
slot int64
slot uint64
network string
timestamp time.Time
}{
Expand Down
10 changes: 5 additions & 5 deletions app/eth2wrap/success.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
package eth2wrap

import (
"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
eth2api "github.com/attestantio/go-eth2-client/api"
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
)

// isSyncStateOk returns true if the sync state is not syncing.
func isSyncStateOk(resp *api.Response[*apiv1.SyncState]) bool {
func isSyncStateOk(resp *eth2api.Response[*eth2v1.SyncState]) bool {
return !resp.Data.IsSyncing
}

// isAggregateAttestationOk returns true if the aggregate attestation is not nil (which can happen if the subscription wasn't successful).
func isAggregateAttestationOk(resp *api.Response[*phase0.Attestation]) bool {
func isAggregateAttestationOk(resp *eth2api.Response[*eth2p0.Attestation]) bool {
return resp.Data != nil
}
28 changes: 14 additions & 14 deletions app/eth2wrap/synthproposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
eth2bellatrix "github.com/attestantio/go-eth2-client/api/v1/bellatrix"
eth2capella "github.com/attestantio/go-eth2-client/api/v1/capella"
"github.com/attestantio/go-eth2-client/spec"
eth2spec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/attestantio/go-eth2-client/spec/capella"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
Expand Down Expand Up @@ -139,7 +139,7 @@ func (h *synthWrapper) BlindedProposal(ctx context.Context, opts *eth2api.Blinde

// syntheticProposal returns a synthetic unsigned beacon block to propose.
func (h *synthWrapper) syntheticProposal(ctx context.Context, slot eth2p0.Slot, vIdx eth2p0.ValidatorIndex) (*eth2api.VersionedProposal, error) {
var signedBlock *spec.VersionedSignedBeaconBlock
var signedBlock *eth2spec.VersionedSignedBeaconBlock

// Work our way back from previous slot to find a proposal to base the synthetic proposal on.
for prev := slot - 1; prev > 0; prev-- {
Expand Down Expand Up @@ -168,24 +168,24 @@ func (h *synthWrapper) syntheticProposal(ctx context.Context, slot eth2p0.Slot,

proposal := &eth2api.VersionedProposal{Version: signedBlock.Version}
switch signedBlock.Version {
case spec.DataVersionPhase0:
case eth2spec.DataVersionPhase0:
proposal.Phase0 = signedBlock.Phase0.Message
proposal.Phase0.Body.Graffiti = GetSyntheticGraffiti()
proposal.Phase0.Slot = slot
proposal.Phase0.ProposerIndex = vIdx
case spec.DataVersionAltair:
case eth2spec.DataVersionAltair:
proposal.Altair = signedBlock.Altair.Message
proposal.Altair.Body.Graffiti = GetSyntheticGraffiti()
proposal.Altair.Slot = slot
proposal.Altair.ProposerIndex = vIdx
case spec.DataVersionBellatrix:
case eth2spec.DataVersionBellatrix:
proposal.Bellatrix = signedBlock.Bellatrix.Message
proposal.Bellatrix.Body.Graffiti = GetSyntheticGraffiti()
proposal.Bellatrix.Slot = slot
proposal.Bellatrix.ProposerIndex = vIdx
proposal.Bellatrix.Body.ExecutionPayload.FeeRecipient = feeRecipient
proposal.Bellatrix.Body.ExecutionPayload.Transactions = fraction(proposal.Bellatrix.Body.ExecutionPayload.Transactions)
case spec.DataVersionCapella:
case eth2spec.DataVersionCapella:
proposal.Capella = signedBlock.Capella.Message
proposal.Capella.Body.Graffiti = GetSyntheticGraffiti()
proposal.Capella.Slot = slot
Expand Down Expand Up @@ -237,9 +237,9 @@ func GetSyntheticGraffiti() [32]byte {
func IsSyntheticBlindedBlock(block *eth2api.VersionedSignedBlindedProposal) bool {
var graffiti [32]byte
switch block.Version {
case spec.DataVersionBellatrix:
case eth2spec.DataVersionBellatrix:
graffiti = block.Bellatrix.Message.Body.Graffiti
case spec.DataVersionCapella:
case eth2spec.DataVersionCapella:
graffiti = block.Capella.Message.Body.Graffiti
default:
return false
Expand All @@ -252,13 +252,13 @@ func IsSyntheticBlindedBlock(block *eth2api.VersionedSignedBlindedProposal) bool
func IsSyntheticProposal(block *eth2api.VersionedSignedProposal) bool {
var graffiti [32]byte
switch block.Version {
case spec.DataVersionPhase0:
case eth2spec.DataVersionPhase0:
graffiti = block.Phase0.Message.Body.Graffiti
case spec.DataVersionAltair:
case eth2spec.DataVersionAltair:
graffiti = block.Altair.Message.Body.Graffiti
case spec.DataVersionBellatrix:
case eth2spec.DataVersionBellatrix:
graffiti = block.Bellatrix.Message.Body.Graffiti
case spec.DataVersionCapella:
case eth2spec.DataVersionCapella:
graffiti = block.Capella.Message.Body.Graffiti
default:
return false
Expand Down Expand Up @@ -436,7 +436,7 @@ func blindedProposal(proposal *eth2api.VersionedProposal) (*eth2api.VersionedBli
var resp *eth2api.VersionedBlindedProposal
// Blinded blocks are only available from bellatrix.
switch proposal.Version {
case spec.DataVersionBellatrix:
case eth2spec.DataVersionBellatrix:
resp = &eth2api.VersionedBlindedProposal{
Version: proposal.Version,
Bellatrix: &eth2bellatrix.BlindedBeaconBlock{
Expand Down Expand Up @@ -473,7 +473,7 @@ func blindedProposal(proposal *eth2api.VersionedProposal) (*eth2api.VersionedBli
},
},
}
case spec.DataVersionCapella:
case eth2spec.DataVersionCapella:
resp = &eth2api.VersionedBlindedProposal{
Version: proposal.Version,
Capella: &eth2capella.BlindedBeaconBlock{
Expand Down
10 changes: 5 additions & 5 deletions app/eth2wrap/synthproposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
eth2api "github.com/attestantio/go-eth2-client/api"
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
eth2capella "github.com/attestantio/go-eth2-client/api/v1/capella"
"github.com/attestantio/go-eth2-client/spec"
eth2spec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestSynthProposer(t *testing.T) {
return cached(ctx)
}
signedBeaconBlock := bmock.SignedBeaconBlock
bmock.SignedBeaconBlockFunc = func(ctx context.Context, blockID string) (*spec.VersionedSignedBeaconBlock, error) {
bmock.SignedBeaconBlockFunc = func(ctx context.Context, blockID string) (*eth2spec.VersionedSignedBeaconBlock, error) {
opts := &eth2api.SignedBeaconBlockOpts{Block: blockID}
resp, err := signedBeaconBlock(ctx, opts)
if err != nil {
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestSynthProposer(t *testing.T) {

continue
}
require.Equal(t, spec.DataVersionCapella, block.Version)
require.Equal(t, eth2spec.DataVersionCapella, block.Version)

signed := testutil.RandomVersionedSignedProposal()
signed.Capella.Message = block.Capella
Expand All @@ -146,10 +146,10 @@ func TestSynthProposer(t *testing.T) {
} else {
require.Equal(t, feeRecipient, block.Capella.Body.ExecutionPayloadHeader.FeeRecipient)
}
require.Equal(t, spec.DataVersionCapella, block.Version)
require.Equal(t, eth2spec.DataVersionCapella, block.Version)

signed := &eth2api.VersionedSignedBlindedProposal{
Version: spec.DataVersionCapella,
Version: eth2spec.DataVersionCapella,
Capella: &eth2capella.SignedBlindedBeaconBlock{
Message: block.Capella,
Signature: testutil.RandomEth2Signature(),
Expand Down
2 changes: 1 addition & 1 deletion app/priorities.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *mutableConfig) getInfoSync() (*infosync.Component, bool) {
}

// BuilderAPI returns true if the cluster supports the builder API for the provided slot.
func (c *mutableConfig) BuilderAPI(_ int64) bool {
func (c *mutableConfig) BuilderAPI(_ uint64) bool {
// TODO(corver): Dynamic BuilderAPI config disabled since VCs do not support it.
return c.conf.BuilderAPI
}
2 changes: 1 addition & 1 deletion app/qbftdebug_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestQBFTDebugger(t *testing.T) {
func randomQBFTMessage() *pbv1.QBFTMsg {
return &pbv1.QBFTMsg{
Type: rand.Int63(),
Duty: &pbv1.Duty{Slot: rand.Int63()},
Duty: &pbv1.Duty{Slot: rand.Uint64()},
PeerIdx: rand.Int63(),
Round: rand.Int63(),
PreparedRound: rand.Int63(),
Expand Down
4 changes: 2 additions & 2 deletions core/aggsigdb/memory_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestDutyExpiration(t *testing.T) {
db.Run(ctx)
}()

slot := int64(99)
slot := uint64(99)
duty := core.NewAttesterDuty(slot)
pubkey := testutil.RandomCorePubKey(t)
sig := testutil.RandomCoreSignature()
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestCancelledQuery(t *testing.T) {

go db.Run(ctx)

slot := int64(99)
slot := uint64(99)
duty := core.NewAttesterDuty(slot)
pubkey := testutil.RandomCorePubKey(t)
sig := testutil.RandomCoreSignature()
Expand Down
6 changes: 3 additions & 3 deletions core/bcast/bcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func New(ctx context.Context, eth2Cl eth2wrap.Client) (Broadcaster, error) {

type Broadcaster struct {
eth2Cl eth2wrap.Client
delayFunc func(slot int64) time.Duration
delayFunc func(slot uint64) time.Duration
}

// Broadcast broadcasts the aggregated signed duty data object to the beacon-node.
Expand Down Expand Up @@ -283,7 +283,7 @@ func setToAttestations(set core.SignedDataSet) ([]*eth2p0.Attestation, error) {
}

// newDelayFunc returns a function that calculates the delay since the start of a slot.
func newDelayFunc(ctx context.Context, eth2Cl eth2wrap.Client) (func(slot int64) time.Duration, error) {
func newDelayFunc(ctx context.Context, eth2Cl eth2wrap.Client) (func(slot uint64) time.Duration, error) {
genesis, err := eth2Cl.GenesisTime(ctx)
if err != nil {
return nil, err
Expand All @@ -294,7 +294,7 @@ func newDelayFunc(ctx context.Context, eth2Cl eth2wrap.Client) (func(slot int64)
return nil, err
}

return func(slot int64) time.Duration {
return func(slot uint64) time.Duration {
slotStart := genesis.Add(slotDuration * time.Duration(slot))
return time.Since(slotStart)
}, nil
Expand Down
2 changes: 1 addition & 1 deletion core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
package core

// BuilderEnabled determines whether the builderAPI is enabled for the provided slot.
type BuilderEnabled func(slot int64) bool
type BuilderEnabled func(slot uint64) bool
2 changes: 1 addition & 1 deletion core/consensus/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ func fmtStepPeers(step roundStep) string {

// leader return the deterministic leader index.
func leader(duty core.Duty, round int64, nodes int) int64 {
return ((duty.Slot) + int64(duty.Type) + round) % int64(nodes)
return (int64(duty.Slot) + int64(duty.Type) + round) % int64(nodes)
}

func valuesByHash(values []*anypb.Any) (map[[32]byte]*anypb.Any, error) {
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/msg_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func randomMsg(t *testing.T) *pbv1.QBFTMsg {

return &pbv1.QBFTMsg{
Type: msgType,
Duty: core.DutyToProto(core.Duty{Type: core.DutyType(rand.Int()), Slot: rand.Int63()}),
Duty: core.DutyToProto(core.Duty{Type: core.DutyType(rand.Int()), Slot: rand.Uint64()}),
PeerIdx: rand.Int63(),
Round: rand.Int63(),
PreparedRound: rand.Int63(),
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/roundtimer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func getTimerFunc() timerFunc {
}

return func(duty core.Duty) roundTimer {
random := rand.New(rand.NewSource(int64(duty.Type) + duty.Slot)) //nolint:gosec // Required for consistent pseudo-randomness.
random := rand.New(rand.NewSource(int64(uint64(duty.Type) + duty.Slot))) //nolint:gosec // Required for consistent pseudo-randomness.
return abTimers[random.Intn(len(abTimers))]()
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/consensus/strategysim_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func testRoundTimers(t *testing.T, timers []roundTimerFunc, itersPerConfig int)
// fmt.Printf("undedicded config=%#v\n", config)
// fmt.Printf("results=%#v\n", results)
// fmt.Println(buf.String())
//}
// }
return Named[[]result]{name, results}, nil
},
allConfigs,
Expand Down Expand Up @@ -396,7 +396,7 @@ func testStrategySimulator(t *testing.T, conf ssConfig, syncer zapcore.WriteSync

log.Debug(ctx, "Starting peer")

err := qbft.Run(ctx, def, transports[p.Idx], core.Duty{Slot: int64(conf.seed)}, p.Idx, valCh)
err := qbft.Run(ctx, def, transports[p.Idx], core.Duty{Slot: uint64(conf.seed)}, p.Idx, valCh)
if err != nil && !errors.Is(err, context.Canceled) {
return res, err
}
Expand Down
8 changes: 4 additions & 4 deletions core/corepb/v1/core.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/corepb/v1/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package core.corepb.v1;
option go_package = "github.com/obolnetwork/charon/core/corepb/v1";

message Duty { // core.Duty
int64 slot = 1; // int64
uint64 slot = 1; // uint64
int32 type = 2; // core.DutyType
}

Expand Down
2 changes: 1 addition & 1 deletion core/deadline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestDeadliner(t *testing.T) {
// Wait till all the duties are added to the deadliner.
wg.Wait()

var maxSlot int64
var maxSlot uint64
for _, duty := range nonExpiredDuties {
if maxSlot < duty.Slot {
maxSlot = duty.Slot
Expand Down
Loading

0 comments on commit 1aa0bb4

Please sign in to comment.