github.com/aakash4dev/cometbft@v0.38.2/proxy/app_conn.go (about) 1 package proxy 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/go-kit/kit/metrics" 8 9 abcicli "github.com/aakash4dev/cometbft/abci/client" 10 "github.com/aakash4dev/cometbft/abci/types" 11 ) 12 13 //go:generate ../scripts/mockery_generate.sh AppConnConsensus|AppConnMempool|AppConnQuery|AppConnSnapshot 14 15 //---------------------------------------------------------------------------------------- 16 // Enforce which abci msgs can be sent on a connection at the type level 17 18 type AppConnConsensus interface { 19 Error() error 20 InitChain(context.Context, *types.RequestInitChain) (*types.ResponseInitChain, error) 21 PrepareProposal(context.Context, *types.RequestPrepareProposal) (*types.ResponsePrepareProposal, error) 22 ProcessProposal(context.Context, *types.RequestProcessProposal) (*types.ResponseProcessProposal, error) 23 ExtendVote(context.Context, *types.RequestExtendVote) (*types.ResponseExtendVote, error) 24 VerifyVoteExtension(context.Context, *types.RequestVerifyVoteExtension) (*types.ResponseVerifyVoteExtension, error) 25 FinalizeBlock(context.Context, *types.RequestFinalizeBlock) (*types.ResponseFinalizeBlock, error) 26 Commit(context.Context) (*types.ResponseCommit, error) 27 } 28 29 type AppConnMempool interface { 30 SetResponseCallback(abcicli.Callback) 31 Error() error 32 33 CheckTx(context.Context, *types.RequestCheckTx) (*types.ResponseCheckTx, error) 34 CheckTxAsync(context.Context, *types.RequestCheckTx) (*abcicli.ReqRes, error) 35 Flush(context.Context) error 36 } 37 38 type AppConnQuery interface { 39 Error() error 40 41 Echo(context.Context, string) (*types.ResponseEcho, error) 42 Info(context.Context, *types.RequestInfo) (*types.ResponseInfo, error) 43 Query(context.Context, *types.RequestQuery) (*types.ResponseQuery, error) 44 } 45 46 type AppConnSnapshot interface { 47 Error() error 48 49 ListSnapshots(context.Context, *types.RequestListSnapshots) (*types.ResponseListSnapshots, error) 50 OfferSnapshot(context.Context, *types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error) 51 LoadSnapshotChunk(context.Context, *types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) 52 ApplySnapshotChunk(context.Context, *types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) 53 } 54 55 //----------------------------------------------------------------------------------------- 56 // Implements AppConnConsensus (subset of abcicli.Client) 57 58 type appConnConsensus struct { 59 metrics *Metrics 60 appConn abcicli.Client 61 } 62 63 var _ AppConnConsensus = (*appConnConsensus)(nil) 64 65 func NewAppConnConsensus(appConn abcicli.Client, metrics *Metrics) AppConnConsensus { 66 return &appConnConsensus{ 67 metrics: metrics, 68 appConn: appConn, 69 } 70 } 71 72 func (app *appConnConsensus) Error() error { 73 return app.appConn.Error() 74 } 75 76 func (app *appConnConsensus) InitChain(ctx context.Context, req *types.RequestInitChain) (*types.ResponseInitChain, error) { 77 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "init_chain", "type", "sync"))() 78 return app.appConn.InitChain(ctx, req) 79 } 80 81 func (app *appConnConsensus) PrepareProposal(ctx context.Context, 82 req *types.RequestPrepareProposal) (*types.ResponsePrepareProposal, error) { 83 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "prepare_proposal", "type", "sync"))() 84 return app.appConn.PrepareProposal(ctx, req) 85 } 86 87 func (app *appConnConsensus) ProcessProposal(ctx context.Context, req *types.RequestProcessProposal) (*types.ResponseProcessProposal, error) { 88 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "process_proposal", "type", "sync"))() 89 return app.appConn.ProcessProposal(ctx, req) 90 } 91 92 func (app *appConnConsensus) ExtendVote(ctx context.Context, req *types.RequestExtendVote) (*types.ResponseExtendVote, error) { 93 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "extend_vote", "type", "sync"))() 94 return app.appConn.ExtendVote(ctx, req) 95 } 96 97 func (app *appConnConsensus) VerifyVoteExtension(ctx context.Context, req *types.RequestVerifyVoteExtension) (*types.ResponseVerifyVoteExtension, error) { 98 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "verify_vote_extension", "type", "sync"))() 99 return app.appConn.VerifyVoteExtension(ctx, req) 100 } 101 102 func (app *appConnConsensus) FinalizeBlock(ctx context.Context, req *types.RequestFinalizeBlock) (*types.ResponseFinalizeBlock, error) { 103 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "finalize_block", "type", "sync"))() 104 return app.appConn.FinalizeBlock(ctx, req) 105 } 106 107 func (app *appConnConsensus) Commit(ctx context.Context) (*types.ResponseCommit, error) { 108 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "commit", "type", "sync"))() 109 return app.appConn.Commit(ctx, &types.RequestCommit{}) 110 } 111 112 //------------------------------------------------ 113 // Implements AppConnMempool (subset of abcicli.Client) 114 115 type appConnMempool struct { 116 metrics *Metrics 117 appConn abcicli.Client 118 } 119 120 func NewAppConnMempool(appConn abcicli.Client, metrics *Metrics) AppConnMempool { 121 return &appConnMempool{ 122 metrics: metrics, 123 appConn: appConn, 124 } 125 } 126 127 func (app *appConnMempool) SetResponseCallback(cb abcicli.Callback) { 128 app.appConn.SetResponseCallback(cb) 129 } 130 131 func (app *appConnMempool) Error() error { 132 return app.appConn.Error() 133 } 134 135 func (app *appConnMempool) Flush(ctx context.Context) error { 136 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "flush", "type", "sync"))() 137 return app.appConn.Flush(ctx) 138 } 139 140 func (app *appConnMempool) CheckTx(ctx context.Context, req *types.RequestCheckTx) (*types.ResponseCheckTx, error) { 141 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "check_tx", "type", "sync"))() 142 return app.appConn.CheckTx(ctx, req) 143 } 144 145 func (app *appConnMempool) CheckTxAsync(ctx context.Context, req *types.RequestCheckTx) (*abcicli.ReqRes, error) { 146 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "check_tx", "type", "async"))() 147 return app.appConn.CheckTxAsync(ctx, req) 148 } 149 150 //------------------------------------------------ 151 // Implements AppConnQuery (subset of abcicli.Client) 152 153 type appConnQuery struct { 154 metrics *Metrics 155 appConn abcicli.Client 156 } 157 158 func NewAppConnQuery(appConn abcicli.Client, metrics *Metrics) AppConnQuery { 159 return &appConnQuery{ 160 metrics: metrics, 161 appConn: appConn, 162 } 163 } 164 165 func (app *appConnQuery) Error() error { 166 return app.appConn.Error() 167 } 168 169 func (app *appConnQuery) Echo(ctx context.Context, msg string) (*types.ResponseEcho, error) { 170 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "echo", "type", "sync"))() 171 return app.appConn.Echo(ctx, msg) 172 } 173 174 func (app *appConnQuery) Info(ctx context.Context, req *types.RequestInfo) (*types.ResponseInfo, error) { 175 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "info", "type", "sync"))() 176 return app.appConn.Info(ctx, req) 177 } 178 179 func (app *appConnQuery) Query(ctx context.Context, req *types.RequestQuery) (*types.ResponseQuery, error) { 180 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "query", "type", "sync"))() 181 return app.appConn.Query(ctx, req) 182 } 183 184 //------------------------------------------------ 185 // Implements AppConnSnapshot (subset of abcicli.Client) 186 187 type appConnSnapshot struct { 188 metrics *Metrics 189 appConn abcicli.Client 190 } 191 192 func NewAppConnSnapshot(appConn abcicli.Client, metrics *Metrics) AppConnSnapshot { 193 return &appConnSnapshot{ 194 metrics: metrics, 195 appConn: appConn, 196 } 197 } 198 199 func (app *appConnSnapshot) Error() error { 200 return app.appConn.Error() 201 } 202 203 func (app *appConnSnapshot) ListSnapshots(ctx context.Context, req *types.RequestListSnapshots) (*types.ResponseListSnapshots, error) { 204 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "list_snapshots", "type", "sync"))() 205 return app.appConn.ListSnapshots(ctx, req) 206 } 207 208 func (app *appConnSnapshot) OfferSnapshot(ctx context.Context, req *types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error) { 209 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "offer_snapshot", "type", "sync"))() 210 return app.appConn.OfferSnapshot(ctx, req) 211 } 212 213 func (app *appConnSnapshot) LoadSnapshotChunk(ctx context.Context, req *types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) { 214 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "load_snapshot_chunk", "type", "sync"))() 215 return app.appConn.LoadSnapshotChunk(ctx, req) 216 } 217 218 func (app *appConnSnapshot) ApplySnapshotChunk(ctx context.Context, req *types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) { 219 defer addTimeSample(app.metrics.MethodTimingSeconds.With("method", "apply_snapshot_chunk", "type", "sync"))() 220 return app.appConn.ApplySnapshotChunk(ctx, req) 221 } 222 223 // addTimeSample returns a function that, when called, adds an observation to m. 224 // The observation added to m is the number of seconds ellapsed since addTimeSample 225 // was initially called. addTimeSample is meant to be called in a defer to calculate 226 // the amount of time a function takes to complete. 227 func addTimeSample(m metrics.Histogram) func() { 228 start := time.Now() 229 return func() { m.Observe(time.Since(start).Seconds()) } 230 }