-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathBlockProducerEnvFactory.cs
160 lines (144 loc) · 7.12 KB
/
BlockProducerEnvFactory.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Receipts;
using Nethermind.Config;
using Nethermind.Consensus.Comparers;
using Nethermind.Consensus.ExecutionRequests;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.TxPool;
namespace Nethermind.Consensus.Producers
{
public class BlockProducerEnvFactory : IBlockProducerEnvFactory
{
protected readonly IWorldStateManager _worldStateManager;
protected readonly IBlockTree _blockTree;
protected readonly ISpecProvider _specProvider;
protected readonly IBlockValidator _blockValidator;
protected readonly IRewardCalculatorSource _rewardCalculatorSource;
protected readonly IReceiptStorage _receiptStorage;
protected readonly IBlockPreprocessorStep _blockPreprocessorStep;
protected readonly ITxPool _txPool;
protected readonly ITransactionComparerProvider _transactionComparerProvider;
protected readonly IBlocksConfig _blocksConfig;
protected readonly ILogManager _logManager;
private readonly IExecutionRequestsProcessor? _executionRequestsProcessor;
public IBlockTransactionsExecutorFactory TransactionsExecutorFactory { get; set; }
public BlockProducerEnvFactory(
IWorldStateManager worldStateManager,
IBlockTree blockTree,
ISpecProvider specProvider,
IBlockValidator blockValidator,
IRewardCalculatorSource rewardCalculatorSource,
IReceiptStorage receiptStorage,
IBlockPreprocessorStep blockPreprocessorStep,
ITxPool txPool,
ITransactionComparerProvider transactionComparerProvider,
IBlocksConfig blocksConfig,
ILogManager logManager,
IExecutionRequestsProcessor? executionRequestsProcessor = null)
{
_worldStateManager = worldStateManager;
_blockTree = blockTree;
_specProvider = specProvider;
_blockValidator = blockValidator;
_rewardCalculatorSource = rewardCalculatorSource;
_receiptStorage = receiptStorage;
_blockPreprocessorStep = blockPreprocessorStep;
_txPool = txPool;
_transactionComparerProvider = transactionComparerProvider;
_blocksConfig = blocksConfig;
_logManager = logManager;
_executionRequestsProcessor = executionRequestsProcessor;
TransactionsExecutorFactory = new BlockProducerTransactionsExecutorFactory(specProvider, logManager);
}
public virtual BlockProducerEnv Create(ITxSource? additionalTxSource = null)
{
ReadOnlyBlockTree readOnlyBlockTree = _blockTree.AsReadOnly();
ReadOnlyTxProcessingEnv txProcessingEnv =
CreateReadonlyTxProcessingEnv(_worldStateManager, readOnlyBlockTree);
IReadOnlyTxProcessingScope scope = txProcessingEnv.Build(Keccak.EmptyTreeHash);
BlockProcessor blockProcessor =
CreateBlockProcessor(
scope,
_specProvider,
_blockValidator,
_rewardCalculatorSource,
_receiptStorage,
_logManager,
_blocksConfig);
IBlockchainProcessor blockchainProcessor =
new BlockchainProcessor(
readOnlyBlockTree,
blockProcessor,
_blockPreprocessorStep,
txProcessingEnv.StateReader,
_logManager,
BlockchainProcessor.Options.NoReceipts);
OneTimeChainProcessor chainProcessor = new(
scope.WorldState,
blockchainProcessor);
return new BlockProducerEnv
{
BlockTree = readOnlyBlockTree,
ChainProcessor = chainProcessor,
ReadOnlyStateProvider = scope.WorldState,
TxSource = CreateTxSourceForProducer(additionalTxSource, txProcessingEnv, _txPool, _blocksConfig, _transactionComparerProvider, _logManager),
ReadOnlyTxProcessingEnv = txProcessingEnv
};
}
protected virtual ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(IWorldStateManager worldStateManager, ReadOnlyBlockTree readOnlyBlockTree) =>
new(worldStateManager, readOnlyBlockTree, _specProvider, _logManager);
protected virtual ITxSource CreateTxSourceForProducer(
ITxSource? additionalTxSource,
ReadOnlyTxProcessingEnv processingEnv,
ITxPool txPool,
IBlocksConfig blocksConfig,
ITransactionComparerProvider transactionComparerProvider,
ILogManager logManager)
{
TxPoolTxSource txPoolSource = CreateTxPoolTxSource(processingEnv, txPool, blocksConfig, transactionComparerProvider, logManager);
InclusionListTxSource inclusionListTxSource = new(_specProvider.ChainId);
return additionalTxSource.Then(txPoolSource).Then(inclusionListTxSource);
}
protected virtual TxPoolTxSource CreateTxPoolTxSource(
ReadOnlyTxProcessingEnv processingEnv,
ITxPool txPool,
IBlocksConfig blocksConfig,
ITransactionComparerProvider transactionComparerProvider,
ILogManager logManager)
=> new TxPoolTxSourceFactory(txPool, _specProvider, transactionComparerProvider, blocksConfig, logManager).Create();
protected virtual BlockProcessor CreateBlockProcessor(
IReadOnlyTxProcessingScope readOnlyTxProcessingEnv,
ISpecProvider specProvider,
IBlockValidator blockValidator,
IRewardCalculatorSource rewardCalculatorSource,
IReceiptStorage receiptStorage,
ILogManager logManager,
IBlocksConfig blocksConfig) =>
new(specProvider,
blockValidator,
rewardCalculatorSource.Get(readOnlyTxProcessingEnv.TransactionProcessor),
TransactionsExecutorFactory.Create(readOnlyTxProcessingEnv),
readOnlyTxProcessingEnv.WorldState,
receiptStorage,
readOnlyTxProcessingEnv.TransactionProcessor,
new BeaconBlockRootHandler(readOnlyTxProcessingEnv.TransactionProcessor, readOnlyTxProcessingEnv.WorldState),
new BlockhashStore(_specProvider, readOnlyTxProcessingEnv.WorldState),
logManager,
new BlockProductionWithdrawalProcessor(new WithdrawalProcessor(readOnlyTxProcessingEnv.WorldState, logManager)),
executionRequestsProcessor: _executionRequestsProcessor
);
}
}