Skip to content

Commit

Permalink
IBlockchain dependency removed from some of the MessageHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
aboimpinto committed Sep 28, 2018
1 parent 49d6244 commit f4ec481
Show file tree
Hide file tree
Showing 20 changed files with 1,054 additions and 582 deletions.
28 changes: 20 additions & 8 deletions src/NeoSharp.Application/Controllers/PromptBlockchainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class PromptBlockchainController : IPromptController
private readonly IBlockPool _blockPool;
private readonly ITransactionPool _transactionPool;
private readonly IBlockchain _blockchain;
private readonly IBlockModel _blockModel;
private readonly ITransactionModel _transactionModel;
private readonly IAssetModel _assetModel;
private readonly IBlockchainContext _blockchainContext;
private readonly IConsoleWriter _consoleWriter;
private readonly IConsoleReader _consoleReader;
Expand All @@ -27,20 +30,29 @@ public class PromptBlockchainController : IPromptController
/// Constructor
/// </summary>
/// <param name="blockchain">Blockchain</param>
/// <param name="blockModel">The Block Model.</param>
/// <param name="transactionModel"></param>
/// <param name="assetModel"></param>
/// <param name="blockchainContext">The block chain context class.</param>
/// <param name="blockPool">Block pool</param>
/// <param name="transactionPool">Transaction Pool</param>
/// <param name="consoleWriter">Console writter</param>
/// <param name="consoleReader">Console reader</param>
public PromptBlockchainController(
IBlockchain blockchain,
IBlockModel blockModel,
ITransactionModel transactionModel,
IAssetModel assetModel,
IBlockchainContext blockchainContext,
IBlockPool blockPool,
ITransactionPool transactionPool,
IConsoleWriter consoleWriter,
IConsoleReader consoleReader)
{
_blockchain = blockchain;
_blockModel = blockModel;
_transactionModel = transactionModel;
_assetModel = assetModel;
_blockchainContext = blockchainContext;
_blockPool = blockPool;
_transactionPool = transactionPool;
Expand Down Expand Up @@ -107,7 +119,7 @@ public void StateCommand()
[PromptCommand("header", Category = "Blockchain", Help = "Get header by index or by hash")]
public async Task HeaderCommand(uint blockIndex, PromptOutputStyle output = PromptOutputStyle.json)
{
_consoleWriter.WriteObject(await _blockchain?.GetBlockHeader(blockIndex), output);
_consoleWriter.WriteObject(await this._blockModel.GetBlockHeader(blockIndex), output);
}

/// <summary>
Expand All @@ -118,7 +130,7 @@ public async Task HeaderCommand(uint blockIndex, PromptOutputStyle output = Prom
[PromptCommand("header", Category = "Blockchain", Help = "Get header by index or by hash")]
public async Task HeaderCommand(UInt256 blockHash, PromptOutputStyle output = PromptOutputStyle.json)
{
_consoleWriter.WriteObject(await _blockchain?.GetBlockHeader(blockHash), output);
_consoleWriter.WriteObject(await this._blockModel.GetBlockHeader(blockHash), output);
}

/// <summary>
Expand All @@ -129,7 +141,7 @@ public async Task HeaderCommand(UInt256 blockHash, PromptOutputStyle output = Pr
[PromptCommand("block", Category = "Blockchain", Help = "Get block by index or by hash")]
public async Task BlockCommand(uint blockIndex, PromptOutputStyle output = PromptOutputStyle.json)
{
_consoleWriter.WriteObject(await _blockchain?.GetBlock(blockIndex), output);
_consoleWriter.WriteObject(await this._blockModel.GetBlock(blockIndex), output);
}

/// <summary>
Expand All @@ -140,7 +152,7 @@ public async Task BlockCommand(uint blockIndex, PromptOutputStyle output = Promp
[PromptCommand("block", Category = "Blockchain", Help = "Get block by index or by hash")]
public async Task BlockCommand(UInt256 blockHash, PromptOutputStyle output = PromptOutputStyle.json)
{
_consoleWriter.WriteObject(await _blockchain?.GetBlock(blockHash), output);
_consoleWriter.WriteObject(await this._blockModel.GetBlock(blockHash), output);
}

/// <summary>
Expand All @@ -151,7 +163,7 @@ public async Task BlockCommand(UInt256 blockHash, PromptOutputStyle output = Pro
[PromptCommand("tx", Category = "Blockchain", Help = "Get tx")]
public async Task TxCommand(UInt256 hash, PromptOutputStyle output = PromptOutputStyle.json)
{
_consoleWriter.WriteObject(await _blockchain?.GetTransaction(hash), output);
_consoleWriter.WriteObject(await this._transactionModel.GetTransaction(hash), output);
}

/// <summary>
Expand All @@ -163,7 +175,7 @@ public async Task TxCommand(UInt256 hash, PromptOutputStyle output = PromptOutpu
[PromptCommand("tx", Category = "Blockchain", Help = "Get tx by block num/tx number")]
public async Task TxCommand(uint blockIndex, ushort txNumber, PromptOutputStyle output = PromptOutputStyle.json)
{
var block = await _blockchain.GetBlock(blockIndex);
var block = await this._blockModel.GetBlock(blockIndex);
_consoleWriter.WriteObject(block.Transactions?[txNumber], output);
}

Expand All @@ -175,7 +187,7 @@ public async Task TxCommand(uint blockIndex, ushort txNumber, PromptOutputStyle
[PromptCommand("asset", Category = "Blockchain", Help = "Get asset", Order = 0)]
public async Task AssetCommand(UInt256 hash, PromptOutputStyle output = PromptOutputStyle.json)
{
_consoleWriter.WriteObject(await _blockchain?.GetAsset(hash), output);
_consoleWriter.WriteObject(await this._assetModel.GetAsset(hash), output);
}

/// <summary>
Expand All @@ -187,7 +199,7 @@ public async Task AssetCommand(UInt256 hash, PromptOutputStyle output = PromptOu
[PromptCommand("asset", Category = "Blockchain", Help = "Get asset", Order = 1)]
public async Task AssetCommand(string query, EnumerableExtensions.QueryMode mode = EnumerableExtensions.QueryMode.Contains, PromptOutputStyle output = PromptOutputStyle.json)
{
var assets = await _blockchain?.GetAssets();
var assets = await this._assetModel.GetAssets();
var result = assets.QueryResult(query, mode).ToArray();

_consoleWriter.WriteObject(result, output);
Expand Down
38 changes: 38 additions & 0 deletions src/NeoSharp.Core/Blockchain/AssetModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NeoSharp.Core.Models;
using NeoSharp.Core.Persistence;
using NeoSharp.Core.Types;

namespace NeoSharp.Core.Blockchain
{
public class AssetModel : IAssetModel
{
#region Private Fields
private readonly IRepository _repository;
#endregion

#region Constructor
public AssetModel(IRepository repository)
{
this._repository = repository;
}
#endregion

#region IAssetModel implementation

/// <inheritdoc />
public Task<Asset> GetAsset(UInt256 hash)
{
return this._repository.GetAsset(hash);
}

/// <inheritdoc />
public Task<IEnumerable<Asset>> GetAssets()
{
throw new NotImplementedException();
}
#endregion
}
}
133 changes: 133 additions & 0 deletions src/NeoSharp.Core/Blockchain/BlockModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using NeoSharp.Core.Models;
using NeoSharp.Core.Persistence;
using NeoSharp.Core.Types;
using Org.BouncyCastle.Crypto.Digests;

namespace NeoSharp.Core.Blockchain
{
public class BlockModel : IBlockModel
{
#region Private Fields
private readonly IRepository _repository;
private readonly ITransactionModel _transactionModel;
#endregion

#region Constructor
public BlockModel(IRepository repository, ITransactionModel transactionModel)
{
this._repository = repository;
_transactionModel = transactionModel;
}
#endregion

#region IBlocksModel implementation
/// <inheritdoc />
public async Task<Block> GetBlock(uint height)
{
var hash = await this.GetBlockHash(height);

return hash == null ? null : await GetBlock(hash);
}

/// <inheritdoc />
public async Task<Block> GetBlock(UInt256 hash)
{
var header = await this._repository.GetBlockHeader(hash);

if (header == null || header.Type != HeaderType.Extended) return null;

var transactions = new Transaction[header.TransactionCount];

for (int x = 0, m = header.TransactionCount; x < m; x++)
{
transactions[x] = await this._transactionModel.GetTransaction(header.TransactionHashes[x]);
}

header.Hash = hash;

return header.GetBlock(transactions);
}

/// <inheritdoc />
public async Task<IEnumerable<Block>> GetBlocks(IReadOnlyCollection<UInt256> blockHashes)
{
var blocks = new List<Block>();

foreach (var hash in blockHashes)
{
var block = await this.GetBlock(hash);

if (block == null) continue;

blocks.Add(block);
}

return blocks;
}

/// <inheritdoc />
public async Task<UInt256> GetBlockHash(uint height)
{
return await this._repository.GetBlockHashFromHeight(height);
}

/// <inheritdoc />
public async Task<Block> GetNextBlock(UInt256 hash)
{
var header = await this._repository.GetBlockHeader(hash);

if (header != null)
{
return await this.GetBlock(header.Index + 1);
}

return null;
}

/// <inheritdoc />
public async Task<UInt256> GetNextBlockHash(UInt256 hash)
{
var header = await this._repository.GetBlockHeader(hash);

if (header != null)
{
return await this._repository.GetBlockHashFromHeight(header.Index + 1);
}

return UInt256.Zero;
}

/// <inheritdoc />
public async Task<long> GetSysFeeAmount(uint height)
{
return GetSysFeeAmount(await GetBlockHash(height));
}

/// <inheritdoc />
public long GetSysFeeAmount(UInt256 hash)
{
return 0;
}

/// <inheritdoc />
public async Task<BlockHeader> GetBlockHeader(uint height)
{
var hash = await this._repository.GetBlockHashFromHeight(height);

if (hash != null) return await this.GetBlockHeader(hash);
return null;
}

/// <inheritdoc />
public async Task<BlockHeader> GetBlockHeader(UInt256 hash)
{
var header = await this._repository.GetBlockHeader(hash);

if (header != null) header.Hash = hash;
return header;
}
#endregion
}
}

0 comments on commit f4ec481

Please sign in to comment.