The deterministic JVM in action
April 15, 2020
A key requirement for any distributed ledger system is the ability to guarantee consensus between ledger participants. Among other things, this means that every ledger participant must agree on whether a given transaction is valid or not, every time it is verified. We refer to this property as determinism.
To achieve this capability, Corda includes a DJVM — or Deterministic Java Virtual Machine — a component for enforcing determinism in the evaluation of Corda smart contracts (two blog posts provide more details on why such a component is necessary).
A partial preview of the DJVM was first delivered in Corda 4. This preview gave developers the ability to try out the DJVM’s bytecode rewriter. The DJVM uses this rewriter to automatically rewrite bytecode to catch calls to non-deterministic methods. Developers could run this tool against their smart contract code to see what bytecode transformations it performs.
Now, the release of Corda 4.4 marks a major milestone for the DJVM:
- Nodes can be configured to use the DJVM for every smart contract they evaluate, either via a configuration file or via the Corda DemoBench. Any nodes configured in this way will automatically detect and reject any non-deterministic transactions they receive
- A new Gradle script plugin allows most non-determinism in smart contracts to be detected at compile time, before a node is even started
The final step, which has been left for a future release, is to integrate the DJVM with Corda’s consensus rules. This will allow Corda networks to (gradually) enforce the use of the DJVM for all transactions on the ledger, eradicating non-deterministic transactions.
However, to bring the DJVM to a conclusion, we need your help. Every CorDapp implements smart contracts slightly differently. At one end, there are smart contracts that impose no restrictions at all. At the other are contracts amounting to full XML processing engines. The more complicated the smart contract, the more likely it is to break the DJVM’s rules on non-determinism.
Some of these rule-breakings will represent actual non-determinism — a smart contract making a network call — but others may not — perhaps a smart contract innocently attempting to use a method from a package that also contains a random number generator. Your input is essential to allow us to understand how real-world smart contracts are written, so that we can modify the DJVM to handle these edge-cases while maintaining the promise of complete determinism.
So if you’re a CorDapp developer, please download Corda 4.4, enable the DJVM, and give it a test run! We look forward to hearing what works and what doesn’t at [email protected].
Let’s give it a whirl
Next, let’s look at a simple demo of the DJVM in action. We’ll start by defining a hopelessly non-deterministic smart contract — in our case, a naïve attempt to prevent from evolving past December 31st 2020:
public class DoofusContract implements Contract { public static final String ID = "com.contracts.DoofusContract"; @Override public void verify(LedgerTransaction tx) { Instant currentTime = Instant.now(); Instant startOf2021 = LocalDate .parse("2021-01-01") .atStartOfDay(ZoneId.of("Europe/London")) .toInstant(); if (currentTime.isAfter(startOf2021)) { throw new IllegalArgumentException("Too late!"); } } public interface Commands extends CommandData { class Action implements Commands {} } }
This contract is problematic. Even apparently-valid transactions committed in 2020 will suddenly stop being valid if they’re re-verified in 2021, compromising the integrity of the entire ledger.
We configure our node to use the DJVM for all transaction verification by adding the following line to the node’s node.conf file:
systemProperties = { “net.corda.djvm” = true }
Now let’s build a transaction using this contract and watch the sparks fly!
This is a simple example, but the DJVM prevents a wide range of non-determinism in smart contracts. You can read more about the full set of rules it implements here.
The deterministic JVM in action was originally published in Corda on Medium, where people are continuing the conversation by highlighting and responding to this story.