Skip to content

Commit 893d09d

Browse files
Alexandr DolenkoAlexandr Dolenko
Alexandr Dolenko
authored and
Alexandr Dolenko
committed
added validation to blockchain client
1 parent 5f5b1a9 commit 893d09d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

block-chain/BlockChainCore/BlockChain.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public IBlock CreateBlock(string data)
2828
return block;
2929
}
3030

31+
/// <summary>
32+
/// Adds an existing block to the blockchain.
33+
/// </summary>
34+
/// <param name="block">The block to be added.</param>
35+
/// <exception cref="InvalidOperationException">Thrown when the block's previous hash does not match the hash of the last block in the blockchain.</exception>
3136
public void AddBlock(IBlock block)
3237
{
3338
var previousBlock = _blocks.Last();
@@ -81,5 +86,5 @@ public bool IsValid()
8186

8287
return true;
8388
}
84-
89+
8590
}

block-chain/bccli/Program.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,24 @@ await Task.Run(() =>
6969
Console.WriteLine(lastJson);
7070
continue;
7171
}
72-
if (command.ToLower() == "clear")
72+
if (command.Equals("clear", StringComparison.OrdinalIgnoreCase))
7373
{
7474
Console.Clear();
7575
continue;
7676
}
77+
if (command.Equals("valid", StringComparison.OrdinalIgnoreCase))
78+
{
79+
var isValid = blockChain.IsValid();
80+
Console.WriteLine($"Chain is valid: {isValid}");
81+
continue;
82+
}
7783
if (command.ToLower() == "help")
7884
{
7985
Console.WriteLine("Commands: ");
8086
Console.WriteLine("add 'item to add' - add a new block to the chain");
8187
Console.WriteLine("showchain - to exit");
8288
Console.WriteLine("showlast - to display the last block");
89+
Console.WriteLine("valid - to check if the chain is valid");
8390
Console.WriteLine("clear - to clear the console");
8491
Console.WriteLine("help - to display this message");
8592
continue;

0 commit comments

Comments
 (0)