Skip to content

Commit

Permalink
Merge pull request ethereum#292 from XinFinOrg/add-standbynodes-api
Browse files Browse the repository at this point in the history
add standbynodes in GetMasternodesByNumber similar way to subnet
  • Loading branch information
wanwiset25 authored Jul 24, 2023
2 parents 2c1aba8 + a959bea commit 176a3d0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
31 changes: 18 additions & 13 deletions consensus/XDPoS/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ type SignerTypes struct {
}

type MasternodesStatus struct {
Number uint64
Round types.Round
MasternodesLen int
Masternodes []common.Address
PenaltyLen int
Penalty []common.Address
Error error
Number uint64
Round types.Round
MasternodesLen int
Masternodes []common.Address
PenaltyLen int
Penalty []common.Address
StandbynodesLen int
Standbynodes []common.Address
Error error
}

type MessageStatus map[string]map[string]SignerTypes
Expand Down Expand Up @@ -144,14 +146,17 @@ func (api *API) GetMasternodesByNumber(number *rpc.BlockNumber) MasternodesStatu

masterNodes := api.XDPoS.EngineV2.GetMasternodes(api.chain, header)
penalties := api.XDPoS.EngineV2.GetPenalties(api.chain, header)
standbynodes := api.XDPoS.EngineV2.GetStandbynodes(api.chain, header)

info := MasternodesStatus{
Number: header.Number.Uint64(),
Round: round,
MasternodesLen: len(masterNodes),
Masternodes: masterNodes,
PenaltyLen: len(penalties),
Penalty: penalties,
Number: header.Number.Uint64(),
Round: round,
MasternodesLen: len(masterNodes),
Masternodes: masterNodes,
PenaltyLen: len(penalties),
Penalty: penalties,
StandbynodesLen: len(standbynodes),
Standbynodes: standbynodes,
}
return info
}
Expand Down
11 changes: 10 additions & 1 deletion consensus/XDPoS/engines/engine_v2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,12 +987,21 @@ func (x *XDPoS_v2) GetMasternodes(chain consensus.ChainReader, header *types.Hea
func (x *XDPoS_v2) GetPenalties(chain consensus.ChainReader, header *types.Header) []common.Address {
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash())
if err != nil {
log.Error("[GetMasternodes] Adaptor v2 getEpochSwitchInfo has error", "err", err)
log.Error("[GetPenalties] Adaptor v2 getEpochSwitchInfo has error", "err", err)
return []common.Address{}
}
return epochSwitchInfo.Penalties
}

func (x *XDPoS_v2) GetStandbynodes(chain consensus.ChainReader, header *types.Header) []common.Address {
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash())
if err != nil {
log.Error("[GetStandbynodes] Adaptor v2 getEpochSwitchInfo has error", "err", err)
return []common.Address{}
}
return epochSwitchInfo.Standbynodes
}

// Calculate masternodes for a block number and parent hash. In V2, truncating candidates[:MaxMasternodes] is done in this function.
func (x *XDPoS_v2) calcMasternodes(chain consensus.ChainReader, blockNum *big.Int, parentHash common.Hash) ([]common.Address, []common.Address, error) {
// using new max masterndoes
Expand Down
19 changes: 17 additions & 2 deletions consensus/XDPoS/engines/engine_v2/epochSwitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,25 @@ func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainReader, header *types
log.Error("[getEpochSwitchInfo] get extra field", "err", err, "number", h.Number.Uint64())
return nil, err
}

snap, err := x.getSnapshot(chain, header.Number.Uint64(), false)
if err != nil {
log.Error("[getEpochSwitchInfo] Adaptor v2 getSnapshot has error", "err", err)
return nil, err
}
penalties := common.ExtractAddressFromBytes(h.Penalties)
candidates := snap.NextEpochMasterNodes
standbynodes := []common.Address{}
if len(masternodes) != len(candidates) {
standbynodes = candidates
standbynodes = common.RemoveItemFromArray(standbynodes, masternodes)
standbynodes = common.RemoveItemFromArray(standbynodes, penalties)
}

epochSwitchInfo := &types.EpochSwitchInfo{
Penalties: penalties,
Masternodes: masternodes,
Penalties: penalties,
Standbynodes: standbynodes,
Masternodes: masternodes,
EpochSwitchBlockInfo: &types.BlockInfo{
Hash: hash,
Number: h.Number,
Expand Down
1 change: 1 addition & 0 deletions core/types/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (e *ExtraFields_v2) EncodeToBytes() ([]byte, error) {

type EpochSwitchInfo struct {
Penalties []common.Address
Standbynodes []common.Address
Masternodes []common.Address
EpochSwitchBlockInfo *BlockInfo
EpochSwitchParentBlockInfo *BlockInfo
Expand Down

0 comments on commit 176a3d0

Please sign in to comment.