Skip to content

Commit 2a40f67

Browse files
committed
Add: assert committe info
1 parent aef6f4d commit 2a40f67

File tree

7 files changed

+42
-21
lines changed

7 files changed

+42
-21
lines changed

src/Neo/SmartContract/Native/ContractManagement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private long GetMinimumDeploymentFee(IReadOnlyStore snapshot)
140140
private void SetMinimumDeploymentFee(ApplicationEngine engine, BigInteger value/* In the unit of datoshi, 1 datoshi = 1e-8 GAS*/)
141141
{
142142
if (value < 0) throw new ArgumentOutOfRangeException(nameof(value), "cannot be negative");
143-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
143+
AssertCommittee(engine);
144144
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_MinimumDeploymentFee)).Set(value);
145145
}
146146

src/Neo/SmartContract/Native/NativeContract.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ protected static bool CheckCommittee(ApplicationEngine engine)
354354
return engine.CheckWitnessInternal(committeeMultiSigAddr);
355355
}
356356

357+
protected static void AssertCommittee(ApplicationEngine engine)
358+
{
359+
if (!CheckCommittee(engine))
360+
throw new InvalidOperationException("Invalid committee signature. It should be a multisig(len(committee) - (len(committee) - 1) / 2)).");
361+
}
362+
357363
#region Storage keys
358364

359365
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/Neo/SmartContract/Native/NeoToken.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ private void SetGasPerBlock(ApplicationEngine engine, BigInteger gasPerBlock)
286286
{
287287
if (gasPerBlock < 0 || gasPerBlock > 10 * GAS.Factor)
288288
throw new ArgumentOutOfRangeException(nameof(gasPerBlock), $"GasPerBlock must be between [0, {10 * GAS.Factor}]");
289-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
289+
AssertCommittee(engine);
290290

291291
var index = engine.PersistingBlock.Index + 1;
292292
var entry = engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_GasPerBlock, index), () => new StorageItem(gasPerBlock));
@@ -314,7 +314,8 @@ private void SetRegisterPrice(ApplicationEngine engine, long registerPrice)
314314
{
315315
if (registerPrice <= 0)
316316
throw new ArgumentOutOfRangeException(nameof(registerPrice), "RegisterPrice must be positive");
317-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
317+
AssertCommittee(engine);
318+
318319
engine.SnapshotCache.GetAndChange(_registerPrice).Set(registerPrice);
319320
}
320321

src/Neo/SmartContract/Native/Notary.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,12 @@ private void SetMaxNotValidBeforeDelta(ApplicationEngine engine, uint value)
257257
{
258258
var maxVUBIncrement = engine.SnapshotCache.GetMaxValidUntilBlockIncrement(engine.ProtocolSettings);
259259
if (value > maxVUBIncrement / 2 || value < ProtocolSettings.Default.ValidatorsCount)
260+
{
260261
throw new FormatException(string.Format("MaxNotValidBeforeDelta cannot be more than {0} or less than {1}",
261262
maxVUBIncrement / 2, ProtocolSettings.Default.ValidatorsCount));
262-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
263+
}
264+
AssertCommittee(engine);
265+
263266
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_MaxNotValidBeforeDelta))!.Set(value);
264267
}
265268

src/Neo/SmartContract/Native/OracleContract.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ internal OracleContract() : base() { }
5959
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
6060
private void SetPrice(ApplicationEngine engine, long price)
6161
{
62-
if (price <= 0)
63-
throw new ArgumentOutOfRangeException(nameof(price), "Price must be positive");
64-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
62+
if (price <= 0) throw new ArgumentOutOfRangeException(nameof(price), "Price must be positive");
63+
AssertCommittee(engine);
64+
6565
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Price)).Set(price);
6666
}
6767

src/Neo/SmartContract/Native/PolicyContract.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ public bool IsBlocked(IReadOnlyStore snapshot, UInt160 account)
269269
public void SetMillisecondsPerBlock(ApplicationEngine engine, uint value)
270270
{
271271
if (value == 0 || value > MaxMillisecondsPerBlock)
272-
throw new ArgumentOutOfRangeException(nameof(value), $"MillisecondsPerBlock must be between 1 and {MaxMillisecondsPerBlock}, got {value}");
273-
if (!CheckCommittee(engine)) throw new InvalidOperationException("Invalid committee signature");
272+
throw new ArgumentOutOfRangeException(nameof(value), $"MillisecondsPerBlock must be between [1, {MaxMillisecondsPerBlock}], got {value}");
273+
AssertCommittee(engine);
274274

275275
var oldTime = GetMillisecondsPerBlock(engine.SnapshotCache);
276276
engine.SnapshotCache.GetAndChange(_millisecondsPerBlock).Set(value);
@@ -325,7 +325,7 @@ private void SetAttributeFee(ApplicationEngine engine, byte attributeType, uint
325325
if (value > MaxAttributeFee)
326326
throw new ArgumentOutOfRangeException(nameof(value), $"AttributeFee must be less than {MaxAttributeFee}");
327327

328-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
328+
AssertCommittee(engine);
329329

330330
engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_AttributeFee, attributeType), () => new StorageItem(DefaultAttributeFee)).Set(value);
331331
}
@@ -335,7 +335,8 @@ private void SetFeePerByte(ApplicationEngine engine, long value)
335335
{
336336
if (value < 0 || value > 1_00000000)
337337
throw new ArgumentOutOfRangeException(nameof(value), $"FeePerByte must be between [0, 100000000], got {value}");
338-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
338+
AssertCommittee(engine);
339+
339340
engine.SnapshotCache.GetAndChange(_feePerByte).Set(value);
340341
}
341342

@@ -344,7 +345,8 @@ private void SetExecFeeFactor(ApplicationEngine engine, uint value)
344345
{
345346
if (value == 0 || value > MaxExecFeeFactor)
346347
throw new ArgumentOutOfRangeException(nameof(value), $"ExecFeeFactor must be between [1, {MaxExecFeeFactor}], got {value}");
347-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
348+
AssertCommittee(engine);
349+
348350
engine.SnapshotCache.GetAndChange(_execFeeFactor).Set(value);
349351
}
350352

@@ -353,7 +355,8 @@ private void SetStoragePrice(ApplicationEngine engine, uint value)
353355
{
354356
if (value == 0 || value > MaxStoragePrice)
355357
throw new ArgumentOutOfRangeException(nameof(value), $"StoragePrice must be between [1, {MaxStoragePrice}], got {value}");
356-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
358+
AssertCommittee(engine);
359+
357360
engine.SnapshotCache.GetAndChange(_storagePrice).Set(value);
358361
}
359362

@@ -365,7 +368,8 @@ private void SetMaxValidUntilBlockIncrement(ApplicationEngine engine, uint value
365368
var mtb = GetMaxTraceableBlocks(engine.SnapshotCache);
366369
if (value >= mtb)
367370
throw new InvalidOperationException($"MaxValidUntilBlockIncrement must be lower than MaxTraceableBlocks ({value} vs {mtb})");
368-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
371+
AssertCommittee(engine);
372+
369373
engine.SnapshotCache.GetAndChange(_maxValidUntilBlockIncrement).Set(value);
370374
}
371375

@@ -379,26 +383,31 @@ private void SetMaxTraceableBlocks(ApplicationEngine engine, uint value)
379383
{
380384
if (value == 0 || value > MaxMaxTraceableBlocks)
381385
throw new ArgumentOutOfRangeException(nameof(value), $"MaxTraceableBlocks must be between [1, {MaxMaxTraceableBlocks}], got {value}");
386+
382387
var oldVal = GetMaxTraceableBlocks(engine.SnapshotCache);
383388
if (value > oldVal)
384389
throw new InvalidOperationException($"MaxTraceableBlocks can not be increased (old {oldVal}, new {value})");
390+
385391
var mVUBIncrement = GetMaxValidUntilBlockIncrement(engine.SnapshotCache);
386392
if (value <= mVUBIncrement)
387393
throw new InvalidOperationException($"MaxTraceableBlocks must be larger than MaxValidUntilBlockIncrement ({value} vs {mVUBIncrement})");
388-
if (!CheckCommittee(engine)) throw new InvalidOperationException("Invalid committee signature");
394+
395+
AssertCommittee(engine);
396+
389397
engine.SnapshotCache.GetAndChange(_maxTraceableBlocks).Set(value);
390398
}
391399

392400
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
393401
private bool BlockAccount(ApplicationEngine engine, UInt160 account)
394402
{
395-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
403+
AssertCommittee(engine);
404+
396405
return BlockAccount(engine.SnapshotCache, account);
397406
}
398407

399408
internal bool BlockAccount(DataCache snapshot, UInt160 account)
400409
{
401-
if (IsNative(account)) throw new InvalidOperationException("It's impossible to block a native contract.");
410+
if (IsNative(account)) throw new InvalidOperationException("Cannot block a native contract.");
402411

403412
var key = CreateStorageKey(Prefix_BlockedAccount, account);
404413
if (snapshot.Contains(key)) return false;
@@ -410,7 +419,8 @@ internal bool BlockAccount(DataCache snapshot, UInt160 account)
410419
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
411420
private bool UnblockAccount(ApplicationEngine engine, UInt160 account)
412421
{
413-
if (!CheckCommittee(engine)) throw new InvalidOperationException();
422+
AssertCommittee(engine);
423+
414424

415425
var key = CreateStorageKey(Prefix_BlockedAccount, account);
416426
if (!engine.SnapshotCache.Contains(key)) return false;

src/Neo/SmartContract/Native/RoleManagement.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] node
6868
throw new ArgumentException($"Nodes count {nodes.Length} must be between 1 and 32", nameof(nodes));
6969
if (!Enum.IsDefined(typeof(Role), role))
7070
throw new ArgumentOutOfRangeException(nameof(role), $"Role {role} is not valid");
71-
if (!CheckCommittee(engine))
72-
throw new InvalidOperationException("Invalid committee signature");
71+
AssertCommittee(engine);
72+
7373
if (engine.PersistingBlock is null)
7474
throw new InvalidOperationException("Persisting block is null");
7575
var index = engine.PersistingBlock.Index + 1;
7676
var key = CreateStorageKey((byte)role, index);
7777
if (engine.SnapshotCache.Contains(key))
78-
throw new InvalidOperationException();
78+
throw new InvalidOperationException("Role already designated");
79+
7980
NodeList list = new();
8081
list.AddRange(nodes);
8182
list.Sort();

0 commit comments

Comments
 (0)