Blockchain , Developers

Learning curve for Solana

For developers already used to the Blockchain world, here is a small introduction to the Solana platform. The goal here is to compare the platform to others on the market and try to demonstrate what the learning curve would be for you, a developer with experience in other blockchain platforms, if you decided to venture ...

For developers already used to the Blockchain world, here is a small introduction to the Solana platform. The goal here is to compare the platform to others on the market and try to demonstrate what the learning curve would be for you, a developer with experience in other blockchain platforms, if you decided to venture into Solana.

When it comes to decentralised networks (DApp), Solana has one main difference between the other two platforms, which is to guarantee scalability. With this, you can ensure that transactions are more agile as well.

Well, let’s go. Assuming we know blockchain and its concepts, so let’s study the features and underlying technology of the Solana platform (SOL). Another thing that we have to take into account is that each blockchain has its own computational model, which is the mechanism used for transactions. Therefore, we have to understand how the consensus mechanism and also the scaling of the Solana blockchain works. The programming languages ​​used in Solana can also be a challenge for some, as he uses the languages ​​Rust, C and C++ for his Smart Contracts (Programs in Solana). But, it can also be an advantage in terms of Languages, as these languages ​​are already well established and are well known among most developers. So if you’re one of them, that won’t be a problem.

Understanding the Proof of History consensus algorithm

I won’t delve into it here, because POH is vast, but I’ll try to briefly describe what it is and how this mechanism that is used by Solana works.

Other blockchain networks use another mechanism, such as Ethereum, which uses Proof of Stake (PoS), however, this mechanism uses external systems to date and time stamp, and validate the correct order of transactions. Solana uses POH as a mechanism for this, which takes a different approach.

I will explain how POH works simply and succinctly.

In the blockchain network, a record is created when an event occurred at a specific time before or after other events. With this, the network nodes can believe in the source of the message and in the message itself, in addition to the blockchain being much faster and decentralised security.

All transactions and events are sha256 hashed, where sequential steps are required for it to be validated producing a unique and reliable result. This result is used as the input source for the next hash, aggregating the transaction output to the hash, forming a chain of transactions.

Solana creator and CEO Anatoly Yakovenko describes here very well why he chose this approach:

“The idea we had at the beginning using a verifiable delay function or proof of history, was to create a clock that is out of consensus and turns a ‘knob’ of when any block producer can transmit a block and does it in a way very predictable and very, very fast.”

In this way, Solana’s consensus mechanism makes it much more scalable than other blockchain platforms, as well as fully decentralised, as it doesn’t rely on any external solution for care and synchronization.

Below is a simple and widely used diagram that shows the flow in a simple and clear way:

Sealevel

Parallel processing of thousands of smart contracts (Sealevel), it is also critical to understand what it means and how it works. This is Solana’s transaction processing engine. It increases scalability so that Smart Contracts (Programs) can run concurrently without slowing down the blockchain.

Smart Contracts on Solana

Solana provides high-performance memory management for smart contracts, called Programs in Solana’s case.

With LLVM infrastructure and the ELF standard ensure dynamic compilation, binding and high performance execution, memory management and parallel execution of smart contracts.

To understand better, look at this article by Yakovenko High performance memory management for smart contracts

Solana Architecture

Looking at Solana’s architecture, the simple yet efficient model of providing the Programs (Smart Contracts), Client, JSON RPC API, and the high scalability of the Solana network is clear.

The image that shows the architecture is often used on networks to demonstrate how Solana works, but it is clear enough to demonstrate here as well. So I’m going to use it.

In practice

So now we know more about the Solana Blockchain in terms of the technical scope so that developers can start venturing into this highly scalable, high performance and fully decentralized platform.

On the Solana website, https://docs.solana.com/, it is possible to have access to a considerable volume of materials to help you in the development of your solution in Solana.

Solana provides some environments for you to connect your application, the one used for development is DEVNET (http://api.devnet.solana.com)

Well, let’s go. I will summarize here how to proceed to run your application on your desktop. But for more information and more detailed instructions, go to the Solana docs site. There you can find how to install the Client, configure your local environment, create a Program, deploy the Program on the Solana network, execute transaction, etc…

Creating a Smart Contract

So let’s create our first smart contract, the Hello World program! on the Solana network. The programming language used here is Rust.

JavaScript
use solana_program::{
   account_info::AccountInfo,
   entrypoint,
   entrypoint::ProgramResult,
   msg,
   pubkey::Pubkey,
};
 
 
entrypoint!(process);
 
 
fn process(
   program_id: &Pubkey,
   accounts: &[AccountInfo],
   instruction_data: &[u8],
) -> ProgramResult {
  
   msg!("Hello World!");
   Ok(())
}

I will not go into the libraries, parameters, nomenclatures and also syntax of the programming language to be used here, but in the Smart Contract object, which, when invoked, emits the message Hello World!

The process() function will be executed and finally, it will emit the message. Note that the entrypoint() function is declared before the function that will execute your smart contract logic. This is mandatory, as only in this way can the Smart Contract be executed by the Client.

As it is the Rust language, it is essential to have the systems and packages management file for the language or Cargo.toml. This file contains the parameters necessary for your rust program to be compiled and finally executed within the Solana network. Parameters like: solana sdk version, solana program version, libs directories…

JavaScript
[package]
name = "hello-world"
version = "0.1.0"
edition = "2022"

[dependencies]
solana-program = "1.9.9"

[dev-dependencies]
solana-program-test = "1.9.9"
solana-sdk = "1.9.9"

[lib]
crate-type = ["cdylib", "lib"]

The time has come to compile and deploy the Smart Contract to the Solana network. But first, we have to perform some steps that will configure which Solana network environment we are going to deploy, in this case, it will be DEVNET

We are also going to create a Waller and AirDrop. In this example, we are going to apply a BIP-39 passphrase to our Wallet and 1 AirDrop. To do this, just run the instructions below from the command line.

Bash
solana-keygen new --no-bip39-passphrase
solana config set --keypair ~/.config/solana/id.json
solana config set --url http://api.devnet.solana.com
 
solana airdrop 1

Compiling and deploying

Bash
cargo build-bpf --manifest-path=./Cargo.toml --bpf-out-dir=dist/program
solana program deploy dist/program/hello_world.so

If you want to list the programs created, just run

Bash
solana program show --programs

To close a program

JavaScript
solana program close <id>

Client

Let’s create a client in Node.js using the official Solana SDK @solana/web3.js. For this example, it was used as a reference and also content extracted from Solana Labs.

JavaScript
import {
 Keypair,
 Connection,
 PublicKey,
 LAMPORTS_PER_SOL,
 TransactionInstruction,
 Transaction,
 sendAndConfirmTransaction,
} from '@solana/web3.js';
import fs from 'mz/fs';
import path from 'path';
 
 
/*
 Our keypair we used to create the on-chain Rust program
*/
const PROGRAM_KEYPAIR_PATH = path.join(
 path.resolve(__dirname, './dist/program'),
 'hello_world-keypair.json'
);
 
 
async function main() {
 /*
 Connect to Solana DEV net
 */
 let connection = new Connection('https://api.devnet.solana.com', 'confirmed');
 
 /*
 Get our program's public key
 */
 const secretKeyString = await fs.readFile(PROGRAM_KEYPAIR_PATH, {encoding: 'utf8'});
 const secretKey = Uint8Array.from(JSON.parse(secretKeyString));
 const programKeypair = Keypair.fromSecretKey(secretKey);
 let programId: PublicKey = programKeypair.publicKey;
 
 /*
 Generate an account (keypair) to transact with our program
 */
 const triggerKeypair = Keypair.generate();
 const airdropRequest = await connection.requestAirdrop(
   triggerKeypair.publicKey,
   LAMPORTS_PER_SOL,
 );
 await connection.confirmTransaction(airdropRequest);
 
 /*
 Conduct a transaction with our program
 */
 const instruction = new TransactionInstruction({
   keys: [{pubkey: triggerKeypair.publicKey, isSigner: false, isWritable: true}],
   programId,
   data: Buffer.alloc(0),
 });
 await sendAndConfirmTransaction(
   connection,
   new Transaction().add(instruction),
   [triggerKeypair],
 );
}
 
 
main().then(
 () => process.exit(),
 err => {
   console.error(err);
   process.exit(-1);
 },
);

Note that in the connection.requestAirDrop() method, in addition to the Keypair, the number LAMPORTS PER SOL is also passed. To learn more about lamports see here https://docs.solana.com/terminology#lamport

A fact that is very much worth mentioning here. This name Lamport is in honor of the Turing Prize winner, Leslie Lamport, an American computer scientist 😀

Related Articles

Secure web applications
Developers Security

Building Secure Web Applications in the Age of Cyber Threats: Essential Strategies

Build secure web applications that withstand evolving threats. Learn secure coding practic...

Read more
About us Blockchain Developers DevOps

Infrastructure, Blockchain, and Scalability with Erick Rettozi

In this article, Cyrex' Lead Backend Engineer Erick Retozzi offers expert advice on buildi...

Read more
APIs Developers

The Hidden Costs of DIY API Integration: When to Consider Professional Help

Avoid costly mistakes with professional API integration. Learn the hidden costs of DIY API...

Read more
AI Developers

LLMs, the gist

Unlock the magic of Large Language Models (LLMs) with our comprehensive guide. Discover ho...

Read more