eos智能合約基礎知識

1. 智能合約以action和訪問共享內存數據庫的形式互相通訊;倆種通訊模式:Inline.被保證在當前的transaction或unwind中執行;結果不管成功或失敗,都不會通知任何操做;Inline操做與original transaction具備相同的範圍和權限。Deferred. Defer將被BP節點安排在以後執行,有可能會通知通訊的結果或者超時。Deferred能夠帶着調用者的受權延伸到不一樣的scopes。數據庫

2. Action表示單個操做,而transaction是一個或多個action的集合。Action是合約和帳戶之間進行通訊的方式。Action能夠單獨執行,或者組合組合起來做爲一個總體執行;包含多個action的transaction, 這些action要麼所有成功要麼所有失敗markdown

3. Action名字約束網絡

Action的類型是 base32被編碼爲64-bit整數. 這意味着它的字符集長度是12,而且只能包含a-z,1-5,和'.'。 若是長度超過12個,他會自動截取前12個符合規則的字符做爲action的名字;app

Transaction 確認 框架

收到一個transaction並不意味着這個transaction已經被確認,它僅僅說明這個transaction被一個BP節點接受而且沒有錯誤,固然也意味着頗有可能這個transaction被其餘bp接受了。函數

當一個transaction被包含在一個block當中的時候,它纔是能夠被確認執行的。區塊鏈

4. ui

eosiocpp也能夠建立3個合約文件,它們僅僅包含了合約的框架。this

$ eosiocpp -n ${contract}

上面的命令會在./${project}目錄下建立一個空的項目,它包含3個文件編碼

${contract}.abi ${contract}.hpp ${contract}.cpp

hpp

${contract}.hpp 這是合約的頭文件,能夠包含一些變量,常量和函數的聲明。

cpp

The ${contract}.cpp 這是合約的源碼文件,包含合約的具體實現。

5.void init() { eosio::print( "Init World!\n" ); // Replace with actual code } init 僅當合約第一次被部署的時候執行。 在這個函數裏能夠初始化變量,

void apply( uint64_t code, uint64_t action ) { eosio::print( "Hello World: ", eosio::name(code), "->", eosio::name(action), "\n" ); }

apply 是一箇中轉函數, 他監聽全部傳入的action,而且根據action調用合約相應的函數。apply函數須要兩個參數, code 和 action

code filter

這個參數是爲了對action作出迴應,好比下面的apply函數,你能夠構造一個通用響應去忽略code

if (code == N(${contract_name}) { // your handler to respond to particular action }

action filter

爲了響應每個action,好比構造好比下面的apply函數。一般和code filter一塊兒使用

if (action == N(${action_name}) {
    //your handler to respond to a particular action
}

hpp

${contract}.hpp 這是合約的頭文件,能夠包含一些變量,常量和函數的聲明。

cpp

The ${contract}.cpp 這是合約的源碼文件,包含合約的具體實現。

若是你用eosiocpp生成了一個 .cpp, 那它的內容大概相似以下:

#include <${contract}.hpp>

extern "C" {

    /**
     *  This method is called once when the contract is published or updated.
     */
    void init()  {
       eosio::print( "Init World!\n" ); // Replace with actual code
    }

    /// The apply method implements the dispatch of actions to this contract
    void apply( uint64_t code, uint64_t action ) {
       eosio::print( "Hello World: ", eosio::name(code), "->", eosio::name(action), "\n" );
    }

} // extern "C"

在這個例子裏,咱們能夠看到兩個函數,initapply。它們會打印log而且不作任何檢查。任何人均可以在任什麼時候刻執行BP容許的全部action。在不須要任何簽名的狀況下,合約將被計入帶寬消耗。(Absent any required signatures, the contract will be billed for the bandwidth consumed.)

init

init 僅當合約第一次被部署的時候執行。 在這個函數裏能夠初始化變量, 好比,在currency合約中整體的token的供應量。

apply

apply 是一箇中轉函數, 他監聽全部傳入的action,而且根據action調用合約相應的函數。apply函數須要兩個參數, code 和 action

code filter

這個參數是爲了對action作出迴應,好比下面的apply函數,你能夠構造一個通用響應去忽略code。 (In order to respond to a particular action, structure the apply function as follows. You may also construct a response to general actions by omitting the code filter.)

if (code == N(${contract_name}) {
    // your handler to respond to particular action
}

固然你也能夠爲每一個action構造各自的一個響應。

action filter

爲了響應每個action,好比構造好比下面的apply函數。一般和code filter一塊兒使用

if (action == N(${action_name}) {
    //your handler to respond to a particular action
}

wast

任何合約程序想要部署到EOSIO的區塊鏈網絡中都必須編譯成WASM格式。這是EOS的支持惟一個的格式。

一旦你的CPP文件寫好了,有就能夠用eosiocpp把它編譯成WASM (.wast)文件了



$ eosiocpp -o ${contract}.wast ${contract}.cpp

abi

ABI( Application Binary Interface)文件是一個JSON格式的描述文件,說明了如何在他們的JSON和二進制之間轉化用戶的action。ABI文件也同時說明了如何轉換數據庫的狀態。一旦你用了ABI描述了你的合約,開發人員就和用戶就能夠和你的合約經過JSON進行交互。

ABI能夠經過.hpp文件用eosiocpp生成。

$ eosiocpp -g ${contract}.abi ${contract}.hpp

Print C++ API 支持

  • a null terminated char array (string)
  • integer (128-bit unsigned, 64-bit unsigned, 32-bit unsigned, signed, unsigned)
  • base32 string encoded as 64-bit unsigned integer
  • struct that has print() method

5.

相關文章
相關標籤/搜索