# 2024 Cairo Bootcamp: Session 3
# 27-04-24
## Txn Lifecycle
- Sequencer:
- Mempool
- txn validation
- checks for correct txn structure
- txns are marked as:
- received
- ignored
- checks for txn valid txn signature:
- if valid, it proceeds to the CairoVM
- if not, it is rejected
- CairoVM - txn is executed and sent to the prover as execution traces - refers to the log of how every transaction was executed
- correct execution = `ACCEPTED ON L2` status
- insufficient gas or failed assertions = `REVERTED`
- Prover:
- logs execution traces and resulting state differences
- generates validity/STARK proof
---
## Setting Up Development Environment
### 1. Scarb
Cairo can be installed by simply downloading Scarb. Scarb bundles the Cairo compiler and the Cairo language server together in an easy-to-install package so that you can start writing Cairo code right away.
Run the following in your terminal, then follow the onscreen instructions. This will install the latest stable release.
``` =shell
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh
```
To check if scarb is successfully installed, run either:
- `scarb --version` - returns the version of scarb
- `which scarb` - returns the path to the scarb bin on your maching
### Install Scarb via asdf (for macOS and Linux only)
`asdf` is a CLI tool that can manage multiple language runtime versions on a per-project basis.
Install `asdf`:
- https://asdf-vm.com/guide/getting-started.html
- Run the following command to add the scarb plugin:
- `asdf plugin add scarb`
# Recap Last Session
- Introduced Starknet - validity rollup-based scaling solution
# Cairo Syntax
#### Starklings Breakdown
1. Data types - https://book.cairo-lang.org/ch02-02-data-types.html
Generally, types are classified into:
- Scalar
- Compound
##### Scalar Types
- Felts - `felt252`
`let x = 10;` - implicitly a `felt252`
- Integers - `u8, u16, u32, u64, u128, u256, usize`
- Booleans
##### Strings
- Short Strings
Cairo uses the felt252 for short strings. As the felt252 is on 251 bits, a short string is limited to 31 characters (31 * 8 = 248 bits, which is the maximum multiple of 8 that fits in 251 bits)
```
let my_first_char = 'C';
let my_first_string = 'Hello world';
```
- Byte Array Strings
With the ByteArray struct added in Cairo 2.4.0, you are not limited to 31 characters anymore. These ByteArray strings are written in double quotes like in the following example:
#### NB - While Using Felts:
- overflow: careful when using `felt252` for calculations that exceed 252 bits
- not ideal for standard arithmetic calculations
```
let a = 3/2
throws an error:
Trait has not implementatino in context: core::traits::Div::<core::felt252>
```
use integer data types instead
- long strings - throws an error:
`error: The value does not fit within the range of type core::felt252`
- Cairo is block-scoped:
return value of a given function in an if statement is the value that evaluates to true
##### Type Casting
- The `try_into` method allows for safe type casting when the target type might not fit the source value. Keep in mind that try_into returns an `Option<T>` type, which you'll need to unwrap to access the new value.
- The `into` method can be used for type casting when success is guaranteed, such as when the source type is smaller than the destination type. It is possible to type-cast from one scalar type to another
```
fn main() {
let my_felt252 = 10;
// Since a felt252 might not fit in a u8, we need to unwrap the Option<T> type
let my_u8: u8 = my_felt252.try_into().unwrap();
let my_u16: u16 = my_u8.into();
let my_u32: u32 = my_u16.into();
let my_u64: u64 = my_u32.into();
let my_u128: u128 = my_u64.into();
// As a felt252 is smaller than a u256, we can use the into() method
let my_u256: u256 = my_felt252.into();
let my_usize: usize = my_felt252.try_into().unwrap();
let my_other_felt252: felt252 = my_u8.into();
let my_third_felt252: felt252 = my_u16.into();
}
```
##### Compound Types
- Struct
- Enum