Skip to content

Commit 68a24ca

Browse files
committed
feat: remoteDb only delay syncs for specified peers
change logic
1 parent 128eb98 commit 68a24ca

5 files changed

Lines changed: 30 additions & 13 deletions

File tree

cmd/utils/flags.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,13 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
19941994
if err != nil {
19951995
Fatalf("%v", err)
19961996
}
1997+
seCfg.AllowedPeerList = make([]string, 0)
1998+
for _, seNode := range stack.Config().P2P.StateExpiryAllowedNodes {
1999+
seCfg.AllowedPeerList = append(seCfg.AllowedPeerList, seNode.ID().String())
2000+
}
2001+
if len(seCfg.AllowedPeerList) == 0 && seCfg.EnableRemoteMode {
2002+
log.Warn("State expiry remote mode is enabled but no allowed peers are specified.")
2003+
}
19972004
cfg.StateExpiryCfg = seCfg
19982005
chaindb.Close()
19992006

core/types/state_expiry.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type StateExpiryConfig struct {
2323
StateEpoch2Block uint64
2424
StateEpochPeriod uint64
2525
EnableLocalRevive bool
26-
EnableRemoteMode bool `rlp:"optional"` // when enable remoteDB mode, it will register specific RPC for partial proof and keep sync behind for safety proof
26+
EnableRemoteMode bool `rlp:"optional"` // when enable remoteDB mode, it will register specific RPC for partial proof and keep sync behind for safety proof
27+
AllowedPeerList []string `rlp:"optional"` // when enable remoteDB mode, it will only delay its sync for the peers in the list
2728
}
2829

2930
// EnableExpiry when enable remote mode, it just check param
@@ -75,7 +76,7 @@ func (s *StateExpiryConfig) CheckCompatible(newCfg *StateExpiryConfig) error {
7576
return errors.New("disable state expiry is dangerous after enabled, expired state may pruned")
7677
}
7778
if s.EnableRemoteMode && !newCfg.EnableRemoteMode {
78-
return errors.New("disable state expiry EnableRemoteMode is dangerous after enabled")
79+
return errors.New("disable state expiry EnableRemoteMode is dangerous after enabled")
7980
}
8081

8182
if err := s.CheckStateEpochCompatible(newCfg.StateEpoch1Block, newCfg.StateEpoch2Block, newCfg.StateEpochPeriod); err != nil {
@@ -115,21 +116,25 @@ func (s *StateExpiryConfig) String() string {
115116
if s.Enable && s.EnableRemoteMode {
116117
return "State Expiry Enable in RemoteMode, it will not expired any state."
117118
}
118-
return fmt.Sprintf("Enable State Expiry, RemoteEndpoint: %v, StateEpoch: [%v|%v|%v], StateScheme: %v, PruneLevel: %v, EnableLocalRevive: %v.",
119-
s.FullStateEndpoint, s.StateEpoch1Block, s.StateEpoch2Block, s.StateEpochPeriod, s.StateScheme, s.PruneLevel, s.EnableLocalRevive)
119+
return fmt.Sprintf("Enable State Expiry, RemoteEndpoint: %v, StateEpoch: [%v|%v|%v], StateScheme: %v, PruneLevel: %v, EnableLocalRevive: %v, AllowedPeerList: %v.",
120+
s.FullStateEndpoint, s.StateEpoch1Block, s.StateEpoch2Block, s.StateEpochPeriod, s.StateScheme, s.PruneLevel, s.EnableLocalRevive, s.AllowedPeerList)
120121
}
121122

122123
// ShouldKeep1EpochBehind when enable state expiry, keep remoteDB behind the latest only 1 epoch blocks
123-
func (s *StateExpiryConfig) ShouldKeep1EpochBehind(remote uint64, local uint64) (bool, uint64) {
124-
if !s.EnableRemoteMode {
125-
return false, remote
126-
}
127-
if remote <= local {
124+
func (s *StateExpiryConfig) ShouldKeep1EpochBehind(remote uint64, local uint64, peerId string) (bool, uint64) {
125+
126+
if !s.EnableRemoteMode || remote <= local || remote < s.StateEpoch1Block {
128127
return false, remote
129128
}
130129

131-
// if in epoch0, just sync
132-
if remote < s.StateEpoch1Block {
130+
allowed := false
131+
for _, allowPeer := range s.AllowedPeerList {
132+
if allowPeer == peerId {
133+
allowed = true
134+
break
135+
}
136+
}
137+
if !allowed {
133138
return false, remote
134139
}
135140

@@ -145,5 +150,6 @@ func (s *StateExpiryConfig) ShouldKeep1EpochBehind(remote uint64, local uint64)
145150
if remote-s.StateEpochPeriod <= local {
146151
return true, 0
147152
}
153+
148154
return false, remote - s.StateEpochPeriod
149155
}

eth/downloader/downloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td, ttd *
527527

528528
if d.expiryConfig.EnableRemote() {
529529
var keep bool
530-
keep, remoteHeight = d.expiryConfig.ShouldKeep1EpochBehind(remoteHeight, localHeight)
530+
keep, remoteHeight = d.expiryConfig.ShouldKeep1EpochBehind(remoteHeight, localHeight, p.id)
531531
log.Debug("EnableRemote wait remote more blocks", "remoteHeight", remoteHeader.Number, "request", remoteHeight, "localHeight", localHeight, "keep", keep, "config", d.expiryConfig)
532532
if keep {
533533
return errCanceled

eth/fetcher/block_fetcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func (f *BlockFetcher) loop() {
388388
}
389389

390390
if f.expiryConfig.EnableRemote() {
391-
if keep, _ := f.expiryConfig.ShouldKeep1EpochBehind(number, height); keep {
391+
if keep, _ := f.expiryConfig.ShouldKeep1EpochBehind(number, height, op.origin); keep {
392392
log.Debug("BlockFetcher EnableRemote wait remote more blocks", "remoteHeight", number, "localHeight", height, "config", f.expiryConfig)
393393
break
394394
}

p2p/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ type Config struct {
134134
// allowed to connect, even above the peer limit.
135135
TrustedNodes []*enode.Node
136136

137+
// StateExpiryAllowedNodes are used to ensure that the state expiry remoteDb
138+
// will only delay its sync for the peers in the list.
139+
StateExpiryAllowedNodes []*enode.Node
140+
137141
// Connectivity can be restricted to certain IP networks.
138142
// If this option is set to a non-nil value, only hosts which match one of the
139143
// IP networks contained in the list are considered.

0 commit comments

Comments
 (0)