BlockChain: Basic users handling.
This commit is contained in:
@@ -11,12 +11,66 @@ namespace BasicBlockChain.Core
|
|||||||
public int Difficulty { get; set; } = 2;
|
public int Difficulty { get; set; } = 2;
|
||||||
public int Reward { get; set; } = 1_000_000;
|
public int Reward { get; set; } = 1_000_000;
|
||||||
|
|
||||||
|
private List<string> _users = null;
|
||||||
|
|
||||||
|
public List<string> Users
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_users == null)
|
||||||
|
{
|
||||||
|
InitUsers();
|
||||||
|
}
|
||||||
|
return _users;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitUsers()
|
||||||
|
{
|
||||||
|
_users = new List<string>();
|
||||||
|
foreach (Block block in Chain)
|
||||||
|
{
|
||||||
|
foreach (Transaction transaction in block.Transactions)
|
||||||
|
{
|
||||||
|
AddUser(transaction.Sender, false);
|
||||||
|
AddUser(transaction.Receiver, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddUser(string user, bool init = true)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(user)) { return; }
|
||||||
|
if (_users == null)
|
||||||
|
{
|
||||||
|
if (init)
|
||||||
|
{
|
||||||
|
InitUsers();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (_users.Contains(user)) { return; }
|
||||||
|
_users.Add(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddUsersOfBlock(Block block)
|
||||||
|
{
|
||||||
|
foreach (Transaction transaction in block.Transactions)
|
||||||
|
{
|
||||||
|
AddUser(transaction.Sender);
|
||||||
|
AddUser(transaction.Receiver);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public BlockChain(DateTime? genesisDate = null, int difficulty = 2, int reward = 1_000_000)
|
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;
|
Reward = reward;
|
||||||
Chain.Add(genesisBlock);
|
Chain.Add(genesisBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,6 +79,7 @@ namespace BasicBlockChain.Core
|
|||||||
Block lastBlock = Chain.Last();
|
Block lastBlock = Chain.Last();
|
||||||
Block newBlock = new Block(date, lastBlock, transactions);
|
Block newBlock = new Block(date, lastBlock, transactions);
|
||||||
newBlock.Mine(Difficulty);
|
newBlock.Mine(Difficulty);
|
||||||
|
AddUsersOfBlock(newBlock);
|
||||||
Chain.Add(newBlock);
|
Chain.Add(newBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +94,7 @@ namespace BasicBlockChain.Core
|
|||||||
Block newBlock = new Block(date, lastBlock, PendingTransactions);
|
Block newBlock = new Block(date, lastBlock, PendingTransactions);
|
||||||
newBlock.Transactions.Add(new Transaction(null, miner, Reward, date));
|
newBlock.Transactions.Add(new Transaction(null, miner, Reward, date));
|
||||||
newBlock.Mine(Difficulty);
|
newBlock.Mine(Difficulty);
|
||||||
|
AddUsersOfBlock(newBlock);
|
||||||
Chain.Add(newBlock);
|
Chain.Add(newBlock);
|
||||||
PendingTransactions.Clear();
|
PendingTransactions.Clear();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user