diff options
author | Sonic <sonic@dexon.org> | 2019-05-08 17:05:04 +0800 |
---|---|---|
committer | Sonic <sonic@dexon.org> | 2019-05-08 17:05:04 +0800 |
commit | 53db7e5a2ae7eb85fa5a2fc412d5746ef6f0f583 (patch) | |
tree | 3b347dcddd84f986398458d9ddea6f8b77523290 | |
parent | b6c2c72d553e4680259fc8d05b0bcf9f4d0d4b58 (diff) | |
download | dexon-53db7e5a2ae7eb85fa5a2fc412d5746ef6f0f583.tar.gz dexon-53db7e5a2ae7eb85fa5a2fc412d5746ef6f0f583.tar.zst dexon-53db7e5a2ae7eb85fa5a2fc412d5746ef6f0f583.zip |
lds: replace eth/downloader with dex/downloader
-rw-r--r-- | lds/backend.go | 2 | ||||
-rw-r--r-- | lds/fetcher.go | 6 | ||||
-rw-r--r-- | lds/handler.go | 22 | ||||
-rw-r--r-- | lds/handler_test.go | 2 | ||||
-rw-r--r-- | lds/peer.go | 19 | ||||
-rw-r--r-- | lds/sync.go | 4 |
6 files changed, 32 insertions, 23 deletions
diff --git a/lds/backend.go b/lds/backend.go index 3fa033b91..6289f2233 100644 --- a/lds/backend.go +++ b/lds/backend.go @@ -32,7 +32,7 @@ import ( "github.com/dexon-foundation/dexon/core/rawdb" "github.com/dexon-foundation/dexon/core/types" "github.com/dexon-foundation/dexon/dex" - "github.com/dexon-foundation/dexon/eth/downloader" + "github.com/dexon-foundation/dexon/dex/downloader" "github.com/dexon-foundation/dexon/eth/filters" "github.com/dexon-foundation/dexon/eth/gasprice" "github.com/dexon-foundation/dexon/event" diff --git a/lds/fetcher.go b/lds/fetcher.go index 8db34398c..b58918a08 100644 --- a/lds/fetcher.go +++ b/lds/fetcher.go @@ -101,7 +101,7 @@ type fetchRequest struct { // fetchResponse represents a header download response type fetchResponse struct { reqID uint64 - headers []*types.Header + headers []*types.HeaderWithGovState peer *peer } @@ -506,7 +506,7 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64, bool) { } // deliverHeaders delivers header download request responses for processing -func (f *lightFetcher) deliverHeaders(peer *peer, reqID uint64, headers []*types.Header) { +func (f *lightFetcher) deliverHeaders(peer *peer, reqID uint64, headers []*types.HeaderWithGovState) { f.deliverChn <- fetchResponse{reqID: reqID, headers: headers, peer: peer} } @@ -518,7 +518,7 @@ func (f *lightFetcher) processResponse(req fetchRequest, resp fetchResponse) boo } headers := make([]*types.Header, req.amount) for i, header := range resp.headers { - headers[int(req.amount)-1-i] = header + headers[int(req.amount)-1-i] = header.Header } if _, err := f.chain.InsertHeaderChain(headers, 1); err != nil { if err == consensus.ErrFutureBlock { diff --git a/lds/handler.go b/lds/handler.go index d8a6fa3b5..6464cb45b 100644 --- a/lds/handler.go +++ b/lds/handler.go @@ -26,14 +26,17 @@ import ( "sync" "time" + dexCore "github.com/dexon-foundation/dexon-consensus/core" + "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/common/mclock" "github.com/dexon-foundation/dexon/consensus" + "github.com/dexon-foundation/dexon/consensus/dexcon" "github.com/dexon-foundation/dexon/core" "github.com/dexon-foundation/dexon/core/rawdb" "github.com/dexon-foundation/dexon/core/state" "github.com/dexon-foundation/dexon/core/types" - "github.com/dexon-foundation/dexon/eth/downloader" + "github.com/dexon-foundation/dexon/dex/downloader" "github.com/dexon-foundation/dexon/ethdb" "github.com/dexon-foundation/dexon/event" "github.com/dexon-foundation/dexon/light" @@ -76,8 +79,11 @@ type BlockChain interface { GetTd(hash common.Hash, number uint64) *big.Int State() (*state.StateDB, error) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) + InsertDexonHeaderChain([]*types.HeaderWithGovState, + dexcon.GovernanceStateFetcher, *dexCore.TSigVerifierCache) (int, error) Rollback(chain []common.Hash) GetHeaderByNumber(number uint64) *types.Header + GetGovStateByNumber(number uint64) (*types.GovState, error) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) Genesis() *types.Block SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription @@ -502,7 +508,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // A batch of headers arrived to one of our previous requests var resp struct { ReqID, BV uint64 - Headers []*types.Header + Headers []*types.HeaderWithGovState } if err := msg.Decode(&resp); err != nil { return errResp(ErrDecode, "msg %v: %v", msg, err) @@ -1026,11 +1032,11 @@ type peerConnection struct { peer *peer } -func (pc *peerConnection) Head() (common.Hash, *big.Int) { - return pc.peer.HeadAndTd() +func (pc *peerConnection) Head() (common.Hash, uint64) { + return pc.peer.HeadAndNumber() } -func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error { +func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool, withGov bool) error { reqID := genReqID() rq := &distReq{ getCost: func(dp distPeer) uint64 { @@ -1054,7 +1060,7 @@ func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, s return nil } -func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error { +func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool, withGov bool) error { reqID := genReqID() rq := &distReq{ getCost: func(dp distPeer) uint64 { @@ -1078,6 +1084,10 @@ func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip return nil } +func (pc *peerConnection) RequestGovStateByHash(hash common.Hash) error { + return fmt.Errorf("Not implemented yet") +} + func (d *downloaderPeerNotify) registerPeer(p *peer) { pm := (*ProtocolManager)(d) pc := &peerConnection{ diff --git a/lds/handler_test.go b/lds/handler_test.go index c148679fa..7be94915b 100644 --- a/lds/handler_test.go +++ b/lds/handler_test.go @@ -29,7 +29,7 @@ import ( "github.com/dexon-foundation/dexon/core/rawdb" "github.com/dexon-foundation/dexon/core/types" "github.com/dexon-foundation/dexon/crypto" - "github.com/dexon-foundation/dexon/eth/downloader" + "github.com/dexon-foundation/dexon/dex/downloader" "github.com/dexon-foundation/dexon/ethdb" "github.com/dexon-foundation/dexon/light" "github.com/dexon-foundation/dexon/p2p" diff --git a/lds/peer.go b/lds/peer.go index e0ee23236..b41824ad7 100644 --- a/lds/peer.go +++ b/lds/peer.go @@ -99,10 +99,9 @@ func (p *peer) queueSend(f func()) { // Info gathers and returns a collection of metadata known about a peer. func (p *peer) Info() *dex.PeerInfo { - // TODO: fix this when modify peer return &dex.PeerInfo{ Version: p.version, - Number: 0, + Number: p.Number(), Head: fmt.Sprintf("%x", p.Head()), } } @@ -116,12 +115,12 @@ func (p *peer) Head() (hash common.Hash) { return hash } -func (p *peer) HeadAndTd() (hash common.Hash, td *big.Int) { +func (p *peer) HeadAndNumber() (hash common.Hash, number uint64) { p.lock.RLock() defer p.lock.RUnlock() copy(hash[:], p.headInfo.Hash[:]) - return hash, p.headInfo.Td + return hash, p.headInfo.Number } func (p *peer) headBlockInfo() blockInfo { @@ -132,11 +131,11 @@ func (p *peer) headBlockInfo() blockInfo { } // Td retrieves the current total difficulty of a peer. -func (p *peer) Td() *big.Int { +func (p *peer) Number() uint64 { p.lock.RLock() defer p.lock.RUnlock() - return new(big.Int).Set(p.headInfo.Td) + return p.headInfo.Number } // waitBefore implements distPeer interface @@ -598,12 +597,12 @@ func (ps *peerSet) BestPeer() *peer { defer ps.lock.RUnlock() var ( - bestPeer *peer - bestTd *big.Int + bestPeer *peer + bestNumber uint64 ) for _, p := range ps.peers { - if td := p.Td(); bestPeer == nil || td.Cmp(bestTd) > 0 { - bestPeer, bestTd = p, td + if number := p.Number(); bestPeer == nil || number > bestNumber { + bestPeer, bestNumber = p, number } } return bestPeer diff --git a/lds/sync.go b/lds/sync.go index 7beb06e45..27d14936c 100644 --- a/lds/sync.go +++ b/lds/sync.go @@ -21,7 +21,7 @@ import ( "time" "github.com/dexon-foundation/dexon/core/rawdb" - "github.com/dexon-foundation/dexon/eth/downloader" + "github.com/dexon-foundation/dexon/dex/downloader" "github.com/dexon-foundation/dexon/light" ) @@ -75,5 +75,5 @@ func (pm *ProtocolManager) synchronise(peer *peer) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() pm.blockchain.(*light.LightChain).SyncCht(ctx) - pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync) + pm.downloader.Synchronise(peer.id, peer.Head(), peer.Number(), downloader.LightSync) } |