I am going to need to write some of this down before I forget - part 1.
.
A transaction can be anything in this case. A transaction is an abstract idea, an interface, and I left the definition reasonably open at this point. Still, I assume that whatever class implements this interface can be serialised into a JSON object.
public interface ITransaction
{
}
The most basic example is a block of text, a note.
public class NoteTransaction : ITransaction
{
public string Note { get; set; } = string.Empty;
}
Any such “transaction” gets wrapped in audit information: when and wherefrom it came into the blockchain and a comment field: an optional description.
public class TransactionWrapper
{
public ITransaction Transaction { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.Now;
public string Description { get; set; } = string.Empty;
public string CreatedBy { get; set; } = string.Empty;
}
That’s the nucleus of a blockchain.
A list of transactions then is what makes up a block. That, and the same audit information as we’ve put on individual transactions.
public class Block
{
public List<TransactionWrapper> Transactions { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.Now;
public string CreatedBy { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
A block then also gets wrapped, not into the audit information (we made that part of the block), but into a hash value. […]Hash values represent large amounts of data as much smaller numeric values[…], see Wikipedia. In this context we’ll use the hash value of a block of transactions first of all to compare two blocks. And we also use hash values to check the integrity of a block: if somebody modifies one or more of the transactions in a block, then their hash value won’t match anymore.
The first bit of code that calculates a hash value as soon as you assign a block of transactions:
public class BlockWrapper
{
public string BlockHash { get; set; } = string.Empty;
private PreviousBlockLink _Block;
// Hash the block as soon as it is "set"
public PreviousBlockLink Block
{
get { return _Block; }
set
{
var json = JsonConvert.SerializeObject(value);
_Block = value;
BlockHash = HashHelper.Hash(json);
}
}
}
Also, worth noting is the fact that in a Blockchain blocks are linked together in a chain, each block also contains the hash value of the previous block. Thereby the hash value of one block gets wrapped and “hashed” into the subsequent block.
public class PreviousBlockLink
{
public string PreviousBlockHash { get; set; } = string.Empty;
public Block LinkedBlock { get; set; }
}
So if somebody modifies one or more of the transactions in a block, and updates the hash values accordingly, that would invalidate the next block and so forth.
A Blockchain itself is then quite simple. It is initially just a list of Blocks, a list of transactions that have not been added to a block yet, and, well, a name.
public class Blockchain
{
public string Name { get; set; }
public List<BlockWrapper> Blocks { get; set; } = new List<BlockWrapper>();
public List<TransactionWrapper> Transactions { get; set; }
= new List<TransactionWrapper>();
}
0 comment(s)