Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compaction threshold config #863

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AddressIndexRepository : MemorySizeCache<string, AddressIndexerData

private readonly ILogger logger;

public AddressIndexRepository(LiteDatabase db, int maxBalanceChangesToKeep = 50_000) : base(maxBalanceChangesToKeep)
public AddressIndexRepository(LiteDatabase db, int maxBalanceChangesToKeep = 150000) : base(maxBalanceChangesToKeep)
{
this.logger = LogManager.GetCurrentClassLogger();
this.addressIndexerDataCollection = db.GetCollection<AddressIndexerData>(DbAddressDataKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public class AddressIndexer : IAddressIndexer
/// </summary>
private const int DelayTimeMs = 2000;

private const int CompactionThreshold = 8000;
private int compactionThreshold;

private const int CompactionAmount = CompactionThreshold / 2;
private int compactionAmount => this.compactionThreshold / 2;

/// <summary>Max distance between consensus and indexer tip to consider indexer synced.</summary>
private const int ConsiderSyncedMaxDistance = 10;
Expand Down Expand Up @@ -162,6 +162,7 @@ public AddressIndexer(StoreSettings storeSettings, DataFolder dataFolder, Networ
this.chainIndexer = chainIndexer;
this.logger = LogManager.GetCurrentClassLogger();

this.compactionThreshold = storeSettings.AddressIndexerCompactionThreshold;
this.averageTimePerBlock = new AverageCalculator(200);
int maxReorgLength = GetMaxReorgOrFallbackMaxReorg(this.network);

Expand Down Expand Up @@ -548,7 +549,7 @@ private void ProcessBalanceChangeLocked(int height, string address, Money amount
// Anything less than that should be compacted.
int heightThreshold = this.consensusManager.Tip.Height - this.compactionTriggerDistance;

bool compact = (indexData.BalanceChanges.Count > CompactionThreshold) && (indexData.BalanceChanges[1].BalanceChangedHeight < heightThreshold);
bool compact = (indexData.BalanceChanges.Count > this.compactionThreshold) && (indexData.BalanceChanges[1].BalanceChangedHeight < heightThreshold);

if (!compact)
{
Expand All @@ -558,7 +559,7 @@ private void ProcessBalanceChangeLocked(int height, string address, Money amount
return;
}

var compacted = new List<AddressBalanceChange>(CompactionThreshold / 2)
var compacted = new List<AddressBalanceChange>(this.compactionThreshold / 2)
{
new AddressBalanceChange()
{
Expand All @@ -572,7 +573,7 @@ private void ProcessBalanceChangeLocked(int height, string address, Money amount
{
AddressBalanceChange change = indexData.BalanceChanges[i];

if ((change.BalanceChangedHeight) < heightThreshold && i < CompactionAmount)
if ((change.BalanceChangedHeight) < heightThreshold && i < this.compactionAmount)
{
this.logger.LogDebug("Balance change: {0} was selected for compaction. Compacted balance now: {1}.", change, compacted[0].Satoshi);

Expand Down
5 changes: 5 additions & 0 deletions src/Stratis.Bitcoin.Features.BlockStore/StoreSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class StoreSettings
/// <summary><c>true</c> to maintain a full addresses index.</summary>
public bool AddressIndex { get; set; }

/// <summary>How many balance changes per address should be accumulated before compaction happens. Compaction compacts half of the balance changes.</summary>
public int AddressIndexerCompactionThreshold { get; set; }

/// <summary>Calculates minimum amount of blocks we need to keep during pruning.</summary>
private int GetMinPruningAmount()
{
Expand Down Expand Up @@ -73,6 +76,7 @@ public StoreSettings(NodeSettings nodeSettings)
this.ReIndex = config.GetOrDefault<bool>("reindex", false, this.logger);
this.ReIndexChain = config.GetOrDefault<bool>("reindex-chain", false, this.logger);
this.AddressIndex = config.GetOrDefault<bool>("addressindex", false, this.logger);
this.AddressIndexerCompactionThreshold = config.GetOrDefault<int>("compactionthreshold", 8000, this.logger);

if (this.PruningEnabled && this.TxIndex)
throw new ConfigurationException("Prune mode is incompatible with -txindex");
Expand All @@ -90,6 +94,7 @@ public static void PrintHelp(Network network)
builder.AppendLine($"-reindex=<0 or 1> Rebuild store with tx index from block data files on disk.");
builder.AppendLine($"-reindex-chain=<0 or 1> Rebuild the coindb from block data files on disk.");
builder.AppendLine($"-addressindex=<0 or 1> Enable to maintain a full address index.");
builder.AppendLine($"-compactionthreshold=<integer value> Specify address indexer compaction threshold.");

NodeSettings.Default(network).Logger.LogInformation(builder.ToString());
}
Expand Down