diff options
author | obscuren <geffobscura@gmail.com> | 2015-04-12 18:38:25 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-04-12 18:38:25 +0800 |
commit | acf8452c33061c200c31529e9ffae0575b859ce5 (patch) | |
tree | b4afd91f6401cb813fb4860db7e6ef29fa3d54ca /eth/downloader/peer.go | |
parent | 61db7a71dd809a003b5c899398b02945c04890c9 (diff) | |
download | dexon-acf8452c33061c200c31529e9ffae0575b859ce5.tar dexon-acf8452c33061c200c31529e9ffae0575b859ce5.tar.gz dexon-acf8452c33061c200c31529e9ffae0575b859ce5.tar.bz2 dexon-acf8452c33061c200c31529e9ffae0575b859ce5.tar.lz dexon-acf8452c33061c200c31529e9ffae0575b859ce5.tar.xz dexon-acf8452c33061c200c31529e9ffae0575b859ce5.tar.zst dexon-acf8452c33061c200c31529e9ffae0575b859ce5.zip |
downloader: implemented new downloader
Diffstat (limited to 'eth/downloader/peer.go')
-rw-r--r-- | eth/downloader/peer.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/eth/downloader/peer.go b/eth/downloader/peer.go new file mode 100644 index 000000000..486c09e38 --- /dev/null +++ b/eth/downloader/peer.go @@ -0,0 +1,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) +} |