/
PolicyContract.cs
205 lines (175 loc) · 8.71 KB
/
PolicyContract.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (C) 2015-2024 The Neo Project.
//
// PolicyContract.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
#pragma warning disable IDE0051
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using System;
using System.Numerics;
namespace Neo.SmartContract.Native
{
/// <summary>
/// A native contract that manages the system policies.
/// </summary>
public sealed class PolicyContract : NativeContract
{
/// <summary>
/// The default execution fee factor.
/// </summary>
public const uint DefaultExecFeeFactor = 30;
/// <summary>
/// The default storage price.
/// </summary>
public const uint DefaultStoragePrice = 100000;
/// <summary>
/// The default network fee per byte of transactions.
/// </summary>
public const uint DefaultFeePerByte = 1000;
/// <summary>
/// The default fee for attribute.
/// </summary>
public const uint DefaultAttributeFee = 0;
/// <summary>
/// The maximum execution fee factor that the committee can set.
/// </summary>
public const uint MaxExecFeeFactor = 100;
/// <summary>
/// The maximum fee for attribute that the committee can set.
/// </summary>
public const uint MaxAttributeFee = 10_0000_0000;
/// <summary>
/// The maximum storage price that the committee can set.
/// </summary>
public const uint MaxStoragePrice = 10000000;
private const byte Prefix_BlockedAccount = 15;
private const byte Prefix_FeePerByte = 10;
private const byte Prefix_ExecFeeFactor = 18;
private const byte Prefix_StoragePrice = 19;
private const byte Prefix_AttributeFee = 20;
internal PolicyContract() : base() { }
internal override ContractTask InitializeAsync(ApplicationEngine engine, Hardfork? hardfork)
{
if (hardfork == ActiveIn)
{
engine.Snapshot.Add(CreateStorageKey(Prefix_FeePerByte), new StorageItem(DefaultFeePerByte));
engine.Snapshot.Add(CreateStorageKey(Prefix_ExecFeeFactor), new StorageItem(DefaultExecFeeFactor));
engine.Snapshot.Add(CreateStorageKey(Prefix_StoragePrice), new StorageItem(DefaultStoragePrice));
}
return ContractTask.CompletedTask;
}
/// <summary>
/// Gets the network fee per transaction byte.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <returns>The network fee per transaction byte.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public long GetFeePerByte(DataCache snapshot)
{
return (long)(BigInteger)snapshot[CreateStorageKey(Prefix_FeePerByte)];
}
/// <summary>
/// Gets the execution fee factor. This is a multiplier that can be adjusted by the committee to adjust the system fees for transactions.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <returns>The execution fee factor.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public uint GetExecFeeFactor(DataCache snapshot)
{
return (uint)(BigInteger)snapshot[CreateStorageKey(Prefix_ExecFeeFactor)];
}
/// <summary>
/// Gets the storage price.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <returns>The storage price.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public uint GetStoragePrice(DataCache snapshot)
{
return (uint)(BigInteger)snapshot[CreateStorageKey(Prefix_StoragePrice)];
}
/// <summary>
/// Gets the fee for attribute.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="attributeType">Attribute type</param>
/// <returns>The fee for attribute.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public uint GetAttributeFee(DataCache snapshot, byte attributeType)
{
if (!Enum.IsDefined(typeof(TransactionAttributeType), attributeType)) throw new InvalidOperationException();
StorageItem entry = snapshot.TryGet(CreateStorageKey(Prefix_AttributeFee).Add(attributeType));
if (entry == null) return DefaultAttributeFee;
return (uint)(BigInteger)entry;
}
/// <summary>
/// Determines whether the specified account is blocked.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="account">The account to be checked.</param>
/// <returns><see langword="true"/> if the account is blocked; otherwise, <see langword="false"/>.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public bool IsBlocked(DataCache snapshot, UInt160 account)
{
return snapshot.Contains(CreateStorageKey(Prefix_BlockedAccount).Add(account));
}
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetAttributeFee(ApplicationEngine engine, byte attributeType, uint value)
{
if (!Enum.IsDefined(typeof(TransactionAttributeType), attributeType)) throw new InvalidOperationException();
if (value > MaxAttributeFee) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_AttributeFee).Add(attributeType), () => new StorageItem(DefaultAttributeFee)).Set(value);
}
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetFeePerByte(ApplicationEngine engine, long value)
{
if (value < 0 || value > 1_00000000) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_FeePerByte)).Set(value);
}
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetExecFeeFactor(ApplicationEngine engine, uint value)
{
if (value == 0 || value > MaxExecFeeFactor) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_ExecFeeFactor)).Set(value);
}
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetStoragePrice(ApplicationEngine engine, uint value)
{
if (value == 0 || value > MaxStoragePrice) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_StoragePrice)).Set(value);
}
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private bool BlockAccount(ApplicationEngine engine, UInt160 account)
{
if (!CheckCommittee(engine)) throw new InvalidOperationException();
return BlockAccount(engine.Snapshot, account);
}
internal bool BlockAccount(DataCache snapshot, UInt160 account)
{
if (IsNative(account)) throw new InvalidOperationException("It's impossible to block a native contract.");
var key = CreateStorageKey(Prefix_BlockedAccount).Add(account);
if (snapshot.Contains(key)) return false;
snapshot.Add(key, new StorageItem(Array.Empty<byte>()));
return true;
}
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private bool UnblockAccount(ApplicationEngine engine, UInt160 account)
{
if (!CheckCommittee(engine)) throw new InvalidOperationException();
var key = CreateStorageKey(Prefix_BlockedAccount).Add(account);
if (!engine.Snapshot.Contains(key)) return false;
engine.Snapshot.Delete(key);
return true;
}
}
}