BLOG
Java SCORE in loopchain
2023.06.19
Greetings from the PARAMETA Team,
Smart Contract
A smart contract is a program that runs on a blockchain, which is a script that implements a contract that was previously written in code and allows the contract to be executed when certain conditions are met. Using the characteristics of the blockchain, anyone can check the code and its results, and the unforgeable execution results are generated and recorded on the blockchain. A major advantage of smart contracts is that they can reduce the cost of enforcing contracts and the possibility of disputes by automatically fulfilling contracts when certain conditions are met.
A prime example of a smart contract is a cryptocurrency (FT, Fungible Token). By recording the owner of the token within a smart contract and implementing the transfer of the token as a program to be executed automatically, it becomes possible to own and transfer tokens without the need for third-party intervention.
Technically, we can explain that the user-generated transactions (TXs, Method calls in smart contracts) are executed by each node and recorded in a block after agreeing on the result, making it impossible to falsify the result of the execution, and the code for the execution is also transparently disclosed on the blockchain so that anyone can check how it works.
How Smart Contracts Work
A smart contract is a program that runs on a blockchain, but it must run on multiple nodes in different environments, and the outcome must be ‘deterministic’ to allow for consensus. The program should not contain code that can change from node to node due to randomness or external variables, and no single smart contract should run for long periods of time as multiple smart contracts run on the same blockchain. This is because most blockchains have something like a gas limit, which limits the amount of code that can be executed at one time.
The leading programming language for writing smart contracts is Solidity of Ethereum. Solidity is a newly designed smart contract-specific programming language that takes into account the aforementioned limitations of smart contracts and runs on top of the Ethereum Virtual Machine (EVM), which means that each node on Ethereum contains an Ethereum Virtual Machine.
Different blockchains use different programming languages to write smart contracts, including Solidity, Rust, Python, and Java.
Smart Contract Languages by Blockchain
Java Smart Contract
PARAMETA’s own smart contract execution environment for loopchain is SCORE (Smart Contract on Reliable Environment), which supports Python and Java as smart contract writing languages. Both languages are general-purpose programming languages with the largest number of developers, which can lower the barrier to entry by eliminating the need to learn a new language when developing smart contracts. In particular, Java is one of the most well-known and stable programming languages, and has a large number of libraries and programming support tools.
Let’s take a look at the Java smart contracts that loopchain supports.
Composition of a Java Smart Contract
A Java smart contract, like any other smart contract, consists of three main parts: Data, Function, and Event Log. First of all, Data can be categorized into data that is permanently recorded in blockchain state data and data that is temporarily created in memory. Data that is recorded in state data is declared as a member of a class, and data that is temporarily generated is declared as a variable in a function. A Function is a method in Java that can be called and executed through a transaction. An Event Log is created during the execution of a Function so that it can be logged on the blockchain, and it facilitates tracking of specific actions within the blockchain.
Example of a Java Smart Contract)
/* * Copyright 2020 PARAMETA Corp. * * Licensed under the Apache License, Version 2.0 (the “License”); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an “AS IS” BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.parameta.score.example; import score.Context; import score.annotation.External; import score.annotation.Payable; public class HelloWorld { private String name; public HelloWorld(String name) { this.name = name; } @External() public void setName(String name) { this.name = name; } @External(readonly=true) public String name() { return name; } @External(readonly=true) public String getGreeting() { String msg = “Hello ” + name + “!”; Context.println(msg); return msg; } @Payable public void fallback() { // just receive incoming funds } } |
* More details can be found at the following link : https://www.icondev.io/icon-stack/smart-contracts
Java as a Smart Contract
As mentioned earlier, smart contracts need to be executed on each blockchain node and agree on the outcome, so the results must be deterministic. However, common programming languages often include random elements in their implementation or rely on external environmental variables. Java also includes such variables by default, which can lead to programming constraints, and there are certain things that need to be checked before execution.
First, you should only APIs (Application Programming Interfaces) that are available to the smart contract, which means that APIs that produce non-deterministic results or are affected by external variables cannot be used.
* A list of available APIs can be found at the following link : https://www.icondev.io/support/advanced-topics/java-smart-contracts/allowed-methods
In addition, the Bytecode Optimizer and Bytecode Transformer are executed before the execution of the existing Java program to check whether the smart contract uses restricted APIs or to insert the code needed to execute the smart contract. The Bytecode Optimizer performs annotation extraction, API mapping, creation of ‘bodies’ for Event Log functions, and code optimization to reduce cost. The Bytecode Transformer checks whether the program uses the allowed APIs before actual execution, and handles many things that need to be restricted when executing a smart contract, such as mapping JCL classes and adding code for cost calculation.
Java Program Execution Process on a Blockchain
Advantages of Java Smart Contracts
Java is a stable programming language that supports a wide range of libraries and development tools and is used by many developers, so it can quickly adapt to smart contract development. Java is especially popular for backend server development, which can make it easier for backend developers to get involved since smart contracts are programs that act as a kind of backend server.
In addition, despite providing a virtual machine-based execution environment, Java uses JIT (Just in Time) technology to perform almost as well as native programs. The processing speed of transactions is the biggest bottleneck in blockchain performance, and scripting languages such as Java or Python, or virtual machine-based programs such as EVM, are bound to have speed limitations.
If you look at the graph comparing the performance of a smart contract written in Java (javaee) and a smart contract written in Python (pyee3), you can see that Java’s performance is superior. (Smart contracts written in Java are measured in various environments such as static and instance.)
Performance Comparison of Smart Contracts Implemented in Java and Python
* References
https://www.icondev.io/getting-started/how-to-write-a-smart-contract