robinmonjo / coincoin

Blockchain based cryptocurrency proof-of-concept built with Elixir. Feedback welcome
402 stars 54 forks source link

Improve polimorphism in the blockchain #11

Closed robinmonjo closed 6 years ago

robinmonjo commented 6 years ago

coincoin makes a strong distinction between the blockchain and the transactions stored on it.

The blockchain only requires that data stored in each block implements this simple protocol:

defprotocol Blockchain.Data do
  def hash(data)
  def verify(data, chain)
end

This is ok so far but the problem is that when data are sent through the network, they are JSON encode / decoded. So for example, when I send a Token.Transaction to the blockchain, I send it as a raw Map (because even if I don't once decoded data will be seen as a raw Map by other peers), so I had to implement the Blockchain.Data protocol for Map. This is not ok, Map is way to generic, that makes the code not reusable at all !

The only way I can think of to improve this is to store somewhere the type of the data in the blockchain. So when blocks get decoded the data field is not a Map but the user defined type.