aboutsummaryrefslogblamecommitdiffstats
path: root/les/request_test.go
blob: c99ebab53d449a258b015952b2296eae84883faa (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                                  


           
                 


                 




                                                      











                                                                                            

                                                                          





                                                                                          

                                                                                





                                                                                             

                                                                                  
                                                                                              



                                                                                                                                  



                                                                        

                                                                        





                                                                                      
                                                          


                                        
                                                                                                




                                                                                               


                                                                                 

                                      


                                                                                                    

                                                                                                              
                                                                       












                                                                                       
                                                
                                                                                                 

                                                                              
 
                                           
                                                                                                 
                                
                                                                                    
                                  

                                        
 
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package les

import (
    "context"
    "testing"
    "time"

    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/core/rawdb"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/ethdb"
    "github.com/dexon-foundation/dexon/light"
)

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 TestBlockAccessLes2(t *testing.T) { testAccess(t, 2, 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 TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, 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 TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) }

func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
        return &light.TrieRequest{Id: light.StateTrieID(rawdb.ReadHeader(db, bhash, *number)), Key: testBankSecureTrieKey}
    }
    return nil
}

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

func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) }

func tfCodeAccess(db ethdb.Database, bhash common.Hash, num uint64) light.OdrRequest {
    number := rawdb.ReadHeaderNumber(db, bhash)
    if number != nil {
        return nil
    }
    header := rawdb.ReadHeader(db, bhash, *number)
    if header.Number.Uint64() < testContractDeployed {
        return nil
    }
    sti := light.StateTrieID(header)
    ci := light.StorageTrieID(sti, crypto.Keccak256Hash(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
    server, client, tearDown := newClientServerEnv(t, 4, protocol, nil, true)
    defer tearDown()
    client.pm.synchronise(client.rPeer)

    test := func(expFail uint64) {
        for i := uint64(0); i <= server.pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
            bhash := rawdb.ReadCanonicalHash(server.db, i)
            if req := fn(client.db, bhash, i); req != nil {
                ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
                defer cancel()
                err := client.pm.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
    client.peers.Unregister(client.rPeer.id)
    time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
    // expect retrievals to fail (except genesis block) without a les peer
    test(0)

    client.peers.Register(client.rPeer)
    time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
    client.rPeer.lock.Lock()
    client.rPeer.hasBlock = func(common.Hash, uint64, bool) bool { return true }
    client.rPeer.lock.Unlock()
    // expect all retrievals to pass
    test(5)
}