-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathGenesisLoader.cs
94 lines (79 loc) · 3.54 KB
/
GenesisLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Linq;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Crypto;
using Nethermind.Int256;
using Nethermind.Evm;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.State;
namespace Nethermind.Blockchain
{
public class GenesisLoader(
ChainSpec chainSpec,
ISpecProvider specProvider,
IWorldState stateProvider,
ITransactionProcessor transactionProcessor)
{
private readonly ChainSpec _chainSpec = chainSpec ?? throw new ArgumentNullException(nameof(chainSpec));
private readonly ISpecProvider _specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
private readonly IWorldState _stateProvider = stateProvider ?? throw new ArgumentNullException(nameof(stateProvider));
private readonly ITransactionProcessor _transactionProcessor = transactionProcessor ?? throw new ArgumentNullException(nameof(transactionProcessor));
public Block Load()
{
Block genesis = _chainSpec.Genesis;
Preallocate(genesis);
// we no longer need the allocations - 0.5MB RAM, 9000 objects for mainnet
_chainSpec.Allocations = null;
if (!_chainSpec.GenesisStateUnavailable)
{
_stateProvider.Commit(_specProvider.GenesisSpec, true);
_stateProvider.CommitTree(0);
genesis.Header.StateRoot = _stateProvider.StateRoot;
}
genesis.Header.Hash = genesis.Header.CalculateHash();
return genesis;
}
private void Preallocate(Block genesis)
{
foreach ((Address address, ChainSpecAllocation allocation) in _chainSpec.Allocations.OrderBy(static a => a.Key))
{
_stateProvider.CreateAccount(address, allocation.Balance, allocation.Nonce);
if (allocation.Code is not null)
{
_stateProvider.InsertCode(address, allocation.Code, _specProvider.GenesisSpec, true);
}
if (allocation.Storage is not null)
{
foreach (KeyValuePair<UInt256, byte[]> storage in allocation.Storage)
{
_stateProvider.Set(new StorageCell(address, storage.Key),
new StorageValue(storage.Value));
}
}
if (allocation.Constructor is not null)
{
Transaction constructorTransaction = new SystemTransaction()
{
SenderAddress = address,
Data = allocation.Constructor,
GasLimit = genesis.GasLimit
};
CallOutputTracer outputTracer = new();
_transactionProcessor.Execute(constructorTransaction, new BlockExecutionContext(genesis.Header, specProvider.GetSpec(genesis.Header)), outputTracer);
if (outputTracer.StatusCode != StatusCode.Success)
{
throw new InvalidOperationException(
$"Failed to initialize constructor for address {address}. Error: {outputTracer.Error}");
}
}
}
}
}
}