aboutsummaryrefslogtreecommitdiffstats
path: root/les/request_test.go
blob: df02afb32daa423777a732acbabd1f6a124598cd (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package les

import (
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/ethdb"
    "github.com/ethereum/go-ethereum/light"
    "golang.org/x/net/context"
)

var testBankSecureTrieKey = secAddr(testBankAddress)

func secAddr(addr common.Address) []byte {
    return crypto.Keccak256(addr[:])
}

type accessTestFn func(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest

func TestBlockAccessLes1(t *testing.T) { testAccess(t, 1, tfBlockAccess) }

func tfBlockAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    return &light.BlockRequest{Hash: bhash, Number: number}
}

func TestReceiptsAccessLes1(t *testing.T) { testAccess(t, 1, tfReceiptsAccess) }

func tfReceiptsAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    return &light.ReceiptsRequest{Hash: bhash, Number: number}
}

func TestTrieEntryAccessLes1(t *testing.T) { testAccess(t, 1, tfTrieEntryAccess) }

func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    return &light.TrieRequest{Id: light.StateTrieID(core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash))), Key: testBankSecureTrieKey}
}

func TestCodeAccessLes1(t *testing.T) { testAccess(t, 1, tfCodeAccess) }

func tfCodeAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    header := core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash))
    if header.GetNumberU64() < testContractDeployed {
        return nil
    }
    sti := light.StateTrieID(header)
    ci := light.StorageTrieID(sti, testContractAddr, common.Hash{})
    return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)}
}

func testAccess(t *testing.T, protocol int, fn accessTestFn) {
    // Assemble the test environment
    pm, db, _ := newTestProtocolManagerMust(t, false, 4, testChainGen)
    lpm, ldb, odr := newTestProtocolManagerMust(t, true, 0, nil)
    _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm)
    select {
    case <-time.After(time.Millisecond * 100):
    case err := <-err1:
        t.Fatalf("peer 1 handshake error: %v", err)
    case err := <-err2:
        t.Fatalf("peer 1 handshake error: %v", err)
    }

    lpm.synchronise(lpeer)

    test := func(expFail uint64) {
        for i := uint64(0); i <= pm.blockchain.CurrentHeader().GetNumberU64(); i++ {
            bhash := core.GetCanonicalHash(db, i)
            if req := fn(ldb, bhash, i); req != nil {
                ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
                err := odr.Retrieve(ctx, req)
                got := err == nil
                exp := i < expFail
                if exp && !got {
                    t.Errorf("object retrieval failed")
                }
                if !exp && got {
                    t.Errorf("unexpected object retrieval success")
                }
            }
        }
    }

    // temporarily remove peer to test odr fails
    odr.UnregisterPeer(lpeer)
    // expect retrievals to fail (except genesis block) without a les peer
    test(0)
    odr.RegisterPeer(lpeer)
    // expect all retrievals to pass
    test(5)
    odr.UnregisterPeer(lpeer)
}