Skip to content

Commit

Permalink
storageTxTracer refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yerke26 committed Nov 1, 2024
1 parent fe8e3fe commit e978b0f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
13 changes: 9 additions & 4 deletions tools/Evm/Evm/t8n/StorageTxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ namespace Evm.t8n;

public class StorageTxTracer : TxTracer, IBlockTracer
{
public readonly Dictionary<Address, Dictionary<UInt256, byte[]>> Storages = new();
private readonly Dictionary<Address, Dictionary<UInt256, byte[]>> _storages = new();
public bool IsTracingRewards => false;
public override bool IsTracingOpLevelStorage => true;

public override void SetOperationStorage(Address address, UInt256 storageIndex, ReadOnlySpan<byte> newValue,
ReadOnlySpan<byte> currentValue)
{
if (!Storages.TryGetValue(address, out _))
if (!_storages.TryGetValue(address, out _))
{
Storages[address] = [];
_storages[address] = [];
}

Storages[address][storageIndex] = newValue.ToArray();
_storages[address][storageIndex] = newValue.ToArray();
}

public Dictionary<UInt256, byte[]>? GetStorage(Address address)
{
_storages.TryGetValue(address, out Dictionary<UInt256, byte[]>? storage);
return storage;
}

public void ReportReward(Address author, string rewardType, UInt256 rewardValue) { }

Expand Down
17 changes: 7 additions & 10 deletions tools/Evm/Evm/t8n/T8NResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@ public static T8NResult ConstructT8NResult(WorldState stateProvider,
private static Dictionary<Address, AccountState> CollectAccounts(T8NTest test, WorldState stateProvider, StorageTxTracer storageTracer, Block block)
{
Dictionary<Address, AccountState?> accounts = test.Alloc.Keys.ToDictionary(address => address,
address => GetAccountState(address, stateProvider, storageTracer.Storages));
address => GetAccountState(address, stateProvider, storageTracer));

accounts.AddRange(test.Ommers.ToDictionary(ommer => ommer.Address,
ommer => GetAccountState(ommer.Address, stateProvider, storageTracer.Storages)));
ommer => GetAccountState(ommer.Address, stateProvider, storageTracer)));

if (block.Beneficiary != null)
{
accounts[block.Beneficiary] = GetAccountState(block.Beneficiary, stateProvider, storageTracer.Storages);
accounts[block.Beneficiary] = GetAccountState(block.Beneficiary, stateProvider, storageTracer);
}

foreach (Transaction tx in test.Transactions)
{
if (tx.To is not null && !accounts.ContainsKey(tx.To))
{
accounts[tx.To] = GetAccountState(tx.To, stateProvider, storageTracer.Storages);
accounts[tx.To] = GetAccountState(tx.To, stateProvider, storageTracer);
}
if (tx.SenderAddress is not null && !accounts.ContainsKey(tx.SenderAddress))
{
accounts[tx.SenderAddress] = GetAccountState(tx.SenderAddress, stateProvider, storageTracer.Storages);
accounts[tx.SenderAddress] = GetAccountState(tx.SenderAddress, stateProvider, storageTracer);
}
}

Expand All @@ -107,7 +107,7 @@ private static Dictionary<Address, AccountState> CollectAccounts(T8NTest test, W
.ToDictionary(addressAndAccount => addressAndAccount.Key, addressAndAccount => addressAndAccount.Value!);
}

private static AccountState? GetAccountState(Address address, WorldState stateProvider, Dictionary<Address, Dictionary<UInt256, byte[]>> storages)
private static AccountState? GetAccountState(Address address, WorldState stateProvider, StorageTxTracer storageTxTracer)
{
if (!stateProvider.AccountExists(address)) return null;

Expand All @@ -120,10 +120,7 @@ private static Dictionary<Address, AccountState> CollectAccounts(T8NTest test, W
Code = code
};

if (storages.TryGetValue(address, out Dictionary<UInt256, byte[]>? storage))
{
accountState.Storage = storage;
}
accountState.Storage = storageTxTracer.GetStorage(address) ?? [];

return accountState;
}
Expand Down

0 comments on commit e978b0f

Please sign in to comment.