Skip to content

Commit

Permalink
fix: fix x/foundation EndBlocker (#712)
Browse files Browse the repository at this point in the history
* Fix EndBlocker

* Update CHANGELOG.md
  • Loading branch information
0Tech authored Oct 17, 2022
1 parent 2eb6fd9 commit 00ab960
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/foundation) [\#687](https://github.com/line/lbm-sdk/pull/687) fix bugs on aborting x/foundation proposals
* (global) [\#694](https://github.com/line/lbm-sdk/pull/694) replace deprecated functions since go 1.16 or 1.17
* (x/bankplus) [\#705](https://github.com/line/lbm-sdk/pull/705) add missing blockedAddr checking in bankplus
* (x/foundation) [\#712](https://github.com/line/lbm-sdk/pull/712) fix x/foundation EndBlocker

### Breaking Changes
* (proto) [\#564](https://github.com/line/lbm-sdk/pull/564) change gRPC path to original cosmos path
Expand Down
72 changes: 72 additions & 0 deletions x/foundation/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,75 @@ func (s *KeeperTestSuite) TestBeginBlocker() {
// s.balance + s.balance * 0.5
s.Require().Equal(sdk.NewDecFromInt(s.balance.Add(s.balance.Quo(sdk.NewInt(2)))), after[0].Amount)
}

func (s *KeeperTestSuite) TestEndBlocker() {
ctx, _ := s.ctx.CacheContext()

// check preconditions
for name, tc := range map[string]struct {
id uint64
status foundation.ProposalStatus
}{
"active proposal": {
s.activeProposal,
foundation.PROPOSAL_STATUS_SUBMITTED,
},
"voted proposal": {
s.votedProposal,
foundation.PROPOSAL_STATUS_SUBMITTED,
},
"withdrawn proposal": {
s.withdrawnProposal,
foundation.PROPOSAL_STATUS_WITHDRAWN,
},
"invalid proposal": {
s.invalidProposal,
foundation.PROPOSAL_STATUS_SUBMITTED,
},
} {
proposal, err := s.keeper.GetProposal(ctx, tc.id)
s.Require().NoError(err, name)
s.Require().NotNil(proposal, name)
s.Require().Equal(tc.status, proposal.Status, name)
}

// voting periods end
votingPeriod := s.keeper.GetFoundationInfo(ctx).GetDecisionPolicy().GetVotingPeriod()
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(votingPeriod))
keeper.EndBlocker(ctx, s.keeper)

for name, tc := range map[string]struct {
id uint64
status foundation.ProposalStatus
}{
"active proposal": {
s.activeProposal,
foundation.PROPOSAL_STATUS_ACCEPTED,
},
"voted proposal": {
s.votedProposal,
foundation.PROPOSAL_STATUS_ACCEPTED,
},
"withdrawn proposal": {
s.withdrawnProposal,
foundation.PROPOSAL_STATUS_WITHDRAWN,
},
"invalid proposal": {
s.invalidProposal,
foundation.PROPOSAL_STATUS_ACCEPTED,
},
} {
proposal, err := s.keeper.GetProposal(ctx, tc.id)
s.Require().NoError(err, name)
s.Require().NotNil(proposal, name)
s.Require().Equal(tc.status, proposal.Status, name)
}

// proposals expire
maxExecutionPeriod := foundation.DefaultConfig().MaxExecutionPeriod
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(maxExecutionPeriod))
keeper.EndBlocker(ctx, s.keeper)

// all proposals must be pruned
s.Require().Empty(s.keeper.GetProposals(ctx))
}
79 changes: 45 additions & 34 deletions x/foundation/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var (
poolKey = []byte{0x30}
)

// must be constant
var lenTime = len(sdk.FormatTimeBytes(time.Now()))

// Uint64FromBytes converts a byte array to uint64
func Uint64FromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
Expand All @@ -38,30 +41,36 @@ func Uint64ToBytes(number uint64) []byte {

// memberKey key for a specific member from the store
func memberKey(address sdk.AccAddress) []byte {
key := make([]byte, len(memberKeyPrefix)+len(address))
copy(key, memberKeyPrefix)
copy(key[len(memberKeyPrefix):], address)
prefix := memberKeyPrefix
key := make([]byte, len(prefix)+len(address))

copy(key, prefix)
copy(key[len(prefix):], address)

return key
}

// proposalKey key for a specific proposal from the store
func proposalKey(id uint64) []byte {
prefix := proposalKeyPrefix
idBz := Uint64ToBytes(id)
key := make([]byte, len(prefix)+len(idBz))

copy(key, prefix)
copy(key[len(prefix):], idBz)

key := make([]byte, len(proposalKeyPrefix)+len(idBz))
copy(key, proposalKeyPrefix)
copy(key[len(proposalKeyPrefix):], idBz)
return key
}

func voteKey(proposalID uint64, voter sdk.AccAddress) []byte {
prefix := voteKeyPrefix
idBz := Uint64ToBytes(proposalID)
key := make([]byte, len(voteKeyPrefix)+len(idBz)+len(voter))
key := make([]byte, len(prefix)+len(idBz)+len(voter))

begin := 0
copy(key[begin:], voteKeyPrefix)
copy(key[begin:], prefix)

begin += len(voteKeyPrefix)
begin += len(prefix)
copy(key[begin:], idBz)

begin += len(idBz)
Expand All @@ -70,39 +79,38 @@ func voteKey(proposalID uint64, voter sdk.AccAddress) []byte {
return key
}

func proposalByVPEndKey(id uint64, end time.Time) []byte {
func proposalByVPEndKey(vpEnd time.Time, id uint64) []byte {
prefix := proposalByVPEndKeyPrefix
vpEndBz := sdk.FormatTimeBytes(vpEnd)
idBz := Uint64ToBytes(id)
endBz := sdk.FormatTimeBytes(end)
key := make([]byte, len(proposalByVPEndKeyPrefix)+1+len(idBz)+len(endBz))
key := make([]byte, len(prefix)+lenTime+len(idBz))

begin := 0
copy(key[begin:], proposalByVPEndKeyPrefix)
copy(key[begin:], prefix)

begin += len(proposalByVPEndKeyPrefix)
key[begin] = byte(len(idBz))
begin += len(prefix)
copy(key[begin:], vpEndBz)

begin++
begin += len(vpEndBz)
copy(key[begin:], idBz)

begin += len(idBz)
copy(key[begin:], endBz)

return key
}

// func splitProposalByVPEndKey(key []byte) (proposalID uint64, vpEnd time.Time) {
// begin := len(proposalByVPEndKeyPrefix) + 1
// end := begin + int(key[begin-1]) // uint64
// proposalID = Uint64FromBytes(key[begin:end])
func splitProposalByVPEndKey(key []byte) (vpEnd time.Time, id uint64) {
prefix := proposalByVPEndKeyPrefix
begin := len(prefix)
end := begin + lenTime
vpEnd, err := sdk.ParseTimeBytes(key[begin:end])
if err != nil {
panic(err)
}

// begin = end
// vpEnd, err := sdk.ParseTimeBytes(key[begin:])
// if err != nil {
// panic(err)
// }
begin = end
id = Uint64FromBytes(key[begin:])

// return
// }
return
}

func grantKey(grantee sdk.AccAddress, url string) []byte {
prefix := grantKeyPrefixByGrantee(grantee)
Expand All @@ -115,12 +123,13 @@ func grantKey(grantee sdk.AccAddress, url string) []byte {
}

func grantKeyPrefixByGrantee(grantee sdk.AccAddress) []byte {
key := make([]byte, len(grantKeyPrefix)+1+len(grantee))
prefix := grantKeyPrefix
key := make([]byte, len(prefix)+1+len(grantee))

begin := 0
copy(key[begin:], grantKeyPrefix)
copy(key[begin:], prefix)

begin += len(grantKeyPrefix)
begin += len(prefix)
key[begin] = byte(len(grantee))

begin++
Expand All @@ -130,7 +139,9 @@ func grantKeyPrefixByGrantee(grantee sdk.AccAddress) []byte {
}

func splitGrantKey(key []byte) (grantee sdk.AccAddress, url string) {
begin := len(grantKeyPrefix) + 1
prefix := grantKeyPrefix

begin := len(prefix) + 1
end := begin + int(key[begin-1])
grantee = key[begin:end]

Expand Down
14 changes: 9 additions & 5 deletions x/foundation/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@ func (k Keeper) iterateProposalsByVPEnd(ctx sdk.Context, endTime time.Time, fn f
defer iter.Close()

for ; iter.Valid(); iter.Next() {
var proposal foundation.Proposal
k.cdc.MustUnmarshal(iter.Value(), &proposal)
_, id := splitProposalByVPEndKey(iter.Key())

proposal, err := k.GetProposal(ctx, id)
if err != nil {
panic(err)
}

if fn(proposal) {
if fn(*proposal) {
break
}
}
Expand Down Expand Up @@ -232,13 +236,13 @@ func (k Keeper) deleteProposal(ctx sdk.Context, proposalID uint64) {

func (k Keeper) addProposalToVPEndQueue(ctx sdk.Context, proposal foundation.Proposal) {
store := ctx.KVStore(k.storeKey)
key := proposalByVPEndKey(proposal.Id, proposal.VotingPeriodEnd)
key := proposalByVPEndKey(proposal.VotingPeriodEnd, proposal.Id)
store.Set(key, []byte{})
}

func (k Keeper) removeProposalFromVPEndQueue(ctx sdk.Context, proposal foundation.Proposal) {
store := ctx.KVStore(k.storeKey)
key := proposalByVPEndKey(proposal.Id, proposal.VotingPeriodEnd)
key := proposalByVPEndKey(proposal.VotingPeriodEnd, proposal.Id)
store.Delete(key)
}

Expand Down

0 comments on commit 00ab960

Please sign in to comment.