To create a new BlockChain object, you only need a genesis string and a HashType.
BlockChain chain = new BlockChain("genesis string", Util.HashType.SHA3_256)
If your blockchain will only store string data, it is preferred to use a StringChain.
StringChain chain = new StringChain("genesis", Util.HashType.SHA3_256)
The genesis string is what will be used to generate the previous hash for the first block. The hash type just specifies whether the SHA 256 or the SHA-3 hashing algorithm will be used. The difference comes down to preformance vs security.
At their essence, blockchains look something like this:
If any data in any block is altered, so would its hash, invalidating the condition that the next block hold the previous block's hash. This process can be explained more in depth, but what it means for us now is that no modifications can happen on any block except the last.
Because of this, adding data is simple.
chain.add("party X transferred $20 to party A");
chain.add("party M transferred $90 to party D");
chain.add("party D transferred $30 to party F");
Typically every x entries, or every predetermined time period, the head block is sealed and data begins being stored in a new head block. Every time this happens, the hash of the previous head is established, stored in the new head, and the data is recorded forever. Commanding this is also a simple process.
chain.nextBlock();
Now all new data added will go in a new block. Run nextBlock() again and further data will go in a third block.
To see your blockchain at this point, a simple toString() will do.
The full list of what you can do with your blockchain is as follows:
boolean nextBlock();
boolean add(Object item);
boolean add(Object... items);
int size();
int totalItems();
void discardBlock();
int find(Object item);
int soonestTo(Date timestamp);
Object[] getContents(int blockID);
Object[][] toArray();
boolean isCurrentEmpty();
boolean isCurrentEmpty();
boolean verify();
String toString();
String serialize();
Blockchains work best decentralized, with various sources having a copy of the chain and saving to a file when changes are made.
To save a blockchain to a file, simply do
Util.save("filename.txt", chain);
Internally, this runs serialize() on the chain and prints it to a specified file. It should look something like this:
To load this file onto a blockchain object, do:
StringChain chain = Util.load("filename.txt")
As of now, only StringChains can be loaded from files. This is because blockchains can only be saved in text files. Database support will be added in the future.