Add Mining/Proof-of-Work

This commit is contained in:
2019-05-23 00:40:40 +02:00
parent 3352ce5273
commit 1fde961709
3 changed files with 22 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ namespace BasicBlockChain
public string PreviousHash { get; set; } public string PreviousHash { get; set; }
public string Data { get; set; } public string Data { get; set; }
public string Hash { get; set; } public string Hash { get; set; }
public int Nonce { get; set; }
public Block(DateTime date, Block previousBlock, string data) public Block(DateTime date, Block previousBlock, string data)
{ {
@@ -25,11 +26,21 @@ namespace BasicBlockChain
{ {
SHA256 sha256 = SHA256.Create(); SHA256 sha256 = SHA256.Create();
byte[] dataBytes = Encoding.UTF8.GetBytes(string.Format("{0}-{1}-{2}", Date, PreviousHash, Data)); byte[] dataBytes = Encoding.UTF8.GetBytes(string.Format("{0}-{1}-{2}-{3}", Date, PreviousHash, Data, Nonce));
byte[] hashBytes = sha256.ComputeHash(dataBytes); byte[] hashBytes = sha256.ComputeHash(dataBytes);
string hash = Convert.ToBase64String(hashBytes); string hash = Convert.ToBase64String(hashBytes);
return hash; return hash;
} }
public void Mine(int difficulty)
{
var leadingZeros = new string('0', difficulty);
while (Hash.Substring(0, difficulty) != leadingZeros)
{
Nonce++;
Hash = CalculateHash();
}
}
} }
} }

View File

@@ -7,10 +7,13 @@ namespace BasicBlockChain
public class BlockChain public class BlockChain
{ {
public List<Block> Chain { get; } = new List<Block>(); public List<Block> Chain { get; } = new List<Block>();
public int Difficulty { get; set; } = 2;
public BlockChain(DateTime? genesisDate = null) public BlockChain(DateTime? genesisDate = null, int difficulty = 2)
{ {
Block genesisBlock = new Block(genesisDate ?? DateTime.UtcNow, null, "{}"); Block genesisBlock = new Block(genesisDate ?? DateTime.UtcNow, null, "{}");
genesisBlock.Mine(difficulty);
Difficulty = difficulty;
Chain.Add(genesisBlock); Chain.Add(genesisBlock);
} }
@@ -18,6 +21,7 @@ namespace BasicBlockChain
{ {
Block lastBlock = Chain.Last(); Block lastBlock = Chain.Last();
Block newBlock = new Block(date, lastBlock, data); Block newBlock = new Block(date, lastBlock, data);
newBlock.Mine(Difficulty);
Chain.Add(newBlock); Chain.Add(newBlock);
} }

View File

@@ -10,12 +10,15 @@ namespace BasicBlockChain
// Example BlockChain with some example data // Example BlockChain with some example data
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("#### BlockChain with sample data"); Console.WriteLine("#### Mining BlockChain with sample data");
BlockChain nullCoin = new BlockChain(new DateTime(2000, 1, 1)); var startTime = DateTime.UtcNow;
BlockChain nullCoin = new BlockChain(genesisDate: new DateTime(2000, 1, 1), difficulty: 2);
nullCoin.AddData(new DateTime(2000, 1, 2), "{ sender: VAR, receiver: NAM, amount: 10.00}"); nullCoin.AddData(new DateTime(2000, 1, 2), "{ sender: VAR, receiver: NAM, amount: 10.00}");
nullCoin.AddData(new DateTime(2000, 1, 3), "{ sender: NAM, receiver: VAR, amount: 5.00}"); nullCoin.AddData(new DateTime(2000, 1, 3), "{ sender: NAM, receiver: VAR, amount: 5.00}");
nullCoin.AddData(new DateTime(2000, 1, 4), "{ sender: NAM, receiver: VAR, amount: 5.00}"); nullCoin.AddData(new DateTime(2000, 1, 4), "{ sender: NAM, receiver: VAR, amount: 5.00}");
Console.WriteLine(jsonWriter.Write(nullCoin)); Console.WriteLine(jsonWriter.Write(nullCoin));
var endTime = DateTime.UtcNow;
Console.WriteLine($"Duration: {endTime - startTime}");
// Verify // Verify
Console.WriteLine("BlockChain is Valid? {0}", nullCoin.Verify() ? "True" : "False"); Console.WriteLine("BlockChain is Valid? {0}", nullCoin.Verify() ? "True" : "False");