From 50ae0c95fd2dbd5e3416ef0b08671f46779ff046 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 5 Jun 2022 11:06:32 +0200 Subject: [PATCH] xin-197 xin-198 fix devnet issues (#99) * xin-197 xin-198 fix devnet issues * update log --- consensus/XDPoS/engines/engine_v2/engine.go | 50 +++++-------------- consensus/XDPoS/engines/engine_v2/timeout.go | 8 +-- .../authorised_masternode_test.go | 6 +-- .../engine_v2_tests/proposed_block_test.go | 14 +++++- eth/backend.go | 2 +- go.mod | 2 +- go.sum | 2 + miner/miner.go | 3 +- 8 files changed, 39 insertions(+), 48 deletions(-) diff --git a/consensus/XDPoS/engines/engine_v2/engine.go b/consensus/XDPoS/engines/engine_v2/engine.go index 0bcc0d70b7b4..63a659ffb87a 100644 --- a/consensus/XDPoS/engines/engine_v2/engine.go +++ b/consensus/XDPoS/engines/engine_v2/engine.go @@ -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 } @@ -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) @@ -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 @@ -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 diff --git a/consensus/XDPoS/engines/engine_v2/timeout.go b/consensus/XDPoS/engines/engine_v2/timeout.go index f0aaafa31c1a..7ec976813070 100644 --- a/consensus/XDPoS/engines/engine_v2/timeout.go +++ b/consensus/XDPoS/engines/engine_v2/timeout.go @@ -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 diff --git a/consensus/tests/engine_v2_tests/authorised_masternode_test.go b/consensus/tests/engine_v2_tests/authorised_masternode_test.go index 017f93cc0a39..9ac58044e137 100644 --- a/consensus/tests/engine_v2_tests/authorised_masternode_test.go +++ b/consensus/tests/engine_v2_tests/authorised_masternode_test.go @@ -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 diff --git a/consensus/tests/engine_v2_tests/proposed_block_test.go b/consensus/tests/engine_v2_tests/proposed_block_test.go index 28742b3f49ba..a85a59898d9b 100644 --- a/consensus/tests/engine_v2_tests/proposed_block_test.go +++ b/consensus/tests/engine_v2_tests/proposed_block_test.go @@ -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) + } } diff --git a/eth/backend.go b/eth/backend.go index 519f528a5671..9fe46f6e48e5 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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) diff --git a/go.mod b/go.mod index 938ddfc3e3b4..8dcc5d45f438 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 7caf629ce96f..cc3ce663a1e5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/miner/miner.go b/miner/miner.go index 80b5af19e05b..4a9d34b9f7fa 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -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"