Skip to content

Commit

Permalink
fix: fix app hash mismatch for genesis block
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Sep 6, 2023
1 parent 9a9d0d5 commit dee9039
Show file tree
Hide file tree
Showing 12 changed files with 1,031 additions and 60 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ replace (
github.com/cometbft/cometbft => github.com/bnb-chain/greenfield-cometbft v0.0.3
github.com/cometbft/cometbft-db => github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230830024916-258a09e84d27
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230906051413-bab493b93970
github.com/cosmos/iavl => github.com/bnb-chain/greenfield-iavl v0.20.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ github.com/bnb-chain/greenfield-cometbft v0.0.3 h1:tv8NMy3bzX/1urqXGQIIAZSLy83lo
github.com/bnb-chain/greenfield-cometbft v0.0.3/go.mod h1:f35mk/r5ab6yvzlqEWZt68LfUje68sYgMpVlt2CUYMk=
github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1 h1:XcWulGacHVRiSCx90Q8Y//ajOrLNBQWR/KDB89dy3cU=
github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1/go.mod h1:ey1CiK4bYo1RBNJLRiVbYr5CMdSxci9S/AZRINLtppI=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230830024916-258a09e84d27 h1:sqmAIC7IRqptGrwNPGQZuqDhcNQYpX1iUADGKMwa9RQ=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230830024916-258a09e84d27/go.mod h1:y3hDhQhil5hMIhwBTpu07RZBF30ZITkoE+GHhVZChtY=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230906051413-bab493b93970 h1:ulx0NMVthyQaO2OBkV2Y1yFLqplitN2a7aHyL8PsUlk=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230906051413-bab493b93970/go.mod h1:y3hDhQhil5hMIhwBTpu07RZBF30ZITkoE+GHhVZChtY=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20230816082903-b48770f5e210 h1:GHPbV2bC+gmuO6/sG0Tm8oGal3KKSRlyE+zPscDjlA8=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20230816082903-b48770f5e210/go.mod h1:vhsZxXE9tYJeYB5JR4hPhd6Pc/uPf7j1T8IJ7p9FdeM=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230816082903-b48770f5e210 h1:FLVOn4+OVbsKi2+YJX5kmD27/4dRu4FW7xCXFhzDO5s=
Expand Down
24 changes: 24 additions & 0 deletions proto/greenfield/payment/v1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
syntax = "proto3";

package greenfield.payment.v1;

import "gogoproto/gogo.proto";
import "greenfield/payment/auto_settle_record.proto";
import "greenfield/payment/payment_account.proto";
import "greenfield/payment/payment_account_count.proto";
import "greenfield/payment/stream_record.proto";
import "greenfield/payment/v1/params.proto";

// this line is used by starport scaffolding # genesis/proto/import

option go_package = "github.com/bnb-chain/greenfield/x/payment/types/v1";

// GenesisState defines the payment module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated StreamRecord stream_record_list = 2 [(gogoproto.nullable) = false];
repeated PaymentAccountCount payment_account_count_list = 3 [(gogoproto.nullable) = false];
repeated PaymentAccount payment_account_list = 4 [(gogoproto.nullable) = false];
repeated AutoSettleRecord auto_settle_record_list = 5 [(gogoproto.nullable) = false];
// this line is used by starport scaffolding # genesis/proto/state
}
35 changes: 35 additions & 0 deletions proto/greenfield/payment/v1/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
syntax = "proto3";
package greenfield.payment.v1;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/bnb-chain/greenfield/x/payment/types/v1";

// Params defines the parameters for the module.
message Params {
VersionedParams versioned_params = 1 [(gogoproto.nullable) = false];
// The maximum number of payment accounts that can be created by one user
uint64 payment_account_count_limit = 2 [(gogoproto.moretags) = "yaml:\"payment_account_count_limit\""];
// Time duration threshold of forced settlement.
// If dynamic balance is less than NetOutFlowRate * forcedSettleTime, the account can be forced settled.
uint64 forced_settle_time = 3 [(gogoproto.moretags) = "yaml:\"forced_settle_time\""];
// the maximum number of flows that will be auto forced settled in one block
uint64 max_auto_settle_flow_count = 4 [(gogoproto.moretags) = "yaml:\"max_auto_settle_flow_count\""];
// the maximum number of flows that will be auto resumed in one block
uint64 max_auto_resume_flow_count = 5 [(gogoproto.moretags) = "yaml:\"max_auto_resume_flow_count\""];
// The denom of fee charged in payment module
string fee_denom = 6 [(gogoproto.moretags) = "yaml:\"fee_denom\""];
}

// VersionedParams defines the parameters with multiple versions, each version is stored with different timestamp.
message VersionedParams {
// Time duration which the buffer balance need to be reserved for NetOutFlow e.g. 6 month
uint64 reserve_time = 1 [(gogoproto.moretags) = "yaml:\"reserve_time\""];
// The tax rate to pay for validators in storage payment. The default value is 1%(0.01)
string validator_tax_rate = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
12 changes: 6 additions & 6 deletions x/payment/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/bnb-chain/greenfield/x/payment/keeper"
"github.com/bnb-chain/greenfield/x/payment/types"
v1 "github.com/bnb-chain/greenfield/x/payment/types/v1"
)

// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState v1.GenesisState) {
// Set all the streamRecord
for _, elem := range genState.StreamRecordList {
k.SetStreamRecord(ctx, &elem)
Expand All @@ -25,16 +25,16 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
for _, elem := range genState.AutoSettleRecordList {
k.SetAutoSettleRecord(ctx, &elem)
}
err := k.SetParams(ctx, genState.Params)
err := k.SetV1Params(ctx, genState.Params)
if err != nil {
panic(err)
}
}

// ExportGenesis returns the module's exported genesis
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *v1.GenesisState {
genesis := v1.DefaultGenesis()
genesis.Params = k.GetV1Params(ctx)

genesis.StreamRecordList = k.GetAllStreamRecord(ctx)
genesis.PaymentAccountCountList = k.GetAllPaymentAccountCount(ctx)
Expand Down
41 changes: 41 additions & 0 deletions x/payment/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,49 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/bnb-chain/greenfield/x/payment/types"
v1 "github.com/bnb-chain/greenfield/x/payment/types/v1"
)

// GetV1Params get all parameters as v1.Params
func (k Keeper) GetV1Params(ctx sdk.Context) (p v1.Params) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
return p
}
k.cdc.MustUnmarshal(bz, &p)
return p
}

// SetV1Params set the params
func (k Keeper) SetV1Params(ctx sdk.Context, params v1.Params) error {
if err := params.Validate(); err != nil {
return err
}

store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set(types.ParamsKey, bz)

// store versioned params
err := k.SetV1VersionedParamsWithTs(ctx, params.VersionedParams)
if err != nil {
return err
}

return nil
}

func (k Keeper) SetV1VersionedParamsWithTs(ctx sdk.Context, verParams v1.VersionedParams) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VersionedParamsKeyPrefix)
key := types.VersionedParamsKey(ctx.BlockTime().Unix())

b := k.cdc.MustMarshal(&verParams)
store.Set(key, b)

return nil
}

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) (p types.Params) {
store := ctx.KVStore(k.storeKey)
Expand Down
2 changes: 1 addition & 1 deletion x/payment/keeper/v2/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

v1 "github.com/bnb-chain/greenfield/x/payment/keeper/v1"
"github.com/bnb-chain/greenfield/x/payment/types"
v1 "github.com/bnb-chain/greenfield/x/payment/types/v1"
)

func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
Expand Down
6 changes: 4 additions & 2 deletions x/payment/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"

v1 "github.com/bnb-chain/greenfield/x/payment/types/v1"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -54,7 +56,7 @@ func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {

// DefaultGenesis returns a default GenesisState for the module, marshalled to json.RawMessage. The default GenesisState need to be defined by the module developer and is primarily used for testing
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesis())
return cdc.MustMarshalJSON(v1.DefaultGenesis())
}

// ValidateGenesis used to validate the GenesisState, given in its json.RawMessage form
Expand Down Expand Up @@ -127,7 +129,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// InitGenesis performs the module's genesis initialization. It returns no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate {
var genState types.GenesisState
var genState v1.GenesisState
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

Expand Down
72 changes: 72 additions & 0 deletions x/payment/types/v1/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package v1

import (
"fmt"

"github.com/bnb-chain/greenfield/x/payment/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// DefaultIndex is the default global index
const DefaultIndex uint64 = 1

// DefaultGenesis returns the default genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
StreamRecordList: []types.StreamRecord{},
PaymentAccountCountList: []types.PaymentAccountCount{},
PaymentAccountList: []types.PaymentAccount{},
AutoSettleRecordList: []types.AutoSettleRecord{},
// this line is used by starport scaffolding # genesis/types/default
Params: DefaultParams(),
}
}

// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
// Check for duplicated index in streamRecord
streamRecordIndexMap := make(map[string]struct{})

for _, elem := range gs.StreamRecordList {
index := string(types.StreamRecordKey(sdk.MustAccAddressFromHex(elem.Account)))
if _, ok := streamRecordIndexMap[index]; ok {
return fmt.Errorf("duplicated index for streamRecord")
}
streamRecordIndexMap[index] = struct{}{}
}
// Check for duplicated index in paymentAccountCount
paymentAccountCountIndexMap := make(map[string]struct{})

for _, elem := range gs.PaymentAccountCountList {
index := string(types.PaymentAccountCountKey(sdk.MustAccAddressFromHex(elem.Owner)))
if _, ok := paymentAccountCountIndexMap[index]; ok {
return fmt.Errorf("duplicated index for paymentAccountCount")
}
paymentAccountCountIndexMap[index] = struct{}{}
}
// Check for duplicated index in paymentAccount
paymentAccountIndexMap := make(map[string]struct{})

for _, elem := range gs.PaymentAccountList {
index := string(types.PaymentAccountKey(sdk.MustAccAddressFromHex(elem.Addr)))
if _, ok := paymentAccountIndexMap[index]; ok {
return fmt.Errorf("duplicated index for paymentAccount")
}
paymentAccountIndexMap[index] = struct{}{}
}

// Check for duplicated index in autoSettleRecord
autoSettleRecordIndexMap := make(map[string]struct{})

for _, elem := range gs.AutoSettleRecordList {
index := string(types.AutoSettleRecordKey(elem.Timestamp, sdk.MustAccAddressFromHex(elem.Addr)))
if _, ok := autoSettleRecordIndexMap[index]; ok {
return fmt.Errorf("duplicated index for autoSettleRecord")
}
autoSettleRecordIndexMap[index] = struct{}{}
}
// this line is used by starport scaffolding # genesis/types/validate

return gs.Params.Validate()
}
Loading

0 comments on commit dee9039

Please sign in to comment.