This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make BlockProcessor with less dependencies (#354)
* RocksDbRepository reach 42% cover * Rename abstract class ICrypto to Crypto. I'm sure this was once an interface but it's not anymore, threfore should not carry the prefix "I" that identify the interfaces. * fix some merge problems * Increase Server tests coverage to 94%. Black listed peers try to connect test * fix merge issues * * Remove protected fields from TestBase and replace with overload usage of the method RandomInt. * Small clean up on the UtCrypto * Create UtBlockchain and create first tests. more tests will be added soon! * fix issues from the merge * Adding testing information to the team Contributing page. * Small CleanUp the test class with properties that aren't been used anymore. * UtBlockChain: InitializeBlockchain_IsGenesisBlock_InitializeComplete * Firsts UtBlockProcessor tests * AddBlock tests added * * UtBlockProcessor: Run test * Fix an inverted if logic. The code only enter in the While loop if the cancellation was requested and should be the other way around. * Cleanup * Replace BlockProcessor dependency from Prompt and replace with IBlockPool * Remove unused properties from BlockProcessor * ContainsBlock method in BlockProcessor signature changed to Private because is not used anywhere else but internally * UtBlockProcessor - Loop test: When the received block is not the expected next. * Remove comment * Remove async modifier from methods that aren't implemented * . * Merge branch 'development' of https://github.com/aboimpinto/neo-sharp into development * Add test class, #301 * Write tests for Add method. #301 * Refactor the Block / BlockHeader in Signed and Unsigned objects (mutable and immutable). The intenstion of this refactor is to bring better readability of the code and remove logic code from the DTO. * Implement the UpdateHash methods in the creation of the Signed<object> * Signed and Unsigned strucuture return GenenisBlock * Draft of the BinaryCustomSeralization of TransactionBase * Revert "Remove stack logs (#321)" This reverts commit 34c676a. * Add BlockOperationsManager and WitnessOperationManager that will be resposable to Sign and Verify the block and the witness * Initial work, removing logic from the model objects and add it into Witness. Transaction, BlockHeader and Block OperationManager classes. In this classes the verify method will be implemented. #306, #307 * Cleanup and remove code that was not merged. * regreassion test for the case where the current block is null. This happen when the node start without genesis block. * Remove dependencies from BlockProcessor and remove BlockHeader references. BlockProcessor use only Block object. #350, #351 * now the Persist consider the genesis block for the case of start a node in a clean environment
- Loading branch information
1 parent
44e1dbe
commit 3d872c2
Showing
7 changed files
with
149 additions
and
101 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NeoSharp.Core.Models; | ||
using NeoSharp.Core.Persistence; | ||
|
||
namespace NeoSharp.Core.Blockchain.Processing | ||
{ | ||
public class BlockPersister : IBlockPersister | ||
{ | ||
#region Private Fields | ||
private readonly IRepository _repository; | ||
private readonly IBlockHeaderPersister _blockHeaderPersister; | ||
private readonly ITransactionPersister<Transaction> _transactionPersister; | ||
private readonly ITransactionPool _transactionPool; | ||
#endregion | ||
|
||
#region Constructor | ||
public BlockPersister( | ||
IRepository repository, | ||
IBlockHeaderPersister blockHeaderPersister, | ||
ITransactionPersister<Transaction> transactionPersister, | ||
ITransactionPool transactionPool) | ||
{ | ||
this._repository = repository; | ||
this._blockHeaderPersister = blockHeaderPersister; | ||
this._transactionPersister = transactionPersister; | ||
this._transactionPool = transactionPool; | ||
} | ||
#endregion | ||
|
||
#region IBlockPersister Implementation | ||
public Block LastPersistedBlock { get; private set; } | ||
|
||
public event EventHandler<BlockHeader[]> OnBlockHeadersPersisted; | ||
|
||
public async Task Persist(params Block[] blocks) | ||
{ | ||
foreach (var block in blocks) | ||
{ | ||
this.LastPersistedBlock = block; | ||
|
||
var blockHeader = await this._repository.GetBlockHeader(block.Hash); | ||
if (blockHeader == null) | ||
{ | ||
await this._blockHeaderPersister.Persist(block.GetBlockHeader()); | ||
} | ||
|
||
foreach (var transaction in block.Transactions) | ||
{ | ||
await this._transactionPersister.Persist(transaction); | ||
this._transactionPool.Remove(transaction.Hash); | ||
} | ||
} | ||
} | ||
|
||
public async Task Persist(params BlockHeader[] blockHeaders) | ||
{ | ||
try | ||
{ | ||
this._blockHeaderPersister.OnBlockHeadersPersisted += this.HandleBlockHandlePersisted; | ||
await this._blockHeaderPersister.Persist(blockHeaders); | ||
} | ||
finally | ||
{ | ||
this._blockHeaderPersister.OnBlockHeadersPersisted -= this.HandleBlockHandlePersisted; | ||
} | ||
} | ||
|
||
public async Task<bool> IsBlockPersisted(Block block) | ||
{ | ||
var blockHeader = await this._repository.GetBlockHeader(block.Hash); | ||
|
||
if (blockHeader?.Type == HeaderType.Extended) | ||
{ | ||
throw new InvalidOperationException($"The block \"{block.Hash.ToString(true)}\" exists already on the blockchain."); | ||
} | ||
|
||
if (blockHeader != null && blockHeader.Hash != block.Hash) | ||
{ | ||
throw new InvalidOperationException($"The block \"{block.Hash.ToString(true)}\" has an invalid hash."); | ||
} | ||
|
||
return false; | ||
} | ||
#endregion | ||
|
||
#region Private Method | ||
private void HandleBlockHandlePersisted(object sender, BlockHeader[] e) | ||
{ | ||
this.OnBlockHeadersPersisted?.Invoke(sender, e); | ||
} | ||
#endregion | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
src/NeoSharp.Core/Blockchain/Processing/IBlockPersister.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,19 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NeoSharp.Core.Models; | ||
|
||
namespace NeoSharp.Core.Blockchain.Processing | ||
{ | ||
public interface IBlockPersister | ||
{ | ||
Block LastPersistedBlock { get; } | ||
|
||
event EventHandler<BlockHeader[]> OnBlockHeadersPersisted; | ||
|
||
Task Persist(params Block[] block); | ||
|
||
Task Persist(params BlockHeader[] blockHeaders); | ||
|
||
Task<bool> IsBlockPersisted(Block blocks); | ||
} | ||
} |
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