Skip to content

Instantly share code, notes, and snippets.

@riemannulus
Created May 4, 2023 12:16
Show Gist options
  • Save riemannulus/cd368762325f99caf962090fefa9dee1 to your computer and use it in GitHub Desktop.
Save riemannulus/cd368762325f99caf962090fefa9dee1 to your computer and use it in GitHub Desktop.
Example of StateDeltaImpl.cs
class StateDeltaImpl : IAccountStateDelta, IValidatorStateDelta, IGasStateDelta {
[Pure]
IReadOnlyList<IValue?> IAccountStateView.GetStates(IReadOnlyList<Address> addresses)
{
ActionContext.GetStateTimer.Value?.Start();
int length = addresses.Count;
// note gas is does not static value
AddGas(length)
ActionContext.GetStateCount.Value += length;
IValue?[] values = new IValue?[length];
var notFoundIndices = new List<int>(length);
for (int i = 0; i < length; i++)
{
Address address = addresses[i];
if (UpdatedStates.TryGetValue(address, out IValue? updatedValue))
{
values[i] = updatedValue;
AddGas(updatedValue.length);
}
else
{
notFoundIndices.Add(i);
}
}
if (notFoundIndices.Count > 0)
{
IReadOnlyList<IValue?> restValues = StateGetter(
notFoundIndices.Select(index => addresses[index]).ToArray());
foreach ((var v, var i) in notFoundIndices.Select((v, i) => (v, i)))
{
values[v] = restValues[i];
AddGas(restValues[i].length);
}
}
ActionContext.GetStateTimer.Value?.Stop();
return values;
}
[Pure]
long IGasStateView.AddGas(long gas)
{
UsedGas += gas;
if(UsedGas >= GasLimit)
{
throw new OutOfGasException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment