Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions cmd/node/config/enableEpochs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
# GovernanceEnableEpoch represents the epoch when governance is enabled
GovernanceEnableEpoch = 5

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

# DelegationManagerEnableEpoch represents the epoch when the delegation manager is enabled
# epoch should not be 0
DelegationManagerEnableEpoch = 1
Expand Down
1 change: 1 addition & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ const (
ESDTFlag core.EnableEpochFlag = "ESDTFlag"
ESDTFlagInSpecificEpochOnly core.EnableEpochFlag = "ESDTFlagInSpecificEpochOnly"
GovernanceFlag core.EnableEpochFlag = "GovernanceFlag"
GovernanceFixesFlag core.EnableEpochFlag = "GovernanceFixesFlag"
GovernanceFlagInSpecificEpochOnly core.EnableEpochFlag = "GovernanceFlagInSpecificEpochOnly"
DelegationManagerFlag core.EnableEpochFlag = "DelegationManagerFlag"
DelegationSmartContractFlag core.EnableEpochFlag = "DelegationSmartContractFlag"
Expand Down
6 changes: 6 additions & 0 deletions common/enablers/enableEpochsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ func (handler *enableEpochsHandler) createAllFlagsMap() {
},
activationEpoch: handler.enableEpochsConfig.GovernanceEnableEpoch,
},
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
1 change: 1 addition & 0 deletions common/enablers/enableEpochsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ 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.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
1 change: 1 addition & 0 deletions config/epochConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type EnableEpochs struct {
DoubleKeyProtectionEnableEpoch uint32
ESDTEnableEpoch uint32
GovernanceEnableEpoch uint32
GovernanceFixesEnableEpoch uint32
DelegationManagerEnableEpoch uint32
DelegationSmartContractEnableEpoch uint32
CorrectLastUnjailedEnableEpoch uint32
Expand Down
1 change: 1 addition & 0 deletions genesis/process/shardGenesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func createGenesisConfig() config.EnableEpochs {
DoubleKeyProtectionEnableEpoch: 0,
ESDTEnableEpoch: unreachableEpoch,
GovernanceEnableEpoch: unreachableEpoch,
GovernanceFixesEnableEpoch: unreachableEpoch,
DelegationManagerEnableEpoch: unreachableEpoch,
DelegationSmartContractEnableEpoch: unreachableEpoch,
CorrectLastUnjailedEnableEpoch: unreachableEpoch,
Expand Down
1 change: 1 addition & 0 deletions node/nodeRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ 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 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
51 changes: 38 additions & 13 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 @@ -398,7 +398,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 +466,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 +526,10 @@ 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)
err := g.updateUserVoteList(address, nonce.Uint64(), direct, scAddress)
if err != nil {
return err
}
Expand All @@ -545,7 +548,7 @@ func (g *governanceContract) addUserVote(
return g.saveGeneralProposal(proposal.CommitHash, proposal)
}

func (g *governanceContract) updateUserVoteList(address []byte, nonce uint64, direct bool) error {
func (g *governanceContract) updateUserVoteList(address []byte, nonce uint64, direct bool, scAddress []byte) error {
userVoteList, err := g.getUserVotes(address)
if err != nil {
return err
Expand All @@ -557,9 +560,16 @@ func (g *governanceContract) updateUserVoteList(address []byte, nonce uint64, di
return err
}
} else {
userVoteList.Delegated, err = addNewNonce(userVoteList.Delegated, nonce)
if err != nil {
return err
if !g.enableEpochsHandler.IsFlagEnabled(common.GovernanceFixesFlag) {
userVoteList.Delegated, err = addNewNonce(userVoteList.Delegated, nonce)
if err != nil {
return err
}
} else {
userVoteList.DelegatedWithAddress, err = addNewNonceV2(userVoteList.DelegatedWithAddress, scAddress, nonce)
if err != nil {
return err
}
}
}

Expand All @@ -577,6 +587,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 @@ -1010,7 +1034,7 @@ 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) saveUserVotes(address []byte, votedList *OngoingVotedListV2) error {
marshaledData, err := g.marshalizer.Marshal(votedList)
if err != nil {
return err
Expand All @@ -1020,10 +1044,11 @@ func (g *governanceContract) saveUserVotes(address []byte, votedList *OngoingVot
return nil
}

func (g *governanceContract) getUserVotes(address []byte) (*OngoingVotedList, error) {
onGoingList := &OngoingVotedList{
Direct: make([]uint64, 0),
Delegated: make([]uint64, 0),
func (g *governanceContract) getUserVotes(address []byte) (*OngoingVotedListV2, error) {
onGoingList := &OngoingVotedListV2{
Direct: make([]uint64, 0),
Delegated: make([]uint64, 0),
DelegatedWithAddress: make([]*DelegatedWithAddress, 0),
}
marshaledData := g.eei.GetStorage(address)
if len(marshaledData) == 0 {
Expand Down
Loading