Notes on Blockchain, Solidity, and Full Stack Web3 Development with JavaScript -part 2

Source: youtube.com/watch?v=gyMwXuJrbJQ&list=PL..

Consensus

Consensus is the mechanism used to agree on the state/ single value of a blockchain, in a decentralized system.
The Consensus mechanism can roughly be broken down into two systems.

  • Chain Selection:
  • Basically helps us to understand which blockchain is the real blockchain. eg. the longest blockchain is held as reference
  • Sybil Resistance:
  • [Proof Of Work], basically a node that solved the problem/ which node found that mine and is the block author. Everyone competes to solve the given problem. All nodes have to go through a computationally expensive method to solve a problem. So no matter how may nodes are there, each one still has to undergo this complex process to solve the mine.
  • [Proof Of Stake], all noted put a certain amount as its stake, if it misbehaves in a network, its stake is slashed. Here a random node is selected to validate the transactions/block(group of transaction). while the rest of the nodes just validate the validators work.

Solidity basics

  • Source: remix-project.org is an integrated development environment. This is where we are going to code and interact with our smart contracts. Solidity is the language we are going to use to write our smart contracts.

In your workspace, in the folder called contracts, we create a file with a .sol extension. Basically lets the compiler know that we are working with a solidity file, and the code here is in solidity.

Lets understand the code. Since solidity is a relatively new language, its constantly changing, hence the first line should specify what version of solidity you are using.

pragma solidity 0.8.7; //only this specific version.

pragma solidity ^0.8.7; //any version of solidity equal to or greater than 0.8.7

pragma solidity >=0.8.7 <0.9.0; //any version between and including 0.8.7-0.9.0

some compilers will throw a warning if you don't mention the SPDX-License-Identifier. Here we use the MIT license as its the most restrictive

//SPDX-License-Identifier: MIT
All code statements should end with a ; .

Now lets define our contract. You can think of a contract similar to a class in any object oriented programming like Java or JavaScript.

contract SimpleStorage{

// **Common dataTypes are as followed.**
//boolean, uint, int, String, address, bytes
//**boolean** -> true/false.
//**uint** -> using uint you can specify how much storage you want to allocate to a given uint
//**Int** -> these numbers can be positive or negative 
//**String** -> these numbers can be positive or negative 
//**Address** -> these datatype can hold addresses.
// **bytes32** -> How many bytes you need in a given memory.

}

The default value for solidity is 0. So if we declare a variable, but not assign a value to it, solidity by default assigns a value 0;

eg: uint256 number; //number holds a value 0 by default

Basic Solidity Functions

Functions or methods are self containing modules that will execute a specific set of instructions when called.

A function has one of 4 visiblity specifiers,

  • public: visible externally and internally
  • external: only visible externally, i.e someone outside the contract can call this function.
  • private: only visible in the current contract
  • internal: only visible internally. i.e only its contract and its children contract can call it.

uint256 public favNumber; The public function creates a getter function for favNumber. If the public keyword is not mentioned, the compiler assumes you are using the keyword internal, which basically means the variable created is accessible only within the contract. Every time you change the state of the contract, its considered as a transaction. And the more stuff you do in the contract, the more gas you'll have to spend.

uint256 public favNumber; is the same as function retrieve() public view retruns(uint256){return favoriteNumber; }. Since a retrieve function does not change the blockchain,henec it does not spend any gas.

  • a pure function is another function that does not spend any gas as it does not modify the blockchain. eg, function add() public pure returns(uint256){ returns (1+1); }

Sidenote: we only spend gas if we update the blockchain. that being said, if a contract(gas calling function) calls a pure or public function, in this case it will spend gas.

function retrieve() public view returns(uint256){ returns xyz }
  • public: makes this function publicly visible.
  • view: states that this function does not modify a state.
  • returns(uint256): basically states that the return type is uint256.

Basic Solidity Arrays & Structs.

So if you want to create your own structure of data, we use Struct, eg. if you want to create a structure holding a String of name, int of age, and an array of hobbies. Think of it like a blueprint for a separate data structure.

struct People {
uint256 favoriteNumber;
string name;
}

People people = People({favoriteNumber:3,name:'Aaron'})

An Array is a data structure that holds a list of other data-types. you initialize an array by adding "[]" after the decalration. eg. People[] public people;. This type of an array is known as a dynamic array.

How to add values in a dynamic array?

people.push(People(_favoriteNumber,_name)) 
// you pass in values in the order how the structure was created.
// **so by default**
// _favoriteNumber will be saved in favoriteNumber
// _name will be saved in name

So the two ways to save a Struct value in a variable are as below

  • People(_favoriteNumber,_name)
  • People({favoriteNumber:3,name:'Aaron'})

Basic Solidity Memory, Storage, & Calldata( Intro )

There are 6 places you can store data in, in solidity.

  • Memory - data in Memory are stored temporarily , Its lifetime is limited to a function call.
  • Storage - if not explicitly defined, data is stored in storage. Basically stored state variables. Basically permanent variables that cannot be modified
  • Calldata - It behaves like Memory, once assigned to a variable in a function, its value cannot be changed.
  • Stack - will explain in further articles
  • Code - will explain in further articles
  • Logs - will explain in further articles

Basic Solidity Mapping.

mapping(string=>uint256) public nameToFavoriteNumber;
//*Basically nameToFavoriteNumber is a dictionary that has a key:value pair of string:uint256 *

Syntax: mapping(key=>Value) public variableName; here every single string will be associated to 0; nameToFavoriteNumber[_name]=_number;