Skip to content

Commit

Permalink
feat: epoched reward distribution part 1 - buffer
Browse files Browse the repository at this point in the history
First, buffer the rewards in the vbank module.
  • Loading branch information
JimLarson committed Jun 14, 2021
1 parent a74e826 commit e6bbb6d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
8 changes: 7 additions & 1 deletion golang/cosmos/x/vbank/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ the bridge to act upon the bank account. Updates to the bank account balance are
sent over the bridge to update the bank purse, whose balance can be queried
entirely at the ERTP level.

## Parameters

- `feeCollectorName`: the module which handles fee distribution to stakers.
- `feeEpochDuration`: the duration (in seconds) over which fees should be given to the fee collector.

## State

The Vbank module maintains no significant state, but will access stored state through the bank module.
Expand All @@ -29,8 +34,9 @@ The following fields are common to the Vbank messages:

Downcalls from JS to Cosmos (by `type`):
- `VBANK_GET_BALANCE (type, address, denom)`: gets the account balance in the given denomination from the bank. Returns the amount as a string.
- `VBANK_GRAB (type, sender, denom, amount)`: burns amount of denomination from account balance to reflect withdrawal from virtual purse. Returns a `VBANK_BALANCE_UPDATE` message restricted to the sender account and denomination.
- `VBANK_GIVE (type, recipeient, denom, amount)`: adds amount of denomination to account balance to reflect a deposit to the virtual purse. Returns a `VBANK_BALANCE_UPDATE` message restricted to the recipient account and denomination.
- `VBANK_GIVE_TO_FEE_COLLECTOR (type, denom, amount)`: stores rewards which will be gradually sent to the fee collector
- `VBANK_GRAB (type, sender, denom, amount)`: burns amount of denomination from account balance to reflect withdrawal from virtual purse. Returns a `VBANK_BALANCE_UPDATE` message restricted to the sender account and denomination.

Upcalls from Cosmos to JS: (by `type`)
- `VBANK_BALANCE_UPDATE (type, nonce, updated)`: inform virtual purse of change to the account balance (including a change initiated by VBANK_GRAB or VBANK_GIVE).
Expand Down
4 changes: 4 additions & 0 deletions golang/cosmos/x/vbank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (k Keeper) GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
return k.bankKeeper.GetAllBalances(ctx, addr)
}

func (k Keeper) StoreFeeCoins(ctx sdk.Context, amt sdk.Coins) error {
return k.bankKeeper.MintCoins(ctx, types.ModuleName, amt)
}

func (k Keeper) SendCoinsToFeeCollector(ctx sdk.Context, amt sdk.Coins) error {
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, amt); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions golang/cosmos/x/vbank/vbank.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ func (ch portHandler) Receive(ctx *vm.ControllerContext, str string) (ret string
return "", fmt.Errorf("cannot convert %s to int", msg.Amount)
}
coins := sdk.NewCoins(sdk.NewCoin(msg.Denom, value))
if err := keeper.SendCoinsToFeeCollector(ctx.Context, coins); err != nil {
return "", fmt.Errorf("cannot give %s coins: %w", coins.Sort().String(), err)
if err := keeper.StoreFeeCoins(ctx.Context, coins); err != nil {
return "", fmt.Errorf("cannot store fee %s coins: %w", coins.Sort().String(), err)
}
if err != nil {
return "", err
Expand Down
26 changes: 26 additions & 0 deletions golang/cosmos/x/vbank/vbank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,32 @@ func Test_Receive_Give(t *testing.T) {
}
}

func Test_Receive_GiveToFeeCollector(t *testing.T) {
bank := &mockBank{}
keeper := makeTestKeeper(bank)
ch := NewPortHandler(AppModule{}, keeper)
sdkCtx := sdk.NewContext(nil, tmproto.Header{}, false, log.NewNopLogger())
ctx := &vm.ControllerContext{Context: sdkCtx}

ret, err := ch.Receive(ctx, `{
"type": "VBANK_GIVE_TO_FEE_COLLECTOR",
"amount": "1000",
"denom": "urun"
}`)
if err != nil {
t.Fatalf("got error = %v", err)
}
if ret != `true` {
t.Errorf("got %v, want \"true\"", ret)
}
wantCalls := []string{
"MintCoins vbank 1000urun",
}
if !reflect.DeepEqual(bank.calls, wantCalls) {
t.Errorf("got calls %v, want %v", bank.calls, wantCalls)
}
}

func Test_Receive_Grab(t *testing.T) {
bank := &mockBank{balance: map[string]sdk.Coin{
addr1: sdk.NewInt64Coin("ubld", 1000),
Expand Down

0 comments on commit e6bbb6d

Please sign in to comment.