Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 83f70fb

Browse files
i-nordenyihuang
authored andcommittedSep 20, 2022
feat: ADR-038 Part 2: StreamingService interface, file writing implementation, and configuration (cosmos#8664)
<!-- < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ☺ v ✰ Thanks for creating a PR! ✰ v Before smashing the submit button please review the checkboxes. v If a checkbox is n/a - please still include it but + a little note why ☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > --> <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> Hello 👋 this PR introduces the second stage of changes to support [ADR-038](cosmos#8012) state listening. This is rebased on top of the [first segment](cosmos#8551), which introduces the low level changes to the MultiStore and KVStore interfaces and implementations, the new WriteListener types, and the new listen.KVStore type. In this segment we introduce the StreamingService interface, an implementation that writes out to files, and it's integration and configuration at the BaseApp level. The idea was to have the first segment reviewed independently first but if people think it is easier/more time efficient to review both at the same time then we could start here. Thanks! This work is towards satisfying [ADR-038](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-038-state-listening.md) --- Before we can merge this PR, please make sure that all the following items have been checked off. If any of the checklist items are not applicable, please leave them but write a little note why. - [x] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [x] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules/structure.md). - [x] Wrote unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] Updated relevant documentation (`docs/`) or specification (`x/<module>/spec/`) - [x] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code). - [x] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md` - [x] Re-reviewed `Files changed` in the Github PR explorer - [x] Review `Codecov Report` in the comment section below once CI passes
1 parent 7c14f51 commit 83f70fb

16 files changed

+1244
-91
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4343
### Improvements
4444

4545
* (store) [\#8012](https://github.com/cosmos/cosmos-sdk/pull/8012) Implementation of ADR-038 WriteListener and listen.KVStore
46+
* (store) [\#8664](https://github.com/cosmos/cosmos-sdk/pull/8664) Implementation of ADR-038 file StreamingService
4647

4748
## [v0.42.11](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.42.11) - 2021-12-07
4849

‎baseapp/abci.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
194194
}
195195
// set the signed validators for addition to context in deliverTx
196196
app.voteInfos = req.LastCommitInfo.GetVotes()
197+
198+
// call the hooks with the BeginBlock messages
199+
for _, streamingListener := range app.abciListeners {
200+
if err := streamingListener.ListenBeginBlock(app.deliverState.ctx, req, res); err != nil {
201+
app.logger.Error("BeginBlock listening hook failed", "height", req.Header.Height, "err", err)
202+
}
203+
}
204+
197205
return res
198206
}
199207

@@ -214,6 +222,13 @@ func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBloc
214222
res.ConsensusParamUpdates = cp
215223
}
216224

225+
// call the streaming service hooks with the EndBlock messages
226+
for _, streamingListener := range app.abciListeners {
227+
if err := streamingListener.ListenEndBlock(app.deliverState.ctx, req, res); err != nil {
228+
app.logger.Error("EndBlock listening hook failed", "height", req.Height, "err", err)
229+
}
230+
}
231+
217232
return res
218233
}
219234

@@ -258,9 +273,17 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
258273
// Otherwise, the ResponseDeliverTx will contain releveant error information.
259274
// Regardless of tx execution outcome, the ResponseDeliverTx will contain relevant
260275
// gas execution context.
261-
func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
276+
func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) (res abci.ResponseDeliverTx) {
262277
defer telemetry.MeasureSince(time.Now(), "abci", "deliver_tx")
263278

279+
defer func() {
280+
for _, streamingListener := range app.abciListeners {
281+
if err := streamingListener.ListenDeliverTx(app.deliverState.ctx, req, res); err != nil {
282+
app.logger.Error("DeliverTx listening hook failed", "err", err)
283+
}
284+
}
285+
}()
286+
264287
gInfo := sdk.GasInfo{}
265288
resultStr := "successful"
266289

‎baseapp/baseapp.go

+4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ type BaseApp struct { // nolint: maligned
128128
// indexEvents defines the set of events in the form {eventType}.{attributeKey},
129129
// which informs Tendermint what to index. If empty, all events will be indexed.
130130
indexEvents map[string]struct{}
131+
132+
// abciListeners for hooking into the ABCI message processing of the BaseApp
133+
// and exposing the requests and responses to external consumers
134+
abciListeners []ABCIListener
131135
}
132136

133137
// NewBaseApp returns a reference to an initialized BaseApp. It accepts a

‎baseapp/options.go

+11
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,14 @@ func (app *BaseApp) SetInterfaceRegistry(registry types.InterfaceRegistry) {
233233
app.grpcQueryRouter.SetInterfaceRegistry(registry)
234234
app.msgServiceRouter.SetInterfaceRegistry(registry)
235235
}
236+
237+
// SetStreamingService is used to set a streaming service into the BaseApp hooks and load the listeners into the multistore
238+
func (app *BaseApp) SetStreamingService(s StreamingService) {
239+
// add the listeners for each StoreKey
240+
for key, lis := range s.Listeners() {
241+
app.cms.AddListeners(key, lis)
242+
}
243+
// register the StreamingService within the BaseApp
244+
// BaseApp will pass BeginBlock, DeliverTx, and EndBlock requests and responses to the streaming services to update their ABCI context
245+
app.abciListeners = append(app.abciListeners, s)
246+
}

‎baseapp/streaming.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package baseapp
2+
3+
import (
4+
"io"
5+
"sync"
6+
7+
abci "github.com/tendermint/tendermint/abci/types"
8+
9+
store "github.com/cosmos/cosmos-sdk/store/types"
10+
"github.com/cosmos/cosmos-sdk/types"
11+
)
12+
13+
// ABCIListener interface used to hook into the ABCI message processing of the BaseApp
14+
type ABCIListener interface {
15+
// ListenBeginBlock updates the streaming service with the latest BeginBlock messages
16+
ListenBeginBlock(ctx types.Context, req abci.RequestBeginBlock, res abci.ResponseBeginBlock) error
17+
// ListenEndBlock updates the steaming service with the latest EndBlock messages
18+
ListenEndBlock(ctx types.Context, req abci.RequestEndBlock, res abci.ResponseEndBlock) error
19+
// ListenDeliverTx updates the steaming service with the latest DeliverTx messages
20+
ListenDeliverTx(ctx types.Context, req abci.RequestDeliverTx, res abci.ResponseDeliverTx) error
21+
}
22+
23+
// StreamingService interface for registering WriteListeners with the BaseApp and updating the service with the ABCI messages using the hooks
24+
type StreamingService interface {
25+
// Stream is the streaming service loop, awaits kv pairs and writes them to some destination stream or file
26+
Stream(wg *sync.WaitGroup) error
27+
// Listeners returns the streaming service's listeners for the BaseApp to register
28+
Listeners() map[store.StoreKey][]store.WriteListener
29+
// ABCIListener interface for hooking into the ABCI messages from inside the BaseApp
30+
ABCIListener
31+
// Closer interface
32+
io.Closer
33+
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.