Ethereum Blockchain

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 77

ETHEREUM BLOCKCHAIN

ĐÀO TẠO KIẾN THỨC CƠ BẢN


Ethereum Blockchain
Đào tạo kiến thức cơ bản về
⎯ Khái niệm Blockchain & Consensus
⎯ Các kiểu blockchain & nền tảng blockchain
⎯ Bức tranh lĩnh vực ứng dụng của blockchain
⎯ Thành phần của Ethereum Blockchain
⎯ Exchange
⎯ Wallet
⎯ Transaction
⎯ Smart Contract & Solidity
⎯ Token
⎯ Ethereum Client (Node)
Dapp

ASIA BLOCKCHAIN CENTER



⎯ Web3
⎯ Explorer
⎯ Oracles
Blockchain là gì ?
Blockchain là một chuỗi các khối có chứa thông tin.
Ngay từ chính cái tên của nó đã nói lên tất cả – block
(khối) và chain (chuỗi).
Blockchain được ví như một cuốn sổ kế toán chính
(cuốn sổ cái) của một công ty. nơi mà mọi hoạt động
liên quan đến tiền của công ty được giám sát rất chặt
chẽ.
Trong trường hợp này Blockchain là một cuốn sổ cái
hoạt động trong lĩnh vực công nghệ, và dữ liệu được lưu
trữ là các dữ liệu số.

allows the resulting ledger to be


accessed by different servers.

ASIA BLOCKCHAIN CENTER


Blockchain là gì ?

Data thực tế trên các Blockchain khác thường gồm 1 tập các Hash dùng để định danh cho Block và nội dung (data) của
Transaction. block. Hash là duy nhất, nếu bạn thay đổi nội dung bên trong
block, thì hash sẽ khác

allows the resulting ledger to be


accessed by different servers.

Block đầu tiên trong chain gọi là Genesis Block ASIA BLOCKCHAIN CENTER
P2P & Cơ chế đồng thuận Proof of Work ?
P2P Network là mạng ngang hàng, có nghĩa ai cũng có thể tham gia, sau khi tham gia thành công, sẽ giữ 1 bản copy dữ liệu
blockchain. Chúng được định nghĩa là 1 Node

1 Block mới được tạo bởi 1 node nào đó, thông qua mạng P2P, block sẽ được gửi tất cả node trong mạng. Tất cả node khi nhận block, chúng sẽ xác thực
block hợp lệ hay không. Sau đó thông qua giao thức đồng thuận, chúng sẽ tập hợp kết quả xác thực tất cả các node về block, trên 50% đạt được sự đồng
thuận về block hợp lệ thì block sẽ được đưa vào chuỗi
Consensus protocol

Proof of Work
Proof of Stake
Proof of Burn
Proof of Authority
Raft consensus
Federated consensus
Proof of activity
Proof of elapsed time
Proof of capacity
Proof of Work (PoW)
POW is the most common consensus mechanism and is used in Bitcoin and Ethereum. When a new block of transactions is being added to the
blockchain, to verify these transactions, POW is performed by miners (i.e., powerful computers or processors) that are competing with one another to
solve a cryptographic problem (i.e., hash puzzle). The cryptographic problem is based on the new block of transactions and also all other previous
transactions block in the blockchain. The cryptographic problem is designed such that it is extremely difficult and time consuming to solve it (i.e.,
hashing) but once found by one of the miners, the solution is easily verifiable on the network by all other miners. Once the cryptographic solution has
been verified as valid, the miner who solved it receives a mining reward for the computational work being done to solve the cryptographic problem.
PoW
Features of Proof of Work system:
There are mainly two features that have contributed to the wide popularity of this consensus protocol and they are:
It is hard to find a solution for the mathematical problem
It is easy to verify the correctness of that solution
Main issues with the Proof-of-Work consensus:
The Proof-of-Work consensus mechanism has some issues which are as follows:
The 51% risk: If a controlling entity owns 51% or more than 51% of nodes in the network, the entity can corrupt the blockchain by gaining the majority of the
network.
Time consuming: Miners have to check over many nonce values to find the right solution to the puzzle that must be solved to mine the block, which is a time
consuming process.
Resource consumption: Miners consume high amounts of computing power in order to find the solution to the hard mathematical puzzle. It leads to a waste of
precious resources(money, energy, space, hardware). It is expected that the 0.3% of the world’s electricity will be spent to verify transactions by the end of
2018.
Transaction confirmation takes about 10–60 minutes. So, it is not an instantaneous transaction; because it takes some time to mine the transaction and add it to
the blockchain thus committing the transaction.
PoS
POS is a consensus mechanism designed to mitigate some of the criticisms of POW. The concept is based on the idea that miners that hold a large
amount of cryptocurrency have a larger stake in ensuring the validity of the blockchain.

In POS, a miner has to submit a portion of coins as their stake. The coins being staked have a “Coin Age”, which is basically a measure of how long a
coin holder has held onto coins without spending or moving them. When a new block of transactions is being added, selecting which miner produces
the new block is based on the total Coin Age of all the coins that are being staked and the cryptographic problem. There are no block rewards, and
each miner receives a transaction fee. POS is faster, more resource-friendly, and energy efficient compared to POW. To improve the security of the
consensus mechanism, newer versions of POS include miners also staking a bet of coins on the validity of a block that is being added to the
blockchain. If the block is successfully (not successfully) appended, the miners will get a transaction fee proportionate to their bets (some or all of
their stake will be penalized/removed).
Difficulty
What is Ethereum difficulty?
Ethereum difficulty (or network difficulty) is a key value for every cryptocurrency. Ethereum Network difficulty is the difficulty of a problem that
miners must solve to find a block

Why does network difficulty change?


Every cryptocurrency has the preset average block find time managed by a network. If the number of miners increases, the network hashrate goes
up. The effective block find time becomes lower than the preset value. As a result, the network gradually increases its difficulty, that is, the difficulty
of a problem that miners are solving. The network will keep increasing it until the block find time reaches the preset value.

Same thing when the number of miners decreases. When miners leave, the network hashrate goes down. Miners need more time to find a block. So
the network lowers its difficulty, thus making a problem easier to solve
Public & Private Blockchain

TOP PUBLIC TOP PRIVATE

ASIA BLOCKCHAIN CENTER


Public & Private Blockchain

ASIA BLOCKCHAIN CENTER


Blockchain Application

ASIA BLOCKCHAIN CENTER


Thành phần của
Ethereum

ASIA BLOCKCHAIN CENTER


Thành phần của Ethereum
1. P2P network
Ethereum blockchain is a peer to peer network, all computers or nodes are connected with each
other.
2. Nodes
Any device whether a computer or a mobile, connected to the blockchain containing the data
are called nodes. All nodes are connected to each other.
3. Transactions
The messages exchanged on the network are called transactions. A transaction involves a value,
a recipient, a sender, and a data payload.
4. Consensus Rules
The set of rules that is followed for considering the validity of a transaction and a block in
Ethereum I called consensus rules. These rules are enforced by all nodes.
5. Consensus algorithm
The consensus algorithm is the procedure to obtain agreement on the longest chain in the
distributed Ethereum network. The most commonly used consensus algorithms for Ethereum
are proof of work and proof of stake.
ASIA BLOCKCHAIN CENTER
Thành phần của Ethereum
6. State Machine
● Ethereum transactions and state changes are processed by the state
machine called Ethereum Virtual Machine abbreviated as EVM.
● Ethereum virtual machine executes the program in machine
language.
● These programs are written in a high level language such as Solidity
and then a compile converts the program into bytecode.
● The program written in a high level language is called a smart
contract.
So, these were some components and terminologies associated with
Ethereum Blockchain. Next, I am going to introduce the cryptocurrency
that Ethereum uses.

ASIA BLOCKCHAIN CENTER


Sàn giao dịch
(Exchange)

ASIA BLOCKCHAIN CENTER


Wallet (Ví)

ASIA BLOCKCHAIN CENTER


Định nghĩa
Wallet

• A blockchain wallet is just like a real-life wallet but in software


form. It is the digital form of a wallet.
• A wallet is application software that acts as a user interface to
the Ethereum network.
• The wallet is a system to manage and store the keys.
• This digital wallet allows transferring cryptocurrencies from
one account to another.

ASIA BLOCKCHAIN CENTER


Chức năng của
Wallet

• Access to your money


• Access to accounts
• Management of keys
• Management of addresses
• Management of cryptocurrencies such as bitcoin or ether
• Tracking and viewing the user’s balance in different accounts
• Initiating transactions
• Signing transactions
• Interacting with contract accounts

ASIA BLOCKCHAIN CENTER


Chức năng quản lý
của Keys
• The actual function of a wallet is managing keys.
• If you think that your wallet will contain money then it is not true. You can view your balance via
your wallet but this balance is actually residing on the Ethereum blockchain.
• Your wallet contains keys (both private and public) and by using these keys, you can sign
transactions on Ethereum.
• To transfer ether or any cryptocurrency to other accounts, you need keys stored in your wallet.
• In centralized systems such as banks, your money is saved under your account. You can check the
account balance and perform transactions with the involvement of banks. While in the blockchain
you can control your money with your wallet yourself.

ASIA BLOCKCHAIN CENTER


Ứng dụng Private Key
& Public Key

ASIA BLOCKCHAIN CENTER


Chữ ký số
(Digital Signature)

ASIA BLOCKCHAIN CENTER


Sign & verify
giao dịch

ASIA BLOCKCHAIN CENTER


Các loại Ví
The wallets are classified into two main types
depending upon the relationship between the
keys they hold. These types are given below:
• Deterministic Wallet
• Non-deterministic Wallet

ASIA BLOCKCHAIN CENTER


Deterministic Wallet
• In this kind of wallet, all wallet keys are generated from a single key.
• This single key is called the master key and it is derived from a seed.
• All of the keys are related to each other. Here, the original seed value is very
important.
• These keys can be generated again with the use of seed value.
• Commonly, these keys are linked in the form of a tree structure where the root
is the seed value.

ASIA BLOCKCHAIN CENTER


Non-Deterministic Wallet
• The second type of wallet is a non-deterministic wallet. In this
kind, the keys are not related to each other.
• Each key of such a wallet is generated independently from
different seed values.
• This wallet is also termed a random wallet and it is better
than a deterministic wallet in terms of security.

ASIA BLOCKCHAIN CENTER


Mnemonic Words
• There is a concern about security in deterministic wallets as all
keys are related.
• If anyone gets a hold of the seed value, he can generate the

ASIA BLOCKCHAIN CENTER


master key and then all other keys of the wallet. It will give access
to the balance.
For unfortunate data loss accidents, when a person loses his seed, the
seed value is converted to mnemonic codewords. The mnemonic code
words are a bunch of English words that the wallet holder has to keep
safe. This word list can be saved in the form of a hard copy by writing
that on a piece of paper and then storing it in a secure place
Mnemonic Words

ASIA BLOCKCHAIN CENTER


Mnemonic Words

ASIA BLOCKCHAIN CENTER


Các loại
Mobile Wallet
phần mềm Ví
Desktop Wallet

Online Wallet

Hardware Wallet

Paper Wallet

ASIA BLOCKCHAIN CENTER


Mobile Wallet
• A mobile wallet is simple a phone
application.
• You install the wallet application on
your mobile phone and then access
your account.
• These wallets can be used anywhere
and are safer than online wallets.
• The only problem arises when there
is a phone breakdown when you can
ASIA BLOCKCHAIN CENTER lose your money.
Desktop Wallet
• A desktop wallet is also a safer option
than online wallets.
• You can download a desktop wallet on
your personal computer and access it
anytime from the same device.
• The issue with this type of wallet arises
when your desktop gets out of order,
gets hacked, or is infected by a virus.

ASIA BLOCKCHAIN CENTER


Online Wallet
• An online wallet is a convenient
option to use.
• It can be opened anywhere from any
device having an internet connection.
• These wallets reside on the cloud.
Online wallets have higher security
issues and can be used wisely.

ASIA BLOCKCHAIN CENTER


Hardware
Wallet
• In this kind of wallet, the keys are stored on
some hardware device such as a flash drive.
• The hardware wallet remains offline mostly
which provides security features.
• This wallet is a convenient and safe option
although expensive among others.

ASIA BLOCKCHAIN CENTER


Paper Wallet
ABOUT THE
• The last type of wallet is a paper wallet. This is the safest PROJECT
option among all options.
• The public and private keys are written or printed on a
sheet of paper to make a physical copy and that copy is
called a paper wallet.
• The transactions are done by entering private keys or you
can also scan the QR code of your paper wallet.

ASIA BLOCKCHAIN CENTER


Phần mềm ví
thông dụng
• Metamask
• Trust Wallet
• V.v

ASIA BLOCKCHAIN CENTER


Venus has a be Ví Custodial và
Non-Custodial

ASIA BLOCKCHAIN CENTER


Ví Custodial
The working of a custodial wallet is similar to that of a bank where the money stored in
your account is yours but is controlled by the bank.
1) As the name suggests, a custodial wallet is a wallet that is stored with a custodian or a
third party.
2) This clearly means that the party holding the wallet holds the private keys to the wallet
which indicates that the third party has full control over the funds of the user, thus
making them less secure.
3) But these custodial wallets have a great advantage of managing funds quickly and

ASIA BLOCKCHAIN CENTER


immediately at any given point of time.
4) Another important boon of custodial wallets is, in case the private keys are lost,
accounts can be easily restored.
5) In custodial wallets, transaction fees are lesser and transactions are quicker. But due to
a lot of AML/KYC checks, withdrawals take a long time and are slower.
6) Coinbase, Binance, BitMEX are some examples of Custodial wallets.
Ví Non- Custodial
Non-Custodial wallets are also known as light wallets.
1) A non-custodial wallet, true to its name, does not allow the custody of the wallet with
any custodian.
2) Only the user/owner of the wallet can access it and has full control of their funds thus
ensuring complete security.
3) User holds the private keys which he/she can use for accessing his/her wallet.
Withdrawals are comparatively faster in non-custodial wallets.

ASIA BLOCKCHAIN CENTER


4) In the event of loss of private keys, non-custodial wallets do not have the feasibility of
recovering the account.
5) Transaction fees have to be entirely borne by the user hence transaction fees are higher.
6) Electrum, Exodus, Ledger Nano X, TREZOR One etc are some examples of non-custodial
wallets
Transaction
ABOUT THE
PROJECT

ASIA BLOCKCHAIN CENTER


Các loại
Transaction
ABOUT THE
The generic transaction mentioned above is used for the three PROJECT
different purposes.
• Fund Transfer Between EOA (Externally Owned Account -
> Tài khoản của User)
• Deploy a Contract on Ethereum Network
• Execute a Function on a Deployed Contract

ASIA BLOCKCHAIN CENTER


Fund Transfer
Between EOA

ASIA BLOCKCHAIN CENTER


Deploy a Contract on
Ethereum Network
In this part we will deploy a compiled contract (i.e. the bytecode) on an Ethereum network.
In Ethereum the deployment of contract is done through a transaction

The transaction looks like this

ASIA BLOCKCHAIN CENTER


Execute a Function on
a Deployed Contract
After a contract is deployed, an EOA can execute functions defined in this contract.
Again it is implemented by sending an Ethereum transaction.

The transaction looks like this

ASIA BLOCKCHAIN CENTER


Quá trình xử lý
Trainsaction trên Chain

ASIA BLOCKCHAIN CENTER


Quá trình xử lý
Trainsaction trên Chain

ASIA BLOCKCHAIN CENTER


Số Nonce
là gì ?

• In Ethereum, every transaction has a nonce. The nonce is the number of


transactions sent from a given address.
• In English, a nonce is a number that can only be used once. In cryptography, a
nonce is a one-time code selected in a random or pseudo-random manner

ASIA BLOCKCHAIN CENTER


that is used to securely transmit a main password, preventing replay attacks.
• Each time you send a transaction, the nonce value increases by 1. There are
rules about what transactions are considered valid transactions, and the
nonce is used to enforce some of these rules. Specifically:
Transactions must be in order. You cannot have a transaction with a nonce
of 1 mined before one with a nonce of 0.
No skipping! You cannot have a transaction with a nonce of 2 mined if you have
not already sent transactions with a nonce of 1 and 0.
Vai trò của
Nonce?

Why does it matter?


This value prevents double-spending, as the nonce will always specify the order of transactions. If a
double-spend does occur, it’s typically due to the following process:
• A transaction is sent to one party.
• They wait for it to register.
ASIA BLOCKCHAIN CENTER

• Something is collected from this first transaction.


• Another transaction is quickly sent with a high gas price.
• The second transaction is mined first, therefore invalidating the first transaction.
This is why exchanges wait for you to have a certain number of confirmations before allowing you to
trade freshly-deposited funds.
When using the Ethereum blockchain, the above is not possible.
In Ethereum, this method of “double-spending” is not possible because each transaction has a nonce
included with it. Even if you attempt to do the above, it will not work as the second transaction
(nonce of 3) cannot be mined before the first transaction (nonce of 2).
Vai trò của
Nonce?

ASIA BLOCKCHAIN CENTER


Phí Gas
& Mempool
Gas: phí trả cho xử lý giao dịch
(transaction)
Mempool: all transactions sent to the
Ethereum network land in the mempool.
This is a place where all pending
transactions are waiting for the miners to
pick them up and include them in the next
ASIA BLOCKCHAIN CENTER Ethereum block.
Cancel hoặc Speed up
giao dịch
• Thiết lập giá Gas cao để giao dịch được xử lý nhanh hơn
• Để Cancel 1 giao dịch đã gửi ?
-> Gửi lại transaction mới có cùng số Nonce với giao dịch cần hủy, với giá Gas cao hơn,
amount = 0
• Để tăng tốc 1 giao dịch đã gửi ?

ASIA BLOCKCHAIN CENTER


-> Gửi lại transaction mới có cùng số Nonce với giao dịch cần xử lý nhanh, với giá Gas cao hơn
Smart Contract
• SMCT là một chương trình máy tính chạy trên máy ảo Ethereum (EVM)
• Được viết bởi ngôn ngữ lập trình cấp cao như Solidity, sau đó được biên dịch và chạy trên EVM
• SMCT sau khi được deploy lên Ethereum, nó cũng có địa chỉ (address) như người dùng, gọi là
Contract address hay Contract account
• Contract address không có private key như User address (EOA – Externally Owned account)

ASIA BLOCKCHAIN CENTER


Ứng dụng của Smart Contract
Smart Contract là một ứng dụng tận dụng tất cả những điểm mạnh của công nghệ Blockchain mang
lại vì vậy nó có rất nhiều lợi ích, dưới đây là các lợi ích chính của nó.
• Tự động hóa: Quá trình được thực hiện hợp đồng là tự động bằng. Đồng thời bạn chính là người
tạo hợp đồng, không còn phải phụ thuộc vào môi giới, luật sư hay bất kì ai khác. Như vậy, nó cũng
xóa bỏ những nguy cơ đến từ bên thứ ba
• Không bị thất lạc: Tài liệu của bạn được mã hóa trên một cuốn sổ cái chung, có nghĩa là không
thể bị thất lạc. Với Blockchain, tất cả những người bạn đều có lưu trữ lại tài liệu của bạn.
• An toàn: Blockchain sẽ đảm bản sự an toàn cho tài liệu của bạn. Không một hacker nào có thể đe
dọa đến chúng.

ASIA BLOCKCHAIN CENTER


• Tốc độ: Hợp đồng thông minh sử dụng các ngôn ngữ lập trình, code phần mềm để tự động hóa
các điều khoản, tiết kiệm hàng tiếng đồng hồ cho những công việc không cần thiết.
• Tiết kiệm: Hợp đồng thông minh tiết kiệm cho bạn hàng đống tiền nhờ xóa bỏ khâu trung gian.
• Chính xác: Các hợp đồng tự động không chỉ nhanh và rẻ hơn mà còn tránh được các lỗi thường
thấy khi viết giấy tờ.
• https://trustweb.vn/hop-dong-thong-minh-la-gi-smart-contract-cach-thuc-hoat-dong-va-ung-
dung/
Code Smart Contract
như thế nào ?
• Code SMCT như code các chương trình hướng đối tượng (Object Oriented
Programming)
• Khai báo các kiểu dữ liệu cho đối tượng SMCT
• Khai báo và implement các hàm (method) cần có cho Class SMCT: hàm transfer,
transferfrom, approve, allowance, v.v
• Cần tìm hiểu các hàm Built-in để tương tác với các đối tượng khác trên blockchain
như Block, Gas price, Address, V.v

ASIA BLOCKCHAIN CENTER


Tạo & Deploy SMCT

ASIA BLOCKCHAIN CENTER


SMCT thực thi khi nào ?

Trường hợp 1: User send transaction, recipient là contract address thì SMCT sẽ đc chạy
• After a contract is deployed, an EOA can execute functions defined in this contract.
Again it is implemented by sending an Ethereum transaction.

ASIA BLOCKCHAIN CENTER


SMCT thực thi khi nào ?

Trường hợp 2: SMCT này gọi SMCT khác

ASIA BLOCKCHAIN CENTER


Token
• Token được sinh ra từ SMCT, khi SMCT được deploy lên blockchain
• Token được sử dụng như tiền số (Currency)
• Token khác với Coin: Coin là đồng tiền của nền tảng blockchain (ví dụ Ethereum thì có
đồng coin ETH là đồng tiền mã hóa của nền tảng này), mỗi blockchain chỉ có duy nhất 1
đồng Coin. Các đồng khác sinh ra từ SMCT đều gọi là Token

Có hai loại Token


Fungible Token: token không có thuộc tính định danh, token nào cũng như token nào

ASIA BLOCKCHAIN CENTER


Non-Fungible Token (NFT): token có thuộc tính định danh, mỗi Token sẽ định danh duy nhất
cho Token đó. NFT được ứng dụng vào Game, các tác phẩm nghệ thuật, vào quyền sở hữu trí
tuệ, v.v (Nơi nào cần gắn token để định danh 1 đối tượng là duy nhất thì dùng NFT)

• ERC20 (Ethereum Request for Comment) là tên gọi của một bộ các tiêu chuẩn mà
những token được phát triển trên nền tảng blockchain của Ethereum phải tuân thủ theo
Tiêu chuẩn
ERC-20
3 quy tắc tùy chọn:
• Token Name: tên của token, ví dụ Marker
• Symbol: ký hiệu của token hay mã token, Maker có mã là MKR
• Decimals: Số thập phân (lên đến 18), quy định về số lượng chữ số thập phân trong đơn vị của
token. Decimals của MKR là 18, nghĩa là đơn vị nhỏ nhất của MKR là
0.000000000000000001 MKR.

ASIA BLOCKCHAIN CENTER


Tiêu chuẩn ERC-20
6 quy tắc bắt buộc
• TotalSupply: tổng nguồn cung, tổng số lượng token có thể được tạo ra, totalsupply của MKR
là 1,005,557 MKR
• BalanceOf: số dư token mà một tài khoản hay một ví đang có
• Transfer: chuyển về tài khoản, chuyển token từ ví của bạn sang ví của người dùng khác bằng
cách cung cấp địa chỉ ví của người nhận và số token muốn chuyển
• TransferFrom: chuyển từ tài khoản này sang tài khoản khác, quy tắc này cũng khá tương tự

ASIA BLOCKCHAIN CENTER


như transfer nhưng tiện dụng hơn, là bạn có thể ủy quyền cho ai đó chuyển token thay cho
bạn
• Approve: đối chiếu giao dịch, giới hạn số lượng token được rút ra từ ví của bạn, quy tắc này
giúp bạn tránh được các rủi ro do lỗi hợp đồng và trường hợp bị đánh cắp tất cả token trong
ví.
• Allowance: cho phép kiểm tra số dư của người dùng. Trong trường hợp bạn cấp quyền cho
một địa chỉ ví nào đó quản lý số token của bạn thì khi sử dụng hàm allowance, bạn sẽ kiểm
tra được số dư có thể rút và số dư còn lại đó sẽ được hoàn lại vào ví của bạn.
Ví & Token
Một Ví Ethereum có thể trữ đồng ETH, và trữ All các token được phát hành trên mạng Ethereum

ASIA BLOCKCHAIN CENTER


Token & SMCT &
Transaction & Wallet
Figure 1. The two-step approve & transferFrom workflow of ERC20 tokens

For the approve & transferFrom workflow, two transactions are needed. Let’s say that
Alice wants to allow the AliceICO contract to sell 50% of all the AliceCoin tokens to
buyers like Bob and Charlie. First, Alice launches the AliceCoin ERC20 contract, issuing
all the AliceCoin to her own address. Then, Alice launches the AliceICO contract that can
sell tokens for ether. Next, Alice initiates the approve & transferFrom workflow. She
sends a transaction to the AliceCoin contract, calling approve with the address of the
AliceICO contract and 50% of the totalSupply as arguments. This will trigger the
Approval event. Now, the AliceICO contract can sell AliceCoin.

ASIA BLOCKCHAIN CENTER


Token & SMCT &
Transaction & Wallet
When the AliceICO contract receives ether from Bob, it needs to send some AliceCoin to Bob in return. Within
the AliceICO contract is an exchange rate between AliceCoin and ether. The exchange rate that Alice set when
she created the AliceICO contract determines how many tokens Bob will receive for the amount of ether sent
to the AliceICO contract. When the AliceICO contract calls the AliceCoin transferFrom function, it sets Alice’s
address as the sender and Bob’s address as the recipient, and uses the exchange rate to determine how many
AliceCoin tokens will be transferred to Bob in the value field. The AliceCoin contract transfers the balance from
Alice’s address to Bob’s address and triggers a Transfer event. The AliceICO contract can call transferFrom an
unlimited number of times, as long as it doesn’t exceed the approval limit Alice set. The AliceICO contract can
keep track of how many AliceCoin tokens it can sell by calling the allowance function.

ASIA BLOCKCHAIN CENTER


Ethereum Client
An Ethereum client is a software application that implements the Ethereum specification and communicates over
the peer-to-peer network with other Ethereum clients
Currently, there are six main implementations of the Ethereum protocol, written in six different languages:
• Parity, written in Rust
• Geth, written in Go
• cpp-ethereum, written in C++
• pyethereum, written in Python
• Mantis, written in Scala
• Harmony, written in Java
Client full node
Ở đây chỉ đề cập hai phiên bản client full node phổ biến là Geth & Parity
1 Client full node khi đã Sync đầy đủ, sẽ có toàn bộ cơ sở dữ liệu blockchain như các khối, transaction, smart
contract code & states, V.v
Client cung cấp 1 giao diện JSON-RPC API, cho phép chúng ta viết các chương trình ứng dụng (Dapp) sử dụng
Client như là một cổng kết nối (Gateway) tới mạng Ethereum
Có EVM
ASIA BLOCKCHAIN CENTER
Ethereum Client
Remote Client
Phần mềm Metamask được xem như là Remote client. Nó không lưu toàn bộ data, states của blockchain
Nó có 1 tập con các chức năng của 1 Full node, chẳng hạn như:
• Manage private keys and Ethereum addresses in a wallet.
• Create, sign, and broadcast transactions.
• Interact with smart contracts, using the data payload.
• Browse and interact with DApps.

ASIA BLOCKCHAIN CENTER


• Offer links to external services such as block explorers.
• Convert ether units and retrieve exchange rates from external sources.
• Inject a web3 instance into the web browser as a JavaScript object.
• Use a web3 instance provided/injected into the browser by another client.
• Access RPC services on a local or remote Ethereum node.
Ethereum Client

ASIA BLOCKCHAIN CENTER


Dapp
Định nghĩa
Là ứng dụng phi tập trung
Là mô hình ứng dụng được sinh ra trên nền tảng blockchain &
smart contract
Những nhóm ứng dụng của Dapp
• Sàn giao dịch • Tài chính
• Ví • Mạng xã hội
• Trò chơi • Khác
• Các ứng dụng cá cược
Tham khảo tất cả các Dapp trên thị trường: https://dapp.com

ASIA BLOCKCHAIN CENTER


Dapp
Một số key word khác liên quan
tới công nghệ Dapp
IPFS: hệ thống lưu trữ nội dung P2P
Swarm: hệ thống lưu trữ nội dung
P2P
Whisper: hệ thống nhắn tin P2P

ASIA BLOCKCHAIN CENTER


Lập trình Dapp như thế nào ?

ASIA BLOCKCHAIN CENTER


Cần chuẩn bị gì để code Dapp?
Một số key word khác liên quan tới công nghệ Dapp
Solidity: 1 ngôn ngữ lập trình hướng contract, dùng để viết smartcontract.
NodeJS.
Geth (go-ethereum).
Truffle framework: dễ dàng phát triển và deploy contract.
web3 JS API.

ASIA BLOCKCHAIN CENTER


MetaMask/MIST.
Html.
Css.
Javascript frameworks để xây dựng frontend.
Cần chuẩn bị gì để code Dapp?
Data storage: Do chi phí cho việc lưu trữ trên smart contract khá là tốn kém, nên ta không
thể lưu trữ toàn bộ dữ liệu trên smart contract, do đó hầu hết các DApp sẽ sử dụng các kho
lưu trữ phân tán như IPFS hoặc Swarm để lưu trữ images, video ...

Ngoài ra thì có 1 số tool static analysis và chạy coverity như oyente (static analysis),
solidity coverage (coverity), cái này thì các dự án lớn sẽ cần, làm demo chơi chơi thì cũng
chưa cần tìm hiểu làm gì.

ASIA BLOCKCHAIN CENTER


Cuối cùng, muốn trở nên master thì bạn phải có khả năng đọc hiểu mã nguồn geth (viết
bằng golang). Vậy nên nếu bạn biết golang thì bạn sẽ lợi thế cực lớn trong việc hiểu sâu và
debug sau này. Ngoài ra, nếu bạn biết C++ thì sẽ cũng là 1 lợi thế nữa vì trình biên dịch
solidity được viết bằng C++ mà các phiên bản của solidity thì thay đổi liên tục.
Thực hành code Dapp cơ bản

Quản lý thú nuôi:

https://viblo.asia/p/xay-dung-ung-dung-dapps-dau-tien-cua-ban-oOVlYn0r58W

ASIA BLOCKCHAIN CENTER


Explorer
Công cụ để truy vấn thông tin trên blockchain, các kiểu dữ liệu truy vấn gồm: block,
transaction, smart contract code & states, Token

ASIA BLOCKCHAIN CENTER


ASIA BLOCKCHAIN CENTER
Web3 & Oracles
Dự án cần làm

Dự án 1: Dự án 2:

Chò trơi mua vé số Dự đoán giá BTC, ETH, BNB trong chu kỳ Nphut
Tham khảo ứng dụng thực tiễn của Tham khảo ứng dụng thực tiển của Pancakeswap
Pancakeswap https://pancakeswap.finance/prediction
https://pancakeswap.finance/lottery Tham khảo logic SMCT của ứng dụng này (xem
Tham khảo logic SMCT của ứng dụng này (xem source code SMCT trên explorer của Binance)
source code SMCT trên explorer của Binance) Copy/Customize lại SMCT cho bài tập này
Copy/Customize lại SMCT cho bài tập này Phân tích và vẽ workflow cho Frontend Dapp
Phân tích và vẽ workflow cho Frontend Dapp Tiến tới code Dapp
Tiến tới code Dapp
ASIA BLOCKCHAIN CENTER
THANKS
Does anyone have any questions?
Follow the project updates

You might also like