Skip to content

Commit

Permalink
Remove LES v5 protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Jun 27, 2023
1 parent 42c2236 commit d8b3680
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 281 deletions.
23 changes: 0 additions & 23 deletions les/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ func TestSequentialAnnouncementsLes3(t *testing.T) {
func TestSequentialAnnouncementsUltraLes3(t *testing.T) {
testSequentialAnnouncements(t, downloader.LightestSync, lpv3)
}
func TestSequentialAnnouncementsLes5(t *testing.T) {
testSequentialAnnouncements(t, downloader.LightSync, lpv5)
}
func TestSequentialAnnouncementsUltraLes5(t *testing.T) {
testSequentialAnnouncements(t, downloader.LightestSync, lpv5)
}

func testSequentialAnnouncements(t *testing.T, syncMode downloader.SyncMode, protocol int) {
netconfig := testnetConfig{
Expand Down Expand Up @@ -132,13 +126,6 @@ func TestGappedAnnouncementsUltraLes3(t *testing.T) {
t.Skip("added in the les refactor, check if necessary for LightestSync")
testGappedAnnouncements(t, downloader.LightestSync, lpv3)
}
func TestGappedAnnouncementsLes5(t *testing.T) {
testGappedAnnouncements(t, downloader.LightSync, lpv5)
}
func TestGappedAnnouncementsUltraLes5(t *testing.T) {
t.Skip("added in the les refactor, check if necessary for LightestSync")
testGappedAnnouncements(t, downloader.LightestSync, lpv5)
}

func testGappedAnnouncements(t *testing.T, syncMode downloader.SyncMode, protocol int) {
// log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
Expand Down Expand Up @@ -203,15 +190,6 @@ func TestTrustedAnnouncementsUltraLes3(t *testing.T) {
testTrustedAnnouncement(t, downloader.LightestSync, lpv3)
}

func TestTrustedAnnouncementsLes5(t *testing.T) {
testTrustedAnnouncement(t, downloader.LightSync, lpv5)
}

func TestTrustedAnnouncementsUltraLes5(t *testing.T) {
t.Skip("added in the les refactor, check if necessary for LightestSync")
testTrustedAnnouncement(t, downloader.LightestSync, lpv5)
}

func testTrustedAnnouncement(t *testing.T, syncMode downloader.SyncMode, protocol int) {
var (
servers []*testServer
Expand Down Expand Up @@ -287,7 +265,6 @@ func testTrustedAnnouncement(t *testing.T, syncMode downloader.SyncMode, protoco

func TestInvalidAnnouncesLES2(t *testing.T) { testInvalidAnnounces(t, downloader.LightSync, lpv2) }
func TestInvalidAnnouncesLES3(t *testing.T) { testInvalidAnnounces(t, downloader.LightSync, lpv3) }
func TestInvalidAnnouncesLES5(t *testing.T) { testInvalidAnnounces(t, downloader.LightSync, lpv5) }

func testInvalidAnnounces(t *testing.T, syncMode downloader.SyncMode, protocol int) {
t.Skip("Validates through a smaller td for the same header number. We verify this using the header number")
Expand Down
5 changes: 0 additions & 5 deletions les/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,6 @@ func testGetBloombitsProofs(t *testing.T, protocol int) {

func TestTransactionStatusLes2(t *testing.T) { testTransactionStatus(t, lpv2) }
func TestTransactionStatusLes3(t *testing.T) { testTransactionStatus(t, lpv3) }
func TestTransactionStatusLes5(t *testing.T) { testTransactionStatus(t, lpv5) }

func testTransactionStatus(t *testing.T, protocol int) {
netconfig := testnetConfig{
Expand Down Expand Up @@ -692,7 +691,6 @@ func testTransactionStatus(t *testing.T, protocol int) {
}

func TestStopResumeLES3(t *testing.T) { testStopResume(t, lpv3) }
func TestStopResumeLES5(t *testing.T) { testStopResume(t, lpv5) }

func testStopResume(t *testing.T, protocol int) {
netconfig := testnetConfig{
Expand Down Expand Up @@ -755,9 +753,6 @@ func TestTransactionGatewayFeeRequirementLes2(t *testing.T) {
func TestTransactionGatewayFeeRequirementLes3(t *testing.T) {
testTransactionGatewayFeeRequirement(t, lpv3)
}
func TestTransactionGatewayFeeRequirementLes5(t *testing.T) {
testTransactionGatewayFeeRequirement(t, lpv5)
}

func testTransactionGatewayFeeRequirement(t *testing.T, protocol int) {
netconfig := testnetConfig{
Expand Down
201 changes: 0 additions & 201 deletions les/odr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ package les
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"math/big"
"reflect"
"testing"
"time"

Expand All @@ -32,7 +29,6 @@ import (
"github.com/celo-org/celo-blockchain/core"
"github.com/celo-org/celo-blockchain/core/rawdb"
"github.com/celo-org/celo-blockchain/core/state"
"github.com/celo-org/celo-blockchain/p2p"

"github.com/celo-org/celo-blockchain/core/types"
"github.com/celo-org/celo-blockchain/core/vm"
Expand Down Expand Up @@ -260,200 +256,3 @@ func testOdr(t *testing.T, protocol int, expFail uint64, checkCached bool, fn od
test(5)
}
}

func TestGetTxStatusFromUnindexedPeersLES5(t *testing.T) { testGetTxStatusFromUnindexedPeers(t, lpv5) }

func testGetTxStatusFromUnindexedPeers(t *testing.T, protocol int) {
var (
blocks = 8
netconfig = testnetConfig{
blocks: blocks,
syncMode: downloader.LightSync,
protocol: protocol,
nopruning: true,
}
)
server, client, tearDown := newClientServerEnv(t, netconfig)
defer tearDown()

// Iterate the chain, create the tx indexes locally
var (
testHash common.Hash
testStatus light.TxStatus

txs = make(map[common.Hash]*types.Transaction) // Transaction objects set
blockNumbers = make(map[common.Hash]uint64) // Transaction hash to block number mappings
blockHashes = make(map[common.Hash]common.Hash) // Transaction hash to block hash mappings
intraIndex = make(map[common.Hash]uint64) // Transaction intra-index in block
)
for number := uint64(1); number < server.backend.Blockchain().CurrentBlock().NumberU64(); number++ {
block := server.backend.Blockchain().GetBlockByNumber(number)
if block == nil {
t.Fatalf("Failed to retrieve block %d", number)
}
for index, tx := range block.Transactions() {
txs[tx.Hash()] = tx
blockNumbers[tx.Hash()] = number
blockHashes[tx.Hash()] = block.Hash()
intraIndex[tx.Hash()] = uint64(index)

if testHash == (common.Hash{}) {
testHash = tx.Hash()
testStatus = light.TxStatus{
Status: core.TxStatusIncluded,
Lookup: &rawdb.LegacyTxLookupEntry{
BlockHash: block.Hash(),
BlockIndex: block.NumberU64(),
Index: uint64(index),
},
}
}
}
}
// serveMsg processes incoming GetTxStatusMsg and sends the response back.
serveMsg := func(peer *testPeer, txLookup uint64) error {
var (
msg p2p.Msg
err error
)
loop:
for {
msg, err = peer.app.ReadMsg()
if err != nil {
return err
}
switch msg.Code {
case GetEtherbaseMsg:
continue
default:
break loop
}
}
if msg.Code != GetTxStatusMsg {
return fmt.Errorf("message code mismatch: got %d, expected %d", msg.Code, GetTxStatusMsg)
}
var r GetTxStatusPacket
if err := msg.Decode(&r); err != nil {
return err
}
stats := make([]light.TxStatus, len(r.Hashes))
for i, hash := range r.Hashes {
number, exist := blockNumbers[hash]
if !exist {
continue // Filter out unknown transactions
}
min := uint64(blocks) - txLookup
if txLookup != txIndexUnlimited && (txLookup == txIndexDisabled || number < min) {
continue // Filter out unindexed transactions
}
stats[i].Status = core.TxStatusIncluded
stats[i].Lookup = &rawdb.LegacyTxLookupEntry{
BlockHash: blockHashes[hash],
BlockIndex: number,
Index: intraIndex[hash],
}
}
data, _ := rlp.EncodeToBytes(stats)
reply := &reply{peer.app, TxStatusMsg, r.ReqID, data}
if err = reply.send(testBufLimit); err != nil {
return err
}
return nil
}

var testspecs = []struct {
peers int
txLookups []uint64
txs []common.Hash
results []light.TxStatus
}{
// Retrieve mined transaction from the empty peerset
{
peers: 0,
txLookups: []uint64{},
txs: []common.Hash{testHash},
results: []light.TxStatus{{}},
},
// Retrieve unknown transaction from the full peers
{
peers: 3,
txLookups: []uint64{txIndexUnlimited, txIndexUnlimited, txIndexUnlimited},
txs: []common.Hash{randomHash()},
results: []light.TxStatus{{}},
},
// Retrieve mined transaction from the full peers
{
peers: 3,
txLookups: []uint64{txIndexUnlimited, txIndexUnlimited, txIndexUnlimited},
txs: []common.Hash{testHash},
results: []light.TxStatus{testStatus},
},
// Retrieve mixed transactions from the full peers
{
peers: 3,
txLookups: []uint64{txIndexUnlimited, txIndexUnlimited, txIndexUnlimited},
txs: []common.Hash{randomHash(), testHash},
results: []light.TxStatus{{}, testStatus},
},
// Retrieve mixed transactions from unindexed peer(but the target is still available)
{
peers: 3,
txLookups: []uint64{uint64(blocks) - testStatus.Lookup.BlockIndex, uint64(blocks) - testStatus.Lookup.BlockIndex - 1, uint64(blocks) - testStatus.Lookup.BlockIndex - 2},
txs: []common.Hash{randomHash(), testHash},
results: []light.TxStatus{{}, testStatus},
},
// Retrieve mixed transactions from unindexed peer(but the target is not available)
{
peers: 3,
txLookups: []uint64{uint64(blocks) - testStatus.Lookup.BlockIndex - 1, uint64(blocks) - testStatus.Lookup.BlockIndex - 1, uint64(blocks) - testStatus.Lookup.BlockIndex - 2},
txs: []common.Hash{randomHash(), testHash},
results: []light.TxStatus{{}, {}},
},
}
for _, testspec := range testspecs {
// Create a bunch of server peers with different tx history
var (
serverPeers []*testPeer
closeFns []func()
)
for i := 0; i < testspec.peers; i++ {
peer, closePeer, _ := client.newRawPeer(t, fmt.Sprintf("server-%d", i), protocol, testspec.txLookups[i])
serverPeers = append(serverPeers, peer)
closeFns = append(closeFns, closePeer)

// Create a one-time routine for serving message
go func(i int, peer *testPeer, lookup uint64) {
serveMsg(peer, lookup)
}(i, peer, testspec.txLookups[i])
}

// Send out the GetTxStatus requests, compare the result with
// expected value.
r := &light.TxStatusRequest{Hashes: testspec.txs}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

err := client.handler.backend.odr.RetrieveTxStatus(ctx, r)
if err != nil {
t.Errorf("Failed to retrieve tx status %v", err)
} else {
if !reflect.DeepEqual(testspec.results, r.Status) {
t.Errorf("Result mismatch, diff")
}
}

// Close all connected peers and start the next round
for _, closeFn := range closeFns {
closeFn()
}
}
}

// randomHash generates a random blob of data and returns it as a hash.
func randomHash() common.Hash {
var hash common.Hash
if n, err := rand.Read(hash[:]); n != common.HashLength || err != nil {
panic(err)
}
return hash
}
39 changes: 7 additions & 32 deletions les/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,6 @@ func (p *peerCommons) handshake(td *big.Int, head common.Hash, headNum uint64, g
send = send.add("headNum", headNum)
send = send.add("genesisHash", genesis)

// If the protocol version is beyond les4, then pass the forkID
// as well. Check http://eips.ethereum.org/EIPS/eip-2124 for more
// spec detail.
if p.version >= lpv5 {
send = send.add("forkID", forkID)
}
// Add client-specified or server-specified fields
if sendCallback != nil {
sendCallback(&send)
Expand Down Expand Up @@ -311,16 +305,6 @@ func (p *peerCommons) handshake(td *big.Int, head common.Hash, headNum uint64, g
if int(rVersion) != p.version {
return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", rVersion, p.version)
}
// Check forkID if the protocol version is beyond the les4
if p.version >= lpv5 {
var forkID forkid.ID
if err := recv.get("forkID", &forkID); err != nil {
return err
}
if err := forkFilter(forkID); err != nil {
return errResp(ErrForkIDRejected, "%v", err)
}
}
if recvCallback != nil {
return recvCallback(recv)
}
Expand Down Expand Up @@ -715,18 +699,12 @@ func (p *serverPeer) Handshake(genesis common.Hash, forkid forkid.ID, forkFilter
if recv.get("txRelay", nil) != nil {
p.onlyAnnounce = true
}
if p.version >= lpv5 {
var recentTx uint
if err := recv.get("recentTxLookup", &recentTx); err != nil {
return err
}
p.txHistory = uint64(recentTx)
} else {
// The weak assumption is held here that legacy les server(les2,3)
// has unlimited transaction history. The les serving in these legacy
// versions is disabled if the transaction is unindexed.
p.txHistory = txIndexUnlimited
}

// The weak assumption is held here that legacy les server(les2,3)
// has unlimited transaction history. The les serving in these legacy
// versions is disabled if the transaction is unindexed.
p.txHistory = txIndexUnlimited

if p.onlyAnnounce && !p.trusted {
return errResp(ErrUselessPeer, "peer cannot serve requests")
}
Expand Down Expand Up @@ -1139,7 +1117,7 @@ func (p *clientPeer) Handshake(td *big.Int, head common.Hash, headNum uint64, ge
if server.config.UltraLightOnlyAnnounce {
recentTx = txIndexDisabled
}
if recentTx != txIndexUnlimited && p.version < lpv5 {
if recentTx != txIndexUnlimited {
return errors.New("Cannot serve old clients without a complete tx index")
}
// Note: clientPeer.headInfo should contain the last head announced to the client by us.
Expand All @@ -1161,9 +1139,6 @@ func (p *clientPeer) Handshake(td *big.Int, head common.Hash, headNum uint64, ge
*lists = (*lists).add("serveRecentState", stateRecent)
*lists = (*lists).add("txRelay", nil)
}
if p.version >= lpv5 {
*lists = (*lists).add("recentTxLookup", recentTx)
}
*lists = (*lists).add("flowControl/BL", server.defParams.BufLimit)
*lists = (*lists).add("flowControl/MRR", server.defParams.MinRecharge)

Expand Down
7 changes: 3 additions & 4 deletions les/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ import (
const (
lpv2 = 2
lpv3 = 3
lpv5 = 5 // eth lpv4
)

// Supported versions of the les protocol (first is primary)
var (
ClientProtocolVersions = []uint{lpv2, lpv3, lpv5}
ServerProtocolVersions = []uint{lpv2, lpv3, lpv5}
ClientProtocolVersions = []uint{lpv2, lpv3}
ServerProtocolVersions = []uint{lpv2, lpv3}
AdvertiseProtocolVersions = []uint{lpv2} // clients are searching for the first advertised protocol in the list
)

// Number of implemented message corresponding to different protocol versions.
var ProtocolLengths = map[uint]uint64{lpv2: 24, lpv3: 26, lpv5: 28}
var ProtocolLengths = map[uint]uint64{lpv2: 24, lpv3: 26}

const (
NetworkId = 1
Expand Down
Loading

0 comments on commit d8b3680

Please sign in to comment.