Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Nethermind/Nethermind.Taiko.Test/L1OriginStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,23 @@ public void Fails_for_invalid_length_signature(int signatureLength)
Action act = () => _decoder.Encode(origin);
act.Should().Throw<RlpException>().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");
}
}

13 changes: 8 additions & 5 deletions src/Nethermind/Nethermind.Taiko/L1OriginDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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));
}
}
Loading