Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/priority: refactor to fix deadlock #1429

Merged
merged 7 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions app/vmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/core"
"github.com/obolnetwork/charon/eth2util"
"github.com/obolnetwork/charon/eth2util/keystore"
"github.com/obolnetwork/charon/testutil/validatormock"
)
Expand All @@ -48,13 +49,13 @@ func wireValidatorMock(conf Config, pubshares []eth2p0.BLSPubKey, sched core.Sch
onStartup := true
sched.SubscribeSlots(func(ctx context.Context, slot core.Slot) error {
// Prepare attestations when slots tick.
vMockWrap(ctx, slot.Slot, slot.Epoch(), func(ctx context.Context, state vMockState) error {
vMockWrap(ctx, slot.Slot, func(ctx context.Context, state vMockState) error {
return state.Attester.Prepare(ctx)
})

// Prepare sync committee message when epoch tick.
if onStartup || slot.FirstInEpoch() {
vMockWrap(ctx, slot.Slot, slot.Epoch(), func(ctx context.Context, state vMockState) error {
vMockWrap(ctx, slot.Slot, func(ctx context.Context, state vMockState) error {
// Either call if it is first slot in epoch or on charon startup.
return state.SyncCommMember.PrepareEpoch(ctx)
})
Expand All @@ -63,13 +64,13 @@ func wireValidatorMock(conf Config, pubshares []eth2p0.BLSPubKey, sched core.Sch
onStartup = false

// Prepare sync committee selections when slots tick.
vMockWrap(ctx, slot.Slot, slot.Epoch(), func(ctx context.Context, state vMockState) error {
vMockWrap(ctx, slot.Slot, func(ctx context.Context, state vMockState) error {
// Either call if it is first slot in epoch or on charon startup.
return state.SyncCommMember.PrepareSlot(ctx, eth2p0.Slot(slot.Slot))
})

// Submit sync committee message 1/3 into the slot.
vMockWrap(ctx, slot.Slot, slot.Epoch(), func(ctx context.Context, state vMockState) error {
vMockWrap(ctx, slot.Slot, func(ctx context.Context, state vMockState) error {
thirdDuration := slot.SlotDuration / 3
thirdTime := slot.Time.Add(thirdDuration)

Expand All @@ -86,7 +87,7 @@ func wireValidatorMock(conf Config, pubshares []eth2p0.BLSPubKey, sched core.Sch

// Handle duties when triggered.
sched.SubscribeDuties(func(ctx context.Context, duty core.Duty, _ core.DutyDefinitionSet) error {
vMockWrap(ctx, duty.Slot, 0, func(ctx context.Context, state vMockState) error {
vMockWrap(ctx, duty.Slot, func(ctx context.Context, state vMockState) error {
return handleVMockDuty(ctx, duty, state.Eth2Cl, state.SignFunc, pubshares, state.Attester, state.SyncCommMember)
})

Expand All @@ -96,7 +97,7 @@ func wireValidatorMock(conf Config, pubshares []eth2p0.BLSPubKey, sched core.Sch
go func() {
// TODO(corver): Improve registrations to use lock file and trigger on epoch transitions.
for registration := range conf.TestConfig.BuilderRegistration {
vMockWrap(context.Background(), 0, 0, func(ctx context.Context, state vMockState) error {
vMockWrap(context.Background(), 0, func(ctx context.Context, state vMockState) error {
return validatormock.Register(ctx, state.Eth2Cl, state.SignFunc, registration, pubshares[0])
})
}
Expand All @@ -117,7 +118,7 @@ type vMockState struct {
type vMockCallback func(context.Context, vMockState) error

// newVMockWrapper returns a stateful validator mock wrapper function.
func newVMockWrapper(conf Config, pubshares []eth2p0.BLSPubKey) (func(ctx context.Context, slot int64, epoch int64, callback vMockCallback), error) {
func newVMockWrapper(conf Config, pubshares []eth2p0.BLSPubKey) (func(ctx context.Context, slot int64, callback vMockCallback), error) {
// Immutable state and providers.
signFunc, err := newVMockSigner(conf, pubshares)
if err != nil {
Expand All @@ -133,7 +134,7 @@ func newVMockWrapper(conf Config, pubshares []eth2p0.BLSPubKey) (func(ctx contex
syncCommMem = new(validatormock.SyncCommMember)
)

return func(ctx context.Context, slot, epoch int64, fn vMockCallback) {
return func(ctx context.Context, slot int64, fn vMockCallback) {
mu.Lock()
defer mu.Unlock()

Expand All @@ -145,12 +146,18 @@ func newVMockWrapper(conf Config, pubshares []eth2p0.BLSPubKey) (func(ctx contex
return
}

epoch, err := eth2util.EpochFromSlot(ctx, eth2Cl, eth2p0.Slot(slot))
if err != nil {
log.Error(ctx, "Epoch from slot", err)
return
}

// Create new slot attester on new slots
if slot != 0 && attester.Slot() != eth2p0.Slot(slot) {
attester = validatormock.NewSlotAttester(eth2Cl, eth2p0.Slot(slot), signFunc, pubshares)
}
if epoch != 0 && syncCommMem.Epoch() != eth2p0.Epoch(epoch) {
syncCommMem = validatormock.NewSyncCommMember(eth2Cl, eth2p0.Epoch(epoch), signFunc, pubshares)
if epoch != 0 && syncCommMem.Epoch() != epoch {
syncCommMem = validatormock.NewSyncCommMember(eth2Cl, epoch, signFunc, pubshares)
}

state := vMockState{
Expand Down
Loading