Smart Contracts 101 - PBAX
Polkadot Smart Contracts
About Me
- Kian Paimani aka.
@kianenigma
- Engineering Lead @ Parity Technologies
- Polkadot Core Fellow
- blog.kianenigma.com (<-- slides)
Blockchain Models
--
Blockchain Models
- Database 🤮
- A legacy of Bitcoing, and the (limited) UTXO model
--
Blockchain Models
- State Machine ♾️
- Legacy of Ethereum, and the YP.
--
Blockchain Models
- State Machine ♾️
- State, represented as a list of
key -> value
s - State Transition Function
- State, represented as a list of
--
Blockchain Models
- Computer 💻
- Legacy of Ethereum (the "World Computer"), Polkadot JAM's Graypaper.
Note:
Database is utter BS
State Machine is accurate, but a bit to math-y
Computer is accurate, and one that most people relate to, we will use this one
The Blockchain Computer
- Computer has
code
andmemory
. - Users can interact with the
code
(trigger a transfer 💸) -
memory
stores valuable information (how much money I own 🤑) - The main novelty of a computer is that it will execute
code
correctly, and therefore you can TRUST thememory
. 🪄
--
The Blockchain Computer
- First generation blockchains had a fixed
code
. - What is the
code
/memory
of the Bitcoin computer?- Transfer of BTC
- Is it extensible?
Note:
First blockchains were computers with a (almost) fixed code
. Bitcoin's code
is ONLY how to do transfer of BTC, and therefore the State of the Bitcoin computer was only user balances. Nothing more.
--
The Blockchain Computer
- Smart Contracts are the first step into making blockchain computers programmable.
--
Blockchain Computer
- Ethereum is a programmable blockchain computer
- What is the
code
of the Ethereum computer?- Transfer of ETH
- Upload a contract
- Call into an existing contract, which might alter memory
--
Blockchain Computer
- Execution of these contracts is as secure as the execution of the main blockchain's
code
. 🪄 - If you can trust Ethereum and its ability to correctly transfer ETH, you should also trust that it can correctly execute whatever is the code of any smart contract.
What Is A Smart Contract
A way to add programs with custom code
and memory
to the blockchain computer, making it extensible, with the very same security guarantees.
Anatomy Of Smart Contracts
A Smart Contract has:
- code
- state/memory
- EVM has an opinion to see contracts exactly like user accounts
- address (at which it can be invoked)
- balance: money owned by the contract
Note:
this is based on Ethereum/Solidity/EVM contracts
Bringing it all together
How to Write a Contract
- Ethereum Contracts are written in a language Solidity
- Part of the Ethereum
code
is to execute Solidity
- Part of the Ethereum
- This happens in a virtual machine known as EVM
Solc
: Solidity -> EVM Bytecode
Note:
EVM: Ethereum Virtual Machine
How to Execute a Contract
If Ethereum wants to execute a totally untrusted code, what challenges does it have?
- Deterministic cost (gas)
- ⏰ Metering!
- ⛽️
gas_limit
!
- Deterministic output (EVM)
- 🫠 We I cannot make an HTTP request in a contract!
--
Extending Memory
What about the fact that a contract can add more data to its own memory, which in turn bloats the overall blockchain memory?
- State cost needs to be taken into account
- Part of gas
- Deposit
- State Rent
Lifecycle
Case Studies
--
Case Study
Simple Escrow Contract
- Buyer deposits funds
- Both buyer and seller approve
- Transfer funds to seller
- Timeout
- Return funds to buyer
--
Case Study
Web3 Roulette
- Deposit Phase
- Anyone can deposit money into the contract
- Roulette Phase
- One participants get nothing back, their money split among everyone else
Advance Topics
--
Composability ~ Shared Environment
- VMs on a hardware
- Ethereum => Hardware
- Contracts => VMs
- Synchronous and Asynchronous
Note:
Contracts on Ethereum can always call one another
--
Re-Entrancy Attacks
contract EtherStore {
mapping(address => uint256) public balances;
function deposit() {
balances[msg.sender] += msg.value;
}
function withdraw() {
uint256 bal = balances[msg.sender];
require(bal > 0);
(bool sent,) = msg.sender.call{value: bal}("");
require(sent, "Failed to send Ether");
balances[msg.sender] = 0;
}
}
--
Re-Entrancy Attacks
contract Attacker {
// Our interface to EthStore contract
EtherStore public etherStore;
receive() external payable {
etherStore.withdraw();
}
}
- Checks-Effects-Interactions
Note:
Cyfrin Code Glossary: Re-entrancy Hack in Solidity
Smart Contracts in Polkadot
Polkadot
- Computer who's
code
allows other Blockchains to be executed and validated. - Polkadot always assumed one of these external blockchains (Parachains) would deliver smart contract capability.
Past
- A lot of Polkadot-based blockchains use the above tech stack
Note:
- Writing smart contracts in Rust: Ink!, executed in a WASM.
- Writing in Solidity, executed in EVM
pallet-evm
Both of these initiated in Parity, but are pursued as community projects now.
Now
- A new, much better virtual machine based on RiskV: PolkaVM
--
Now
- Outcome: Polkadot will have fast Solidity-compatible Smart Contracts in H2 2025
- Demo
Open Question
- Smart Contract vs. AppChain Model
Outro
- Smart Contracts are the most basic form of building an application on top of a secure blockchain computer 🪄.
- Battle tested approach that has been used for almost a decade now
Questions
Show Comments
- Made with Obsidian-Digital-Garden
- © Kian Paimani . Content on this site is licensed under a Creative Commons Attribution 4.0 License.