-
Notifications
You must be signed in to change notification settings - Fork 1k
[Add] Block Builder
#4205
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
Closed
cschuchardt88
wants to merge
7
commits into
neo-project:dev
from
cschuchardt88:dev/add/block-builder
Closed
[Add] Block Builder
#4205
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
233c553
[`Add`] Block Builder
cschuchardt88 c4f5804
Update src/Neo/Builders/BlockBuilder.cs
cschuchardt88 0f85b92
Update src/Neo/Builders/BlockBuilder.cs
cschuchardt88 5fee8fe
Merge branch 'dev' into dev/add/block-builder
cschuchardt88 f705aea
Merge branch 'dev' into dev/add/block-builder
cschuchardt88 800c16a
Merge branch 'dev' into dev/add/block-builder
ajara87 885e0ea
Merge branch 'dev' into dev/add/block-builder
ajara87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,166 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // BlockBuilder.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Cryptography; | ||
| using Neo.Extensions.Factories; | ||
| using Neo.Network.P2P.Payloads; | ||
| using System; | ||
| using System.Linq; | ||
|
|
||
| namespace Neo.Builders | ||
| { | ||
| public class BlockBuilder | ||
| { | ||
| private BlockBuilder() { } | ||
|
|
||
| private readonly Block _block = new() | ||
| { | ||
| Header = new() | ||
| { | ||
| Nonce = RandomNumberFactory.NextUInt64(), | ||
| Timestamp = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), | ||
| MerkleRoot = new(), | ||
| NextConsensus = new(), | ||
| PrevHash = new(), | ||
| Witness = new() | ||
| { | ||
| InvocationScript = Memory<byte>.Empty, | ||
| VerificationScript = Memory<byte>.Empty, | ||
| }, | ||
| }, | ||
| Transactions = [], | ||
| }; | ||
|
|
||
| public static BlockBuilder CreateEmpty() => | ||
| new(); | ||
|
|
||
| public static BlockBuilder CreateNext(Block prevBlock) => | ||
| new BlockBuilder() | ||
| .AddPrevHash(prevBlock.Hash) | ||
| .AddIndex(prevBlock.Index + 1); | ||
|
|
||
| public static BlockBuilder CreateNext(Block prevBlock, ulong MillisecondsPerBlock) => | ||
| new BlockBuilder() | ||
| .AddPrevHash(prevBlock.Hash) | ||
| .AddIndex(prevBlock.Index + 1) | ||
| .AddTimestamp(config => config += MillisecondsPerBlock); | ||
cschuchardt88 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static BlockBuilder CreateNext(Block prevBlock, ProtocolSettings protocolSettings) => | ||
| new BlockBuilder() | ||
| .AddPrevHash(prevBlock.Hash) | ||
| .AddIndex(prevBlock.Index + 1) | ||
| .AddTimestamp(config => config += protocolSettings.MillisecondsPerBlock); | ||
|
|
||
| public BlockBuilder AddIndex(uint index) | ||
| { | ||
| _block.Header.Index = index; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddPrimaryIndex(byte index) | ||
| { | ||
| _block.Header.PrimaryIndex = index; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddNextConsensus(UInt160 hash) | ||
| { | ||
| _block.Header.NextConsensus = hash; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddNonce(ulong nonce) | ||
| { | ||
| _block.Header.Nonce = nonce; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddPrevHash(UInt256 hash) | ||
| { | ||
| _block.Header.PrevHash = hash; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddTimestamp(ulong timestamp) | ||
| { | ||
| _block.Header.Timestamp = timestamp; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddTimestamp(Action<ulong> config) | ||
| { | ||
| var timestamp = _block.Header.Timestamp; | ||
| config(timestamp); | ||
| _block.Header.Timestamp = timestamp; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddTimestamp(DateTimeOffset timestamp) | ||
| { | ||
| _block.Header.Timestamp = (ulong)timestamp.ToUnixTimeMilliseconds(); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddVersion(uint version) | ||
| { | ||
| _block.Header.Version = version; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddWitness(Action<WitnessBuilder> config) | ||
| { | ||
| var wb = WitnessBuilder.CreateEmpty(); | ||
| config(wb); | ||
| _block.Header.Witness = wb.Build(); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddWitness(Witness witness) | ||
| { | ||
| _block.Header.Witness = witness; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddTransaction(Action<TransactionBuilder> config) | ||
| { | ||
|
|
||
| var tx = TransactionBuilder.CreateEmpty(); | ||
| config(tx); | ||
| _block.Transactions = [.. _block.Transactions, tx.Build()]; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public BlockBuilder AddTransaction(Transaction tx) | ||
| { | ||
| _block.Transactions = [.. _block.Transactions, tx]; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public Block Build() | ||
| { | ||
| _block.Header.MerkleRoot = MerkleTree.ComputeRoot([.. _block.Transactions.Select(static s => s.Hash)]); | ||
| return _block; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,156 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // UT_BlockBuilder.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Neo.Builders; | ||
| using Neo.Extensions.Factories; | ||
|
|
||
| namespace Neo.UnitTests.Builders | ||
| { | ||
| [TestClass] | ||
| public class UT_BlockBuilder | ||
| { | ||
| [TestMethod] | ||
| public void TestCreateBuilder() | ||
| { | ||
| var builder = BlockBuilder.CreateEmpty(); | ||
|
|
||
| Assert.IsNotNull(builder); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestEmptyBlock() | ||
| { | ||
| var block = BlockBuilder.CreateEmpty().Build(); | ||
|
|
||
| Assert.IsNotNull(block); | ||
| Assert.IsNotNull(block.Hash); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockVersion() | ||
| { | ||
| byte expectedVersion = 1; | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddVersion(expectedVersion) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedVersion, actualBlock.Version); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockIndex() | ||
| { | ||
| byte expectedIndex = 1; | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddIndex(expectedIndex) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedIndex, actualBlock.Index); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockPrimaryIndex() | ||
| { | ||
| byte expectedPrimaryIndex = 1; | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddPrimaryIndex(expectedPrimaryIndex) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedPrimaryIndex, actualBlock.PrimaryIndex); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockNextConsensus() | ||
| { | ||
| var expectedNextConsensus = UInt160.Zero; | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddNextConsensus(expectedNextConsensus) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedNextConsensus, actualBlock.NextConsensus); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockNonce() | ||
| { | ||
| var expectedNonce = RandomNumberFactory.NextUInt64(); | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddNonce(expectedNonce) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedNonce, actualBlock.Nonce); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockPrevHash() | ||
| { | ||
| var expectedPrevHash = UInt256.Zero; | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddPrevHash(expectedPrevHash) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedPrevHash, actualBlock.PrevHash); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockTimestamp() | ||
| { | ||
| var expectedTimestamp = RandomNumberFactory.NextUInt64(); | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddTimestamp(expectedTimestamp) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedTimestamp, actualBlock.Timestamp); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockTransaction() | ||
| { | ||
| var expectedTransaction = TransactionBuilder.CreateEmpty().Build(); | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddTransaction(expectedTransaction) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedTransaction, actualBlock.Transactions[0]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockWitness() | ||
| { | ||
| var expectedWitness = WitnessBuilder.CreateEmpty().Build(); | ||
| var actualBlock = BlockBuilder.CreateEmpty() | ||
| .AddWitness(expectedWitness) | ||
| .Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedWitness, actualBlock.Witness); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestBlockMerkleRoot() | ||
| { | ||
| var expectedMerkleRoot = UInt256.Zero; | ||
| var actualBlock = BlockBuilder.CreateEmpty().Build(); | ||
|
|
||
| Assert.IsNotNull(actualBlock); | ||
| Assert.AreEqual(expectedMerkleRoot, actualBlock.MerkleRoot); | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.