Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/node/config/enableEpochs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
# GovernanceEnableEpoch represents the epoch when governance is enabled
GovernanceEnableEpoch = 5

# GovernanceDisableProposeEnableEpoch represents the epoch when governance disable proposal is enabled
GovernanceDisableProposeEnableEpoch = 6

# GovernanceFixesEnableEpoch represents the epoch when governance fixes are enabled
GovernanceFixesEnableEpoch = 7

# DelegationManagerEnableEpoch represents the epoch when the delegation manager is enabled
# epoch should not be 0
DelegationManagerEnableEpoch = 1
Expand Down
2 changes: 2 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,8 @@ const (
ESDTFlag core.EnableEpochFlag = "ESDTFlag"
ESDTFlagInSpecificEpochOnly core.EnableEpochFlag = "ESDTFlagInSpecificEpochOnly"
GovernanceFlag core.EnableEpochFlag = "GovernanceFlag"
GovernanceDisableProposeFlag core.EnableEpochFlag = "GovernanceDisableProposeFlag"
GovernanceFixesFlag core.EnableEpochFlag = "GovernanceFixesFlag"
GovernanceFlagInSpecificEpochOnly core.EnableEpochFlag = "GovernanceFlagInSpecificEpochOnly"
DelegationManagerFlag core.EnableEpochFlag = "DelegationManagerFlag"
DelegationSmartContractFlag core.EnableEpochFlag = "DelegationSmartContractFlag"
Expand Down
12 changes: 12 additions & 0 deletions common/enablers/enableEpochsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ func (handler *enableEpochsHandler) createAllFlagsMap() {
},
activationEpoch: handler.enableEpochsConfig.GovernanceEnableEpoch,
},
common.GovernanceDisableProposeFlag: {
isActiveInEpoch: func(epoch uint32) bool {
return epoch >= handler.enableEpochsConfig.GovernanceDisableProposeEnableEpoch
},
activationEpoch: handler.enableEpochsConfig.GovernanceDisableProposeEnableEpoch,
},
common.GovernanceFixesFlag: {
isActiveInEpoch: func(epoch uint32) bool {
return epoch >= handler.enableEpochsConfig.GovernanceFixesEnableEpoch
},
activationEpoch: handler.enableEpochsConfig.GovernanceFixesEnableEpoch,
},
common.DelegationManagerFlag: {
isActiveInEpoch: func(epoch uint32) bool {
return epoch >= handler.enableEpochsConfig.DelegationManagerEnableEpoch
Expand Down
6 changes: 6 additions & 0 deletions common/enablers/enableEpochsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func createEnableEpochsConfig() config.EnableEpochs {
DoubleKeyProtectionEnableEpoch: 19,
ESDTEnableEpoch: 20,
GovernanceEnableEpoch: 21,
GovernanceDisableProposeEnableEpoch: 22,
GovernanceFixesEnableEpoch: 23,
DelegationManagerEnableEpoch: 22,
DelegationSmartContractEnableEpoch: 23,
CorrectLastUnjailedEnableEpoch: 24,
Expand Down Expand Up @@ -213,6 +215,8 @@ func TestEnableEpochsHandler_IsFlagEnabled(t *testing.T) {
require.False(t, handler.IsFlagEnabled(common.ESDTFlagInSpecificEpochOnly)) // ==
require.True(t, handler.IsFlagEnabled(common.GovernanceFlag))
require.False(t, handler.IsFlagEnabled(common.GovernanceFlagInSpecificEpochOnly)) // ==
require.True(t, handler.IsFlagEnabled(common.GovernanceDisableProposeFlag)) // ==
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the purpose for the == comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove in the next PR.

require.True(t, handler.IsFlagEnabled(common.GovernanceFixesFlag)) // ==
require.True(t, handler.IsFlagEnabled(common.DelegationManagerFlag))
require.True(t, handler.IsFlagEnabled(common.DelegationSmartContractFlag))
require.False(t, handler.IsFlagEnabled(common.DelegationSmartContractFlagInSpecificEpochOnly)) // ==
Expand Down Expand Up @@ -326,6 +330,8 @@ func TestEnableEpochsHandler_GetActivationEpoch(t *testing.T) {
require.Equal(t, cfg.DoubleKeyProtectionEnableEpoch, handler.GetActivationEpoch(common.DoubleKeyProtectionFlag))
require.Equal(t, cfg.ESDTEnableEpoch, handler.GetActivationEpoch(common.ESDTFlag))
require.Equal(t, cfg.GovernanceEnableEpoch, handler.GetActivationEpoch(common.GovernanceFlag))
require.Equal(t, cfg.GovernanceDisableProposeEnableEpoch, handler.GetActivationEpoch(common.GovernanceDisableProposeFlag))
require.Equal(t, cfg.GovernanceFixesEnableEpoch, handler.GetActivationEpoch(common.GovernanceFixesFlag))
require.Equal(t, cfg.DelegationManagerEnableEpoch, handler.GetActivationEpoch(common.DelegationManagerFlag))
require.Equal(t, cfg.DelegationSmartContractEnableEpoch, handler.GetActivationEpoch(common.DelegationSmartContractFlag))
require.Equal(t, cfg.CorrectLastUnjailedEnableEpoch, handler.GetActivationEpoch(common.CorrectLastUnJailedFlag))
Expand Down
2 changes: 2 additions & 0 deletions config/epochConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type EnableEpochs struct {
DoubleKeyProtectionEnableEpoch uint32
ESDTEnableEpoch uint32
GovernanceEnableEpoch uint32
GovernanceDisableProposeEnableEpoch uint32
GovernanceFixesEnableEpoch uint32
DelegationManagerEnableEpoch uint32
DelegationSmartContractEnableEpoch uint32
CorrectLastUnjailedEnableEpoch uint32
Expand Down
8 changes: 8 additions & 0 deletions config/tomlConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,12 @@ func TestEnableEpochConfig(t *testing.T) {
# GovernanceEnableEpoch represents the epoch when governance is enabled
GovernanceEnableEpoch = 21

# GovernanceDisableProposeEnableEpoch represents the epoch when governance disable proposal is enabled
GovernanceDisableProposeEnableEpoch = 22

# GovernanceFixesEnableEpoch represents the epoch when governance fixes are enabled
GovernanceFixesEnableEpoch = 23

# DelegationManagerEnableEpoch represents the epoch when the delegation manager is enabled
# epoch should not be 0
DelegationManagerEnableEpoch = 22
Expand Down Expand Up @@ -883,6 +889,8 @@ func TestEnableEpochConfig(t *testing.T) {
DoubleKeyProtectionEnableEpoch: 19,
ESDTEnableEpoch: 20,
GovernanceEnableEpoch: 21,
GovernanceDisableProposeEnableEpoch: 22,
GovernanceFixesEnableEpoch: 23,
DelegationManagerEnableEpoch: 22,
DelegationSmartContractEnableEpoch: 23,
CorrectLastUnjailedEnableEpoch: 24,
Expand Down
2 changes: 2 additions & 0 deletions genesis/process/shardGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func createGenesisConfig() config.EnableEpochs {
DoubleKeyProtectionEnableEpoch: 0,
ESDTEnableEpoch: unreachableEpoch,
GovernanceEnableEpoch: unreachableEpoch,
GovernanceDisableProposeEnableEpoch: unreachableEpoch,
GovernanceFixesEnableEpoch: unreachableEpoch,
DelegationManagerEnableEpoch: unreachableEpoch,
DelegationSmartContractEnableEpoch: unreachableEpoch,
CorrectLastUnjailedEnableEpoch: unreachableEpoch,
Expand Down
2 changes: 2 additions & 0 deletions integrationTests/testProcessorNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3172,6 +3172,8 @@ func CreateEnableEpochsConfig() config.EnableEpochs {
DoubleKeyProtectionEnableEpoch: 0,
ESDTEnableEpoch: UnreachableEpoch,
GovernanceEnableEpoch: UnreachableEpoch,
GovernanceDisableProposeEnableEpoch: UnreachableEpoch,
GovernanceFixesEnableEpoch: UnreachableEpoch,
DelegationManagerEnableEpoch: UnreachableEpoch,
DelegationSmartContractEnableEpoch: UnreachableEpoch,
CorrectLastUnjailedEnableEpoch: UnreachableEpoch,
Expand Down
2 changes: 2 additions & 0 deletions node/nodeRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func printEnableEpochs(configs *config.Configs) {
log.Debug(readEpochFor("double key protection"), "epoch", enableEpochs.DoubleKeyProtectionEnableEpoch)
log.Debug(readEpochFor("esdt"), "epoch", enableEpochs.ESDTEnableEpoch)
log.Debug(readEpochFor("governance"), "epoch", enableEpochs.GovernanceEnableEpoch)
log.Debug(readEpochFor("governance disable proposal"), "epoch", enableEpochs.GovernanceDisableProposeEnableEpoch)
log.Debug(readEpochFor("governance fixes"), "epoch", enableEpochs.GovernanceFixesEnableEpoch)
log.Debug(readEpochFor("delegation manager"), "epoch", enableEpochs.DelegationManagerEnableEpoch)
log.Debug(readEpochFor("delegation smart contract"), "epoch", enableEpochs.DelegationSmartContractEnableEpoch)
log.Debug(readEpochFor("correct last unjailed"), "epoch", enableEpochs.CorrectLastUnjailedEnableEpoch)
Expand Down
154 changes: 100 additions & 54 deletions vm/systemSmartContracts/governance.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --gogoslick_out=. governance.proto
//go:generate protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --gogoslick_out=. governance.proto
package systemSmartContracts

import (
Expand Down Expand Up @@ -155,8 +155,6 @@ func (g *governanceContract) Execute(args *vmcommon.ContractCallInput) vmcommon.
return g.viewVotingPower(args)
case "viewConfig":
return g.viewConfig(args)
case "viewUserVoteHistory":
return g.viewUserVoteHistory(args)
case "viewDelegatedVoteInfo":
return g.viewDelegatedVoteInfo(args)
case "viewProposal":
Expand Down Expand Up @@ -284,6 +282,10 @@ func (g *governanceContract) changeConfig(args *vmcommon.ContractCallInput) vmco

// proposal creates a new proposal from passed arguments
func (g *governanceContract) proposal(args *vmcommon.ContractCallInput) vmcommon.ReturnCode {
if g.enableEpochsHandler.IsFlagEnabled(common.GovernanceDisableProposeFlag) && !g.enableEpochsHandler.IsFlagEnabled(common.GovernanceFixesFlag) {
g.eei.AddReturnMessage("proposing is disabled")
return vmcommon.UserError
}
err := g.eei.UseGas(g.gasCost.MetaChainSystemSCsCost.Proposal)
if err != nil {
g.eei.AddReturnMessage("not enough gas")
Expand Down Expand Up @@ -398,7 +400,8 @@ func (g *governanceContract) vote(args *vmcommon.ContractCallInput) vmcommon.Ret
string(args.Arguments[1]),
totalVotingPower,
totalStake,
true)
true,
nil)
if err != nil {
g.eei.AddReturnMessage(err.Error())
return vmcommon.UserError
Expand Down Expand Up @@ -465,7 +468,8 @@ func (g *governanceContract) delegateVote(args *vmcommon.ContractCallInput) vmco
string(args.Arguments[1]),
votePower,
userStake,
false)
false,
args.CallerAddr)
if err != nil {
g.eei.AddReturnMessage(err.Error())
return vmcommon.UserError
Expand Down Expand Up @@ -524,9 +528,11 @@ func (g *governanceContract) addUserVote(
totalVotingPower *big.Int,
totalStake *big.Int,
direct bool,
scAddress []byte,
) error {
nonce := big.NewInt(0).SetBytes(nonceAsBytes)
err := g.updateUserVoteList(address, nonce.Uint64(), direct)
//TODO: remove extra struct & methods for V2 if no V1 proposal exists
err := g.updateUserVoteListV2(address, nonce.Uint64(), direct, scAddress)
if err != nil {
return err
}
Expand All @@ -545,25 +551,57 @@ func (g *governanceContract) addUserVote(
return g.saveGeneralProposal(proposal.CommitHash, proposal)
}

func (g *governanceContract) updateUserVoteList(address []byte, nonce uint64, direct bool) error {
userVoteList, err := g.getUserVotes(address)
func (g *governanceContract) updateUserVoteListV1(address []byte, nonce uint64, direct bool) error {
userVoteList, err := g.getUserVotesV1(address)
if err != nil {
return err
}

err = g.updateUserVotesV1(userVoteList, nonce, direct)
if err != nil {
return err
}

return g.saveUserVotesV1(address, userVoteList)
}

func (g *governanceContract) updateUserVoteListV2(address []byte, nonce uint64, direct bool, scAddress []byte) error {
if !g.enableEpochsHandler.IsFlagEnabled(common.GovernanceFixesFlag) {
return g.updateUserVoteListV1(address, nonce, direct)
}
userVoteList, err := g.getUserVotesV2(address)
if err != nil {
return err
}

err = g.updateUserVotesV2(userVoteList, nonce, direct, scAddress)
if err != nil {
return err
}

return g.saveUserVotesV2(address, userVoteList)
}

func (g *governanceContract) updateUserVotesV1(userVoteList *OngoingVotedList, nonce uint64, direct bool) error {
var err error
if direct {
userVoteList.Direct, err = addNewNonce(userVoteList.Direct, nonce)
if err != nil {
return err
}
} else {
userVoteList.Delegated, err = addNewNonce(userVoteList.Delegated, nonce)
if err != nil {
return err
}
return err
}

userVoteList.Delegated, err = addNewNonce(userVoteList.Delegated, nonce)
return err
}

func (g *governanceContract) updateUserVotesV2(userVoteList *OngoingVotedListV2, nonce uint64, direct bool, scAddress []byte) error {
var err error
if direct {
userVoteList.Direct, err = addNewNonce(userVoteList.Direct, nonce)
return err
}

return g.saveUserVotes(address, userVoteList)
userVoteList.DelegatedWithAddress, err = addNewNonceV2(userVoteList.DelegatedWithAddress, scAddress, nonce)
return err
}

func addNewNonce(nonceList []uint64, newNonce uint64) ([]uint64, error) {
Expand All @@ -577,6 +615,20 @@ func addNewNonce(nonceList []uint64, newNonce uint64) ([]uint64, error) {
return nonceList, nil
}

func addNewNonceV2(nonceList []*DelegatedWithAddress, newDelegatedAddress []byte, newNonce uint64) ([]*DelegatedWithAddress, error) {
for _, delegatedStruct := range nonceList {
if newNonce == delegatedStruct.Nonce && bytes.Equal(delegatedStruct.DelegatedAddress, newDelegatedAddress) {
return nil, vm.ErrDoubleVote
}
}

nonceList = append(nonceList, &DelegatedWithAddress{
Nonce: newNonce,
DelegatedAddress: newDelegatedAddress,
})
return nonceList, nil
}

func (g *governanceContract) getMinValueToVote() (*big.Int, error) {
delegationManagement, err := getDelegationManagement(g.eei, g.marshalizer, g.delegationMgrSCAddress)
if err != nil {
Expand Down Expand Up @@ -756,41 +808,6 @@ func (g *governanceContract) viewConfig(args *vmcommon.ContractCallInput) vmcomm
return vmcommon.Ok
}

func (g *governanceContract) viewUserVoteHistory(args *vmcommon.ContractCallInput) vmcommon.ReturnCode {
err := g.checkViewFuncArguments(args, 1)
if err != nil {
g.eei.AddReturnMessage(err.Error())
return vmcommon.UserError
}

userVotes, err := g.getUserVotes(args.Arguments[0])
if err != nil {
g.eei.AddReturnMessage(err.Error())
return vmcommon.UserError
}

g.finishWithIntValue(len(userVotes.Delegated)) // first we send the number of delegated nonces and afterward the nonces
for _, val := range userVotes.Delegated {
g.finishWithIntValue(int(val))
}

g.finishWithIntValue(len(userVotes.Direct)) // then we send the number of direct nonces and afterward the nonces
for _, val := range userVotes.Direct {
g.finishWithIntValue(int(val))
}

return vmcommon.Ok
}

func (g *governanceContract) finishWithIntValue(value int) {
if value == 0 {
g.eei.Finish([]byte{0})
return
}

g.eei.Finish(big.NewInt(int64(value)).Bytes())
}

func (g *governanceContract) viewProposal(args *vmcommon.ContractCallInput) vmcommon.ReturnCode {
err := g.checkViewFuncArguments(args, 1)
if err != nil {
Expand Down Expand Up @@ -1010,7 +1027,17 @@ func (g *governanceContract) getTotalStake(validatorAddress []byte) (*big.Int, e
return validatorData.TotalStakeValue, nil
}

func (g *governanceContract) saveUserVotes(address []byte, votedList *OngoingVotedList) error {
func (g *governanceContract) saveUserVotesV1(address []byte, votedList *OngoingVotedList) error {
marshaledData, err := g.marshalizer.Marshal(votedList)
if err != nil {
return err
}
g.eei.SetStorage(address, marshaledData)

return nil
}

func (g *governanceContract) saveUserVotesV2(address []byte, votedList *OngoingVotedListV2) error {
marshaledData, err := g.marshalizer.Marshal(votedList)
if err != nil {
return err
Expand All @@ -1020,7 +1047,7 @@ func (g *governanceContract) saveUserVotes(address []byte, votedList *OngoingVot
return nil
}

func (g *governanceContract) getUserVotes(address []byte) (*OngoingVotedList, error) {
func (g *governanceContract) getUserVotesV1(address []byte) (*OngoingVotedList, error) {
onGoingList := &OngoingVotedList{
Direct: make([]uint64, 0),
Delegated: make([]uint64, 0),
Expand All @@ -1038,6 +1065,25 @@ func (g *governanceContract) getUserVotes(address []byte) (*OngoingVotedList, er
return onGoingList, nil
}

// getUserVotesV2 returns the ongoing voted list for a user
// if the user has v1 ongoing list, it will be converted to v2, dropping Delegate field
func (g *governanceContract) getUserVotesV2(address []byte) (*OngoingVotedListV2, error) {
onGoingListV2 := &OngoingVotedListV2{
Direct: make([]uint64, 0),
DelegatedWithAddress: make([]*DelegatedWithAddress, 0),
}
marshaledData := g.eei.GetStorage(address)
if len(marshaledData) == 0 {
return onGoingListV2, nil
}

err := g.marshalizer.Unmarshal(onGoingListV2, marshaledData)
if err != nil {
return nil, err
}
return onGoingListV2, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should return onGoingList not the v2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method will be called only after GovernanceFixes gets activated, that means there is no ongoing v1 proposal, thus the information is not necessary anymore.

}

func (g *governanceContract) getDelegatedContractInfo(scAddress []byte, reference []byte) (*DelegatedSCVoteInfo, error) {
scVoteInfo := &DelegatedSCVoteInfo{
TotalPower: big.NewInt(0),
Expand Down
Loading
Loading