-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* udpate comments * basket invarinats * fix build * fix configurator initialization * normalize coins * update mocks * adding unit tests to basket invariants
- Loading branch information
1 parent
dbdf4ee
commit fa6616a
Showing
9 changed files
with
222 additions
and
21 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,98 @@ | ||
package basket | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
basketv1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" | ||
"github.com/regen-network/regen-ledger/types/math" | ||
"github.com/regen-network/regen-ledger/x/ecocredit" | ||
) | ||
|
||
func (k Keeper) RegisterInvariants(ir sdk.InvariantRegistry) { | ||
ir.RegisterRoute(ecocredit.ModuleName, "basket-supply", k.basketSupplyInvariant()) | ||
} | ||
|
||
func (k Keeper) basketSupplyInvariant() sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
goCtx := sdk.WrapSDKContext(ctx) | ||
|
||
bals, err := k.computeBasketBalances(goCtx) | ||
if err != nil { | ||
return err.Error(), true | ||
} | ||
return BasketSupplyInvariant(ctx, k.stateStore.BasketStore(), k.bankKeeper, bals) | ||
} | ||
} | ||
|
||
type bankSupplyStore interface { | ||
GetSupply(ctx sdk.Context, denom string) sdk.Coin | ||
} | ||
|
||
// BasketSupplyInvariant cross check the balance of baskets and bank | ||
func BasketSupplyInvariant(ctx sdk.Context, store basketv1.BasketStore, bank bankSupplyStore, basketBalances map[uint64]math.Dec) (string, bool) { | ||
goCtx := sdk.WrapSDKContext(ctx) | ||
|
||
bids := make([]uint64, len(basketBalances)) | ||
i := 0 | ||
for bid := range basketBalances { | ||
bids[i] = bid | ||
i++ | ||
} | ||
sort.Slice(bids, func(i, j int) bool { return bids[i] < bids[j] }) | ||
|
||
var inbalances []string | ||
for _, bid := range bids { | ||
bal := basketBalances[bid] | ||
balInt, err := bal.BigInt() | ||
if err != nil { | ||
return fmt.Sprintf("Can't convert Dec to big.Int, %v", err), true | ||
} | ||
b, err := store.Get(goCtx, bid) | ||
if err != nil { | ||
return fmt.Sprintf("Can't get basket %v: %v", bid, err), true | ||
} | ||
c := bank.GetSupply(ctx, b.BasketDenom) | ||
balSdkInt := sdk.NewIntFromBigInt(balInt) | ||
if !c.Amount.Equal(balSdkInt) { | ||
inbalances = append(inbalances, fmt.Sprintf("Basket denom %s is imbalanced, expected: %v, got %v", | ||
b.BasketDenom, balSdkInt, c.Amount)) | ||
} | ||
} | ||
if len(inbalances) != 0 { | ||
return strings.Join(inbalances, "\n"), true | ||
} | ||
return "", false | ||
} | ||
|
||
// computeBasketBalances returns a map from basket id to the total number of eco credits | ||
func (k Keeper) computeBasketBalances(ctx context.Context) (map[uint64]math.Dec, error) { | ||
it, err := k.stateStore.BasketBalanceStore().List(ctx, &basketv1.BasketBalancePrimaryKey{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't create basket balance iterator, %w", err) | ||
} | ||
balances := map[uint64]math.Dec{} | ||
for it.Next() { | ||
b, err := it.Value() | ||
if err != nil { | ||
return nil, fmt.Errorf("Can't get basket balance %w", err) | ||
} | ||
bal, err := math.NewDecFromString(b.Balance) | ||
if err != nil { | ||
return nil, fmt.Errorf("Can't decode balance %s as math.Dec: %w", b.Balance, err) | ||
} | ||
if a, ok := balances[b.BasketId]; ok { | ||
if a, err = a.Add(bal); err != nil { | ||
return nil, fmt.Errorf("Can't add balances: %w", err) | ||
} | ||
balances[b.BasketId] = a | ||
} else { | ||
balances[b.BasketId] = bal | ||
} | ||
} | ||
return balances, nil | ||
} |
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,84 @@ | ||
package basket_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
basketv1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1" | ||
"github.com/regen-network/regen-ledger/types/math" | ||
"github.com/regen-network/regen-ledger/x/ecocredit/server/basket" | ||
) | ||
|
||
type BasketWithSupply struct { | ||
supply int64 | ||
b basketv1.Basket | ||
} | ||
|
||
type BankSupplyMock map[string]sdk.Coin | ||
|
||
func (bs BankSupplyMock) GetSupply(_ sdk.Context, denom string) sdk.Coin { | ||
if c, ok := bs[denom]; ok { | ||
return c | ||
} | ||
return sdk.NewInt64Coin(denom, 0) | ||
} | ||
|
||
func TestBasketSupplyInvarint(t *testing.T) { | ||
require := require.New(t) | ||
s := setupBase(t) | ||
|
||
baskets := []BasketWithSupply{ | ||
{10, basketv1.Basket{BasketDenom: "bb1", Name: "b1"}}, | ||
{20, basketv1.Basket{BasketDenom: "bb2", Name: "b2"}}, | ||
} | ||
store := s.stateStore.BasketStore() | ||
basketBalances := map[uint64]math.Dec{} | ||
correctBalances := BankSupplyMock{} | ||
for _, b := range baskets { | ||
id, err := store.InsertReturningID(s.ctx, &b.b) | ||
require.NoError(err) | ||
basketBalances[id] = math.NewDecFromInt64(b.supply) | ||
correctBalances[b.b.BasketDenom] = sdk.NewInt64Coin(b.b.BasketDenom, b.supply) | ||
} | ||
|
||
tcs := []struct { | ||
name string | ||
bank BankSupplyMock | ||
msg string | ||
}{ | ||
{"no bank supply", | ||
BankSupplyMock{}, "imbalanced"}, | ||
{"partial bank supply", | ||
BankSupplyMock{"bb1": newCoin("bb1", 10)}, "bb2 is imbalanced"}, | ||
{"smaller bank supply", | ||
BankSupplyMock{"bb1": newCoin("bb1", 8)}, "bb1 is imbalanced"}, | ||
{"smaller bank supply2", | ||
BankSupplyMock{"bb1": newCoin("bb1", 10), "bb2": newCoin("bb2", 10)}, "bb2 is imbalanced"}, | ||
{"bigger bank supply", | ||
BankSupplyMock{"bb1": newCoin("bb1", 10), "bb2": newCoin("bb2", 30)}, "bb2 is imbalanced"}, | ||
|
||
{"all good", | ||
correctBalances, ""}, | ||
{"more denoms", | ||
BankSupplyMock{"bb1": newCoin("bb1", 10), "bb2": newCoin("bb2", 20), "other": newCoin("other", 100)}, ""}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc.bank.GetSupply(s.sdkCtx, "abc") | ||
|
||
msg, _ := basket.BasketSupplyInvariant(s.sdkCtx, store, tc.bank, basketBalances) | ||
if tc.msg != "" { | ||
require.Contains(msg, tc.msg, tc.name) | ||
} else { | ||
require.Empty(msg, tc.name) | ||
} | ||
} | ||
|
||
t.Log(baskets) | ||
} | ||
|
||
func newCoin(denom string, a int64) sdk.Coin { | ||
return sdk.NewInt64Coin(denom, a) | ||
} |
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