Solana smart contact is also called on-chain program, its executable is an BPF file of ELF format, BPF is compiled from C, Rust or other programming languages. For now, Solana has SDK support for C and Rust(which uses LLVM as compiler).
The BPF file(usually with .so as extension) needs to be deployed to Solana validator, and executed through Solana runtime.
Environment setup
We suggested to use Ubuntu 18.04 in accordance with official tutorial. Of course, you can also use MacOS, but you may need to install some dependent packages manually. Here we use Ubuntu 18.04 in this tutorial.
1. Install Rust
If you already has installed rust, you can skill this part, actually it’s just one line of code:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
then update your bash profile by using command:
source ~/.bash_profile
At this point, we can check your rust version to check whether you have successfully installed it:
ubuntu@VM-0–12-ubuntu:~/solana/example-helloworld/src/program-rust$ rustc — version
rustc 1.47.0 (18bf6b4f0 2020–10–07)
ubuntu@VM-0–12-ubuntu:~/solana/example-helloworld/src/program-rust$ cargo — version
cargo 1.47.0 (f3c7e066a 2020–08–28)
2. Install Solana command line tool suite
Solana has provided some command line tool suite like solana-validator for validating node, solana-keygen for generating key pairs, and cargo-build-bpf for smart contract development(we suggest to latest 1.4.x version which published after 2020–10–22)
You can download compiled versions from Solana official github. Here we choose precompiled version 1.4.8.
wget https://github.com/solana-labs/solana/releases/download/v1.4.8/solana-release-x86_64-unknown-linux-gnu.tar.bz2
Unzip the file and add it to PATH, then we execute the following command to check its version
ubuntu@VM-0-12-ubuntu:~$ solana --version
solana-cli 1.4.8 (src:c8b3d0ba; feat:3582801427)
Set up local test node
Solana provides docker image file for easy local test node setup,first you need to install docker if you do not have it, then we have two ways to run the image file.
1. use npm
This is the easiest way to run local net. Using npm to pull “@solana/web3.js” package and it contains all the scripts we need.
First we created a fold like “localnet_test”, and run command:
npm init
npm install @solana/web3.js
After fetching all the packages, we can add the scripts we need to start the docker to package.json in the root folder:
"localnet:up": "set -x; solana-localnet down; set -e; solana-localnet up",
"localnet:down": "solana-localnet down",
"localnet:logs": "solana-localnet logs -f",
Then we run:
npm run localnet:up
Now, the node should be up and running. Try sending a request to like getTransactionCount to http://localhost:8899 using curl, if you can get response like we show below, then congratulations! You have your node now.
+ curl -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":1, "method":"getTransactionCount"}' http://localhost:8899
{"jsonrpc":"2.0","result":0,"id":1}
Then we config our local test node address to Solana command tool:
solana config set --url http://localhost:8899
You can use command cluster-version to check our docker image version:
solana cluster-version
1.3.21
A very common command we would usually use is to check logs, especially if you are testing your Solana on-chain program:
npm run localnet:logs
If you wan to stop it, just run:
npm run localnet:down
See, it’s very easy to play with Solana 🙌
2. Use localnet.sh
The npm package is a wrapper for localnet.sh , if you want more customizations for your node, you can just run the script manually. As an example, here we changed the docker image version of the cluster.
First, we download the file to your local machine, for example to our newly made directory localnet.sh_test:
mkdir localnet.sh_test
cd localnet.sh_test
curl https://raw.githubusercontent.com/solana-labs/solana-web3.js/master/bin/localnet.sh -o localnet.sh
Then we change the permission of our localnet.sh file:
chmod a+x localnet.sh
Then we can change the version of our image by modifying the channel property, like here we use verion 1.4.7:
channel="v1.4.7"
Then we get our node using command:
./localnet.sh up
Config the node address to Solana command tool like we do before, then we can see that the docker image version has been change to 1.4.7:
ubuntu@VM-0-12-ubuntu:~/solana/localnet.sh_test$ solana cluster-version
1.4.7
Also you can run./localnet.sh logs
to check logs, and./localnet.sh down
to stop the node.
PS: what just happened under the hood
Let’s dive deep into the localnet.sh file:
If we run ./localnet.sh up , the it will finally execute:
docker run "${ARGS[@]}" solanalabs/solana:"$channel"
so it is actually a docker run command with preset parameters.
let us see what does the Solana docker look like, we can find it is Solana SDK:
FROM debian:buster# JSON RPC port
EXPOSE 8899/tcpRUN apt update && \
apt-get install -y bzip2 libssl-dev && \
rm -rf /var/lib/apt/lists/*COPY usr/bin /usr/bin/ENTRYPOINT [ "/usr/bin/solana-run.sh" ]
CMD [""]
We can see that it’s actually a debian:buster image with some dependency packages installed and then copied into /usr/bin while setting the solana-run.sh as the entry script which starts with the ${ARGS[@]} we passed in.
So the main logic resides in solana-run.sh, as showed below:
ledgerDir=$PWD/config/ledger // set ledger directory
solana-keygen new --no-passphrase // Generate wallet
validator_identity="$dataDir/validator-identity.json" // set validator account
validator_vote_account="$dataDir/validator-vote-account.json" // set vote account
validator_stake_account="$dataDir/validator-stake-account.json" // set stake account
[[ -e "$ledgerDir"/genesis.bin || -e "$ledgerDir"/genesis.tar.bz2 ]] // config genesis file
./fetch-spl.sh // config SPL Token program
solana-genesis // set genesis
solana-validator "${args[@]}" & // start validator
So if you run validator on your own machine, you are actually running this script. Do not forget the install Solana command line tool and add it to PATH before running it.
Conclusion
If you just want to test your on-chain program, you can send it to devnet to get it run, but if you want to debug it or see more details of how it runs, you need to run the node locally. So Solana has made this docker image along with other scripts and tool suite for easy set up. We will have more tutorials about how to develop Solana on-chain programs and we love to hear your response!
Chinese version of this tutorial see here