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

Oracles #1584

Closed
wants to merge 72 commits into from
Closed

Oracles #1584

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
e3f6b03
Oracle policy contract (#1445)
doubiliu Mar 11, 2020
3564d73
Oracle Syscall (#1399)
shargon Mar 11, 2020
971d64a
Oracle policy contract (#1445)
doubiliu Mar 11, 2020
687345a
Oracle Syscall (#1399)
shargon Mar 11, 2020
3a87e3f
Merge branch 'oracles' of https://github.com/neo-project/neo into ora…
belane Mar 26, 2020
ffb4b42
Rename OracleResult->OracleResponse (#1516)
shargon Mar 29, 2020
8ed9200
Oracle policy contract (#1445)
doubiliu Mar 11, 2020
4e7d9b2
Oracle Syscall (#1399)
shargon Mar 11, 2020
efcde83
Rename OracleResult->OracleResponse (#1516)
shargon Mar 29, 2020
dd341b5
Merge remote-tracking branch 'neo-project/oracles' into oracles
shargon Apr 3, 2020
2322c1a
Oracle Service (#1517)
shargon Apr 4, 2020
473c097
fix GetOracleMultiSigAddress (#1538)
belane Apr 4, 2020
587b779
Oracle: Tx comunication (#1540)
shargon Apr 6, 2020
06d281c
Transaction Version (#1539)
belane Apr 6, 2020
c101958
Oracle: Allow wallets to add assert for the oracle response (#1541)
shargon Apr 6, 2020
0354c1a
Oracle: Set the right Tx version (#1542)
shargon Apr 12, 2020
384a043
Oracle: P2P Response Signature Message (#1553)
belane Apr 13, 2020
c32e6a4
Oracle: OracleService (#1555)
shargon Apr 16, 2020
653181c
Oracle: Remove some TODOs (#1574)
shargon Apr 17, 2020
3a98905
Oracle policy contract (#1445)
shargon Apr 17, 2020
b439b94
Merge remote-tracking branch 'neo-project/oracles' into oracles
shargon Apr 17, 2020
4a5bc1a
Fix conflicts
shargon Apr 17, 2020
fae1dc6
Fix rebase
shargon Apr 17, 2020
a970ed0
Oracle: Improve filters with argument (#1578)
shargon Apr 17, 2020
76d2214
Oracle: Fix filter hash (#1580)
shargon Apr 18, 2020
614480a
Fix wallet bug (#1581)
shargon Apr 18, 2020
e62827f
Oracle: Move Oracle syscall to oracle native contract (#1582)
shargon Apr 18, 2020
d75268a
Merge branch 'master' into oracles
shargon Apr 18, 2020
1398277
Oracle: Log errors (#1585)
shargon Apr 18, 2020
503411e
Fix UT
shargon Apr 18, 2020
f2c4d23
Oracle: Create UT certificate (#1590)
shargon Apr 19, 2020
d54023a
Merge branch 'master' into oracles
shargon Apr 21, 2020
2d593f6
Fix UT
shargon Apr 21, 2020
79d3d67
Oracle: Allow filtering in the same contract (#1588)
shargon Apr 22, 2020
d2af0f2
Oracle: Allow to define allowed content types in policy (#1591)
shargon Apr 22, 2020
9520f99
Oracle: Remove snapshot from memPool iterator (#1607)
shargon Apr 23, 2020
04d4578
Change StopOracle to return bool
shargon Apr 23, 2020
d9c032b
Oracle: Optimize the filter payment (#1610)
shargon Apr 25, 2020
ed35d53
Merge remote-tracking branch 'neo-project/master' into oracles
shargon May 7, 2020
e7e492d
Fix merge
shargon May 7, 2020
5503a98
Oracles: Move oracle Flag to an attribute (#1618)
shargon May 8, 2020
85a161b
Oracle: Clean changes and add some UT (#1630)
shargon May 8, 2020
8016d47
Merge branch 'master' into oracles
shargon May 10, 2020
a1a75ff
Oracle: Add UT (#1635)
shargon May 10, 2020
44f29db
Oracle: UTs (#1636)
shargon May 11, 2020
7d7d123
Unify relay cache (#1631)
shargon May 11, 2020
36b14d9
Merge branch 'master' into oracles
shargon May 12, 2020
7e5b390
Oracles: Optimize mempool (#1641)
shargon May 12, 2020
9dcbe33
Change variable to const
shargon May 15, 2020
223f357
Unify MemPool methods
shargon May 15, 2020
ebf9170
Merge branch 'master' into oracles
shargon May 15, 2020
b16126d
Merge branch 'master' into oracles
erikzhang May 17, 2020
0192ab0
Subscribe to the RelayResult event
erikzhang May 17, 2020
e6e6fbc
Optimize
erikzhang May 17, 2020
cd554df
Merge branch 'master' into oracles
erikzhang May 17, 2020
6f7e400
Fix
erikzhang May 17, 2020
f1d8a1f
Fix UT
erikzhang May 17, 2020
803cf40
Update MemoryPool.cs
erikzhang May 17, 2020
bc396e0
Clean code
shargon May 17, 2020
286ecd9
Merge branch 'master' into oracles
shargon May 17, 2020
4c13628
Merge branch 'master' into oracles
shargon May 18, 2020
c445371
Update OracleContract
shargon May 18, 2020
c4bed04
Update OracleRequestAttribute.cs
erikzhang May 23, 2020
7c85d18
Merge branch 'master' into oracles
shargon May 23, 2020
5c2ccdb
Merge branch 'master' into oracles
shargon May 26, 2020
1de9d21
Remove duplicate code
shargon May 26, 2020
e82eb78
Rename
shargon May 26, 2020
233a298
Merge branch 'master' into oracles
shargon May 26, 2020
2497bd2
Merge branch 'master' into oracles
shargon May 27, 2020
0a2b5b3
Fix UT
shargon May 27, 2020
9b4529a
Merge remote-tracking branch 'neo-project/master' into oracles
shargon Jul 22, 2020
53f8f2b
Merge
shargon Jul 22, 2020
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
2 changes: 1 addition & 1 deletion src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu
private uint stored_header_count = 0;
private readonly Dictionary<UInt256, Block> block_cache = new Dictionary<UInt256, Block>();
private readonly Dictionary<uint, LinkedList<Block>> block_cache_unverified = new Dictionary<uint, LinkedList<Block>>();
internal readonly RelayCache RelayCache = new RelayCache(100);
internal readonly RelayCache RelayCache = new RelayCache(200);
private SnapshotView currentSnapshot;

public IStore Store { get; }
Expand Down
30 changes: 26 additions & 4 deletions src/neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class MemoryPool : IReadOnlyCollection<Transaction>

private readonly NeoSystem _system;

//
/// <summary>
/// Guarantees consistency of the pool data structures.
///
Expand Down Expand Up @@ -174,7 +173,7 @@ public IEnumerable<Transaction> GetVerifiedTransactions()
_txRwLock.EnterReadLock();
try
{
return _unsortedTransactions.Select(p => p.Value.Tx).ToArray();
return GetTransactions(_unsortedTransactions.Values);
}
finally
{
Expand Down Expand Up @@ -202,14 +201,36 @@ public IEnumerable<Transaction> GetSortedVerifiedTransactions()
_txRwLock.EnterReadLock();
try
{
return _sortedTransactions.Reverse().Select(p => p.Tx).ToArray();
return GetTransactions(_sortedTransactions);
}
finally
{
_txRwLock.ExitReadLock();
}
}

private IEnumerable<Transaction> GetTransactions(IEnumerable<PoolItem> set)
{
var state = new PoolItem.DelayState();
var ret = set
.Reverse()
.Where(u => u.IsReady(state))
.Select(p => p.Tx)
.ToArray();

// Fetch transactions

foreach (var tx in ret)
{
yield return tx;
}
foreach (var delayed in state.Delayed)
{
if (state.Allowed.Contains(delayed.Hash))
yield return delayed;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private PoolItem GetLowestFeeTransaction(SortedSet<PoolItem> verifiedTxSorted,
SortedSet<PoolItem> unverifiedTxSorted, out SortedSet<PoolItem> sortedPool)
Expand Down Expand Up @@ -239,7 +260,7 @@ private PoolItem GetLowestFeeTransaction(out Dictionary<UInt256, PoolItem> unsor
}
finally
{
unsortedTxPool = Object.ReferenceEquals(sortedPool, _unverifiedSortedTransactions)
unsortedTxPool = ReferenceEquals(sortedPool, _unverifiedSortedTransactions)
? _unverifiedTransactions : _unsortedTransactions;
}
}
Expand Down Expand Up @@ -447,6 +468,7 @@ internal void InvalidateAllTransactions()
{
if (_unsortedTransactions.TryAdd(item.Tx.Hash, item))
{
item.CheckOracleResponse(snapshot);
verifiedSortedTxPool.Add(item);

if (item.LastBroadcastTimestamp < rebroadcastCutOffTime)
Expand Down
61 changes: 61 additions & 0 deletions src/neo/Ledger/PoolItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract.Native;
using System;
using System.Collections.Generic;

namespace Neo.Ledger
{
Expand All @@ -26,11 +29,69 @@ internal class PoolItem : IComparable<PoolItem>
/// </summary>
public DateTime LastBroadcastTimestamp;

/// <summary>
/// check if oracle response was already found in storage
/// </summary>
private bool _responseExists;

internal PoolItem(Transaction tx)
{
Tx = tx;
Timestamp = TimeProvider.Current.UtcNow;
LastBroadcastTimestamp = Timestamp;
_responseExists = false;
}

internal class DelayState
{
public HashSet<UInt256> Allowed = new HashSet<UInt256>();
public HashSet<Transaction> Delayed = new HashSet<Transaction>();
}

public void CheckOracleResponse(StoreView snapshot)
{
if (!_responseExists &&
Tx.IsOracleRequest() &&
NativeContract.Oracle.ContainsResponse(snapshot, Tx.Hash))
{
_responseExists = true;
}
}

public bool IsReady(DelayState state)
{
if (Tx.IsOracleRequest())
{
if (state.Allowed.Remove(Tx.Hash))
{
// The response was already fetched, we can put request and response in the same block

return true;
}
else
{
if (_responseExists)
{
// The response it's waiting to be consumed (block+n)

return true;
}
else
{
// If the response it's in the pool it's located after the request
// We save the request in order to put after the response

state.Delayed.Add(Tx);
return false;
}
}
}
else if (Tx.IsOracleResponse(out var oracleRequestTx))
{
state.Allowed.Add(oracleRequestTx);
}

return true;
}

public int CompareTo(Transaction otherTx)
Expand Down
84 changes: 84 additions & 0 deletions src/neo/Ledger/SortedBlockingCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace Neo.Ledger
{
public class SortedBlockingCollection<TKey, TValue>
{
/// <summary>
/// _oracleTasks will consume from this pool
/// </summary>
private readonly BlockingCollection<TValue> _asyncPool = new BlockingCollection<TValue>();

/// <summary>
/// Queue
/// </summary>
private readonly SortedConcurrentDictionary<TKey, TValue> _queue;

/// <summary>
/// Constructor
/// </summary>
/// <param name="comparer">Comparer</param>
/// <param name="capacity">Capacity</param>
public SortedBlockingCollection(IComparer<KeyValuePair<TKey, TValue>> comparer, int capacity)
{
_queue = new SortedConcurrentDictionary<TKey, TValue>(comparer, capacity);
}

/// <summary>
/// Add entry
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
public void Add(TKey key, TValue value)
{
if (_queue.TryAdd(key, value) && _asyncPool.Count <= 0)
{
Pop();
}
}

/// <summary>
/// Clear
/// </summary>
public void Clear()
{
_queue.Clear();

while (_asyncPool.Count > 0)
{
_asyncPool.TryTake(out _);
}
}

/// <summary>
/// Get consuming enumerable
/// </summary>
/// <param name="token">Token</param>
public IEnumerable<TValue> GetConsumingEnumerable(CancellationToken token)
{
foreach (var entry in _asyncPool.GetConsumingEnumerable(token))
{
// Prepare other item in _asyncPool

Pop();

// Iterate items

yield return entry;
}
}

/// <summary>
/// Move one item from the sorted queue to _asyncPool, this will ensure that the threads process the entries according to the priority
/// </summary>
private void Pop()
{
if (_queue.TryPop(out var entry))
{
_asyncPool.Add(entry);
}
}
}
}
Loading