[ARCHIVE] Hello World … from the Aion Virtual Machine!


Now that the blockchain has officially said hello to the JVM, we thought it was time that the JVM returned the gesture. So let’s see what a Hello World smart contract written in Java actually looks like.

Anatomy Of A Java Smart Contract

The best way to understand the anatomy of a Java smart contract is to contrast it against what that same contract would look like written as a regular Java program.

Below is a simple Hello World program written in Java. It’s a little more verbose than usual. We’re printing the Hello World message in a separate method so that we can show you a couple of the fundamental features of how Java smart contracts work.

A regular Java program.

And below is that same program but written as a smart contract and able to be deployed to the blockchain.

A Java smart contract.

The similarities are obvious, but it’s the differences that matter. Let’s dive in and figure out what’s going on.

The Smart Contract Entry Point

The first point of difference between the two is the entry point into the code. A Java smart contract also has a main method as its entry point, but the signature of its main method differs slightly.

The Java smart contract returns a byte array instead of nothing. This return type in the main method is what allows a Java smart contract to be able to pass data back to its caller, whatever that data may be.

The other dissimilarity in the smart contract method signature is that it does not accept any input parameters. This does not mean that you cannot call into a Java smart contract with some specific input — of course you can, this would be a huge waste of time if you couldn’t. However, in the context of a blockchain, the input has a specific ABI encoding, and trying to capture that as an array of strings just doesn’t make much sense.

Passing Input Into The Smart Contract

Instead, the input you pass into the smart contract is captured by one of our provided API classes, called BlockchainRuntime, and you can retrieve this input by calling its getData method. This is exactly what’s happening in our example contract above.

So what kind of input do you pass into the Java smart contract? Well, the same kind you would pass into a solidity smart contract: the ABI encodings of the method name you want to invoke, followed by zero or more ABI encoded parameters that will be passed in to the invoked method. In our case, we would simply call the contract with the ABI encoding of the method name “sayHello”.

Execution Flow Of The Smart Contract

This brings us to the second API class we provide you with, ABIDecoder. The call to the decodeAndRunWithClass method takes the class we want to use (the HelloWorld class in this case), and interprets the ABI encoded input data and then proceeds to invoke the specified method on the provided class using the zero or more parameters that are also part of the input.

In other words, this method will see that we’re trying to call a static method named “sayHello” with zero parameters, and that this “sayHello” method is defined in the HelloWorld class. It will then invoke that method for us, and execution will flow as expected from there. (If we had an instance method instead then we would use ABIDecoder.decodeAndRunWithObject and pass in a HelloWorld instance rather than the class).

Blockchain-Safe Java

This takes us into the sayHello method, whose single line does exactly what you think it does. It prints “Hello World!” to the console, just the same as System.out.println does. Why not just use System.out? Well, not all of the functionality in the Java class library is “blockchain safe,” and the usage of the System class in particular is heavily restricted.

What exactly does blockchain-safe mean? We’ll dive into this term in greater detail and explain its implications in future additions to this series. For now, it suffices to say that smart contracts must have certain properties that regular programs are not bound by, the biggest of which is that smart contract code must always be completely deterministic. But as I’ve said, we’ll outline what all of this means in upcoming posts.

The Aion Virtual Machine Source Code

You can check out the source code for the Aion Virtual Machine (AVM) at our Github repository here, and run through the example for yourself.


written by

Aion

The blog for the digital asset of The Open Application Network