Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove oracle slashing #253

Merged
merged 8 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.stakingKeeper = *stakingKeeper.SetHooks(
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks(), app.oracleKeeper.StakingHooks()))
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks()))

app.mm = module.NewManager(
genaccounts.NewAppModule(app.accountKeeper),
Expand Down
24 changes: 23 additions & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/terra-project/core/x/oracle"
"github.com/terra-project/core/x/slashing"
"github.com/terra-project/core/x/staking"
)
Expand Down Expand Up @@ -164,7 +165,28 @@ func (app *TerraApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []s
},
)

/* Handle market state. */
/* Handle oracle state. */

// Clear all prevotes
app.oracleKeeper.IteratePrevotes(ctx, func(prevote oracle.PricePrevote) (stop bool) {
app.oracleKeeper.DeletePrevote(ctx, prevote)

return false
})

// Clear all votes
app.oracleKeeper.IterateVotes(ctx, func(vote oracle.PriceVote) (stop bool) {
app.oracleKeeper.DeleteVote(ctx, vote)
return false
})

// Clear all prices
app.oracleKeeper.IterateLunaPrices(ctx, func(denom string, price sdk.Dec) bool {
app.oracleKeeper.DeletePrice(ctx, denom)
return false
})

/* Handle market state. */

// clear all market pools
app.marketKeeper.SetTerraPoolDelta(ctx, sdk.ZeroDec())
Expand Down
52 changes: 0 additions & 52 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2334,58 +2334,6 @@ paths:
description: Bad Request
500:
description: Internal Server Error
/oracle/voters/{voter}/voting_info:
get:
summary: Get voting info of a voter
tags:
- Oracle
produces:
- application/json
parameters:
- in: path
name: voter
required: true
type: string
responses:
200:
description: OK
schema:
$ref: "#/definitions/VotingInfo"
400:
description: Bad Request
500:
description: Internal Server Error
/oracle/voting_infos:
get:
summary: Get voting infos
tags:
- Oracle
produces:
- application/json
parameters:
- in: query
name: page
description: Page number
type: integer
required: true
x-example: 1
- in: query
name: limit
description: Maximum number of items per page
type: integer
required: true
x-example: 5
responses:
200:
description: OK
schema:
type: array
items:
$ref: "#/definitions/VotingInfo"
400:
description: Bad Request
500:
description: Internal Server Error
/oracle/parameters:
get:
summary: Get oracle params
Expand Down
20 changes: 1 addition & 19 deletions x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/terra-project/core/x/oracle/internal/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/exported"

core "github.com/terra-project/core/types"
)
Expand All @@ -26,13 +25,6 @@ func EndBlocker(ctx sdk.Context, k Keeper) {
k.DeletePrice(ctx, activeDenom)
}

ballotAttendees := make(map[string]bool)
k.StakingKeeper.IterateBondedValidatorsByPower(ctx, func(_ int64, validator exported.ValidatorI) (stop bool) {
key := validator.GetOperator().String()
ballotAttendees[key] = true
return false
})

// Changes whitelist array to map for fast lookup
whitelistMap := make(map[string]bool)
for _, denom := range k.Whitelist(ctx) {
Expand All @@ -49,14 +41,7 @@ func EndBlocker(ctx sdk.Context, k Keeper) {
}

// Get weighted median prices, and faithful respondants
mod, ballotWinners, ballotLosers := tally(ctx, ballot, k)

for _, loser := range ballotLosers {
key := loser.String()
if _, exists := ballotAttendees[key]; exists {
ballotAttendees[key] = false // inproper vote
}
}
mod, ballotWinners := tally(ctx, ballot, k)

// Collect claims of ballot winners
for _, winner := range ballotWinners {
Expand Down Expand Up @@ -89,9 +74,6 @@ func EndBlocker(ctx sdk.Context, k Keeper) {
// Distribute rewards to ballot winners
k.RewardBallotWinners(ctx, claimPool)

// Update & check slash condition for the ballot losers
k.HandleBallotSlashing(ctx, ballotAttendees)

// Clear all prevotes
k.IteratePrevotes(ctx, func(prevote PricePrevote) (stop bool) {
if ctx.BlockHeight() > prevote.SubmitBlock+params.VotePeriod {
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestOracleTally(t *testing.T) {
}
}

tallyMedian, ballotWinner, _ := tally(input.Ctx, ballot, input.OracleKeeper)
tallyMedian, ballotWinner := tally(input.Ctx, ballot, input.OracleKeeper)

require.Equal(t, len(rewardees), len(ballotWinner))
require.Equal(t, tallyMedian.MulInt64(100).TruncateInt(), weightedMedian.MulInt64(100).TruncateInt())
Expand Down
11 changes: 0 additions & 11 deletions x/oracle/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,12 @@ var (
ErrInvalidMsgFormat = types.ErrInvalidMsgFormat
ErrNoVotingInfoFound = types.ErrNoVotingInfoFound
NewGenesisState = types.NewGenesisState
NewMissedVote = types.NewMissedVote
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
GetPrevoteKey = types.GetPrevoteKey
GetVoteKey = types.GetVoteKey
GetPriceKey = types.GetPriceKey
GetFeederDelegationKey = types.GetFeederDelegationKey
GetMissedVoteBitArrayPrefixKey = types.GetMissedVoteBitArrayPrefixKey
GetMissedVoteBitArrayKey = types.GetMissedVoteBitArrayKey
GetVotingInfoKey = types.GetVotingInfoKey
NewMsgPricePrevote = types.NewMsgPricePrevote
NewMsgPriceVote = types.NewMsgPriceVote
NewMsgDelegateFeederPermission = types.NewMsgDelegateFeederPermission
Expand All @@ -92,15 +88,10 @@ var (
VoteKey = types.VoteKey
PriceKey = types.PriceKey
FeederDelegationKey = types.FeederDelegationKey
MissedVoteBitArrayKey = types.MissedVoteBitArrayKey
VotingInfoKey = types.VotingInfoKey
ParamStoreKeyVotePeriod = types.ParamStoreKeyVotePeriod
ParamStoreKeyVoteThreshold = types.ParamStoreKeyVoteThreshold
ParamStoreKeyRewardBand = types.ParamStoreKeyRewardBand
ParamStoreKeyRewardDistributionPeriod = types.ParamStoreKeyRewardDistributionPeriod
ParamStoreKeyVotesWindow = types.ParamStoreKeyVotesWindow
ParamStoreKeyMinValidVotesPerWindow = types.ParamStoreKeyMinValidVotesPerWindow
ParamStoreKeySlashFraction = types.ParamStoreKeySlashFraction
ParamStoreKeyWhitelist = types.ParamStoreKeyWhitelist
DefaultVoteThreshold = types.DefaultVoteThreshold
DefaultRewardBand = types.DefaultRewardBand
Expand All @@ -119,7 +110,6 @@ type (
DistributionKeeper = types.DistributionKeeper
SupplyKeeper = types.SupplyKeeper
GenesisState = types.GenesisState
MissedVote = types.MissedVote
MsgPricePrevote = types.MsgPricePrevote
MsgPriceVote = types.MsgPriceVote
MsgDelegateFeederPermission = types.MsgDelegateFeederPermission
Expand All @@ -136,5 +126,4 @@ type (
PriceVotes = types.PriceVotes
VotingInfo = types.VotingInfo
Keeper = keeper.Keeper
Hooks = keeper.Hooks
)
36 changes: 0 additions & 36 deletions x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
GetCmdQueryActive(cdc),
GetCmdQueryParams(cdc),
GetCmdQueryFeederDelegation(cdc),
GetCmdQueryVotingInfo(cdc),
)...)

return oracleQueryCmd
Expand Down Expand Up @@ -265,38 +264,3 @@ $ terracli query oracle feeder terravaloper...

return cmd
}

// GetCmdQueryVotingInfo implements the command to query voting info.
func GetCmdQueryVotingInfo(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "voting-info [validator-addr]",
Short: "Query a validator's voting information",
Long: strings.TrimSpace(`Use a validators' address to find the voting-info for that validator:

$ <appcli> query oracle voting-info terravaloper...
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}

key := types.GetVotingInfoKey(valAddr)
res, _, err := cliCtx.QueryStore(key, types.QuerierRoute)
if err != nil {
return err
}

if len(res) == 0 {
return fmt.Errorf("Validator %s not found in oracle store", valAddr)
}

var votingInfo types.VotingInfo
cdc.MustUnmarshalBinaryLengthPrefixed(res, &votingInfo)
return cliCtx.PrintOutput(votingInfo)
},
}
}
57 changes: 33 additions & 24 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import (
// InitGenesis initialize default parameters
// and the keeper's address to pubkey map
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {

for addr, info := range data.VotingInfos {
address, err := sdk.ValAddressFromBech32(addr)
for delegatorBechAddr, delegatee := range data.FeederDelegations {
delegator, err := sdk.ValAddressFromBech32(delegatorBechAddr)
if err != nil {
panic(err)
}
keeper.SetVotingInfo(ctx, address, info)
keeper.SetFeedDelegate(ctx, delegator, delegatee)
}

for addr, array := range data.MissedVotes {
address, err := sdk.ValAddressFromBech32(addr)
if err != nil {
panic(err)
}
for _, missed := range array {
keeper.SetMissedVoteBitArray(ctx, address, missed.Index, missed.Missed)
}
for _, prevote := range data.PricePrevotes {
keeper.AddPrevote(ctx, prevote)
}

for _, vote := range data.PriceVotes {
keeper.AddVote(ctx, vote)
}

for denom, price := range data.Prices {
keeper.SetLunaPrice(ctx, denom, price)
}

keeper.SetParams(ctx, data.Params)
Expand All @@ -34,22 +35,30 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
// with InitGenesis
func ExportGenesis(ctx sdk.Context, keeper Keeper) (data GenesisState) {
params := keeper.GetParams(ctx)
votingInfos := make(map[string]VotingInfo)
missedVotes := make(map[string][]MissedVote)
keeper.IterateVotingInfos(ctx, func(info VotingInfo) (stop bool) {
bechAddr := info.Address.String()
feederDelegations := make(map[string]sdk.AccAddress)
keeper.IterateFeederDelegations(ctx, func(delegator sdk.ValAddress, delegatee sdk.AccAddress) (stop bool) {
bechAddr := delegator.String()
feederDelegations[bechAddr] = delegatee
return false
})

votingInfos[bechAddr] = info
localMissedBlocks := []MissedVote{}
var pricePrevotes []PricePrevote
keeper.IteratePrevotes(ctx, func(prevote PricePrevote) (stop bool) {
pricePrevotes = append(pricePrevotes, prevote)
return false
})

keeper.IterateMissedVoteBitArray(ctx, info.Address, func(index int64, missed bool) (stop bool) {
localMissedBlocks = append(localMissedBlocks, NewMissedVote(index, missed))
return false
})
missedVotes[bechAddr] = localMissedBlocks
var priceVotes []PriceVote
keeper.IterateVotes(ctx, func(vote PriceVote) (stop bool) {
priceVotes = append(priceVotes, vote)
return false
})

prices := make(map[string]sdk.Dec)
keeper.IterateLunaPrices(ctx, func(denom string, price sdk.Dec) bool {
prices[denom] = price
return false
})

return NewGenesisState(params, votingInfos, missedVotes)
return NewGenesisState(params, pricePrevotes, priceVotes, prices, feederDelegations)
}
24 changes: 24 additions & 0 deletions x/oracle/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package oracle

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/terra-project/core/x/oracle/internal/keeper"
)

func TestExportInitGenesis(t *testing.T) {
input := keeper.CreateTestInput(t)
input.OracleKeeper.AddPrevote(input.Ctx, NewPricePrevote("1234", "denom", sdk.ValAddress{}, int64(2)))
input.OracleKeeper.AddVote(input.Ctx, NewPriceVote(sdk.NewDec(1), "denom", sdk.ValAddress{}))
input.OracleKeeper.SetLunaPrice(input.Ctx, "denom", sdk.NewDec(123))
genesis := ExportGenesis(input.Ctx, input.OracleKeeper)

newInput := keeper.CreateTestInput(t)
InitGenesis(newInput.Ctx, newInput.OracleKeeper, genesis)
newGenesis := ExportGenesis(newInput.Ctx, newInput.OracleKeeper)

require.Equal(t, genesis, newGenesis)
}
Loading