Skip to content

Commit

Permalink
add genesis for distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Oct 10, 2024
1 parent d143808 commit 96407c6
Show file tree
Hide file tree
Showing 5 changed files with 1,221 additions and 112 deletions.
52 changes: 45 additions & 7 deletions proto/exocore/feedistribution/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,55 @@ message GenesisState {
FeePool fee_pool = 2 [(gogoproto.nullable) = false];
// validator_accumulated_commissions represents accumulated commission
// for a validator kept as a running counter, can be withdrawn at any time.
repeated ValidatorAccumulatedCommission validator_accumulated_commissions = 3 [(gogoproto.nullable) = false];
// validator_current_rewards represents current rewards and current
repeated ValidatorAccumulatedCommissions validator_accumulated_commissions = 3 [(gogoproto.nullable) = false];
// validator_current_rewards_list represents current rewards and current
// period for a validator kept as a running counter and incremented
// each block as long as the validator's tokens remain constant.
repeated ValidatorCurrentRewards validator_current_rewards = 4 [(gogoproto.nullable) = false];
repeated ValidatorCurrentRewardsList validator_current_rewards_list = 4 [(gogoproto.nullable) = false];

// validator_outstanding_rewards represents outstanding (un-withdrawn) rewards
// validator_outstanding_rewards_list represents outstanding (un-withdrawn) rewards
// for a validator inexpensive to track, allows simple sanity checks.
repeated ValidatorOutstandingRewards validator_outstanding_rewards = 5 [(gogoproto.nullable) = false];
// staker_outstanding_rewards represents outstanding (un-withdrawn) rewards
repeated ValidatorOutstandingRewardsList validator_outstanding_rewards_list = 5 [(gogoproto.nullable) = false];
// staker_outstanding_rewards_list represents outstanding (un-withdrawn) rewards
// for a staker inexpensive to track, allows simple sanity checks.
repeated StakerOutstandingRewards staker_outstanding_rewards = 6 [(gogoproto.nullable) = false];
repeated StakerOutstandingRewardsList staker_outstanding_rewards_list = 6 [(gogoproto.nullable) = false];

}


message ValidatorAccumulatedCommissions {
// val_addr is the address of validator
string val_addr = 1;

// ValidatorAccumulatedCommission represents accumulated commission
// for a validator kept as a running counter, can be withdrawn at any time.
ValidatorAccumulatedCommission commission = 2;
}

message ValidatorCurrentRewardsList {
// val_addr is the address of validator
string val_addr = 1;

// ValidatorCurrentRewards represents current rewards and current
// period for a validator kept as a running counter and incremented
// each block as long as the validator's tokens remain constant.
ValidatorCurrentRewards current_rewards = 2;
}

message ValidatorOutstandingRewardsList {
// val_addr is the address of validator
string val_addr = 1;

// ValidatorOutstandingRewards represents outstanding (un-withdrawn) rewards
// for a validator inexpensive to track, allows simple sanity checks.
ValidatorOutstandingRewards outstanding_rewards = 2;
}

message StakerOutstandingRewardsList {
// val_addr is the address of validator
string val_addr = 1;

// StakerOutstandingRewards represents outstanding (un-withdrawn) rewards
// for a staker inexpensive to track, allows simple sanity checks.
StakerOutstandingRewards staker_outstanding_rewards = 2;
}
80 changes: 73 additions & 7 deletions x/avs/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,91 @@ package keeper
import (
errorsmod "cosmossdk.io/errors"
"github.com/ExocoreNetwork/exocore/x/avs/types"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
)

// InitGenesis initializes the module's state from a provided genesis state.
// Since this action typically occurs on chain starts, this function is allowed to panic.
func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) []abci.ValidatorUpdate {
func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
// Set all the avs infos
for _, avs := range state.AvsInfos {

if err := k.SetAVSInfo(ctx, &avs); err != nil { //nolint:gosec
panic(errorsmod.Wrap(err, "failed to set avs info"))
err := k.SetAVSInfo(ctx, &avs) //nolint:gosec
if err != nil {
panic(errorsmod.Wrap(err, "failed to set all avs info"))
}
}
// Set all the task infos
for _, elem := range state.TaskInfos {
err := k.SetTaskInfo(ctx, &elem) //nolint:gosec
if err != nil {
panic(errorsmod.Wrap(err, "failed to set all task info"))
}
}
// Set all the bls infos
for _, elem := range state.BlsPubKeys {
err := k.SetOperatorPubKey(ctx, &elem) //nolint:gosec
if err != nil {
panic(errorsmod.Wrap(err, "failed to set all bls info"))
}
}
// Set all the taskNum infos
for _, elem := range state.TaskNums {
k.SetTaskID(ctx, common.HexToAddress(elem.TaskAddr), elem.TaskId)
}
// Set all the task result infos
for _, elem := range state.TaskResultInfos {
err := k.SetTaskResultInfo(ctx, elem.OperatorAddress, &elem) //nolint:gosec
if err != nil {
panic(errorsmod.Wrap(err, "failed to set all task result info"))
}
}
// Set all the task challenge infos
err := k.SetAllTaskChallengedInfo(ctx, state.ChallengeInfos)
if err != nil {
panic(errorsmod.Wrap(err, "failed to set all challenge info"))
}
// Set all the chainID infos
for _, elem := range state.ChainIdInfos {
k.SetAVSAddrToChainID(ctx, common.HexToAddress(elem.AvsAddress), elem.ChainId)
}

return []abci.ValidatorUpdate{}
}

func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
res := types.GenesisState{}
var _ error
var err error
res.AvsInfos = k.AllAvsInfos(ctx)

Check failure on line 60 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

k.AllAvsInfos undefined (type Keeper has no field or method AllAvsInfos)

Check failure on line 60 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

k.AllAvsInfos undefined (type Keeper has no field or method AllAvsInfos)

Check failure on line 60 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.AllAvsInfos undefined (type Keeper has no field or method AllAvsInfos)

Check failure on line 60 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.AllAvsInfos undefined (type Keeper has no field or method AllAvsInfos)

Check failure on line 60 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / build

k.AllAvsInfos undefined (type Keeper has no field or method AllAvsInfos)

res.TaskInfos, err = k.GetAllTaskInfos(ctx)

Check failure on line 62 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

k.GetAllTaskInfos undefined (type Keeper has no field or method GetAllTaskInfos)

Check failure on line 62 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.GetAllTaskInfos undefined (type Keeper has no field or method GetAllTaskInfos)

Check failure on line 62 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.GetAllTaskInfos undefined (type Keeper has no field or method GetAllTaskInfos)

Check failure on line 62 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / build

k.GetAllTaskInfos undefined (type Keeper has no field or method GetAllTaskInfos)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all task infos").Error())
}

res.BlsPubKeys, err = k.GetAllBlsPubKeys(ctx)

Check failure on line 67 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

k.GetAllBlsPubKeys undefined (type Keeper has no field or method GetAllBlsPubKeys)

Check failure on line 67 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.GetAllBlsPubKeys undefined (type Keeper has no field or method GetAllBlsPubKeys)

Check failure on line 67 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.GetAllBlsPubKeys undefined (type Keeper has no field or method GetAllBlsPubKeys)

Check failure on line 67 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / build

k.GetAllBlsPubKeys undefined (type Keeper has no field or method GetAllBlsPubKeys)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all bls key info").Error())
}

res.TaskNums, err = k.GetAllTaskNums(ctx)

Check failure on line 72 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

k.GetAllTaskNums undefined (type Keeper has no field or method GetAllTaskNums)

Check failure on line 72 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.GetAllTaskNums undefined (type Keeper has no field or method GetAllTaskNums)

Check failure on line 72 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / build

k.GetAllTaskNums undefined (type Keeper has no field or method GetAllTaskNums)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all TaskNums").Error())
}

res.TaskResultInfos, err = k.GetAllTaskResultInfos(ctx)

Check failure on line 77 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

k.GetAllTaskResultInfos undefined (type Keeper has no field or method GetAllTaskResultInfos)

Check failure on line 77 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.GetAllTaskResultInfos undefined (type Keeper has no field or method GetAllTaskResultInfos)

Check failure on line 77 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / build

k.GetAllTaskResultInfos undefined (type Keeper has no field or method GetAllTaskResultInfos)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all TaskResultInfos").Error())
}

res.ChallengeInfos, err = k.GetAllChallengeInfos(ctx)

Check failure on line 82 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

k.GetAllChallengeInfos undefined (type Keeper has no field or method GetAllChallengeInfos)

Check failure on line 82 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.GetAllChallengeInfos undefined (type Keeper has no field or method GetAllChallengeInfos)

Check failure on line 82 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / build

k.GetAllChallengeInfos undefined (type Keeper has no field or method GetAllChallengeInfos)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all ChallengeInfos").Error())
}

res.ChainIdInfos, err = k.GetAllChainIdInfos(ctx)

Check failure on line 87 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

k.GetAllChainIdInfos undefined (type Keeper has no field or method GetAllChainIdInfos) (typecheck)

Check failure on line 87 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

k.GetAllChainIdInfos undefined (type Keeper has no field or method GetAllChainIdInfos)

Check failure on line 87 in x/avs/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / build

k.GetAllChainIdInfos undefined (type Keeper has no field or method GetAllChainIdInfos)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all ChainIdInfos").Error())
}

return &res
}
17 changes: 16 additions & 1 deletion x/avs/keeper/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (k Keeper) GetTaskID(ctx sdk.Context, taskAddr common.Address) uint64 {
store.Set(taskAddr.Bytes(), sdk.Uint64ToBigEndian(id))
return id
}
func (k Keeper) SetTaskID(ctx sdk.Context, taskAddr common.Address, id uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixLatestTaskNum)
store.Set(taskAddr.Bytes(), sdk.Uint64ToBigEndian(id))
}

// SetTaskResultInfo is used to store the operator's sign task information.
func (k *Keeper) SetTaskResultInfo(
Expand Down Expand Up @@ -371,7 +375,18 @@ func (k *Keeper) SetTaskChallengedInfo(

return nil
}

func (k *Keeper) SetAllTaskChallengedInfo(ctx sdk.Context, states []types.ChallengeInfo) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTaskChallengeResult)
for i := range states {
state := states[i]
bz, err := sdk.AccAddressFromBech32(state.ChallengeAddr)
if err != nil {
return err
}
store.Set([]byte(state.Key), bz)
}
return nil
}
func (k *Keeper) IsExistTaskChallengedInfo(ctx sdk.Context, operatorAddress, taskContractAddress string, taskID uint64) bool {
infoKey := assetstype.GetJoinedStoreKey(operatorAddress, taskContractAddress,
strconv.FormatUint(taskID, 10))
Expand Down
49 changes: 49 additions & 0 deletions x/feedistribution/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
"github.com/ExocoreNetwork/exocore/x/feedistribution/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -15,11 +16,59 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
// is not running. it means that the genesis file is malformed.
panic("not found the epoch info")
}

// Set fee pool
k.SetFeePool(ctx, &genState.FeePool)

// Set all the validatorAccumulatedCommission
for _, elem := range genState.ValidatorAccumulatedCommissions {
k.SetValidatorAccumulatedCommission(ctx, sdk.ValAddress(elem.ValAddr), *elem.Commission)
}

// Set all the validatorCurrentRewards
for _, elem := range genState.ValidatorCurrentRewardsList {
k.SetValidatorCurrentRewards(ctx, sdk.ValAddress(elem.ValAddr), *elem.CurrentRewards)
}

// Set all the validatorOutstandingRewards
for _, elem := range genState.ValidatorOutstandingRewardsList {
k.SetValidatorOutstandingRewards(ctx, sdk.ValAddress(elem.ValAddr), *elem.OutstandingRewards)
}

// Set all the stakerRewards
for _, elem := range genState.StakerOutstandingRewardsList {
k.SetStakerRewards(ctx, elem.ValAddr, *elem.StakerOutstandingRewards)
}

}

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

feelPool := k.GetFeePool(ctx)

genesis.FeePool = *feelPool

genesis.ValidatorAccumulatedCommissions, err := k.GetAllValidatorAccumulatedCommissions(ctx)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all ValidatorAccumulatedCommissions").Error())
}

genesis.ValidatorCurrentRewardsList, err = k.GetAllValidatorCurrentRewardsList(ctx)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all ValidatorCurrentRewardsList").Error())
}

genesis.ValidatorOutstandingRewardsList, err = k.GetAllValidatorOutstandingRewardsList(ctx)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all ValidatorOutstandingRewardsList").Error())
}

genesis.StakerOutstandingRewardsList, err = k.GetAllStakerOutstandingRewardsList(ctx)
if err != nil {
panic(errorsmod.Wrap(err, "failed to get all StakerOutstandingRewardsList").Error())
}
return genesis
}
Loading

0 comments on commit 96407c6

Please sign in to comment.