0% found this document useful (0 votes)
33 views13 pages

Understanding Smart Contracts and Solidity

Uploaded by

xometex256
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views13 pages

Understanding Smart Contracts and Solidity

Uploaded by

xometex256
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

5/20/2023

Smart Contracts Outline


• What are smart contracts?
• Basics of Solidity programming in Ethereum
• Remix IDE
Buddhika Sandaruwan

University of Colombo School of • Disaster stories of smart contracts


Computing

5/20/2023
University of Colombo School of Computing 2

1 2

What are smart Introduction to smart contracts


contracts? Smart contracts are self-executing contracts with the terms of the
agreement directly written into lines of code.
• Introduction
These contracts are stored on a blockchain network, typically
• Initial thoughts
associated with decentralized platforms like Ethereum.
• Blockchain and Bitcoin
Smart contracts aim to facilitate, verify, and enforce the
• Contracts vs smart negotiation and performance of agreements without the need for
contracts
intermediaries.
5/20/2023
5/20/2023

University of Colombo School of Computing 3 University of Colombo School of Computing 4

3 4
5/20/2023

Characteristics of
smart contracts (1) Characteristics of smart contracts (2)
• Autonomy

• Smart contracts operate automatically, executing


• Trust
predefined actions once the specified conditions • Smart contracts leverage the security and transparency of
are met.
blockchain technology.
• They eliminate the need for manual intervention
and reduce the reliance on trusted third • The terms of the contract are recorded on a distributed ledger,
parties. making them immutable and resistant to tampering.
• Participants can verify the execution and outcome of the contract
without placing trust in a central authority.
5/20/2023

5/20/2023
University of Colombo School of Computing 5 University of Colombo School of Computing 6

5 6

Characteristics of smart contracts (3) Characteristics of smart contracts (4)


• Efficiency • Transparency
• By automating contract execution and removing intermediaries, • The decentralized nature of blockchain technology ensures that
smart contracts can streamline processes, reduce costs, and the details of a smart contract are visible to all participants
enhance efficiency. on the network.
• The need for manual paperwork, administrative tasks, and • This transparency fosters trust, accountability, and auditability
reconciliation is minimized, saving time and resources. as every action and transaction associated with the contract is
recorded on the blockchain.
5/20/2023

5/20/2023

University of Colombo School of Computing 7 University of Colombo School of Computing 8

7 8
5/20/2023

Applications Initial thoughts: Nick Szabo in 1994


• Smart contracts find applications in various fields, such as A smart contract is a computerized transaction protocol that
financial services, supply chain management, real estate, executes the terms of a contract. The general objectives are to
healthcare, and more. satisfy common contractual conditions (such as payment terms,
liens, confidentiality, and even enforcement), minimize exceptions
• They can be used for tasks like facilitating digital asset
both malicious and accidental, and minimize the need for trusted
transfers, enabling decentralized crowdfunding (initial coin
offerings or ICOs), establishing escrow services, automating intermediaries. Related economic goals include lowering fraud
loss, arbitrations and enforcement costs, and other transaction
supply chain processes, and enforcing the terms of agreements
between parties. costs.
-Nick Szabo “The Idea of Smart Contracts”
5/20/2023

5/20/2023
University of Colombo School of Computing 9 University of Colombo School of Computing 10

9 10

Blockchain and Bitcoin


Blockchain and Bitcoin
• Blockchain is a decentralized and • At its core, a blockchain is a chain of
distributed digital ledger that records blocks, where each block contains a list
transactions across multiple computers or of transactions.
nodes.
• These transactions are grouped together
• It is designed to be secure, transparent, and added to the blockchain in a
and tamper-resistant. sequential and chronological order.
• The blockchain technology was introduced • Once a block is added, it is extremely
in 2008 as the underlying technology difficult to alter or remove the
behind the popular cryptocurrency, information contained within it, ensuring
Bitcoin, by an anonymous person or group the integrity of the data.
5/20/2023

5/20/2023

of people known as Satoshi Nakamoto.

University of Colombo School of Computing 11 University of Colombo School of Computing 12

11 12
5/20/2023

Digital currencies: just one blockchain Smart Contracts: user-defined programs


application running on top of a blockchain

Money Decentralized Consensus Money Decentralized Consensus


Users “Blockchain” Users “Blockchain”

Account Balances Contracts


Storage
Alice: ᴃ 10
Bob: ᴃ 15 Code
Carol: ᴃ 120
Data
5/20/2023

5/20/2023
University of Colombo School of Computing 13 University of Colombo School of Computing 14

13 14

Contracts vs Smart contracts Contracts vs Smart contracts


• Traditional Contract • Smart Contract
• A traditional contract is a legally binding agreement between two • A smart contract, on the other hand, is a self-executing digital
or more parties that outlines the terms, conditions, and contract with the terms of the agreement written in lines of
obligations of the involved parties. code.
• These contracts are typically written in natural language (e.g., • Smart contracts are typically built on blockchain platforms, such
English) and are enforced by legal systems. as Ethereum.
• Traditional contracts often require manual verification, • They are designed to automate and facilitate the execution,
execution, and enforcement, which may involve intermediaries, verification, and enforcement of agreements without the need for
legal processes, and paperwork. intermediaries.
5/20/2023

5/20/2023

University of Colombo School of Computing 15 University of Colombo School of Computing 16

15 16
5/20/2023

Contracts vs Smart contracts


Crowdfunding

Domain registry

Car sales
Basics of Solidity
Auction
programming in
Insurance
Ethereum
5/20/2023

5/20/2023
University of Colombo School of Computing 17 University of Colombo School of Computing 18

17 18

Solidity Key features


• Solidity is a high-level programming language specifically designed for writing • Contract-oriented: Solidity is a contract-oriented language, meaning it focuses on
smart contracts on blockchain platforms, with Ethereum being the primary target. defining and implementing smart contracts. A smart contract in Solidity is a
collection of functions and data variables that define the behavior and state of
• It is the most widely used programming language for developing decentralized
an agreement.
applications (DApps) and smart contracts on the Ethereum blockchain.

• Solidity combines elements from established languages like JavaScript, C++, and
Python and incorporates features specifically tailored for blockchain and smart • Strongly Typed: Solidity is a statically-typed language, which means variables
contract development. must have their type explicitly declared and checked at compile-time. This helps
prevent type-related errors during contract execution.
• It allows developers to define the rules and logic of smart contracts, specifying
how they should behave and interact with other contracts and participants in the
network.
• Object-oriented: Solidity supports object-oriented programming (OOP) principles.
Developers can define and use custom data structures, create inheritance
5/20/2023

5/20/2023

hierarchies, and utilize encapsulation, polymorphism, and other OOP concepts.

University of Colombo School of Computing 19 University of Colombo School of Computing 20

19 20
5/20/2023

Key features Key features


• Ethereum-specific Features: Solidity provides specific features to interact with • Tooling and Ecosystem: Solidity has an extensive tooling ecosystem, including
the Ethereum blockchain and its ecosystem. This includes functionalities like development frameworks (such as Truffle and Hardhat), integrated development
accessing account balances, managing Ethereum tokens (ERC-20, ERC-721), environments (IDEs) with Solidity support (such as Remix and Visual Studio Code),
interacting with other contracts, and handling events. and testing frameworks (like Mocha and Chai) tailored for smart contract
development.

• Security Considerations: Solidity aims to enable secure contract development.


However, writing secure contracts requires careful consideration of potential
vulnerabilities, such as reentrancy attacks, integer overflows/underflows, and
other common pitfalls. The Solidity language and development practices have
evolved to address and mitigate these risks.
5/20/2023

5/20/2023
University of Colombo School of Computing 21 University of Colombo School of Computing 22

21 22

Solidity Example: Domain Name Registry in


• Solidity code is compiled into bytecode that can be executed on the Ethereum Ethereum
Virtual Machine (EVM) or compatible blockchain platforms. Once deployed, smart
contracts written in Solidity are immutable and enforceable on the blockchain,
executing their predefined logic autonomously.

• It's worth noting that while Solidity is primarily associated with Ethereum,
there are other blockchain platforms that support alternative languages for
writing smart contracts, such as Vyper for Ethereum and other domain-specific
languages for platforms like NEO, EOS, and Cardano.
5/20/2023

5/20/2023

University of Colombo School of Computing 23 University of Colombo School of Computing 24

23 24
5/20/2023

Example: Domain Name Registry in Interaction between Contracts


Ethereum

ucsc.lk
5/20/2023

5/20/2023
University of Colombo School of Computing 25 University of Colombo School of Computing 26

25 26

Remix IDE: File explorer and


Remix IDE workspace

https://remix.ethereum.org
5/20/2023

5/20/2023

University of Colombo School of Computing 27 University of Colombo School of Computing 28

27 28
5/20/2023

Remix IDE: Solidity compiler and Failures and


Deploy Vulnerabilities
Smart contracts, despite their benefits, are not
immune to failures or vulnerabilities. Here are some
common failures and challenges associated with smart
contracts:
• Code Vulnerabilities:
 Smart contracts are written in programming
languages, and like any software, they can
contain bugs, logic errors, or security
vulnerabilities.
 These vulnerabilities can be exploited by
attackers to manipulate or steal funds, disrupt
contract functionality, or cause unintended
5/20/2023

5/20/2023
consequences.

University of Colombo School of Computing 29 University of Colombo School of Computing 30

29 30

Failures and Failures and


Vulnerabilities Vulnerabilities
• External Data Dependence: • Immutability Limitations:
 Smart contracts on blockchain  While immutability is a desirable
platforms, such as Ethereum, generally characteristic of smart contracts, it can also
have limited access to external data
pose challenges.
sources.
 Once deployed on the blockchain, it is
 They rely on oracles or other
mechanisms to interact with off-chain difficult to modify the contract's code or fix
data. issues without creating a new contract.

 If the external data is compromised or  This lack of upgradability can be problematic


manipulated, it can impact the if vulnerabilities or bugs are discovered after
behavior and integrity of the smart deployment.
contract.
5/20/2023

5/20/2023

University of Colombo School of Computing 31 University of Colombo School of Computing 32

31 32
5/20/2023

Failures and Failures and


Vulnerabilities Vulnerabilities
• Regulatory and Legal Compliance: • Human Errors and Intentions:

 Smart contracts may operate in regulatory gray  Smart contracts are created by humans, and they
areas or may not fully comply with existing can inadvertently introduce errors or biases into
legal frameworks. the code.

 The enforceability and legal recognition of  Additionally, the intentions or motivations of


smart contracts can vary across jurisdictions. the contract creators or participants may not
always align with the best interests of all
 This can lead to uncertainties and challenges
parties involved.
in resolving disputes or addressing legal
issues that arise from smart contract  Malicious actors can intentionally exploit or
interactions. manipulate smart contracts for personal gain.
5/20/2023

5/20/2023
University of Colombo School of Computing 33 University of Colombo School of Computing 34

33 34

Failures and
Disaster stories of
Vulnerabilities
• Lack of Common Standards:
Smart Contracts
 The lack of standardized practices and
protocols in smart contract development can
contribute to interoperability issues and • King of the Ether Throne
inconsistent security practices.
 It can also hinder the ability to audit and
verify smart contracts effectively. • Parity Multisig Wallet

• The DAO attack


5/20/2023

5/20/2023

University of Colombo School of Computing 35 University of Colombo School of Computing 36

35 36
5/20/2023

King of the Ether Throne King of the Ether Throne


• Ether is stored in accounts. The King of the Ether Throne contract ("KotET contract" for short) is another
• There are two fundamental types of accounts - example of a "contract account". The normal operation of the KotET contract is
"externally-owned accounts" and "contract accounts". (essentially) this:
• The "externally-owned accounts" are normally controlled • Suppose the current claim price for the throne is 10 ether.
by a human, whereas the "contract accounts" are under
the control of a contract. • You want to be King/Queen, so you send 10 ether to the contract.

• The Ethereum Mist Wallet Client encourages Ethereum • The contract sends your 10 ether (less a 1% commission) to the previous
users to create "contract-based wallets" (that is, King/Queen, as a "compensation payment".
"contract accounts") to hold their ether.
• The contract makes you the new King/Queen of the Ether Throne.
• All Ethereum transactions such as payments and calls
• The new claim price for the throne goes up by 50%, to 15 ether in this case.
are always started by an "externally-owned account" -
if you pay someone from a "contract-based wallet", your • If a user comes along who is willing to pay 15 ether, they depose you and
"externally-owned account" must have told your become King/Queen, and you receive their payment of 15 ether as your
"contract-based wallet" to do so.

5/20/2023
5/20/2023

"compensation payment".

https://www.kingoftheether.com/postmortem.html https://www.kingoftheether.com/postmortem.html

University of Colombo School of Computing 37 University of Colombo School of Computing 38

37 38

King of the Ether Throne King of the Ether Throne


• In Ethereum, carrying out a "transaction" such as • The King of the Ether Throne contract behaved
sending a payment to a contract, or calling a correctly in all cases apart from when it sent a
contract, costs "gas". payment to a "contract account" such as an
Ethereum Mist "contract-based wallet".
• The amount of "gas" consumed depends on what sort
• When the King of the Ether Throne contract sent a
of operations the contract you call does (and how
payment to a "contract account", it inadvertently
many).
included only a small amount of gas with the
• This "gas" is a small payment which goes to the payment - 2300 gas. This was not enough gas for an
miners and helpers pay for providing the Ethereum Ethereum Mist "contract-based wallet" contract to
network and block-chain storage. successfully process a payment - instead, the
wallet contract failed.
• The gas is paid for by the "externally-owned
• When a wallet contract failed to process the
account" which stared the transaction.
payment sent to it by the KotET contract, the
• When making a transaction, you include a little ether paid was returned to the KotET contract. The
gas with the transaction (unused gas is refunded). KotET was not aware that the payment had failed,
and it continued processing, making the caller
5/20/2023

5/20/2023

• Often Ethereum clients do this for you. King despite the compensation payment not having
https://www.kingoftheether.com/postmortem.html been sent to the previous monarch.
https://www.kingoftheether.com/postmortem.html
University of Colombo School of Computing 39 University of Colombo School of Computing 40

39 40
5/20/2023

// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT


pragma solidity ^0.8.13; pragma solidity ^0.8.13;

contract KingOfEther {
contract Attack {
address public king;
KingOfEther kingOfEther;
uint public balance;

function claimThrone() external payable { constructor(KingOfEther _kingOfEther) {

require(msg.value > balance, "Need to pay more to become the king"); kingOfEther = KingOfEther(_kingOfEther);

}
(bool sent, ) = king.call{value: balance}("");
require(sent, "Failed to send Ether");
function attack() public payable {

kingOfEther.claimThrone{value: msg.value}();
balance = msg.value;
}
king = msg.sender;
}
}
5/20/2023

5/20/2023
}

University of Colombo School of Computing 41 University of Colombo School of Computing 42

41 42

• Parity multisig wallet, also known as the


Parity Multi-signature Wallet, is a type of
Ethereum wallet that provides multi-signature Parity Multisig Wallet
functionality.
• It is a wallet contract implemented on the • On November 6th, 2017, Github user devops199 posted the following statement under
Parity Ethereum client, which is an Ethereum the Github issues section of the Parity Multi-Sig Library:
client developed by Parity Technologies.
“I accidentally killed it”.
• Multi-signature wallets, including Parity
multisig wallet, are designed to enhance the
security of funds by requiring multiple • Just moments earlier, devops199 exploited a vulnerability within the smart-
parties to authorize transactions. contract library code, blocking funds in 587 wallets holding a total of
513,774.16 Ether as well as various other tokens.
• Instead of a single private key controlling
access to the wallet, a multi-signature • So, what exactly happened and, more importantly, what as the aftermath?
wallet requires multiple private keys or
signatures to approve and execute • First, let’s see how it all started. Following the aftermath of the first Parity
transactions. hack, the WalletLibrary contract was deployed on July 20th.

• The lines one should take most notice of are lines 215 (only_uninitialized) and
225 (kill).
5/20/2023
5/20/2023

Parity Multisig Wallet


University of Colombo School of Computing 43 University of Colombo School of Computing 44

43 44
5/20/2023

Parity Multisig Wallet


• On November 6th, Transaction 0x05f71e1b was sent to WalletLibrary, which called
the initWallet method.

• This transaction ended up making the 0xae7168deb525862f4fee37d987a971b385b96952


the sole owner.

• Transaction 0x47f7cff7 called the kill method of WalletLibrary with


0xae7168deb525862f4fee37d987a971b385b96952 as the beneficiary address.

• Approximately 30 minutes later, Devops199 created Parity issue #6995 to document


what had just occurred.

• Devops199 posted a link to the issue in the Parity gitter channel, asking “Is
this serious issue?”

• He then followed up by asking “Will I get arrested for this?”.

Parity Multisig Wallet

5/20/2023
• It seemed as though Devops199 failed to understand the consequences of what he
5/20/2023

had done, by stating “I’m eth newbie.. just learning”

University of Colombo School of Computing 45 University of Colombo School of Computing 46

45 46

Parity Multisig Wallet


• About three hours later, Parity released a warning on Twitter: “A user exploited Parity Multisig
an issue and thus removed the library code, as it seems unaware of the
consequences. This froze funds in all Parity multi-sig wallets deployed after 20
July. We are analyzing the situation and release further details shortly.”
Wallet
• It appears as though those funds will be locked, at least in the foreseeable
future. • Parity has decided to remove the ability
to deploy multi-sig wallets until they’ve
• Let’s take a look at the aftermath of the hack — on November 15th, 2017, Parity
confirmed they have the proper security in
posted a “Postmortem” detailing what exactly occurred, as well as the steps that
place to ensure it doesn’t happen again.
the Parity team would commit to in the future.
• Secondly, they are going forward with
• Parity stated that, as a result of the hack, “the entire ecosystem as a whole is
another full-stack external security audit
in urgent need of procedures and tooling to prevent similar issues from happening
of all existing sensitive code (i.e., key
again, in particular if and when the number and complexity of live contracts
generation, password management, signing
grows.”
5/20/2023

5/20/2023

etc.).

University of Colombo School of Computing 47 University of Colombo School of Computing 48

47 48
5/20/2023

The DAO attack The DAO attack


• DAO (Decentralized Autonomous Organization) • Once the crowd sale was over, there was much discussion of first addressing the
vulnerabilities before starting to fund proposals.
• Goal is to codify the rules and decision-making apparatus of an organization,
eliminating the need for documents and people in governing, creating a structure • In particular, Stephan Tual, one of The DAO’s creators, announced on 12th June
with decentralized control. that a "recursive call bug" had been found in the software but that "no DAO funds
[were] at risk".
• The DAO launched on 30th April 2016, with a 28-day funding window.
• At the time, more than 50 project proposals were waiting for The DAO’s token
• For whatever reason, The DAO was popular, raising over $100m by 15th May, and by
holders to vote on them.
the end of the funding period, The DAO was the largest crowdfunding in history,
having raised over $150m from more than 11,000 enthusiastic members. The DAO • Everyone who writes a smart contract knows that if it can move a large amount of
raised far more money than its creators expected. cash, it will be subject to attack.

• It can be said that the marketing was better than the execution, for during the • This particular vulnerability was discovered recently in another system, called
crowd sale, several people expressed concerns that the code was vulnerable to Maker DAO, and was neutralized quickly because that DAO was still in testing.
attack.
5/20/2023

5/20/2023
University of Colombo School of Computing 49 University of Colombo School of Computing 50

49 50

The DAO attack The DAO attack


• Unfortunately, while programmers were working on fixing this and other problems, • Because the child DAO has the same structure, limitations, and vulnerabilities as
an unknown attacker began using this approach to start draining The DAO of ether the parent DAO, the ether in this newly created child DAO can’t be accessed for
collected from the sale of its tokens. 28 days, as that is the initial funding period.

• By Saturday, 18th June, the attacker managed to drain more than 3.6m ether into a • Everyone can see the ether in this child DAO – any attempts to cash it in will
“child DAO” that has the same structure as The DAO. trigger alarms and investigations.

• The price of ether dropped from over $20 to under $13. • It could be that the attacker will never get to cash or spend a single ether of
it.
• Several people made attempts to split The DAO to prevent more ether from being
taken, but they couldn’t get the votes necessary in such a short time. • It’s entirely possible that the attacker had a large short position on ether at
the time of the attack, which he or she then cashed out after ether had been cut
• Because the designers didn’t expect this much money, all the ether was in a
roughly in half.
single address (bad idea), and we believe the attacker stopped voluntarily after
hearing about the fork proposal. • The attacker may already have made his money, regardless of the ether sitting in
the child DAO.
• In fact, that attack, or another similar one, could continue at any time.
5/20/2023

5/20/2023

University of Colombo School of Computing 51 University of Colombo School of Computing 52

51 52

You might also like