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

Remove IPolicyPlugin #971

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
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
8 changes: 3 additions & 5 deletions neo/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.Plugins;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.Wallets;
Expand Down Expand Up @@ -210,10 +209,9 @@ public ConsensusPayload MakePrepareRequest()
{
byte[] buffer = new byte[sizeof(ulong)];
random.NextBytes(buffer);
IEnumerable<Transaction> memoryPoolTransactions = Blockchain.Singleton.MemPool.GetSortedVerifiedTransactions();
foreach (IPolicyPlugin plugin in Plugin.Policies)
memoryPoolTransactions = plugin.FilterForBlock(memoryPoolTransactions);
List<Transaction> transactions = memoryPoolTransactions.ToList();
List<Transaction> transactions = Blockchain.Singleton.MemPool.GetSortedVerifiedTransactions()
.Take((int)NativeContract.Policy.GetMaxTransactionsPerBlock(Snapshot))
.ToList();
TransactionHashes = transactions.Select(p => p.Hash).ToArray();
Transactions = transactions.ToDictionary(p => p.Hash);
Block.Timestamp = Math.Max(TimeProvider.Current.UtcNow.ToTimestampMS(), PrevHeader.Timestamp + 1);
Expand Down
3 changes: 2 additions & 1 deletion neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.Plugins;
using Neo.SmartContract.Native;
using Neo.Wallets;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -66,7 +67,7 @@ private bool AddTransaction(Transaction tx, bool verify)
RequestChangeView(ChangeViewReason.TxInvalid);
return false;
}
if (!Plugin.CheckPolicy(tx))
if (!NativeContract.Policy.CheckPolicy(tx, context.Snapshot))
{
Log($"reject tx: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}", LogLevel.Warning);
RequestChangeView(ChangeViewReason.TxRejectedByPolicy);
Expand Down
4 changes: 2 additions & 2 deletions neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private void OnFillMemoryPool(IEnumerable<Transaction> transactions)
{
if (Store.ContainsTransaction(tx.Hash))
continue;
if (!Plugin.CheckPolicy(tx))
if (!NativeContract.Policy.CheckPolicy(tx, currentSnapshot))
continue;
// First remove the tx if it is unverified in the pool.
MemPool.TryRemoveUnVerified(tx.Hash, out _);
Expand Down Expand Up @@ -361,7 +361,7 @@ private RelayResultReason OnNewTransaction(Transaction transaction)
return RelayResultReason.OutOfMemory;
if (!transaction.Verify(currentSnapshot, MemPool.GetVerifiedTransactions()))
return RelayResultReason.Invalid;
if (!Plugin.CheckPolicy(transaction))
if (!NativeContract.Policy.CheckPolicy(transaction, currentSnapshot))
return RelayResultReason.PolicyFail;

if (!MemPool.TryAdd(transaction.Hash, transaction))
Expand Down
11 changes: 0 additions & 11 deletions neo/Plugins/IPolicyPlugin.cs

This file was deleted.

11 changes: 0 additions & 11 deletions neo/Plugins/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Configuration;
using Neo.Network.P2P.Payloads;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -13,7 +12,6 @@ public abstract class Plugin
{
public static readonly List<Plugin> Plugins = new List<Plugin>();
private static readonly List<ILogPlugin> Loggers = new List<ILogPlugin>();
internal static readonly List<IPolicyPlugin> Policies = new List<IPolicyPlugin>();
internal static readonly List<IRpcPlugin> RpcPlugins = new List<IRpcPlugin>();
internal static readonly List<IPersistencePlugin> PersistencePlugins = new List<IPersistencePlugin>();
internal static readonly List<IP2PPlugin> P2PPlugins = new List<IP2PPlugin>();
Expand Down Expand Up @@ -51,22 +49,13 @@ protected Plugin()

if (this is ILogPlugin logger) Loggers.Add(logger);
if (this is IP2PPlugin p2p) P2PPlugins.Add(p2p);
if (this is IPolicyPlugin policy) Policies.Add(policy);
if (this is IRpcPlugin rpc) RpcPlugins.Add(rpc);
if (this is IPersistencePlugin persistence) PersistencePlugins.Add(persistence);
if (this is IMemoryPoolTxObserverPlugin txObserver) TxObserverPlugins.Add(txObserver);

Configure();
}

public static bool CheckPolicy(Transaction tx)
{
foreach (IPolicyPlugin plugin in Policies)
if (!plugin.FilterForMemoryPool(tx))
return false;
return true;
}

public abstract void Configure();

protected virtual void OnPluginsLoaded()
Expand Down
9 changes: 9 additions & 0 deletions neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using Neo.VM;
Expand All @@ -26,6 +27,14 @@ public PolicyContract()
Manifest.Features = ContractFeatures.HasStorage;
}

internal bool CheckPolicy(Transaction tx, Snapshot snapshot)
{
UInt160[] blockedAccounts = GetBlockedAccounts(snapshot);
if (blockedAccounts.Intersect(tx.GetScriptHashesForVerifying(snapshot)).Any())
return false;
return true;
}

private bool CheckValidators(ApplicationEngine engine)
{
UInt256 prev_hash = engine.Snapshot.PersistingBlock.PrevHash;
Expand Down