The smart contracts of hyperchain can be divided into two categories: contract library and contract process:
Contract library: can be deployed on the blockchain for other contract calls;
Contract process: can be executed, can call other contract processes or contract libraries, can be called by other contract libraries or procedures, can read/output contract execution results, and the output execution results can be stored on the chain for other contracts to read.
Program Example
Contract Library Example: Define a contract class and export it as a contract library for other smart contract codes to call.
class Student2 { //Define a class: Student2
constructor(name, sex, age) {
this.name = name;
this.sex = sex;
this.age = age;
}
toString() { //Custom formatted output for execution results to be stored on the chain
return 'The Student v2 is ' + this.name + ', and sex is ' + this.sex + ',age is ' + this.age;
}
}
export {Student2} //Declare that Student2 can be imported and used by other contract codes, key statement. If missing, Student2 cannot be imported and used by other contracts.
Note: After the compiled bytecode of the contract library is deployed and published on the chain, the publisher will obtain the blockchain address (super blockchain block address triplet and block hash) of the contract library, and the calling contract can import the contract library through this block address.
Sample contract process: Import a contract library already deployed on the blockchain, call and execute the creation of a Student2 instance, call another smart contract process, and finally output the calculation results.
contract.importas( //Import the contract library to be called
[443,1,1],
"d1cad57bab030f3e897f322242e8706591f3d1aff0757a555115372859cd762d",
"StudentEx"
);
let st1 = new StudentEx.Student2('Tom','male', 18);
let st2 = contract.call([3,2,6], '');//Calling the contract process in blocks [3,2,6]
st1.toString() + "\n" + st2.toString();//Format output of execution results
Note: The contract contract class is used for modular organization of contract code and is one of the basic classes of the Hyperchain Smart Contract Language. It provides importas methods to import contract libraries, and call methods to call other contract processes.
The declaration of the contract class is as follows:
class contract {
JSValue importas(
vector<int> tripleaddr,
const std::string& localblkhash,
const string& modulename
);
JSValue call(
vector<int> tripleaddr,
const std::string& localblkhash
);
};
Note: Among the parameters of the importas method,
Tripleaddr is the triplet address of a sub chain block in a super blockchain. The method retrieves the contract module from the specified block and imports it into the current contract for subsequent calls.
Localblkhash is the block hash corresponding to a triplet address, used for block verification, and can also be left blank.
Modulename is an alias for importing a contract library, which can be arbitrarily set as a string to resolve possible name conflicts between multiple contract libraries and avoid abnormal execution results.
Among them, the call method is used to directly run the contract process in the specified block;
In addition, after the contract process is completed, the execution result can be obtained through the statements st1. toString()+” n”+st2. toString(); To achieve formatted output, the application program can submit the contract process to the virtual machine of the Paralism node through the Paralism node interface for running. The submitted contract process bytecode and formatted output execution results will be automatically submitted by the node after execution is completed.
Development environment preparation
At least two nodes in a blockchain network are running online and have joined each other’s neighboring nodes. Node software version 0.6.0 or higher.
Note: For node configuration and operation, please refer to the “Hyperchain Core Node CLI Operation Manual”.
Precautions for operating environment
Currently, the data load of a single block in a hyperchain network is set to 2M bytes, so
—The bytecode of a single contract library or procedure compilation should be<2M bytes.
—The bytecode of the process contract code and the formatted output content of the execution result should be<2M bytes.
The default uplink cycle for a blockchain network is set to 2.5 minutes.
Compilation and Debugging
Common Javascript Integrated development environment can easily support the compilation and simulation debugging of smart contracts. The debugging of the target running environment can be completed through the Restful API service interface EstimateScript provided by the Paralism node:
EstimateScript
Send the smart contract code to the node virtual machine and return the execution result.
Note: For access to the node Restful interface, please refer to the ‘Paralism Node Restapi Interface Document’.
Deployment and Execution
The deployment and execution of smart contracts can be completed through the Restful API interface as follows:
SubmitRegistrationEx
Submit the smart contract code for deployment on the chain, and return the query handle of the online transaction. The status and results of contract deployment can be obtained through the RestAPI interface GetOnchainState.
SubmitScriptResult
Send the smart contract code to the node, and after the node executes, the formatted output result and code bytecode are automatically submitted for uplink.
Development examples
Step 1: Write a smart contract library
class Student2 {
constructor(name,sex, age) {
this.name = name;
this.sex = sex;
this.age = age;
}
toString() {
return 'The Student v2 is ' + this.name + ', and sex is ' + this.sex + ',age is ' + this.age;
}
}
export {Student2}
Step 2: Deploy the contract library to the blockchain using SubmitRegistrationEx
Call example: http://127.0.0.1:8081/SubmitRegistrationEx
When using the http request post method to submit, just inject the contract Code injection into the input element of the form form name=script.
When debugging using the Postman tool, select the Body/form data method and add two key values: payload and script. The value of script is set to the contract code, and the value of payload can be filled in arbitrarily, as shown in the following figure:
The return value obtained from the SubmitRegistrationEx call is as follows:
{
"queuenum": "1",
"requestid": "25gK2mxmsFJUQvE3PnRpa7cJVscf",
"state": "queueing"
}
Then use the Restful interface to call GetOnchainState to query the execution status of contract deployment transactions.
http://127.0.0.1:8081/GetOnchainState?requestid=25gK2mxmsFJUQvE3PnRpa7cJVscf
GetOnchainState queries until the following return values are obtained:
{
"hyperBlockId": 443,
"chainNumber": 1,
"localBlockId": 1,
"onChainState": "onchained"
}
The returned result indicates that the bytecode of the contract code has been deployed to a sub chain block with a triple address of [443,1,1] in the super blockchain.
Step 3: Import and call the deployed contract library
Write the contract process and call the deployed contract module:
contract.importas(
[443,1,1],
"d1cad57bab030f3e897f322242e8706591f3d1aff0757a555115372859cd762d",
"StudentEx"
);
let st1 = new StudentEx.Student2('Tom', 'male', 18);
let st2 = contract.call([3,2,6], '');
st1.toString() + "\n" + st2.toString();
Step 4: Debug the contract process
1.Restful mode debugging
Call example: http://127.0.0.1:8081/EstimateScript
When using the http request post method to submit, just inject the contract process Code injection into the Body element.
If using the Postman tool, select the Body raw method and call it as shown in the following figure:
2.Hc console mode
Save the contract process code as a file *. js and use the vm run – f file name to run debugging. You can also directly use vm run+contract code to debug on the hyperchain console of the hyperchain node software.
Step 5: Use SubmitScriptResult to execute the contract process
The Restful interface SubmitScriptResult is responsible for compiling the contract process into bytecode for execution, and storing the formatted output results in a sub chain block. The Playload section of this block stores the compiled bytecode and formatted output results of the contract process.
If using the Postman tool to submit contract code, simply place the submitted code in the body and use raw format, as shown in the following figure:
The returned results are as follows:
{
"queuenum": "1",
"requestid": "3CX5PXt1eYUwc4VQncZK63hSpmML",
"state": "queueing"
}