Assignment 1
Assignment 1
Assignment 1
Hyperledger(Sawtooth Lake)
Popularity: High and actively updated in GitHub
N/w Type: Both Private and Public
Cost: Open Source
Programming Languages: Python (For Sawtooth Lake)
GitHub Repo: sawtooth-core (Python)
5
Blockchain Platforms (2)
Multichain
Popularity: Medium but actively updated in GitHub
N/w Type: Private, Permissioned
Pricing: Free, Open Source
Supported Languages: Python, C#, JavaScript , PHP, Ruby
GitHub Repo: savior (Python), c# MultichainLib (C#), Multichain-
Node (JavaScript), libphp-multichain (PHP), multichain-client (Ruby)
HydraChain
Popularity: Low but actively updated in GitHub
N/w Type: Private, Permissioned
Pricing: Open Source
Supported Languages: Python
GitHub Repo: hydrachain (Python)
6
Blockchain Platforms (3)
Open Chain
Popularity: Medium but actively updated in GitHub
N/w Type: Private
Pricing: Open Source
Supported Languages: Javascript
GitHub Repo: openchain-js (Javascript)
IOTA
Popularity: Low but actively updated in GitHub
N/w Type: Public, Permissioned
Pricing: Based on IOTA Token
Supported Languages: Python , C, Javascript
GitHub Repo: iota.lib.py (Java), ccurl (C), iota.lib.js (Javascript)
8
What is Smart Contract?
Facilitates transfer of assets other than value or cryptocurrency.
Specifies rules for an operation on blockchain.
Implements policies for transfer of assets in decentralized networks.
Adds programmability and intelligence to the blockchain.
Represents business logic layer.
Includes messages that invokes functions.
Bitcoin transaction vs. Smart contract transaction
Tx(SendValue) Tx(Vote)
S2 S3 S2 S3
Tx(SendValue)
Tx(Validate Voter) Tx(Count)
Tx(SendValue)
S1 S1
S4 S4
Tx(Declare winner)
Tx(SendValue)
S5 S5
Functions on Addresses
Solidity features
Inheritance
contract StandardPolicies { ….}
contract StatePolicies is StandardPolicies {
// other policies …
}
Function definition
function nameOfFunction(parameters) visibilityModifiers
accessModifiers returns (returnParameters) { … }
Any number of parameters can be returned unlike other programming
languages
e.g., var(name,age) = getNameAge();
Solidity Structure
Solidity: Java Script + Java + C++, written to target EVM
Remix IDE is used to code and test the solidity.
Remix IDE provides three test environments:
Java Script VM
Injected Web 3 (e.g., Meta Mask)
Web 3 Provider (e.g., Ethereum Node)
Detailed structure of Solidity includes
Data or state variables
List of functions
Constructor – Default or user specified
Fall back functions
View functions
Pure functions (e.g., Math functions)
Public functions – accessible to transactions
Private functions – accessible to current contract
Internal functions – accessible to inside/inherited contract
External functions – accessible from only outside of the contract
User defined types in struct and enums
Modifiers
Events
Processing Smart Contracts
Smart Smart contract address is computed
by hashing EOA number with nonce
Contract
Remix
Compile
Process
Execution order
Modifiers (Example)
Execution order
Some questions needs to be addressed
Can we reject the transaction if it doesn’t conform to the rules?
Use revert() declaration
Apply the concepts of function modifier, revert and assert
Can we separate validation from the code that is executed?
Can we specify the problem specific rules and conditions so that
they can be independently specified and audited?
How to interact with the client applications?
Use events to interact with client applications. It is by defining and
pushing an event to the subscribers listener.
Syntax: event nameOfEvent(parameters);
Example: event votingCompleted();
Events are pushed from the smart contract and user applications listen
them using listener code to 1) track transactions, 2) receive results, 3)
initiate pull request to receive information from smart contract.
Smart contracts – Best practices (1)
Make sure that your application requirements require
blockchain features
Blockchain suitable for decentralized problems
Applications involve peer-to-peer transactions
Applications that work beyond boundaries of trust among
unknown peers.
Applications that require verification, validation and recording
of transactions over global time with immutable ledger.
Applications involve autonomous operations guided by rules
and policies.
Smart contracts – Best practices (2)
Make sure you need a smart contract on blockchain for your
application.
Smart contracts are visible to all participants on the chain and can be
executed on all full nodes in the network.
Smart contracts are needed when collective agreement of rules and
policies are required.
Decisions and problems must be recorded.
Smart contract is not for single node computation.
Keep the smart contract code simple, coherent and auditable.
Should not include redundant data and non useful functions.
Make smart contract functions auditable by using custom function
modifiers instead of inline if-else code.
Keep only necessary data in the smart contract.
Separate on-chain and off-chain data.
Smart contracts – Best practices (3)
Use appropriate data types.
Example: In Ballot smart contract, use number for proposal instead
of name (string) for it. It is because string is dynamic.
Top level applications may map id with names.
EVM has a 256 bit processor.
Make sure using of integer arithmetic for computational needs.
Uint8 to 256-bit data type
Understand public visibility modifier for the data
Irrespective of a variable visibility, each variable can be visible
to all participants on the chain.
If a variable is explicitly declared as public then Solidity
compiler generates a getter method for it.
Public data internally accessed via variable name, but externally via
getter function generated by Solidity compiler.
Smart contracts – Best Practices (4)
Public visibility example
contract MyC {
uint public data = 33;
}
contract CallerMyC {
MyC myc = new MyC();
function fun() {
uint local = myc.data();
}
}
Maintain a standard order for different function types with in the smart
contracts. The order may be 1) constructor, 2) fallback functions, 3) external, 4)
public, 5) internal, 6) private, and place 7) constant functions at last.
Functions can have any number of modifiers. Visibility modifier should come
before custom defined modifiers.
Multiple modifier functions should be written by a white space separation.
Modifier functions to be called based on dependency.
Smart contracts – Best practices (5)
Use Solidity defined payable modifier when sending a value.
function deposit() payable {}
function register (address sender) payable {}
Pay attention to the order of functions in the code.
Use modifier declarations for implementing rules.
Use function access modifiers for
Implementing rules, policies and regulations
Implementing common rules for all who may access the functions
Declaratively validating application specific functions
Providing auditable elements to allow verification of the correctness of a smart
contract.
Use events for notification
Events can carry at most 3 index parameters.
Beware of now time variable.
Use secure hashing for protecting data.
(keccak256(), sha256(),ripemd256())
Beware of static warning in Remix IDE.