-
Notifications
You must be signed in to change notification settings - Fork 380
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
ERA history import/export start #6173
Closed
Closed
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
301180c
ERA history import/export start
ak88 5a0b8c4
Rename in unit test
ak88 04209fb
Fix block index not set
ak88 4b10a61
Start export
ak88 bfcbfb5
Era builder
ak88 e9d91b6
Accumulator calculation
ak88 7b88e17
ERA history import/export
ak88 1543a39
Rename in unit test
ak88 e07cca5
Fix block index not set
ak88 42131e6
Start export
ak88 551d974
Era builder
ak88 aba6cfb
Accumulator calculation
ak88 65a9225
Builder finalize
ak88 d0741e5
Merge branch 'feature/era-history' of https://github.com/ak88/netherm…
ak88 508f1ae
Unit tests
ak88 c7636c2
Unit tests
ak88 ee53da7
import geth files
ak88 8503f08
Reader with netty
ak88 66eacd1
1mb read buffer
ak88 8d48264
refactor test and use of netty
ak88 904ecab
Big blocks test
ak88 74d14ad
Change to IByteBuffer
ak88 e1e86ae
buffer allocater injected
ak88 d8f4ad8
Merge branch 'NettyBuffers' into feature/era-history
ak88 c27adf6
Squashed commit of the following:
ak88 27fc5cd
Merge from master
ak88 756b170
assert files exist
ak88 e3a6647
reverted
ak88 f18e742
rollback gitbook
ak88 7066e64
Merge branch 'master' into feature/era-history
ak88 d2656d8
revert gitbook
ak88 5d43047
revert launchsettings
ak88 dedba82
Delete from this pr
ak88 f2f03db
format whitespace
ak88 58f8a06
small fix
ak88 f089911
accumulator test
ak88 5c9f698
Squashed commit of the following:
ak88 ce691f0
ReceiptMessageDecoder cleanup
ak88 c618d97
Squashed commit of the following:
ak88 c3e6df8
no seeking to index
ak88 c0640ed
reuse compression streams
ak88 85dfb85
format
ak88 6330162
Performance optimization
ak88 3fed302
avoid a copy and dispose on ex
ak88 1900b69
Merge branch 'feature/era-history' of https://github.com/ak88/netherm…
ak88 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
46 changes: 46 additions & 0 deletions
46
src/Nethermind/Nethermind.Era.Test/AccumulatorCalculatorTests.cs
This file contains 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,46 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Runtime.Intrinsics.Arm; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Nethermind.Core.Crypto; | ||
|
||
namespace Nethermind.Era1.Test; | ||
public class AccumulatorCalculatorTests | ||
{ | ||
[Test] | ||
public void Add_AddOneHashAndUInt256_DoesNotThrow() | ||
{ | ||
using var sut = new AccumulatorCalculator(); | ||
|
||
Assert.That(() => sut.Add(Keccak.Zero, 0), Throws.Nothing); | ||
} | ||
[Test] | ||
public void ComputeRoot_AddValues_ReturnsExpectedResult() | ||
{ | ||
using var sut = new AccumulatorCalculator(); | ||
sut.Add(Keccak.Zero, 1); | ||
sut.Add(Keccak.MaxValue, 2); | ||
|
||
var result = sut.ComputeRoot().ToArray(); | ||
|
||
Assert.That(result, Is.EquivalentTo(new[] { 0x3E, 0xD6, 0x26, 0x52, 0xDF, 0xB7, 0xE1, 0x07, 0x2D, 0x0F, 0x04, 0x0F, 0xEB, 0x6D, 0x00, 0x2A, 0x9F, 0x7C, 0xE3, 0x7C, 0xF8, 0xDD, 0xB1, 0x65, 0x49, 0xA7, 0xAC, 0x5C, 0xF8, 0xE3, 0xB7, 0x91 })); | ||
} | ||
|
||
[Test] | ||
public void ComputeRoot_AddOneHashAndUInt256_DoesNotThrow() | ||
{ | ||
using var sut = new AccumulatorCalculator(); | ||
sut.Add(Keccak.Zero, 1); | ||
sut.Add(Keccak.MaxValue, 2); | ||
|
||
var result = sut.ComputeRoot().ToArray(); | ||
|
||
Assert.That(result, Is.EquivalentTo(new[] { 0x3E, 0xD6, 0x26, 0x52, 0xDF, 0xB7, 0xE1, 0x07, 0x2D, 0x0F, 0x04, 0x0F, 0xEB, 0x6D, 0x00, 0x2A, 0x9F, 0x7C, 0xE3, 0x7C, 0xF8, 0xDD, 0xB1, 0x65, 0x49, 0xA7, 0xAC, 0x5C, 0xF8, 0xE3, 0xB7, 0x91 })); | ||
} | ||
} |
This file contains 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,258 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DotNetty.Buffers; | ||
using FluentAssertions; | ||
using Nethermind.Serialization.Rlp; | ||
using Snappier; | ||
|
||
namespace Nethermind.Era1.Test; | ||
internal class E2StoreTests | ||
{ | ||
[TestCase(EntryTypes.Version)] | ||
[TestCase(EntryTypes.CompressedHeader)] | ||
[TestCase(EntryTypes.CompressedBody)] | ||
[TestCase(EntryTypes.CompressedReceipts)] | ||
[TestCase(EntryTypes.Accumulator)] | ||
[TestCase(EntryTypes.BlockIndex)] | ||
public async Task WriteEntry_WritingAnEntry_WritesCorrectHeaderType(ushort type) | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
|
||
await sut.WriteEntry(type, Array.Empty<byte>()); | ||
|
||
Assert.That(BitConverter.ToInt16(stream.ToArray()), Is.EqualTo(type)); | ||
} | ||
|
||
[TestCase(6)] | ||
[TestCase(20)] | ||
[TestCase(32)] | ||
public async Task WriteEntry_WritingAnEntry_WritesCorrectLengthInHeader(int length) | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
|
||
await sut.WriteEntry(EntryTypes.CompressedHeader, new byte[length]); | ||
|
||
Assert.That(BitConverter.ToInt32(stream.ToArray(), 2), Is.EqualTo(length)); | ||
} | ||
|
||
[TestCase(1)] | ||
[TestCase(5)] | ||
[TestCase(12)] | ||
public async Task WriteEntry_WritingAnEntry_ReturnCorrectNumberofBytesWritten(int length) | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
|
||
int result = await sut.WriteEntry(EntryTypes.CompressedHeader, new byte[length]); | ||
|
||
Assert.That(result, Is.EqualTo(length + E2Store.HeaderSize)); | ||
} | ||
|
||
|
||
[Test] | ||
public async Task WriteEntry_WritingAnEntry_ZeroesAtCorrectIndexesInHeader() | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
|
||
await sut.WriteEntry(EntryTypes.CompressedHeader, new byte[] { 0xff, 0xff, 0xff, 0xff }); | ||
byte[] bytes = stream.ToArray(); | ||
|
||
Assert.That(bytes[6], Is.EqualTo(0)); | ||
Assert.That(bytes[7], Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public async Task WriteEntry_WritingEntryValue_BytesAreWrittenToStream() | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
|
||
byte[] bytes = new byte[] { 0x0f, 0xf0, 0xff, 0xff }; | ||
await sut.WriteEntry(EntryTypes.CompressedHeader, bytes); | ||
byte[] result = stream.ToArray(); | ||
|
||
Assert.That(new ArraySegment<byte>(result, E2Store.HeaderSize, bytes.Length), Is.EquivalentTo(bytes)); | ||
} | ||
|
||
[Test] | ||
public async Task WriteEntryAsSnappy_WritingEntryValue_WritesEncodedBytesToStream() | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
byte[] bytes = new byte[] { 0x0f, 0xf0, 0xff, 0xff }; | ||
|
||
await sut.WriteEntryAsSnappy(EntryTypes.CompressedHeader, bytes); | ||
stream.Position = E2Store.HeaderSize; | ||
using var snappy = new SnappyStream(stream, System.IO.Compression.CompressionMode.Decompress); | ||
byte[] buffer = new byte[32]; | ||
|
||
Assert.That(() => snappy.Read(buffer), Throws.Nothing); | ||
} | ||
|
||
[Test] | ||
public async Task WriteEntryAsSnappy_WritingEntryValue_ReturnsCompressedSize() | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
byte[] bytes = new byte[] { 0x0f, 0xf0, 0xff, 0xff }; | ||
|
||
int result = await sut.WriteEntryAsSnappy(EntryTypes.CompressedHeader, bytes); | ||
|
||
Assert.That(result, Is.EqualTo(stream.Length)); | ||
} | ||
|
||
[Test] | ||
public async Task ReadEntryValue_ReadingValueBytesOfEntry_ReturnsBytes() | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
byte[] bytes = new byte[] { 0x0f, 0xf0, 0xff, 0xff }; | ||
await sut.WriteEntry(EntryTypes.Accumulator, bytes); | ||
IByteBuffer buffer = UnpooledByteBufferAllocator.Default.Buffer(bytes.Length); | ||
try | ||
{ | ||
int read = await sut.ReadEntryValue(buffer, new Entry(EntryTypes.Accumulator, 0, bytes.Length)); | ||
|
||
Assert.That(new ArraySegment<byte>(buffer.Array, buffer.ArrayOffset, read), Is.EquivalentTo(bytes)); | ||
} | ||
finally | ||
{ | ||
buffer.Release(); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task ReadEntryValue_ReadingValueBytesOfEntry_ReturnsBytesRead() | ||
{ | ||
using MemoryStream stream = new MemoryStream(); | ||
using E2Store sut = new E2Store(stream); | ||
byte[] bytes = new byte[] { 0x0f, 0xf0, 0xff, 0xff }; | ||
await sut.WriteEntry(EntryTypes.Accumulator, bytes); | ||
IByteBuffer buffer = UnpooledByteBufferAllocator.Default.Buffer(bytes.Length); | ||
|
||
int result = await sut.ReadEntryValue(buffer, new Entry(EntryTypes.Accumulator, 0, bytes.Length)); | ||
|
||
Assert.That(result, Is.EqualTo(bytes.Length)); | ||
} | ||
|
||
[Test] | ||
public async Task ReadEntryValueAsSnappy_ReadingValueBytesOfEntry_ReturnsDecompressedBytes() | ||
{ | ||
using MemoryStream stream = new(); | ||
using E2Store sut = new E2Store(stream); | ||
byte[] bytes = new byte[] { 0x0f, 0xf0, 0xff, 0xff }; | ||
MemoryStream compressed = new(); | ||
using SnappyStream snappy = new SnappyStream(compressed, System.IO.Compression.CompressionMode.Compress); | ||
snappy.Write(bytes); | ||
snappy.Flush(); | ||
byte[] compressedBytes = compressed.ToArray(); | ||
await sut.WriteEntry(EntryTypes.CompressedHeader, compressedBytes); | ||
IByteBuffer buffer = UnpooledByteBufferAllocator.Default.Buffer(32); | ||
try | ||
{ | ||
int read = await sut.ReadEntryValueAsSnappy(buffer, new Entry(EntryTypes.CompressedHeader, 0, compressedBytes.Length)); | ||
|
||
Assert.That(new ArraySegment<byte>(buffer.Array, 0, read), Is.EquivalentTo(bytes)); | ||
} | ||
finally | ||
{ | ||
buffer.Release(); | ||
} | ||
} | ||
|
||
|
||
[TestCase(0)] | ||
[TestCase(8)] | ||
[TestCase(16)] | ||
public async Task ReadValueAt_ReadingValueAtDifferentOffset_ReturnsCorrectValue(int offset) | ||
{ | ||
using MemoryStream stream = new(); | ||
using E2Store sut = new E2Store(stream); | ||
byte[] bytes = new byte[] { 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 }; | ||
stream.Write(bytes, 0, bytes.Length); | ||
|
||
long result = await sut.ReadValueAt(offset); | ||
|
||
Assert.That(result, Is.EqualTo(BitConverter.ToInt64(bytes, offset))); | ||
} | ||
|
||
[TestCase(6)] | ||
[TestCase(14)] | ||
[TestCase(24)] | ||
public async Task ReadEntryAt_ReadingEntryAtDifferentOffset_ReturnsCorrectEntry(int offset) | ||
{ | ||
using MemoryStream stream = new(); | ||
using E2Store sut = new E2Store(stream); | ||
byte[] bytes = new byte[] { 0xff, 0xff, 0xff, 0xff }; | ||
stream.SetLength(offset); | ||
stream.Seek(0, SeekOrigin.End); | ||
await sut.WriteEntry(EntryTypes.CompressedHeader, bytes); | ||
|
||
Entry result = await sut.ReadEntryAt(offset); | ||
|
||
Assert.That(result.Type, Is.EqualTo(EntryTypes.CompressedHeader)); | ||
Assert.That(result.Length, Is.EqualTo(bytes.Length)); | ||
Assert.That(result.Offset, Is.EqualTo(offset)); | ||
} | ||
|
||
[TestCase(12)] | ||
[TestCase(20)] | ||
[TestCase(32)] | ||
public async Task ReadEntryHeaderAt_ReadingEntryHeaderAtDifferentOffset_ReturnsCorrectEntryHeader(int offset) | ||
{ | ||
using MemoryStream stream = new(); | ||
using E2Store sut = new E2Store(stream); | ||
byte[] bytes = new byte[] { 0xff, 0xff, 0xff, 0xff }; | ||
stream.SetLength(offset); | ||
stream.Seek(0, SeekOrigin.End); | ||
await sut.WriteEntry(EntryTypes.CompressedHeader, bytes); | ||
|
||
HeaderData result = await sut.ReadEntryHeaderAt(offset); | ||
|
||
Assert.That(result.Type, Is.EqualTo(EntryTypes.CompressedHeader)); | ||
Assert.That(result.Length, Is.EqualTo(bytes.Length)); | ||
} | ||
|
||
[Test] | ||
public void Test() | ||
{ | ||
//TODO possible optimization avoid alloc snappy stream by not disposing the stream and reusing like this | ||
using MemoryStream stream = new(); | ||
byte[] bytes = new byte[] { 0x0f, 0xf0, 0xff, 0xff }; | ||
byte[] bytes1 = new byte[] { 0x1, 0x2, 0x3, 0x4 }; | ||
MemoryStream compressed = new(); | ||
using SnappyStream snappy = new SnappyStream(compressed, System.IO.Compression.CompressionMode.Compress); | ||
snappy.Write(bytes); | ||
snappy.Flush(); | ||
byte[] compressedBytes = compressed.ToArray(); | ||
compressed.Seek(0, SeekOrigin.Begin); | ||
using SnappyStream snappyDecom = new SnappyStream(compressed, System.IO.Compression.CompressionMode.Decompress); | ||
var decomBytes = new byte[bytes.Length]; | ||
snappyDecom.Read(decomBytes, 0, decomBytes.Length); | ||
decomBytes.Should().BeEquivalentTo(bytes); | ||
|
||
compressed.SetLength(0); | ||
var snappyHeader = new byte[] | ||
{ | ||
0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59 | ||
}; | ||
compressed.Write(snappyHeader, 0, snappyHeader.Length); | ||
snappy.Write(bytes1); | ||
snappy.Flush(); | ||
compressed.Seek(0, SeekOrigin.Begin); | ||
|
||
snappyDecom.Read(decomBytes, 0, decomBytes.Length); | ||
decomBytes.Should().BeEquivalentTo(bytes1); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Not newer
Nethermind.Serialization.Ssz
?@flcl42 Can you advise?