Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Add docstrings and improve readability to make the source code more welcoming to newcomers. #122

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
12 changes: 0 additions & 12 deletions Common/Class1.cs

This file was deleted.

38 changes: 0 additions & 38 deletions Common/Common.csproj

This file was deleted.

7 changes: 4 additions & 3 deletions QBitNinja/ActionDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ namespace QBitNinja
{
public class ActionDisposable : IDisposable
{
Action _Act;
readonly Action _Act;

public ActionDisposable(Action act)
{
_Act = act;
}

public ActionDisposable(Action start, Action act)
{
start();
_Act = act;
}

#region IDisposable Members

public void Dispose()
{
_Act();
}

#endregion
}
}
28 changes: 15 additions & 13 deletions QBitNinja/CacheBlocksRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,45 @@ namespace QBitNinja
{
public class CacheBlocksRepository : IBlocksRepository
{
private IBlocksRepository _Repo;
private readonly IBlocksRepository _Repo;

public CacheBlocksRepository(IBlocksRepository repo)
{
if(repo == null)
throw new ArgumentNullException("repo");
this._Repo = repo;
this._Repo = repo ?? throw new ArgumentNullException("repo");
}

List<Tuple<uint256, Block>> _LastAsked = new List<Tuple<uint256, Block>>();
List<Tuple<uint256, Block>> _LastAsked = new List<Tuple<uint256, Block>>(); // A cache of blocks.


#region IBlocksRepository Members

int MaxBlocks = 70;
static readonly int MaxBlocks = 70; // The maximum number of blocks that may be cached.

public IEnumerable<NBitcoin.Block> GetBlocks(IEnumerable<NBitcoin.uint256> hashes, CancellationToken cancellation)
public IEnumerable<NBitcoin.Block> GetBlocks(IEnumerable<NBitcoin.uint256> hashes, CancellationToken cancellationToken)
{
var asked = hashes.ToList();
if(asked.Count > MaxBlocks)
return _Repo.GetBlocks(hashes, cancellation);

if (asked.Count > MaxBlocks) // Asked for more than fit in the cache size, no point in checking cache.
return _Repo.GetBlocks(hashes, cancellationToken); // Fetch from repo instead.

var lastAsked = _LastAsked;

if(lastAsked != null && asked.SequenceEqual(lastAsked.Select(a => a.Item1)))
if (lastAsked != null && asked.SequenceEqual(lastAsked.Select(a => a.Item1)))
return lastAsked.Select(l=>l.Item2);
var blocks = _Repo.GetBlocks(hashes, cancellation).ToList();
if(blocks.Count < 5)

var blocks = _Repo.GetBlocks(hashes, cancellationToken).ToList();

if (blocks.Count < 5) // Shouldn't this number match 'MaxBlocks = 70' ?
{
_LastAsked = blocks.Select(b => Tuple.Create(b.GetHash(), b)).ToList();
}
else
{
_LastAsked = null;
}

return blocks;
}

#endregion
}
}
28 changes: 12 additions & 16 deletions QBitNinja/ChainTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,27 @@
namespace QBitNinja
{
/// <summary>
/// Such table can store data keyed by Height/BlockId/TransactionId, and range query them
/// Wraps an Azure table that may store data keyed by Height/BlockId/TransactionId, and allows for range queries against it.
/// </summary>
public class ChainTable<T>
{
private readonly JsonSerializerSettings serializerSettings;
readonly CloudTable _cloudTable;
readonly CloudTable _cloudTable; // An Azure table

public ChainTable(CloudTable cloudTable, Network network)
{
if(cloudTable == null)
throw new ArgumentNullException("cloudTable");
if (network == null)
throw new ArgumentNullException(nameof(network));
JsonSerializerSettings serializerSettings = new JsonSerializerSettings();

var serializerSettings = new JsonSerializerSettings();
QBitNinja.Serializer.RegisterFrontConverters(serializerSettings, network);
this.serializerSettings = serializerSettings;
_cloudTable = cloudTable;
}

public CloudTable Table
{
get
{
return _cloudTable;
}
}
public CloudTable Table => _cloudTable;

public Scope Scope
{
Expand All @@ -50,8 +46,6 @@ public void Create(ConfirmedBalanceLocator locator, T item)
Table.Execute(TableOperation.InsertOrReplace(entity));
}



public void Delete(ConfirmedBalanceLocator locator)
{
var entity = new DynamicTableEntity(Escape(Scope), Escape(locator))
Expand All @@ -60,15 +54,16 @@ public void Delete(ConfirmedBalanceLocator locator)
};
Table.Execute(TableOperation.Delete(entity));
}

public void Delete()
{
foreach(var entity in Table.ExecuteQuery(new TableQuery()
var tableQuery = new TableQuery()
{
FilterString = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, Escape(Scope))
}))
{
};

foreach (var entity in Table.ExecuteQuery(tableQuery))
Table.Execute(TableOperation.Delete(entity));
}
}

public IEnumerable<T> Query(ChainBase chain, BalanceQuery query = null)
Expand All @@ -95,6 +90,7 @@ private string ParseData(DynamicTableEntity entity)
}
return builder.ToString();
}

private void PutData(DynamicTableEntity entity, string str)
{
int i = 0;
Expand Down
22 changes: 17 additions & 5 deletions QBitNinja/Models/WhatIsItModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,14 @@ public WhatIsExtPubKey(BitcoinExtPubKey data) : base(data)

public string PubKey
{
get; set;
get;
set;
}

public bool Hardened
{
get; set;
get;
set;
}
public string ChainCode
{
Expand Down Expand Up @@ -305,11 +307,13 @@ public WhatIsExtKey(BitcoinExtKey data) : base(data)

public string ExtPubKey
{
get; set;
get;
set;
}
public string PrivateKey
{
get; set;
get;
set;
}
}

Expand Down Expand Up @@ -362,6 +366,7 @@ public string Hash
get;
set;
}

public string ColoredAddress
{
get;
Expand All @@ -373,11 +378,13 @@ public WhatIsScript ScriptPubKey
get;
set;
}

public WhatIsScript RedeemScript
{
get;
set;
}

public WhatIsPublicKey PublicKey
{
get;
Expand All @@ -390,6 +397,7 @@ public WhatIsBase58()
{

}

public WhatIsBase58(IBitcoinString data)
{
Data = data.ToString();
Expand Down Expand Up @@ -418,15 +426,19 @@ public string Data
get;
set;
}

public string Type
{
get;
set;
}

public string StringType
{
get; set;
get;
set;
}

public Network Network
{
get;
Expand Down
Loading