mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-13 11:36:15 +01:00
Merge a127540bde
into 1090649211
This commit is contained in:
commit
81fdd042cd
1 changed files with 30 additions and 18 deletions
|
@ -56,9 +56,15 @@ public class FilterMerger {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Result {
|
public static class Result {
|
||||||
public BloomFilter filter;
|
public final BloomFilter filter;
|
||||||
public Instant earliestKeyTime;
|
public final Instant earliestKeyTime;
|
||||||
public boolean changed;
|
public final boolean changed;
|
||||||
|
|
||||||
|
public Result(BloomFilter filter, Instant earliestKeyTime, boolean changed) {
|
||||||
|
this.filter = filter;
|
||||||
|
this.earliestKeyTime = earliestKeyTime;
|
||||||
|
this.changed = changed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result calculate(List<PeerFilterProvider> providerList) {
|
public Result calculate(List<PeerFilterProvider> providerList) {
|
||||||
|
@ -73,13 +79,20 @@ public class FilterMerger {
|
||||||
provider.beginBloomFilterCalculation();
|
provider.beginBloomFilterCalculation();
|
||||||
begunProviders.add(provider);
|
begunProviders.add(provider);
|
||||||
}
|
}
|
||||||
Result result = new Result();
|
BloomFilter filter;
|
||||||
result.earliestKeyTime = Instant.MAX;
|
boolean changed;
|
||||||
int elements = 0;
|
// We adjust the earliest key time backwards by a week to handle the case of clock drift. This can occur
|
||||||
for (PeerFilterProvider p : providers) {
|
// both in block header timestamps and if the users clock was out of sync when the key was first created
|
||||||
result.earliestKeyTime = TimeUtils.earlier(result.earliestKeyTime, p.earliestKeyCreationTime());
|
// (to within a small amount of tolerance).
|
||||||
elements += p.getBloomFilterElementCount();
|
Instant earliestKeyTime = providers.stream()
|
||||||
}
|
.map(PeerFilterProvider::earliestKeyCreationTime)
|
||||||
|
.min(Instant::compareTo)
|
||||||
|
.map(t -> t.minus(7, ChronoUnit.DAYS))
|
||||||
|
.orElse(Instant.MAX);
|
||||||
|
|
||||||
|
int elements = providers.stream()
|
||||||
|
.mapToInt(PeerFilterProvider::getBloomFilterElementCount)
|
||||||
|
.sum();
|
||||||
|
|
||||||
if (elements > 0) {
|
if (elements > 0) {
|
||||||
// We stair-step our element count so that we avoid creating a filter with different parameters
|
// We stair-step our element count so that we avoid creating a filter with different parameters
|
||||||
|
@ -89,19 +102,18 @@ public class FilterMerger {
|
||||||
lastBloomFilterElementCount = elements > lastBloomFilterElementCount ? elements + 100 : lastBloomFilterElementCount;
|
lastBloomFilterElementCount = elements > lastBloomFilterElementCount ? elements + 100 : lastBloomFilterElementCount;
|
||||||
double fpRate = vBloomFilterFPRate;
|
double fpRate = vBloomFilterFPRate;
|
||||||
// We now always use UPDATE_ALL because with segwit there is hardly any wallet that can do without.
|
// We now always use UPDATE_ALL because with segwit there is hardly any wallet that can do without.
|
||||||
BloomFilter filter = new BloomFilter(lastBloomFilterElementCount, fpRate, bloomFilterTweak,
|
filter = new BloomFilter(lastBloomFilterElementCount, fpRate, bloomFilterTweak,
|
||||||
BloomFilter.BloomUpdate.UPDATE_ALL);
|
BloomFilter.BloomUpdate.UPDATE_ALL);
|
||||||
for (PeerFilterProvider p : providers)
|
for (PeerFilterProvider p : providers)
|
||||||
filter.merge(p.getBloomFilter(lastBloomFilterElementCount, fpRate, bloomFilterTweak));
|
filter.merge(p.getBloomFilter(lastBloomFilterElementCount, fpRate, bloomFilterTweak));
|
||||||
|
|
||||||
result.changed = !filter.equals(lastFilter);
|
changed = !filter.equals(lastFilter);
|
||||||
result.filter = lastFilter = filter;
|
lastFilter = filter;
|
||||||
|
} else {
|
||||||
|
changed = false;
|
||||||
|
filter = null;
|
||||||
}
|
}
|
||||||
// Now adjust the earliest key time backwards by a week to handle the case of clock drift. This can occur
|
return new Result(filter, earliestKeyTime, changed);
|
||||||
// both in block header timestamps and if the users clock was out of sync when the key was first created
|
|
||||||
// (to within a small amount of tolerance).
|
|
||||||
result.earliestKeyTime = result.earliestKeyTime.minus(7, ChronoUnit.DAYS);
|
|
||||||
return result;
|
|
||||||
} finally {
|
} finally {
|
||||||
for (PeerFilterProvider provider : begunProviders) {
|
for (PeerFilterProvider provider : begunProviders) {
|
||||||
provider.endBloomFilterCalculation();
|
provider.endBloomFilterCalculation();
|
||||||
|
|
Loading…
Add table
Reference in a new issue