Skip to content

Commit

Permalink
[R4R] Add deposit address and do some refactor (#101)
Browse files Browse the repository at this point in the history
* add address for deposits

* refactor

* refactor

* revert change

* refactor

* refactor

* change proposals to publish

* update deposit address
  • Loading branch information
yutianwu authored Mar 29, 2019
1 parent f3c94ce commit 115a52b
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 93 deletions.
2 changes: 1 addition & 1 deletion cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.govKeeper = gov.NewKeeper(
app.cdc,
app.keyGov,
app.paramsKeeper, app.paramsKeeper.Subspace(gov.DefaultParamspace), app.bankKeeper, app.stakeKeeper,
app.paramsKeeper, app.paramsKeeper.Subspace(gov.DefaultParamSpace), app.bankKeeper, app.stakeKeeper,
app.RegisterCodespace(gov.DefaultCodespace),
app.Pool,
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func NewMockGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppO
app.govKeeper = gov.NewKeeper(
app.cdc,
app.keyGov,
app.paramsKeeper, app.paramsKeeper.Subspace(gov.DefaultParamspace), app.bankKeeper, app.stakeKeeper,
app.paramsKeeper, app.paramsKeeper.Subspace(gov.DefaultParamSpace), app.bankKeeper, app.stakeKeeper,
app.RegisterCodespace(gov.DefaultCodespace),
app.Pool,
)
Expand Down
24 changes: 19 additions & 5 deletions x/gov/endblocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestTickExpiredDepositPeriod(t *testing.T) {
gov.EndBlocker(ctx, keeper)
require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx))
require.False(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
require.Equal(t, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 1000e8)}, ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))

newHeader := ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(1) * time.Second)
Expand All @@ -47,7 +48,7 @@ func TestTickExpiredDepositPeriod(t *testing.T) {
require.False(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))

newHeader = ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod)
newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositParams(ctx).MaxDepositPeriod)
ctx = ctx.WithBlockHeader(newHeader)

require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx))
Expand All @@ -57,13 +58,14 @@ func TestTickExpiredDepositPeriod(t *testing.T) {
validatorCoins := ck.GetCoins(ctx, addrs[0])
// check distribute deposits to proposer
require.Equal(t, validatorCoins, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 6000e8)})
require.Equal(t, sdk.Coins(nil), ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))

require.Nil(t, keeper.InactiveProposalQueuePeek(ctx))
require.False(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
}

func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
mapp, _, keeper, stakeKeeper, addrs, pubKeys, _ := getMockApp(t, 10)
mapp, ck, keeper, stakeKeeper, addrs, pubKeys, _ := getMockApp(t, 10)

validator := stake.NewValidator(sdk.ValAddress(addrs[0]), pubKeys[0], stake.Description{})
mapp.BeginBlock(abci.RequestBeginBlock{})
Expand All @@ -86,6 +88,7 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
gov.EndBlocker(ctx, keeper)
require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx))
require.False(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
require.Equal(t, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 1000e8)}, ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))

newHeader := ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(2) * time.Second)
Expand All @@ -100,14 +103,15 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
require.True(t, res.IsOK())

newHeader = ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositProcedure(ctx).MaxDepositPeriod).Add(time.Duration(-1) * time.Second)
newHeader.Time = ctx.BlockHeader().Time.Add(keeper.GetDepositParams(ctx).MaxDepositPeriod).Add(time.Duration(-1) * time.Second)
ctx = ctx.WithBlockHeader(newHeader)

require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx))
require.True(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
gov.EndBlocker(ctx, keeper)
require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx))
require.False(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
require.Equal(t, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 5)}, ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))

newHeader = ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(5) * time.Second)
Expand All @@ -118,10 +122,11 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
gov.EndBlocker(ctx, keeper)
require.Nil(t, keeper.InactiveProposalQueuePeek(ctx))
require.False(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
require.Equal(t, sdk.Coins(nil), ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))
}

func TestTickPassedDepositPeriod(t *testing.T) {
mapp, _, keeper, _, addrs, _, _ := getMockApp(t, 10)
mapp, ck, keeper, _, addrs, _, _ := getMockApp(t, 10)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(sdk.RunTxModeDeliver, abci.Header{})
govHandler := gov.NewHandler(keeper)
Expand All @@ -141,6 +146,7 @@ func TestTickPassedDepositPeriod(t *testing.T) {
gov.EndBlocker(ctx, keeper)
require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx))
require.False(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
require.Equal(t, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 1000e8)}, ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))

newHeader := ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(time.Duration(1) * time.Second)
Expand All @@ -157,14 +163,14 @@ func TestTickPassedDepositPeriod(t *testing.T) {
require.NotNil(t, keeper.InactiveProposalQueuePeek(ctx))
require.True(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
require.NotNil(t, keeper.ActiveProposalQueuePeek(ctx))
require.Equal(t, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 2000e8)}, ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))

gov.EndBlocker(ctx, keeper)

require.Nil(t, keeper.InactiveProposalQueuePeek(ctx))
require.False(t, gov.ShouldPopInactiveProposalQueue(ctx, keeper))
require.NotNil(t, keeper.ActiveProposalQueuePeek(ctx))
require.False(t, gov.ShouldPopActiveProposalQueue(ctx, keeper))

}

func TestTickPassedVotingPeriodRejected(t *testing.T) {
Expand Down Expand Up @@ -210,6 +216,8 @@ func TestTickPassedVotingPeriodRejected(t *testing.T) {
newVoteMsg := gov.NewMsgVote(addrs[0], int64(proposalID), gov.OptionNo)
res = govHandler(ctx, newVoteMsg)
require.True(t, res.IsOK())
require.Equal(t, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 2000e8)}, ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))

gov.EndBlocker(ctx, keeper)

// pass voting period
Expand All @@ -234,6 +242,7 @@ func TestTickPassedVotingPeriodRejected(t *testing.T) {
// check distribute deposits to proposer
validatorCoins := ck.GetCoins(ctx, addrs[0])
require.Equal(t, validatorCoins, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 6000e8)})
require.Equal(t, sdk.Coins(nil), ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))
}

func TestTickPassedVotingPeriodPassed(t *testing.T) {
Expand Down Expand Up @@ -273,6 +282,7 @@ func TestTickPassedVotingPeriodPassed(t *testing.T) {
newDepositMsg := gov.NewMsgDeposit(addrs[1], int64(proposalID), sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 1000e8)})
res = govHandler(ctx, newDepositMsg)
require.True(t, res.IsOK())
require.Equal(t, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 2000e8)}, ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))
gov.EndBlocker(ctx, keeper)

newHeader = ctx.BlockHeader()
Expand Down Expand Up @@ -305,6 +315,7 @@ func TestTickPassedVotingPeriodPassed(t *testing.T) {
// check refund deposits
validatorCoins := ck.GetCoins(ctx, addrs[0])
require.Equal(t, validatorCoins, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 5000e8)})
require.Equal(t, sdk.Coins(nil), ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))
}

func TestTickPassedVotingPeriodUnreachedQuorum(t *testing.T) {
Expand Down Expand Up @@ -357,6 +368,8 @@ func TestTickPassedVotingPeriodUnreachedQuorum(t *testing.T) {
newVoteMsg := gov.NewMsgVote(addrs[0], int64(proposalID), gov.OptionYes)
res = govHandler(ctx, newVoteMsg)
require.True(t, res.IsOK())
require.Equal(t, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 2000e8)}, ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))

gov.EndBlocker(ctx, keeper)

// pass voting period
Expand All @@ -381,6 +394,7 @@ func TestTickPassedVotingPeriodUnreachedQuorum(t *testing.T) {
// check refund deposits
validatorCoins := ck.GetCoins(ctx, addrs[0])
require.Equal(t, validatorCoins, sdk.Coins{sdk.NewCoin(gov.DefaultDepositDenom, 5000e8)})
require.Equal(t, sdk.Coins(nil), ck.GetCoins(ctx, gov.DepositedCoinsAccAddr))
}

func TestTickPassedVotingPeriodAllAbstain(t *testing.T) {
Expand Down
28 changes: 14 additions & 14 deletions x/gov/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ const (

// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
StartingProposalID int64 `json:"starting_proposalID"`
DepositProcedure DepositProcedure `json:"deposit_period"`
TallyingProcedure TallyingProcedure `json:"tallying_procedure"`
StartingProposalID int64 `json:"starting_proposalID"`
DepositParams DepositParams `json:"deposit_params"`
TallyParams TallyParams `json:"tally_params"`
}

func NewGenesisState(startingProposalID int64, dp DepositProcedure, tp TallyingProcedure) GenesisState {
func NewGenesisState(startingProposalID int64, dp DepositParams, tp TallyParams) GenesisState {
return GenesisState{
StartingProposalID: startingProposalID,
DepositProcedure: dp,
TallyingProcedure: tp,
DepositParams: dp,
TallyParams: tp,
}
}

// get raw genesis raw message for testing
func DefaultGenesisState() GenesisState {
return GenesisState{
StartingProposalID: 1,
DepositProcedure: DepositProcedure{
DepositParams: DepositParams{
MinDeposit: sdk.Coins{sdk.NewCoin(DefaultDepositDenom, 2000e8)},
MaxDepositPeriod: time.Duration(2*24) * time.Hour, // 2 days
},
TallyingProcedure: TallyingProcedure{
TallyParams: TallyParams{
Quorum: sdk.NewDecWithPrec(5, 1),
Threshold: sdk.NewDecWithPrec(5, 1),
Veto: sdk.NewDecWithPrec(334, 3),
Expand All @@ -48,19 +48,19 @@ func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) {
// TODO: Handle this with #870
panic(err)
}
k.setDepositProcedure(ctx, data.DepositProcedure)
k.setTallyingProcedure(ctx, data.TallyingProcedure)
k.setDepositParams(ctx, data.DepositParams)
k.setTallyParams(ctx, data.TallyParams)
}

// WriteGenesis - output genesis parameters
func WriteGenesis(ctx sdk.Context, k Keeper) GenesisState {
startingProposalID, _ := k.getNewProposalID(ctx)
depositProcedure := k.GetDepositProcedure(ctx)
tallyingProcedure := k.GetTallyingProcedure(ctx)
depositParams := k.GetDepositParams(ctx)
tallyingParams := k.GetTallyParams(ctx)

return GenesisState{
StartingProposalID: startingProposalID,
DepositProcedure: depositProcedure,
TallyingProcedure: tallyingProcedure,
DepositParams: depositParams,
TallyParams: tallyingParams,
}
}
20 changes: 12 additions & 8 deletions x/gov/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func handleMsgVote(ctx sdk.Context, keeper Keeper, msg MsgVote) sdk.Result {
}

// Called every block, process inflation, update validator set
func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags, passedProposals, failedProposals []int64) {
func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags, refundProposals, notRefundProposals []int64) {

logger := ctx.Logger().With("module", "x/gov")

resTags = sdk.NewTags()
passedProposals = make([]int64, 0)
failedProposals = make([]int64, 0)
refundProposals = make([]int64, 0)
notRefundProposals = make([]int64, 0)

// Delete proposals that haven't met minDeposit
for ShouldPopInactiveProposalQueue(ctx, keeper) {
Expand All @@ -132,14 +132,17 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags, passedProposa
keeper.DistributeDeposits(ctx, inactiveProposal.GetProposalID())

keeper.DeleteProposal(ctx, inactiveProposal)

notRefundProposals = append(notRefundProposals, inactiveProposal.GetProposalID())

resTags.AppendTag(tags.Action, tags.ActionProposalDropped)
resTags.AppendTag(tags.ProposalID, proposalIDBytes)

logger.Info(
fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %v (had only %v); distribute to validator",
inactiveProposal.GetProposalID(),
inactiveProposal.GetTitle(),
keeper.GetDepositProcedure(ctx).MinDeposit,
keeper.GetDepositParams(ctx).MinDeposit,
inactiveProposal.GetTotalDeposit(),
),
)
Expand All @@ -164,18 +167,19 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags, passedProposa

// refund deposits
keeper.RefundDeposits(ctx, activeProposal.GetProposalID())
passedProposals = append(passedProposals, activeProposal.GetProposalID())
refundProposals = append(refundProposals, activeProposal.GetProposalID())
} else {
activeProposal.SetStatus(StatusRejected)
action = tags.ActionProposalRejected

// if votes reached quorum and not all votes are abstain, distribute deposits to validator, else refund deposits
if refundDeposits {
keeper.RefundDeposits(ctx, activeProposal.GetProposalID())
refundProposals = append(refundProposals, activeProposal.GetProposalID())
} else {
keeper.DistributeDeposits(ctx, activeProposal.GetProposalID())
notRefundProposals = append(notRefundProposals, activeProposal.GetProposalID())
}
failedProposals = append(failedProposals, activeProposal.GetProposalID())
}

activeProposal.SetTallyResult(tallyResults)
Expand All @@ -192,14 +196,14 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags, passedProposa
}

func ShouldPopInactiveProposalQueue(ctx sdk.Context, keeper Keeper) bool {
depositProcedure := keeper.GetDepositProcedure(ctx)
depositParams := keeper.GetDepositParams(ctx)
peekProposal := keeper.InactiveProposalQueuePeek(ctx)

if peekProposal == nil {
return false
} else if peekProposal.GetStatus() != StatusDepositPeriod {
return true
} else if !ctx.BlockHeader().Time.Before(peekProposal.GetSubmitTime().Add(depositProcedure.MaxDepositPeriod)) {
} else if !ctx.BlockHeader().Time.Before(peekProposal.GetSubmitTime().Add(depositParams.MaxDepositPeriod)) {
return true
}
return false
Expand Down
Loading

0 comments on commit 115a52b

Please sign in to comment.