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

Policy inputs checks #1938

Merged
merged 10 commits into from
Sep 19, 2020
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: 6 additions & 2 deletions src/neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma warning disable IDE0051

using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using System;
Expand Down Expand Up @@ -87,8 +89,8 @@ public bool IsAnyAccountBlocked(StoreView snapshot, params UInt160[] hashes)
[ContractMethod(0_03000000, CallFlags.AllowModifyStates)]
private bool SetMaxBlockSize(ApplicationEngine engine, uint value)
{
if (value > Message.PayloadMaxSize) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) return false;
if (Network.P2P.Message.PayloadMaxSize <= value) return false;
StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_MaxBlockSize), () => new StorageItem());
storage.Set(value);
return true;
Expand All @@ -97,6 +99,7 @@ private bool SetMaxBlockSize(ApplicationEngine engine, uint value)
[ContractMethod(0_03000000, CallFlags.AllowModifyStates)]
private bool SetMaxTransactionsPerBlock(ApplicationEngine engine, uint value)
{
if (value > Block.MaxTransactionsPerBlock) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) return false;
StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_MaxTransactionsPerBlock), () => new StorageItem());
storage.Set(value);
Expand All @@ -106,8 +109,8 @@ private bool SetMaxTransactionsPerBlock(ApplicationEngine engine, uint value)
[ContractMethod(0_03000000, CallFlags.AllowModifyStates)]
private bool SetMaxBlockSystemFee(ApplicationEngine engine, long value)
{
if (value <= 4007600) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) return false;
if (value <= 4007600) return false;
StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_MaxBlockSystemFee), () => new StorageItem());
storage.Set(value);
return true;
Expand All @@ -116,6 +119,7 @@ private bool SetMaxBlockSystemFee(ApplicationEngine engine, long value)
[ContractMethod(0_03000000, CallFlags.AllowModifyStates)]
private bool SetFeePerByte(ApplicationEngine engine, long value)
{
if (value < 0 || value > 1_00000000) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) return false;
StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_FeePerByte), () => new StorageItem());
storage.Set(value);
Expand Down
4 changes: 3 additions & 1 deletion tests/neo.UnitTests/Extensions/NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public static StackItem Call(this NativeContract contract, StoreView snapshot, I

if (engine.Execute() != VMState.HALT)
{
throw new InvalidOperationException();
Exception exception = engine.FaultException;
while (exception?.InnerException != null) exception = exception.InnerException;
throw exception ?? new InvalidOperationException();
}

return engine.ResultStack.Pop();
Expand Down
17 changes: 10 additions & 7 deletions tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.UnitTests.Extensions;
using System;
using System.Linq;

namespace Neo.UnitTests.SmartContract.Native
Expand Down Expand Up @@ -72,10 +73,11 @@ public void Check_SetMaxBlockSize()

// More than expected

ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(committeeMultiSigAddr),
"setMaxBlockSize", new ContractParameter(ContractParameterType.Integer) { Value = Neo.Network.P2P.Message.PayloadMaxSize });
ret.Should().BeOfType<VM.Types.Boolean>();
ret.GetBoolean().Should().BeFalse();
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
{
NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(committeeMultiSigAddr),
"setMaxBlockSize", new ContractParameter(ContractParameterType.Integer) { Value = Neo.Network.P2P.Message.PayloadMaxSize + 1 });
});

ret = NativeContract.Policy.Call(snapshot, "getMaxBlockSize");
ret.Should().BeOfType<VM.Types.Integer>();
Expand Down Expand Up @@ -120,10 +122,11 @@ public void Check_SetMaxBlockSystemFee()

// Less than expected

ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(committeeMultiSigAddr),
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
{
NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(committeeMultiSigAddr),
"setMaxBlockSystemFee", new ContractParameter(ContractParameterType.Integer) { Value = -1000 });
ret.Should().BeOfType<VM.Types.Boolean>();
ret.GetBoolean().Should().BeFalse();
});

ret = NativeContract.Policy.Call(snapshot, "getMaxBlockSystemFee");
ret.Should().BeOfType<VM.Types.Integer>();
Expand Down