-
Notifications
You must be signed in to change notification settings - Fork 193
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
refactor(perp): use collections for state management #952
Merged
+593
−865
Merged
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e351ff8
add: indexed map
0478f92
Merge branch 'master' into mercilex/perp-collections
c158021
change: use IndexedMap for positions
75c1c51
add: query trader positions + lint
2f63a47
add: query trader pos tests
4354888
chore: lint
1f89e7f
chore: docs
52b7680
chore: CHANGELOG.md
fc5035a
add: move pairs metadata to collections
e3bf3a9
change: move prepaid bad debt to collections
13fcb43
chore: lint
7049c52
chore: update CHANGELOG.md
e0276cd
chore: docs
ac872bb
change: refactor collections
62dc83f
fix: remove double iteration
6799185
Merge branch 'master' into mercilex/perp-collections
matthiasmatt 9633626
change: rename search to match + docs
64af81c
change: simplify pr
7dc1aaa
change: rename BadDebt->PrepaidBadDebt
33cd23c
change: make genesis fields be non-nullable
30ac985
change: move state functions to appropriate files and add bad debt tests
00a90f3
chore lint
bfaf083
change: BadDebt->PrepaidBadDebt
372fbbe
Merge branch 'master' into mercilex/perp-collections
testinginprod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package keys | ||
|
||
// Bound defines a key bound. | ||
type Bound[K Key] struct { | ||
value K // value is the concrete key. | ||
inclusive bool // inclusive defines if the key bound should include or not the provided value. | ||
} | ||
|
||
// Inclusive creates a key Bound which is inclusive, | ||
// which means the provided key will be included | ||
// in the key range (if present). | ||
func Inclusive[K Key](k K) Bound[K] { | ||
return Bound[K]{ | ||
value: k, | ||
inclusive: true, | ||
} | ||
} | ||
|
||
// Exclusive creates a key Bound which is exclusive, | ||
// which means the provided key will be excluded from | ||
// the key range. | ||
func Exclusive[K Key](k K) Bound[K] { | ||
return Bound[K]{ | ||
value: k, | ||
inclusive: false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,105 @@ | ||
package keys | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// IteratorFromRange returns a sdk.Iterator given the range | ||
// and the sdk.KVStore to apply the iterator to. | ||
// prefixBytes MUST not be mutated. | ||
func IteratorFromRange[K Key](store sdk.KVStore, r Range[K]) (iter sdk.Iterator, prefixBytes []byte) { | ||
pfx, start, end, order := r.RangeValues() | ||
if pfx != nil { | ||
prefixBytes = (*pfx).KeyBytes() | ||
store = prefix.NewStore(store, prefixBytes) | ||
} | ||
var startBytes []byte // default is nil | ||
if start != nil { | ||
startBytes = start.value.KeyBytes() | ||
// iterators are inclusive at start by default | ||
// so if we want to make the iteration exclusive | ||
// we extend by one byte. | ||
if !start.inclusive { | ||
startBytes = extendOneByte(startBytes) | ||
} | ||
} | ||
var endBytes []byte // default is nil | ||
if end != nil { | ||
endBytes = end.value.KeyBytes() | ||
// iterators are exclusive at end by default | ||
// so if we want to make the iteration | ||
// inclusive we need to extend by one byte. | ||
if end.inclusive { | ||
endBytes = extendOneByte(endBytes) | ||
} | ||
} | ||
|
||
switch order { | ||
case OrderAscending: | ||
return store.Iterator(startBytes, endBytes), prefixBytes | ||
case OrderDescending: | ||
return store.ReverseIterator(startBytes, endBytes), prefixBytes | ||
default: | ||
panic("unrecognized order") | ||
} | ||
} | ||
|
||
// Range defines an interface which instructs on how to iterate | ||
// over keys. | ||
type Range[K Key] interface { | ||
// RangeValues returns the range instructions. | ||
RangeValues() (prefix *K, start *Bound[K], end *Bound[K], order Order) | ||
} | ||
|
||
// NewRange returns a Range instance | ||
// which iterates over all keys in | ||
// ascending order. | ||
func NewRange[K Key]() Range[K] { | ||
return Range[K]{ | ||
func NewRange[K Key]() RawRange[K] { | ||
return RawRange[K]{ | ||
prefix: nil, | ||
start: nil, | ||
end: nil, | ||
order: OrderAscending, | ||
} | ||
} | ||
|
||
// Range defines a range of keys. | ||
type Range[K Key] struct { | ||
// RawRange is a Range implementer. | ||
type RawRange[K Key] struct { | ||
prefix *K | ||
start *Bound[K] | ||
end *Bound[K] | ||
order Order | ||
} | ||
|
||
// Prefix sets a fixed prefix for the key range. | ||
func (r Range[K]) Prefix(key K) Range[K] { | ||
func (r RawRange[K]) Prefix(key K) RawRange[K] { | ||
r.prefix = &key | ||
return r | ||
} | ||
|
||
// Start sets the start range of the key. | ||
func (r Range[K]) Start(bound Bound[K]) Range[K] { | ||
func (r RawRange[K]) Start(bound Bound[K]) RawRange[K] { | ||
r.start = &bound | ||
return r | ||
} | ||
|
||
// End sets the end range of the key. | ||
func (r Range[K]) End(bound Bound[K]) Range[K] { | ||
func (r RawRange[K]) End(bound Bound[K]) RawRange[K] { | ||
r.end = &bound | ||
return r | ||
} | ||
|
||
// Descending sets the key range to be inverse. | ||
func (r Range[K]) Descending() Range[K] { | ||
func (r RawRange[K]) Descending() RawRange[K] { | ||
r.order = OrderDescending | ||
return r | ||
} | ||
|
||
func (r Range[K]) Compile() (prefix []byte, start []byte, end []byte, order Order) { | ||
order = r.order | ||
if r.prefix != nil { | ||
prefix = (*r.prefix).KeyBytes() | ||
} | ||
if r.start != nil { | ||
start = r.compileStart() | ||
} | ||
if r.end != nil { | ||
end = r.compileEnd() | ||
} | ||
return | ||
} | ||
|
||
func (r Range[K]) compileStart() []byte { | ||
bytes := r.start.value.KeyBytes() | ||
// iterator start is inclusive by default | ||
if r.start.inclusive { | ||
return bytes | ||
} else { | ||
return extendOneByte(bytes) | ||
} | ||
} | ||
|
||
func (r Range[K]) compileEnd() []byte { | ||
bytes := r.end.value.KeyBytes() | ||
// iterator end is exclusive by default | ||
if !r.end.inclusive { | ||
return bytes | ||
} else { | ||
return extendOneByte(bytes) | ||
} | ||
func (r RawRange[K]) RangeValues() (prefix *K, start *Bound[K], end *Bound[K], order Order) { | ||
return r.prefix, r.start, r.end, r.order | ||
} | ||
|
||
func extendOneByte(b []byte) []byte { | ||
return append(b, 0) | ||
} | ||
|
||
type Bound[K Key] struct { | ||
value K | ||
inclusive bool | ||
} | ||
|
||
// Inclusive creates a key Bound which is inclusive. | ||
func Inclusive[K Key](k K) Bound[K] { | ||
return Bound[K]{ | ||
value: k, | ||
inclusive: true, | ||
} | ||
} | ||
|
||
// Exclusive creates a key Bound which is exclusive. | ||
func Exclusive[K Key](k K) Bound[K] { | ||
return Bound[K]{ | ||
value: k, | ||
inclusive: false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftovers from multi index... but we can keep this since it's going to be needed