-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Dynamically adjust fees #1453
Closed
Closed
Dynamically adjust fees #1453
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e70a210
dynamically adjust fees
8a0f0f2
Merge remote-tracking branch 'upstream/master' into dynamic-fee
cabc08f
add initiated value and modify set logic
b360a3f
remove initiate
0a4b436
minor modify
8c074f0
minor modify
c770656
format
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#pragma warning disable IDE0051 | ||
#pragma warning disable IDE0060 | ||
|
||
using Neo.Ledger; | ||
using Neo.Persistence; | ||
using Neo.SmartContract.Manifest; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
using Array = Neo.VM.Types.Array; | ||
|
||
namespace Neo.SmartContract.Native | ||
{ | ||
public sealed class FeeContract : NativeContract | ||
{ | ||
public override string ServiceName => "Neo.Native.Fee"; | ||
public override int Id => -4; | ||
|
||
private const byte Prefix_Ratio = 11; | ||
private const byte Prefix_Syscall = 12; | ||
private const byte Prefix_OpCode = 13; | ||
|
||
private const uint DefaultRatio = 1; | ||
|
||
public FeeContract() | ||
{ | ||
Manifest.Features = ContractFeatures.HasStorage; | ||
} | ||
|
||
internal override bool Initialize(ApplicationEngine engine) | ||
{ | ||
if (!base.Initialize(engine)) return false; | ||
engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_Ratio), new StorageItem | ||
{ | ||
Value = BitConverter.GetBytes(DefaultRatio) | ||
}); | ||
return true; | ||
} | ||
|
||
private bool CheckValidators(ApplicationEngine engine) | ||
{ | ||
UInt256 prev_hash = engine.Snapshot.PersistingBlock.PrevHash; | ||
TrimmedBlock prev_block = engine.Snapshot.Blocks[prev_hash]; | ||
return InteropService.Runtime.CheckWitnessInternal(engine, prev_block.NextConsensus); | ||
} | ||
|
||
[ContractMethod(0_00010000, ContractParameterType.Integer, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })] | ||
private StackItem GetSyscallPrice(ApplicationEngine engine, Array args) | ||
{ | ||
uint method = (uint)args[0].GetBigInteger(); | ||
return GetSyscallPrice(method, engine.Snapshot); | ||
} | ||
|
||
public long GetSyscallPrice(uint method, StoreView snapshot, EvaluationStack stack = null) | ||
{ | ||
if (snapshot.Storages.TryGet(CreateStorageKey(Prefix_Syscall, BitConverter.GetBytes(method))) != null) | ||
{ | ||
return BitConverter.ToInt64(snapshot.Storages[CreateStorageKey(Prefix_Syscall, BitConverter.GetBytes(method))].Value, 0) / GetRatio(snapshot); | ||
} | ||
return InteropService.GetPrice(method, stack) / GetRatio(snapshot); | ||
} | ||
|
||
[ContractMethod(0_00030000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Array }, ParameterNames = new[] { "value" })] | ||
private StackItem SetSyscallPrice(ApplicationEngine engine, Array args) | ||
{ | ||
if (!CheckValidators(engine)) return false; | ||
uint method = (uint)((Array)args[0])[0].GetBigInteger(); | ||
long value = (long)((Array)args[0])[1].GetBigInteger(); | ||
if (value < 0) return false; | ||
StorageKey key = CreateStorageKey(Prefix_Syscall, BitConverter.GetBytes(method)); | ||
StorageItem item = engine.Snapshot.Storages.GetAndChange(key, () => new StorageItem()); | ||
item.Value = BitConverter.GetBytes(value); | ||
return true; | ||
} | ||
|
||
[ContractMethod(0_00010000, ContractParameterType.Integer, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })] | ||
private StackItem GetOpCodePrice(ApplicationEngine engine, Array args) | ||
{ | ||
uint opCode = (uint)args[0].GetBigInteger(); | ||
return GetOpCodePrice((OpCode)opCode, engine.Snapshot); | ||
} | ||
|
||
public long GetOpCodePrice(OpCode opCode, StoreView snapshot) | ||
{ | ||
if (snapshot.Storages.TryGet(CreateStorageKey(Prefix_OpCode, BitConverter.GetBytes((int)opCode))) != null) | ||
{ | ||
return BitConverter.ToInt64(snapshot.Storages[CreateStorageKey(Prefix_OpCode, BitConverter.GetBytes((int)opCode))].Value, 0) / GetRatio(snapshot); | ||
} | ||
return ApplicationEngine.OpCodePrices[opCode] / GetRatio(snapshot); | ||
} | ||
|
||
[ContractMethod(0_00030000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Array }, ParameterNames = new[] { "value" })] | ||
private StackItem SetOpCodePrice(ApplicationEngine engine, Array args) | ||
{ | ||
if (!CheckValidators(engine)) return false; | ||
uint opCode = (uint)((Array)args[0])[0].GetBigInteger(); | ||
long value = (long)((Array)args[0])[1].GetBigInteger(); | ||
if (value < 0) return false; | ||
StorageKey key = CreateStorageKey(Prefix_OpCode, BitConverter.GetBytes(opCode)); | ||
StorageItem item = engine.Snapshot.Storages.GetAndChange(key, () => new StorageItem()); | ||
item.Value = BitConverter.GetBytes(value); | ||
return true; | ||
} | ||
|
||
[ContractMethod(0_00010000, ContractParameterType.Integer, SafeMethod = true)] | ||
private StackItem GetRatio(ApplicationEngine engine, Array args) | ||
{ | ||
return GetRatio(engine.Snapshot); | ||
} | ||
|
||
public uint GetRatio(StoreView snapshot) | ||
{ | ||
if (snapshot.Storages.TryGet(CreateStorageKey(Prefix_Ratio)) != null) | ||
return BitConverter.ToUInt32(snapshot.Storages[CreateStorageKey(Prefix_Ratio)].Value, 0); | ||
else | ||
return DefaultRatio; | ||
} | ||
|
||
[ContractMethod(0_00030000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })] | ||
private StackItem SetRatio(ApplicationEngine engine, Array args) | ||
{ | ||
if (!CheckValidators(engine)) return false; | ||
uint value = (uint)args[0].GetBigInteger(); | ||
if (value == 0) return false; | ||
StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Ratio)); | ||
storage.Value = BitConverter.GetBytes(value); | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use GetRatio only, otherwise we will lose a big performance, other way is caching the prices, and refresh this cache when it's needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're concerned that some instructions' cost may be too high, which may need to be adjusted in the future.