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

Transaction size plugin #10

Merged
merged 23 commits into from Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions SimplePolicy/Settings.cs
Expand Up @@ -11,6 +11,10 @@ internal class Settings
{
public int MaxTransactionsPerBlock { get; }
public int MaxFreeTransactionsPerBlock { get; }
public int MaxTransactionSize { get; }
public int MaxFreeTransactionSize { get; }
public int TransactionExtraSize { get; }
public decimal FeePerExtraByte { get; }
public BlockedAccounts BlockedAccounts { get; }

public static Settings Default { get; }
Expand All @@ -24,6 +28,10 @@ public Settings(IConfigurationSection section)
{
this.MaxTransactionsPerBlock = GetValueOrDefault(section.GetSection("MaxTransactionsPerBlock"), 500, p => int.Parse(p));
this.MaxFreeTransactionsPerBlock = GetValueOrDefault(section.GetSection("MaxFreeTransactionsPerBlock"), 20, p => int.Parse(p));
this.MaxTransactionSize = GetValueOrDefault(section.GetSection("MaxTransactionSize"), 100000, p => int.Parse(p));
this.MaxFreeTransactionSize = GetValueOrDefault(section.GetSection("MaxFreeTransactionSize"), 400, p => int.Parse(p));
belane marked this conversation as resolved.
Show resolved Hide resolved
this.TransactionExtraSize = GetValueOrDefault(section.GetSection("TransactionExtraSize"), 2000, p => int.Parse(p));
this.FeePerExtraByte = GetValueOrDefault(section.GetSection("FeePerExtraByte"), 0.00001M, p => decimal.Parse(p));
jsolman marked this conversation as resolved.
Show resolved Hide resolved
this.BlockedAccounts = new BlockedAccounts(section.GetSection("BlockedAccounts"));
}

Expand Down
4 changes: 4 additions & 0 deletions SimplePolicy/SimplePolicy/config.json
Expand Up @@ -2,6 +2,10 @@
"PluginConfiguration": {
"MaxTransactionsPerBlock": 500,
"MaxFreeTransactionsPerBlock": 20,
"MaxTransactionSize": 100000,
"MaxFreeTransactionSize": 400,
"TransactionExtraSize": 2000,
"FeePerExtraByte": 0.00001,
jsolman marked this conversation as resolved.
Show resolved Hide resolved
"BlockedAccounts": {
"Type": "AllowAll",
"List": []
Expand Down
148 changes: 84 additions & 64 deletions SimplePolicy/SimplePolicyPlugin.cs
@@ -1,64 +1,84 @@
using Neo.Consensus;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Plugins
{
public class SimplePolicyPlugin : Plugin, ILogPlugin, IPolicyPlugin
{
private static string log_dictionary = Path.Combine(AppContext.BaseDirectory, "Logs");

public bool FilterForMemoryPool(Transaction tx)
{
switch (Settings.Default.BlockedAccounts.Type)
{
case PolicyType.AllowAll:
return true;
case PolicyType.AllowList:
return tx.Witnesses.All(p => Settings.Default.BlockedAccounts.List.Contains(p.VerificationScript.ToScriptHash())) || tx.Outputs.All(p => Settings.Default.BlockedAccounts.List.Contains(p.ScriptHash));
case PolicyType.DenyList:
return tx.Witnesses.All(p => !Settings.Default.BlockedAccounts.List.Contains(p.VerificationScript.ToScriptHash())) && tx.Outputs.All(p => !Settings.Default.BlockedAccounts.List.Contains(p.ScriptHash));
default:
return false;
}
}

public IEnumerable<Transaction> FilterForBlock(IEnumerable<Transaction> transactions)
{
Transaction[] array = transactions.ToArray();
if (array.Length + 1 <= Settings.Default.MaxTransactionsPerBlock)
return array;
transactions = array.OrderByDescending(p => p.NetworkFee / p.Size).ThenByDescending(p => p.NetworkFee).Take(Settings.Default.MaxTransactionsPerBlock - 1);
return FilterFree(transactions);
}

private IEnumerable<Transaction> FilterFree(IEnumerable<Transaction> transactions)
{
int count = 0;
foreach (Transaction tx in transactions)
if (tx.NetworkFee > Fixed8.Zero || tx.SystemFee > Fixed8.Zero)
yield return tx;
else if (count++ < Settings.Default.MaxFreeTransactionsPerBlock)
yield return tx;
}

void ILogPlugin.Log(string source, LogLevel level, string message)
{
if (source != nameof(ConsensusService)) return;
DateTime now = DateTime.Now;
string line = $"[{now.TimeOfDay:hh\\:mm\\:ss\\.fff}] {message}";
Console.WriteLine(line);
if (string.IsNullOrEmpty(log_dictionary)) return;
lock (log_dictionary)
{
Directory.CreateDirectory(log_dictionary);
string path = Path.Combine(log_dictionary, $"{now:yyyy-MM-dd}.log");
File.AppendAllLines(path, new[] { line });
}
}
}
}
using Neo.Consensus;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Plugins
{
public class SimplePolicyPlugin : Plugin, ILogPlugin, IPolicyPlugin
{
private static string log_dictionary = Path.Combine(AppContext.BaseDirectory, "Logs");

public bool FilterForMemoryPool(Transaction tx)
{
if (!VerifySizeLimits(tx)) return false;

switch (Settings.Default.BlockedAccounts.Type)
{
case PolicyType.AllowAll:
return true;
case PolicyType.AllowList:
return tx.Witnesses.All(p => Settings.Default.BlockedAccounts.List.Contains(p.VerificationScript.ToScriptHash())) || tx.Outputs.All(p => Settings.Default.BlockedAccounts.List.Contains(p.ScriptHash));
belane marked this conversation as resolved.
Show resolved Hide resolved
case PolicyType.DenyList:
return tx.Witnesses.All(p => !Settings.Default.BlockedAccounts.List.Contains(p.VerificationScript.ToScriptHash())) && tx.Outputs.All(p => !Settings.Default.BlockedAccounts.List.Contains(p.ScriptHash));
default:
return false;
}
}

public IEnumerable<Transaction> FilterForBlock(IEnumerable<Transaction> transactions)
{
Transaction[] array = transactions.ToArray();
if (array.Length + 1 <= Settings.Default.MaxTransactionsPerBlock)
return array;
transactions = array.OrderByDescending(p => p.NetworkFee / p.Size).ThenByDescending(p => p.NetworkFee).Take(Settings.Default.MaxTransactionsPerBlock - 1);
belane marked this conversation as resolved.
Show resolved Hide resolved
return FilterFree(transactions);
}

private IEnumerable<Transaction> FilterFree(IEnumerable<Transaction> transactions)
{
int count = 0;
foreach (Transaction tx in transactions)
if (tx.NetworkFee > Fixed8.Zero || tx.SystemFee > Fixed8.Zero)
yield return tx;
else if (count++ < Settings.Default.MaxFreeTransactionsPerBlock)
yield return tx;
}

void ILogPlugin.Log(string source, LogLevel level, string message)
{
if (source != nameof(ConsensusService)) return;
DateTime now = DateTime.Now;
string line = $"[{now.TimeOfDay:hh\\:mm\\:ss\\.fff}] {message}";
Console.WriteLine(line);
if (string.IsNullOrEmpty(log_dictionary)) return;
belane marked this conversation as resolved.
Show resolved Hide resolved
lock (log_dictionary)
{
Directory.CreateDirectory(log_dictionary);
string path = Path.Combine(log_dictionary, $"{now:yyyy-MM-dd}.log");
File.AppendAllLines(path, new[] { line });
}
}

private bool VerifySizeLimits(Transaction tx)
{
// Not Allow free TX bigger than MaxFreeTransactionSize
if (tx.NetworkFee.Equals(0) && tx.Size > Settings.Default.MaxFreeTransactionSize) return false;

// Not Allow TX bigger than MaxTransactionSize
if (tx.Size > Settings.Default.MaxTransactionSize) return false;

// For TX bigger than TransactionExtraSize require proportional fee
if (tx.Size > Settings.Default.TransactionExtraSize)
{
decimal fee = (tx.Size - Settings.Default.TransactionExtraSize) * Settings.Default.FeePerExtraByte;

if (tx.NetworkFee < Fixed8.FromDecimal(fee)) return false;
}
return true;
}
}
}
98 changes: 49 additions & 49 deletions neo-plugins.sln
@@ -1,49 +1,49 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplePolicy", "SimplePolicy\SimplePolicy.csproj", "{322C366E-F891-47FD-A039-0FEF79BCB6D5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationLogs", "ApplicationLogs\ApplicationLogs.csproj", "{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcDisabled", "RpcDisabled\RpcDisabled.csproj", "{6800D782-8EC0-49E9-98C4-195C8F781A1F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StatesDumper", "StatesDumper\StatesDumper.csproj", "{86531DB1-A231-46C4-823F-BE60972F7523}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImportBlocks", "ImportBlocks\ImportBlocks.csproj", "{B7A42984-57BB-4F8D-967B-23B0E841B726}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{322C366E-F891-47FD-A039-0FEF79BCB6D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{322C366E-F891-47FD-A039-0FEF79BCB6D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{322C366E-F891-47FD-A039-0FEF79BCB6D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{322C366E-F891-47FD-A039-0FEF79BCB6D5}.Release|Any CPU.Build.0 = Release|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Release|Any CPU.Build.0 = Release|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Release|Any CPU.Build.0 = Release|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Release|Any CPU.Build.0 = Release|Any CPU
{B7A42984-57BB-4F8D-967B-23B0E841B726}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B7A42984-57BB-4F8D-967B-23B0E841B726}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7A42984-57BB-4F8D-967B-23B0E841B726}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7A42984-57BB-4F8D-967B-23B0E841B726}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61D3ADE6-BBFC-402D-AB42-1C71C9F9EDE3}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplePolicy", "SimplePolicy\SimplePolicy.csproj", "{322C366E-F891-47FD-A039-0FEF79BCB6D5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationLogs", "ApplicationLogs\ApplicationLogs.csproj", "{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcDisabled", "RpcDisabled\RpcDisabled.csproj", "{6800D782-8EC0-49E9-98C4-195C8F781A1F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StatesDumper", "StatesDumper\StatesDumper.csproj", "{86531DB1-A231-46C4-823F-BE60972F7523}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImportBlocks", "ImportBlocks\ImportBlocks.csproj", "{B7A42984-57BB-4F8D-967B-23B0E841B726}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{322C366E-F891-47FD-A039-0FEF79BCB6D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{322C366E-F891-47FD-A039-0FEF79BCB6D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{322C366E-F891-47FD-A039-0FEF79BCB6D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{322C366E-F891-47FD-A039-0FEF79BCB6D5}.Release|Any CPU.Build.0 = Release|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}.Release|Any CPU.Build.0 = Release|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6800D782-8EC0-49E9-98C4-195C8F781A1F}.Release|Any CPU.Build.0 = Release|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86531DB1-A231-46C4-823F-BE60972F7523}.Release|Any CPU.Build.0 = Release|Any CPU
{B7A42984-57BB-4F8D-967B-23B0E841B726}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B7A42984-57BB-4F8D-967B-23B0E841B726}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7A42984-57BB-4F8D-967B-23B0E841B726}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7A42984-57BB-4F8D-967B-23B0E841B726}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61D3ADE6-BBFC-402D-AB42-1C71C9F9EDE3}
EndGlobalSection
EndGlobal