Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu committed Dec 4, 2023
1 parent 16160b7 commit e10a052
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
20 changes: 5 additions & 15 deletions vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type builder struct {
blkManager blockexecutor.Manager

// This timer goes off when it is time for the next staker to add/leave
// the staking set. When it goes off, [maybeAdvanceTime()] is called,
// the staking set. When it goes off, [maybeIssueEmptyBlock()] is called,
// potentially triggering creation of a new block.
timer *timer.Timer
nextStakerChangeTimeLock sync.RWMutex
Expand All @@ -81,7 +81,7 @@ func New(
blkManager: blkManager,
}

builder.timer = timer.NewTimer(builder.maybeAdvanceTime)
builder.timer = timer.NewTimer(builder.maybeIssueEmptyBlock)

go txExecutorBackend.Ctx.Log.RecoverAndPanic(builder.timer.Dispatch)
return builder
Expand Down Expand Up @@ -112,22 +112,11 @@ func (b *builder) BuildBlock(context.Context) (snowman.Block, error) {
return nil, fmt.Errorf("%w: %s", state.ErrMissingParentState, preferredID)
}

timestamp, timeWasCapped, nextStakerChangeTime, err := txexecutor.NextBlockTime(preferredState, b.txExecutorBackend.Clk)
timestamp, timeWasCapped, err := txexecutor.NextBlockTime(preferredState, b.txExecutorBackend.Clk)
if err != nil {
return nil, fmt.Errorf("could not calculate next staker change time: %w", err)
}

waitTime := nextStakerChangeTime.Sub(timestamp)
ctx.Log.Debug("setting next scheduled event",
zap.Time("nextEventTime", nextStakerChangeTime),
zap.Duration("timeUntil", waitTime),
)

b.nextStakerChangeTimeLock.Lock()
b.nextStakerChangeTime = nextStakerChangeTime
b.nextStakerChangeTimeLock.Unlock()
b.timer.SetTimeoutIn(waitTime)

statelessBlk, err := buildBlock(
b,
preferredID,
Expand Down Expand Up @@ -201,7 +190,7 @@ func (b *builder) ResetBlockTimer() {
b.timer.SetTimeoutIn(waitTime)
}

func (b *builder) maybeAdvanceTime() {
func (b *builder) maybeIssueEmptyBlock() {
ctx := b.txExecutorBackend.Ctx

// Grabbing the lock here enforces that this function is not called mid-way
Expand All @@ -215,6 +204,7 @@ func (b *builder) maybeAdvanceTime() {
now := b.txExecutorBackend.Clk.Time()
if b.nextStakerChangeTime.After(now) {
// [nextStakerChangeTime] is in the future, no need to advance time.
b.ResetBlockTimer()
return
}

Expand Down
8 changes: 4 additions & 4 deletions vms/platformvm/txs/executor/tx_mempool_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (v *MempoolTxVerifier) standardBaseState() (state.Diff, error) {
return nil, err
}

nextBlkTime, _, _, err := NextBlockTime(state, v.Clk)
nextBlkTime, _, err := NextBlockTime(state, v.Clk)
if err != nil {
return nil, err
}
Expand All @@ -124,7 +124,7 @@ func (v *MempoolTxVerifier) standardBaseState() (state.Diff, error) {
return state, nil
}

func NextBlockTime(state state.Chain, clk *mockable.Clock) (time.Time, bool, time.Time, error) {
func NextBlockTime(state state.Chain, clk *mockable.Clock) (time.Time, bool, error) {
var (
timestamp = clk.Time()
parentTime = state.GetTimestamp()
Expand All @@ -136,7 +136,7 @@ func NextBlockTime(state state.Chain, clk *mockable.Clock) (time.Time, bool, tim

nextStakerChangeTime, err := GetNextStakerChangeTime(state)
if err != nil {
return time.Time{}, false, time.Time{}, fmt.Errorf("failed getting next staker change time: %w", err)
return time.Time{}, false, fmt.Errorf("failed getting next staker change time: %w", err)
}

// timeWasCapped means that [timestamp] was reduced to [nextStakerChangeTime]
Expand All @@ -145,5 +145,5 @@ func NextBlockTime(state state.Chain, clk *mockable.Clock) (time.Time, bool, tim
timestamp = nextStakerChangeTime
}
// [timestamp] = min(max(now, parentTime), nextStakerChangeTime)
return timestamp, timeWasCapped, nextStakerChangeTime, nil
return timestamp, timeWasCapped, nil
}

0 comments on commit e10a052

Please sign in to comment.