PARAMETA

Java SCORE (Smart Contract on Reliable Environment) on loopchain

2022.07.28Insight
Blog — ArticleScroll

Smart contracts

A smart contract is a script that takes a contract once written on paper, expresses it in code, and executes it when specified conditions are met — a program that runs on a blockchain. Thanks to the properties of blockchain, anyone can inspect the code and its results, and every execution produces a tamper-proof outcome recorded on chain. The great advantage of smart contracts is that automatically enforcing a contract once its conditions are satisfied cuts both the cost of enforcement and the odds of a dispute.

The classic example of a smart contract is a virtual asset (FT, or fungible token). Token ownership is recorded inside the smart contract and token transfers are implemented as a program that runs automatically. Ownership and transfer become possible without any third party stepping in.

Technically speaking: a user creates a transaction (TX, a call to a smart contract method), every node executes it, the nodes reach consensus on the result, and the result is written to a block — which makes the outcome impossible to tamper with. The execution code is also published transparently on the blockchain, so anyone can check how it behaves.

A smart contract is a program that runs on a blockchain, but it has to run across many nodes with different environments, and its results must be deterministic so that consensus is possible. It must contain no code whose output can vary node to node because of randomness or external variables. And since many smart contracts run on the same blockchain, no single contract may run for a long stretch — which is why most blockchains set something like a gas limit to cap how much code can execute at once.

The best-known programming language for writing smart contracts is Ethereum's Solidity. Solidity was designed from scratch as a smart-contract-only language that accommodates the constraints described above, and it runs on the EVM (Ethereum Virtual Machine) — which is why every Ethereum node embeds the Ethereum Virtual Machine.

Different blockchains use different programming languages for smart contract development: Solidity, Rust, Python, and Java, to name a few.

* Source: https://www.freecodecamp.org/news/the-most-popular-programming-languages-used-in-blockchain-development-5133a0a207dc

Java smart contracts

The smart contract execution environment of loopchain, developed in-house by PARAMETA, is SCORE (Smart Contract on Reliable Environment), and SCORE supports both Python and Java as contract languages. Both are general-purpose languages with the largest developer populations around, so nobody has to learn a new language to write a smart contract — the barrier to entry drops. Java in particular is one of the best-known and most stable programming languages, with a wealth of libraries and development tooling behind it.

So let's take a look at the Java smart contracts loopchain supports.

> The anatomy of a Java smart contract

Like any smart contract, a Java smart contract is broadly made up of Data, Functions, and Event Logs. Data divides into what is permanently written to blockchain state and what is created temporarily in memory. Anything recorded in state data is declared as a member of the class; temporary data is declared as a variable inside a function. Functions are Java methods, callable and executable through transactions. Event Logs are generated during function execution and written to the blockchain as logs, which makes tracking specific actions on chain much easier.

Java smart contract example )

/*

* Copyright 2020 ICONLOOP Inc.

*

* 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.iconloop.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

}

You can find fuller details at the following link. (https://www.icondev.io/icon-stack/smart-contracts)

> Java as a smart contract language

As explained above, a smart contract runs on every blockchain node and the nodes must reach consensus on the result, so the result has to be deterministic. Ordinary programming languages, however, often include random elements or depend on external environment variables in their implementations. Java carries such variables by default, which imposes some constraints when programming and means there are a few things to check before execution.

First, only the APIs (Application Programming Interfaces) available to smart contracts may be used. In other words, any API that produces a non-deterministic result or is influenced by external variables is off limits. You can find the list of permitted APIs at the following link. (https://www.icondev.io/support/advanced-topics/java-smart-contracts/allowed-methods)

Then, to check whether restricted APIs are being used and to insert the code needed for smart contract execution, a bytecode optimizer and a bytecode transformer run before the existing Java program executes. The bytecode optimizer extracts annotations, maps APIs, generates the body of Event Log functions, and optimizes code to reduce cost. The bytecode transformer checks for permitted API usage before the program actually runs, maps JCL classes, adds code for cost calculation, and handles the various restrictions that smart contract execution requires.

> Why Java is a good fit

Java is a stable language backed by a broad range of libraries and development tools, and it is used by an enormous number of developers — so adapting it to smart contract development is fast. Java is especially common in backend server development, and since a smart contract is in a sense a program playing the role of a backend server, it becomes that much easier for backend developers to get involved.

Java also delivers performance close to native programs thanks to JIT (Just in Time) compilation, even though it runs on a virtual machine. Transaction throughput is the single biggest bottleneck in blockchain performance, and scripting languages like Python — or virtual-machine-based systems like the EVM — inevitably hit a speed ceiling.

Look at the graph comparing the performance of a smart contract written in Java (javaee) against one written in Python (pyee3), and Java's advantage is unmistakable. (The Java contracts are measured across a range of environments — static, instance, and so on.)

  • * References
  • https://www.icondev.io/getting-started/how-to-write-a-smart-contract
  • https://www.icondev.io/icon-stack/smart-contracts
  • [Reference] Visit the blog section of the official PARAMETA website ▼

Java SCORE (Smart Contract on Reliable Environment) on loopchain

Smart contracts. A smart contract is a script that takes a contract once written on paper, expresses it in code, and executes it when specified conditions are met — a program that runs on a blockchain. Thanks to the properties of blockchain, anyone can inspect the code and its results, and every execution produces a tamper-proof outcome recorded on chain. The great advantage of smart contracts is that automatically enforcing a contract once its conditions are satisfied cuts both the cost of enforcement and the odds of a dispute. Smart contracts ...

www.iconloop.com

Web3 Enabler, PARAMETA

Back to list