-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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(gov)!: use collections for deposit state management #16127
Changes from 19 commits
dddd529
3d8306b
e559d41
41d8e73
6bb70e6
c503920
9bf4d8c
0cd34ed
8fc50d7
915618b
d291e99
6932323
0792749
a9cc7ba
f29f12e
4e53600
99066f3
b23e38d
6af4aa9
5191de7
4f9bea7
3ee570e
64bf12f
39c3c4a
a4fe45c
66b7866
730116e
3a04628
44906b0
abbe962
a69ed21
9aa0be4
21efcb5
c295c8f
7f8b066
667c4e3
2830f8a
72dd01b
d400342
98195ea
f5eb34c
b60262f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package keeper | |
|
||
import ( | ||
"context" | ||
"cosmossdk.io/collections" | ||
"errors" | ||
"fmt" | ||
|
||
"cosmossdk.io/core/store" | ||
|
@@ -256,9 +258,12 @@ func (k BaseKeeper) GetAllDenomMetaData(ctx context.Context) []types.Metadata { | |
// provides the metadata to a callback. If true is returned from the | ||
// callback, iteration is halted. | ||
func (k BaseKeeper) IterateAllDenomMetaData(ctx context.Context, cb func(types.Metadata) bool) { | ||
_ = k.BaseViewKeeper.DenomMetadata.Walk(ctx, nil, func(_ string, metadata types.Metadata) bool { | ||
return cb(metadata) | ||
err := k.BaseViewKeeper.DenomMetadata.Walk(ctx, nil, func(_ string, metadata types.Metadata) (stop bool, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adjusts to match the collections API breaking change. |
||
return cb(metadata), nil | ||
}) | ||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) { | ||
panic(err) | ||
} | ||
} | ||
|
||
// SetDenomMetaData sets the denominations metadata | ||
|
@@ -474,7 +479,10 @@ func (k BaseKeeper) trackUndelegation(ctx context.Context, addr sdk.AccAddress, | |
// with the balance of each coin. | ||
// The iteration stops if the callback returns true. | ||
func (k BaseViewKeeper) IterateTotalSupply(ctx context.Context, cb func(sdk.Coin) bool) { | ||
_ = k.Supply.Walk(ctx, nil, func(s string, m math.Int) bool { | ||
return cb(sdk.NewCoin(s, m)) | ||
err := k.Supply.Walk(ctx, nil, func(s string, m math.Int) (bool, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adjusts to match the collections API breaking change. |
||
return cb(sdk.NewCoin(s, m)), nil | ||
}) | ||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) { | ||
panic(err) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package keeper | |
import ( | ||
"context" | ||
"fmt" | ||
"github.com/pkg/errors" | ||
testinginprod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
"cosmossdk.io/collections" | ||
"cosmossdk.io/core/store" | ||
|
@@ -376,7 +377,12 @@ func (k BaseSendKeeper) DeleteSendEnabled(ctx context.Context, denoms ...string) | |
|
||
// IterateSendEnabledEntries iterates over all the SendEnabled entries. | ||
func (k BaseSendKeeper) IterateSendEnabledEntries(ctx context.Context, cb func(denom string, sendEnabled bool) bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adjusts to match the collections API breaking change. |
||
_ = k.SendEnabled.Walk(ctx, nil, cb) | ||
err := k.SendEnabled.Walk(ctx, nil, func(key string, value bool) (stop bool, err error) { | ||
return cb(key, value), nil | ||
}) | ||
if err != nil && errors.Is(err, collections.ErrInvalidIterator) { | ||
panic(err) | ||
} | ||
} | ||
|
||
// GetAllSendEnabledEntries gets all the SendEnabled entries that are stored. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,8 +157,8 @@ func (k BaseViewKeeper) GetBalance(ctx context.Context, addr sdk.AccAddress, den | |
// provides the token balance to a callback. If true is returned from the | ||
// callback, iteration is halted. | ||
func (k BaseViewKeeper) IterateAccountBalances(ctx context.Context, addr sdk.AccAddress, cb func(sdk.Coin) bool) { | ||
err := k.Balances.Walk(ctx, collections.NewPrefixedPairRange[sdk.AccAddress, string](addr), func(key collections.Pair[sdk.AccAddress, string], value math.Int) bool { | ||
return cb(sdk.NewCoin(key.K2(), value)) | ||
err := k.Balances.Walk(ctx, collections.NewPrefixedPairRange[sdk.AccAddress, string](addr), func(key collections.Pair[sdk.AccAddress, string], value math.Int) (stop bool, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adjusts to match the collections API breaking change. |
||
return cb(sdk.NewCoin(key.K2(), value)), nil | ||
}) | ||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) { // TODO(tip): is this the correct strategy | ||
panic(err) | ||
|
@@ -169,10 +169,10 @@ func (k BaseViewKeeper) IterateAccountBalances(ctx context.Context, addr sdk.Acc | |
// denominations that are provided to a callback. If true is returned from the | ||
// callback, iteration is halted. | ||
func (k BaseViewKeeper) IterateAllBalances(ctx context.Context, cb func(sdk.AccAddress, sdk.Coin) bool) { | ||
err := k.Balances.Walk(ctx, nil, func(key collections.Pair[sdk.AccAddress, string], value math.Int) bool { | ||
return cb(key.K1(), sdk.NewCoin(key.K2(), value)) | ||
err := k.Balances.Walk(ctx, nil, func(key collections.Pair[sdk.AccAddress, string], value math.Int) (stop bool, err error) { | ||
return cb(key.K1(), sdk.NewCoin(key.K2(), value)), nil | ||
}) | ||
if err != nil { | ||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) { | ||
panic(err) | ||
} | ||
} | ||
|
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.
added the possibility to make walkFunc error, this plays well with gov and generally is better to allow for a straightforward way to error during iterations and propagate errors back.