Skip to content

Commit 69f61e4

Browse files
committed
Add SSZ encoding and hashing for Blob txs
1 parent 0ed97aa commit 69f61e4

48 files changed

Lines changed: 1233 additions & 371 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Nethermind/Nethermind.Cli/Console/ColorfulCliConsole.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
// Copyright (c) 2022 Demerzel Solutions Limited
2-
// This file is part of the Nethermind library.
3-
//
4-
// The Nethermind library is free software: you can redistribute it and/or modify
5-
// it under the terms of the GNU Lesser General Public License as published by
6-
// the Free Software Foundation, either version 3 of the License, or
7-
// (at your option) any later version.
8-
//
9-
// The Nethermind library is distributed in the hope that it will be useful,
10-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
// GNU Lesser General Public License for more details.
13-
//
14-
// You should have received a copy of the GNU Lesser General Public License
15-
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
1+
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
2+
// SPDX-License-Identifier: LGPL-3.0-only
163

174
using System;
185
using System.Drawing;

src/Nethermind/Nethermind.Consensus/Transactions/EmptyTxSource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using Nethermind.Core;
7-
using Nethermind.Int256;
87

98
namespace Nethermind.Consensus.Transactions
109
{

src/Nethermind/Nethermind.Consensus/Transactions/FilteredTxSource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using Nethermind.Core;
7-
using Nethermind.Int256;
87
using Nethermind.Logging;
98
using Nethermind.TxPool;
109

src/Nethermind/Nethermind.Consensus/Transactions/ITxSource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Collections.Generic;
55
using Nethermind.Core;
6-
using Nethermind.Int256;
76

87
namespace Nethermind.Consensus.Transactions
98
{

src/Nethermind/Nethermind.Consensus/Transactions/SinglePendingTxSelector.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using Nethermind.Core;
8-
using Nethermind.Int256;
98

109
namespace Nethermind.Consensus.Transactions
1110
{

src/Nethermind/Nethermind.Consensus/Validators/TxValidator.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,12 @@ private bool ValidateSignature(Signature? signature, IReleaseSpec spec)
101101

102102
private static bool Validate4844Fields(Transaction transaction)
103103
{
104-
// Execution-payload version part
104+
// Execution-payload version verification
105105
if (transaction.Type != TxType.Blob)
106106
{
107107
return transaction.MaxFeePerDataGas is null &&
108108
transaction.BlobVersionedHashes is null &&
109-
transaction.BlobKzgs is null &&
110-
transaction.Blobs is null &&
111-
transaction.BlobProofs is null;
109+
transaction is not { NetworkWrapper: ShardBlobNetworkWrapper };
112110
}
113111

114112
if (transaction.MaxFeePerDataGas is null ||
@@ -130,32 +128,33 @@ transaction.BlobVersionedHashes is null ||
130128
}
131129
}
132130

133-
// And mempool version part if presents
134-
if (transaction.BlobVersionedHashes!.Length > 0 && (transaction.Blobs is not null ||
135-
transaction.BlobKzgs is not null ||
136-
transaction.BlobProofs is not null))
131+
// Mempool version verification if presents
132+
if (transaction.NetworkWrapper is ShardBlobNetworkWrapper wrapper)
137133
{
138-
if (transaction.BlobKzgs is null)
134+
int blobCount = wrapper.Blobs.Length / Ckzg.Ckzg.BytesPerBlob;
135+
if (transaction.BlobVersionedHashes.Length != blobCount ||
136+
(wrapper.BlobKzgs.Length / Ckzg.Ckzg.BytesPerCommitment) != blobCount ||
137+
(wrapper.BlobProofs.Length / Ckzg.Ckzg.BytesPerProof) != blobCount)
139138
{
140139
return false;
141140
}
142141

143142
Span<byte> hash = stackalloc byte[32];
144-
Span<byte> commitements = transaction.BlobKzgs;
143+
Span<byte> commitements = wrapper.BlobKzgs;
145144
for (int i = 0, n = 0;
146-
i < transaction.BlobVersionedHashes!.Length;
145+
i < transaction.BlobVersionedHashes.Length;
147146
i++, n += Ckzg.Ckzg.BytesPerCommitment)
148147
{
149148
if (!KzgPolynomialCommitments.TryComputeCommitmentHashV1(
150149
commitements[n..(n + Ckzg.Ckzg.BytesPerCommitment)], hash) ||
151-
!hash.SequenceEqual(transaction.BlobVersionedHashes![i]))
150+
!hash.SequenceEqual(transaction.BlobVersionedHashes[i]))
152151
{
153152
return false;
154153
}
155154
}
156155

157-
return KzgPolynomialCommitments.AreProofsValid(transaction.Blobs!,
158-
transaction.BlobKzgs!, transaction.BlobProofs!);
156+
return KzgPolynomialCommitments.AreProofsValid(wrapper.Blobs,
157+
wrapper.BlobKzgs, wrapper.BlobProofs);
159158
}
160159

161160
return true;
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
22
// SPDX-License-Identifier: LGPL-3.0-only
33

4-
namespace Nethermind.Core.Test.Builders
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Nethermind.Core.Eip2930;
7+
using Nethermind.Core.Extensions;
8+
using Nethermind.Crypto;
9+
using Nethermind.Int256;
10+
using Nethermind.Logging;
11+
12+
namespace Nethermind.Core.Test.Builders;
13+
14+
public partial class Build
515
{
6-
public partial class Build
7-
{
8-
public TransactionBuilder<Transaction> Transaction => new();
9-
public TransactionBuilder<SystemTransaction> SystemTransaction => new();
10-
public TransactionBuilder<GeneratedTransaction> GeneratedTransaction => new();
11-
public TransactionBuilder<T> TypedTransaction<T>() where T : Transaction, new() => new();
16+
public TransactionBuilder<Transaction> Transaction => new();
17+
public TransactionBuilder<SystemTransaction> SystemTransaction => new();
18+
public TransactionBuilder<GeneratedTransaction> GeneratedTransaction => new();
19+
public TransactionBuilder<T> TypedTransaction<T>() where T : Transaction, new() => new();
1220

13-
public TransactionBuilder<NamedTransaction> NamedTransaction(string name)
14-
{
15-
return new() { TestObjectInternal = { Name = name } };
16-
}
21+
public TransactionBuilder<NamedTransaction> NamedTransaction(string name)
22+
{
23+
return new() { TestObjectInternal = { Name = name } };
1724
}
1825
}

src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Nethermind.Core.Test.Builders
1515
{
1616
public static partial class TestItem
1717
{
18-
public static Random Random { get; } = new();
18+
public static Random Random { get; } = new(1337);
1919
private static readonly AccountDecoder _accountDecoder = new();
2020

2121
static TestItem()

src/Nethermind/Nethermind.Core.Test/Builders/TransactionBuilder.cs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,7 @@ public TransactionBuilder<T> WithBlobVersionedHashes(int? count)
163163
return this;
164164
}
165165

166-
public TransactionBuilder<T> WithBlobs(byte[] blobs)
167-
{
168-
TestObjectInternal.Blobs = blobs;
169-
170-
return this;
171-
}
172-
173-
public TransactionBuilder<T> WithShardBlobTxTypeAndFields(int blobCount)
166+
public TransactionBuilder<T> WithShardBlobTxTypeAndFields(int blobCount, bool isMempoolTx = true)
174167
{
175168
if (blobCount is 0)
176169
{
@@ -179,34 +172,44 @@ public TransactionBuilder<T> WithShardBlobTxTypeAndFields(int blobCount)
179172

180173
TestObjectInternal.Type = TxType.Blob;
181174
TestObjectInternal.MaxFeePerDataGas ??= 1;
182-
TestObjectInternal.Blobs = new byte[Ckzg.Ckzg.BytesPerBlob * blobCount];
183-
TestObjectInternal.BlobKzgs = new byte[Ckzg.Ckzg.BytesPerCommitment * blobCount];
184-
TestObjectInternal.BlobProofs = new byte[Ckzg.Ckzg.BytesPerProof * blobCount];
185-
TestObjectInternal.BlobVersionedHashes = new byte[blobCount][];
186-
for (int i = 0; i < blobCount; i++)
175+
176+
if (isMempoolTx)
187177
{
188-
TestObjectInternal.BlobVersionedHashes[i] = new byte[32];
189-
TestObjectInternal.Blobs[Ckzg.Ckzg.BytesPerBlob * i] = 1;
190-
KzgPolynomialCommitments.KzgifyBlob(
191-
TestObjectInternal.Blobs.AsSpan(Ckzg.Ckzg.BytesPerBlob * i, Ckzg.Ckzg.BytesPerBlob * (i + 1)),
192-
TestObjectInternal.BlobKzgs.AsSpan(Ckzg.Ckzg.BytesPerCommitment * i, Ckzg.Ckzg.BytesPerCommitment * (i + 1)),
193-
TestObjectInternal.BlobProofs.AsSpan(Ckzg.Ckzg.BytesPerProof * i, Ckzg.Ckzg.BytesPerProof * (i + 1)),
194-
TestObjectInternal.BlobVersionedHashes[i].AsSpan());
178+
TestObjectInternal.BlobVersionedHashes = new byte[blobCount][];
179+
ShardBlobNetworkWrapper wrapper = new(
180+
blobs: new byte[Ckzg.Ckzg.BytesPerBlob * blobCount],
181+
blobKzgs: new byte[Ckzg.Ckzg.BytesPerCommitment * blobCount],
182+
blobProofs: new byte[Ckzg.Ckzg.BytesPerProof * blobCount]
183+
);
184+
185+
for (int i = 0; i < blobCount; i++)
186+
{
187+
TestObjectInternal.BlobVersionedHashes[i] = new byte[32];
188+
wrapper.Blobs[Ckzg.Ckzg.BytesPerBlob * i] = (byte)(i % 256);
189+
if (KzgPolynomialCommitments.IsInitialized)
190+
{
191+
KzgPolynomialCommitments.KzgifyBlob(
192+
wrapper.Blobs.AsSpan(Ckzg.Ckzg.BytesPerBlob * i, Ckzg.Ckzg.BytesPerBlob * (i + 1)),
193+
wrapper.BlobKzgs.AsSpan(Ckzg.Ckzg.BytesPerCommitment * i,
194+
Ckzg.Ckzg.BytesPerCommitment * (i + 1)),
195+
wrapper.BlobProofs.AsSpan(Ckzg.Ckzg.BytesPerProof * i,
196+
Ckzg.Ckzg.BytesPerProof * (i + 1)),
197+
TestObjectInternal.BlobVersionedHashes[i].AsSpan());
198+
}
199+
else
200+
{
201+
wrapper.BlobKzgs[Ckzg.Ckzg.BytesPerCommitment * i] = (byte)(i % 256);
202+
wrapper.BlobProofs[Ckzg.Ckzg.BytesPerProof * i] = (byte)(i % 256);
203+
}
204+
}
205+
206+
TestObjectInternal.NetworkWrapper = wrapper;
207+
}
208+
else
209+
{
210+
return WithBlobVersionedHashes(blobCount);
195211
}
196212

197-
198-
return this;
199-
}
200-
201-
public TransactionBuilder<T> WithBlobKzgs(byte[] blobKzgs)
202-
{
203-
TestObjectInternal.BlobKzgs = blobKzgs;
204-
return this;
205-
}
206-
207-
public TransactionBuilder<T> WithProofs(byte[] proofs)
208-
{
209-
TestObjectInternal.BlobProofs = proofs;
210213
return this;
211214
}
212215

src/Nethermind/Nethermind.Core.Test/Encoding/ShardBlobTxDecoderTests.TestCases.cs

Lines changed: 45 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)