cosmos-sdk/store
Ian Norden 1326fa2a7d
feat: ADR-038 Part 2: StreamingService interface, file writing implementation, and configuration (#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
☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >  -->

## Description

<!-- 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](https://github.com/cosmos/cosmos-sdk/pull/8012) state listening. This is rebased on top of the [first segment](https://github.com/cosmos/cosmos-sdk/pull/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
2021-10-24 21:37:37 +00:00
..
cache all: ensure b.ReportAllocs() in all the benchmarks (#8460) 2021-01-27 23:52:08 -08:00
cachekv fix!: store/cachekv: reduce growth factor for iterator ranging using binary searches (#10024) 2021-10-14 21:58:25 +00:00
cachemulti feat: ADR-038 Part 2: StreamingService interface, file writing implementation, and configuration (#8664) 2021-10-24 21:37:37 +00:00
dbadapter ADR-038 Part 1: WriteListener, listen.KVStore, MultiStore and KVStore updates (#8551) 2021-03-30 16:13:51 -04:00
gaskv perf: Remove more telemetry ops, update docs (#10334) 2021-10-11 08:23:42 +00:00
iavl feat!: support debug trace QueryResult (#9576) 2021-07-08 09:25:40 +00:00
internal store/internal: validate keys before calling ProofsFromMap (#9235) 2021-05-02 15:53:59 -07:00
listenkv codec: Rename codec and marshaler interfaces (#9226) 2021-04-29 10:46:22 +00:00
mem ADR-038 Part 1: WriteListener, listen.KVStore, MultiStore and KVStore updates (#8551) 2021-03-30 16:13:51 -04:00
prefix ADR-038 Part 1: WriteListener, listen.KVStore, MultiStore and KVStore updates (#8551) 2021-03-30 16:13:51 -04:00
rootmulti feat: ADR-040: Implement KV Store with decoupled storage and SMT (#9892) 2021-10-19 11:58:06 +00:00
streaming feat: ADR-038 Part 2: StreamingService interface, file writing implementation, and configuration (#8664) 2021-10-24 21:37:37 +00:00
tracekv ADR-038 Part 1: WriteListener, listen.KVStore, MultiStore and KVStore updates (#8551) 2021-03-30 16:13:51 -04:00
transient Merge PR #7265: Tendermint Block Pruning 2020-09-14 10:12:49 -04:00
types feat: ADR-040: Implement KV Store with decoupled storage and SMT (#9892) 2021-10-19 11:58:06 +00:00
v2 feat: ADR-040: Implement KV Store with decoupled storage and SMT (#9892) 2021-10-19 11:58:06 +00:00
README.md chore: add markdownlint to lint commands (#9353) 2021-05-27 15:31:04 +00:00
firstlast.go types: add kv type (#6897) 2020-07-30 14:53:02 +00:00
reexport.go Merge PR #6475: Pruning Refactor 2020-06-22 16:31:33 -04:00
store.go Merge PR #6475: Pruning Refactor 2020-06-22 16:31:33 -04:00

README.md

Store

CacheKV

cachekv.Store is a wrapper KVStore which provides buffered writing / cached reading functionalities over the underlying KVStore.

type Store struct {
    cache map[string]cValue
    parent types.KVStore
}

Get

Store.Get() checks Store.cache first in order to find if there is any cached value associated with the key. If the value exists, the function returns it. If not, the function calls Store.parent.Get(), sets the key-value pair to the Store.cache, and returns it.

Set

Store.Set() sets the key-value pair to the Store.cache. cValue has the field dirty bool which indicates whether the cached value is different from the underlying value. When Store.Set() cache new pair, the cValue.dirty is set true so when Store.Write() is called it can be written to the underlying store.

Iterator

Store.Iterator() have to traverse on both caches items and the original items. In Store.iterator(), two iterators are generated for each of them, and merged. memIterator is essentially a slice of the KVPairs, used for cached items. mergeIterator is a combination of two iterators, where traverse happens ordered on both iterators.

CacheMulti

cachemulti.Store is a wrapper MultiStore which provides buffered writing / cached reading functionalities over the underlying MutliStore

type Store struct {
    db types.CacheKVStore
    stores map[types.StoreKey] types.CacheWrap
}

cachemulti.Store branches all substores in its constructor and hold them in Store.stores. Store.GetKVStore() returns the store from Store.stores, and Store.Write() recursively calls CacheWrap.Write() on the substores.

DBAdapter

dbadapter.Store is a adapter for dbm.DB making it fulfilling the KVStore interface.

type Store struct {
    dbm.DB
}

dbadapter.Store embeds dbm.DB, so most of the KVStore interface functions are implemented. The other functions(mostly miscellaneous) are manually implemented.

IAVL

iavl.Store is a base-layer self-balancing merkle tree. It is guaranteed that

  1. Get & set operations are O(log n), where n is the number of elements in the tree
  2. Iteration efficiently returns the sorted elements within the range
  3. Each tree version is immutable and can be retrieved even after a commit(depending on the pruning settings)

Specification and implementation of IAVL tree can be found in [https://github.com/tendermint/iavl].

GasKV

gaskv.Store is a wrapper KVStore which provides gas consuming functionalities over the underlying KVStore.

type Store struct {
    gasMeter types.GasMeter
    gasConfig types.GasConfig
    parent types.KVStore
}

When each KVStore methods are called, gaskv.Store automatically consumes appropriate amount of gas depending on the Store.gasConfig.

Prefix

prefix.Store is a wrapper KVStore which provides automatic key-prefixing functionalities over the underlying KVStore.

type Store struct {
    parent types.KVStore
    prefix []byte
}

When Store.{Get, Set}() is called, the store forwards the call to its parent, with the key prefixed with the Store.prefix.

When Store.Iterator() is called, it does not simply prefix the Store.prefix, since it does not work as intended. In that case, some of the elements are traversed even they are not starting with the prefix.

RootMulti

rootmulti.Store is a base-layer MultiStore where multiple KVStore can be mounted on it and retrieved via object-capability keys. The keys are memory addresses, so it is impossible to forge the key unless an object is a valid owner(or a receiver) of the key, according to the object capability principles.

TraceKV

tracekv.Store is a wrapper KVStore which provides operation tracing functionalities over the underlying KVStore.

type Store struct {
    parent types.KVStore
    writer io.Writer
    context types.TraceContext
}

When each KVStore methods are called, tracekv.Store automatically logs traceOperation to the Store.writer.

type traceOperation struct {
    Operation operation
    Key string
    Value string
    Metadata map[string]interface{}
}

traceOperation.Metadata is filled with Store.context when it is not nil. TraceContext is a map[string]interface{}.

Transient

transient.Store is a base-layer KVStore which is automatically discarded at the end of the block.

type Store struct {
    dbadapter.Store
}

Store.Store is a dbadapter.Store with a dbm.NewMemDB(). All KVStore methods are reused. When Store.Commit() is called, new dbadapter.Store is assigned, discarding previous reference and making it garbage collected.