Skip to content

Commit

Permalink
Merge pull request ethereum#57 from status-im/ulc/issue-55/fix-announce
Browse files Browse the repository at this point in the history
les: fix announce receiving
  • Loading branch information
b00ris authored May 31, 2018
2 parents 7e96d84 + 914c43c commit ab009b1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
12 changes: 6 additions & 6 deletions les/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type peer struct {
version int // Protocol version negotiated
network uint64 // Network ID being on

announceType, requestAnnounceType uint64
requestAnnounceType uint64

id string

Expand Down Expand Up @@ -423,11 +423,11 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
p.fcCosts = list.decode()
} else {
//on client node
p.announceType = announceTypeSimple
p.requestAnnounceType = announceTypeSimple
if p.isTrusted {
p.announceType = uint64(announceTypeSigned)
p.requestAnnounceType = announceTypeSigned
}
send = send.add("announceType", p.announceType)
send = send.add("announceType", p.requestAnnounceType)
}

recvList, err := p.sendReceiveHandshake(send)
Expand Down Expand Up @@ -475,9 +475,9 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
/*if recv.get("serveStateSince", nil) == nil {
return errResp(ErrUselessPeer, "wanted client, got server")
}*/
if recv.get("announceType", &p.announceType) != nil {
if recv.get("announceType", &p.requestAnnounceType) != nil {
//set default announceType on server side
p.announceType = announceTypeSimple
p.requestAnnounceType = announceTypeSimple
}
p.fcClient = flowcontrol.NewClientNode(server.fcManager, server.defParams)
} else {
Expand Down
4 changes: 2 additions & 2 deletions les/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestPeerHandshakeSetAnnounceTypeToAnnounceTypeSignedForTrustedPeer(t *testi
t.Fatalf("Handshake error: %s", err)
}

if p.announceType != announceTypeSigned {
if p.requestAnnounceType != announceTypeSigned {
t.Fatal("Incorrect announceType")
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestPeerHandshakeAnnounceTypeSignedForTrustedPeersPeerNotInTrusted(t *testi
if err != nil {
t.Fatal(err)
}
if p.announceType == announceTypeSigned {
if p.requestAnnounceType == announceTypeSigned {
t.Fatal("Incorrect announceType")
}
}
Expand Down
2 changes: 1 addition & 1 deletion les/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (pm *ProtocolManager) blockLoop() {
)

for _, p := range peers {
switch p.announceType {
switch p.requestAnnounceType {

case announceTypeSimple:
select {
Expand Down
47 changes: 46 additions & 1 deletion les/ulc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/light"
Expand Down Expand Up @@ -36,11 +37,55 @@ func TestULCSyncWithOnePeer(t *testing.T) {
l.PM.synchronise(fPeer)

if !reflect.DeepEqual(f.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
t.Fatal("sync don't work")
t.Fatal("sync doesn't work")
}
}

func TestULCReceiveAnnounce(t *testing.T) {
f := newFullPeerPair(t, 1, 4, testChainGen)
ulcConfig := &eth.ULCConfig{
MinTrustedFraction: 100,
TrustedNodes: []string{f.ID.String()},
}

key, err := crypto.GenerateKey()
ID := discover.PubkeyID(&key.PublicKey)
l := newLightPeer(t, ulcConfig)
l.ID = ID

fPeer, lPeer, err := connectPeers(f, l, 2)
if err != nil {
t.Fatal(err)
}

l.PM.synchronise(fPeer)

//check that the sync is finished correctly
if !reflect.DeepEqual(f.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
t.Fatal("sync doesn't work")
}

l.PM.peers.lock.Lock()
if len(l.PM.peers.peers) == 0 {
t.Fatal("peer list should not be empty")
}
l.PM.peers.lock.Unlock()

//send a signed announce message(payload doesn't matter)
announce := announceData{}
announce.sign(key)
lPeer.SendAnnounce(announce)

l.PM.peers.lock.Lock()
if len(l.PM.peers.peers) == 0 {
t.Fatal("peer list after receiving message should not be empty")
}
l.PM.peers.lock.Unlock()
}

//TODO(b00ris) it's failing test. it should be fixed by https://github.com/status-im/go-ethereum/issues/51
func TestULCShouldNotSyncWithTwoPeersOneHaveEmptyChain(t *testing.T) {
t.Skip()
f1 := newFullPeerPair(t, 1, 4, testChainGen)
f2 := newFullPeerPair(t, 3, 0, nil)
ulcConf := &ulc{minTrustedFraction: 100, trustedKeys: make(map[string]struct{})}
Expand Down

0 comments on commit ab009b1

Please sign in to comment.