Skip to content

Commit c7f2536

Browse files
authored
les: les/4 minimalistic version (#21909)
* les: allow tx unindexing in les/4 light server mode * les: minor fixes * les: more small fixes * les: add meaningful constants for recentTxIndex handshake field
1 parent 8cde296 commit c7f2536

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

cmd/utils/flags.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,10 +1491,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
14911491
CheckExclusive(ctx, LegacyLightServFlag, LightServeFlag, SyncModeFlag, "light")
14921492
CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer
14931493
CheckExclusive(ctx, GCModeFlag, "archive", TxLookupLimitFlag)
1494-
// todo(rjl493456442) make it available for les server
1495-
// Ancient tx indices pruning is not available for les server now
1496-
// since light client relies on the server for transaction status query.
1497-
CheckExclusive(ctx, LegacyLightServFlag, LightServeFlag, TxLookupLimitFlag)
1494+
if (ctx.GlobalIsSet(LegacyLightServFlag.Name) || ctx.GlobalIsSet(LightServeFlag.Name)) && ctx.GlobalIsSet(TxLookupLimitFlag.Name) {
1495+
log.Warn("LES server cannot serve old transaction status and cannot connect below les/4 protocol version if transaction lookup index is limited")
1496+
}
14981497
var ks *keystore.KeyStore
14991498
if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
15001499
ks = keystores[0].(*keystore.KeyStore)

les/odr_requests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ func (r *TxStatusRequest) GetCost(peer *serverPeer) uint64 {
488488

489489
// CanSend tells if a certain peer is suitable for serving the given request
490490
func (r *TxStatusRequest) CanSend(peer *serverPeer) bool {
491-
return peer.version >= lpv2
491+
return peer.serveTxLookup
492492
}
493493

494494
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)

les/peer.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ type serverPeer struct {
341341
onlyAnnounce bool // The flag whether the server sends announcement only.
342342
chainSince, chainRecent uint64 // The range of chain server peer can serve.
343343
stateSince, stateRecent uint64 // The range of state server peer can serve.
344+
serveTxLookup bool // The server peer can serve tx lookups.
344345

345346
// Advertised checkpoint fields
346347
checkpointNumber uint64 // The block height which the checkpoint is registered.
@@ -628,6 +629,18 @@ func (p *serverPeer) Handshake(genesis common.Hash, forkid forkid.ID, forkFilter
628629
if recv.get("txRelay", nil) != nil {
629630
p.onlyAnnounce = true
630631
}
632+
if p.version >= lpv4 {
633+
var recentTx uint
634+
if err := recv.get("recentTxLookup", &recentTx); err != nil {
635+
return err
636+
}
637+
// Note: in the current version we only consider the tx index service useful
638+
// if it is unlimited. This can be made configurable in the future.
639+
p.serveTxLookup = recentTx == txIndexUnlimited
640+
} else {
641+
p.serveTxLookup = true
642+
}
643+
631644
if p.onlyAnnounce && !p.trusted {
632645
return errResp(ErrUselessPeer, "peer cannot serve requests")
633646
}
@@ -969,6 +982,20 @@ func (p *clientPeer) freezeClient() {
969982
// Handshake executes the les protocol handshake, negotiating version number,
970983
// network IDs, difficulties, head and genesis blocks.
971984
func (p *clientPeer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis common.Hash, forkID forkid.ID, forkFilter forkid.Filter, server *LesServer) error {
985+
recentTx := server.handler.blockchain.TxLookupLimit()
986+
if recentTx != txIndexUnlimited {
987+
if recentTx < blockSafetyMargin {
988+
recentTx = txIndexDisabled
989+
} else {
990+
recentTx -= blockSafetyMargin - txIndexRecentOffset
991+
}
992+
}
993+
if server.config.UltraLightOnlyAnnounce {
994+
recentTx = txIndexDisabled
995+
}
996+
if recentTx != txIndexUnlimited && p.version < lpv4 {
997+
return errors.New("Cannot serve old clients without a complete tx index")
998+
}
972999
// Note: clientPeer.headInfo should contain the last head announced to the client by us.
9731000
// The values announced in the handshake are dummy values for compatibility reasons and should be ignored.
9741001
p.headInfo = blockInfo{Hash: head, Number: headNum, Td: td}
@@ -981,13 +1008,16 @@ func (p *clientPeer) Handshake(td *big.Int, head common.Hash, headNum uint64, ge
9811008

9821009
// If local ethereum node is running in archive mode, advertise ourselves we have
9831010
// all version state data. Otherwise only recent state is available.
984-
stateRecent := uint64(core.TriesInMemory - 4)
1011+
stateRecent := uint64(core.TriesInMemory - blockSafetyMargin)
9851012
if server.archiveMode {
9861013
stateRecent = 0
9871014
}
9881015
*lists = (*lists).add("serveRecentState", stateRecent)
9891016
*lists = (*lists).add("txRelay", nil)
9901017
}
1018+
if p.version >= lpv4 {
1019+
*lists = (*lists).add("recentTxLookup", recentTx)
1020+
}
9911021
*lists = (*lists).add("flowControl/BL", server.defParams.BufLimit)
9921022
*lists = (*lists).add("flowControl/MRR", server.defParams.MinRecharge)
9931023

les/protocol.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ var ProtocolLengths = map[uint]uint64{lpv2: 22, lpv3: 24, lpv4: 24}
5050
const (
5151
NetworkId = 1
5252
ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
53+
blockSafetyMargin = 4 // safety margin applied to block ranges specified relative to head block
54+
55+
txIndexUnlimited = 0 // this value in the "recentTxLookup" handshake field means the entire tx index history is served
56+
txIndexDisabled = 1 // this value means tx index is not served at all
57+
txIndexRecentOffset = 1 // txIndexRecentOffset + N in the handshake field means then tx index of the last N blocks is supported
5358
)
5459

5560
// les protocol message codes

0 commit comments

Comments
 (0)