這片博文主要是繼續(xù)的簡要的介紹一下The DAO事件,以及ETH和ETC的關(guān)系。
- 3分鐘漫談以太坊The DAO事件,淺入淺出區(qū)塊鏈(4)>> 你在這里
- 3分鐘漫談以太坊The DAO事件,淺入淺出區(qū)塊鏈(3)
- 3分鐘漫談以太坊The DAO事件,淺入淺出區(qū)塊鏈(2)
- 3分鐘漫談以太坊The DAO事件,淺入淺出區(qū)塊鏈(1)
這是一系列關(guān)于區(qū)塊鏈的漫談博客。用講故事的方法,漫聊一下區(qū)塊鏈。其中也會穿插一些區(qū)塊鏈相關(guān)概念,以及編程的介紹。主要目的是漫談,每個主題之間相對獨立,在閱讀的時候,可以選擇你有興趣的部分閱讀。
雖然目的是漫談,但準確的信息是博文的基本,如果有不準確的地方,請留言或者發(fā)消息,我會及時訂正。

復(fù)習(xí)一下DAO
DAO是Decentralized Autonomous Organization的簡稱。
可以理解為:去中心化自治組織。任何符合以下特征的組織,都可以被成為DAO組織。
DAO組織的操作過程:發(fā)布智能合約,發(fā)行代幣(ICO),眾籌資金,運營和投資眾籌的資金。這整個過程也被成為DAO項目。
復(fù)習(xí)一下The DAO事件
德國一家專注“智能鎖”的公司發(fā)布了一個DAO項目,黑客利用一個DAO項目的漏洞,轉(zhuǎn)移了一筆巨款,然后以太坊的白帽黑客們,通過軟分叉,和硬分叉的方法拿回了被盜的以太幣。

復(fù)習(xí)一下msg.sender 等全局變量
在學(xué)習(xí)以太坊DApp編程的時候,我們經(jīng)常會看到 msg.sender 等的使用,但是我們沒有定義這些變量,這些變量是什么意思呢?
msg.sender 等這樣的變量或者函數(shù),是以太坊區(qū)塊鏈默認提供的,其中包含了在處理請求的時候,請求方的相關(guān)信息。
- msg.sender : 返回請求發(fā)送方的地址。
- msg.data : 包含了所有請求發(fā)送方的信息。這個數(shù)據(jù)是不可變的,也不會永久性的存儲到區(qū)塊鏈里面。
- msg.gas : 請求發(fā)送方剩余的gas數(shù)量。
- msg.sig : 發(fā)送方需要的處理函數(shù)的地址的前4個bytes。(智能合約可以提供很多種功能,很多時候我們只需要使用其中的一個功能的時候,我們可以指定智能合約分配個每個功能的ID:opcodes來實現(xiàn)。)
- msg.value : 我們發(fā)送給這個智能合約的Ether,以wei為單位??梢愿綆Оl(fā)送信息。(wei是描述Ether的最小單位,就好像一分錢是人民幣最小的單位一樣)
全部的函數(shù)和變量可以在下方官網(wǎng)查看:
transfer vs send vs call
如果我們想給最可愛的五月天發(fā)送一筆500wei的ether,讓他們可以“私奔到月球”,我們可以通過這三個方法實現(xiàn):
- msg.sender.transfer(500)
- msg.sender.send(number)
- msg.sender.call.value(500)()
這里的 msg.sender 將返回五月天“私奔到月球”智能合約的地址
那這三種方式的發(fā)送又有什么不同呢?
address-related相關(guān)的函數(shù)
<address payable>.transfer(uint256 amount):
send given amount of Wei to Address, reverts on failure, forwards 2300 gas stipend, not adjustable
發(fā)送Wei到某個地址,如果失敗,發(fā)出例外,消耗固定的2300gas。<address payable>.send(uint256 amount) returns (bool):
send given amount of Wei to Address, returns false on failure, forwards 2300 gas stipend, not adjustable
發(fā)送Wei到某個地址,如果失敗,返回false,消耗固定的2300gas。<address>.call(bytes memory) returns (bool, bytes memory):
issue low-level CALL with the given payload, returns success condition and return data, forwards all available gas, adjustable
發(fā)送Wei到某個地址,如果失敗,返回false,可以接受可變的gas,用于執(zhí)行其他的函數(shù)。
privat key vs address 密匙和地址的關(guān)系
在上面我們漫談了3個關(guān)于地址操作的函數(shù),我們順便介紹一下地址和私鑰的關(guān)系。
There are three main steps to get from private -> address:
- Create a random private key (64 (hex) characters / 256 bits / 32 bytes)
- Derive the public key from this private key (128 (hex) characters / 512 bits / 64 bytes).
- Derive the address from this public key. (40 (hex) characters / 160 bits / 20 bytes) Take the Keccak-256 hash of the public key.
從產(chǎn)生密匙到生成公共地址,需要這個三個步驟。(通過密匙,可以操作賬號哦,要絕對保密,地址則是公開的)
- 用算法得到一個隨機數(shù)(64位16進制),作為密匙
- 從密匙派生出公匙(128位16進制)
- 從公匙派生出地址(40位16進制)
Account Types 賬號類型
在以太坊上,有兩種賬號類型:
- Externally owned accounts (EOAs) 可以認為是個人賬號
- Contract accounts 智能合約賬號
兩種賬號的區(qū)別:
Externally owned accounts (EOAs)
- has an ether balance 可以保存ether
- can send transactions (ether transfer or trigger contract code), 可以交易ether
- is controlled by private keys, 通過私鑰來控制
- has no associated code. 沒有處理請求的代碼
Contract accounts
- has an ether balance, 可以保存ether
- has associated code, 有處理請求的代碼
- code execution is triggered by transactions or messages (calls) received from other contracts. 其他智能合約可以通過transactions or messages (calls)來執(zhí)行代碼
- when executed - perform operations of arbitrary complexity (Turing completeness) - manipulate its own persistent storage 代碼相對復(fù)雜,可以操作所屬的區(qū)塊鏈
- can call other contracts 可以向其他智能合約發(fā)出請求
圖片來源
圖片來自網(wǎng)絡(luò)