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

Optimize OnPersists #3146

Closed
wants to merge 6 commits into from
Closed
Changes from 5 commits
Commits
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
48 changes: 45 additions & 3 deletions src/Neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -184,6 +185,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 }));
Copy link
Contributor

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.

engine.Snapshot.Add(CreateStorageKey(Prefix_RegisterPrice), new StorageItem(1000 * GAS.Factor));
return Mint(engine, Contract.GetBFTAddress(engine.ProtocolSettings.StandbyValidators), TotalAmount, false);
}
Expand Down Expand Up @@ -262,9 +264,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 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one argument is missing here, no?
The correct constructor requires 3

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PreviousGas default value is 0


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>
Expand All @@ -275,7 +286,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)]
Expand Down Expand Up @@ -620,6 +642,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; }
Expand Down
Loading