Page 2 of 16

Blockchain Concepts

Advantages of Blockchain-

  • Decentralised – Having no single point of authority
  • Transperancy and Flexibility
  • Speed and Effeciency
  • Security and Immutability
  • Removal of counterparty risk
  • Trust minimised agreements
  • Hybrid smart conrtract with Chain Link (Oracle)

Terms and Abbrevations used in Blockchain-

DAO’s – Decentralised Autonomous Organization’s

Testnet – are free and for testing smart contracts

Mainnet – cost money and are considered “live”

Faucet – is and application that gives us free test token, like free test Rinkeby Ethereum

Block Explorer – an application that allows us to “view” transactions that happen on a blockchain.

Gas – is a unit of computational measure. The more computation a transaction uses the more “gas” you have to pay for.

Every transaction that happens on-chain pays a “gas fee” to node operators.

The amount of “gas” used and how much you pay depends on how “computationally expensive” your transaction is.

Sending ETH to 1 address would be “cheaper” than sending ETH to 1,000 addresses.

Gwei (giga-wei) – denomination or a small unit of Ether 1 Ether is 1 billion Gwei. It is used to measure cost of transaction i.e. used in the Ethereum network to but and sell goods and services.

Gas – Measure of computation use

Gas Price – How much it costs per unit of gas.

Gas Limit – Max amount of gas in a transaction

Transaction Fee – Gas used X Gas Price i.e. 21,000 gas @ 1GWEI per gas =21,000 GWEI

Gas Price of Ehtereum fluctuates based on the demand of the blockchain. More perople using or want to make transactions, the higher the gas price and therefore the higher the transaction fees.

Gnesis Block – First block in a blockchain

Hash – Unique fixed length string to identify a piece of data

Hash Algorithm – A function or algorithm that computes data into a unique hash.

Mining – The precess of finding the “solution” to the blockchain “problem”. Node get paid for mining blocks.

Block – A list of transactions mined together

Nonce – A “number used once” to find the “solution” to the blockchain problem. It’s also used to define the transaction number for an account/address.

Public Key – Is derived from your private key. Anyone can “see” it, and user it, and user it to verify that a transaction came from you.

Private key – Only know to the keyholder, its used to “sign” transactions.

Elliptic Curve Digital Signature Algorithm (ECDSA) – generates public key using private key.

Signing Transactions – A “one way” process. Someone with a private key signs a transaction by their private key being hashed with their transaction data. Anyone can then verify this new transaction hash with your public key.

Node: Single instance in a decentralized network. Anyone can join the network. Blockchains are resilient. Blockchain nodes keep lists of the transactions that occur. They are immutable- nothing can corrupt them.

Consensus – is the mechanism used to agree on the state of a blockchain.

Broken into 2 :-

— Chain Selection

Sybil Resistance – Proof of work. Block Author, defends on fake blockchains

— — PoW – Minners. Uses lot of electricity and is hence costly.

-Transaction Fees and Block Reward.- compete each other to find the answer to the problems. Whoever finds first get the transaction fees.

Sybil Attack – creates fake accounts to influence the network

51% attack

Longest Chain Rule

– — PoS — Validators – nodes put up collateral as a sybil resistance mechanism. Uses less energy. Slightly less decentralised.

ABI– ApplicationBinary Interface- tells Solidity how it can interact with another contract. What functions can be called from the contract. Interfaces compile down to ABI.

Python – Metaclass

In Python, metclass is a class whose instances are classes. Metaclass are derived from type.

Metaclasses change the classes automatically when decorators are not efficient use metaclass.

Metaclass implements __new__ method which calls the parent class to create a new instance. The creation of metaclass includes the name of the class, class bases and attributes used in the class.

Metaclass are used to create class dyunamically using the type keyword. See following code snippet-

car = type("vehicle", "Vehicle", "{speed:50}")

Here the type accepts the name of the instance, then the baseclass name and attributes defined for the object.

See the below code to create your own metaclass.

Vehicle_Meta class inherits from type. Implements __new__ method which accepts four parameters. cls, name – name of the class to be dynamically created, bases – meta class base class and dict- attributes attached to the class. custom attribute speed is created.

Vehicle class refers to the meta class Vehicle_Meta.

When the __dict__ is accessed it has the custom attribute along with other attributes created as a part of Class.

Shallow and Deep Copy in Python

Shallow Copy

Shallow copy in Python allows to create copy of objects. With shallow copy the object references to the same memory and shares the value across the objects.

See example below different ways to shallow copy objects.

Assign object to other object with “=” operator.

Performance: this approach is fastest as comapred to [:] and deep copy

Assign object to other object with [:]. This creates a new reference to the memory allocated for the list.

Performance: this approach is slower than the “=” operator as it creates a new reference.

Deep Copy

Deep copy creates a new instance of the object and allocates different memory location and the values associated are different from the copied/source object.

Below example uses deepcopy() method from the copy package.

Performance: This approach has a comprehensive operation and slower the above 2 approaches.

Deep copy wont call the constructor and copies the data with the new memory chunk.

Property getter, setter and deleter in Python

Python uses getter, setter and deleter to the method to the attrinutye which is decorated as @property

Python allows to control the access of attribute using @property keyword. This is also called as attribute encapsulation.

Attribute that is decorated as @proptery uses a method name same as encapsulated atribute.

This methods are called atutomatically when the objects are trying to access or set value to such encapsulated attribute.

When a value is set to the property, attribute setter ios called.

When attribute is deleted using del, deleter is called.

Here the __empname is hidden to the objects and can be accessed using getter method. This shows the encapsulation concept of object oriented programming with Python.

Python- Abstract Class and Method

Abstract class are a blueprint for other classes and is considered as the class designer.

A class whn designed needs a concrete implementation. Abstract class hold a contract between a designer and the implementer.

Instance of Abstract class cannot be created and it needs to be inherited and implemented in the subclass.

For a larger functional unit or a generic/common functionlaity is to be provided use Abstract class. Abstract class allows to implement the fucntionlaity in the subclass.

Abstract method allows to declare the method and impelement the functionality in the subclasses where the Abstract class is inherited.

A method in class can be decorated/marked as abstract with @abstractmethod keyword. Use Import abc to use the Abstract method.

See below example of using Abstract method-

Here abc is imported and the print_output method is decorated as @abstractmethod. The definition of such method is pass which means a control is passed to the subclasses where a implementation is expected for printing based on the output device.

The print_out method is implemented in Printer and Console subclasses inherited from class having a abstract method.


Python raises a TypeError if we try to instantitate a Abstract Class.

TypeError: Can’t instantiate abstract class OutputDevice with abstract method print_output

Python also raises error if the subclass doesn’t implement the abstract method in suclass.

See the below example for TypeErrors whilst using Abstract Class

Here the Log class does not implement the print_output abstract method.

You can also see the various usage of Static Methods, ClassMethods and there difference’s.

Difference between Static, abstract and Class Methods in Python

Class MethodStatic Method
requires “cls” as first parameter does not require parameters
has the ability to access the state of objectdoes not influence the state of the object
method decorated with @classmethodmethod decorated with @staticmethod
they can be used to create objectscan be only used as a helper method
More about @classmethodMore about @staticmethod
Abstract Method
requires to import abc package and abc.ABC as first parameter
cannot be instanted, gives TypeError is tried to instantiate Abstract Class
method decorated with @abstractmethod
can be used as super class and implementation should be in subclass
More about @abstractmethod

Python- Static Methods

Static Methods can be used when Class object is not required to call a method but the method’s within that class are related to execute the code.

Also static methods can be used when the state of the object is not required to maintain. i.e. they don’t have the ability to change the state of the object

Decorate class method with @staticmethod keyword to make the method static.

See below example of using Static Method-

validate_instance_counter(param) method is marked as static method.

Add multiple constructor to Python Class

Python does not explicitly support multiple constructors in class. But there are ways you can provision a class with multiple constructors.

  1. Constructor Overloading using arguments
  2. Calling methods from constructors
  3. Using Class Method decorator

Constructor Overloading using arguments

Constructor overloading can be achieved using the same __init__ method and writing the functionality in the constructor method based on arguments.

See below example-

Calling methods from constructors

Same as above example the below example calls the method from the constructor based on the isntance of the argument.

Python- Class Method

Class Methods are like Class Varaibles which works on Class but not on object level.

So this methods are bound to class and not object. See class varaibles here

Clas Methods are decorated with @classmethod attribute to the method in class.

This method can be called without creating instance of the class.

See below example definition and usage of class method.

In the above example get_instance_counter() method is decorated as @classmethod and a cls variable is passed. This holdsthe class reference for which the method is called.

The constructor works normal with __init__ method and updates calss variable counter increments the counter when an instance is created. This class variable accessed in the get_instance_counter() method.

You can also provision Python class to have multiple constructor with class method. See here – How to add multiple constructor to Python class?

Python- Decorator Stacking

Python allows to apply multiple decorators to callable object.

The decorator function is called based on the order or the sequence it has been listed.

Decorator can also use there own arguments. In the below example @log decorator a parameter is passed with value ‘log sample’.

Log decorator has 2 wrapper functions. Outer function accepts the reference or pointer to the decorated fucntion and an internal function holds arguments that are passed to the decorated fucntion.

The decorated function is called from the internal wrapper function along with the parameters.

The parameter passed to the decorator is also assessible in the decorator function.

Coming back to Decorator stacking. The below example is used to decorate the fucntion with multiple decorators. Here log and executiontime are decorator function and stacked on the decorated function i.e. sample