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

Null Wallet Indexer support in NEP6Wallet (for NEO 2.x) #961

Merged
merged 4 commits into from
Jul 28, 2019
Merged
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
34 changes: 24 additions & 10 deletions neo/Wallets/NEP6/NEP6Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NEP6Wallet : Wallet

public override string Name => name;
public override Version Version => version;
public override uint WalletHeight => indexer.IndexHeight;
public override uint WalletHeight => indexer != null ? indexer.IndexHeight : default;

public NEP6Wallet(WalletIndexer indexer, string path, string name = null)
{
Expand All @@ -46,7 +46,8 @@ public NEP6Wallet(WalletIndexer indexer, string path, string name = null)
this.Scrypt = ScryptParameters.FromJson(wallet["scrypt"]);
this.accounts = ((JArray)wallet["accounts"]).Select(p => NEP6Account.FromJson(p, this)).ToDictionary(p => p.ScriptHash);
this.extra = wallet["extra"];
indexer.RegisterAccounts(accounts.Keys);

indexer?.RegisterAccounts(accounts.Keys);
}
else
{
Expand All @@ -56,7 +57,11 @@ public NEP6Wallet(WalletIndexer indexer, string path, string name = null)
this.accounts = new Dictionary<UInt160, NEP6Account>();
this.extra = JObject.Null;
}
indexer.WalletTransaction += WalletIndexer_WalletTransaction;

if (indexer != null)
{
indexer.WalletTransaction += WalletIndexer_WalletTransaction;
}
}

private void AddAccount(NEP6Account account, bool is_import)
Expand Down Expand Up @@ -86,7 +91,7 @@ private void AddAccount(NEP6Account account, bool is_import)
}
else
{
indexer.RegisterAccounts(new[] { account.ScriptHash }, is_import ? 0 : Blockchain.Singleton.Height);
indexer?.RegisterAccounts(new[] { account.ScriptHash }, is_import ? 0 : Blockchain.Singleton.Height);
}
accounts[account.ScriptHash] = account;
}
Expand Down Expand Up @@ -177,14 +182,17 @@ public override bool DeleteAccount(UInt160 scriptHash)
}
if (removed)
{
indexer.UnregisterAccounts(new[] { scriptHash });
indexer?.UnregisterAccounts(new[] { scriptHash });
}
return removed;
}

public override void Dispose()
{
indexer.WalletTransaction -= WalletIndexer_WalletTransaction;
if (indexer != null)
{
indexer.WalletTransaction -= WalletIndexer_WalletTransaction;
}
}

public override Coin[] FindUnspentCoins(UInt256 asset_id, Fixed8 amount, UInt160[] from)
Expand Down Expand Up @@ -212,6 +220,9 @@ public override IEnumerable<WalletAccount> GetAccounts()

public override IEnumerable<Coin> GetCoins(IEnumerable<UInt160> accounts)
{
if (indexer == null)
return Enumerable.Empty<Coin>();

if (unconfirmed.Count == 0)
return indexer.GetCoins(accounts);
else
Expand Down Expand Up @@ -265,12 +276,15 @@ IEnumerable<Coin> GetCoinsInternal()

public override IEnumerable<UInt256> GetTransactions()
{
foreach (UInt256 hash in indexer.GetTransactions(accounts.Keys))
yield return hash;
lock (unconfirmed)
if (indexer != null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, GitHub's diff of GetTransactions is a little weird. Since GetTransactions is using yield return, I wrapped the existing function in an indexer null check. So if the indexer is null, this function implicitly returns an empty UInt256 enumerable.

{
foreach (UInt256 hash in unconfirmed.Keys)
foreach (UInt256 hash in indexer.GetTransactions(accounts.Keys))
yield return hash;
lock (unconfirmed)
{
foreach (UInt256 hash in unconfirmed.Keys)
yield return hash;
}
}
}

Expand Down