Move是一種新的編程語言,旨在爲Libra Blockchain提供安全可編程的基礎。Libra Blockchain中的賬戶是任意數量的Move資源和Move模塊的容器。提交給Libra Blockchain的每一個事務都使用Move中編寫的事務腳原本編碼其邏輯。事務腳本能夠調用模塊聲明的過程來更新區塊鏈的全局狀態。css
在本指南的第一部分中,咱們將對Move語言的主要功能進行高級介紹:html
對於好奇的讀者,Move技術論文包含有關該語言的更多細節。git
在本指南的第二部分中,咱們將「深刻了解」並向您展現如何在Move中間表示中編寫您本身的Move程序。初始testnet版本不支持自定義移動程序,但這些功能可供您在本地試用。程序員
移動模塊定義了更新Libra Blockchain全局狀態的規則。模塊在其餘區塊鏈系統中與智能合約相同。模塊聲明能夠在用戶賬戶下發布的資源類型。Libra Blockchain中的每一個賬戶都是一個容器,能夠容納任意數量的資源和模塊。編程
LibraCoin.T
。LibraCoin.T
在該語言中沒有特殊的地位; 每一個Move資源都享有相同的保護。本節介紹如何在Move中間表示(IR)中編寫事務腳本和模塊。咱們提醒讀者,IR是即將推出的Move源語言的早期(且不穩定)前體(有關更多詳細信息,請參閱Future Developer Experience)。移動IR是移動字節碼上的一個薄語法層,用於測試字節碼驗證器和虛擬機,它不是特別適合開發人員。它的高級別足以編寫人類可讀的代碼,但低級別足以直接編譯爲移動字節碼。儘管如此,咱們對Move語言感到很興奮,並但願開發人員儘量地嘗試IR,儘管邊緣粗糙。安全
咱們將繼續介紹重要評論的Move IR的片斷。咱們鼓勵讀者經過在本地編譯,運行和修改示例來跟隨這些示例。README文件libra/language/README.md
並libra/language/compiler/README.md
解釋如何執行此操做。微信
正如咱們在Move Transaction Scripts Enable Programmable Transactions中所述,用戶編寫事務腳本以請求更新Libra Blockchain的全局存儲。幾乎全部事務腳本中都會出現兩個重要的構建塊:資源類型LibraAccount.T
和LibraCoin.T
資源類型。LibraAccount
是模塊T
的名稱,是該模塊聲明的資源的名稱。這是Move中常見的命名約定; 模塊聲明的「main」類型一般是命名的T
。數據結構
When we say that a user "has an account at address 0xff
on the Libra Blockchain", what we mean is that the address 0xff
holds an instance of the LibraAccount.T
resource. Every nonempty address has a LibraAccount.T
resource. This resource stores account data, such as the sequence number, authentication key, and balance. Any part of the Libra system that wants to interact with an account must do so by reading data from the LibraAccount.T
resource or invoking procedures of the LibraAccount
module.app
賬戶餘額是類型的資源LibraCoin.T
。正如咱們在Move Has First Class Resources中所解釋的那樣,這是天秤座硬幣的類型。與任何其餘Move資源同樣,此類型是語言中的「一等公民」。類型的資源LibraCoin.T
能夠存儲在程序變量中,在程序之間傳遞,等等。less
咱們鼓勵感興趣的讀者檢查目錄下LibraAccount
和LibraCoin
模塊中這兩個關鍵資源的Move IR定義libra/language/stdlib/modules/
。
如今讓咱們看看程序員如何在事務腳本中與這些模塊和資源進行交互。
// Simple peer-peer payment example. // Use LibraAccount module published on the blockchain at account address // 0x0...0 (with 64 zeroes). 0x0 is shorthand that the IR pads out to // 256 bits (64 digits) by adding leading zeroes. import 0x0.LibraAccount; import 0x0.LibraCoin; main(payee: address, amount: u64) { // The bytecode (and consequently, the IR) has typed locals. The scope of // each local is the entire procedure. All local variable declarations must // be at the beginning of the procedure. Declaration and initialization of // variables are separate operations, but the bytecode verifier will prevent // any attempt to use an uninitialized variable. let coin: R#LibraCoin.T; // The R# part of the type above is one of two *kind annotation* R# and V# // (shorthand for "Resource" and "unrestricted Value"). These annotations // must match the kind of the type declaration (e.g., does the LibraCoin // module declare `resource T` or `struct T`?). // Acquire a LibraCoin.T resource with value `amount` from the sender's // account. This will fail if the sender's balance is less than `amount`. coin = LibraAccount.withdraw_from_sender(move(amount)); // Move the LibraCoin.T resource into the account of `payee`. If there is no // account at the address `payee`, this step will fail LibraAccount.deposit(move(payee), move(coin)); // Every procedure must end in a `return`. The IR compiler is very literal: // it directly translates the source it is given. It will not do fancy // things like inserting missing `return`s. return; }
此事務腳本有一個不幸的問題 - 若是地址下沒有賬戶,它將失敗payee
。咱們將經過修改腳原本修復此問題,以便爲payee
尚不存在的賬戶建立賬戶。
// A small variant of the peer-peer payment example that creates a fresh // account if one does not already exist. import 0x0.LibraAccount; import 0x0.LibraCoin; main(payee: address, amount: u64) { let coin: R#LibraCoin.T; let account_exists: bool; // Acquire a LibraCoin.T resource with value `amount` from the sender's // account. This will fail if the sender's balance is less than `amount`. coin = LibraAccount.withdraw_from_sender(move(amount)); account_exists = LibraAccount.exists(copy(payee)); if (!move(account_exists)) { // Creates a fresh account at the address `payee` by publishing a // LibraAccount.T resource under this address. If theres is already a // LibraAccount.T resource under the address, this will fail. create_account(copy(payee)); } LibraAccount.deposit(move(payee), move(coin)); return; }
讓咱們看一個更復雜的例子。在此示例中,咱們將使用事務腳原本支付多個收件人而不僅是一個。
// Multiple payee example. This is written in a slightly verbose way to // emphasize the ability to split a `LibraCoin.T` resource. The more concise // way would be to use multiple calls to `LibraAccount.withdraw_from_sender`. import 0x0.LibraAccount; import 0x0.LibraCoin; main(payee1: address, amount1: u64, payee2: address, amount2: u64) { let coin1: R#LibraCoin.T; let coin2: R#LibraCoin.T; let total: u64; total = move(amount1) + copy(amount2); coin1 = LibraAccount.withdraw_from_sender(move(total)); // This mutates `coin1`, which now has value `amount1`. // `coin2` has value `amount2`. coin2 = LibraCoin.withdraw(&mut coin1, move(amount2)); // Perform the payments LibraAccount.deposit(move(payee1), move(coin1)); LibraAccount.deposit(move(payee2), move(coin2)); return; }
這就結束了咱們對事務腳本的「瀏覽」。有關更多示例,包括初始testnet中支持的事務腳本,請參閱libra/language/stdlib/transaction_scripts
。
咱們如今將注意力轉向編寫本身的Move模塊,而不是僅重用現有模塊LibraAccount
和LibraCoin
模塊。考慮這種狀況:Bob將在將來的某個時刻在地址a建立一個賬戶。Alice但願爲Bob「收取」一些資金,以便他們能夠在建立後將其提取到他的賬戶中。但若是鮑勃從未建立賬戶,她也但願可以爲本身收回資金。
爲了解決Alice的這個問題,咱們將編寫一個模塊EarmarkedLibraCoin
:
EarmarkedLibraCoin.T
,用於包裝Libra硬幣和收件人地址。create
程序)。claim_for_recipient
過程)。EarmarkedLibraCoin.T
摧毀它並得到底層硬幣(unwrap
程序)。// A module for earmarking a coin for a specific recipient module EarmarkedLibraCoin { import 0x0.LibraCoin; // A wrapper containing a Libra coin and the address of the recipient the // coin is earmarked for. resource T { coin: R#LibraCoin.T, recipient: address } // Create a new earmarked coin with the given `recipient`. // Publish the coin under the transaction sender's account address. public create(coin: R#LibraCoin.T, recipient: address) { let t: R#Self.T; // Construct or "pack" a new resource of type T. Only procedures of the // `EarmarkedCoin` module can create an `EarmarkedCoin.T`. t = T { coin: move(coin), recipient: move(recipient), }; // Publish the earmarked coin under the transaction sender's account // address. Each account can contain at most one resource of a given type; // this call will fail if the sender already has a resource of this type. move_to_sender<T>(move(t)); return; } // Allow the transaction sender to claim a coin that was earmarked for her. public claim_for_recipient(earmarked_coin_address: address): R#Self.T { let t: R#Self.T; let t_ref: &R#Self.T; let sender: address; // Remove the earmarked coin resource published under `earmarked_coin_address`. // If there is resource of type T published under the address, this will fail. t = move_from<T>(move(earmarked_coin_address)); t_ref = &t; // This is a builtin that returns the address of the transaction sender. sender = get_txn_sender(); // Ensure that the transaction sender is the recipient. If this assertion // fails, the transaction will fail and none of its effects (e.g., // removing the earmarked coin) will be committed. 99 is an error code // that will be emitted in the transaction output if the assertion fails. assert(*(&move(t_ref).recipient) == move(sender), 99); return move(t); } // Allow the creator of the earmarked coin to reclaim it. public claim_for_creator(): R#Self.T { let t: R#Self.T; let coin: R#LibraCoin.T; let recipient: address; let sender: address; sender = get_txn_sender(); // This will fail if no resource of type T under the sender's address. t = move_from<T>(move(sender)); return move(t); } // Extract the Libra coin from its wrapper and return it to the caller. public unwrap(t: R#Self.T): R#LibraCoin.T { let coin: R#LibraCoin.T; let recipient: address; // This "unpacks" a resource type by destroying the outer resource, but // returning its contents. Only the module that declares a resource type // can unpack it. T { coin, recipient } = move(t); return move(coin); } }
Alice能夠經過建立一個調用create
Bob的地址a和LibraCoin.T
她擁有的事務腳本爲Bob建立一個專用硬幣。一旦一被建立,鮑勃能夠經過從發送交易權利要求的硬幣一個。這會調用claim_for_recipient
,將結果傳遞給unwrap
,並將返回的內容存儲LibraCoin
在他但願的任何地方。若是Bob花費太長時間在a下建立一個賬戶而且Alice想要收回她的資金,那麼她能夠經過使用claim_for_creator
後續來完成unwrap
。
細心的讀者可能已經注意到,該模塊中的代碼與內部結構無關LibraCoin.T
。它能夠像使用通用編程同樣容易地編寫(例如resource T<AnyResource: R> { coin: AnyResource, ... }
)。咱們目前正致力於爲Move添加對這種參數多態的支持。
在不久的未來,IR將穩定下來,編譯和驗證程序將變得更加用戶友好。此外,將跟蹤來自IR源的位置信息並將其傳遞給驗證程序,以使錯誤消息更易於調試。可是,IR將繼續做爲測試Move字節碼的工具。它意味着是底層字節碼的語義透明表示。爲了進行有效的測試,IR編譯器必須生成錯誤的代碼,這些代碼將被字節碼驗證程序拒絕或在運行時在編譯器中失敗。用戶友好的源語言會作出不一樣的選擇; 它應該拒絕編譯將在管道的後續步驟中失敗的代碼。
未來,咱們將擁有更高級別的Move源語言。該源語言旨在安全輕鬆地表達常見的Move習語和編程模式。因爲Move字節碼是一種新語言,而Libra Blockchain是一種新的編程環境,咱們對應該支持的習語和模式的理解仍在不斷髮展。源語言還處於開發的早期階段,咱們尚未發佈它的時間表。
Libra國內開發者微信交流羣:
不能入羣請加管理微信,拉你進羣=>