mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 10:20:44 +00:00
Fix API penalty bug and add more info in api (#262)
* fix api bug * remove cmd
This commit is contained in:
parent
5b75d3a904
commit
153f8d296d
7 changed files with 57 additions and 20 deletions
|
|
@ -60,11 +60,13 @@ type SignerTypes struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type MasternodesStatus struct {
|
type MasternodesStatus struct {
|
||||||
|
Number uint64
|
||||||
|
Round types.Round
|
||||||
MasternodesLen int
|
MasternodesLen int
|
||||||
Masternodes []common.Address
|
Masternodes []common.Address
|
||||||
|
PenaltyLen int
|
||||||
PenaltyLen int
|
Penalty []common.Address
|
||||||
Penalty []common.Address
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageStatus map[string]map[string]SignerTypes
|
type MessageStatus map[string]map[string]SignerTypes
|
||||||
|
|
@ -130,13 +132,22 @@ func (api *API) GetMasternodesByNumber(number *rpc.BlockNumber) MasternodesStatu
|
||||||
} else {
|
} else {
|
||||||
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
|
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
|
||||||
}
|
}
|
||||||
masternodes, penalties, err := api.XDPoS.EngineV2.CalcMasternodes(api.chain, header.Number, header.ParentHash)
|
|
||||||
|
round, err := api.XDPoS.EngineV2.GetRoundNumber(header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MasternodesStatus{}
|
return MasternodesStatus{
|
||||||
|
Error: err,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
masterNodes := api.XDPoS.EngineV2.GetMasternodes(api.chain, header)
|
||||||
|
penalties := api.XDPoS.EngineV2.GetPenalties(api.chain, header)
|
||||||
|
|
||||||
info := MasternodesStatus{
|
info := MasternodesStatus{
|
||||||
MasternodesLen: len(masternodes),
|
Number: header.Number.Uint64(),
|
||||||
Masternodes: masternodes,
|
Round: round,
|
||||||
|
MasternodesLen: len(masterNodes),
|
||||||
|
Masternodes: masterNodes,
|
||||||
PenaltyLen: len(penalties),
|
PenaltyLen: len(penalties),
|
||||||
Penalty: penalties,
|
Penalty: penalties,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ func TestCalculateSignersVote(t *testing.T) {
|
||||||
masternodes := []common.Address{{1}, {2}, {3}}
|
masternodes := []common.Address{{1}, {2}, {3}}
|
||||||
|
|
||||||
vote1 := types.Vote{
|
vote1 := types.Vote{
|
||||||
|
Signature: types.Signature{1},
|
||||||
ProposedBlockInfo: &types.BlockInfo{
|
ProposedBlockInfo: &types.BlockInfo{
|
||||||
Hash: common.Hash{1},
|
Hash: common.Hash{1},
|
||||||
Round: types.Round(10),
|
Round: types.Round(10),
|
||||||
|
|
@ -27,10 +28,11 @@ func TestCalculateSignersVote(t *testing.T) {
|
||||||
vote1.SetSigner(common.Address{1})
|
vote1.SetSigner(common.Address{1})
|
||||||
|
|
||||||
vote2 := types.Vote{
|
vote2 := types.Vote{
|
||||||
|
Signature: types.Signature{2},
|
||||||
ProposedBlockInfo: &types.BlockInfo{
|
ProposedBlockInfo: &types.BlockInfo{
|
||||||
Hash: common.Hash{2},
|
Hash: common.Hash{1},
|
||||||
Round: types.Round(11),
|
Round: types.Round(10),
|
||||||
Number: big.NewInt(911),
|
Number: big.NewInt(910),
|
||||||
},
|
},
|
||||||
GapNumber: 450,
|
GapNumber: 450,
|
||||||
}
|
}
|
||||||
|
|
@ -40,9 +42,7 @@ func TestCalculateSignersVote(t *testing.T) {
|
||||||
votes.Add(&vote2)
|
votes.Add(&vote2)
|
||||||
|
|
||||||
calculateSigners(info, votes.Get(), masternodes)
|
calculateSigners(info, votes.Get(), masternodes)
|
||||||
|
assert.Equal(t, info["10:450:910:0x0100000000000000000000000000000000000000000000000000000000000000"].CurrentNumber, 2)
|
||||||
//assert.Equal(t, info["xxx"].CurrentNumber, 2)
|
|
||||||
assert.Equal(t, 2, 2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCalculateSignersTimeout(t *testing.T) {
|
func TestCalculateSignersTimeout(t *testing.T) {
|
||||||
|
|
@ -52,13 +52,15 @@ func TestCalculateSignersTimeout(t *testing.T) {
|
||||||
masternodes := []common.Address{{1}, {2}, {3}}
|
masternodes := []common.Address{{1}, {2}, {3}}
|
||||||
|
|
||||||
timeout1 := types.Timeout{
|
timeout1 := types.Timeout{
|
||||||
|
Signature: types.Signature{1},
|
||||||
Round: types.Round(10),
|
Round: types.Round(10),
|
||||||
GapNumber: 450,
|
GapNumber: 450,
|
||||||
}
|
}
|
||||||
timeout1.SetSigner(common.Address{1})
|
timeout1.SetSigner(common.Address{1})
|
||||||
|
|
||||||
timeout2 := types.Timeout{
|
timeout2 := types.Timeout{
|
||||||
Round: types.Round(11),
|
Signature: types.Signature{2},
|
||||||
|
Round: types.Round(10),
|
||||||
GapNumber: 450,
|
GapNumber: 450,
|
||||||
}
|
}
|
||||||
timeout1.SetSigner(common.Address{2})
|
timeout1.SetSigner(common.Address{2})
|
||||||
|
|
@ -67,7 +69,5 @@ func TestCalculateSignersTimeout(t *testing.T) {
|
||||||
timeouts.Add(&timeout2)
|
timeouts.Add(&timeout2)
|
||||||
|
|
||||||
calculateSigners(info, timeouts.Get(), masternodes)
|
calculateSigners(info, timeouts.Get(), masternodes)
|
||||||
|
assert.Equal(t, info["10:450"].CurrentNumber, 2)
|
||||||
//assert.Equal(t, info["xxx"].CurrentNumber, 2)
|
|
||||||
assert.Equal(t, 2, 2)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -972,14 +972,20 @@ func (x *XDPoS_v2) GetMasternodesFromEpochSwitchHeader(epochSwitchHeader *types.
|
||||||
func (x *XDPoS_v2) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address {
|
func (x *XDPoS_v2) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address {
|
||||||
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash())
|
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[GetMasternodes] Adaptor v2 getEpochSwitchInfo has error, potentially bug", "err", err)
|
log.Error("[GetMasternodes] Adaptor v2 getEpochSwitchInfo has error", "err", err)
|
||||||
return []common.Address{}
|
return []common.Address{}
|
||||||
}
|
}
|
||||||
return epochSwitchInfo.Masternodes
|
return epochSwitchInfo.Masternodes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *XDPoS_v2) CalcMasternodes(chain consensus.ChainReader, blockNum *big.Int, parentHash common.Hash) ([]common.Address, []common.Address, error) {
|
// Given header, get master node from the epoch switch block of that epoch
|
||||||
return x.calcMasternodes(chain, blockNum, parentHash)
|
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)
|
||||||
|
return []common.Address{}
|
||||||
|
}
|
||||||
|
return epochSwitchInfo.Penalties
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *XDPoS_v2) calcMasternodes(chain consensus.ChainReader, blockNum *big.Int, parentHash common.Hash) ([]common.Address, []common.Address, error) {
|
func (x *XDPoS_v2) calcMasternodes(chain consensus.ChainReader, blockNum *big.Int, parentHash common.Hash) ([]common.Address, []common.Address, error) {
|
||||||
|
|
|
||||||
|
|
@ -53,9 +53,12 @@ func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainReader, header *types
|
||||||
log.Debug("[getEpochSwitchInfo] header is epoch switch", "hash", hash.Hex(), "number", h.Number.Uint64())
|
log.Debug("[getEpochSwitchInfo] header is epoch switch", "hash", hash.Hex(), "number", h.Number.Uint64())
|
||||||
quorumCert, round, masternodes, err := x.getExtraFields(h)
|
quorumCert, round, masternodes, err := x.getExtraFields(h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error("[getEpochSwitchInfo] get extra field", "err", err, "number", h.Number.Uint64())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
penalties := common.ExtractAddressFromBytes(h.Penalties)
|
||||||
epochSwitchInfo := &types.EpochSwitchInfo{
|
epochSwitchInfo := &types.EpochSwitchInfo{
|
||||||
|
Penalties: penalties,
|
||||||
Masternodes: masternodes,
|
Masternodes: masternodes,
|
||||||
EpochSwitchBlockInfo: &types.BlockInfo{
|
EpochSwitchBlockInfo: &types.BlockInfo{
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,7 @@ func (x *XDPoS_v2) getExtraFields(header *types.Header) (*types.QuorumCert, type
|
||||||
var decodedExtraField types.ExtraFields_v2
|
var decodedExtraField types.ExtraFields_v2
|
||||||
err := utils.DecodeBytesExtraFields(header.Extra, &decodedExtraField)
|
err := utils.DecodeBytesExtraFields(header.Extra, &decodedExtraField)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error("[getExtraFields] error on decode extra fields", "err", err, "extra", header.Extra)
|
||||||
return nil, types.Round(0), masternodes, err
|
return nil, types.Round(0), masternodes, err
|
||||||
}
|
}
|
||||||
return decodedExtraField.QuorumCert, decodedExtraField.Round, masternodes, nil
|
return decodedExtraField.QuorumCert, decodedExtraField.Round, masternodes, nil
|
||||||
|
|
|
||||||
|
|
@ -136,3 +136,18 @@ func TestHookPenaltyV2LessThen150Blocks(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, 2, len(penalty))
|
assert.Equal(t, 2, len(penalty))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPenalties(t *testing.T) {
|
||||||
|
config := params.TestXDPoSMockChainConfig
|
||||||
|
blockchain, _, _, _, _ := PrepareXDCTestBlockChainWithPenaltyForV2Engine(t, int(config.XDPoS.Epoch)*3, config)
|
||||||
|
adaptor := blockchain.Engine().(*XDPoS.XDPoS)
|
||||||
|
|
||||||
|
header2699 := blockchain.GetHeaderByNumber(2699)
|
||||||
|
header1801 := blockchain.GetHeaderByNumber(1801)
|
||||||
|
|
||||||
|
penalty2699 := adaptor.EngineV2.GetPenalties(blockchain, header2699)
|
||||||
|
penalty1801 := adaptor.EngineV2.GetPenalties(blockchain, header1801)
|
||||||
|
|
||||||
|
assert.Equal(t, 1, len(penalty2699))
|
||||||
|
assert.Equal(t, 1, len(penalty1801))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -111,6 +111,7 @@ func (e *ExtraFields_v2) EncodeToBytes() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type EpochSwitchInfo struct {
|
type EpochSwitchInfo struct {
|
||||||
|
Penalties []common.Address
|
||||||
Masternodes []common.Address
|
Masternodes []common.Address
|
||||||
EpochSwitchBlockInfo *BlockInfo
|
EpochSwitchBlockInfo *BlockInfo
|
||||||
EpochSwitchParentBlockInfo *BlockInfo
|
EpochSwitchParentBlockInfo *BlockInfo
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue