Skip to content

Commit

Permalink
fix: Add supply offsets to genesis (#253) (#257)
Browse files Browse the repository at this point in the history
* Add supply offsets to genesis

* Fix comments

* Fix migration test

* Fix test

Co-authored-by: mattverse <mattpark1028@gmail.com>
(cherry picked from commit 46bb624)

# Conflicts:
#	proto/cosmos/gov/v1beta1/gov.proto

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
  • Loading branch information
mergify[bot] and ValarDragon committed Jun 7, 2022
1 parent 743adc9 commit 51108b6
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 37 deletions.
19 changes: 19 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
- [cosmos/bank/v1beta1/genesis.proto](#cosmos/bank/v1beta1/genesis.proto)
- [Balance](#cosmos.bank.v1beta1.Balance)
- [GenesisState](#cosmos.bank.v1beta1.GenesisState)
- [GenesisSupplyOffset](#cosmos.bank.v1beta1.GenesisSupplyOffset)

- [cosmos/bank/v1beta1/query.proto](#cosmos/bank/v1beta1/query.proto)
- [QueryAllBalancesRequest](#cosmos.bank.v1beta1.QueryAllBalancesRequest)
Expand Down Expand Up @@ -1702,6 +1703,24 @@ GenesisState defines the bank module's genesis state.
| `balances` | [Balance](#cosmos.bank.v1beta1.Balance) | repeated | balances is an array containing the balances of all the accounts. |
| `supply` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | supply represents the total supply. If it is left empty, then supply will be calculated based on the provided balances. Otherwise, it will be used to validate that the sum of the balances equals this amount. |
| `denom_metadata` | [Metadata](#cosmos.bank.v1beta1.Metadata) | repeated | denom_metadata defines the metadata of the differents coins. |
| `supply_offsets` | [GenesisSupplyOffset](#cosmos.bank.v1beta1.GenesisSupplyOffset) | repeated | supply_offsets defines the amount of supply offset. |






<a name="cosmos.bank.v1beta1.GenesisSupplyOffset"></a>

### GenesisSupplyOffset
GenesisSupplyOffset encodes the supply offsets, just for genesis.
The offsets are serialized directly by denom in state.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `denom` | [string](#string) | | Denom |
| `offset` | [string](#string) | | SupplyOffset |



Expand Down
17 changes: 17 additions & 0 deletions proto/cosmos/bank/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ message GenesisState {

// denom_metadata defines the metadata of the differents coins.
repeated Metadata denom_metadata = 4 [(gogoproto.moretags) = "yaml:\"denom_metadata\"", (gogoproto.nullable) = false];

// supply_offsets defines the amount of supply offset.
repeated GenesisSupplyOffset supply_offsets = 5
[(gogoproto.moretags) = "yaml:\"supply_offsets\"", (gogoproto.nullable) = false];
}

// Balance defines an account address and balance pair used in the bank module's
Expand All @@ -37,3 +41,16 @@ message Balance {
repeated cosmos.base.v1beta1.Coin coins = 2
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins", (gogoproto.nullable) = false];
}

// GenesisSupplyOffset encodes the supply offsets, just for genesis.
// The offsets are serialized directly by denom in state.
message GenesisSupplyOffset {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// Denom
string denom = 1;

// SupplyOffset
string offset = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
}
6 changes: 3 additions & 3 deletions proto/cosmos/base/snapshots/v1beta1/snapshot.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ message SnapshotStoreItem {

// SnapshotIAVLItem is an exported IAVL node.
message SnapshotIAVLItem {
bytes key = 1;
bytes value = 2;
bytes key = 1;
bytes value = 2;
// version is block height
int64 version = 3;
// height is depth of the tree.
int32 height = 4;
int32 height = 4;
}

// SnapshotExtensionMeta contains metadata about an external snapshotter.
Expand Down
15 changes: 15 additions & 0 deletions proto/cosmos/gov/v1beta1/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,18 @@ message TallyParams {
(gogoproto.moretags) = "yaml:\"veto_threshold\""
];
}
<<<<<<< HEAD
=======

// ProposalVotingPeriod defines custom voting periods for a unique governance
// proposal type.
message ProposalVotingPeriod {
string proposal_type = 1; // e.g. "cosmos.params.v1beta1.ParameterChangeProposal"
google.protobuf.Duration voting_period = 2 [
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true,
(gogoproto.jsontag) = "voting_period,omitempty",
(gogoproto.moretags) = "yaml:\"voting_period\""
];
}
>>>>>>> 46bb6247d (fix: Add supply offsets to genesis (#253))
4 changes: 2 additions & 2 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
})

// update total supply
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.GenesisSupplyOffset{})
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)

stateBytes, err := json.MarshalIndent(genesisState, "", " ")
Expand Down Expand Up @@ -180,7 +180,7 @@ func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...ba
totalSupply = totalSupply.Add(b.Coins...)
}

bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.GenesisSupplyOffset{})
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)

stateBytes, err := json.MarshalIndent(genesisState, "", " ")
Expand Down
5 changes: 5 additions & 0 deletions x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
for _, meta := range genState.DenomMetadata {
k.SetDenomMetaData(ctx, meta)
}

for _, supplyOffset := range genState.SupplyOffsets {
k.setSupplyOffset(ctx, supplyOffset.Denom, supplyOffset.Offset)
}
}

// ExportGenesis returns the bank module's genesis state.
Expand All @@ -53,5 +57,6 @@ func (k BaseKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
k.GetAccountsBalances(ctx),
totalSupply,
k.GetAllDenomMetaData(ctx),
k.getGenesisSupplyOffsets(ctx),
)
}
6 changes: 3 additions & 3 deletions x/bank/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ func (suite *IntegrationTestSuite) TestTotalSupply() {
}{
{
"calculation NOT matching genesis Supply field",
types.NewGenesisState(defaultGenesis.Params, balances, sdk.NewCoins(sdk.NewCoin("wrongcoin", sdk.NewInt(1))), defaultGenesis.DenomMetadata),
types.NewGenesisState(defaultGenesis.Params, balances, sdk.NewCoins(sdk.NewCoin("wrongcoin", sdk.NewInt(1))), defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets),
nil, true, "genesis supply is incorrect, expected 1wrongcoin, got 21barcoin,11foocoin",
},
{
"calculation matches genesis Supply field",
types.NewGenesisState(defaultGenesis.Params, balances, totalSupply, defaultGenesis.DenomMetadata),
types.NewGenesisState(defaultGenesis.Params, balances, totalSupply, defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets),
totalSupply, false, "",
},
{
"calculation is correct, empty genesis Supply field",
types.NewGenesisState(defaultGenesis.Params, balances, nil, defaultGenesis.DenomMetadata),
types.NewGenesisState(defaultGenesis.Params, balances, nil, defaultGenesis.DenomMetadata, defaultGenesis.SupplyOffsets),
totalSupply, false, "",
},
}
Expand Down
19 changes: 19 additions & 0 deletions x/bank/keeper/supply_offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,22 @@ func (k BaseViewKeeper) IterateTotalSupplyWithOffsets(ctx sdk.Context, cb func(s
}
}
}

// getGenesisSupplyOffsets returns supply offset for genesis, encoded with denom in state
func (k BaseViewKeeper) getGenesisSupplyOffsets(ctx sdk.Context) []types.GenesisSupplyOffset {
store := ctx.KVStore(k.storeKey)
supplyStore := prefix.NewStore(store, types.SupplyKey)

iterator := supplyStore.Iterator(nil, nil)
defer iterator.Close()

supplyOffsets := []types.GenesisSupplyOffset{}
for ; iterator.Valid(); iterator.Next() {
supplyOffset := types.GenesisSupplyOffset{
Denom: string(iterator.Key()),
Offset: k.GetSupplyOffset(ctx, string(iterator.Key())),
}
supplyOffsets = append(supplyOffsets, supplyOffset)
}
return supplyOffsets
}
2 changes: 1 addition & 1 deletion x/bank/legacy/v040/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMigrate(t *testing.T) {
}

migrated := v040bank.Migrate(bankGenState, authGenState, supplyGenState)
expected := `{"params":{"send_enabled":[],"default_send_enabled":true},"balances":[{"address":"cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u","coins":[{"denom":"stake","amount":"50"}]},{"address":"cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74","coins":[{"denom":"stake","amount":"50"}]}],"supply":[{"denom":"stake","amount":"1000"}],"denom_metadata":[]}`
expected := `{"params":{"send_enabled":[],"default_send_enabled":true},"balances":[{"address":"cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u","coins":[{"denom":"stake","amount":"50"}]},{"address":"cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74","coins":[{"denom":"stake","amount":"50"}]}],"supply":[{"denom":"stake","amount":"1000"}],"denom_metadata":[],"supply_offsets":[]}`

bz, err := clientCtx.Codec.MarshalJSON(migrated)
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion x/bank/legacy/v043/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func TestMigrateJSON(t *testing.T) {
"amount": "10",
"denom": "foo"
}
]
],
"supply_offsets": []
}`

require.Equal(t, expected, string(indentedBz))
Expand Down
6 changes: 4 additions & 2 deletions x/bank/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,20 @@ func (gs GenesisState) Validate() error {
}

// NewGenesisState creates a new genesis state.
func NewGenesisState(params Params, balances []Balance, supply sdk.Coins, denomMetaData []Metadata) *GenesisState {
func NewGenesisState(params Params, balances []Balance, supply sdk.Coins,
denomMetaData []Metadata, supplyOffsets []GenesisSupplyOffset) *GenesisState {
return &GenesisState{
Params: params,
Balances: balances,
Supply: supply,
DenomMetadata: denomMetaData,
SupplyOffsets: supplyOffsets,
}
}

// DefaultGenesisState returns a default bank module genesis state.
func DefaultGenesisState() *GenesisState {
return NewGenesisState(DefaultParams(), []Balance{}, sdk.Coins{}, []Metadata{})
return NewGenesisState(DefaultParams(), []Balance{}, sdk.Coins{}, []Metadata{}, []GenesisSupplyOffset{})
}

// GetGenesisStateFromAppState returns x/bank GenesisState given raw application
Expand Down

0 comments on commit 51108b6

Please sign in to comment.