Skip to content

Commit c858f82

Browse files
authored
Merge branch 'master' into gastonponti/fix-decode-rlp
2 parents c4bf1b9 + ebe2377 commit c858f82

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

consensus/consensustest/mockprotocol.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,13 @@ func (e *MockEngine) Prepare(chain consensus.ChainHeaderReader, header *types.He
350350
if parent == nil {
351351
return consensus.ErrUnknownAncestor
352352
}
353+
354+
// Matches delay in consensus/istanbul/backend/engine.go:386 in (*Backend).Prepare
355+
delay := time.Until(time.Unix(int64(header.Time), 0))
356+
if delay > 0 {
357+
time.Sleep(delay)
358+
}
359+
353360
return nil
354361
}
355362

consensus/istanbul/backend/engine.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,16 @@ func (sb *Backend) Prepare(chain consensus.ChainHeaderReader, header *types.Head
383383
header.Time = nowTime
384384
}
385385

386-
// Record what the delay should be, but sleep in the miner, not the consensus engine.
386+
// Record what the delay should be and sleep if greater than 0.
387+
// TODO(victor): Sleep here was previously removed and added to the miner instead, that change
388+
// has been temporarily reverted until it can be reimplemented without causing fewer signatures
389+
// to be included by the block producer.
387390
delay := time.Until(time.Unix(int64(header.Time), 0))
388391
if delay < 0 {
389392
sb.sleepGauge.Update(0)
390393
} else {
391394
sb.sleepGauge.Update(delay.Nanoseconds())
395+
time.Sleep(delay)
392396
}
393397

394398
if err := writeEmptyIstanbulExtra(header); err != nil {

miner/worker.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ func (w *worker) constructAndSubmitNewBlock(ctx context.Context) {
246246
start := time.Now()
247247

248248
// Initialize the block.
249+
// Note: In the current implementation, this will sleep until the time of the next block.
249250
b, err := prepareBlock(w)
250251
defer func() {
251252
if b != nil {
@@ -258,15 +259,7 @@ func (w *worker) constructAndSubmitNewBlock(ctx context.Context) {
258259
}
259260
w.updatePendingBlock(b)
260261

261-
// TODO: worker based adaptive sleep with this delay
262-
// wait for the timestamp of header, use this to adjust the block period
263-
delay := time.Until(time.Unix(int64(b.header.Time), 0))
264-
select {
265-
case <-time.After(delay):
266-
case <-ctx.Done():
267-
return
268-
}
269-
262+
startConstruction := time.Now()
270263
err = b.selectAndApplyTransactions(ctx, w)
271264
if err != nil {
272265
log.Error("Failed to apply transactions to the block", "err", err)
@@ -286,7 +279,7 @@ func (w *worker) constructAndSubmitNewBlock(ctx context.Context) {
286279
// the proposer and the engine has already gotten and is verifying the proposal). See
287280
// https://github.com/celo-org/celo-blockchain/issues/1639#issuecomment-888611039
288281
// And we subtract the time we spent sleeping, since we want the time spent actually building the block.
289-
w.blockConstructGauge.Update(time.Since(start).Nanoseconds() - delay.Nanoseconds())
282+
w.blockConstructGauge.Update(time.Since(startConstruction).Nanoseconds())
290283

291284
if w.isRunning() {
292285
if w.fullTaskHook != nil {

0 commit comments

Comments
 (0)