Mining/Proof-Of-Work Rewards.
This commit is contained in:
@@ -6,14 +6,17 @@ namespace BasicBlockChain
|
|||||||
{
|
{
|
||||||
public class BlockChain
|
public class BlockChain
|
||||||
{
|
{
|
||||||
|
private List<Transaction> _pendingTransactions = new List<Transaction>();
|
||||||
public List<Block> Chain { get; } = new List<Block>();
|
public List<Block> Chain { get; } = new List<Block>();
|
||||||
public int Difficulty { get; set; } = 2;
|
public int Difficulty { get; set; } = 2;
|
||||||
|
public int Reward { get; set; } = 1_000_000;
|
||||||
|
|
||||||
public BlockChain(DateTime? genesisDate = null, int difficulty = 2)
|
public BlockChain(DateTime? genesisDate = null, int difficulty = 2, int reward = 1_000_000)
|
||||||
{
|
{
|
||||||
Block genesisBlock = new Block(genesisDate ?? DateTime.UtcNow, null, null);
|
Block genesisBlock = new Block(genesisDate ?? DateTime.UtcNow, null, null);
|
||||||
genesisBlock.Mine(difficulty);
|
genesisBlock.Mine(difficulty);
|
||||||
Difficulty = difficulty;
|
Difficulty = difficulty;
|
||||||
|
Reward = 1_000_000;
|
||||||
Chain.Add(genesisBlock);
|
Chain.Add(genesisBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,6 +28,21 @@ namespace BasicBlockChain
|
|||||||
Chain.Add(newBlock);
|
Chain.Add(newBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void AddTransaction(Transaction transaction)
|
||||||
|
{
|
||||||
|
_pendingTransactions.Add(transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ProcessPendingTransactions(DateTime date, string miner)
|
||||||
|
{
|
||||||
|
Block lastBlock = Chain.Last();
|
||||||
|
Block newBlock = new Block(date, lastBlock, _pendingTransactions);
|
||||||
|
newBlock.Transactions.Add(new Transaction(null, miner, Reward, date));
|
||||||
|
newBlock.Mine(Difficulty);
|
||||||
|
Chain.Add(newBlock);
|
||||||
|
_pendingTransactions.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
public bool Verify()
|
public bool Verify()
|
||||||
{
|
{
|
||||||
for (int i = 1; i < Chain.Count; i++)
|
for (int i = 1; i < Chain.Count; i++)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace BasicBlockChain
|
namespace BasicBlockChain
|
||||||
{
|
{
|
||||||
@@ -14,9 +13,12 @@ namespace BasicBlockChain
|
|||||||
Console.WriteLine("#### Mining BlockChain with sample data");
|
Console.WriteLine("#### Mining BlockChain with sample data");
|
||||||
var startTime = DateTime.UtcNow;
|
var startTime = DateTime.UtcNow;
|
||||||
BlockChain nullCoin = new BlockChain(genesisDate: new DateTime(2000, 1, 1), difficulty: 2);
|
BlockChain nullCoin = new BlockChain(genesisDate: new DateTime(2000, 1, 1), difficulty: 2);
|
||||||
nullCoin.AddBlock(new DateTime(2000, 1, 2), new List<Transaction> { new Transaction("VAR", "NAM", 10_000_000) });
|
nullCoin.AddTransaction(new Transaction("VAR", "NAM", 10_000_000, new DateTime(2000, 1, 2)));
|
||||||
nullCoin.AddBlock(new DateTime(2000, 1, 3), new List<Transaction> { new Transaction("NAM", "VAR", 5_000_000) });
|
nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 2), "Kable");
|
||||||
nullCoin.AddBlock(new DateTime(2000, 1, 4), new List<Transaction> { new Transaction("NAM", "VAR", 5_000_000) });
|
nullCoin.AddTransaction(new Transaction("NAM", "VAR", 5_000_000, new DateTime(2000, 1, 3)));
|
||||||
|
nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 3), "Kable");
|
||||||
|
nullCoin.AddTransaction(new Transaction("NAM", "VAR", 5_000_000, new DateTime(2000, 1, 4)));
|
||||||
|
nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 4), "Kable");
|
||||||
Console.WriteLine(jsonWriter.Write(nullCoin));
|
Console.WriteLine(jsonWriter.Write(nullCoin));
|
||||||
var endTime = DateTime.UtcNow;
|
var endTime = DateTime.UtcNow;
|
||||||
Console.WriteLine($"Duration: {endTime - startTime}");
|
Console.WriteLine($"Duration: {endTime - startTime}");
|
||||||
@@ -27,18 +29,19 @@ namespace BasicBlockChain
|
|||||||
// Show balance
|
// Show balance
|
||||||
Console.WriteLine("Balance of \"{0}\": {1}", "VAR", nullCoin.GetMicroCoinBalance("VAR"));
|
Console.WriteLine("Balance of \"{0}\": {1}", "VAR", nullCoin.GetMicroCoinBalance("VAR"));
|
||||||
Console.WriteLine("Balance of \"{0}\": {1}", "NAM", nullCoin.GetMicroCoinBalance("NAM"));
|
Console.WriteLine("Balance of \"{0}\": {1}", "NAM", nullCoin.GetMicroCoinBalance("NAM"));
|
||||||
|
Console.WriteLine("Balance of \"{0}\": {1}", "Kable", nullCoin.GetMicroCoinBalance("Kable"));
|
||||||
|
|
||||||
// Tamper with the data
|
// Tamper with the data
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine("#### Tampering with the data");
|
Console.WriteLine("#### Tampering with the data");
|
||||||
nullCoin.Chain[1].Transactions.Clear();
|
nullCoin.Chain[1].Transactions[0].MicroCoinAmount = 1000_000_000;
|
||||||
nullCoin.Chain[1].Transactions.Add(new Transaction("VAR", "NAM", 1000_000_000));
|
|
||||||
Console.WriteLine(jsonWriter.Write(nullCoin));
|
Console.WriteLine(jsonWriter.Write(nullCoin));
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
Console.WriteLine("BlockChain is Valid? {0}", nullCoin.Verify() ? "True" : "False");
|
Console.WriteLine("BlockChain is Valid? {0}", nullCoin.Verify() ? "True" : "False");
|
||||||
Console.WriteLine("Balance of \"{0}\": {1}", "VAR", nullCoin.GetMicroCoinBalance("VAR"));
|
Console.WriteLine("Balance of \"{0}\": {1}", "VAR", nullCoin.GetMicroCoinBalance("VAR"));
|
||||||
Console.WriteLine("Balance of \"{0}\": {1}", "NAM", nullCoin.GetMicroCoinBalance("NAM"));
|
Console.WriteLine("Balance of \"{0}\": {1}", "NAM", nullCoin.GetMicroCoinBalance("NAM"));
|
||||||
|
Console.WriteLine("Balance of \"{0}\": {1}", "Kable", nullCoin.GetMicroCoinBalance("Kable"));
|
||||||
|
|
||||||
Console.Read();
|
Console.Read();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
namespace BasicBlockChain
|
using System;
|
||||||
|
|
||||||
|
namespace BasicBlockChain
|
||||||
{
|
{
|
||||||
public class Transaction
|
public class Transaction
|
||||||
{
|
{
|
||||||
public string Sender { get; set; }
|
public string Sender { get; set; }
|
||||||
public string Receiver { get; set; }
|
public string Receiver { get; set; }
|
||||||
public long MicroCoinAmount { get; set; }
|
public long MicroCoinAmount { get; set; }
|
||||||
|
public DateTime Date { get; set; }
|
||||||
|
|
||||||
public Transaction(string sender, string receiver, long microCoinAmount)
|
public Transaction(string sender, string receiver, long microCoinAmount, DateTime date)
|
||||||
{
|
{
|
||||||
Sender = sender;
|
Sender = sender;
|
||||||
Receiver = receiver;
|
Receiver = receiver;
|
||||||
MicroCoinAmount = microCoinAmount;
|
MicroCoinAmount = microCoinAmount;
|
||||||
|
Date = date;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user