我眼中的Solana Blockchain

基于对rust的理解,我们探索的脚步继续前进。

Rust企业落地探索_Raymond-Shen的博客-CSDN博客前言最近我在微博转发了亚马逊对Rust和Go语言的对比。亚马逊发文力捧 Rust ,Go 技术负责人:别“拉踩”我们AWS的文章对Rust的推崇溢于言表。引起了业内的震动。再次把cloud native, green programming language 这些问题推到舆论风口。那我们就多关注一下Rust在学习Rust的时候,我们再语言入门的时候一定有很多的资料。我可以推荐大家看一些基本的资料。Rust学习资源和路线_weixin_34174132的博客-CSDN博客Rust学习资料汇https://blog.csdn.net/weixin_41308834/article/details/123752140?spm=1001.2014.3001.5501当前最为重要的区块链实现之一的Solana一直以超高的TPS著称。这个绝大多数代码由rust编写的高速区块链。对未来DeFi提供了哪些支持呢?

本人之前对PolkaDot做了一定的研究。非常赞赏PolkaDot使用WebAssembly标准支持多语言合约编写能力。而Solana,为了追求速度,真的要放弃脚本语言写合约吗?

真正实力

Solana的主页有性能指标的实时监控。我们对Solana的生产环境的情况还是可以看到的。

这个性能指标作为第三代区块链实现确实让人印象深刻。 

zhttps://github.com/agjell/sol-tutorials/blob/master/solana-benchmarks.mdhttps://github.com/agjell/sol-tutorials/blob/master/solana-benchmarks.md我在自己的Apple M1笔记本上进行了验证。基本上可以得到2万笔每秒的交易速率。这还是非常令人振奋的。我觉的性能瓶颈还是内存。由于Solana的validator 节点内存从4G逐步升高到7.4G。性能还是逐步的下降。从这个角度看,生产环境配置还是需要考虑大内存的节点。

对于Solana共识机制的一些介绍可以参见这个文档。

另类的Solana:不走分片之路_腾讯新闻(奥维的街道及阶梯,梵高)蓝狐笔记之前多次介绍过Solana,可以查阅《为什么Solana是区块链开发者需要的“世界计算机”?》、《区块链的“历史证明机制”:时间与共识》等,如今它已经发展了不少。前……https://new.qq.com/omn/20200810/20200810A00JJ000.html但是如果想了解更多的细节,有时间还是需要学习白皮书。下面是2021年底的Solana生态图。

 对于Solana的生态具体解释可以看如下文章。这里面提及的一些关键项目,我们也会在后面进行简单的介绍。

https://medium.com/hydraswap/solana-2021-in-60-seconds-a-visual-journey-8fe24d407ae7https://medium.com/hydraswap/solana-2021-in-60-seconds-a-visual-journey-8fe24d407ae7

初步开始工作

安装

我使用的是MacOS Arm64,我按照官方文档进行了安装。在安装过程中并没有什么大的问题。看来Solana对于自身的节点能耗还是有自知之明,快速转移到Arm64还是能有帮助。

https://docs.solana.com/cli/install-solana-cli-tools#build-from-sourcehttps://docs.solana.com/cli/install-solana-cli-tools#build-from-source

Token开发

并且开始了尝试token开发。

https://spl.solana.com/tokenhttps://spl.solana.com/token

由于网络链接的问题,我们还是尽量使用本地节点。

solana config set --url localhost

代理的问题

但是在国内大家有可能会遇到一些网络的问题。我是想通过代理配置解决这个问题。但是不工作:

export ALL_PROXY=socks5://127.0.0.1:10010

这个问题已经在Github上开了一个issue,希望有人能解答。 

​​​​​​https://github.com/solana-labs/solana/issues/24075https://github.com/solana-labs/solana/issues/24075

本地节点和Hello  World

在无法使用devnet的情况下,我们还是可以使用本地节点来进行一些开发,测试的工作。这个Helloworld项目详细介绍了如何使用本地节点,开发测试合约。这里的合约有两种语言。Rust和C。大家可以看一下还是比较好的。

https://github.com/solana-labs/example-helloworldhttps://github.com/solana-labs/example-helloworld我在开发过程中补充一些步骤。在helloworld示例中。有这么一步:

npm run build:program-rust

这一步会要求安装 bpf 工具。这个工具比较大,在下载过程中有可能会出现断连接的情况。如果出现这种情况。可以手动下载 solana-bpf-tools。具体的下载链接在下面:Releases · solana-labs/bpf-tools · GitHubContribute to solana-labs/bpf-tools development by creating an account on GitHub.https://github.com/solana-labs/bpf-tools/releases

按照自己的机器架构,你可以下载相应的报。在我这里我使用了 solana-bpf-tools-osx.tar.bz2 .

mkdir -p <path-to-solana-install>/solana-release/bin/sdk/bpf/dependencies/bpf-tools
cd <path-to-solana-install>/solana-release/bin/sdk/bpf/dependencies/bpf-tools
cp ~/Downloads/solana-bpf-tools-osx.tar.bz2 ./
tar -jxf solana-bpf-tools-osx.tar.bz2

这样您就可以继续执行合约编译的工作了。 

bpf-tools

bpf工具现在看起来只发布了x86_64的版本。arm64版本估计还是要看看能不能通过代码进行编译得到。大家有时间的可以试试。大概看了一下build.sh,似乎是平台无关的。

GitHub - solana-labs/bpf-toolshttps://github.com/solana-labs/bpf-tools我们合约输出的文件格式如下。

% file helloworld.so 
helloworld.so: ELF 64-bit LSB shared object, eBPF, version 1 (SYSV), dynamically linked, stripped

如果想更多了解eBPF文件格式,我们可以仔细阅读 Berkeley Packet Filter了解更多。总之这是一种中间代码,在真正执行的时候会被JIT compiler编译成真正的本地代码执行。由于一开始这种技术使用在内核对网络包的过滤上,所以,效率很高。更加深入的了解可以看一下:

eBPF assembly with LLVMClang and LLVM, used to compile from C to eBPF, got support for eBPF assembly in version 6.0. Let's have a look at it.https://qmonnet.github.io/whirl-offload/2020/04/12/llvm-ebpf-asm/最终由于llvm中间层的存在,rust, clang支持的语言,都可以变异成 eBPF的目标代码,在又可能不同CPU类型的solana节点里部署,运行。

而对于rust项目输出为eBPF代码我们只需要用好命令行参数。

cargo build-bpf --manifest-path=./src/program-rust/Cargo.toml --bpf-out-dir=dist/program

合约开发探索

Hello World

已经有一篇非常好的文档来介绍如何在Solana开发helloworld程序。大家可以使用这个文档体验一下:

example-helloworld/README_ZH_CN.md at master · solana-labs/example-helloworld · GitHubhttps://github.com/solana-labs/example-helloworld/blob/master/README_ZH_CN.md

这个是比较好的官方文档。还是比较全面的。

Chainlink Solana Demo

GitHub - smartcontractkit/chainlink-solana-demo: Showing how to deploy a Solana program using Chainlink Price FeedsShowing how to deploy a Solana program using Chainlink Price Feeds - GitHub - smartcontractkit/chainlink-solana-demo: Showing how to deploy a Solana program using Chainlink Price Feedshttps://github.com/smartcontractkit/chainlink-solana-demo

也可以看一下Solana的官方文档来帮助我们理解更多Chainlink在Solana上的用法。 

Chainlink on Solana | Chainlink Documentationhttps://docs.chain.link/solana/

Solana Escrow 

https://hackmd.io/@ironaddicteddog/solana-starter-kit

https://github.com/paul-schaaf/solana-escrowhttps://github.com/paul-schaaf/solana-escrowSolana Escro with Anchor

​​​​​​https://hackmd.io/@ironaddicteddog/solana-anchor-escrow

https://github.com/ironaddicteddog/anchor-escrowhttps://github.com/ironaddicteddog/anchor-escrow

 为了更安全的开发Solana的相关程序。Anchor引入进来了。这个官方文档还是需要仔细看一下。当然。

Introduction - The Anchor Book v0.23.0https://book.anchor-lang.com/这个官方实例也说明了Anchor如何使用。

GitHub - smartcontractkit/solana-starter-kit: Example code for using Chainlink on SolanaExample code for using Chainlink on Solana. Contribute to smartcontractkit/solana-starter-kit development by creating an account on GitHub.https://github.com/smartcontractkit/solana-starter-kit

Simple Vote Tutorial

这是一个非常全面的例子来说明一个Solana DApp的开发。 

GitHub - mcf-rocks/simple-vote-tutorial: Simple Smart Contract TutorialSimple Smart Contract Tutorial. Contribute to mcf-rocks/simple-vote-tutorial development by creating an account on GitHub.https://github.com/mcf-rocks/simple-vote-tutorial

整体体验下来,对于合约开发,如果直接使用rust API来进行编码,会涉及到比较底层的逻辑。甚至参数的输入输出都需要考虑编解码。这对于合约开发的生产力还是有很大的影响。所以,还是推荐使用Anchor库进行开发。所以在上面的几个示例里,推荐大家完整了解 Solana Escrow with Anchor这个示例。可能今后快速上手相关开发,还是要用它来作为团队的入门教材。

DeFi

这里Solana有很多资料可以帮助我们理解DeFi的开发。

https://solana.com/developers/defihttps://solana.com/developers/defi

Saber - AMM Swap

这是一个AMM的Swap项目。Saber | Solana AMMSaber is an automated market maker for trading assets on Solana.https://app.saber.so/#/swap

这里是代码:

https://github.com/saber-hq/stable-swaphttps://github.com/saber-hq/stable-swap

Serum - OrderBook DEX

这是一个在Solana上面构建订单薄的项目。如果Solana的高性能能够支持外部做市所需要的下撤单速度,这个项目无疑会比较成功。

https://www.projectserum.com/https://www.projectserum.com/代码在这里,我们可以仔细看看。

serum-dex/dex at master · project-serum/serum-dex · GitHubProject Serum Rust Monorepo. Contribute to project-serum/serum-dex development by creating an account on GitHub.https://github.com/project-serum/serum-dex/tree/master/dex

以此为例,我们可以看到一个真正的完全的订单簿实现的智能合约。

参考资料

Escrow Program and Escrow with Anchor 

GitHub - smartcontractkit/solana-starter-kit: Example code for using Chainlink on Solana

BPF and related observability tools give software professionals unprecedented visibility into software, helping them analyze operating system and application performance, troubleshoot code, and strengthen security. BPF Performance Tools: Linux System and Application Observability is the industry’s most comprehensive guide to using these tools for observability. Brendan Gregg, author of the industry’s definitive guide to system performance, introduces powerful new methods and tools for doing analysis that leads to more robust, reliable, and safer code. This authoritative guide: Explores a wide spectrum of software and hardware targets Thoroughly covers open source BPF tools from the Linux Foundation iovisor project’s bcc and bpftrace repositories Summarizes performance engineering and kernel internals you need to understand Provides and discusses 150+ bpftrace tools, including 80 written specifically for this book: tools you can run as-is, without programming — or customize and develop further, using diverse interfaces and the bpftrace front-end You’ll learn how to use BPF (eBPF) tracing tools to analyze CPUs, memory, disks, file systems, networking, languages, applications, containers, hypervisors, security, and the Linux kernel. You’ll move from basic to advanced tools and techniques, producing new metrics, stack traces, custom latency histograms, and more. It’s like having a superpower: with Gregg’s guidance and tools, you can analyze virtually everything that impacts system performance, so you can improve virtually any Linux operating system or application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值