aboutsummaryrefslogtreecommitdiffstats
path: root/eth/downloader/synchronous.go
blob: 7bb49d24edc4501459ebdec854182dcbf56ca8db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package downloader

import (
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/logger/glog"
)

// THIS IS PENDING AND TO DO CHANGES FOR MAKING THE DOWNLOADER SYNCHRONOUS

// SynchroniseWithPeer will select the peer and use it for synchronising. If an empty string is given
// it will use the best peer possible and synchronise if it's TD is higher than our own. If any of the
// checks fail an error will be returned. This method is synchronous
func (d *Downloader) SynchroniseWithPeer(id string) (types.Blocks, error) {
    // Check if we're busy
    if d.isBusy() {
        return nil, errBusy
    }

    // Attempt to select a peer. This can either be nothing, which returns, best peer
    // or selected peer. If no peer could be found an error will be returned
    var p *peer
    if len(id) == 0 {
        p = d.peers[id]
        if p == nil {
            return nil, errUnknownPeer
        }
    } else {
        p = d.peers.bestPeer()
    }

    // Make sure our td is lower than the peer's td
    if p.td.Cmp(d.currentTd()) <= 0 || d.hasBlock(p.recentHash) {
        return nil, errLowTd
    }

    // Get the hash from the peer and initiate the downloading progress.
    err := d.getFromPeer(p, p.recentHash, false)
    if err != nil {
        return nil, err
    }

    return d.queue.blocks, nil
}

// Synchronise will synchronise using the best peer.
func (d *Downloader) Synchronise() (types.Blocks, error) {
    return d.SynchroniseWithPeer("")
}

func (d *Downloader) getFromPeer(p *peer, hash common.Hash, ignoreInitial bool) error {
    d.activePeer = p.id

    glog.V(logger.Detail).Infoln("Synchronising with the network using:", p.id)
    // Start the fetcher. This will block the update entirely
    // interupts need to be send to the appropriate channels
    // respectively.
    if err := d.startFetchingHashes(p, hash, ignoreInitial); err != nil {
        // handle error
        glog.V(logger.Debug).Infoln("Error fetching hashes:", err)
        // XXX Reset
        return err
    }

    // Start fetching blocks in paralel. The strategy is simple
    // take any available peers, seserve a chunk for each peer available,
    // let the peer deliver the chunkn and periodically check if a peer
    // has timedout. When done downloading, process blocks.
    if err := d.startFetchingBlocks(p); err != nil {
        glog.V(logger.Debug).Infoln("Error downloading blocks:", err)
        // XXX reset
        return err
    }

    glog.V(logger.Detail).Infoln("Sync completed")

    return nil
}