Skip to content

Commit

Permalink
fix: apply foundation audit (#834)
Browse files Browse the repository at this point in the history
* Fix the field number

* Chore

* Fix RegisterInvariants

* Lint

* Use Run() in the tests

* Update CHANGELOG.md

* Revert breaking change

This reverts commit dba3509.
  • Loading branch information
0Tech authored Dec 26, 2022
1 parent 194b02d commit f26c56b
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 360 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes
* (client) [\#817](https://github.com/line/lbm-sdk/pull/817) remove support for composite (BLS) type

* (x/foundation) [#834](https://github.com/line/lbm-sdk/pull/834) Apply foundation audit

### Breaking Changes
* (rest) [\#807](https://github.com/line/lbm-sdk/pull/807) remove legacy REST API

Expand Down
18 changes: 10 additions & 8 deletions x/foundation/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ func TestReceiveFromTreasuryAuthorization(t *testing.T) {
}

for name, tc := range testCases {
authorization := &foundation.ReceiveFromTreasuryAuthorization{}
t.Run(name, func(t *testing.T) {
authorization := &foundation.ReceiveFromTreasuryAuthorization{}

resp, err := authorization.Accept(sdk.Context{}, tc.msg)
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)
resp, err := authorization.Accept(sdk.Context{}, tc.msg)
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)

require.Equal(t, tc.accept, resp.Accept)
require.Equal(t, tc.accept, resp.Accept)
})
}
}
276 changes: 147 additions & 129 deletions x/foundation/foundation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,33 @@ func TestThresholdDecisionPolicy(t *testing.T) {
}

for name, tc := range testCases {
policy := foundation.ThresholdDecisionPolicy{
Threshold: tc.threshold,
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: tc.votingPeriod,
MinExecutionPeriod: tc.minExecutionPeriod,
},
}
require.Equal(t, tc.votingPeriod, policy.GetVotingPeriod())

err := policy.ValidateBasic()
if !tc.validBasic {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)

info := foundation.FoundationInfo{
TotalWeight: tc.totalWeight,
}
err = policy.Validate(info, config)
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)
t.Run(name, func(t *testing.T) {
policy := foundation.ThresholdDecisionPolicy{
Threshold: tc.threshold,
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: tc.votingPeriod,
MinExecutionPeriod: tc.minExecutionPeriod,
},
}
require.Equal(t, tc.votingPeriod, policy.GetVotingPeriod())

err := policy.ValidateBasic()
if !tc.validBasic {
require.Error(t, err)
return
}
require.NoError(t, err)

info := foundation.FoundationInfo{
TotalWeight: tc.totalWeight,
}
err = policy.Validate(info, config)
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

Expand Down Expand Up @@ -171,17 +173,19 @@ func TestThresholdDecisionPolicyAllow(t *testing.T) {
}

for name, tc := range testCases {
result, err := policy.Allow(tc.tally, tc.totalWeight, tc.sinceSubmission)
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)

require.Equal(t, tc.final, result.Final, name)
if tc.final {
require.Equal(t, tc.allow, result.Allow, name)
}
t.Run(name, func(t *testing.T) {
result, err := policy.Allow(tc.tally, tc.totalWeight, tc.sinceSubmission)
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)

require.Equal(t, tc.final, result.Final)
if tc.final {
require.Equal(t, tc.allow, result.Allow)
}
})
}
}

Expand Down Expand Up @@ -231,31 +235,33 @@ func TestPercentageDecisionPolicy(t *testing.T) {
}

for name, tc := range testCases {
policy := foundation.PercentageDecisionPolicy{
Percentage: tc.percentage,
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: tc.votingPeriod,
MinExecutionPeriod: tc.minExecutionPeriod,
},
}
require.Equal(t, tc.votingPeriod, policy.GetVotingPeriod())

err := policy.ValidateBasic()
if !tc.validBasic {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)

info := foundation.FoundationInfo{
TotalWeight: tc.totalWeight,
}
err = policy.Validate(info, config)
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)
t.Run(name, func(t *testing.T) {
policy := foundation.PercentageDecisionPolicy{
Percentage: tc.percentage,
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: tc.votingPeriod,
MinExecutionPeriod: tc.minExecutionPeriod,
},
}
require.Equal(t, tc.votingPeriod, policy.GetVotingPeriod())

err := policy.ValidateBasic()
if !tc.validBasic {
require.Error(t, err)
return
}
require.NoError(t, err)

info := foundation.FoundationInfo{
TotalWeight: tc.totalWeight,
}
err = policy.Validate(info, config)
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

Expand Down Expand Up @@ -321,17 +327,19 @@ func TestPercentageDecisionPolicyAllow(t *testing.T) {
}

for name, tc := range testCases {
result, err := policy.Allow(tc.tally, totalWeight, tc.sinceSubmission)
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)

require.Equal(t, tc.final, result.Final, name)
if tc.final {
require.Equal(t, tc.allow, result.Allow, name)
}
t.Run(name, func(t *testing.T) {
result, err := policy.Allow(tc.tally, totalWeight, tc.sinceSubmission)
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)

require.Equal(t, tc.final, result.Final)
if tc.final {
require.Equal(t, tc.allow, result.Allow)
}
})
}
}

Expand Down Expand Up @@ -372,13 +380,15 @@ func TestMembers(t *testing.T) {
}

for name, tc := range testCases {
members := foundation.Members{tc.members}
err := members.ValidateBasic()
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)
t.Run(name, func(t *testing.T) {
members := foundation.Members{tc.members}
err := members.ValidateBasic()
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

Expand Down Expand Up @@ -421,13 +431,15 @@ func TestMemberRequests(t *testing.T) {
}

for name, tc := range testCases {
requests := foundation.MemberRequests{tc.members}
err := requests.ValidateBasic()
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)
t.Run(name, func(t *testing.T) {
requests := foundation.MemberRequests{tc.members}
err := requests.ValidateBasic()
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

Expand Down Expand Up @@ -524,19 +536,21 @@ func TestProposal(t *testing.T) {
}

for name, tc := range testCases {
proposal := foundation.Proposal{
Id: tc.id,
Proposers: tc.proposers,
FoundationVersion: tc.version,
}.WithMsgs(tc.msgs)
require.NotNil(t, proposal)

err := proposal.ValidateBasic()
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)
t.Run(name, func(t *testing.T) {
proposal := foundation.Proposal{
Id: tc.id,
Proposers: tc.proposers,
FoundationVersion: tc.version,
}.WithMsgs(tc.msgs)
require.NotNil(t, proposal)

err := proposal.ValidateBasic()
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

Expand All @@ -555,25 +569,27 @@ func TestOutsourcingDecisionPolicy(t *testing.T) {
}

for name, tc := range testCases {
policy := foundation.OutsourcingDecisionPolicy{}
require.Zero(t, policy.GetVotingPeriod())

err := policy.ValidateBasic()
if !tc.validBasic {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)

info := foundation.FoundationInfo{
TotalWeight: tc.totalWeight,
}
err = policy.Validate(info, config)
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)
t.Run(name, func(t *testing.T) {
policy := foundation.OutsourcingDecisionPolicy{}
require.Zero(t, policy.GetVotingPeriod())

err := policy.ValidateBasic()
if !tc.validBasic {
require.Error(t, err)
return
}
require.NoError(t, err)

info := foundation.FoundationInfo{
TotalWeight: tc.totalWeight,
}
err = policy.Validate(info, config)
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

Expand Down Expand Up @@ -604,16 +620,18 @@ func TestOutsourcingDecisionPolicyAllow(t *testing.T) {
}

for name, tc := range testCases {
result, err := policy.Allow(tc.tally, tc.totalWeight, tc.sinceSubmission)
if !tc.valid {
require.Error(t, err, name)
continue
}
require.NoError(t, err, name)

require.Equal(t, tc.final, result.Final, name)
if tc.final {
require.Equal(t, tc.allow, result.Allow, name)
}
t.Run(name, func(t *testing.T) {
result, err := policy.Allow(tc.tally, tc.totalWeight, tc.sinceSubmission)
if !tc.valid {
require.Error(t, err)
return
}
require.NoError(t, err)

require.Equal(t, tc.final, result.Final)
if tc.final {
require.Equal(t, tc.allow, result.Allow)
}
})
}
}
6 changes: 3 additions & 3 deletions x/foundation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ func (i FoundationInfo) ValidateBasic() error {

// Is foundation outsourcing the proposal feature
_, isOutsourcing := i.GetDecisionPolicy().(*OutsourcingDecisionPolicy)
memberExists := !i.TotalWeight.IsZero()
if isOutsourcing && memberExists {
memberNotExists := i.TotalWeight.IsZero()
if isOutsourcing && !memberNotExists {
return sdkerrors.ErrInvalidRequest.Wrap("outsourcing policy not allows members")
}
if !isOutsourcing && !memberExists {
if !isOutsourcing && memberNotExists {
return sdkerrors.ErrInvalidRequest.Wrap("one member must exist at least")
}

Expand Down
Loading

0 comments on commit f26c56b

Please sign in to comment.