EOS合約虛擬機架構(gòu)
EOS操作系統(tǒng)軟件的目的是可以支持多種虛擬機,同時可以按需求增加新的虛擬機涝滴。
以太虛擬機(EVM)
這個虛擬機已經(jīng)被用于大多數(shù)現(xiàn)有的智能合約吧黄,并且可以在EOS系統(tǒng)區(qū)塊鏈上使用肺魁√泶桑可以想象,在EOS操作系統(tǒng)區(qū)塊鏈上痢士,EVM合約可以在內(nèi)部沙箱中運行彪薛,只需要少量適配就可以與其他EOS應(yīng)用程序交互。
Wren
Wren(http://wren.io)是一種小型的怠蹂、快速的善延、基于類別的編程語言。短小精悍城侧、易于文檔記錄和理解的代碼庫易遣。具有非常好的性能,并且可以很容易地嵌入C++應(yīng)用程序中嫌佑。
Web Assembly(WASM)
WASM是構(gòu)建高性能Web應(yīng)用程序的新興Web標(biāo)準(zhǔn)豆茫,通過少量適配就可以被明確定義和沙箱化。WASM的好處在于業(yè)界廣泛支持屋摇,因此可以用熟悉的語言開發(fā)開發(fā)智能合約揩魂,例如C或C++/Rust。
以太發(fā)人員已經(jīng)開始適配WASM炮温,以提供適當(dāng)?shù)纳诚洳⑹褂靡蕴籛ASM定義(https://github.com/ewasm/design)火脉。這種方法很容易改編后用于EOS系統(tǒng)軟件集成。
性能原因
開始使用WREN, 1000 TPS, 達不到EOS的性能要求柒啤。WASM的測試達到 50000 TPS倦挂。實際主網(wǎng)運行,3996 TPS白修,一般100左右妒峦。https://eosnetworkmonitor.io/開發(fā)友好多語言兼容
當(dāng)前主要使用cpp工具集重斑。
https://steemit.com/eos/@dantheman/web-assembly-on-eos-50-000-transfers-per-second
EOS的賬戶設(shè)計
比特幣/以太坊使用私鑰和地址概念的賬戶系統(tǒng)兵睛。
EOS使用一個可讀的12個字符來創(chuàng)建賬戶。
? eos-contracts kcleos get account hellomykey11
created: 2018-11-16T06:54:10.500
permissions:
owner 1: 1 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
active 1: 1 EOS58idArYhygKSaT6i9q4PjXoaSYC5bv93NEfWh5qipASzpAMxyG, 1 hellomykey11@eosio.code
memory:
quota: 539.8 KiB used: 521.8 KiB
net bandwidth:
delegated: 1.0000 EOS (total staked delegated to account from others)
used: 268.2 KiB
available: 437.9 KiB
limit: 706 KiB
cpu bandwidth:
delegated: 1.0000 EOS (total staked delegated to account from others)
used: 54.73 ms
available: 78.12 ms
limit: 132.8 ms
EOS balances:
liquid: 307.9120 EOS
staked: 0.0000 EOS
unstaking: 0.0000 EOS
total: 307.9120 EOS
賬戶的消息處理
- 每個賬戶都可以發(fā)送結(jié)構(gòu)化消息到其他賬戶
- 賬戶可以定義消息被接收后的處理腳本(合約)
- 每個賬戶有自己獨有的數(shù)據(jù)庫
- 賬戶的消息處理程序可以向其他賬戶發(fā)送消息
消息和自動的消息處理程序的組合是EOS定義智能合約的方式
mykey創(chuàng)建賬戶:
https://eosq.app/tx/f99f8057290488bd6c11b9fdab70197a8a50efcccada62acc6b0b18a4988a1bb
ETH EOS 合約升級設(shè)計差異
"code is law" vs "intent of code is law"
李嘉圖合約,社區(qū)治理
https://medium.com/@bytemaster/the-intent-of-code-is-law-c0e0cd318032
EOS合約開發(fā)
- Action handler and Action Apply Context
Smart contracts provide action handlers to do the work of requested actions. Each time an action runs, i.e., the action is "applied" by running the apply method in the contract implementation, EOSIO creates a new action "apply" context within which the action runs. The diagram below illustrates key elements of the action "apply" context.
helloworld合約
#include <eosiolib/eosio.hpp>
class hello : public eosio::contract {
public:
using contract::contract;
/// @abi action
void hi( account_name user ) {
print( "Hello, ", name{user} );
}
};
extern "C" {
void apply( uint64_t receiver, uint64_t code, uint64_t action ) {
auto self = receiver;
if( action == N(onerror)) {
/* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */
eosio_assert(code == N(eosio), "onerror action's are only valid from the \"eosio\" system account");
}
if( code == self || action == N(onerror) ) {
hello thiscontract( self );
switch( action ) {
EOSIO_API( hello, (hi) )
}
}
}
}
- 合約數(shù)據(jù)持久化存儲 Multi-Index API
EOS中引入了Multi-Index多索引容器祖很,Multi-Index基于boost中的Multi-Index開發(fā)笛丙。Multi-Index API 提供了EOSIO數(shù)據(jù)庫的C++接口瓤介,它可以存儲任意數(shù)據(jù)類型能颁。通過API能夠讀取和修改EOSIO數(shù)據(jù)庫中的持久狀態(tài)。
eosio::multi_index
在概念上被視為傳統(tǒng)數(shù)據(jù)庫中的表格鳖目,其中行是容器中的單個對象笨鸡,列是容器中對象的成員屬性姜钳,并且索引通過與一個鍵兼容的鍵提供對對象的快速查找對象成員屬性。
https://github.com/EOSIO/eos/blob/v1.6.0/contracts/eosiolib/multi_index.hpp
https://www.boost.org/doc/libs/1_66_0/libs/multi_index/doc/index.html
class token : public contract {
...
struct account {
asset balance;
uint64_t primary_key()const { return balance.symbol.name(); }
};
struct currency_stats {
asset supply;
asset max_supply;
account_name issuer;
uint64_t primary_key()const { return supply.symbol.name(); }
};
typedef eosio::multi_index<N(accounts), account> accounts;
typedef eosio::multi_index<N(stat), currency_stats> stats;
...
};
- EOSIO合約基礎(chǔ)庫eosiolib
智能合約開發(fā)最重要的eosiolib庫形耗,現(xiàn)在已經(jīng)整合到eosio.cdt項目中了哥桥。
https://github.com/EOSIO/eosio.cdt
eosiolib庫,位于eosio.cdt/libraries/eosiolib目錄激涤,共有40個C/C++文件拟糕,下面這張圖描述了這些文件之間的引用關(guān)系:
- db.h
- action.h
- crypto.h
- system.h
- wasm_interface.cpp
https://github.com/EOSIO/eos/blob/v1.7.1/libraries/chain/wasm_interface.cpp
- 開發(fā)環(huán)境
vscode + cpp plugin
EOS Studio, https://www.eosstudio.io/
開發(fā)庫不成熟,對比以太openzeppelin倦踢。
開發(fā)測試組件缺乏送滞,合約測試麻煩。缺乏想truffle一樣的輕量級的開發(fā)環(huán)境辱挥。(mykey的開發(fā)主要是js/shell腳本在kylin測試網(wǎng)測試犁嗅。本地的測試使用docker。)
基礎(chǔ)工具待完善晤碘,交易的資源費用預(yù)估等愧哟。
https://github.com/OpenZeppelin/openzeppelin-solidity
https://hub.docker.com/r/eosio/eos-dev