Skip to content

Commit

Permalink
Add POE setup to genesis (#167)
Browse files Browse the repository at this point in the history
* Add POE setup to genesis

* Move bonded denum up in genesis
  • Loading branch information
alpe authored Oct 27, 2021
1 parent e4feef8 commit 87727f4
Show file tree
Hide file tree
Showing 6 changed files with 531 additions and 105 deletions.
19 changes: 19 additions & 0 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [PoEContractType](#confio.poe.v1beta1.PoEContractType)

- [confio/poe/v1beta1/genesis.proto](#confio/poe/v1beta1/genesis.proto)
- [EngagementContractConfig](#confio.poe.v1beta1.EngagementContractConfig)
- [GenesisState](#confio.poe.v1beta1.GenesisState)
- [PoEContract](#confio.poe.v1beta1.PoEContract)
- [StakeContractConfig](#confio.poe.v1beta1.StakeContractConfig)
Expand Down Expand Up @@ -230,6 +231,21 @@ PoEContractType type of PoE contract



<a name="confio.poe.v1beta1.EngagementContractConfig"></a>

### EngagementContractConfig
EngagementContractConfig initial setup config


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `halflife` | [google.protobuf.Duration](#google.protobuf.Duration) | | |






<a name="confio.poe.v1beta1.GenesisState"></a>

### GenesisState
Expand All @@ -246,6 +262,7 @@ GenesisState - initial state of module
| `engagement` | [TG4Member](#confio.poe.v1beta1.TG4Member) | repeated | Engagement weighted members of the engagement group. Validators should be in here. |
| `stake_contract_config` | [StakeContractConfig](#confio.poe.v1beta1.StakeContractConfig) | | |
| `valset_contract_config` | [ValsetContractConfig](#confio.poe.v1beta1.ValsetContractConfig) | | |
| `engagment_contract_config` | [EngagementContractConfig](#confio.poe.v1beta1.EngagementContractConfig) | | |
| `bond_denom` | [string](#string) | | BondDenom defines the bondable coin denomination. |


Expand Down Expand Up @@ -318,6 +335,8 @@ ValsetContractConfig initial setup config
| `epoch_reward` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |
| `scaling` | [uint32](#uint32) | | |
| `fee_percentage` | [string](#string) | | |
| `ValidatorsRewardRatio` | [uint32](#uint32) | | ValidatorsRewardRatio in percentage |
| `AutoUnjail` | [bool](#bool) | | AutoUnjail if set to true, we will auto-unjail any validator after their jailtime is over. |



Expand Down
15 changes: 14 additions & 1 deletion proto/confio/poe/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ message GenesisState {

ValsetContractConfig valset_contract_config = 8
[ (gogoproto.jsontag) = "valset_contract_config,omitempty" ];
EngagementContractConfig engagment_contract_config = 9
[ (gogoproto.jsontag) = "engagment_contract_config,omitempty" ];
// BondDenom defines the bondable coin denomination.
string bond_denom = 9 [ (gogoproto.moretags) = "yaml:\"bond_denom\"" ];
string bond_denom = 10 [ (gogoproto.moretags) = "yaml:\"bond_denom\"" ];
}

// StakeContractConfig initial setup config
Expand All @@ -75,6 +77,17 @@ message ValsetContractConfig {
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"
];
// ValidatorsRewardRatio in percentage
uint32 ValidatorsRewardRatio = 7;
// AutoUnjail if set to true, we will auto-unjail any validator after their
// jailtime is over.
bool AutoUnjail = 8;
}

// EngagementContractConfig initial setup config
message EngagementContractConfig {
google.protobuf.Duration halflife = 1
[ (gogoproto.nullable) = false, (gogoproto.stdduration) = true ];
}

// PoEContract address and type information
Expand Down
31 changes: 14 additions & 17 deletions x/poe/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,19 @@ type poeKeeper interface {
//
// * [tg4-group](https://github.com/confio/tgrade-contracts/tree/main/contracts/tg4-group) - engagement group with weighted
// members
//* [tg4-stake](https://github.com/confio/tgrade-contracts/tree/main/contracts/tg4-stake) - validator group weighted by
// * [tg4-stake](https://github.com/confio/tgrade-contracts/tree/main/contracts/tg4-stake) - validator group weighted by
// staked amount
//* [valset](https://github.com/confio/tgrade-contracts/tree/main/contracts/tgrade-valset) - privileged contract to map a
// * [valset](https://github.com/confio/tgrade-contracts/tree/main/contracts/tgrade-valset) - privileged contract to map a
// trusted cw4 contract to the Tendermint validator set running the chain
//* [mixer](https://github.com/confio/tgrade-contracts/tree/main/contracts/tg4-mixer) - calculates the combined value of
// * [mixer](https://github.com/confio/tgrade-contracts/tree/main/contracts/tg4-mixer) - calculates the combined value of
// stake and engagement points. Source for the valset contract.
func bootstrapPoEContracts(ctx sdk.Context, k wasmtypes.ContractOpsKeeper, tk twasmKeeper, poeKeeper poeKeeper, gs types.GenesisState) error {
tg4EngagementInitMsg := contract.TG4EngagementInitMsg{
Admin: gs.SystemAdminAddress,
Members: make([]contract.TG4Member, len(gs.Engagement)),
Preauths: 1,
Token: gs.BondDenom,
// TODO: allow us to configure halflife in Genesis
// now hardcoded as 180 days = 180 * 86400s
Halflife: 15552000,
Halflife: uint64(gs.EngagmentContractConfig.Halflife.Seconds()),
}
for i, v := range gs.Engagement {
tg4EngagementInitMsg.Members[i] = contract.TG4Member{
Expand Down Expand Up @@ -174,17 +172,16 @@ func newValsetInitMsg(
engagementID uint64,
) contract.ValsetInitMsg {
return contract.ValsetInitMsg{
Membership: mixerContractAddr.String(),
MinWeight: gs.ValsetContractConfig.MinWeight,
MaxValidators: gs.ValsetContractConfig.MaxValidators,
EpochLength: uint64(gs.ValsetContractConfig.EpochLength.Seconds()),
EpochReward: gs.ValsetContractConfig.EpochReward,
InitialKeys: []contract.Validator{},
Scaling: gs.ValsetContractConfig.Scaling,
FeePercentage: contract.DecimalFromPercentage(gs.ValsetContractConfig.FeePercentage),
// TODO: set AutoJail from genesis
// TODO: set ValidatorsRewardRatio from genesis (hardcode to 50% here)
ValidatorsRewardRatio: contract.DecimalFromPercentage(sdk.NewDec(50)),
Membership: mixerContractAddr.String(),
MinWeight: gs.ValsetContractConfig.MinWeight,
MaxValidators: gs.ValsetContractConfig.MaxValidators,
EpochLength: uint64(gs.ValsetContractConfig.EpochLength.Seconds()),
EpochReward: gs.ValsetContractConfig.EpochReward,
InitialKeys: []contract.Validator{},
Scaling: gs.ValsetContractConfig.Scaling,
FeePercentage: contract.DecimalFromPercentage(gs.ValsetContractConfig.FeePercentage),
AutoUnjail: gs.ValsetContractConfig.AutoUnjail,
ValidatorsRewardRatio: contract.DecimalFromPercentage(sdk.NewDec(int64(gs.ValsetContractConfig.ValidatorsRewardRatio))),
DistributionContract: engagementAddr.String(),
RewardsCodeId: engagementID,
}
Expand Down
41 changes: 32 additions & 9 deletions x/poe/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,41 @@ func DefaultGenesisState() GenesisState {
PreAuths: 1,
},
ValsetContractConfig: &ValsetContractConfig{
MinWeight: 1,
MaxValidators: 100,
EpochLength: 60 * time.Second,
EpochReward: sdk.NewCoin(DefaultBondDenom, sdk.NewInt(100_000)),
Scaling: 1,
FeePercentage: sdk.NewDec(50),
MinWeight: 1,
MaxValidators: 100,
EpochLength: 60 * time.Second,
EpochReward: sdk.NewCoin(DefaultBondDenom, sdk.NewInt(100_000)),
Scaling: 1,
FeePercentage: sdk.NewDec(50),
AutoUnjail: false,
ValidatorsRewardRatio: 50,
},
EngagmentContractConfig: &EngagementContractConfig{
Halflife: 180 * 24 * time.Hour,
},
SystemAdminAddress: sdk.AccAddress(rand.Bytes(sdk.AddrLen)).String(),
Params: DefaultParams(),
}
}

func ValidateGenesis(g GenesisState, txJSONDecoder sdk.TxDecoder) error {
if err := sdk.ValidateDenom(g.BondDenom); err != nil {
return sdkerrors.Wrap(err, "bond denom")
}
if g.SeedContracts {
if len(g.Contracts) != 0 {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "seed enabled but PoE contracts addresses provided")
}
if len(g.Engagement) == 0 {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty engagement group")
}
if g.EngagmentContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty engagement contract config")
}
if err := g.EngagmentContractConfig.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "engagement contract config")
}
if err := sdk.ValidateDenom(g.BondDenom); err != nil {
return sdkerrors.Wrap(err, "bond denom")
}

if g.ValsetContractConfig == nil {
return sdkerrors.Wrap(wasmtypes.ErrInvalidGenesis, "empty valset contract config")
}
Expand Down Expand Up @@ -128,6 +140,14 @@ func ValidateGenesis(g GenesisState, txJSONDecoder sdk.TxDecoder) error {
return nil
}

// ValidateBasic ensure basic constraints
func (c *EngagementContractConfig) ValidateBasic() error {
if c.Halflife.Truncate(time.Second) != c.Halflife {
return sdkerrors.Wrap(ErrInvalid, "halflife must not contain anything less than seconds")
}
return nil
}

// ValidateBasic ensure basic constraints
func (c ValsetContractConfig) ValidateBasic() error {
if c.MaxValidators == 0 {
Expand All @@ -139,6 +159,9 @@ func (c ValsetContractConfig) ValidateBasic() error {
if c.Scaling == 0 {
return sdkerrors.Wrap(ErrEmpty, "scaling")
}
if c.ValidatorsRewardRatio > 100 {
return sdkerrors.Wrap(ErrInvalid, "validator reward ratio must not be greater 100")
}

minFeePercentage := sdk.NewDecFromIntWithPrec(sdk.OneInt(), 16)
if c.FeePercentage.LT(minFeePercentage) {
Expand Down
Loading

0 comments on commit 87727f4

Please sign in to comment.