In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to use Python to develop Neo intelligent contracts locally. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
We will use the neo-local project to set up a private chain for local development and testing of Neo smart contracts. Using private chains gives us complete control over our environment and enables us to work independently without having to deal with external test networks.
To better understand the contents of the document, you need to use a Unix-like terminal and some kind of text editor. I will operate in the virtual machine and use nano for text editing:
Ubuntu 18.04 (minimum installation)
4GB RAM
50GB disk
Please note that you may need at least 20GB disk space to store your private chain.
Docker,Docker Compose and neo-local
The Neo-local project needs to run on Docker, so the first thing to do is to install Docker. Docker is a container engine that can run preconfigured settings, which is why neo-local uses it. We will use Docker Community Edition (Docker CE).
Install Docker
You can find detailed installation instructions for the selected operating system in the Docker documentation. The following are links to installation documentation for several common operating systems:
Windows
MacOS
Ubuntu (post-installation steps for Linux-also take a look at these and follow these)
Install Docker components
We also need Docker components. For Windows and MacOS systems, it should already be included in the package installed in the previous step. Linux users need to follow the component installation section of the document. Make sure you have followed the instructions in the guide to view the GitHub version page and make sure you download the latest version-don't just copy / paste commands without checking.
Test Docker
Just like a quick test, you should now be able to run the following commands (lines starting with'$') and view the corresponding output:
$docker--versionDocker version 18.06.0-ce, build 0ffa825 $docker-compose-- versiondocker-compose version 1.22.0, build f46880fe
Your version number may not exactly match my version number, just make sure you are running the latest version.
If the post-installation steps are valid, the Linux user should also be able to run the 'hello-world' docker container without sudo privileges. Please note that if you receive some kind of "deny permission" error, you may need to restart your computer:
$docker run hello-world sets neo-local
Finally, we need to set up neo-local by cloning the repository code and installing the preconfigured wallet file so that we can use GAS on our private network.
In the terminal, navigate to the directory of files associated with the NEO storage, and then clone the repository. For those who are not familiar with git, this will create a directory called "neo-local" that contains the files we need. Navigate to the neo-local directory.
$git clone https://github.com/CityOfZion/neo-local.git$ cd neo-local
Most of the files here are related to the neo-local project itself, and we don't have to make any changes to them. We will only deal with the wallets/ directory during setup. Once we are up and running, most of our files will be saved in the smart-contracts / directory.
The wallet files we will use are not easy to find. You can find it on the Docker NEO Private chain Center page, or just click on the link to get it. Place the downloaded wallet file in the wallets / directory of the neo-local project repository. For reference, the password of this wallet is coz.
Enable neo-local stack
The method of starting the stack between different operating systems is slightly different. Both sets of commands take you to the neo-python command line interface (CLI).
Windows (wiki) $docker-compose up-d-- build-- remove-orphans$ docker exec-it neo-python np-prompt-p-vMacOs (wiki) and Linux (wiki)
Install the make command, if you haven't already installed it:
$sudo apt install make
Enable stack
$make start Open your wallet
The last setup step before using a smart contract is to open the wallet we copied earlier. Docker is set to mount the wallets/ directory under the root directory, so our wallets are under the / wallets/neo-privnet.wallet path. The help command in neo-python displays a list of all possible commands. The command we are looking for is open wallet {path}, which will prompt you for your wallet password (coz). The whole process should look like this:
Neo > open wallet / wallets/neo-privnet.wallet [password] > * * Opened wallet at / wallets/neo-privnet.walletneo >
Basic smart contracts
Because the neo-local stack uses neo-python, we will write a basic smart contract in Python. Create the file plus_one.py in the smart-contracts / directory and add the following code:
Def Main (num): return num + 1
As you can see, the contract takes a number as input and returns the value of that number plus 1. Just like the wallets / directory, smart-contracts/ is mounted on the root directory, so the path to the contract is / smart-contracts/plus_one.py.
Neo smart contracts run on NeoVM (Neo virtual machines) and must first be converted to bytecode, similar to Java, before they can be deployed. The build {path} command in neo-python can do this for us, providing a .py file as an input parameter, and then outputting the generated .avm file. However, the document itself does not bring us much benefit. We will start with the morph build..test command of this command:
Neo > build / smart-contracts/plus_one.py test 02 02 False False False 5
Take a look at this command before we get the output file. The full signature of the command is:
Neo > build {path/to/file.py} test {param_types} {return_type} {needs_storage} {needs_dynamic_invoke} {is_payable} [params]
The path to the file is quite self-evident. However, the type is provided as in the ContractParameterType page, where both the parameter and the return type are represented as a single byte:
TypeByteSignature00Signature01Integer02Hash26003Hash35604ByteArray05PublicKey06String07Array10InteropInterfaceF0Voidff
In our example, the contract takes integer (integer) (02) and returns an integer (02). The next three parameters are the properties set on the contract, which we will not consider now. The last parameter is the data used when actually invoking the contract. Running this command at the neo-python prompt should output the following:
Neo > build / smart-contracts/plus_one.py test 02 02 False False False 5 [I 180909 22:53:38 BuildNRun:50] Saved output to / smart-contracts/plus_ one.avm [I 180909 22:53:38 Invoke:586] Used 0.021 Gas Calling / smart-contracts/plus_one.py with arguments ['5'] Test deploy invoke successfulUsed total of 19 operationsResult [{'type':' Integer' 'value': 6}] Invoke TX gas cost: 0.0001---neo >
The first output line is to confirm that the bytecode has been built and where it is saved. The rest of the output describes the settings and results of the contract. We called it with the value "5", and the call succeeded, and we received the integer'6' as the return result. Looks like our contract is in effect!
Deploy smart contracts
Finally, once we have built and tested our smart contract, we need to deploy it to the network. This is actually a fairly simple process, and we only need to use the import contract command.
Neo > import contract / smart-contracts/plus_one.avm 02 02 False False False
You will be prompted to fill in some fields, such as the contract name and version, the author's name and email, etc. After filling in, some metadata information about the contract will be printed out, and you will be prompted to enter your wallet password. After entering the password, you will start to deploy the contract and charge you the necessary amount of GAS. Since this is a private network, you should be able to continue to deploy-there are many GAS available for testing wallets.
After the deployment is complete, get the "hash" from the printed metadata and run the testinvoke command (replace with your own contract hash):
Testinvoke 0x2b46bfe08185fbda2cb8121d6a2fd1a1d228c586 8
You should see the output of the expected result:
-Test invoke successfulTotal operations: 19Results [{'type':' Integer' 'value':' 9'}] Invoke TX GAS cost: 0.0Invoke TX fee: 0.0001Murray-
If you receive a message that you can't find a contract, you may have to wait a minute or two for your contract to be excavated by a block before it can be invoked. Again, this will be a local call, and enter your password to run a real contract on the network, charging you a GAS fee in the process.
This is how to use Python to develop Neo smart contracts locally. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.