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

Adjust the Charge for Storage Writting #1441

Merged
merged 60 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
805d370
Rebase
lock9 Dec 16, 2019
21ced64
Refactor, more tests
lock9 Dec 17, 2019
a376c48
Discarding changes
lock9 Dec 17, 2019
0239d04
Fixes, tests and refactor
lock9 Dec 19, 2019
92b256a
Calculating final fee during onPersist
lock9 Dec 19, 2019
26c50fa
Rebase
lock9 Dec 19, 2019
1fb19e4
dotnet format + missing change
lock9 Dec 19, 2019
daa4e2b
Minor improvements
lock9 Dec 19, 2019
42c6dd3
fix
lock9 Dec 19, 2019
c2aa683
Removing empty lines
lock9 Dec 19, 2019
cb33f47
add comment
eryeer Feb 6, 2020
50762dc
merge conflict
eryeer Feb 6, 2020
6238206
remove checkStorageContext
eryeer Feb 6, 2020
a600683
update ut
eryeer Feb 6, 2020
b5b0fb4
revert modification
eryeer Feb 7, 2020
96aca15
revert
eryeer Feb 7, 2020
5074592
add recycle reward gas
eryeer Feb 7, 2020
764c67d
update
eryeer Feb 7, 2020
54efe6a
fix ut
eryeer Feb 7, 2020
3174c0d
fix
eryeer Feb 7, 2020
51643d7
add OnRecycleRewardGas test
eryeer Feb 7, 2020
7131999
optimise
eryeer Feb 10, 2020
b5dac3d
update
eryeer Feb 10, 2020
1da5ebf
optimise
eryeer Feb 11, 2020
3996853
Merge branch 'master' into storage-payback
eryeer Feb 11, 2020
dfc3d7a
Removing extra space
vncoelho Feb 11, 2020
a856d44
update comment
eryeer Feb 12, 2020
ddac1f5
update static
eryeer Feb 12, 2020
5c61cbd
format
eryeer Feb 12, 2020
cbb8531
revert virtual
eryeer Feb 14, 2020
c661c41
format
eryeer Feb 14, 2020
1bf3815
update
eryeer Feb 18, 2020
5be0d71
remove ThrowIfNot
eryeer Feb 18, 2020
3d21376
update TryGet and engine notify
eryeer Feb 19, 2020
938af95
format
eryeer Feb 19, 2020
c39bb89
format
eryeer Feb 19, 2020
b8a6824
Update ApplicationEngine.cs
shargon Feb 19, 2020
ca9ab68
update mint
eryeer Feb 20, 2020
1196f86
Merge branch 'storage-payback' of github.com:eryeer/neoUT into storag…
eryeer Feb 20, 2020
d233346
format
eryeer Feb 20, 2020
e7c5d23
Fix typo
shargon Feb 20, 2020
e567bd3
Simplify
shargon Feb 20, 2020
45eaf5e
Merge branch 'master' into storage-payback
eryeer Feb 21, 2020
8eb8cf4
Merge branch 'master' into storage-payback
eryeer Feb 27, 2020
ed1d0d6
Merge branch 'master' into storage-payback
eryeer Feb 28, 2020
7134701
reset, charge only for increment of data, add put basic fee
Mar 3, 2020
1cd0e25
fix ut
Mar 3, 2020
7b0ad73
fix ut
Mar 3, 2020
d638d5c
Merge branch 'master' into storage-payback
Mar 4, 2020
0fe0c2e
Clean
shargon Mar 4, 2020
0e0b63c
add throw in GetStoragePrice if non-interface
Mar 5, 2020
bec4313
Merge branch 'storage-payback' of https://github.com/eryeer/neoUT int…
Mar 5, 2020
a4893df
fix ut
Mar 5, 2020
8acfe41
Merge branch 'master' into storage-payback
eryeer Mar 10, 2020
ffad20a
fix GetPrice parameters
Mar 11, 2020
c9e6b7a
Merge branch 'master' into storage-payback
Mar 12, 2020
2b1939e
Update InteropService.Storage.cs
erikzhang Mar 12, 2020
d683efd
add check: skeyValue.Value is null
Mar 12, 2020
073849f
reset, fix ut
Mar 12, 2020
5242131
adjust Storage.Delete price = 1 * GasPerByte
Mar 12, 2020
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
3 changes: 2 additions & 1 deletion src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public partial class ApplicationEngine : ExecutionEngine
public IVerifiable ScriptContainer { get; }
public StoreView Snapshot { get; }
public long GasConsumed { get; private set; } = 0;

public UInt160 CurrentScriptHash => CurrentContext?.GetState<ExecutionContextState>().ScriptHash;
public UInt160 CallingScriptHash => CurrentContext?.GetState<ExecutionContextState>().CallingScriptHash;
public UInt160 EntryScriptHash => EntryContext?.GetState<ExecutionContextState>().ScriptHash;
Expand Down Expand Up @@ -78,7 +79,7 @@ public override void Dispose()

protected override bool OnSysCall(uint method)
{
if (!AddGas(InteropService.GetPrice(method, CurrentContext.EvaluationStack)))
if (!AddGas(InteropService.GetPrice(method, CurrentContext.EvaluationStack, Snapshot)))
return false;
return InteropService.Invoke(this, method);
}
Expand Down
9 changes: 5 additions & 4 deletions src/neo/SmartContract/InteropDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.Persistence;
using Neo.VM;
using System;

Expand All @@ -9,7 +10,7 @@ public class InteropDescriptor
public uint Hash { get; }
internal Func<ApplicationEngine, bool> Handler { get; }
public long Price { get; }
public Func<EvaluationStack, long> PriceCalculator { get; }
public Func<EvaluationStack, StoreView, long> PriceCalculator { get; }
public TriggerType AllowedTriggers { get; }
public CallFlags RequiredCallFlags { get; }

Expand All @@ -19,7 +20,7 @@ internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler,
this.Price = price;
}

internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, long> priceCalculator, TriggerType allowedTriggers, CallFlags requiredCallFlags)
internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, StoreView, long> priceCalculator, TriggerType allowedTriggers, CallFlags requiredCallFlags)
: this(method, handler, allowedTriggers, requiredCallFlags)
{
this.PriceCalculator = priceCalculator;
Expand All @@ -34,9 +35,9 @@ private InteropDescriptor(string method, Func<ApplicationEngine, bool> handler,
this.RequiredCallFlags = requiredCallFlags;
}

public long GetPrice(EvaluationStack stack)
public long GetPrice(EvaluationStack stack, StoreView snapshot)
{
return PriceCalculator is null ? Price : PriceCalculator(stack);
return PriceCalculator is null ? Price : PriceCalculator(stack, snapshot);
}

public static implicit operator uint(InteropDescriptor descriptor)
Expand Down
3 changes: 2 additions & 1 deletion src/neo/SmartContract/InteropService.Contract.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Neo.IO;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using Neo.VM;
using Neo.VM.Types;
Expand All @@ -19,7 +20,7 @@ public static class Contract
public static readonly InteropDescriptor CallEx = Register("System.Contract.CallEx", Contract_CallEx, 0_01000000, TriggerType.System | TriggerType.Application, CallFlags.AllowCall);
public static readonly InteropDescriptor IsStandard = Register("System.Contract.IsStandard", Contract_IsStandard, 0_00030000, TriggerType.All, CallFlags.None);

private static long GetDeploymentPrice(EvaluationStack stack)
private static long GetDeploymentPrice(EvaluationStack stack, StoreView snapshot)
{
int size = stack.Peek(0).GetByteLength() + stack.Peek(1).GetByteLength();
return Storage.GasPerByte * size;
Expand Down
3 changes: 2 additions & 1 deletion src/neo/SmartContract/InteropService.Crypto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Neo.Cryptography;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.VM;
using Neo.VM.Types;
using System;
Expand All @@ -16,7 +17,7 @@ public static class Crypto
public static readonly InteropDescriptor ECDsaVerify = Register("Neo.Crypto.ECDsaVerify", Crypto_ECDsaVerify, 0_01000000, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor ECDsaCheckMultiSig = Register("Neo.Crypto.ECDsaCheckMultiSig", Crypto_ECDsaCheckMultiSig, GetECDsaCheckMultiSigPrice, TriggerType.All, CallFlags.None);

private static long GetECDsaCheckMultiSigPrice(EvaluationStack stack)
private static long GetECDsaCheckMultiSigPrice(EvaluationStack stack, StoreView snapshot)
{
if (stack.Count < 2) return 0;
var item = stack.Peek(1);
Expand Down
26 changes: 23 additions & 3 deletions src/neo/SmartContract/InteropService.Storage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract.Iterators;
using Neo.VM;
using Neo.VM.Types;
Expand All @@ -22,11 +23,30 @@ public static class Storage
public static readonly InteropDescriptor Find = Register("System.Storage.Find", Storage_Find, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor Put = Register("System.Storage.Put", Storage_Put, GetStoragePrice, TriggerType.Application, CallFlags.AllowModifyStates);
public static readonly InteropDescriptor PutEx = Register("System.Storage.PutEx", Storage_PutEx, GetStoragePrice, TriggerType.Application, CallFlags.AllowModifyStates);
public static readonly InteropDescriptor Delete = Register("System.Storage.Delete", Storage_Delete, 0_01000000, TriggerType.Application, CallFlags.AllowModifyStates);
public static readonly InteropDescriptor Delete = Register("System.Storage.Delete", Storage_Delete, 1 * GasPerByte, TriggerType.Application, CallFlags.AllowModifyStates);

private static long GetStoragePrice(EvaluationStack stack)
private static long GetStoragePrice(EvaluationStack stack, StoreView snapshot)
{
return (stack.Peek(1).GetByteLength() + stack.Peek(2).GetByteLength()) * GasPerByte;
var key = stack.Peek(1);
var value = stack.Peek(2);
var newDataSize = value.IsNull ? 0 : value.GetByteLength();
if (!(stack.Peek() is InteropInterface _interface))
throw new InvalidOperationException();

StorageContext context = _interface.GetInterface<StorageContext>();
StorageKey skey = new StorageKey
{
Id = context.Id,
Key = key.GetSpan().ToArray()
};
var skeyValue = snapshot.Storages.TryGet(skey);
if (skeyValue is null)
newDataSize += key.GetByteLength();
else if (newDataSize <= skeyValue.Value.Length)
newDataSize = 1;
shargon marked this conversation as resolved.
Show resolved Hide resolved
else
newDataSize -= skeyValue.Value.Length;
return newDataSize * GasPerByte;
}

private static bool PutExInternal(ApplicationEngine engine, StorageContext context, byte[] key, byte[] value, StorageFlags flags)
Expand Down
7 changes: 4 additions & 3 deletions src/neo/SmartContract/InteropService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.Persistence;
using Neo.VM;
using System;
using System.Collections.Generic;
Expand All @@ -15,9 +16,9 @@ static InteropService()
t.GetFields()[0].GetValue(null);
}

public static long GetPrice(uint hash, EvaluationStack stack)
public static long GetPrice(uint hash, EvaluationStack stack, StoreView snapshot)
{
return methods[hash].GetPrice(stack);
return methods[hash].GetPrice(stack, snapshot);
}

public static IEnumerable<InteropDescriptor> SupportedMethods()
Expand All @@ -44,7 +45,7 @@ private static InteropDescriptor Register(string method, Func<ApplicationEngine,
return descriptor;
}

private static InteropDescriptor Register(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, long> priceCalculator, TriggerType allowedTriggers, CallFlags requiredCallFlags)
private static InteropDescriptor Register(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, StoreView, long> priceCalculator, TriggerType allowedTriggers, CallFlags requiredCallFlags)
{
InteropDescriptor descriptor = new InteropDescriptor(method, handler, priceCalculator, allowedTriggers, requiredCallFlags);
methods.Add(descriptor.Hash, descriptor);
Expand Down
3 changes: 2 additions & 1 deletion src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Neo.IO;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native.Tokens;
using Neo.VM;
Expand Down Expand Up @@ -101,7 +102,7 @@ internal bool Invoke(ApplicationEngine engine)
return true;
}

internal long GetPrice(EvaluationStack stack)
internal long GetPrice(EvaluationStack stack, StoreView snapshot)
{
return methods.TryGetValue(stack.Peek().GetString(), out ContractMethodMetadata method) ? method.Price : 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public static long CalculateNetworkFee(byte[] witness_script, ref int size)
if (witness_script.IsSignatureContract())
{
size += 67 + witness_script.GetVarSize();
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + InteropService.GetPrice(InteropService.Crypto.ECDsaVerify, null);
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + InteropService.GetPrice(InteropService.Crypto.ECDsaVerify, null, null);
}
else if (witness_script.IsMultiSigContract(out int m, out int n))
{
Expand All @@ -366,7 +366,7 @@ public static long CalculateNetworkFee(byte[] witness_script, ref int size)
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * n;
using (ScriptBuilder sb = new ScriptBuilder())
networkFee += ApplicationEngine.OpCodePrices[(OpCode)sb.EmitPush(n).ToArray()[0]];
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + InteropService.GetPrice(InteropService.Crypto.ECDsaVerify, null) * n;
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + InteropService.GetPrice(InteropService.Crypto.ECDsaVerify, null, null) * n;
}
else
{
Expand Down
Loading