aboutsummaryrefslogtreecommitdiffstats
path: root/eth/downloader/peer.go
blob: 486c09e38b0e8fed9469db0bd0a7cd4740546e17 (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
package downloader

import (
    "math/big"
    "sync"

    "github.com/ethereum/go-ethereum/common"
)

const (
    workingState = 2
    idleState    = 4
)

// peer represents an active peer
type peer struct {
    state int

    mu         sync.RWMutex
    id         string
    td         *big.Int
    recentHash common.Hash

    getHashes hashFetcherFn
    getBlocks blockFetcherFn
}

// create a new peer
func newPeer(id string, td *big.Int, hash common.Hash, getHashes hashFetcherFn, getBlocks blockFetcherFn) *peer {
    return &peer{id: id, td: td, recentHash: hash, getHashes: getHashes, getBlocks: getBlocks, state: idleState}
}

// fetch a chunk using the peer
func (p *peer) fetch(chunk *chunk) {
    p.mu.Lock()
    defer p.mu.Unlock()

    // set working state
    p.state = workingState
    // convert the set to a fetchable slice
    hashes, i := make([]common.Hash, chunk.hashes.Size()), 0
    chunk.hashes.Each(func(v interface{}) bool {
        hashes[i] = v.(common.Hash)
        i++
        return true
    })
    p.getBlocks(hashes)
}