diff --git a/src/Nethermind/Nethermind.Taiko.Test/L1OriginStoreTests.cs b/src/Nethermind/Nethermind.Taiko.Test/L1OriginStoreTests.cs index 954bfacd3082..4c0870ecc5e9 100644 --- a/src/Nethermind/Nethermind.Taiko.Test/L1OriginStoreTests.cs +++ b/src/Nethermind/Nethermind.Taiko.Test/L1OriginStoreTests.cs @@ -202,5 +202,23 @@ public void Fails_for_invalid_length_signature(int signatureLength) Action act = () => _decoder.Encode(origin); act.Should().Throw().WithMessage($"*Signature*{L1OriginDecoder.SignatureLength}*"); } + + [Test] + public void Encode_produces_RLP_with_correct_sequence_length( + [Values(false, true)] bool withBuildPayload, + [Values(false, true)] bool withForcedInclusion, + [Values(false, true)] bool withSignature) + { + int[]? buildPayloadArgsId = withBuildPayload ? Enumerable.Range(0, 8).ToArray() : null; + int[]? signature = withSignature ? Enumerable.Range(0, 65).ToArray() : null; + L1Origin origin = new(123, Hash256.Zero, 456, Hash256.Zero, buildPayloadArgsId, withForcedInclusion, signature); + + Rlp encoded = _decoder.Encode(origin); + RlpStream stream = new(encoded.Bytes); + (int prefixLength, int contentLength) = stream.ReadPrefixAndContentLength(); + + contentLength.Should().Be(encoded.Bytes.Length - prefixLength, + "StartSequence must receive content length, not total length"); + } } diff --git a/src/Nethermind/Nethermind.Taiko/L1OriginDecoder.cs b/src/Nethermind/Nethermind.Taiko/L1OriginDecoder.cs index 9e57c56f940a..329a766b3b7a 100644 --- a/src/Nethermind/Nethermind.Taiko/L1OriginDecoder.cs +++ b/src/Nethermind/Nethermind.Taiko/L1OriginDecoder.cs @@ -49,7 +49,7 @@ public Rlp Encode(L1Origin? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None) public override void Encode(RlpStream stream, L1Origin item, RlpBehaviors rlpBehaviors = RlpBehaviors.None) { - stream.StartSequence(GetLength(item, rlpBehaviors)); + stream.StartSequence(GetContentLength(item, rlpBehaviors)); stream.Encode(item.BlockId); stream.Encode(item.L2BlockHash); @@ -92,6 +92,11 @@ public override void Encode(RlpStream stream, L1Origin item, RlpBehaviors rlpBeh } public override int GetLength(L1Origin item, RlpBehaviors rlpBehaviors) + { + return Rlp.LengthOfSequence(GetContentLength(item, rlpBehaviors)); + } + + private int GetContentLength(L1Origin item, RlpBehaviors rlpBehaviors) { int buildPayloadLength = 0; if (item.BuildPayloadArgsId is not null || item.IsForcedInclusion || item.Signature is not null) @@ -107,14 +112,12 @@ public override int GetLength(L1Origin item, RlpBehaviors rlpBehaviors) isForcedInclusionLength = Rlp.LengthOf(item.IsForcedInclusion); } - return Rlp.LengthOfSequence( - Rlp.LengthOf(item.BlockId) + return Rlp.LengthOf(item.BlockId) + Rlp.LengthOf(item.L2BlockHash) + Rlp.LengthOf(item.L1BlockHeight ?? 0) + Rlp.LengthOf(item.L1BlockHash) + buildPayloadLength + isForcedInclusionLength - + (item.Signature is null ? 0 : Rlp.LengthOfByteString(SignatureLength, 0)) - ); + + (item.Signature is null ? 0 : Rlp.LengthOfByteString(SignatureLength, 0)); } }