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

import (
    "testing"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "gopkg.in/fatih/set.v0"
)

func createHashSet(hashes []common.Hash) *set.Set {
    hset := set.New()

    for _, hash := range hashes {
        hset.Add(hash)
    }

    return hset
}

func createBlocksFromHashSet(hashes *set.Set) []*types.Block {
    blocks := make([]*types.Block, hashes.Size())

    var i int
    hashes.Each(func(v interface{}) bool {
        blocks[i] = createBlock(i, common.Hash{}, v.(common.Hash))
        i++
        return true
    })

    return blocks
}

func TestChunking(t *testing.T) {
    queue := newQueue()
    peer1 := newPeer("peer1", common.Hash{}, nil, nil)
    peer2 := newPeer("peer2", common.Hash{}, nil, nil)

    // 99 + 1 (1 == known genesis hash)
    hashes := createHashes(0, 99)
    queue.Insert(hashes)

    chunk1 := queue.Reserve(peer1, 99)
    if chunk1 == nil {
        t.Errorf("chunk1 is nil")
        t.FailNow()
    }
    chunk2 := queue.Reserve(peer2, 99)
    if chunk2 == nil {
        t.Errorf("chunk2 is nil")
        t.FailNow()
    }

    if len(chunk1.Hashes) != 99 {
        t.Error("expected chunk1 hashes to be 99, got", len(chunk1.Hashes))
    }

    if len(chunk2.Hashes) != 1 {
        t.Error("expected chunk1 hashes to be 1, got", len(chunk2.Hashes))
    }
}