CosmWasm / CosmWasm/cw-storage-plus

Prevent invalid implementation of `PrimaryKey` and `Prefixer` outside of storage-plus

Open
#10 9 comments 1 reaction 0 assignees View on GitHub
documentation
Dominant language
Rust
Stars
51
Forks
36
PR merge metrics
No merged PRs in 30d

Description

Implementing `PrimaryKey` and `Prefixer` outside of storage-plus doesn't seems scary at all (you just want to use your type as a key, why not to?), but there is an issue - if done improperly it may break things. Trivial example is making a type like:
```
struct FancyKeyType {
secondary: U32Key,
primary: U32Key,
};
```

With an `Ord` implementation which compares firstly on primary, then on secondary. Then when calling `.range(..)` on such key, I expect them being sorted with my `Ord` implementation, but they come in different one, because they are sorted basing on their internal binary representation (basically bitwise lexografical order is used). And even worse, when using `Bounds` on such key I would get random items.

I have 3 proposal to work it around:
* Making `PrimaryKey` and `Prefixer` `unsafe` - this would not prevent from implementing them externally, but it would bring attention to the reasons why they are marked as not safe to implement. In the documentation there should be an additional explanation about how keys are compared and how to properly implement them.
* Making `PrimaryKey` and `Prefixer` sealed traits which is trivial, but prevents implementing them outside the crate at all. (which might be useful for some custom keys when done correctly)
* Making internal comparisons using actual `Ord`, but it would require reversing conversions of key to `&[u8]`, so it is blocked by CosmWasm/cw-plus#198.

Contributor guide

No contributing guide indexed for this repository

Research direction

No file, test, or entry point is named. Read the implementations and documentation for the PrimaryKey and Prefixer traits, then compare the three proposals and determine which behavior should be specified or changed before defining completion.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.