Skip to content

Commit

Permalink
replace consensus/valset with kaiax/valset
Browse files Browse the repository at this point in the history
  • Loading branch information
yoomee1313 committed Dec 19, 2024
1 parent e304532 commit eec259c
Show file tree
Hide file tree
Showing 31 changed files with 825 additions and 1,618 deletions.
2 changes: 2 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/kaiax"
"github.com/kaiachain/kaia/kaiax/staking"
"github.com/kaiachain/kaia/kaiax/valset"
"github.com/kaiachain/kaia/networks/p2p"
"github.com/kaiachain/kaia/networks/rpc"
"github.com/kaiachain/kaia/params"
Expand Down Expand Up @@ -177,6 +178,7 @@ type Istanbul interface {
// UpdateParam updates the governance parameter
UpdateParam(num uint64) error

RegisterValsetModule(mValset valset.ValsetModule)
kaiax.ConsensusModuleHost
staking.StakingModuleHost
}
Expand Down
18 changes: 9 additions & 9 deletions consensus/istanbul/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,16 @@ type Backend interface {
// Address returns the owner's address
Address() common.Address

// Validators returns the validator set
Validators(proposal Proposal) ValidatorSet

// EventMux returns the event mux in backend
EventMux() *event.TypeMux

// Broadcast sends a message to all validators (include self)
Broadcast(prevHash common.Hash, valSet ValidatorSet, payload []byte) error
Broadcast(prevHash common.Hash, payload []byte) error

// Gossip sends a message to all validators (exclude self)
Gossip(valSet ValidatorSet, payload []byte) error
Gossip(payload []byte) error

GossipSubPeer(prevHash common.Hash, valSet ValidatorSet, payload []byte) map[common.Address]bool
GossipSubPeer(prevHash common.Hash, payload []byte)

// Commit delivers an approved proposal to backend.
// The delivered proposal will be put into blockchain.
Expand All @@ -75,9 +72,6 @@ type Backend interface {
// GetProposer returns the proposer of the given block height
GetProposer(number uint64) common.Address

// ParentValidators returns the validator set of the given proposal's parent block
ParentValidators(proposal Proposal) ValidatorSet

// HasBadProposal returns whether the proposal with the hash is a bad proposal
HasBadProposal(hash common.Hash) bool

Expand All @@ -86,4 +80,10 @@ type Backend interface {
SetCurrentView(view *View)

NodeType() common.ConnType

GetValidatorSet(num uint64) (*BlockValSet, error)

GetCommitteeState(num uint64) (*RoundCommitteeState, error)

GetCommitteeStateByRound(num uint64, round uint64) (*RoundCommitteeState, error)
}
156 changes: 29 additions & 127 deletions consensus/istanbul/backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type API struct {
}

// GetSnapshot retrieves the state snapshot at a given block.
// TODO-kaia-valset: consider deprecation of GetSnapshot
func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
// Retrieve the requested block number (or current if none requested)
header, err := headerByRpcNumber(api.chain, number)
Expand All @@ -58,6 +59,7 @@ func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
}

// GetSnapshotAtHash retrieves the state snapshot at a given block.
// TODO-kaia-valset: consider deprecation of GetSnapshotAtHash
func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
Expand All @@ -66,54 +68,18 @@ func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
return checkStatesAndGetSnapshot(api.chain, api.istanbul, header.Number.Uint64(), header.Hash())
}

// GetValidators retrieves the list of authorized validators with the given block number.
// GetValidators retrieves the list of qualified validators with the given block number.
func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error) {
header, err := headerByRpcNumber(api.chain, number)
if err != nil {
return nil, err
}

blockNumber := header.Number.Uint64()
if blockNumber == 0 {
// The committee of genesis block can not be calculated because it requires a previous block.
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return nil, errExtractIstanbulExtra
}
return istanbulExtra.Validators, nil
}

snap, err := checkStatesAndGetSnapshot(api.chain, api.istanbul, header.Number.Uint64()-1, header.ParentHash)
valSet, err := api.istanbul.GetValidatorSet(header.Number.Uint64())
if err != nil {
logger.Error("Failed to get snapshot.", "blockNum", blockNumber, "err", err)
return nil, err
}
return snap.validators(), nil
}

// GetValidatorsAtHash retrieves the list of authorized validators with the given block hash.
func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
return nil, errUnknownBlock
}

blockNumber := header.Number.Uint64()
if blockNumber == 0 {
// The committee of genesis block can not be calculated because it requires a previous block.
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return nil, errExtractIstanbulExtra
}
return istanbulExtra.Validators, nil
}

snap, err := checkStatesAndGetSnapshot(api.chain, api.istanbul, header.Number.Uint64()-1, header.ParentHash)
if err != nil {
logger.Error("Failed to get snapshot.", "blockNum", blockNumber, "err", err)
return nil, errInternalError
}
return snap.validators(), nil
return valSet.Qualified().BytesCmpSortedList(), nil
}

// GetDemotedValidators retrieves the list of authorized, but demoted validators with the given block number.
Expand All @@ -123,47 +89,31 @@ func (api *API) GetDemotedValidators(number *rpc.BlockNumber) ([]common.Address,
return nil, err
}

blockNumber := header.Number.Uint64()
if blockNumber == 0 {
snap, err := checkStatesAndGetSnapshot(api.chain, api.istanbul, header.Number.Uint64(), header.Hash())
if err != nil {
logger.Error("Failed to get snapshot.", "blockNum", blockNumber, "err", err)
return nil, err
}
return snap.demotedValidators(), nil
} else {
snap, err := checkStatesAndGetSnapshot(api.chain, api.istanbul, header.Number.Uint64()-1, header.ParentHash)
if err != nil {
logger.Error("Failed to get snapshot.", "blockNum", blockNumber, "err", err)
return nil, err
}
return snap.demotedValidators(), nil
valSet, err := api.istanbul.GetValidatorSet(header.Number.Uint64())
if err != nil {
return nil, err
}
return valSet.Demoted().BytesCmpSortedList(), nil
}

// GetDemotedValidatorsAtHash retrieves the list of authorized, but demoted validators with the given block hash.
func (api *API) GetDemotedValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
// GetValidatorsAtHash retrieves the list of authorized validators with the given block hash.
func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
return nil, errUnknownBlock
}
rpcBlockNumber := rpc.BlockNumber(header.Number.Uint64())
return api.GetValidators(&rpcBlockNumber)
}

blockNumber := header.Number.Uint64()
if blockNumber == 0 {
snap, err := checkStatesAndGetSnapshot(api.chain, api.istanbul, header.Number.Uint64(), header.Hash())
if err != nil {
logger.Error("Failed to get snapshot.", "blockNum", blockNumber, "err", err)
return nil, err
}
return snap.demotedValidators(), nil
} else {
snap, err := checkStatesAndGetSnapshot(api.chain, api.istanbul, header.Number.Uint64()-1, header.ParentHash)
if err != nil {
logger.Error("Failed to get snapshot.", "blockNum", blockNumber, "err", err)
return nil, err
}
return snap.demotedValidators(), nil
// GetDemotedValidatorsAtHash retrieves the list of demoted validators with the given block hash.
func (api *API) GetDemotedValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
header := api.chain.GetHeaderByHash(hash)
if header != nil {
return nil, errUnknownBlock
}
rpcBlockNumber := rpc.BlockNumber(header.Number.Uint64())
return api.GetDemotedValidators(&rpcBlockNumber)
}

// Candidates returns the current candidates the node tries to uphold and vote on.
Expand Down Expand Up @@ -210,8 +160,6 @@ var (
errStartLargerThanEnd = errors.New("start should be smaller than end")
errRequestedBlocksTooLarge = errors.New("number of requested blocks should be smaller than 50")
errRangeNil = errors.New("range values should not be nil")
errExtractIstanbulExtra = errors.New("extract Istanbul Extra from block header of the given block number")
errNoBlockExist = errors.New("block with the given block number is not existed")
errNoBlockNumber = errors.New("block number is not assigned")
)

Expand All @@ -222,85 +170,39 @@ func (api *APIExtension) GetCouncil(number *rpc.BlockNumber) ([]common.Address,
return nil, err
}

blockNumber := header.Number.Uint64()
if blockNumber == 0 {
// The committee of genesis block can not be calculated because it requires a previous block.
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return nil, errExtractIstanbulExtra
}
return istanbulExtra.Validators, nil
}

snap, err := checkStatesAndGetSnapshot(api.chain, api.istanbul, blockNumber-1, header.ParentHash)
valSet, err := api.istanbul.GetValidatorSet(header.Number.Uint64())
if err != nil {
logger.Error("Failed to get snapshot.", "blockNum", blockNumber, "err", err)
return nil, err
}

return append(snap.validators(), snap.demotedValidators()...), nil
return valSet.Council(), err
}

func (api *APIExtension) GetCouncilSize(number *rpc.BlockNumber) (int, error) {
council, err := api.GetCouncil(number)
if err == nil {
return len(council), nil
} else {
if err != nil {
return -1, err
}
return len(council), nil
}

func (api *APIExtension) GetCommittee(number *rpc.BlockNumber) ([]common.Address, error) {
header, err := headerByRpcNumber(api.chain, number)
if err != nil {
return nil, err
}

blockNumber := header.Number.Uint64()
if blockNumber == 0 {
// The committee of genesis block can not be calculated because it requires a previous block.
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return nil, errExtractIstanbulExtra
}
return istanbulExtra.Validators, nil
}

snap, err := checkStatesAndGetSnapshot(api.chain, api.istanbul, blockNumber-1, header.ParentHash)
if err != nil {
return nil, err
}
round := header.Round()
view := &istanbul.View{
Sequence: new(big.Int).SetUint64(blockNumber),
Round: new(big.Int).SetUint64(uint64(round)),
}

// get the proposer of this block.
proposer, err := ecrecover(header)
roundState, err := api.istanbul.GetCommitteeState(header.Number.Uint64())
if err != nil {
return nil, err
}

parentHash := header.ParentHash

// get the committee list of this block at the view (blockNumber, round)
committee := snap.ValSet.SubListWithProposer(parentHash, proposer, view)
addresses := make([]common.Address, len(committee))
for i, v := range committee {
addresses[i] = v.Address()
}

return addresses, nil
return roundState.Committee(), nil
}

func (api *APIExtension) GetCommitteeSize(number *rpc.BlockNumber) (int, error) {
committee, err := api.GetCommittee(number)
if err == nil {
return len(committee), nil
} else {
if err != nil {
return -1, err
}
return len(committee), nil
}

func (api *APIExtension) makeRPCBlockOutput(b *types.Block,
Expand Down
Loading

0 comments on commit eec259c

Please sign in to comment.