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

Cleanup storage #185

Merged
merged 18 commits into from
Jul 27, 2017
Merged

Cleanup storage #185

merged 18 commits into from
Jul 27, 2017

Conversation

ethanfrey
Copy link
Contributor

Lots of improvements:

  • Middleware to control what parts are committed and what rolledback upon error in DeliverTx (by default, nonce update and fee deduction happens).

  • Expose more power of IAVLTree, remove, lists and first/last in range

  • Improve caching mechanism to be adapted to the storage layer

app/store.go Outdated
// Expand the path fully
dbPath, err := filepath.Abs(dbName)
if err != nil {
panic(fmt.Sprintf("Invalid Database Name: %s", dbName))
Copy link
Collaborator

Choose a reason for hiding this comment

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

So for a package function, wouldn't it be better if instead we returned an error instead of surprisingly panicking in the code so

func NewStore(dbName string, cacheSize int, logger log.Logger) (*Store, error)

otherwise the function itself has no mention that it'll panic if we can't expand the path. IMHO if we want to panic, maybe make another function say

func MustMakeNewStore(dbName string, cacheSize int, logger log.Logger) *Store

and then from there one can panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree...
This code was adapted from another package. Basically all the parsing code copied.
There are way too many panics all over our codebase... and after fighting against them for a while I get lazy. Thanks for pushing me to do this properly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Gotcha, I've encountered this quite often too when adapting code from different packages. For sure, you've extra hands here and we'll fight these panics 🙏 #panicBusters

app/store.go Outdated
}

// CloseDB closes the database
// func (s *Store) CloseDB() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you mean to leave this commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was never really called anywhere, so I guess it can be removed. I'll delete it fully.

// SetChainID stores the chain id in the store
func (s *ChainState) SetChainID(store KVStore, chainID string) {
s.chainID = chainID
store.Set([]byte("base/chain_id"), []byte(chainID))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's lift this out of here and instead make it a variable

var baseChainIDKey = []byte("base/chain_id")
...
store.Set(baseChainIDKey, []byte(chainID))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good point

state/kvcache.go Outdated
e *list.Element // The KVCache.keys element
}

// NOTE: If store is nil, creates a new MemKVStore
Copy link
Collaborator

Choose a reason for hiding this comment

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

// NewKVCache creates a new KVCache from store.
// If store is nil, it creates a new MemKVStore.

state/kvstore.go Outdated
type MemKVStore struct {
m map[string][]byte
}

var _ SimpleDB = NewMemKVStore()
Copy link
Collaborator

Choose a reason for hiding this comment

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

var _ SimpleDB = (*MemKVStore)(nil)

state/kvstore.go Outdated
keys *list.List
logging bool
logLines []string
func (mkv *MemKVStore) Has(key []byte) (has bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a random question: whenever I see a key-value store immediately I imagine concurrent usage, would that apply for this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are correct, but since the whole blockchain is about serializing transactions, this doesn't apply. You can have two caches over one store (check and deliver), but in the end one will be discarded and there are never concurrent writes while you are reading.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Aye aye, thanks for letting me know, TIL.

state/merkle.go Outdated
merkle.Tree
}

var _ SimpleDB = &Bonsai{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

var _ SimpleDB = (*Bonsai)(nil)

state/merkle.go Outdated
stopAtCount := func(key []byte, value []byte) (stop bool) {
m := Model{key, value}
res = append(res, m)
// return false
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove this, please.

state/merkle.go Outdated
func (b *Bonsai) Commit(sub SimpleDB) error {
bb, ok := sub.(*Bonsai)
if !ok || (b.id != bb.id) {
return errors.New("Not a sub-transaction")
Copy link
Collaborator

Choose a reason for hiding this comment

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

can be lifted out and made a var so

return errNotASubTransaction

@ethanfrey ethanfrey changed the title WIP: Cleanup storage Cleanup storage Jul 27, 2017
app/store.go Outdated
err = wire.ReadBinaryBytes(eyesStateBytes, &chainState)
if err != nil {
logger.Error("error reading MerkleEyesState", "err", err)
panic(err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We forgot to remove this panic here :)

@odeke-em
Copy link
Collaborator

Thank you for the PR and for addressing my comments @ethanfrey! Just one nit left otherwise LGTM from a code standpoint.

Copy link
Collaborator

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

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

LGTM after removing the last panic mentioned previously.

@ethanfrey ethanfrey merged commit 761fb0d into unstable Jul 27, 2017
@ethanfrey ethanfrey deleted the feature/cleanup-state branch July 27, 2017 19:38
mattverse referenced this pull request in mattverse/cosmos-sdk Apr 20, 2022
fix: prevent panic in Coins.IsEqual
sbellem pushed a commit to initc3/cosmos-sdk that referenced this pull request Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants