-
Notifications
You must be signed in to change notification settings - Fork 716
XDC block producer #9512
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
Merged
Merged
XDC block producer #9512
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3b50c55
block producer
ak88 6fe8a95
format
ak88 0bc7152
test
ak88 e955a1a
Merge branch 'master' into feature/xdc-block-builder
ak88 4e63d9e
block production test
ak88 fda2b9b
format
ak88 c80df36
use the constant
ak88 83e7fbd
use timestamp from attributes
ak88 f746609
default for timestamp
ak88 42ced14
name
ak88 9159de5
Merge branch 'master' into feature/xdc-block-builder
ak88 8a3745a
merged
ak88 9cd3e4d
merge fix
ak88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using Nethermind.Blockchain; | ||
| using Nethermind.Config; | ||
| using Nethermind.Consensus; | ||
| using Nethermind.Consensus.Processing; | ||
| using Nethermind.Consensus.Producers; | ||
| using Nethermind.Consensus.Transactions; | ||
| using Nethermind.Core; | ||
| using Nethermind.Core.Crypto; | ||
| using Nethermind.Core.Specs; | ||
| using Nethermind.Evm.State; | ||
| using Nethermind.Evm.Tracing; | ||
| using Nethermind.Int256; | ||
| using Nethermind.Logging; | ||
| using Nethermind.Xdc.RLP; | ||
| using Nethermind.Xdc.Spec; | ||
| using Nethermind.Xdc.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Nethermind.Xdc; | ||
| internal class XdcBlockProducer : BlockProducerBase | ||
| { | ||
| private readonly IEpochSwitchManager epochSwitchManager; | ||
| private readonly ISnapshotManager snapshotManager; | ||
| private readonly XdcContext xdcContext; | ||
| private readonly ISealer sealer; | ||
| private readonly ISpecProvider specProvider; | ||
| private readonly ILogManager logManager; | ||
| private static readonly ExtraConsensusDataDecoder _extraConsensusDataDecoder = new(); | ||
|
|
||
| public XdcBlockProducer(IEpochSwitchManager epochSwitchManager, ISnapshotManager snapshotManager, XdcContext xdcContext, ITxSource txSource, IBlockchainProcessor processor, ISealer sealer, IBlockTree blockTree, IWorldState stateProvider, IGasLimitCalculator? gasLimitCalculator, ITimestamper? timestamper, ISpecProvider specProvider, ILogManager logManager, IDifficultyCalculator? difficultyCalculator, IBlocksConfig? blocksConfig) : base(txSource, processor, sealer, blockTree, stateProvider, gasLimitCalculator, timestamper, specProvider, logManager, difficultyCalculator, blocksConfig) | ||
| { | ||
| this.epochSwitchManager = epochSwitchManager; | ||
| this.snapshotManager = snapshotManager; | ||
| this.xdcContext = xdcContext; | ||
| this.sealer = sealer; | ||
| this.specProvider = specProvider; | ||
| this.logManager = logManager; | ||
| } | ||
|
|
||
| protected override BlockHeader PrepareBlockHeader(BlockHeader parent, PayloadAttributes? payloadAttributes) | ||
| { | ||
| if (parent is not XdcBlockHeader xdcParent) | ||
| throw new ArgumentException("Only XDC header are supported."); | ||
|
|
||
| QuorumCertificate highestCert = xdcContext.HighestQC; | ||
| var currentRound = xdcContext.CurrentRound; | ||
|
|
||
| //TODO maybe some sanity checks here for round and hash | ||
|
|
||
| byte[] extra = [2, .. _extraConsensusDataDecoder.Encode(new ExtraFieldsV2(currentRound, highestCert)).Bytes]; | ||
|
|
||
| Address blockAuthor = sealer.Address; | ||
| XdcBlockHeader xdcBlockHeader = new( | ||
| parent.Hash!, | ||
| Keccak.OfAnEmptySequenceRlp, | ||
| blockAuthor, | ||
| UInt256.Zero, | ||
| parent.Number + 1, | ||
| XdcConstants.TargetGasLimit, | ||
| 0, | ||
| extra) | ||
| { | ||
| Author = blockAuthor, | ||
| }; | ||
|
|
||
| IXdcReleaseSpec spec = specProvider.GetXdcSpec(xdcBlockHeader, currentRound); | ||
|
|
||
| xdcBlockHeader.Timestamp = parent.Timestamp + (ulong)spec.MinePeriod; | ||
|
|
||
| parent.Difficulty = 1; | ||
|
|
||
| xdcBlockHeader.BaseFeePerGas = BaseFeeCalculator.Calculate(parent, spec); | ||
|
|
||
| if (epochSwitchManager.IsEpochSwitch(xdcBlockHeader, out _)) | ||
| { | ||
| (Address[] masternodes, Address[] penalties) = snapshotManager.CalculateNextEpochMasternodes(xdcBlockHeader, spec); | ||
|
|
||
| xdcBlockHeader.Validators = new byte[masternodes.Length * Address.Size]; | ||
|
|
||
| for (int i = 0; i < masternodes.Length; i++) | ||
| { | ||
| Array.Copy(masternodes[i].Bytes, 0, xdcBlockHeader.Validators, i * Address.Size, Address.Size); | ||
| } | ||
|
|
||
| xdcBlockHeader.Penalties = new byte[penalties.Length * Address.Size]; | ||
|
|
||
| for (int i = 0; i < penalties.Length; i++) | ||
| { | ||
| Array.Copy(penalties[i].Bytes, 0, xdcBlockHeader.Penalties, i * Address.Size, Address.Size); | ||
| } | ||
| } | ||
| return xdcBlockHeader; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to:
Have some base or helper method taking in generic for actual header type.
Reusing base implementation to fill the basic stuff.
Only ammend custom stuff in this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case there is very little work saved, since the constructor will set most of the fields and other fields has to be overridden in the XDC version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest I'm more concern about future changes that could be auto-applied for XDC and here will have to be manually ported