diff --git a/vm/systemSmartContracts/governance.go b/vm/systemSmartContracts/governance.go index 9984cf19954..ca92b7d7b55 100644 --- a/vm/systemSmartContracts/governance.go +++ b/vm/systemSmartContracts/governance.go @@ -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 { @@ -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 } @@ -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) } 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) @@ -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) { diff --git a/vm/systemSmartContracts/governance.pb.go b/vm/systemSmartContracts/governance.pb.go index a884dfb6009..f36aacd52b5 100644 --- a/vm/systemSmartContracts/governance.pb.go +++ b/vm/systemSmartContracts/governance.pb.go @@ -437,7 +437,7 @@ func (m *OngoingVotedListV2) GetDelegatedWithAddress() []*DelegatedWithAddress { } type DelegatedWithAddress struct { - Nonce uint64 `protobuf:"varint,1,opt,name=Nonce,proto3" json:"Nonce,omitempty"` + Nonce uint64 `protobuf:"varint,1,opt,name=Nonce,proto3" json:"Nonce"` DelegatedAddress []byte `protobuf:"bytes,2,opt,name=DelegatedAddress,proto3" json:"DelegatedAddress"` } @@ -560,68 +560,68 @@ func init() { func init() { proto.RegisterFile("governance.proto", fileDescriptor_e18a03da5266c714) } var fileDescriptor_e18a03da5266c714 = []byte{ - // 971 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x56, 0x4f, 0x6f, 0xe3, 0x44, - 0x14, 0x8f, 0xf3, 0xaf, 0xed, 0x34, 0xdd, 0xf5, 0x0e, 0x45, 0xb2, 0x40, 0xf2, 0x54, 0x39, 0x45, - 0xa0, 0x26, 0x52, 0x41, 0x5a, 0x09, 0x2e, 0xbb, 0xce, 0x96, 0xa5, 0x52, 0x37, 0xdb, 0x75, 0x4b, - 0x96, 0x45, 0x48, 0x68, 0x62, 0x4f, 0x1d, 0x8b, 0x78, 0xa6, 0xf2, 0x4c, 0xf6, 0x0f, 0x12, 0x12, - 0x27, 0xae, 0xf0, 0x1d, 0xb8, 0x20, 0x3e, 0x09, 0xc7, 0x1e, 0x7b, 0x32, 0x34, 0xbd, 0x80, 0xc5, - 0x61, 0x25, 0xbe, 0x00, 0x9a, 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x65, 0x28, - 0xff, 0x69, 0x0a, 0x96, 0x27, 0x9b, 0x71, 0xa6, 0xe1, 0x26, 0x4f, 0x6f, 0x6d, 0x29, 0xdc, 0x92, - 0x5f, 0xde, 0x1c, 0xe9, 0x22, 0xdc, 0x44, 0xb7, 0x5e, 0x0c, 0xb7, 0x64, 0xd9, 0x8c, 0xf3, 0xbd, - 0xbb, 0x60, 0x47, 0xfe, 0xe9, 0x21, 0x9e, 0x4c, 0xc9, 0xd9, 0xab, 0x0b, 0x02, 0x37, 0x54, 0x5f, - 0xa8, 0x57, 0x60, 0x53, 0x36, 0x6f, 0xba, 0x06, 0x37, 0x93, 0x1e, 0x4b, 0xaf, 0xc2, 0xed, 0xb4, - 0x0d, 0xd2, 0x6b, 0xd6, 0xe0, 0xf2, 0xda, 0xac, 0x5c, 0x5d, 0x9b, 0x95, 0xd7, 0xd7, 0xa6, 0xf6, - 0xdd, 0xcc, 0xd4, 0x7e, 0x9e, 0x99, 0xda, 0xaf, 0x33, 0x53, 0xbb, 0x9c, 0x99, 0xda, 0xd5, 0xcc, - 0xd4, 0x7e, 0x9f, 0x99, 0xda, 0x1f, 0x33, 0xb3, 0xf2, 0x7a, 0x66, 0x6a, 0x3f, 0xde, 0x98, 0x95, - 0xcb, 0x1b, 0xb3, 0x72, 0x75, 0x63, 0x56, 0xbe, 0xd8, 0xe5, 0xaf, 0xb8, 0x20, 0xc1, 0x69, 0x80, - 0x43, 0xd1, 0x67, 0x54, 0x84, 0xd8, 0x11, 0x7c, 0xd4, 0x54, 0x47, 0xe7, 0x83, 0x7f, 0x02, 0x00, - 0x00, 0xff, 0xff, 0x1c, 0x53, 0x09, 0x81, 0xb7, 0x0b, 0x00, 0x00, + // 968 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x56, 0x4f, 0x6f, 0x1b, 0x45, + 0x14, 0xf7, 0xfa, 0x5f, 0x92, 0x89, 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66, 0xe5, 0x8b, 0x6d, 0x76, 0xce, 0x38, 0x09, 0x8e, 0x03, 0x1c, 0xf1, 0x3e, + 0x0d, 0x79, 0x84, 0x1d, 0xce, 0x46, 0x4d, 0x75, 0x8a, 0x3e, 0xf8, 0x27, 0x00, 0x00, 0xff, 0xff, + 0xe3, 0x61, 0x5d, 0xd5, 0xc2, 0x0b, 0x00, 0x00, } func (x VoteValueType) String() string { diff --git a/vm/systemSmartContracts/governance.proto b/vm/systemSmartContracts/governance.proto index ba245d0f7d4..72bd4915f3c 100644 --- a/vm/systemSmartContracts/governance.proto +++ b/vm/systemSmartContracts/governance.proto @@ -58,7 +58,7 @@ message OngoingVotedListV2 { } message DelegatedWithAddress { - uint64 Nonce = 1; + uint64 Nonce = 1 [(gogoproto.jsontag) = "Nonce"]; bytes DelegatedAddress = 2 [(gogoproto.jsontag) = "DelegatedAddress"]; } diff --git a/vm/systemSmartContracts/governance_test.go b/vm/systemSmartContracts/governance_test.go index 00c19b79903..4d18873e2b2 100644 --- a/vm/systemSmartContracts/governance_test.go +++ b/vm/systemSmartContracts/governance_test.go @@ -1486,6 +1486,175 @@ func TestGovernanceContract_CloseProposalComputeResultsErr(t *testing.T) { require.Contains(t, retMessage, errSubstr) } +func TestGovernanceContract_CloseProposalAfterGovernanceFixesShouldSetLastEndedNonce(t *testing.T) { + t.Parallel() + + callerAddress := []byte("address") + gsc, blockchainHook, _ := createGovernanceBlockChainHookStubContextHandler() + gsc.enableEpochsHandler = enableEpochsHandlerMock.NewEnableEpochsHandlerStub(common.GovernanceFlag, common.GovernanceDisableProposeFlag, common.GovernanceFixesFlag) + currentEpoch := uint32(17) + blockchainHook.CurrentEpochCalled = func() uint32 { + return currentEpoch + } + + generalProposal := &GeneralProposal{ + Nonce: 1, + CommitHash: bytes.Repeat([]byte("a"), commitHashLength), + StartVoteEpoch: 10, + EndVoteEpoch: 15, + Yes: big.NewInt(0), + No: big.NewInt(0), + Veto: big.NewInt(0), + Abstain: big.NewInt(0), + ProposalCost: big.NewInt(10), + } + nonceKey := append([]byte(noncePrefix), big.NewInt(int64(generalProposal.Nonce)).Bytes()...) + gsc.eei.SetStorage(nonceKey, generalProposal.CommitHash) + _ = gsc.saveGeneralProposal(generalProposal.CommitHash, generalProposal) + + callInputArgs := [][]byte{{1}} + callInput := createVMInput(zero, "closeProposal", callerAddress, vm.GovernanceSCAddress, callInputArgs) + retCode := gsc.Execute(callInput) + + require.Equal(t, vmcommon.Ok, retCode) + lastEndedNonce := gsc.getLastEndedNonce() + require.Equal(t, generalProposal.Nonce, lastEndedNonce) + + generalProposal.Nonce = 2 + generalProposal.CommitHash = bytes.Repeat([]byte("b"), commitHashLength) + nonceKey = append([]byte(noncePrefix), big.NewInt(int64(generalProposal.Nonce)).Bytes()...) + gsc.eei.SetStorage(nonceKey, generalProposal.CommitHash) + _ = gsc.saveGeneralProposal(generalProposal.CommitHash, generalProposal) + + generalProposal.Nonce = 3 + generalProposal.CommitHash = bytes.Repeat([]byte("c"), commitHashLength) + nonceKey = append([]byte(noncePrefix), big.NewInt(int64(generalProposal.Nonce)).Bytes()...) + gsc.eei.SetStorage(nonceKey, generalProposal.CommitHash) + _ = gsc.saveGeneralProposal(generalProposal.CommitHash, generalProposal) + + generalProposal.Nonce = 4 + generalProposal.CommitHash = bytes.Repeat([]byte("d"), commitHashLength) + generalProposal.EndVoteEpoch = 18 + nonceKey = append([]byte(noncePrefix), big.NewInt(int64(generalProposal.Nonce)).Bytes()...) + gsc.eei.SetStorage(nonceKey, generalProposal.CommitHash) + _ = gsc.saveGeneralProposal(generalProposal.CommitHash, generalProposal) + + generalProposal.Nonce = 5 + generalProposal.CommitHash = bytes.Repeat([]byte("e"), commitHashLength) + generalProposal.EndVoteEpoch = 15 + nonceKey = append([]byte(noncePrefix), big.NewInt(int64(generalProposal.Nonce)).Bytes()...) + gsc.eei.SetStorage(nonceKey, generalProposal.CommitHash) + _ = gsc.saveGeneralProposal(generalProposal.CommitHash, generalProposal) + + generalProposal.Nonce = 6 + generalProposal.CommitHash = bytes.Repeat([]byte("f"), commitHashLength) + generalProposal.EndVoteEpoch = 15 + nonceKey = append([]byte(noncePrefix), big.NewInt(int64(generalProposal.Nonce)).Bytes()...) + gsc.eei.SetStorage(nonceKey, generalProposal.CommitHash) + _ = gsc.saveGeneralProposal(generalProposal.CommitHash, generalProposal) + + generalProposal.Nonce = 7 + generalProposal.CommitHash = bytes.Repeat([]byte("g"), commitHashLength) + generalProposal.StartVoteEpoch = 15 + generalProposal.EndVoteEpoch = 20 + nonceKey = append([]byte(noncePrefix), big.NewInt(int64(generalProposal.Nonce)).Bytes()...) + gsc.eei.SetStorage(nonceKey, generalProposal.CommitHash) + _ = gsc.saveGeneralProposal(generalProposal.CommitHash, generalProposal) + + currentEpoch = 18 + callInput.Arguments = [][]byte{{3}} + retCode = gsc.Execute(callInput) + + require.Equal(t, vmcommon.Ok, retCode) + lastEndedNonce = gsc.getLastEndedNonce() + require.Equal(t, uint64(3), lastEndedNonce) + + callInput.Arguments = [][]byte{{6}} + retCode = gsc.Execute(callInput) + + require.Equal(t, vmcommon.Ok, retCode) + lastEndedNonce = gsc.getLastEndedNonce() + require.Equal(t, uint64(3), lastEndedNonce) + + currentEpoch = 19 + callInput.Arguments = [][]byte{{4}} + retCode = gsc.Execute(callInput) + + require.Equal(t, vmcommon.Ok, retCode) + lastEndedNonce = gsc.getLastEndedNonce() + require.Equal(t, uint64(6), lastEndedNonce) +} + +func TestGovernanceContract_ClearEndedProposalsNoArguments(t *testing.T) { + t.Parallel() + + callerAddress := []byte("address") + args := createMockGovernanceArgs() + isCalled := false + args.Eei = &mock.SystemEIStub{ + UseGasCalled: func(gas uint64) error { + isCalled = true + return nil + }, + } + + gsc, _ := NewGovernanceContract(args) + callInput := createVMInput(zero, "clearEndedProposals", callerAddress, vm.GovernanceSCAddress, make([][]byte, 0)) + retCode := gsc.Execute(callInput) + + // Assert + require.Equal(t, vmcommon.Ok, retCode) + require.False(t, isCalled) +} + +func TestGovernanceContract_ClearEndedProposalsBadArguments(t *testing.T) { + t.Parallel() + + callerAddress := bytes.Repeat([]byte{2}, 32) + gsc, _, eei := createGovernanceBlockChainHookStubContextHandler() + callInputArgs := [][]byte{ + bytes.Repeat([]byte{2}, 31), + } + callInput := createVMInput(zero, "clearEndedProposals", callerAddress, vm.GovernanceSCAddress, callInputArgs) + retCode := gsc.Execute(callInput) + + require.Equal(t, vmcommon.UserError, retCode) + require.Equal(t, eei.GetReturnMessage(), "invalid delegator address") +} + +func TestGovernanceContract_ClearEndedProposalsOutOfGas(t *testing.T) { + t.Parallel() + + callerAddress := []byte("address") + args := createMockGovernanceArgs() + args.GasCost.MetaChainSystemSCsCost.ClearProposal = 10 + args.Eei = &mock.SystemEIStub{ + UseGasCalled: func(gas uint64) error { + if gas <= args.GasCost.MetaChainSystemSCsCost.ClearProposal*3 { + return nil + } + return vm.ErrNotEnoughGas + }, + } + gsc, _ := NewGovernanceContract(args) + + callInputArgs := [][]byte{ + callerAddress, + } + callInput := createVMInput(zero, "clearEndedProposals", callerAddress, vm.GovernanceSCAddress, callInputArgs) + retCode := gsc.Execute(callInput) + require.Equal(t, vmcommon.Ok, retCode) + + callInput.Arguments = [][]byte{ + callerAddress, + callerAddress, + callerAddress, + callerAddress, + } + retCode = gsc.Execute(callInput) + require.Equal(t, vmcommon.OutOfGas, retCode) +} + func TestGovernanceContract_GetVotingPower(t *testing.T) { t.Parallel()