Understanding Smart Contracts and Solidity
Understanding Smart Contracts and Solidity
5/20/2023
University of Colombo School of Computing 2
1 2
3 4
5/20/2023
Characteristics of
smart contracts (1) Characteristics of smart contracts (2)
• Autonomy
5/20/2023
University of Colombo School of Computing 5 University of Colombo School of Computing 6
5 6
5/20/2023
7 8
5/20/2023
5/20/2023
University of Colombo School of Computing 9 University of Colombo School of Computing 10
9 10
5/20/2023
11 12
5/20/2023
5/20/2023
University of Colombo School of Computing 13 University of Colombo School of Computing 14
13 14
5/20/2023
15 16
5/20/2023
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 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
19 20
5/20/2023
5/20/2023
University of Colombo School of Computing 21 University of Colombo School of Computing 22
21 22
• 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
23 24
5/20/2023
ucsc.lk
5/20/2023
5/20/2023
University of Colombo School of Computing 25 University of Colombo School of Computing 26
25 26
https://remix.ethereum.org
5/20/2023
5/20/2023
27 28
5/20/2023
5/20/2023
consequences.
29 30
5/20/2023
31 32
5/20/2023
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.
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
5/20/2023
35 36
5/20/2023
• 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
37 38
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
contract KingOfEther {
contract Attack {
address public king;
KingOfEther kingOfEther;
uint public balance;
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
}
41 42
• The lines one should take most notice of are lines 215 (only_uninitialized) and
225 (kill).
5/20/2023
5/20/2023
43 44
5/20/2023
• Devops199 posted a link to the issue in the Parity gitter channel, asking “Is
this serious issue?”
5/20/2023
• It seemed as though Devops199 failed to understand the consequences of what he
5/20/2023
45 46
5/20/2023
etc.).
47 48
5/20/2023
• 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
• 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
51 52