Skip to content

Commit

Permalink
Merge branch 'master' into contract_redirectiton
Browse files Browse the repository at this point in the history
  • Loading branch information
vncoelho authored Jan 6, 2020
2 parents eaba8be + 6a91212 commit 5377b78
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/neo/IO/Caching/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public TValue this[TKey key]
}
}

/// <summary>
/// Try to Add a specific key, with associated value, to the current cached dictionary.
/// It will not read from internal state.
/// However, if previously cached into Dictionary, request may fail.
/// </summary>
/// <param name="key">Key to be possible added.
/// Key will not be added if value exists cached and the modification was not a Deleted one.
/// </param>
/// <param name="value">Corresponding value to be added, in the case of sucess.</param>
/// <exception cref="ArgumentException">If cached on dictionary, with any state rather than `Deleted`, an Exception will be raised.</exception>
public void Add(TKey key, TValue value)
{
lock (dictionary)
Expand All @@ -60,6 +70,9 @@ public void Add(TKey key, TValue value)

protected abstract void AddInternal(TKey key, TValue value);

/// <summary>
/// Update internals with all changes cached on Dictionary which are not None.
/// </summary>
public void Commit()
{
foreach (Trackable trackable in GetChangeSet())
Expand All @@ -82,6 +95,10 @@ public DataCache<TKey, TValue> CreateSnapshot()
return new CloneCache<TKey, TValue>(this);
}

/// <summary>
/// Delete key from cached Dictionary or search in Internal.
/// </summary>
/// <param name="key">Key to be deleted.</param>
public void Delete(TKey key)
{
lock (dictionary)
Expand Down Expand Up @@ -186,6 +203,14 @@ public IEnumerable<Trackable> GetChangeSet()

protected abstract TValue GetInternal(TKey key);

/// <summary>
/// Try to Get a specific key from current cached dictionary.
/// Otherwise, tries to get from internal data with TryGetInternal.
/// </summary>
/// <param name="key">Key to be searched.</param>
/// <param name="factory">Function that may replace current object stored.
/// If object already exists the factory passed as parameter will not be used.
/// </param>
public TValue GetAndChange(TKey key, Func<TValue> factory = null)
{
lock (dictionary)
Expand Down
19 changes: 17 additions & 2 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected override void NeedMorePeers(int count)
else
{
// Will call AddPeers with default SeedList set cached on <see cref="ProtocolSettings"/>.
// It will try to add those, sequentially, to the list of currently uncconected ones.
// It will try to add those, sequentially, to the list of currently unconnected ones.

Random rand = new Random();
AddPeers(SeedList.Where(u => u != null).OrderBy(p => rand.Next()).Take(count));
Expand Down Expand Up @@ -197,7 +197,22 @@ private void OnRelay(IInventory inventory)
system.Blockchain.Tell(inventory);
}

private void OnRelayDirectly(IInventory inventory) => SendToRemoteNodes(new RemoteNode.Relay { Inventory = inventory });
private void OnRelayDirectly(IInventory inventory)
{
var message = new RemoteNode.Relay { Inventory = inventory };
// When relaying a block, if the block's index is greater than 'LastBlockIndex' of the RemoteNode, relay the block;
// otherwise, don't relay.
if (inventory is Block block)
{
foreach (KeyValuePair<IActorRef, RemoteNode> kvp in RemoteNodes)
{
if (block.Index > kvp.Value.LastBlockIndex)
kvp.Key.Tell(message);
}
}
else
SendToRemoteNodes(message);
}

private void OnSendDirectly(IInventory inventory) => SendToRemoteNodes(inventory);

Expand Down

0 comments on commit 5377b78

Please sign in to comment.