-
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
Optimize OnPersists #3146
Closed
Closed
Optimize OnPersists #3146
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5edc48f
Optimize OnPersists
shargon 4541116
Same don't hurt
shargon 6c2ae90
Merge branch 'master' into core-optimize-persist
vncoelho 952241c
Merge branch 'master' into core-optimize-persist
shargon 80fa2be
Update src/Neo/SmartContract/Native/NeoToken.cs
shargon 277a2b8
Merge branch 'master' into core-optimize-persist
Jim8y 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ public sealed class NeoToken : FungibleToken<NeoToken.NeoAccountState> | |
private const byte Prefix_Candidate = 33; | ||
private const byte Prefix_Committee = 14; | ||
private const byte Prefix_GasPerBlock = 29; | ||
private const byte Prefix_GasPerBlockCurrent = 30; | ||
private const byte Prefix_RegisterPrice = 13; | ||
private const byte Prefix_VoterRewardPerCommittee = 23; | ||
|
||
|
@@ -187,6 +188,7 @@ internal override ContractTask InitializeAsync(ApplicationEngine engine, Hardfor | |
engine.Snapshot.Add(CreateStorageKey(Prefix_Committee), new StorageItem(cachedCommittee)); | ||
engine.Snapshot.Add(CreateStorageKey(Prefix_VotersCount), new StorageItem(System.Array.Empty<byte>())); | ||
engine.Snapshot.Add(CreateStorageKey(Prefix_GasPerBlock).AddBigEndian(0u), new StorageItem(5 * GAS.Factor)); | ||
engine.Snapshot.Add(CreateStorageKey(Prefix_GasPerBlockCurrent), new StorageItem(new GasPerBlock() { Gas = 5 * GAS.Factor, PreviousGas = 5 * GAS.Factor, Index = 0 })); | ||
engine.Snapshot.Add(CreateStorageKey(Prefix_RegisterPrice), new StorageItem(1000 * GAS.Factor)); | ||
return Mint(engine, Contract.GetBFTAddress(engine.ProtocolSettings.StandbyValidators), TotalAmount, false); | ||
} | ||
|
@@ -265,9 +267,18 @@ private void SetGasPerBlock(ApplicationEngine engine, BigInteger gasPerBlock) | |
throw new ArgumentOutOfRangeException(nameof(gasPerBlock)); | ||
if (!CheckCommittee(engine)) throw new InvalidOperationException(); | ||
|
||
uint index = engine.PersistingBlock.Index + 1; | ||
StorageItem entry = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_GasPerBlock).AddBigEndian(index), () => new StorageItem(gasPerBlock)); | ||
var gp = new GasPerBlock() { Gas = gasPerBlock, Index = engine.PersistingBlock.Index + 1 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one argument is missing here, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
var entry = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_GasPerBlock).AddBigEndian(gp.Index), () => new StorageItem(gasPerBlock)); | ||
entry.Set(gasPerBlock); | ||
|
||
// Set current entry | ||
|
||
entry = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_GasPerBlockCurrent), () => new StorageItem(gp)); | ||
var gpCurrent = entry.GetInteroperable<GasPerBlock>(); | ||
gpCurrent.PreviousGas = gpCurrent.Gas; | ||
gpCurrent.Gas = gasPerBlock; | ||
gpCurrent.Index = gp.Index; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -278,7 +289,18 @@ private void SetGasPerBlock(ApplicationEngine engine, BigInteger gasPerBlock) | |
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)] | ||
public BigInteger GetGasPerBlock(DataCache snapshot) | ||
{ | ||
return GetSortedGasRecords(snapshot, Ledger.CurrentIndex(snapshot) + 1).First().GasPerBlock; | ||
var gp = snapshot.TryGet(CreateStorageKey(Prefix_GasPerBlockCurrent)).GetInteroperable<GasPerBlock>(); | ||
|
||
if (gp.Index <= Ledger.CurrentIndex(snapshot)) | ||
{ | ||
return gp.Gas; | ||
} | ||
else | ||
{ | ||
return gp.PreviousGas; | ||
} | ||
|
||
//return GetSortedGasRecords(snapshot, Ledger.CurrentIndex(snapshot) + 1).First().GasPerBlock; | ||
} | ||
|
||
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)] | ||
|
@@ -625,6 +647,26 @@ protected override StackItem ElementToStackItem((ECPoint PublicKey, BigInteger V | |
} | ||
} | ||
|
||
private record GasPerBlock : IInteroperable | ||
{ | ||
public uint Index; | ||
public BigInteger Gas; | ||
public BigInteger PreviousGas; | ||
|
||
public void FromStackItem(StackItem stackItem) | ||
{ | ||
Struct @struct = (Struct)stackItem; | ||
Index = (uint)@struct[0].GetInteger(); | ||
Gas = @struct[1].GetInteger(); | ||
PreviousGas = @struct[2].GetInteger(); | ||
} | ||
|
||
public StackItem ToStackItem(ReferenceCounter referenceCounter) | ||
{ | ||
return new Struct(referenceCounter) { Index, Gas, PreviousGas }; | ||
} | ||
} | ||
|
||
private record GasDistribution | ||
{ | ||
public UInt160 Account { get; init; } | ||
|
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.
Please, do not change the storage scheme, it's very easy to cache the value.