Skip to content
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

Query genesis transactions #4788

Merged
merged 18 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implemet supply interface
  • Loading branch information
alexanderbez committed Jul 31, 2019
commit 286789dbd51190ea5e723f72a99e85109e441ded
3 changes: 1 addition & 2 deletions x/staking/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
stakingexported "github.com/cosmos/cosmos-sdk/x/staking/exported"
"github.com/cosmos/cosmos-sdk/x/supply"
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
)

Expand All @@ -21,7 +20,7 @@ type AccountKeeper interface {

// SupplyKeeper defines the expected supply Keeper (noalias)
type SupplyKeeper interface {
GetSupply(ctx sdk.Context) supply.Supply
GetSupply(ctx sdk.Context) supplyexported.SupplyI

GetModuleAddress(name string) sdk.AccAddress
GetModuleAccount(ctx sdk.Context, moduleName string) supplyexported.ModuleAccountI
Expand Down
19 changes: 18 additions & 1 deletion x/supply/exported/exported.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package exported

import "github.com/cosmos/cosmos-sdk/x/auth/exported"
import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/x/auth/exported"
)

// ModuleAccountI defines an account interface for modules that hold tokens in an escrow
type ModuleAccountI interface {
exported.Account

GetName() string
GetPermissions() []string
HasPermission(string) bool
}

// TODO: ...
type SupplyI interface {
GetTotal() sdk.Coins
SetTotal(total sdk.Coins) SupplyI

Inflate(amount sdk.Coins) SupplyI
Deflate(amount sdk.Coins) SupplyI

String() string
ValidateBasic() error
}
6 changes: 4 additions & 2 deletions x/supply/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
// CONTRACT: all types of accounts must have been already initialized/created
func InitGenesis(ctx sdk.Context, keeper Keeper, ak types.AccountKeeper, data GenesisState) {
// manually set the total supply based on accounts if not provided
if data.Supply.Total.Empty() {
if data.Supply.GetTotal().Empty() {
var totalSupply sdk.Coins
ak.IterateAccounts(ctx,
func(acc authexported.Account) (stop bool) {
totalSupply = totalSupply.Add(acc.GetCoins())
return false
},
)
data.Supply.Total = totalSupply

data.Supply = data.Supply.SetTotal(totalSupply)
}

keeper.SetSupply(ctx, data.Supply)
}

Expand Down
4 changes: 2 additions & 2 deletions x/supply/internal/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func TotalSupply(k Keeper) sdk.Invariant {
return false
})

broken := !expectedTotal.IsEqual(supply.Total)
broken := !expectedTotal.IsEqual(supply.GetTotal())

return sdk.FormatInvariant(types.ModuleName, "total supply",
fmt.Sprintf(
"\tsum of accounts coins: %v\n"+
"\tsupply.Total: %v\n",
expectedTotal, supply.Total), broken)
expectedTotal, supply.GetTotal()), broken)
}
}
8 changes: 3 additions & 5 deletions x/supply/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ type Keeper struct {
}

// NewKeeper creates a new Keeper instance
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ak types.AccountKeeper, bk types.BankKeeper,
codespace sdk.CodespaceType, maccPerms map[string][]string) Keeper {

func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ak types.AccountKeeper, bk types.BankKeeper, maccPerms map[string][]string) Keeper {
// set the addresses
permAddrs := make(map[string]types.PermissionsForAddress)
for name, perms := range maccPerms {
Expand All @@ -45,7 +43,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
}

// GetSupply retrieves the Supply from store
func (k Keeper) GetSupply(ctx sdk.Context) (supply types.Supply) {
func (k Keeper) GetSupply(ctx sdk.Context) (supply exported.SupplyI) {
store := ctx.KVStore(k.storeKey)
b := store.Get(SupplyKey)
if b == nil {
Expand All @@ -56,7 +54,7 @@ func (k Keeper) GetSupply(ctx sdk.Context) (supply types.Supply) {
}

// SetSupply sets the Supply to store
func (k Keeper) SetSupply(ctx sdk.Context, supply types.Supply) {
func (k Keeper) SetSupply(ctx sdk.Context, supply exported.SupplyI) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(supply)
store.Set(SupplyKey, b)
Expand Down
7 changes: 5 additions & 2 deletions x/supply/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import (
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {

case types.QueryTotalSupply:
return queryTotalSupply(ctx, req, k)

case types.QuerySupplyOf:
return querySupplyOf(ctx, req, k)

default:
return nil, sdk.ErrUnknownRequest("unknown supply query endpoint")
}
Expand All @@ -32,7 +35,7 @@ func queryTotalSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte,
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}

totalSupply := k.GetSupply(ctx).Total
totalSupply := k.GetSupply(ctx).GetTotal()

start, end := client.Paginate(len(totalSupply), params.Page, params.Limit, 100)
if start < 0 || end < 0 {
Expand All @@ -57,7 +60,7 @@ func querySupplyOf(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sd
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}

supply := k.GetSupply(ctx).Total.AmountOf(params.Denom)
supply := k.GetSupply(ctx).GetTotal().AmountOf(params.Denom)

res, err := supply.MarshalJSON()
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions x/supply/internal/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package types

import (
"github.com/cosmos/cosmos-sdk/x/supply/exported"
)

// GenesisState is the supply state that must be provided at genesis.
type GenesisState struct {
Supply Supply `json:"supply" yaml:"supply"`
Supply exported.SupplyI `json:"supply" yaml:"supply"`
}

// NewGenesisState creates a new genesis state.
func NewGenesisState(supply Supply) GenesisState {
func NewGenesisState(supply exported.SupplyI) GenesisState {
return GenesisState{supply}
}

Expand Down
31 changes: 26 additions & 5 deletions x/supply/internal/types/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,51 @@ package types
import (
"fmt"

yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/supply/exported"
)

// Implements Delegation interface
var _ exported.SupplyI = Supply{}

// Supply represents a struct that passively keeps track of the total supply amounts in the network
type Supply struct {
Total sdk.Coins `json:"total" yaml:"total"` // total supply of tokens registered on the chain
}

// SetTotal sets the total supply.
func (supply Supply) SetTotal(total sdk.Coins) exported.SupplyI {
supply.Total = total
return supply
}

// GetTotal returns the supply total.
func (supply Supply) GetTotal() sdk.Coins {
return supply.Total
}

// NewSupply creates a new Supply instance
func NewSupply(total sdk.Coins) Supply { return Supply{total} }
func NewSupply(total sdk.Coins) exported.SupplyI {
return Supply{total}
}

// DefaultSupply creates an empty Supply
func DefaultSupply() Supply { return NewSupply(sdk.NewCoins()) }
func DefaultSupply() exported.SupplyI {
return NewSupply(sdk.NewCoins())
}

// Inflate adds coins to the total supply
func (supply *Supply) Inflate(amount sdk.Coins) {
func (supply Supply) Inflate(amount sdk.Coins) exported.SupplyI {
supply.Total = supply.Total.Add(amount)
return supply
}

// Deflate subtracts coins from the total supply
func (supply *Supply) Deflate(amount sdk.Coins) {
func (supply Supply) Deflate(amount sdk.Coins) exported.SupplyI {
supply.Total = supply.Total.Sub(amount)
return supply
}

// String returns a human readable string representation of a supplier.
Expand Down