Skip to content

Commit

Permalink
MAT-1355 - changes for dividendaccountroot in checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mankenavenkatesh committed May 10, 2020
1 parent 4f61cf7 commit a036a12
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 23 deletions.
8 changes: 4 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"encoding/json"
"fmt"
"math/big"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -151,8 +150,8 @@ func (d ModuleCommunicator) IsCurrentValidatorByAddress(ctx sdk.Context, address
}

// AddFeeToDividendAccount add fee to dividend account
func (d ModuleCommunicator) AddFeeToDividendAccount(ctx sdk.Context, valID types.ValidatorID, fee *big.Int) sdk.Error {
return d.App.StakingKeeper.AddFeeToDividendAccount(ctx, valID, fee)
func (d ModuleCommunicator) GetAllDividendAccounts(ctx sdk.Context) []types.DividendAccount {
return d.App.TopupKeeper.GetAllDividendAccounts(ctx)
}

// GetValidatorFromValID get validator from validator id
Expand Down Expand Up @@ -331,6 +330,7 @@ func NewHeimdallApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.Ba
common.DefaultCodespace,
app.StakingKeeper,
app.ChainKeeper,
moduleCommunicator,
)

app.BorKeeper = bor.NewKeeper(
Expand Down Expand Up @@ -374,7 +374,7 @@ func NewHeimdallApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.Ba
gov.NewAppModule(app.GovKeeper, app.SupplyKeeper),
chainmanager.NewAppModule(app.ChainKeeper, &app.caller),
staking.NewAppModule(app.StakingKeeper, &app.caller),
checkpoint.NewAppModule(app.CheckpointKeeper, app.StakingKeeper, &app.caller),
checkpoint.NewAppModule(app.CheckpointKeeper, app.StakingKeeper, app.TopupKeeper, &app.caller),
bor.NewAppModule(app.BorKeeper, &app.caller),
clerk.NewAppModule(app.ClerkKeeper, &app.caller),
topup.NewAppModule(app.TopupKeeper, &app.caller),
Expand Down
2 changes: 1 addition & 1 deletion app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func SetupTopupGenesis() *HeimdallApp {
// initialize the chain with the default genesis state
genesisState := NewDefaultGenesisState()

topupGenesis := topupTypes.NewGenesisState(topupTypes.DefaultGenesisState().TopupSequences)
topupGenesis := topupTypes.NewGenesisState(topupTypes.DefaultGenesisState().TopupSequences, topupTypes.DefaultGenesisState().DividentAccounts)
genesisState[topupTypes.ModuleName] = app.Codec().MustMarshalJSON(topupGenesis)

stateBytes, err := codec.MarshalJSONIndent(app.Codec(), genesisState)
Expand Down
3 changes: 0 additions & 3 deletions bank/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bank

import (
"fmt"
"math/big"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -22,8 +21,6 @@ var (
// TODO: Remove this later
// ModuleCommunicator manager to access validator info
type ModuleCommunicator interface {
// AddFeeToDividendAccount add fee to dividend account
AddFeeToDividendAccount(ctx sdk.Context, valID hmTypes.ValidatorID, fee *big.Int) sdk.Error
// GetValidatorFromValID get validator from validator id
GetValidatorFromValID(ctx sdk.Context, valID hmTypes.ValidatorID) (validator hmTypes.Validator, ok bool)
}
Expand Down
2 changes: 1 addition & 1 deletion checkpoint/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func handleMsgCheckpoint(ctx sdk.Context, msg types.MsgCheckpoint, k Keeper, con

// Make sure latest AccountRootHash matches
// Calculate new account root hash
dividendAccounts := k.sk.GetAllDividendAccounts(ctx)
dividendAccounts := k.moduleCommunicator.GetAllDividendAccounts(ctx)
logger.Debug("DividendAccounts of all validators", "dividendAccountsLength", len(dividendAccounts))

// Get account root has from dividend accounts
Expand Down
22 changes: 16 additions & 6 deletions checkpoint/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ var (
LastNoACKKey = []byte{0x14} // key to store last no-ack
)

// ModuleCommunicator manages different module interaction
type ModuleCommunicator interface {
GetAllDividendAccounts(ctx sdk.Context) []hmTypes.DividendAccount
}

// Keeper stores all related data
type Keeper struct {
cdc *codec.Codec
Expand All @@ -38,6 +43,9 @@ type Keeper struct {
codespace sdk.CodespaceType
// param space
paramSpace subspace.Subspace

// module communicator
moduleCommunicator ModuleCommunicator
}

// NewKeeper create new keeper
Expand All @@ -48,14 +56,16 @@ func NewKeeper(
codespace sdk.CodespaceType,
stakingKeeper staking.Keeper,
chainKeeper chainmanager.Keeper,
moduleCommunicator ModuleCommunicator,
) Keeper {
keeper := Keeper{
cdc: cdc,
storeKey: storeKey,
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
codespace: codespace,
sk: stakingKeeper,
ck: chainKeeper,
cdc: cdc,
storeKey: storeKey,
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
codespace: codespace,
sk: stakingKeeper,
ck: chainKeeper,
moduleCommunicator: moduleCommunicator,
}
return keeper
}
Expand Down
7 changes: 5 additions & 2 deletions checkpoint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/gorilla/mux"
chainmanagerTypes "github.com/maticnetwork/heimdall/chainmanager/types"
"github.com/maticnetwork/heimdall/topup"
hmTypes "github.com/maticnetwork/heimdall/types"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -99,15 +100,17 @@ type AppModule struct {

keeper Keeper
stakingKeeper staking.Keeper
topupKeeper topup.Keeper
contractCaller helper.IContractCaller
}

// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper, sk staking.Keeper, contractCaller helper.IContractCaller) AppModule {
func NewAppModule(keeper Keeper, sk staking.Keeper, tk topup.Keeper, contractCaller helper.IContractCaller) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: keeper,
stakingKeeper: sk,
topupKeeper: tk,
contractCaller: contractCaller,
}
}
Expand Down Expand Up @@ -137,7 +140,7 @@ func (AppModule) QuerierRoute() string {

// NewQuerierHandler returns the auth module sdk.Querier.
func (am AppModule) NewQuerierHandler() sdk.Querier {
return NewQuerier(am.keeper, am.stakingKeeper, am.contractCaller)
return NewQuerier(am.keeper, am.stakingKeeper, am.topupKeeper, am.contractCaller)
}

// InitGenesis performs genesis initialization for the auth module. It returns
Expand Down
9 changes: 5 additions & 4 deletions checkpoint/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"github.com/maticnetwork/heimdall/common"
"github.com/maticnetwork/heimdall/helper"
"github.com/maticnetwork/heimdall/staking"
"github.com/maticnetwork/heimdall/topup"
hmTypes "github.com/maticnetwork/heimdall/types"
abci "github.com/tendermint/tendermint/abci/types"
)

// NewQuerier creates a querier for auth REST endpoints
func NewQuerier(keeper Keeper, stakingKeeper staking.Keeper, contractCaller helper.IContractCaller) sdk.Querier {
func NewQuerier(keeper Keeper, stakingKeeper staking.Keeper, topupKeeper topup.Keeper, contractCaller helper.IContractCaller) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) {
switch path[0] {
case types.QueryParams:
Expand All @@ -30,7 +31,7 @@ func NewQuerier(keeper Keeper, stakingKeeper staking.Keeper, contractCaller help
case types.QueryCheckpointList:
return handleQueryCheckpointList(ctx, req, keeper)
case types.QueryNextCheckpoint:
return handleQueryNextCheckpoint(ctx, req, keeper, stakingKeeper, contractCaller)
return handleQueryNextCheckpoint(ctx, req, keeper, stakingKeeper, topupKeeper, contractCaller)
default:
return nil, sdk.ErrUnknownRequest("unknown auth query endpoint")
}
Expand Down Expand Up @@ -117,7 +118,7 @@ func handleQueryCheckpointList(ctx sdk.Context, req abci.RequestQuery, keeper Ke
return bz, nil
}

func handleQueryNextCheckpoint(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, sk staking.Keeper, contractCaller helper.IContractCaller) ([]byte, sdk.Error) {
func handleQueryNextCheckpoint(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, sk staking.Keeper, tk topup.Keeper, contractCaller helper.IContractCaller) ([]byte, sdk.Error) {
var queryParams types.QueryBorChainID
if err := keeper.cdc.UnmarshalJSON(req.Data, &queryParams); err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse query params: %s", err))
Expand Down Expand Up @@ -146,7 +147,7 @@ func handleQueryNextCheckpoint(ctx sdk.Context, req abci.RequestQuery, keeper Ke
return nil, sdk.ErrInternal(sdk.AppendMsgToErr(fmt.Sprintf("could not fetch roothash for start:%v end:%v error:%v", start, end, err), err.Error()))
}

accs := sk.GetAllDividendAccounts(ctx)
accs := tk.GetAllDividendAccounts(ctx)
accRootHash, err := types.GetAccountRootHash(accs)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr(fmt.Sprintf("could not get generate account root hash. Error:%v", err), err.Error()))
Expand Down
9 changes: 8 additions & 1 deletion cmd/heimdalld/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/maticnetwork/heimdall/helper"
stakingcli "github.com/maticnetwork/heimdall/staking/client/cli"
stakingTypes "github.com/maticnetwork/heimdall/staking/types"
topupTypes "github.com/maticnetwork/heimdall/topup/types"
hmTypes "github.com/maticnetwork/heimdall/types"
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func initCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
}

// staking state change
appStateBytes, err = stakingTypes.SetGenesisStateToAppState(appStateBytes, vals, *validatorSet, dividendAccounts)
appStateBytes, err = stakingTypes.SetGenesisStateToAppState(appStateBytes, vals, *validatorSet)
if err != nil {
return err
}
Expand All @@ -88,6 +89,12 @@ func initCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
return err
}

// topup state change
appStateBytes, err = topupTypes.SetGenesisStateToAppState(appStateBytes, dividendAccounts)
if err != nil {
return err
}

// app state json
appStateJSON, err := json.Marshal(appStateBytes)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion cmd/heimdalld/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/maticnetwork/heimdall/helper"
stakingcli "github.com/maticnetwork/heimdall/staking/client/cli"
stakingTypes "github.com/maticnetwork/heimdall/staking/types"
topupTypes "github.com/maticnetwork/heimdall/topup/types"
hmTypes "github.com/maticnetwork/heimdall/types"
)

Expand Down Expand Up @@ -151,7 +152,7 @@ testnet --v 4 --n 8 --output-dir ./output --starting-ip-address 192.168.10.2
}

// staking state change
appStateBytes, err = stakingTypes.SetGenesisStateToAppState(appStateBytes, validators, *validatorSet, dividendAccounts)
appStateBytes, err = stakingTypes.SetGenesisStateToAppState(appStateBytes, validators, *validatorSet)
if err != nil {
return err
}
Expand All @@ -162,6 +163,12 @@ testnet --v 4 --n 8 --output-dir ./output --starting-ip-address 192.168.10.2
return err
}

// topup state change
appStateBytes, err = topupTypes.SetGenesisStateToAppState(appStateBytes, dividendAccounts)
if err != nil {
return err
}

appStateJSON, err := json.Marshal(appStateBytes)
if err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions topup/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package types

import (
"encoding/json"
"errors"

"github.com/maticnetwork/heimdall/bor/types"
hmTypes "github.com/maticnetwork/heimdall/types"
)

Expand Down Expand Up @@ -35,3 +37,22 @@ func ValidateGenesis(data GenesisState) error {
}
return nil
}

// GetGenesisStateFromAppState returns staking GenesisState given raw application genesis state
func GetGenesisStateFromAppState(appState map[string]json.RawMessage) GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
types.ModuleCdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}
return genesisState
}

// SetGenesisStateToAppState sets state into app state
func SetGenesisStateToAppState(appState map[string]json.RawMessage, dividendAccounts []hmTypes.DividendAccount) (map[string]json.RawMessage, error) {
// set state to staking state
topupState := GetGenesisStateFromAppState(appState)
topupState.DividentAccounts = dividendAccounts

appState[ModuleName] = types.ModuleCdc.MustMarshalJSON(topupState)
return appState, nil
}

0 comments on commit a036a12

Please sign in to comment.