Skip to content

Commit

Permalink
xin-197 xin-198 fix devnet issues (ethereum#99)
Browse files Browse the repository at this point in the history
* xin-197 xin-198 fix devnet issues

* update log
  • Loading branch information
liam-lai authored Jun 5, 2022
1 parent 0317e87 commit 50ae0c9
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 48 deletions.
50 changes: 13 additions & 37 deletions consensus/XDPoS/engines/engine_v2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,35 +422,16 @@ func (x *XDPoS_v2) CalcDifficulty(chain consensus.ChainReader, time uint64, pare
}

func (x *XDPoS_v2) IsAuthorisedAddress(chain consensus.ChainReader, header *types.Header, address common.Address) bool {
x.lock.RLock()
defer x.lock.RUnlock()

_, round, _, err := x.getExtraFields(header)
snap, err := x.GetSnapshot(chain, header)
if err != nil {
log.Error("[IsAuthorisedAddress] Fail to decode v2 extra data", "Hash", header.Hash().Hex(), "Extra", header.Extra, "Error", err)
log.Error("[IsAuthorisedAddress] Can't get snapshot with at ", "number", header.Number, "hash", header.Hash().Hex(), "err", err)
return false
}
blockRound := round

masterNodes := x.GetMasternodes(chain, header)

if len(masterNodes) == 0 {
log.Error("[IsAuthorisedAddress] Fail to find any master nodes from current block round epoch", "Hash", header.Hash().Hex(), "Round", blockRound, "Number", header.Number)
return false
}

for index, masterNodeAddress := range masterNodes {
if masterNodeAddress == address {
log.Debug("[IsAuthorisedAddress] Found matching master node address", "index", index, "Address", address, "MasterNodes", masterNodes)
for _, mn := range snap.NextEpochMasterNodes {
if mn == address {
return true
}
}

log.Warn("Not authorised address", "Address", address.Hex(), "Hash", header.Hash().Hex(), "round", round)
for index, mn := range masterNodes {
log.Warn("Master node list item", "mn", mn.Hex(), "index", index)
}

return false
}

Expand Down Expand Up @@ -679,9 +660,9 @@ func (x *XDPoS_v2) ProposedBlockHandler(chain consensus.ChainReader, blockHeader
return err
}

err = x.allowedToSend(chain, blockHeader, "vote")
if err != nil {
return err
allow := x.allowedToSend(chain, blockHeader, "vote")
if !allow {
return nil
}

verified, err := x.verifyVotingRule(chain, blockInfo, quorumCert)
Expand Down Expand Up @@ -1013,8 +994,7 @@ func (x *XDPoS_v2) FindParentBlockToAssign(chain consensus.ChainReader) *types.B
return parent
}

func (x *XDPoS_v2) allowedToSend(chain consensus.ChainReader, blockHeader *types.Header, sendType string) error {
allowedToSend := false
func (x *XDPoS_v2) allowedToSend(chain consensus.ChainReader, blockHeader *types.Header, sendType string) bool {
// Don't hold the signFn for the whole signing operation
x.signLock.RLock()
signer := x.signer
Expand All @@ -1024,18 +1004,14 @@ func (x *XDPoS_v2) allowedToSend(chain consensus.ChainReader, blockHeader *types
for i, mn := range masterNodes {
if signer == mn {
log.Debug("[allowedToSend] Yes, I'm allowed to send", "sendType", sendType, "MyAddress", signer.Hex(), "Index in master node list", i)
allowedToSend = true
break
return true
}
}
if !allowedToSend {
for _, mn := range masterNodes {
log.Debug("[allowedToSend] Master node list", "masterNodeAddress", mn.Hash())
}
log.Warn("[allowedToSend] Not in the Masternode list, not suppose to send", "sendType", sendType, "MyAddress", signer.Hex())
return fmt.Errorf("Not in the master node list, not suppose to %v", sendType)
for _, mn := range masterNodes {
log.Debug("[allowedToSend] Master node list", "masterNodeAddress", mn.Hash())
}
return nil
log.Info("[allowedToSend] Not in the Masternode list, not suppose to send message", "sendType", sendType, "MyAddress", signer.Hex())
return false
}

// Periodlly execution(Attached to engine initialisation during "new"). Used for pool cleaning etc
Expand Down
8 changes: 4 additions & 4 deletions consensus/XDPoS/engines/engine_v2/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ func (x *XDPoS_v2) OnCountdownTimeout(time time.Time, chain interface{}) error {
defer x.lock.Unlock()

// Check if we are within the master node list
err := x.allowedToSend(chain.(consensus.ChainReader), chain.(consensus.ChainReader).CurrentHeader(), "timeout")
if err != nil {
return err
allow := x.allowedToSend(chain.(consensus.ChainReader), chain.(consensus.ChainReader).CurrentHeader(), "timeout")
if !allow {
return nil
}

err = x.sendTimeout(chain.(consensus.ChainReader))
err := x.sendTimeout(chain.(consensus.ChainReader))
if err != nil {
log.Error("Error while sending out timeout message at time: ", time)
return err
Expand Down
6 changes: 3 additions & 3 deletions consensus/tests/engine_v2_tests/authorised_masternode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

func TestIsAuthorisedMNForConsensusV2(t *testing.T) {
// we skip test for v1 since it's hard to make a real genesis block
blockchain, _, currentBlock, signer, signFn, _ := PrepareXDCTestBlockChainForV2Engine(t, 900, params.TestXDPoSMockChainConfig, nil)
blockchain, _, currentBlock, signer, signFn, _ := PrepareXDCTestBlockChainForV2Engine(t, 901, params.TestXDPoSMockChainConfig, nil)
adaptor := blockchain.Engine().(*XDPoS.XDPoS)
blockNum := 901
blockNum := 902
blockCoinBase := "0x111000000000000000000000000000000123"
currentBlock = CreateBlock(blockchain, params.TestXDPoSMockChainConfig, currentBlock, blockNum, 1, blockCoinBase, signer, signFn, nil, nil)
currentBlock = CreateBlock(blockchain, params.TestXDPoSMockChainConfig, currentBlock, blockNum, 2, blockCoinBase, signer, signFn, nil, nil)
err := blockchain.InsertBlock(currentBlock)
assert.Nil(t, err)
// As long as the address is in the master node list, they are all valid
Expand Down
14 changes: 13 additions & 1 deletion consensus/tests/engine_v2_tests/proposed_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,17 @@ func TestProposedBlockMessageHandlerNotGenerateVoteIfSignerNotInMNlist(t *testin
}

err = engineV2.ProposedBlockHandler(blockchain, currentBlock.Header())
assert.Equal(t, "Not in the master node list, not suppose to vote", err.Error())
if err != nil {
t.Fatal("Fail propose proposedBlock handler", err)
}

// Should not receive anything from the channel
select {
case <-engineV2.BroadcastCh:
t.Fatal("Should not trigger vote")
case <-time.After(2 * time.Second):
// Shoud not trigger setNewRound
round, _, _, _, _, _ := engineV2.GetPropertiesFaker()
assert.Equal(t, types.Round(6), round)
}
}
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func (s *Ethereum) StartStaking(local bool) error {
if XDPoS, ok := s.engine.(*XDPoS.XDPoS); ok {
wallet, err := s.accountManager.Find(accounts.Account{Address: eb})
if wallet == nil || err != nil {
log.Error("Etherbase account unavailable locally", "err", err)
log.Error("Etherbase account unavailable locally", "err", err, "address", eb.Hex())
return fmt.Errorf("signer missing: %v", err)
}
XDPoS.Authorize(eb, wallet.SignHash)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/btcsuite/winsvc v1.0.0 // indirect
github.com/cespare/cp v1.1.1
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea
github.com/deckarep/golang-set v1.8.0
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf
github.com/edsrzf/mmap-go v1.0.0
github.com/elastic/gosigar v0.10.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea h1:j4317fAZh7X6GqbFowYdYdI0L9bwxL07jyPZIdepyZ0=
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4=
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf h1:sh8rkQZavChcmakYiSlqu2425CHyFXLZZnvm7PDpU8M=
Expand Down
3 changes: 2 additions & 1 deletion miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package miner

import (
"fmt"
"github.com/XinFinOrg/XDPoSChain/XDCxlending"
"sync/atomic"

"github.com/XinFinOrg/XDPoSChain/XDCxlending"

"github.com/XinFinOrg/XDPoSChain/XDCx"
"github.com/XinFinOrg/XDPoSChain/accounts"
"github.com/XinFinOrg/XDPoSChain/common"
Expand Down

0 comments on commit 50ae0c9

Please sign in to comment.