Notes on Solidity, Blockchain, and Smart Contracts
Overview
This post will capture notes about Solidity, blockchain development, smart contracts, and the EVM. It is meant to be used for future Abdul’s reference. These notes follow a Youtube tutorial on Solidity by Patrick Collins.
Blockchain Specific
Blocks
-
Miners get the block #, the data, and the hash; they must reverse engineer the nonce (PoW).
-
Genesis block - First block
-
Changing a middle block will result in needing to change every downstream block.
- But other miners will notice any changes to an existing block and kick the bad actor out of the network.
Keys
-
Private keys are used for authentication; they are also used to sign transactions/messages. A private key can sign a message. A validator can utilize your public key against the message to make sure your private key signed it.
-
Someone with our private key can access our accounts, but they can also sign messages for us.
-
Your Ethereum address is a hash of your public key.
-
Each node keeps track of the blockchain
Consensus
- Consensus - Mechanism to agree on the state of the blockchain
Proof of Work
-
Sybil resistance mechanism (Proof of Work) - Provides a way to figure out who the block author is (through proof of work). Sybil Resistance is the blockchain’s ability to defend against multiple people creating fake blockchains.
-
Block time - The time between blocks being published. It correlates to the difficulty of the problem that needs to be solved by miners.
-
Nakamoto consensus - A combination of proof of work and longest chain rule.
-
Block confirmation - The number of blocks on top of some origin block.
-
Proof of work is not the consensus protocol; it is a part of the Nakamoto Consensus protocol.
-
-
Miners get paid in two ways
-
The transaction fee - Provided by the sender
-
The block reward - Provided by the protocol
-
-
Sybil Attack
-
When users create many accounts to try to influence the network.
-
Pretending to be multiple people
-
-
51% Attack
- If you own 51% of the network and create a fork (you add fake transactions and validate them).
Proof of Stake
-
Users put up a stake to become validators. If they get out of line, they lose their stake.
-
Miners are called Validators.
-
A user is randomly selected to create the next block. Once they have made it, a group of validators validates that the users were honest.
-
RanDAO - Collectively chooses which node is selected next.
Scalability
Sharding
-
A blockchain of blockchains
-
More chains for people to make transactions on.
Rollups
-
Kind of like a shared chain.
-
They send bulk transactions to layer 1.
Solidity Notes
- EVM - Ethereum Virtual Machine, where most smart contracts are deployed.
General Process for Brownie - Manual
This is how to set up a project using Brownie manually.
-
brownie init
-
Write a smart contract in
contracts/
-
Setup
brownie-config.yaml
-
Add any dependencies, networks,
dotenv
,wallets
, etc. -
You can find important addresses on chainlinks.
-
-
Add a
.env
file for testing. -
Write a generic test in
tests/
-
Use the default account:
accounts[0]
-
The purpose here is to ensure that you have a generic connection with your test and contracts.
-
-
Create a deployment script in
scripts
-
Import your
helpful_scripts.py
package -
Integrate your
helpful_scripts.py
in yourscripts/deploy.py
-
-
Setup any
mocks
inhelpful_scripts.py
-
Create a function that will decipher between a development network and a live network.
-
Create a
contracts/test
folder. -
Add any
.sol
files for creating the mocks.- It might be easiest to simply copy the
test
folder from chainlink mix.
- It might be easiest to simply copy the
-
-
Add any interfaces
- Might be easiest to simply copy the
interfaces
folder from chainlink mix.
- Might be easiest to simply copy the
-
Deploy contract locally
- Add all the necessary functions to interact with the smart contract into the
scripts/deploy.sh
- Add all the necessary functions to interact with the smart contract into the
-
Testing code
-
Unit testing
-
Test functions that should pass as well as those that shouldn’t
-
Its okay to hardcode here for predictable functionality
-
Integration Testing
-
General Process for Brownie - Bake
This process uses brownie bake.
-
Use
brownie bake {mix} {dir_name}
- This will set up all the necessary project files saving time.
-
Review the
brownie-config.yaml
- Utilize
.dotenv
- Utilize
-
Add a
deploy
script
Testing
The priority for testing should be:
-
Brownie Ganache chain with mocks: Always
-
Testnet: Always (Mostly for integration testing)
-
Brownie mainnet fork: Optional
-
Custom mainnet fork: Optional
-
Self/Local Ganache: Good for tinkering
Unit Tests
-
A majority of tests should be unit tests.
-
Run unit tests in the development environment. This is quicker and more efficient.
-
3 Phases
-
Arrange
-
Act
-
Assert
-
Integration Test
-
It can be run on a test net.
-
It can be run on a local mainnet fork.
NFTs
-
ERC-721 - The NFT standard
-
ERC 1155 - Semi-fungible tokens
-
ERC-721
-
Each NFT has metadata
-
The NFT contains the token uri
-
-
Images and NFT attributes are currently not stored on-chain.
-
IPFS is commonly used to store images in a decentralized fashion.
-
An NFT must be verifiably scarce to hold value.
-
Utilize a service like Pinata to pin your image… forever.
Upgrades
Three types of upgrades:
-
Not really/parametrize
-
Can’t add new storage and logic
-
But we parameterize each variable within the contract and change the values.
-
Doesn’t provide full functionality for upgrades.
-
-
Social YEET/migration
-
You create a new contract, which is unassociated with the original contract, and tell everyone to move over (Uniswap V1 → Uniswap V2)
-
Cons
-
Lots of work to move users over
-
New address
-
-
Pros
-
Truly immutable
-
Easy to Audit
-
-
-
Proxies
-
delegatecall
- Call another contracts functions in your contract. -
Terminology
-
Implementation Contract - Contains all of the logic for the contract.
-
Proxy Contract - Points to the “correct” implementations contract and routes everyone’s calls there.
-
User - Calls the proxy.
-
Admin - User/group responsible for upgrading the implementation contracts.
-
-
Gotchas
-
Storage Clashes - The proxy contracts change the implementation contracts storage values.
-
Function Selector - Two functions can have the same function selector causing errors.
-
No current “golden standard” for upgrades.
-
-
Leave a comment
Your email address will not be published. Required fields are marked *