Skip to content

Commit

Permalink
fix: export and init genesis state
Browse files Browse the repository at this point in the history
  • Loading branch information
zhang0125 committed Sep 13, 2021
1 parent ca792ad commit 6dbdcc5
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 23 deletions.
20 changes: 20 additions & 0 deletions checkpoint/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {

// Set initial ack count
keeper.UpdateACKCountWithValue(ctx, data.AckCount)

// Add finalised checkpoints to state
if len(data.TronCheckpoints) != 0 {
// check if we are provided all the headers
if int(data.TronAckCount) != len(data.TronCheckpoints) {
panic(errors.New("Incorrect state in state-dump , Please Check "))
}
// sort headers before loading to state
data.TronCheckpoints = hmTypes.SortHeaders(data.TronCheckpoints)
// load checkpoints to state
for i, checkpoint := range data.TronCheckpoints {
checkpointIndex := uint64(i) + 1
if err := keeper.AddOtherCheckpoint(ctx, checkpointIndex, checkpoint, hmTypes.RootChainTypeTron); err != nil {
keeper.Logger(ctx).Error("InitGenesis | TronAddCheckpoint", "error", err)
}
}
}
keeper.UpdateOtherACKCountWithValue(ctx, data.TronAckCount, hmTypes.RootChainTypeTron)
}

// ExportGenesis returns a GenesisState for a given context and keeper.
Expand All @@ -57,5 +75,7 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
keeper.GetLastNoAck(ctx),
keeper.GetACKCount(ctx),
hmTypes.SortHeaders(keeper.GetCheckpoints(ctx)),
keeper.GetOtherACKCount(ctx, hmTypes.RootChainTypeTron),
hmTypes.SortHeaders(keeper.GetOtherCheckpoints(ctx, hmTypes.RootChainTypeTron)),
)
}
2 changes: 2 additions & 0 deletions checkpoint/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (suite *GenesisTestSuite) TestInitExportGenesis() {
uint64(lastNoACK),
uint64(ackCount),
checkpoints,
uint64(ackCount),
checkpoints,
)

checkpoint.InitGenesis(ctx, app.CheckpointKeeper, genesisState)
Expand Down
2 changes: 2 additions & 0 deletions checkpoint/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func createTestApp(isCheckTx bool) (*app.HeimdallApp, sdk.Context, context.CLICo
types.DefaultGenesisState().LastNoACK,
types.DefaultGenesisState().AckCount,
types.DefaultGenesisState().Checkpoints,
types.DefaultGenesisState().AckCount,
types.DefaultGenesisState().Checkpoints,
)

genesisState[types.ModuleName] = app.Codec().MustMarshalJSON(checkpointGenesis)
Expand Down
35 changes: 35 additions & 0 deletions checkpoint/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,28 @@ func (k *Keeper) GetCheckpoints(ctx sdk.Context) []hmTypes.Checkpoint {
return headers
}

// GetOtherCheckpoints get checkpoint all checkpoints
func (k *Keeper) GetOtherCheckpoints(ctx sdk.Context, rootChain string) []hmTypes.Checkpoint {
store := ctx.KVStore(k.storeKey)
// get checkpoint header iterator
var iterator sdk.Iterator
if hmTypes.RootChainTypeTron == rootChain {
iterator = sdk.KVStorePrefixIterator(store, TronCheckpointKey)
defer iterator.Close()
}
// create headers
var headers []hmTypes.Checkpoint

// loop through validators to get valid validators
for ; iterator.Valid(); iterator.Next() {
var checkpoint hmTypes.Checkpoint
if err := k.cdc.UnmarshalBinaryBare(iterator.Value(), &checkpoint); err == nil {
headers = append(headers, checkpoint)
}
}
return headers
}

//
// Ack count
//
Expand Down Expand Up @@ -465,6 +487,19 @@ func (k Keeper) UpdateACKCountWithValue(ctx sdk.Context, value uint64) {
store.Set(ACKCountKey, ackCount)
}

// UpdateOtherACKCountWithValue updates ACK with value
func (k Keeper) UpdateOtherACKCountWithValue(ctx sdk.Context, value uint64, rootChain string) {
store := ctx.KVStore(k.storeKey)

// convert
ackCount := []byte(strconv.FormatUint(value, 10))

// update
if rootChain == hmTypes.RootChainTypeTron {
store.Set(TronACKCountKey, ackCount)
}
}

// UpdateACKCount updates ACK count by 1
func (k Keeper) UpdateACKCount(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
Expand Down
2 changes: 2 additions & 0 deletions checkpoint/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func RandomizedGenState(simState *module.SimulationState) {
uint64(lastNoACK),
uint64(ackCount),
Checkpoints,
uint64(ackCount),
Checkpoints,
)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(genesisState)

Expand Down
6 changes: 6 additions & 0 deletions checkpoint/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type GenesisState struct {
LastNoACK uint64 `json:"last_no_ack" yaml:"last_no_ack"`
AckCount uint64 `json:"ack_count" yaml:"ack_count"`
Checkpoints []hmTypes.Checkpoint `json:"checkpoints" yaml:"checkpoints"`
TronAckCount uint64 `json:"tron_ack_count" yaml:"tron_ack_count"`
TronCheckpoints []hmTypes.Checkpoint `json:"tron_checkpoints" yaml:"tron_checkpoints"`
}

// NewGenesisState creates a new genesis state.
Expand All @@ -25,13 +27,17 @@ func NewGenesisState(
lastNoACK uint64,
ackCount uint64,
checkpoints []hmTypes.Checkpoint,
tronAckCount uint64,
tronCheckpoints []hmTypes.Checkpoint,
) GenesisState {
return GenesisState{
Params: params,
BufferedCheckpoint: bufferedCheckpoint,
LastNoACK: lastNoACK,
AckCount: ackCount,
Checkpoints: checkpoints,
TronAckCount: tronAckCount,
TronCheckpoints: tronCheckpoints,
}
}

Expand Down
1 change: 1 addition & 0 deletions staking/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
// return new genesis state
return types.NewGenesisState(
keeper.GetParams(ctx),
keeper.GetAllValidators(ctx),
keeper.GetValidatorSet(ctx),
keeper.GetStakingSequences(ctx),
Expand Down
5 changes: 4 additions & 1 deletion staking/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ func (suite *GenesisTestSuite) TestInitExportGenesis() {
accounts[i].Address,
)
}
param := types.Params{
StakingBufferTime: time.Duration(simulation.RandIntBetween(r1, 1, 10)) * time.Minute,
}

// validator set
validatorSet := hmTypes.NewValidatorSet(validators)

genesisState := types.NewGenesisState(validators, *validatorSet, stakingSequence)
genesisState := types.NewGenesisState(param, validators, *validatorSet, stakingSequence)
staking.InitGenesis(ctx, app.StakingKeeper, genesisState)

actualParams := staking.ExportGenesis(ctx, app.StakingKeeper)
Expand Down
1 change: 1 addition & 0 deletions staking/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
func createTestApp(isCheckTx bool) (*app.HeimdallApp, sdk.Context, context.CLIContext) {
genesisState := app.NewDefaultGenesisState()
stakingGenesis := stakingTypes.NewGenesisState(
stakingTypes.DefaultGenesisState().Params,
stakingTypes.DefaultGenesisState().Validators,
stakingTypes.DefaultGenesisState().CurrentValSet,
stakingTypes.DefaultGenesisState().StakingSequences)
Expand Down
6 changes: 4 additions & 2 deletions staking/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func RandomizedGenState(simState *module.SimulationState) {

// validator set
validatorSet := hmTypes.NewValidatorSet(validators)

genesisState := types.NewGenesisState(validators, *validatorSet, stakingSequence)
param := types.Params{
StakingBufferTime: time.Duration(simulation.RandIntBetween(r1, 1, 10)) * time.Minute,
}
genesisState := types.NewGenesisState(param, validators, *validatorSet, stakingSequence)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(genesisState)
}
4 changes: 3 additions & 1 deletion staking/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ type GenesisState struct {

// NewGenesisState creates a new genesis state.
func NewGenesisState(
params Params,
validators []*hmTypes.Validator,
currentValSet hmTypes.ValidatorSet,
stakingSequences []string,
) GenesisState {
return GenesisState{
Params: params,
Validators: validators,
CurrentValSet: currentValSet,
StakingSequences: stakingSequences,
Expand All @@ -55,7 +57,7 @@ func NewGenesisState(

// DefaultGenesisState returns a default genesis state
func DefaultGenesisState() GenesisState {
return NewGenesisState(nil, hmTypes.ValidatorSet{}, nil)
return NewGenesisState(Params{}, nil, hmTypes.ValidatorSet{}, nil)
}

// ValidateGenesis performs basic validation of bor genesis data returning an
Expand Down
12 changes: 6 additions & 6 deletions topup/side_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (suite *SideHandlerTestSuite) TestSideHandleMsgTopup() {
User: ethCommon.BytesToAddress(addr1.Bytes()),
Fee: coins.AmountOf(authTypes.FeeToken).BigInt(),
}
suite.contractCaller.On("GetConfirmedTxReceipt", txHash.EthHash(), chainParams.MainchainTxConfirmations).Return(txReceipt, nil)
suite.contractCaller.On("GetTronTransactionReceipt", txHash.String()).Return(txReceipt, nil)
suite.contractCaller.On("DecodeValidatorTopupFeesEvent", chainParams.ChainParams.StateSenderAddress.EthAddress(), txReceipt, logIndex).Return(event, nil)

// execute handler
Expand Down Expand Up @@ -143,7 +143,7 @@ func (suite *SideHandlerTestSuite) TestSideHandleMsgTopup() {
blockNumber,
)

suite.contractCaller.On("GetConfirmedTxReceipt", txHash.EthHash(), chainParams.MainchainTxConfirmations).Return(nil, nil)
suite.contractCaller.On("GetTronTransactionReceipt", txHash.String()).Return(nil, nil)
suite.contractCaller.On("DecodeValidatorTopupFeesEvent", chainParams.ChainParams.StateSenderAddress.EthAddress(), nil, logIndex).Return(nil, nil)

// execute handler
Expand Down Expand Up @@ -176,7 +176,7 @@ func (suite *SideHandlerTestSuite) TestSideHandleMsgTopup() {
blockNumber,
)

suite.contractCaller.On("GetConfirmedTxReceipt", txHash.EthHash(), chainParams.MainchainTxConfirmations).Return(txReceipt, nil)
suite.contractCaller.On("GetTronTransactionReceipt", txHash.String()).Return(txReceipt, nil)
suite.contractCaller.On("DecodeValidatorTopupFeesEvent", chainParams.ChainParams.StateSenderAddress.EthAddress(), txReceipt, logIndex).Return(nil, nil)

// execute handler
Expand Down Expand Up @@ -214,7 +214,7 @@ func (suite *SideHandlerTestSuite) TestSideHandleMsgTopup() {
User: ethCommon.BytesToAddress(addr1.Bytes()),
Fee: coins.AmountOf(authTypes.FeeToken).BigInt(),
}
suite.contractCaller.On("GetConfirmedTxReceipt", txHash.EthHash(), chainParams.MainchainTxConfirmations).Return(txReceipt, nil)
suite.contractCaller.On("GetTronTransactionReceipt", txHash.String()).Return(txReceipt, nil)
suite.contractCaller.On("DecodeValidatorTopupFeesEvent", chainParams.ChainParams.StateSenderAddress.EthAddress(), txReceipt, logIndex).Return(event, nil)

// execute handler
Expand Down Expand Up @@ -252,7 +252,7 @@ func (suite *SideHandlerTestSuite) TestSideHandleMsgTopup() {
User: ethCommon.BytesToAddress(addr2.Bytes()),
Fee: coins.AmountOf(authTypes.FeeToken).BigInt(),
}
suite.contractCaller.On("GetConfirmedTxReceipt", txHash.EthHash(), chainParams.MainchainTxConfirmations).Return(txReceipt, nil)
suite.contractCaller.On("GetTronTransactionReceipt", txHash.String()).Return(txReceipt, nil)
suite.contractCaller.On("DecodeValidatorTopupFeesEvent", chainParams.ChainParams.StateSenderAddress.EthAddress(), txReceipt, logIndex).Return(event, nil)

// execute handler
Expand Down Expand Up @@ -290,7 +290,7 @@ func (suite *SideHandlerTestSuite) TestSideHandleMsgTopup() {
User: ethCommon.BytesToAddress(addr1.Bytes()),
Fee: big.NewInt(1), // different fee
}
suite.contractCaller.On("GetConfirmedTxReceipt", txHash.EthHash(), chainParams.MainchainTxConfirmations).Return(txReceipt, nil)
suite.contractCaller.On("GetTronTransactionReceipt", txHash.String()).Return(txReceipt, nil)
suite.contractCaller.On("DecodeValidatorTopupFeesEvent", chainParams.ChainParams.StateSenderAddress.EthAddress(), txReceipt, logIndex).Return(event, nil)

// execute handler
Expand Down
26 changes: 13 additions & 13 deletions tron/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tron

import (
"github.com/maticnetwork/bor/core/types"
"math/rand"
"strconv"

"github.com/maticnetwork/bor/core/types"
)

const (
Expand All @@ -21,39 +22,38 @@ type NewFilter struct {
}
type FilterEventParams struct {
BaseQueryParam
Method string `json:"method"`
Params []NewFilter `json:"params"`
Method string `json:"method"`
Params []NewFilter `json:"params"`
}
type FilterOtherParams struct {
BaseQueryParam
Method string `json:"method"`
Params []string `json:"params"`
Method string `json:"method"`
Params []string `json:"params"`
}

type BaseQueryParam struct {
Jsonrpc string `json:"jsonrpc"`
Id string `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Id string `json:"id"`
}
type FilterEventResponse struct {
BaseQueryParam
Result []types.Log `json:result`
Result []types.Log `json:result`
}

type FilterTxResponse struct {
BaseQueryParam
Result types.Receipt `json:result`
Result types.Receipt `json:result`
}

type FilterTxNumberResponse struct {
BaseQueryParam
Result string `json:result`
Result string `json:result`
}


func GetDefaultBaseParm() BaseQueryParam {
func GetDefaultBaseParm() BaseQueryParam {
param := BaseQueryParam{
Jsonrpc: JsonRpcVersion,
Id: strconv.FormatInt(int64(rand.Int()%100), 10),
}
return param
}
}

0 comments on commit 6dbdcc5

Please sign in to comment.