Skip to content

Commit

Permalink
Fix a dead lock in WalletIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Jan 9, 2019
1 parent c16475e commit b1311ba
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions neo/Wallets/WalletIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public IEnumerable<UInt256> GetTransactions(IEnumerable<UInt160> accounts)
yield return hash;
}

private void ProcessBlock(Block block, HashSet<UInt160> accounts, WriteBatch batch)
private (Transaction, UInt160[])[] ProcessBlock(Block block, HashSet<UInt160> accounts, WriteBatch batch)
{
var change_set = new List<(Transaction, UInt160[])>();
foreach (Transaction tx in block.Transactions)
{
HashSet<UInt160> accounts_changed = new HashSet<UInt160>();
Expand Down Expand Up @@ -218,31 +219,29 @@ private void ProcessBlock(Block block, HashSet<UInt160> accounts, WriteBatch bat
{
foreach (UInt160 account in accounts_changed)
batch.Put(SliceBuilder.Begin(DataEntryPrefix.ST_Transaction).Add(account).Add(tx.Hash), false);
WalletTransaction?.Invoke(null, new WalletTransactionEventArgs
{
Transaction = tx,
RelatedAccounts = accounts_changed.ToArray(),
Height = block.Index,
Time = block.Timestamp
});
change_set.Add((tx, accounts_changed.ToArray()));
}
}
return change_set.ToArray();
}

private void ProcessBlocks()
{
while (!disposed)
{
while (!disposed)
{
Block block;
(Transaction, UInt160[])[] change_set;
lock (SyncRoot)
{
if (indexes.Count == 0) break;
uint height = indexes.Keys.Min();
Block block = Blockchain.Singleton.Store.GetBlock(height);
block = Blockchain.Singleton.Store.GetBlock(height);
if (block == null) break;
WriteBatch batch = new WriteBatch();
HashSet<UInt160> accounts = indexes[height];
ProcessBlock(block, accounts, batch);
change_set = ProcessBlock(block, accounts, batch);
ReadOptions options = ReadOptions.Default;
byte[] groupId = db.Get(options, SliceBuilder.Begin(DataEntryPrefix.IX_Group).Add(height)).ToArray();
indexes.Remove(height);
Expand All @@ -261,6 +260,17 @@ private void ProcessBlocks()
}
db.Write(WriteOptions.Default, batch);
}
foreach (var (tx, accounts) in change_set)
{
WalletTransaction?.Invoke(null, new WalletTransactionEventArgs
{
Transaction = tx,
RelatedAccounts = accounts,
Height = block.Index,
Time = block.Timestamp
});
}
}
for (int i = 0; i < 20 && !disposed; i++)
Thread.Sleep(100);
}
Expand Down

0 comments on commit b1311ba

Please sign in to comment.