Facebook最新Libra币开发指南---接口服务器开发2

2024-02-02 12:32

本文主要是介绍Facebook最新Libra币开发指南---接口服务器开发2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Facebook最新Libra币开发指南---接口服务器开发2

2019年06月24日 16:23:16 最老程序员闫涛 阅读数 1145

在上一篇博文中,我们已经使用Rust语言开发了一款简单的Web服务器,虽然以单线程方式工作,但是可以正确解析Libra Core的常见命令,完成了程序的基本框架,在这一篇文件中,我们将带领大家逐个实现这些命令,最后形成一个基本完整的程序,最后集成Libra Core的client工具中。这样我们就有一个Libra Core的Web接口,大家就可以将自己的应用系统第一时间移植到Libra Core,相信应该在绝对是Libra Core上的第一批应用,为明年Facebook推出的主网商用服务抢占有利的位置。

集成到命令行工具

我们首先需要将我们开发的Web服务器,集成到Libra Core Client中。首先打开libra/client/src/main.rs文件,将上一篇博文中的代码,除main函数外,全部拷贝到libra/client/src/main.rs文件中。
拷贝完成之后,我们首先编译运行一下Libra Core Client,以保证我们的程序没有编译错误,在libra目录下运行如下命令:

./scripts/cli/start_cli_testnet.sh
  • 1

在编译过程中会有一些警告信息,提示我们定义了一些函数而没有使用这些函数,因为我们定义的命令处理函数,确实没有调用过,所以我们可以暂时忽略这些警告信息。
接着我们在原来loop循环开始时调用启动我们服务器的方法,启动服务器,如下所示:

......let config = Config::builder().history_ignore_space(true).completion_type(CompletionType::List).auto_add_history(true).build();let mut rl = Editor::<()>::with_config(config);start_server(&mut client_proxy);/*loop {let readline = rl.readline("libra% ");match readline {Ok(line) => {......
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在上面的代码中,我们将client_proxy作为参数传入,实际上所有调用Libra Core功能的操作,均由此对象来实现。
重新编译运行客户端。我们应该可以看到Web服务器已经正常启动,在浏览器上输入http://127.0.0.1:7878/ls?cmd=query_balance&account_id=88 ,会显示如下所示的结果:
在这里插入图片描述
如果可以显示上述结果,就证明我们的Web服务器已经正常启动,并且可以正常接受和处理Libra Core的命令了。下面我们将在接收到客户端发送过来的命令之后,调用Libra Core相应接口,执行相应的逻辑,最后将调用结果返回给客户端。

改造启动方法和处理连接方法

由于我们要在Libra Core Client的模式下运行,我们的启动方法需要做出如下改动:

fn start_server(client_proxy: &mut ClientProxy) {println!("Libra Server v0.0.3 Starting up ...");let listener = TcpListener::bind("127.0.0.1:7878").unwrap();for stream in listener.incoming() {let stream = stream.unwrap();handle_connection(stream, client_proxy);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

我们的连接处理方法需要做出如下改动:

fn handle_connection(mut stream: TcpStream, client_proxy: &mut ClientProxy) {let mut contents: String = String::from("Hello World!");let mut buffer = [0; 1024];// 获取请求信息stream.read(&mut buffer).unwrap();println!("Request: {}", String::from_utf8_lossy(&buffer[..]));let request = String::from_utf8_lossy(&buffer[..]);// 不处理请求网站图标请求if request.find("GET /favicon.ico HTTP/1.1") >= Some(0) {return ;}// 请出请求中的query stringlet query_string = &get_query_string(&request);println!("query_string:{}", query_string);let cmd = get_cmd_param(query_string.to_string());println!("接收到命令:cmd={}!", cmd);let params: Vec<_> = query_string.split("&").collect();if cmd.find("account_create")>=Some(0) {contents = handle_account_create(params, client_proxy);} else if cmd.find("account_list")>=Some(0) {contents = handle_account_list(params, client_proxy);} else if cmd.find("account_mint")>=Some(0) {contents = handle_account_mint(params, client_proxy);} else if cmd.find("query_balance")>=Some(0) {contents = handle_query_balance(params, client_proxy);} else if cmd.find("query_sequence")>=Some(0) {contents = handle_query_sequence(params, client_proxy);} else if cmd.find("transfer")>=Some(0) {contents = handle_transfer(params, client_proxy);} else if cmd.find("query_txn_acc_seq")>=Some(0) {contents = handle_query_txn_acc_seq(params, client_proxy);}let response = format!("HTTP/1.1 200 OK\r\n\r\n{}", contents);stream.write(response.as_bytes()).unwrap();stream.flush().unwrap();
}/**
* 获取请求中的Query String,规定参数以?cmd=开头
* @version v0.0.1 闫涛 2019.06.23
*/
fn get_query_string(request: &str) -> String {let pos = request.find("?cmd=");if pos <= Some(0) {return "Has no parameters in request".to_string();}let end_pos = request.find(" HTTP/1.1");return (&request[(pos.unwrap()+1)..end_pos.unwrap()]).to_string();
}/**
* 获取请求cmd参数值
* @version v0.0.1 闫涛 2019.06.23
*/
fn get_cmd_param(query_string: String) -> String {let params: Vec<_> = query_string.split("&").collect();for param in params.iter() {println!("item: {}!", param);if param.find("cmd=") >= Some(0) {let cmd = &param[4..];return cmd.to_string();}}return "".to_string();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

与前一篇文章中的程序不同点在于,调用每个命令处理函数时,需将client_proxy作为参数传入。有了这些公共方法之后,我们就可以开始实现各个命令处理函数了。

账户生成

在命令行模式下,我们通过account create来创建账户,在我们的web服务器中我们通过http://127.0.0.1:7878/ls?cmd=account_create来创建账户,命令处理代码如下所示:

/**
* 生成账户命令处理函数
* @version v0.0.1 闫涛 2019.06.23
*/
fn handle_account_create(_params: Vec<&str>, client_proxy: &mut ClientProxy) -> String {println!("生成新账户!");let mut rst: String;match client_proxy.create_next_account() {Ok(account_data) => {rst = format!("{{\"account_id\": \"{}\", \"wallet_address\": \"{}\"}}", account_data.index, hex::encode(account_data.address));},Err(e) => rst = format!("Error creating account:{}", e),}return rst;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这个例子中使用了Rust的一种比较特别的语法,我们用match修饰调用client_proxy.create_next_account方法,该方法会返回Ok或Err两种情况,我们需要分别进行处理,大家可以将match 理解为其他语言中的switch语句。

查询所有账户列表

我们通过http://127.0.0.1:7878/ls?cmd=account_list请求来获取所有账户列表,命令处理函数如下所示:

/**
* 列出当前系统的所有账户
* @version v0.0.1 闫涛 2019.06.23
*/
fn handle_account_list(params: Vec<&str>, client_proxy: &ClientProxy) -> String {return client_proxy.get_all_accounts();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我们在client/src/client_proxy.rs中添加一个新的函数,如下所示:

    /// Print index and address of all accounts.pub fn get_all_accounts(&self) -> String {let mut rst: String = String::new();rst.push_str("[");if !self.accounts.is_empty() {for (ref index, ref account) in self.accounts.iter().enumerate() {let mut item: String = String::new();item.push_str(&format!("{{\"account_id\":{}, \"wallet_address\":\"{}\", \"account_seq\":{}, \"account_status\":\"{:?}\"}},", index, hex::encode(&account.address), account.sequence_number, account.status));rst.push_str(&item);}}if let Some(faucet_account) = &self.faucet_account {println!("Faucet account address: {}, sequence_number: {}, status: {:?}",hex::encode(&faucet_account.address),faucet_account.sequence_number,faucet_account.status,);}rst.push_str("]");return rst;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

本来在官方程序中,有一个print_all_accounts方法,但是该方法只是将信息打印到屏幕上,这显然不是我们所需要的,所以我们重写了这个函数,将其返回值定义为Json字符串。

挖矿和发币

我们通过http://127.0.0.1:7878/ls?cmd=account_mint&account_id=1&num_coins=100来进行挖矿和发币,表明向编号为1的钱包发送100个Libra币,命令处理函数如下所示:

/**
* 挖指定数量的币发送给指定的账户
* @version v0.0.1 闫涛 2019.06.23
*/
fn handle_account_mint(params: Vec<&str>, client_proxy: &mut ClientProxy) -> String {let mut account_id: String = String::new();let mut num_coins: String = String::new();for param in params.iter() {if param.find("account_id=") >= Some(0) {account_id.push_str(&param[11..]);} else if param.find("num_coins=") >= Some(0) {num_coins.push_str(&param[10..]);}}println!("挖矿发币:account_id={}; num_coins={}!", account_id, num_coins);let cmd = format!("mint {} {}", account_id, num_coins);let params = ["mint", account_id.as_str(), num_coins.as_str()];match client_proxy.account_mint_coins(&params, false) {Ok(msg) => return msg,Err(_) => "{{\"status\": \"Error\"}}".to_string()}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

查询账户余额

我们通过http://127.0.0.1:7878/ls?cmd=query_balance&account_id=1来查询账户编号为1的账户的余额,命令处理函数如下所示:

/**
* 查询账户余额
* @version v0.0.1 闫涛 2019.06.23
*/
fn handle_query_balance(params: Vec<&str>, client_proxy: &mut ClientProxy) -> String {let mut account_id: String = String::new();for param in params.iter() {if param.find("account_id=") >= Some(0) {account_id.push_str(&param[11..]);} }println!("查询余额:account_id={};!", account_id);let params = ["balance", account_id.as_str()];match client_proxy.get_balance(&params) {Ok(num) => {let resp = format!("{{\"status\": \"Ok\", \"balance\": {} }}", num);return resp;},Err(_) => "{{\"status\": \"Error\"}}".to_string()}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

查询账户最新交易编号

我们通过http://127.0.0.1:7878/ls?cmd=query_sequence&account_id=1查询账户编号为1的账户的交易编号,注意这里只有资金转出时才产生交易,命令处理方法如下所示:

/**
* 查询指定账户的交易编号,即已经发生的交易数(指转出的笔数)
* @version v0.0.1 闫涛 2019.06.23
*/
fn handle_query_sequence(params: Vec<&str>, client_proxy: &mut ClientProxy) -> String {let mut account_id: String = String::new();for param in params.iter() {if param.find("account_id=") >= Some(0) {account_id.push_str(&param[11..]);}}println!("查询交易编号:account_id={};!", account_id);let params = ["sequence", account_id.as_str()];match client_proxy.get_sequence_number(&params) {Ok(seq) => {let resp = format!("{{\"status\": \"Ok\", \"sequence\": {} }}", seq);return resp;},Err(_) => "{{\"status\": \"Error\"}}".to_string()}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

转账

我们通过http://127.0.0.1:7878/ls?cmd=transfer&src_account_id=1&dest_account_id=2&amount=20来实现从账户1转20个Libra币给账户2,命令处理函数如下所示:

/**
* 账户之间转账
* @version v0.0.1 闫涛 2019.06.23
*/
fn handle_transfer(params: Vec<&str>, client_proxy: &mut ClientProxy) -> String {let mut src_account_id: String = String::new();let mut dest_account_id: String = String::new();let mut amount: String = String::new();for param in params.iter() {if param.find("src_account_id=") >= Some(0) {src_account_id.push_str(&param[15..]);} else if param.find("dest_account_id=") >= Some(0) {dest_account_id.push_str(&param[16..]);} else if param.find("amount=") >= Some(0) {amount.push_str(&param[7..]);}}println!("账户间转账交易:src_account_id={}; dest_account_id={}; amount={}!", src_account_id, dest_account_id, amount);let params = ["transfer", src_account_id.as_str(),dest_account_id.as_str(),amount.as_str()];match client_proxy.transfer_coins(&params, false) {Ok(ias) => {let resp = format!("{{\"status\": \"Ok\", \"account_index\": {}, \"account_number\": {} }}", ias.account_index, ias.sequence_number);return resp;},Err(_) => "{{\"status\": \"Error\"}}".to_string()}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

查询交易详细信息

我们通过http://127.0.0.1:7878/ls?cmd=query_txn_acc_seq&account_id=1&seq=1用来查询账户1的编号为1的交易的详情,命令处理函数如下所示:

/**
* 查询交易详情
* @version v0.0.1 闫涛 2019.06.23
*/
fn handle_query_txn_acc_seq(params: Vec<&str>, client_proxy: &mut ClientProxy) -> String {let mut account_id: String = String::new();let mut seq: String = String::new();for param in params.iter() {if param.find("account_id=") >= Some(0) {account_id.push_str(&param[11..]);} else if param.find("seq=") >= Some(0) {seq.push_str(&param[4..]);}}println!("查询交易详情:account_id={}; seq={}!", account_id, seq);let params = ["txn_acc_seq", account_id.as_str(), seq.as_str(), "false"];match client_proxy.get_committed_txn_by_acc_seq(&params) {Ok(rst) => {let mut resp = String::new(); //format!("{{\"status\": \"Ok\" }}");if let Some(obj) = rst {let trans = obj.0;resp.push_str(&trans.format_for_client(name_cb));}return resp;},Err(_) => "{{\"status\": \"Error\"}}".to_string()}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

这样,我们就拥有了一个与官方命令功能相等的Web API,虽然十分简陋,但是作为应用系统测试后台已经足够了,我们可以现在就开始构建基于Libra的应用系统,祝大家能抓住Libra的商机。

这篇关于Facebook最新Libra币开发指南---接口服务器开发2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/670716

相关文章

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

MySQL中C接口的实现

《MySQL中C接口的实现》本节内容介绍使用C/C++访问数据库,包括对数据库的增删查改操作,主要是学习一些接口的调用,具有一定的参考价值,感兴趣的可以了解一下... 目录准备mysql库使用mysql库编译文件官方API文档对象的创建和关闭链接数据库下达sql指令select语句前言:本节内容介绍使用C/

使用Java填充Word模板的操作指南

《使用Java填充Word模板的操作指南》本文介绍了Java填充Word模板的实现方法,包括文本、列表和复选框的填充,首先通过Word域功能设置模板变量,然后使用poi-tl、aspose-words... 目录前言一、设置word模板普通字段列表字段复选框二、代码1. 引入POM2. 模板放入项目3.代码

macOS彻底卸载Python的超完整指南(推荐!)

《macOS彻底卸载Python的超完整指南(推荐!)》随着python解释器的不断更新升级和项目开发需要,有时候会需要升级或者降级系统中的python的版本,系统中留存的Pytho版本如果没有卸载干... 目录MACOS 彻底卸载 python 的完整指南重要警告卸载前检查卸载方法(按安装方式)1. 卸载

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别