Skip to content

Commit

Permalink
MX-15033: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos-rebegea committed Feb 6, 2024
1 parent c5103a0 commit 2a18830
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 91 deletions.
82 changes: 55 additions & 27 deletions vm/systemSmartContracts/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,13 @@ func (g *governanceContract) updateUserVoteListV2(address []byte, nonce uint64,
return err
}

return g.saveUserVotesV2(address, userVoteList)
err = g.saveUserVotesV2(address, userVoteList)
if err != nil {
return err
}

lastEndedNonce := g.getLastEndedNonce()
return g.clearUserVotes(address, lastEndedNonce)
}

func (g *governanceContract) updateUserVotesV1(userVoteList *OngoingVotedList, nonce uint64, direct bool) error {
Expand Down Expand Up @@ -720,16 +726,12 @@ func (g *governanceContract) closeProposal(args *vmcommon.ContractCallInput) vmc
return vmcommon.Ok
}

err = g.processLastEndedNonce(generalProposal.Nonce)
if err != nil {
g.eei.AddReturnMessage(err.Error())
return vmcommon.UserError
}
g.processLastEndedNonce()
return vmcommon.Ok
}

func (g *governanceContract) clearEndedProposals(addresses [][]byte) vmcommon.ReturnCode {
numAddresses := uint64(len(addresses))
func (g *governanceContract) clearEndedProposals(args *vmcommon.ContractCallInput) vmcommon.ReturnCode {
numAddresses := uint64(len(args.Arguments))
if numAddresses == 0 {
return vmcommon.Ok
}
Expand All @@ -739,17 +741,45 @@ func (g *governanceContract) clearEndedProposals(addresses [][]byte) vmcommon.Re
g.eei.AddReturnMessage("not enough gas")
return vmcommon.OutOfGas
}

lastEndedNonce := g.getLastEndedNonce()
for i := uint64(0); i < numAddresses; i++ {
voter := addresses[i]
voter := args.Arguments[i]
if len(voter) != len(args.CallerAddr) {
g.eei.AddReturnMessage("invalid delegator address")
return vmcommon.UserError
}
err = g.clearUserVotes(voter)
err = g.clearUserVotes(voter, lastEndedNonce)

Check failure on line 752 in vm/systemSmartContracts/governance.go

View workflow job for this annotation

GitHub Actions / golangci linter

ineffectual assignment to err (ineffassign)
}
return vmcommon.Ok
}

func (g *governanceContract) clearUserVotes(address []byte, lastEndedNonce uint64) error {
userVoteList, err := g.getUserVotesV2(address)
if err != nil {
return err
}

clearedUserVoteList := &OngoingVotedListV2{
Direct: make([]uint64, 0),
DelegatedWithAddress: make([]*DelegatedWithAddress, 0),
}

for _, delegatedWithAddress := range userVoteList.DelegatedWithAddress {
if delegatedWithAddress.Nonce > lastEndedNonce {
clearedUserVoteList.DelegatedWithAddress = append(clearedUserVoteList.DelegatedWithAddress, delegatedWithAddress)
}
}

for i, direct := range userVoteList.Direct {
if direct > lastEndedNonce {
clearedUserVoteList.Direct = append(clearedUserVoteList.Direct, userVoteList.Direct[i])
}
}

return g.saveUserVotesV2(address, clearedUserVoteList)
}

func (g *governanceContract) getAccumulatedFees() *big.Int {
currentData := g.eei.GetStorage([]byte(accumulatedFeeKey))
return big.NewInt(0).SetBytes(currentData)
Expand Down Expand Up @@ -1300,37 +1330,35 @@ func (g *governanceContract) convertV2Config(config config.GovernanceSystemSCCon
}, nil
}

func (g *governanceContract) processLastEndedNonce(lastNonce uint64) error {
lastEndedNonce, err := g.getLastEndedNonce()
if err != nil {
return err
}
func (g *governanceContract) processLastEndedNonce() {
lastEndedNonce := g.getLastEndedNonce()
lastEndedNonceBig := big.NewInt(int64(lastEndedNonce))
currentEpoch := g.eei.BlockChainHook().CurrentEpoch()

for lastEndedNonceBig.Cmp(big.NewInt(int64(lastNonce))) < 0 {
var activeProposalFound bool
for !activeProposalFound {
lastEndedNonceBig = lastEndedNonceBig.Add(lastEndedNonceBig, big.NewInt(1))
proposal, err := g.getProposalFromNonce(lastEndedNonceBig)
if err != nil {
return err
}

if !proposal.Closed {
if err != nil || proposal.EndVoteEpoch >= uint64(currentEpoch) {
activeProposalFound = true
break
}
lastEndedNonceBig.Add(lastEndedNonceBig, big.NewInt(1))
}

g.eei.SetStorage([]byte(lastEndedNonceKey), lastEndedNonceBig.Bytes())
if activeProposalFound {
lastEndedNonceBig = lastEndedNonceBig.Sub(lastEndedNonceBig, big.NewInt(1))
}

return nil
g.eei.SetStorage([]byte(lastEndedNonceKey), lastEndedNonceBig.Bytes())
}

func (g *governanceContract) getLastEndedNonce() (uint64, error) {
func (g *governanceContract) getLastEndedNonce() uint64 {
marshaledData := g.eei.GetStorage([]byte(lastEndedNonceKey))
if len(marshaledData) == 0 {
return 0, vm.ErrElementNotFound
return 0
}

return big.NewInt(0).SetBytes(marshaledData).Uint64(), nil
return big.NewInt(0).SetBytes(marshaledData).Uint64()
}

func convertDecimalToPercentage(arg []byte) (float32, error) {
Expand Down
126 changes: 63 additions & 63 deletions vm/systemSmartContracts/governance.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vm/systemSmartContracts/governance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ message OngoingVotedListV2 {
}

message DelegatedWithAddress {
uint64 Nonce = 1;
uint64 Nonce = 1 [(gogoproto.jsontag) = "Nonce"];
bytes DelegatedAddress = 2 [(gogoproto.jsontag) = "DelegatedAddress"];
}

Expand Down
Loading

0 comments on commit 2a18830

Please sign in to comment.