@@ -100,6 +100,9 @@ import (
100100 "github.com/spf13/cast"
101101 "google.golang.org/grpc"
102102
103+ // Project logging helper for consistent logs with context
104+ applog "github.com/dydxprotocol/v4-chain/protocol/lib/log"
105+
103106 // App
104107 appconstants "github.com/dydxprotocol/v4-chain/protocol/app/constants"
105108 "github.com/dydxprotocol/v4-chain/protocol/app/flags"
@@ -355,6 +358,9 @@ type App struct {
355358 VaultKeeper vaultmodulekeeper.Keeper
356359 // this line is used by starport scaffolding # stargate/app/keeperDeclaration
357360
361+ // Instrumentation: store wall-clock when BeginBlock starts for execution duration metrics.
362+ beginBlockStartTime time.Time
363+
358364 ModuleManager * module.Manager
359365 ModuleBasics module.BasicManager
360366
@@ -1816,20 +1822,47 @@ func (app *App) BeginBlocker(ctx sdk.Context) (sdk.BeginBlock, error) {
18161822 proposerAddr := sdk .ConsAddress (ctx .BlockHeader ().ProposerAddress )
18171823 middleware .Logger = ctx .Logger ().With ("proposer_cons_addr" , proposerAddr .String ())
18181824
1825+ // Record start time and compute consensus lag (header time to now) before execution.
1826+ app .beginBlockStartTime = time .Now ()
1827+ consensusLag := app .beginBlockStartTime .Sub (ctx .BlockTime ())
1828+ applog .InfoLog (
1829+ ctx ,
1830+ "PIERRICK: BeginBlock timing" ,
1831+ "height" , ctx .BlockHeight (),
1832+ "consensus_lag_ms" , consensusLag .Milliseconds (),
1833+ )
1834+
18191835 return app .ModuleManager .BeginBlock (ctx )
18201836}
18211837
18221838// EndBlocker application updates every end block
18231839func (app * App ) EndBlocker (ctx sdk.Context ) (sdk.EndBlock , error ) {
1824- // Measure the lag between current timestamp and the end blocker time stamp
1825- // as an indicator of whether the node is lagging behind.
1840+ // Measure total lag vs header time for tip heuristic.
18261841 metrics .ModuleMeasureSince (metrics .EndBlocker , metrics .EndBlockerLag , ctx .BlockTime ())
18271842
1843+ // Execution duration since BeginBlock start.
1844+ execDurationMs := int64 (0 )
1845+ if ! app .beginBlockStartTime .IsZero () {
1846+ execDurationMs = time .Since (app .beginBlockStartTime ).Milliseconds ()
1847+ }
1848+
1849+ // Total lag includes consensus delay + execution time.
1850+ totalLag := time .Since (ctx .BlockTime ())
1851+ const tipThresholdSeconds = 3
1852+ atTip := totalLag <= tipThresholdSeconds * time .Second
1853+
1854+ applog .InfoLog (
1855+ ctx ,
1856+ "PIERRICK: EndBlock timing" ,
1857+ "height" , ctx .BlockHeight (),
1858+ "exec_duration_ms" , execDurationMs ,
1859+ "total_lag_ms" , totalLag .Milliseconds (),
1860+ "at_tip" , atTip ,
1861+ )
1862+
18281863 ctx = ctx .WithExecMode (lib .ExecModeEndBlock )
18291864
18301865 // Reset the logger for middleware.
1831- // Note that the middleware is only used by `CheckTx` and `DeliverTx`, and not `EndBlocker`.
1832- // Panics from `EndBlocker` will not be logged by the middleware and will lead to consensus failures.
18331866 middleware .Logger = app .Logger ()
18341867
18351868 response , err := app .ModuleManager .EndBlock (ctx )
0 commit comments