Skip to content

Commit

Permalink
Merge pull request #187 from ian0371/kaiax/remove-gov
Browse files Browse the repository at this point in the history
Remove old governance
  • Loading branch information
ian0371 authored Dec 31, 2024
2 parents f65ff60 + d5017ff commit 977e134
Show file tree
Hide file tree
Showing 28 changed files with 67 additions and 5,189 deletions.
5 changes: 0 additions & 5 deletions cmd/utils/nodecmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/kaiachain/kaia/blockchain"
"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/cmd/utils"
"github.com/kaiachain/kaia/governance"
headergov_impl "github.com/kaiachain/kaia/kaiax/gov/headergov/impl"
"github.com/kaiachain/kaia/log"
"github.com/kaiachain/kaia/params"
Expand Down Expand Up @@ -245,10 +244,6 @@ func ValidateGenesisConfig(g *blockchain.Genesis) error {
}

if g.Config.Istanbul != nil {
if err := governance.CheckGenesisValues(g.Config); err != nil {
return err
}

// TODO-Kaia: Add validation logic for other GovernanceModes
// Check if governingNode is properly set
if strings.ToLower(g.Config.Governance.GovernanceMode) == "single" {
Expand Down
58 changes: 10 additions & 48 deletions cmd/utils/nodecmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package nodecmd

import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -33,8 +32,7 @@ import (
"github.com/kaiachain/kaia/consensus/istanbul/backend"
"github.com/kaiachain/kaia/crypto"
"github.com/kaiachain/kaia/crypto/sha3"
"github.com/kaiachain/kaia/governance"
"github.com/kaiachain/kaia/params"
"github.com/kaiachain/kaia/kaiax/gov/headergov"
"github.com/kaiachain/kaia/rlp"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -222,51 +220,15 @@ func decodeExtra(headerFile string) (map[string]interface{}, error) {
}

func decodeVote(bytes []byte) (map[string]interface{}, error) {
vote := new(governance.GovernanceVote)
m := make(map[string]interface{})
if err := rlp.DecodeBytes(bytes, &vote); err == nil {
m["validator"] = vote.Validator.String()
m["key"] = vote.Key
switch governance.GovernanceKeyMap[vote.Key] {
case params.GovernanceMode, params.MintingAmount, params.MinimumStake, params.Ratio, params.Kip82Ratio:
m["value"] = string(vote.Value.([]uint8))
case params.GoverningNode, params.GovParamContract:
m["value"] = common.BytesToAddress(vote.Value.([]uint8)).String()
case params.Epoch, params.CommitteeSize, params.UnitPrice, params.DeriveShaImpl, params.StakeUpdateInterval,
params.ProposerRefreshInterval, params.ConstTxGasHumanReadable, params.Policy, params.Timeout,
params.LowerBoundBaseFee, params.UpperBoundBaseFee, params.GasTarget, params.MaxBlockGasUsedForBaseFee, params.BaseFeeDenominator:
v := vote.Value.([]uint8)
v = append(make([]byte, 8-len(v)), v...)
m["value"] = binary.BigEndian.Uint64(v)
case params.UseGiniCoeff, params.DeferredTxFee:
v := vote.Value.([]uint8)
v = append(make([]byte, 8-len(v)), v...)
if binary.BigEndian.Uint64(v) != uint64(0) {
m["value"] = true
} else {
m["value"] = false
}
case params.AddValidator, params.RemoveValidator:
if v, ok := vote.Value.([]uint8); ok {
m["value"] = common.BytesToAddress(v)
} else if addresses, ok := vote.Value.([]interface{}); ok {
if len(addresses) == 0 {
return nil, governance.ErrValueTypeMismatch
}
var nodeAddresses []common.Address
for _, item := range addresses {
if in, ok := item.([]uint8); !ok || len(in) != common.AddressLength {
return nil, governance.ErrValueTypeMismatch
}
nodeAddresses = append(nodeAddresses, common.BytesToAddress(item.([]uint8)))
}
m["value"] = nodeAddresses
} else {
return nil, governance.ErrValueTypeMismatch
}
}
return m, nil
} else {
var vb headergov.VoteBytes = bytes
vote, err := vb.ToVoteData()
if err != nil {
return nil, err
}

m := make(map[string]interface{})
m["validator"] = vote.Voter()
m["key"] = vote.Name()
m["value"] = vote.Value()
return m, nil
}
34 changes: 26 additions & 8 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"github.com/kaiachain/kaia/consensus"
"github.com/kaiachain/kaia/crypto"
"github.com/kaiachain/kaia/crypto/sha3"
"github.com/kaiachain/kaia/governance"
"github.com/kaiachain/kaia/log"
"github.com/kaiachain/kaia/networks/rpc"
"github.com/kaiachain/kaia/params"
Expand Down Expand Up @@ -214,6 +213,22 @@ type Clique struct {
fakeBlockScore bool // Skip blockScore verifications
}

type voteData struct {
Validator common.Address
Key string
Value any
}

func (v *voteData) ToVoteBytes() ([]byte, error) {
return rlp.EncodeToBytes(v)
}

func BytesToVoteData(b []byte) (voteData, error) {
var v voteData
err := rlp.DecodeBytes(b, &v)
return v, err
}

// New creates a Clique proof-of-authority consensus engine with the initial
// signers set to the ones provided by the user.
func New(config *params.CliqueConfig, db database.DBManager) *Clique {
Expand Down Expand Up @@ -527,19 +542,22 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro
}
// If there's pending proposals, cast a vote on them
if len(addresses) > 0 {
vote := new(governance.GovernanceVote)
addr := addresses[rand.Intn(len(addresses))]
v := &voteData{
Validator: c.signer,
Value: addr,
}

if c.proposals[addr] == true {
vote.Key = "addvalidator"
v.Key = "addvalidator"
} else {
vote.Key = "removevalidator"
v.Key = "removevalidator"
}
vote.Value = addr
encoded, err := rlp.EncodeToBytes(vote)

header.Vote, err = v.ToVoteBytes()
if err != nil {
logger.Error("Failed to RLP Encode a vote", "vote", vote)
logger.Error("Failed to RLP Encode a vote", "vote", v)
}
header.Vote = encoded
}
c.lock.RUnlock()
}
Expand Down
11 changes: 4 additions & 7 deletions consensus/clique/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import (
lru "github.com/hashicorp/golang-lru"
"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/governance"
"github.com/kaiachain/kaia/params"
"github.com/kaiachain/kaia/rlp"
"github.com/kaiachain/kaia/storage/database"
)

Expand Down Expand Up @@ -235,19 +233,18 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
var nonce []byte

if len(header.Vote) > 0 {
gVote := new(governance.GovernanceVote)
if err := rlp.DecodeBytes(header.Vote, gVote); err != nil {
logger.Error("Failed to decode a vote", "number", header.Number, "key", gVote.Key, "value", gVote.Value, "validator", gVote.Validator)
voteData, err := BytesToVoteData(header.Vote)
if err != nil {
return nil, errInvalidVote
}

if temp, ok := gVote.Value.([]uint8); !ok {
if temp, ok := voteData.Value.([]uint8); !ok {
return nil, errInvalidVote
} else {
cb = common.BytesToAddress(temp)
}

switch gVote.Key {
switch voteData.Key {
case "addvalidator":
nonce = nonceAuthVote
case "removevalidator":
Expand Down
23 changes: 16 additions & 7 deletions consensus/clique/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import (
"github.com/kaiachain/kaia/blockchain/vm"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/crypto"
"github.com/kaiachain/kaia/governance"
"github.com/kaiachain/kaia/kaiax/gov"
"github.com/kaiachain/kaia/params"
"github.com/kaiachain/kaia/rlp"
"github.com/kaiachain/kaia/storage/database"
)

Expand Down Expand Up @@ -420,16 +419,26 @@ func TestClique(t *testing.T) {
engine.fakeBlockScore = true

blocks, _ := blockchain.GenerateChain(config, genesis.ToBlock(common.Hash{}, db), engine, db, len(tt.votes), func(j int, gen *blockchain.BlockGen) {
vote := new(governance.GovernanceVote)
var voteName gov.ParamName
if tt.votes[j].auth {
vote.Key = "addvalidator"
voteName = "addvalidator"
} else {
vote.Key = "removevalidator"
voteName = "removevalidator"
}

if len(tt.votes[j].voted) > 0 {
vote.Value = accounts.address(tt.votes[j].voted)
encoded, _ := rlp.EncodeToBytes(vote)
addr := accounts.address(tt.votes[j].voted)
vote := &voteData{
Validator: common.Address{},
Key: string(voteName),
Value: addr,
}
encoded, err := vote.ToVoteBytes()
if err != nil {
t.Fatalf("test %d: failed to encode vote: %v", i, err)
return
}

gen.SetVoteData(encoded)
}
})
Expand Down
6 changes: 1 addition & 5 deletions consensus/istanbul/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/kaiachain/kaia/crypto"
"github.com/kaiachain/kaia/crypto/bls"
"github.com/kaiachain/kaia/event"
"github.com/kaiachain/kaia/governance"
"github.com/kaiachain/kaia/kaiax"
"github.com/kaiachain/kaia/kaiax/gov"
"github.com/kaiachain/kaia/kaiax/staking"
Expand All @@ -62,7 +61,6 @@ type BackendOpts struct {
PrivateKey *ecdsa.PrivateKey // Consensus message signing key
BlsSecretKey bls.SecretKey // Randao signing key. Required since Randao fork
DB database.DBManager
Governance governance.Engine // TODO-kaiax: Remove
GovModule gov.GovModule
BlsPubkeyProvider BlsPubkeyProvider // If not nil, override the default BLS public key provider
NodeType common.ConnType
Expand All @@ -87,7 +85,6 @@ func New(opts *BackendOpts) consensus.Istanbul {
recentMessages: recentMessages,
knownMessages: knownMessages,
rewardbase: opts.Rewardbase,
governance: opts.Governance,
govModule: opts.GovModule,
blsPubkeyProvider: opts.BlsPubkeyProvider,
nodetype: opts.NodeType,
Expand Down Expand Up @@ -143,8 +140,7 @@ type backend struct {
currentView atomic.Value //*istanbul.View

// Reference to the governance.Engine
governance governance.Engine
govModule gov.GovModule
govModule gov.GovModule

// Reference to BlsPubkeyProvider
blsPubkeyProvider BlsPubkeyProvider
Expand Down
3 changes: 0 additions & 3 deletions consensus/istanbul/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/consensus/istanbul"
"github.com/kaiachain/kaia/crypto"
"github.com/kaiachain/kaia/governance"
"github.com/kaiachain/kaia/kaiax/valset"
"github.com/kaiachain/kaia/params"
"github.com/kaiachain/kaia/storage/database"
Expand Down Expand Up @@ -154,7 +153,6 @@ func newTestBackendWithConfig(chainConfig *params.ChainConfig, blockPeriod uint6
// if governance mode is single, set the node key to the governing node.
chainConfig.Governance.GoverningNode = crypto.PubkeyToAddress(key.PublicKey)
}
g := governance.NewMixedEngine(chainConfig, dbm)
istanbulConfig := &istanbul.Config{
Epoch: chainConfig.Istanbul.Epoch,
ProposerPolicy: istanbul.ProposerPolicy(chainConfig.Istanbul.ProposerPolicy),
Expand All @@ -168,7 +166,6 @@ func newTestBackendWithConfig(chainConfig *params.ChainConfig, blockPeriod uint6
Rewardbase: common.HexToAddress("0x2A35FE72F847aa0B509e4055883aE90c87558AaD"),
PrivateKey: key,
DB: dbm,
Governance: g,
NodeType: common.CONSENSUSNODE,
}).(*backend)
return backend
Expand Down
20 changes: 0 additions & 20 deletions consensus/istanbul/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package backend
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"math/big"
"time"
Expand Down Expand Up @@ -426,22 +425,6 @@ func (sb *backend) Prepare(chain consensus.ChainReader, header *types.Header) er
// use the same blockscore for all blocks
header.BlockScore = defaultBlockScore

// If it reaches the Epoch, governance config will be added to block header
pset := sb.govModule.GetParamSet(number)
if number%pset.Epoch == 0 {
if g := sb.governance.GetGovernanceChange(); g != nil {
if data, err := json.Marshal(g); err != nil {
logger.Error("Failed to encode governance changes!! Possible configuration mismatch!! ")
} else {
if header.Governance, err = rlp.EncodeToBytes(data); err != nil {
logger.Error("Failed to encode governance data for the header", "num", number)
} else {
logger.Info("Put governanceData", "num", number, "data", hex.EncodeToString(header.Governance))
}
}
}
}

if chain.Config().IsRandaoForkEnabled(header.Number) {
prevMixHash := headerMixHash(chain, parent)
randomReveal, mixHash, err := sb.CalcRandao(header.Number, prevMixHash)
Expand Down Expand Up @@ -761,9 +744,6 @@ func (sb *backend) Stop() error {

// UpdateParam implements consensus.Istanbul.UpdateParam and it updates the governance parameters
func (sb *backend) UpdateParam(number uint64) error {
if err := sb.governance.UpdateParams(number); err != nil {
return err
}
return nil
}

Expand Down
1 change: 0 additions & 1 deletion consensus/istanbul/backend/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ func newBlockChain(n int, items ...interface{}) (*blockchain.BlockChain, *backen
if err != nil {
panic(err)
}
b.governance.SetBlockchain(bc)

// kaiax module setup
mGov := gov_impl.NewGovModule()
Expand Down
Loading

0 comments on commit 977e134

Please sign in to comment.