diff options
author | Sonic <sonic@dexon.org> | 2018-12-04 16:13:41 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:19 +0800 |
commit | 5d031e08ed2758eb7fa9c2d9c107204f93ae0733 (patch) | |
tree | 3a7ed42404b9b9ace5424578c7b2753444014417 /dex/handler.go | |
parent | 122e398cd89925216226f8f5a194c93bf8921897 (diff) | |
download | go-tangerine-5d031e08ed2758eb7fa9c2d9c107204f93ae0733.tar.gz go-tangerine-5d031e08ed2758eb7fa9c2d9c107204f93ae0733.tar.zst go-tangerine-5d031e08ed2758eb7fa9c2d9c107204f93ae0733.zip |
dex: replace total difficulty with block number (#73)
In dexon, we don't "mine" and the blockchain won't and shouldn't fork, so
there is no difficulty concept, just replace it with block number.
Note: this commit only replace total difficulty related logic and code
in dex, dex/downloader package.
Diffstat (limited to 'dex/handler.go')
-rw-r--r-- | dex/handler.go | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/dex/handler.go b/dex/handler.go index c64da2c87..5753a7cd8 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -38,7 +38,6 @@ import ( "errors" "fmt" "math" - "math/big" "net" "sync" "sync/atomic" @@ -350,9 +349,8 @@ func (pm *ProtocolManager) handle(p *peer) error { head = pm.blockchain.CurrentHeader() hash = head.Hash() number = head.Number.Uint64() - td = pm.blockchain.GetTd(hash, number) ) - if err := p.Handshake(pm.networkID, td, hash, genesis.Hash()); err != nil { + if err := p.Handshake(pm.networkID, number, hash, genesis.Hash()); err != nil { p.Log().Debug("Ethereum handshake failed", "err", err) return err } @@ -727,32 +725,32 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { break } // Retrieve and decode the propagated block - var request newBlockData - if err := msg.Decode(&request); err != nil { + var block types.Block + if err := msg.Decode(&block); err != nil { return errResp(ErrDecode, "%v: %v", msg, err) } - request.Block.ReceivedAt = msg.ReceivedAt - request.Block.ReceivedFrom = p + block.ReceivedAt = msg.ReceivedAt + block.ReceivedFrom = p // Mark the peer as owning the block and schedule it for import - p.MarkBlock(request.Block.Hash()) - pm.fetcher.Enqueue(p.id, request.Block) + p.MarkBlock(block.Hash()) + pm.fetcher.Enqueue(p.id, &block) // Assuming the block is importable by the peer, but possibly not yet done so, - // calculate the head hash and TD that the peer truly must have. + // calculate the head hash and number that the peer truly must have. var ( - trueHead = request.Block.ParentHash() - trueTD = new(big.Int).Sub(request.TD, request.Block.Difficulty()) + trueHead = block.ParentHash() + trueNumber = block.NumberU64() - 1 ) - // Update the peers total difficulty if better than the previous - if _, td := p.Head(); trueTD.Cmp(td) > 0 { - p.SetHead(trueHead, trueTD) + // Update the peers number if better than the previous + if _, number := p.Head(); trueNumber > number { + p.SetHead(trueHead, trueNumber) // Schedule a sync if above ours. Note, this will not fire a sync for a gap of - // a singe block (as the true TD is below the propagated block), however this + // a singe block (as the true number is below the propagated block), however this // scenario should easily be covered by the fetcher. currentBlock := pm.blockchain.CurrentBlock() - if trueTD.Cmp(pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())) > 0 { + if trueNumber > currentBlock.NumberU64() { go pm.synchronise(p) } } @@ -917,18 +915,14 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) { // If propagation is requested, send to a subset of the peer if propagate { - // Calculate the TD of the block (it's not imported yet, so block.Td is not valid) - var td *big.Int - if parent := pm.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent != nil { - td = new(big.Int).Add(block.Difficulty(), pm.blockchain.GetTd(block.ParentHash(), block.NumberU64()-1)) - } else { + if parent := pm.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent == nil { log.Error("Propagating dangling block", "number", block.Number(), "hash", hash) return } // Send the block to a subset of our peers transfer := peers[:int(math.Sqrt(float64(len(peers))))] for _, peer := range transfer { - peer.AsyncSendNewBlock(block, td) + peer.AsyncSendNewBlock(block) } log.Trace("Propagated block", "hash", hash, "recipients", len(transfer), "duration", common.PrettyDuration(time.Since(block.ReceivedAt))) return @@ -1201,21 +1195,21 @@ func (pm *ProtocolManager) peerSetLoop() { // NodeInfo represents a short summary of the Ethereum sub-protocol metadata // known about the host peer. type NodeInfo struct { - Network uint64 `json:"network"` // DEXON network ID (237=Mainnet, 238=Taiwan, 239=Taipei) - Difficulty *big.Int `json:"difficulty"` // Total difficulty of the host's blockchain - Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block - Config *params.ChainConfig `json:"config"` // Chain configuration for the fork rules - Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block + Network uint64 `json:"network"` // DEXON network ID (237=Mainnet, 238=Taiwan, 239=Taipei) + Number uint64 `json:"number"` // Total difficulty of the host's blockchain + Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block + Config *params.ChainConfig `json:"config"` // Chain configuration for the fork rules + Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block } // NodeInfo retrieves some protocol metadata about the running host node. func (pm *ProtocolManager) NodeInfo() *NodeInfo { currentBlock := pm.blockchain.CurrentBlock() return &NodeInfo{ - Network: pm.networkID, - Difficulty: pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()), - Genesis: pm.blockchain.Genesis().Hash(), - Config: pm.blockchain.Config(), - Head: currentBlock.Hash(), + Network: pm.networkID, + Number: currentBlock.NumberU64(), + Genesis: pm.blockchain.Genesis().Hash(), + Config: pm.blockchain.Config(), + Head: currentBlock.Hash(), } } |