Simple Blockchain example in Java
5 stars based on
61 reviews
This is an annotation Jar I have created which adds Blockchain capabilites to any Java application including Android apps. I wrapped up the alpha of this today, it works.
Creating a blockchain ledger can provide self validating data. However, there is coding overhead and discovery involved. In order to make adding blockchain abilities easy, I created this annotation preprocessor for Java applications. What that means is, very little code combines with the preprocessor annotations provides a lot of software mechanics, without writing them.
In the "wired" age, we have to trust not only software, we have to trust data. As we decentralize our data centers into multiple fall overs redundanciesload balancing multiple front doors and such creatures as servlets multiple exploitable mini server code chunks the opportunities for bad actors increases. If one servlet, on one fall over is compromised then due to traffic volume the number of users can be affected may be quite high.
This is of course in addition to the media dissection, bad publicity, leaving room for competitors to take customers. With an annotated class of variables and a dash of business logic, one can create an entire block chain based self validating system. The business logic is clean, simple, easy to sustain and simple to add onto. The image shows our example application.
It is a simple implementation. There is a class "blockTest. The compiler uses these annotations to derive and write a new class based on "blockTest". It will write a class, on it's own at compile time, called "testBlockChainClass. We then create a class called "BlockChainTest. As it extends BlockChainTest, all of our runtime, base variables, setters and getters are now accessible.
The business logic that you see is all we need to create a fully functional blockchain system. It will add a ledger, create 5 records and send them to the ledger, and retrieve record 3. If we wanted to add a remote ledger or 2, it would be extremely simple see API reference on ledgers below for how this is done. The basic architecture is as follows. What can we use this for? Here a smart phone app verifies that the data within the local db and the remote ledgers are in alignment verifying that the remotes have not been tampered with, nor the local db.
There are times one does not want the blockchain mechanics to sit on the client side of a system for security and to avoid reverse engineering. The ledgers, validation and block chain mechanics can exist on the server side. The data may not be sent to the client if a compromised situation is determined.
Here is an implementation where an email server pumps incoming and outgoing emails to ledgers. The email server does not implement "fetch" so the data only goes one way to the ledgers. The ledgers are monitored by another application which verifies that the data has not been tampered with. Nothing has been modified, removed or artificially added.
This is handy for heavily regulated environments where email, email chains must be retained in order for compliance. Due to the mechanics, the system provides verifiable proof that the emails are not altered, deleted or altered. The ledgers may be distributed to remote data centers to provide redundancy as can the auditing system.
In this case, a data acquisition system for example pharma or diagnosticsretains all data and the paper trail for the raw data.
It also does so for the data analytics. This ensures raw data and results have not been altered. This may be good for a regulated environment where not just the data, but the peoples actions must be ensured. Multiple ledgers exist after acquisition to verify data integrity. The same exists for data analysis.
As the ledgers are additive, the historical sequence of events are stored to retain the events leading up to conclusions. All 4 remote ledgers are available for regulatory in order to validate integrity of the data and processes for compliance.
This may also be beneficial in defending an IP portfolio. The blockChain annotation system is not designed to exhibit the continuously degrading performance characteristic of cryptocurrency mining. In this tool set, that is avoided in order to provide usability. I suppose one can use this for crypto if they wanted to, however the goal of this is to be more general purpose.
The intent is to create a tool where the data is self validating and can leverage inference to audit the integrity of the run time software that creates it. So where will this annotation processor go? I have no idea at this time. If there is interest let me know if this thing sounds usefuland if I can make this worthwhile, then great! If not, then it was still a lot of fun to create.
By adding simple annotations, the entire heavy lifting of multiple ledgers that co-validate data, with data that self validates within it's link in a blockchain can be added with a few symbols and a handful of API calls. In your project properties, enable annotation processing, then add the jar in the Factory Path.
Now every time the project builds, the annotations will direct the compiler to execute the BlockChain. The BlockControl annotation declares a class as a "Block Chain" class. It is a class with variables that are accessed via setters and getters standard Java stuff. This will make more sense later on.
GetLedgerVariable denotes a getter which will provide a member variable value. By adding this annotation, that getter is now linked into the block chain. When you send your data out to the ledger s it will be pulled from this method.
SetLedgerVariable denotes a setter which will set a member variable value. By adding this annotation, that setter is now linked into the block chain and will be called when data returns from the ledger sprovided they are validated. The preprocessor will use your annotations and create this class file with everything predone. All of this can be accessed via a couple of API calls. This is a section of what the class looks like with the annotations. Just simply primitive variables, setters and getters.
I've tried to make the API calls as simple as possible. There are just a few in order to reduce code complexity. The createNewLedgerRecord indicates that you will be pushing out a new record to the ledgers. It performs all of the required initialization and preparation of a new ledger entry, including the "rolling" key. The generateLedgerEntry call commits your data to the ledgers. When called, it will call your getters to get those current values. It will perform all of the work in order to create a valid link and build the payload in JSON.
One call to this, and the current state of your data is sent to all ledgers. Included are the rolling key which will move forward, which sits internally, ready for the next commit. It will pull all records from all ledgers for the requested record.
It will then validate that they are all in alignment. If even one ledger is out of sync an error is flagged and a LedgerExpection is thrown. If the ledgers are in alignment, it will pull the data for the requested record, and extract all of the data as well as reconfirm that the block chain entry is accurate.
It will also validate that the data has not been tampered with. If any checks fail, the data is zero'd out and an error is flagged. If there is no error, the the data within you class is auto filled with the validated data.
The return value is true if the data passed validation, false if it failed. We can set some values within the system via a few API calls, or leave them as they are built by the preprocessor. It will default to what is declared in the BlockControl annotation. There is no limit to the size of the key. There is no limited character set. The keyLength determines the number of bytes within the block control validation.
The default value is bytes bits. You may set any value from 64 to bytes in length bits. A ledger tool is use to create and verify a payload of a set of values.
One can create a ledger tool, then use setLedgerTool to link it in so it is used. That way, custom validation can be created.
Alternatively one can rely on their "key" as set within the BlockControl annotation. A ledger can be a local database, flat file, SQL database, remote server, realm db or other thing one can think up. There is also no limit at this time to the number and types of ledgers you may connect. For example, you may use an SQL db on a mobile phone which also publishes to a remote server via secure comms. You may have a dozen remotes which will then all be part of the data validation.