Week 12
Week 12
Pritom Rajkhowa
Getting Started with Rust and
Solidity
Solidity Syntax
fn main() {
println!("Hello, world!");
}
Data Types
Primitive Data Types
• 32-bit Integers: signed (i32) and unsigned (u32)
• 64-bit Integers: signed (i64) and unsigned (u64)
• 128-bit Integers: signed (i128) and unsigned (u128)
• Bool (bool)
• Bytes, Strings (Bytes, BytesN): byte arrays and strings that can be passed to contracts and stores
• Vec (Vec): sequential and indexable growable collection type
• Map (Map): ordered key-value dictionary
• Address (Address): universal opaque identifier used in contracts
• String (String): a contiguous growable array type containing u8s and requires an env to be passed in
• Symbol:
• (Symbol::new): small efficient strings up to 32 characters in length and requires an env to be passed in
• (symbol_short!) small efficient strings up to 9 characters in length
Both are limited to the characters a-zA-Z0-9_ and are encoded into 64-bit integers.
Custom Data Types
• Structs (with Named Fields): A custom type consisting of named fields stored on the ledger as
a map of key-value pairs.
• Structs (with Unnamed Fields): A custom type consisting of unnamed fields stored on the ledger
as a vector of values.
• Enum (Unit and Tuple Variants): A custom type consisting of unit and tuple variants stored on the
ledger as a two-element vector, with the first element being the name of the variant and the
second being the value.
• Enum (Integer Variants): A custom type consisting of integer variants stored on the ledger as
the u32 value.
A Brief Introduction to Modules, Macros,
Structs, Traits, and Attribute Macros
we will provide a concise introduction to some
fundamental concepts in
Rust: Modules, Macros, Structs, Traits,
and Attribute Macros.
Macros in Rust are powerful tools that allow you to do metaprogramming, enabling you to
build chunks of reusable code at compile time.
There are two basic types: declarative and procedural macros. The most common is the
declarative macro, or plain "macro", which is defined with macro_rules!
Structs
Structs are custom data types in Rust that enable you to bundle data together. They provide a way to define and
create more complex data structures.
Traits
Traits in Rust define a shared set of behaviors that types can then either use as-is (default implementations) or
implement themselves. They can be thought of as interfaces in other languages. Traits are defined with
the trait keyword, and their methods can be implemented for different types using the impl keyword.
Attribute Macros
Attribute macros in Rust are a form of procedural macros that enable you to define custom attributes for various language elements
such as functions, structs, and enums. They can modify or generate code based on the annotated items.
During your smart contract developer journey, you will frequently encounter the attribute macro
#[contractimpl] which exports publicly accessible functions to the Soroban environment.
Functions that are publicly accessible in the implementation are invocable by other contracts, or directly by
transactions, when deployed.
Ownership Model
Rust enforces strict ownership rules to manage memory and resources:
• Each value has a single owner.
• When the owner goes out of scope, the value is automatically deallocated.
• Borrowing: Values can be borrowed as immutable or mutable references.
• Lifetimes: Used to ensure that references remain valid.
Hyperledger Fabric
Hyperledger Fabric is a type of technology that helps
businesses create their own secure and private
blockchains. Think of it as a digital ledger that records
transactions in a way that multiple parties can trust,
without needing a central authority like a bank or
government.
Here’s a breakdown in simpler
terms
• Blockchain: Imagine a notebook where every page is a record of a
transaction. Once something is written, it can’t be changed or erased. This is
like how blockchain works, ensuring transparency and security.
• Private and Permissioned: Unlike public blockchains (like Bitcoin),
Hyperledger Fabric allows businesses to create a private network. Only
certain people (or organizations) can join and see the transactions, which
keeps sensitive information safe.
• Modular Architecture: Hyperledger Fabric is built in a way that allows
companies to customize how they use it. They can choose different features
depending on their needs, making it flexible for various industries.
• Smart Contracts: These are self-executing contracts with the terms directly
written into code. They automatically carry out actions when certain
conditions are met, like making payments or transferring ownership.
Core Concept of Hyperledger Fabric
1.Permisson and Private
2.Sared Ledger
3.Smart Contract
4. Privacy
5. Consensus
Work Flow Diagram
Certificate Authority
Organization Structure
Peers
X509 Certificate Authority
Membership