public BlockHeader?Decode(RlpStream rlpStream, RlpBehaviors rlpBehaviors = RlpBehaviors.None) { if (rlpStream.IsNextItemNull()) { rlpStream.ReadByte(); return(null); } Span <byte> headerRlp = rlpStream.PeekNextItem(); int headerSequenceLength = rlpStream.ReadSequenceLength(); int headerCheck = rlpStream.Position + headerSequenceLength; Keccak? parentHash = rlpStream.DecodeKeccak(); Keccak? unclesHash = rlpStream.DecodeKeccak(); Address?beneficiary = rlpStream.DecodeAddress(); Keccak? stateRoot = rlpStream.DecodeKeccak(); Keccak? transactionsRoot = rlpStream.DecodeKeccak(); Keccak? receiptsRoot = rlpStream.DecodeKeccak(); Bloom? bloom = rlpStream.DecodeBloom(); UInt256 difficulty = rlpStream.DecodeUInt256(); long number = rlpStream.DecodeLong(); long gasLimit = rlpStream.DecodeLong(); long gasUsed = rlpStream.DecodeLong(); UInt256 timestamp = rlpStream.DecodeUInt256(); byte[]? extraData = rlpStream.DecodeByteArray(); BlockHeader blockHeader = new( parentHash, unclesHash, beneficiary, difficulty, number, gasLimit, timestamp, extraData) { StateRoot = stateRoot, TxRoot = transactionsRoot, ReceiptsRoot = receiptsRoot, Bloom = bloom, GasUsed = gasUsed, Hash = Keccak.Compute(headerRlp) }; if (rlpStream.PeekPrefixAndContentLength().ContentLength == Keccak.Size) { blockHeader.MixHash = rlpStream.DecodeKeccak(); blockHeader.Nonce = (ulong)rlpStream.DecodeUBigInt(); } else { blockHeader.AuRaStep = (long)rlpStream.DecodeUInt256(); blockHeader.AuRaSignature = rlpStream.DecodeByteArray(); } if (blockHeader.Number >= Eip1559TransitionBlock) { blockHeader.BaseFeePerGas = rlpStream.DecodeUInt256(); } if ((rlpBehaviors & RlpBehaviors.AllowExtraData) != RlpBehaviors.AllowExtraData) { rlpStream.Check(headerCheck); } return(blockHeader); }
public BlockHeader Decode(RlpStream rlpStream, RlpBehaviors rlpBehaviors = RlpBehaviors.None) { if (rlpStream.IsNextItemNull()) { rlpStream.ReadByte(); return(null); } var headerRlp = rlpStream.PeekNextItem(); int headerSequenceLength = rlpStream.ReadSequenceLength(); int headerCheck = rlpStream.Position + headerSequenceLength; Keccak parentHash = rlpStream.DecodeKeccak(); Keccak ommersHash = rlpStream.DecodeKeccak(); Address beneficiary = rlpStream.DecodeAddress(); Keccak stateRoot = rlpStream.DecodeKeccak(); Keccak transactionsRoot = rlpStream.DecodeKeccak(); Keccak receiptsRoot = rlpStream.DecodeKeccak(); Bloom bloom = rlpStream.DecodeBloom(); UInt256 difficulty = rlpStream.DecodeUInt256(); UInt256 number = rlpStream.DecodeUInt256(); UInt256 gasLimit = rlpStream.DecodeUInt256(); UInt256 gasUsed = rlpStream.DecodeUInt256(); UInt256 timestamp = rlpStream.DecodeUInt256(); byte[] extraData = rlpStream.DecodeByteArray(); BlockHeader blockHeader = new BlockHeader( parentHash, ommersHash, beneficiary, difficulty, (long)number, (long)gasLimit, timestamp, extraData) { StateRoot = stateRoot, TxRoot = transactionsRoot, ReceiptsRoot = receiptsRoot, Bloom = bloom, GasUsed = (long)gasUsed, Hash = Keccak.Compute(headerRlp) }; if (rlpStream.PeekPrefixAndContentLength().ContentLength == Keccak.Size) { blockHeader.MixHash = rlpStream.DecodeKeccak(); blockHeader.Nonce = (ulong)rlpStream.DecodeUBigInt(); } else { blockHeader.AuRaStep = (long)rlpStream.DecodeUInt256(); blockHeader.AuRaSignature = rlpStream.DecodeByteArray(); } if ((rlpBehaviors & RlpBehaviors.AllowExtraData) != RlpBehaviors.AllowExtraData) { rlpStream.Check(headerCheck); } return(blockHeader); }