The understanding of foundational concepts of a subject is crucial to unlocking first principle thinking and the ability to have a better grasp in its design space. In the case of being a better Cairo developer, it is mandatory to have a concrete understanding of the Starknet Cairo Virtual Machine ( CVM ).
And in the Starknet smart contract world, the "Cairo Virtual Machine" along with its algorithms and data structures are the first principle thinking. We are able to build smart contracts on starknet due to the existence of this.
This article assumes you have some basic knowledge of Cairo and how to deploy smart contract to the Starknet blockchain. Go through the quick start guide from Starknet official documentation and use Cairo book for references if you need a refresher on this.
Another thing to note is that Cairo compiles to Sierra prior to being deployed on the Starknet network, which is then compiled to casm ( Cairo assembly ) instructions that the CVM interprets.
This series focus on specific parts of the compiled Sierra code and illuminate how they work. By the end of this article, you should have a clearer understanding of how each component functions, and also learn a lot of the foundational concepts relating to the CVM.
We are going to take a look at a SimpleStorage contract along with an excerpt of its Sierra and casm code to demonstrate how the CVM selects functions.
#[derive(Copy, Drop)]
struct Point {
x: u128,
y: u128,
}
fn main() {
let p1 = Point { x: 5, y: 10 };
foo(p1);
foo(p1);
}
fn foo(p: Point) { // do something with p
}
The contract has 2 functions set(ref self: TContractState)
and get(self: @ContractState)
that the CVM will have to decide between when a function call comes in. Below is the compiled Sierra code.
https://gist.github.com/okhaimie-dev/4388576d06601300ca46b86330403e40
%% TODO: Complete this section%%