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

feat: update genesis_time upon export #172

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 0 additions & 17 deletions baseapp/testutil/buf.lock

This file was deleted.

2 changes: 1 addition & 1 deletion docs/architecture/adr-023-protobuf-naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ or significantly alter the package in the near future
significantly refactor/rework the functionality in the near future but not remove it
* modules _can and should_ have types in both stable (i.e. `v1` or `v2`) and unstable (`alpha` or `beta`) packages.

*`alpha` and `beta` should not be used to avoid responsibility for maintaining compatibility.*
_`alpha` and `beta` should not be used to avoid responsibility for maintaining compatibility._
Whenever code is released into the wild, especially on a blockchain, there is a high cost to changing things. In some
cases, for instance with immutable smart contracts, a breaking change may be impossible to fix.

Expand Down
86 changes: 86 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@

- [cosmos/mint/v1beta1/mint.proto](#cosmos/mint/v1beta1/mint.proto)
- [Minter](#cosmos.mint.v1beta1.Minter)
- [Minter.MunicipalInflationEntry](#cosmos.mint.v1beta1.Minter.MunicipalInflationEntry)
- [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation)
- [Params](#cosmos.mint.v1beta1.Params)

- [cosmos/mint/v1beta1/genesis.proto](#cosmos/mint/v1beta1/genesis.proto)
Expand All @@ -404,6 +406,9 @@
- [QueryAnnualProvisionsResponse](#cosmos.mint.v1beta1.QueryAnnualProvisionsResponse)
- [QueryInflationRequest](#cosmos.mint.v1beta1.QueryInflationRequest)
- [QueryInflationResponse](#cosmos.mint.v1beta1.QueryInflationResponse)
- [QueryMunicipalInflationRequest](#cosmos.mint.v1beta1.QueryMunicipalInflationRequest)
- [QueryMunicipalInflationResponse](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse)
- [QueryMunicipalInflationResponse.InflationsEntry](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry)
- [QueryParamsRequest](#cosmos.mint.v1beta1.QueryParamsRequest)
- [QueryParamsResponse](#cosmos.mint.v1beta1.QueryParamsResponse)

Expand Down Expand Up @@ -5748,6 +5753,39 @@ Minter represents the minting state.
| ----- | ---- | ----- | ----------- |
| `inflation` | [string](#string) | | current annual inflation rate |
| `annual_provisions` | [string](#string) | | current annual expected provisions |
| `municipal_inflation` | [Minter.MunicipalInflationEntry](#cosmos.mint.v1beta1.Minter.MunicipalInflationEntry) | repeated | |






<a name="cosmos.mint.v1beta1.Minter.MunicipalInflationEntry"></a>

### Minter.MunicipalInflationEntry



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `key` | [string](#string) | | |
| `value` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | | |






<a name="cosmos.mint.v1beta1.MunicipalInflation"></a>

### MunicipalInflation
Inflation holds parameters for individual native token inflation


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `target_address` | [string](#string) | | |
| `inflation` | [string](#string) | | current annual inflation rate |



Expand Down Expand Up @@ -5872,6 +5910,53 @@ method.



<a name="cosmos.mint.v1beta1.QueryMunicipalInflationRequest"></a>

### QueryMunicipalInflationRequest
QueryMunicipalInflationRequest is the request type for the Query/MunicipalInflation RPC method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `denom` | [string](#string) | optional | |






<a name="cosmos.mint.v1beta1.QueryMunicipalInflationResponse"></a>

### QueryMunicipalInflationResponse
QueryInflationResponse is the response type for the Query/Inflation RPC
method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `inflations` | [QueryMunicipalInflationResponse.InflationsEntry](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry) | repeated | inflation is the current minting inflation value. |






<a name="cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry"></a>

### QueryMunicipalInflationResponse.InflationsEntry



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `key` | [string](#string) | | |
| `value` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | | |






<a name="cosmos.mint.v1beta1.QueryParamsRequest"></a>

### QueryParamsRequest
Expand Down Expand Up @@ -5912,6 +5997,7 @@ Query provides defines the gRPC querier service.
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `Params` | [QueryParamsRequest](#cosmos.mint.v1beta1.QueryParamsRequest) | [QueryParamsResponse](#cosmos.mint.v1beta1.QueryParamsResponse) | Params returns the total set of minting parameters. | GET|/cosmos/mint/v1beta1/params|
| `Inflation` | [QueryInflationRequest](#cosmos.mint.v1beta1.QueryInflationRequest) | [QueryInflationResponse](#cosmos.mint.v1beta1.QueryInflationResponse) | Inflation returns the current minting inflation value. | GET|/cosmos/mint/v1beta1/inflation|
| `MunicipalInflation` | [QueryMunicipalInflationRequest](#cosmos.mint.v1beta1.QueryMunicipalInflationRequest) | [QueryMunicipalInflationResponse](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse) | Inflation returns the current minting inflation value. | GET|/cosmos/mint/v1beta1/municipal_inflation|
| `AnnualProvisions` | [QueryAnnualProvisionsRequest](#cosmos.mint.v1beta1.QueryAnnualProvisionsRequest) | [QueryAnnualProvisionsResponse](#cosmos.mint.v1beta1.QueryAnnualProvisionsResponse) | AnnualProvisions current minting annual provisions value. | GET|/cosmos/mint/v1beta1/annual_provisions|

<!-- end services -->
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/golangci/golangci-lint v1.48.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
Expand Down Expand Up @@ -52,8 +53,6 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require github.com/golangci/golangci-lint v1.48.0

require (
4d63.com/gochecknoglobals v0.1.0 // indirect
filippo.io/edwards25519 v1.0.0-beta.2 // indirect
Expand Down
13 changes: 13 additions & 0 deletions proto/cosmos/mint/v1beta1/mint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ message Minter {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

map<string, MunicipalInflation> municipal_inflation = 3;
}

// Inflation holds parameters for individual native token inflation
message MunicipalInflation {
string target_address = 2;
// current annual inflation rate
string inflation = 3 [
(gogoproto.moretags) = "yaml:\"inflation_rate\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// Params holds parameters for the mint module.
Expand Down
17 changes: 17 additions & 0 deletions proto/cosmos/mint/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ service Query {
option (google.api.http).get = "/cosmos/mint/v1beta1/inflation";
}

// Inflation returns the current minting inflation value.
rpc MunicipalInflation(QueryMunicipalInflationRequest) returns (QueryMunicipalInflationResponse) {
option (google.api.http).get = "/cosmos/mint/v1beta1/municipal_inflation";
}

// AnnualProvisions current minting annual provisions value.
rpc AnnualProvisions(QueryAnnualProvisionsRequest) returns (QueryAnnualProvisionsResponse) {
option (google.api.http).get = "/cosmos/mint/v1beta1/annual_provisions";
Expand All @@ -37,13 +42,25 @@ message QueryParamsResponse {
// QueryInflationRequest is the request type for the Query/Inflation RPC method.
message QueryInflationRequest {}

// QueryMunicipalInflationRequest is the request type for the Query/MunicipalInflation RPC method.
message QueryMunicipalInflationRequest {
optional string denom = 1;
}

// QueryInflationResponse is the response type for the Query/Inflation RPC
// method.
message QueryInflationResponse {
// inflation is the current minting inflation value.
bytes inflation = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// QueryInflationResponse is the response type for the Query/Inflation RPC
// method.
message QueryMunicipalInflationResponse {
// inflation is the current minting inflation value.
map<string, MunicipalInflation> inflations = 1;
}

// QueryAnnualProvisionsRequest is the request type for the
// Query/AnnualProvisions RPC method.
message QueryAnnualProvisionsRequest {}
Expand Down
27 changes: 26 additions & 1 deletion server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package server
// DONTCOVER

import (
"errors"
"fmt"
"os"

"github.com/spf13/cobra"
tmjson "github.com/tendermint/tendermint/libs/json"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmstore "github.com/tendermint/tendermint/store"
tmtypes "github.com/tendermint/tendermint/types"
tmdb "github.com/tendermint/tm-db"
"os"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server/types"
Expand All @@ -22,6 +25,22 @@ const (
FlagJailAllowedAddrs = "jail-allowed-addrs"
)

// GetLatestBlockHeaderFromDB returns the latest block header from the blockstore database
func GetLatestBlockHeaderFromDB(blockStoreDB tmdb.DB) (*tmtypes.Header, error) {
// Load the blockstore
blockStore := tmstore.NewBlockStore(blockStoreDB)
defer blockStoreDB.Close()

// Get the latest block meta
latestHeight := blockStore.Height()
blockMeta := blockStore.LoadBlockMeta(latestHeight)
if blockMeta == nil {
return nil, errors.New("latest block metadata not found in blockstore")
}

return &blockMeta.Header, nil
}

// ExportCmd dumps app state to JSON.
func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -77,6 +96,12 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
return err
}

latestBlockHeader, err := GetLatestBlockHeaderFromDB(db)
if err != nil {
return err
}
doc.GenesisTime = latestBlockHeader.Time

doc.AppState = exported.AppState
doc.Validators = exported.Validators
doc.InitialHeight = exported.Height
Expand Down
91 changes: 89 additions & 2 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,101 @@
package mint

import (
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
"github.com/cosmos/cosmos-sdk/x/mint/types"
"time"
)

type MunicipalInflationCache struct {
blocksPerYear uint64
perBlockInflations map[string]sdk.Dec // {denom: inflationPerBlock}
}

var (
// NOTE(pb): This is *NOT* thread safe.
// However, in our case, this global variable is by design
// *NOT* supposed to be accessed simultaneously in multiple
// different threads, or in global scope somewhere else.
// Once such requirements arise, concept of this global variable
// needs to be changed to something what is thread safe.
infCache = MunicipalInflationCache{0, nil}
)

// NOTE(pb): Not thread safe, as per comment above.
func (cache *MunicipalInflationCache) refresh(minter *types.Minter, blocksPerYear uint64) {
if err := types.ValidateMunicipalInflations(&minter.MunicipalInflation); err != nil {
panic(err)
}

cache.blocksPerYear = blocksPerYear
cache.perBlockInflations = map[string]sdk.Dec{}

for denom, inflation := range minter.MunicipalInflation {
inflationPerBlock, err := types.CalculateInflationPerBlock(inflation, blocksPerYear)
if err != nil {
panic(err)
}

cache.perBlockInflations[denom] = inflationPerBlock
}
}

// NOTE(pb): Not thread safe, as per comment above.
func (cache *MunicipalInflationCache) refreshIfNecessary(minter *types.Minter, blocksPerYear uint64) {
if infCache.blocksPerYear != blocksPerYear {
cache.refresh(minter, blocksPerYear)
}
}

// HandleMunicipalInflation iterates through all other native tokens specified in the minter.MunicipalInflation structure, and processes
// the minting of new coins in line with the respective inflation rate of each denomination
func HandleMunicipalInflation(ctx sdk.Context, k keeper.Keeper) {
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)

infCache.refreshIfNecessary(&minter, params.BlocksPerYear)

// iterate through native denominations
for denom, inflation := range minter.MunicipalInflation {
targetAddress := inflation.TargetAddress

// gather supply value & calculate number of new tokens created from relevant inflation
totalDenomSupply := k.BankKeeper.GetSupply(ctx, denom)
coinsToMint := types.CalculateInflationIssuance(infCache.perBlockInflations[denom], totalDenomSupply)

err := k.MintCoins(ctx, coinsToMint)

if err != nil {
panic(err)
}

// send these new tokens to respective target address
// TODO(JS): investigate whether this should be carried out in distribution module or not

// Convert targetAddress to sdk.AccAddress
acc, err := sdk.AccAddressFromBech32(targetAddress)
if err != nil {
panic(err)
}

err = k.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, acc, coinsToMint)
if err != nil {
panic(err)
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMunicipalMint,
sdk.NewAttribute(types.AttributeKeyDenom, denom),
sdk.NewAttribute(types.AttributeKeyInflation, inflation.Inflation.String()),
sdk.NewAttribute(types.AttributeKeyTargetAddr, inflation.TargetAddress),
sdk.NewAttribute(sdk.AttributeKeyAmount, coinsToMint.String()),
),
)
}
}

// BeginBlocker mints new tokens for the previous block.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
Expand Down
Loading
Loading