@@ -87,16 +87,59 @@ class InboundLedgersImp : public InboundLedgers
87
87
reason != InboundLedger::Reason::SHARD ||
88
88
(seq != 0 && app_.getShardStore ()));
89
89
90
+ // If the node is not in "full" state, it needs to sync to the network,
91
+ // and doesn't have the necessary tx's and ledger entries to build the
92
+ // ledger.
90
93
bool const isFull = app_.getOPs ().isFull ();
94
+ // fallingBehind means the last closed ledger is at least 2 behind the
95
+ // validated ledger. If the node is falling behind the network, it
96
+ // probably needs information from the network to catch up.
97
+ //
98
+ // The reason this should not simply be only at least 1 behind the
99
+ // validated ledger is that a slight lag is normal case because some
100
+ // nodes get there slightly later than others. A difference of 2 means
101
+ // that at least a full ledger interval has passed, so the node is
102
+ // beginning to fall behind.
91
103
bool const fallingBehind = app_.getOPs ().isFallingBehind ();
104
+ // If everything else is ok, don't try to acquire the ledger if the
105
+ // requested seq is in the near future relative to the validated ledger.
106
+ // If the requested ledger is between 1 and 19 inclusive ledgers ahead
107
+ // of the valid ledger this node has not built it yet, but it's
108
+ // possible/likely it has the tx's necessary to build it and get caught
109
+ // up. Plus it might not become validated. On the other hand, if it's
110
+ // more than 20 in the future, this node should request it so that it
111
+ // can jump ahead and get caught up.
92
112
LedgerIndex const validSeq =
93
113
app_.getLedgerMaster ().getValidLedgerIndex ();
94
114
constexpr std::size_t lagLeeway = 20 ;
95
115
bool const nearFuture =
96
116
(seq > validSeq) && (seq < validSeq + lagLeeway);
117
+ // If everything else is ok, don't try to acquire the ledger if the
118
+ // request is related to consensus. (Note that consensus calls usually
119
+ // pass a seq of 0, so nearFuture will be false other than on a brand
120
+ // new network.)
97
121
bool const consensus = reason == InboundLedger::Reason::CONSENSUS;
98
- bool const shouldAcquire =
99
- !(isFull && !fallingBehind && (nearFuture || consensus));
122
+
123
+ bool const shouldAcquire = [&]() {
124
+ // If the node is not synced, try to get the ledger.
125
+ if (!isFull)
126
+ return true ;
127
+ // If the node is falling behind, try to get the ledger.
128
+ if (fallingBehind)
129
+ return true ;
130
+ // If the ledger is in the near future, do NOT get the ledger. This
131
+ // node is probably about to build it.
132
+ if (nearFuture)
133
+ return false ;
134
+ // If the request is because of consensus, do NOT get the ledger.
135
+ // This node is probably about to build it.
136
+ if (consensus)
137
+ return false ;
138
+ return true ;
139
+ }();
140
+ assert (
141
+ shouldAcquire ==
142
+ !(isFull && !fallingBehind && (nearFuture || consensus)));
100
143
ss << " Evaluating whether to acquire ledger " << hash
101
144
<< " . full: " << (isFull ? " true" : " false" )
102
145
<< " . falling behind: " << (fallingBehind ? " true" : " false" )
@@ -113,7 +156,7 @@ class InboundLedgersImp : public InboundLedgers
113
156
ScopedLockType sl (mLock );
114
157
if (stopping_)
115
158
{
116
- JLOG (j_.debug ()) << " Abort (stopping): " << ss.str ();
159
+ JLOG (j_.debug ()) << " Abort(stopping): " << ss.str ();
117
160
return {};
118
161
}
119
162
@@ -140,7 +183,7 @@ class InboundLedgersImp : public InboundLedgers
140
183
141
184
if (inbound->isFailed ())
142
185
{
143
- JLOG (j_.debug ()) << " Abort (failed): " << ss.str ();
186
+ JLOG (j_.debug ()) << " Abort(failed): " << ss.str ();
144
187
return {};
145
188
}
146
189
@@ -149,7 +192,7 @@ class InboundLedgersImp : public InboundLedgers
149
192
150
193
if (!inbound->isComplete ())
151
194
{
152
- JLOG (j_.debug ()) << " Abort (incomplete): " << ss.str ();
195
+ JLOG (j_.debug ()) << " Abort(incomplete): " << ss.str ();
153
196
return {};
154
197
}
155
198
@@ -198,7 +241,7 @@ class InboundLedgersImp : public InboundLedgers
198
241
{
199
242
// This check should be before the others because it's cheaper, but
200
243
// it's at the end for now to test the effectiveness of the change
201
- JLOG (j_.debug ()) << " Abort (rule): " << ss.str ();
244
+ JLOG (j_.debug ()) << " Abort(rule): " << ss.str ();
202
245
return {};
203
246
}
204
247
0 commit comments