日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Libra教程之:来了,你最爱的Move语言

發(fā)布時(shí)間:2024/2/28 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Libra教程之:来了,你最爱的Move语言 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • Move語(yǔ)言
    • Move的核心概念
      • Move交易腳本
      • Move modules
      • Move resources
    • 寫一個(gè)Move程序
      • 編寫交易腳本
      • 編寫自己的Modules

Move語(yǔ)言

Move是一種新的編程語(yǔ)言,旨在為L(zhǎng)ibra區(qū)塊鏈提供安全且可編程的基礎(chǔ)。 Libra區(qū)塊鏈中的帳戶就是由任意數(shù)量的Move resources和Move modules組成的。 提交給Libra區(qū)塊鏈的每個(gè)交易都使用Move編寫的交易腳本來(lái)對(duì)其邏輯進(jìn)行編碼。

交易腳本通過(guò)調(diào)用module聲明的procedures來(lái)更新區(qū)塊鏈的全局狀態(tài)。

Move的核心概念

Move交易腳本

每個(gè)Libra交易都包含一個(gè)Move交易腳本,該腳本對(duì)驗(yàn)證程序代表客戶執(zhí)行的邏輯進(jìn)行編碼(例如,將Libra從A的帳戶轉(zhuǎn)移到B的帳戶)。

通過(guò)調(diào)用一個(gè)或多個(gè)Move模塊的procedures,事務(wù)腳本與發(fā)布在Libra區(qū)塊鏈的全局存儲(chǔ)中的Move resources進(jìn)行交互。

事務(wù)腳本并不會(huì)在全局狀態(tài)中存儲(chǔ),并且其他事務(wù)腳本也無(wú)法調(diào)用它。 它是一個(gè)一次性程序。

Move modules

Move modules定義了用來(lái)更新Libra區(qū)塊鏈的全局狀態(tài)的規(guī)則。 modules相當(dāng)于其他區(qū)塊鏈中的智能合約。 它聲明了可以在用戶帳戶下發(fā)布的resources類型。 Libra區(qū)塊鏈中的每個(gè)帳戶都是一個(gè)容器,用于容納任意數(shù)量的resources和modules。

module主要用來(lái)聲明結(jié)構(gòu)類型(包括資源,這是一種特殊的結(jié)構(gòu))和procedures。

Move module的procedures定義了用于創(chuàng)建,訪問和銷毀其聲明的類型的規(guī)則。

modules是可重用的。 在一個(gè)module中聲明的結(jié)構(gòu)類型可以使用在另一個(gè)module中聲明的結(jié)構(gòu)類型,并且在一個(gè)module中聲明的可以procedure調(diào)用在另一個(gè)module中聲明的public procedures。 一個(gè)module可以調(diào)用在其他Move module中聲明的procedures。 事務(wù)腳本可以調(diào)用已發(fā)布module的任何public procedures。

最后,Libra用戶將能夠使用自己的帳戶發(fā)布modules。

Move resources

Move的主要功能是能夠定義自定義資源類型。 資源類型主要對(duì)數(shù)字資產(chǎn)進(jìn)行編碼。

資源在Libra中隨處可見。 它們可以存儲(chǔ)為數(shù)據(jù)結(jié)構(gòu),可以作為參數(shù)傳遞給過(guò)程,可以從過(guò)程中返回,等等。

Move type system為資源提供了特殊的安全保證。 Move resources永遠(yuǎn)不能被復(fù)制,重用或丟棄。 資源類型只能由定義該類型的模塊創(chuàng)建或銷毀。這是由Move虛擬機(jī)通過(guò)字節(jié)碼驗(yàn)證來(lái)強(qiáng)制進(jìn)行保證的。 Move虛擬機(jī)將拒絕運(yùn)行尚未通過(guò)字節(jié)碼驗(yàn)證程序的代碼。

Libra貨幣是通過(guò)LibraCoin.T的資源類型來(lái)實(shí)現(xiàn)的。 和其他的資源一樣,LibraCoin.T也是一種資源。

寫一個(gè)Move程序

本節(jié)我會(huì)介紹怎么使用Move IR來(lái)編寫事務(wù)腳本和模塊。IR是即將推出的Move源語(yǔ)言的預(yù)覽版本(不穩(wěn)定)。 Move IR是Move字節(jié)碼上的一個(gè)薄語(yǔ)法層,用于測(cè)試字節(jié)碼驗(yàn)證程序和虛擬機(jī),它對(duì)開發(fā)人員并不特別友好。 它足夠高,可以編寫人類可讀的代碼,但是也足夠低,可以直接編譯為Move字節(jié)碼。

編寫交易腳本

用戶通過(guò)交易腳本來(lái)請(qǐng)求對(duì)Libra區(qū)塊鏈的全局存儲(chǔ)進(jìn)行更新。幾乎所有事務(wù)腳本中都會(huì)出現(xiàn)兩個(gè)重要資源:LibraAccount.T和LibraCoin.T資源類型。 LibraAccount是module的名稱,而T是該module聲明的資源的名稱。這是Move中的通用命名約定。module聲明的“主要”類型通常稱為T。

當(dāng)我們說(shuō)用戶“在Libra區(qū)塊鏈上的地址0xff擁有一個(gè)帳戶”時(shí),我們的意思是地址0xff擁有LibraAccount.T資源的實(shí)例。每個(gè)非空地址都有一個(gè)LibraAccount.T資源。此資源存儲(chǔ)帳戶數(shù)據(jù),例如序列號(hào),身份驗(yàn)證密鑰和余額。要與帳戶進(jìn)行交互的Libra系統(tǒng)的任何部分都必須通過(guò)從LibraAccount.T資源中讀取數(shù)據(jù)或調(diào)用LibraAccount module的procedures來(lái)進(jìn)行此操作。

帳戶余額是LibraCoin.T類型的資源。這是Libra貨幣的類型。與任何其他Move資源一樣,此類型在語(yǔ)言上是一等公民。

LibraCoin.T類型的資源可以存儲(chǔ)在程序變量中,在過(guò)程之間傳遞,等等。

現(xiàn)在讓我們看看程序員如何在事務(wù)腳本中與這些模塊和資源進(jìn)行交互。

// 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: LibraCoin.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 failLibraAccount.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; }

此交易腳本有一個(gè)不幸的問題-如果收款人下沒有帳戶,它將失敗。 我們將通過(guò)修改腳本為收款人創(chuàng)建帳戶(如果尚不存在)來(lái)解決此問題。

// 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: 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; }

讓我們看一個(gè)更復(fù)雜的例子。 在此示例中,我們將使用交易腳本向多個(gè)收件人付款,而不僅僅是一個(gè)。

// 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: LibraCoin.T;let coin2: 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 paymentsLibraAccount.deposit(move(payee1), move(coin1));LibraAccount.deposit(move(payee2), move(coin2));return; }

好了,這就是簡(jiǎn)單的交易腳本,雖然我們不了解Move IR的語(yǔ)法,但是直接看內(nèi)容應(yīng)該就很容易明白這個(gè)腳本到底在做什么了。

編寫自己的Modules

上面的交易腳本使用了現(xiàn)有的LibraAccount和LibraCoin modules,那么我們?cè)趺淳帉懽约旱腗ove modules呢?

考慮這種情況:B將來(lái)會(huì)在地址a創(chuàng)建一個(gè)帳戶。 A想為B“??睢币恍┵Y金,以便他一旦創(chuàng)建就可以將其存入他的帳戶。 但是,如果B從未創(chuàng)建該帳戶,她還希望能夠自己收回資金。

為了解決A的這個(gè)問題,我們將編寫一個(gè)模塊EarmarkedLibraCoin:

  • 聲明一個(gè)新的資源類型EarmarkedLibraCoin.T,該資源類型包裝了Libra coin和收件人地址。
  • 允許A創(chuàng)建此類類型并將其發(fā)布到她的帳戶下(創(chuàng)建過(guò)程)。
  • 允許B聲明資源(claim_for_recipient過(guò)程)。
  • 允許擁有EarmarkedLibraCoin.T的任何人銷毀它并獲得相應(yīng)的coin(拆包程序)。
// 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: 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: LibraCoin.T, recipient: address) {let t: Self.T;// Construct or "pack" a new resource of type T. Only procedures of the// `EarmarkedLibraCoin` module can create an `EarmarkedLibraCoin.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): Self.T acquires T {let t: Self.T;let t_ref: &Self.T;let sender: address;// Remove the earmarked coin resource published under `earmarked_coin_address`.// If there is no 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(): Self.T acquires T {let t: Self.T;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: Self.T): LibraCoin.T {let coin: 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);}}

A可以通過(guò)創(chuàng)建交易腳本來(lái)為B創(chuàng)建專用coin,該交易腳本調(diào)用B地址a上的create和她擁有的LibraCoin.T。 創(chuàng)建a之后,B可以通過(guò)發(fā)送來(lái)自a的交易來(lái)claim coin。 這將調(diào)用claim_for_recipient,將結(jié)果傳遞給unwrap,并在他希望的任何地方存儲(chǔ)返回的LibraCoin。

如果B花費(fèi)太長(zhǎng)時(shí)間在a帳戶下創(chuàng)建帳戶,而A想要收回其資金,則可以通過(guò)使用Claim_for_creator然后取消unwrap來(lái)做到這一點(diǎn)。

好了,我們的程序就寫完了。目前IR還是不穩(wěn)定版本,關(guān)于IR語(yǔ)法的更詳細(xì)內(nèi)容,我會(huì)在后面的文章中講到。

更多精彩內(nèi)容且看:

  • 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級(jí)賬本,以太坊,Libra,比特幣等持續(xù)更新
  • Spring Boot 2.X系列教程:七天從無(wú)到有掌握Spring Boot-持續(xù)更新
  • Spring 5.X系列教程:滿足你對(duì)Spring5的一切想象-持續(xù)更新
  • java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程

更多教程請(qǐng)參考 flydean的博客

總結(jié)

以上是生活随笔為你收集整理的Libra教程之:来了,你最爱的Move语言的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。