Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ADR 040: New DB interface #9573

Merged
merged 10 commits into from
Aug 19, 2021
Merged

Conversation

roysc
Copy link
Contributor

@roysc roysc commented Jun 23, 2021

New DB interface to replace tm-db

This introduces a new interface to wrap the backend KV store DB while supporting versioning and ACID transactions.
Part of ADR-040 changes.

This has been revised to consist of only the interface types. All implementations and utilities will be in follow-up PRs.

Partially resolves: vulcanize#2


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules - N/A
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@github-actions github-actions bot added C:CLI C:Cosmovisor Issues and PR related to Cosmovisor C:Keys Keybase, KMS and HSMs C:Rosetta Issues and PR related to Rosetta C:Simulations C:x/auth C:x/authz C:x/bank C:x/distribution distribution module related C:x/evidence C:x/feegrant C:x/genutil genutil module issues C:x/gov C:x/slashing C:x/staking C:x/upgrade T: ADR An issue or PR relating to an architectural decision record Type: Build T: CI labels Jun 23, 2021
Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be rebased onto master please. Many changes arent relevant

@roysc roysc force-pushed the roysc/adr-040-db branch from 924f265 to 692241d Compare June 24, 2021 05:09
@github-actions github-actions bot removed C:Simulations C:x/slashing Type: Build C:Rosetta Issues and PR related to Rosetta C:x/auth C:x/feegrant C:Keys Keybase, KMS and HSMs C:x/distribution distribution module related C:x/genutil genutil module issues labels Jun 24, 2021
@roysc roysc force-pushed the roysc/adr-040-db branch from e663fb5 to 80f9e6a Compare August 13, 2021 16:21
@roysc
Copy link
Contributor Author

roysc commented Aug 13, 2021

I've added a DeleteVersions method to the connection - this will of course be needed for pruning.

// if err := itr.Error(); err != nil {
// ...
// }
type Iterator interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've written/used iterators with the pattern:

var iter Iterator

for iter.Next() {

}

if err := iter.Err(); err != nil {
    return err
}
if err := iter.Close(); err != nil {
    return err 
}

Which collapses Valid/Next,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine, the only odd thing about this is it requires the iterator to start in an invalid state

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 - had similar opinion about it above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking a bit more on this, it may not be ideal as it violates the single-responsibility principle. The interface is smaller but you can't check the iterator's state without mutating it.

That said, I have pushed the change to the interface.

// Empty keys are not valid.
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
// CONTRACT: start, end readonly []byte
ReverseIterator(start, end []byte) (Iterator, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my inclination is that you'd specify iteration direction as an option to the method that produes the iterator and not require duplicate methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a trivial change. Any other opinions on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong preference, but tend to have the opposite inclination 🤷‍♂️

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping the interface minimal is a desired design. However it shouldn't on a cost of overloading the functions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't have a strong opinion here, but we use different methods in the SDK currently. Being as we're only adding one parameter, I don't see why not.

@roysc
Copy link
Contributor Author

roysc commented Aug 17, 2021

How does this look @aaronc and @alexanderbez? I think the design changes @robert-zaremba and @tychoish brought up seem fine, but I don't have a strong opinion either way.

Copy link
Member

@aaronc aaronc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-approving - seems like most of the remaining changes are minor, so if you can work those out with @robert-zaremba we can move forward

@roysc roysc force-pushed the roysc/adr-040-db branch from 96640ef to 69e5a9a Compare August 18, 2021 11:29
@codecov
Copy link

codecov bot commented Aug 18, 2021

Codecov Report

Merging #9573 (5533111) into master (b92308e) will increase coverage by 0.00%.
The diff coverage is n/a.

❗ Current head 5533111 differs from pull request most recent head d0f8927. Consider uploading reports for the commit d0f8927 to get more accurate results
Impacted file tree graph

@@           Coverage Diff           @@
##           master    #9573   +/-   ##
=======================================
  Coverage   63.80%   63.80%           
=======================================
  Files         566      566           
  Lines       53362    53362           
=======================================
+ Hits        34046    34047    +1     
+ Misses      17397    17396    -1     
  Partials     1919     1919           
Impacted Files Coverage Δ
crypto/keys/internal/ecdsa/privkey.go 84.21% <0.00%> (+1.75%) ⬆️

db/types.go Outdated Show resolved Hide resolved
// Empty keys are not valid.
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
// CONTRACT: start, end readonly []byte
ReverseIterator(start, end []byte) (Iterator, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't have a strong opinion here, but we use different methods in the SDK currently. Being as we're only adding one parameter, I don't see why not.

roysc and others added 2 commits August 18, 2021 22:37
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Copy link
Collaborator

@robert-zaremba robert-zaremba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for merging Next and Valid.

@aaronc aaronc added the A:automerge Automatically merge PR once all prerequisites pass. label Aug 18, 2021
@roysc roysc force-pushed the roysc/adr-040-db branch from eef18d9 to dbdb1c9 Compare August 19, 2021 12:50
@tac0turtle
Copy link
Member

tac0turtle commented Aug 19, 2021

@roysc once you update to master the bot should be able to merge the PR automatically

@mergify mergify bot merged commit e3aec18 into cosmos:master Aug 19, 2021
@orijbot
Copy link

orijbot commented Aug 19, 2021

@roysc roysc deleted the roysc/adr-040-db branch August 20, 2021 18:20
mergify bot pushed a commit that referenced this pull request Aug 31, 2021
## Description

Implements an in-memory backend for the DB interface introduced by #9573 and specified by [ADR-040](https://github.com/cosmos/cosmos-sdk/blob/eb7d939f86c6cd7b4218492364cdda3f649f06b5/docs/architecture/adr-040-storage-and-smt-state-commitments.md). This expands on the [btree](https://pkg.go.dev/github.com/google/btree)-based [`MemDB`](https://github.com/tendermint/tm-db/tree/master/memdb) from `tm-db` by using copy-on-write clones to implement versioning.

Resolves: vulcanize#2

Will move out of draft once #9573 is merged and rebased on.

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) n/a
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
mergify bot pushed a commit that referenced this pull request Sep 15, 2021
## Description

Partially resolves: vulcanize#14

Implements a [BadgerDB](https://pkg.go.dev/github.com/dgraph-io/badger/v3)-based backend for the DB interface introduced by #9573 and specified by [ADR-040](https://github.com/cosmos/cosmos-sdk/blob/eb7d939f86c6cd7b4218492364cdda3f649f06b5/docs/architecture/adr-040-storage-and-smt-state-commitments.md). This uses Badger's "managed" mode for version management, and supports optimistically concurrent transactions (required one [patch](dgraph-io/badger#1716) upstream to fix handling of write conflicts).

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - n/a
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
mergify bot pushed a commit that referenced this pull request Oct 5, 2021
## Description

Partially resolves: vulcanize#14

Implements a [RocksDB](https://github.com/facebook/rocksdb)-based backend for the DB interface introduced by #9573 and specified by [ADR-040](https://github.com/cosmos/cosmos-sdk/blob/eb7d939f86c6cd7b4218492364cdda3f649f06b5/docs/architecture/adr-040-storage-and-smt-state-commitments.md). 
* Historical versioning is implemented with [Checkpoints](https://github.com/facebook/rocksdb/wiki/Checkpoints). 
* Uses `OptimisticTransactionDB` to allow concurrent transactions with write conflict detection. This depends on some additional CGo bindings - see tecbot/gorocksdb#216, facebook/rocksdb#8526. We'll need to replace the `gorocksdb` module until these are upstream.
---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - n/a
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge Automatically merge PR once all prerequisites pass.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Epic 2: Replace tm-db database interface with new cosmos-db
10 participants