探索 Solana 与 合约核心概念
<!--StartFragment-->
Solana 介绍
-
Solana 是一条高性能的 L1 公链
-
Proof of History
(PoH)- 用于在不信任彼此的计算机之间进行时间同步
- 比没有时钟的区块链快好多倍 (1k, 10k)
-
部署到 Solana 区块链上的代码称为 program(合约)
- 要与 program 交互,您需要从客户端在区块链上发送一笔 Transaction
-
部署上去的合约通过 sdk 或者 cli 进行通信
- 通过
sdk
编写dapp
- 通过
<!--EndFragment-->
<!--StartFragment-->
Solana 核心概念
Account
<!--EndFragment-->
pub struct Account {
/// lamports in the account
pub lamports: u64,
/// data held in this account
#[serde(with = "serde_bytes")]
pub data: Vec<u8>,
/// the program that owns this account. If executable, the program that loads this account.
pub owner: Pubkey,
/// this account's data contains a loaded program (and is now read-only)
pub executable: bool,
/// the epoch at which this account will next owe rent
pub rent_epoch: Epoch,
}
<!--StartFragment-->
账号和签名
- 我们用户理解的
账号
,就是一串Ed25519的私钥
- 助记词 → 随机数种子 → 私钥
- 而
用户账号的地址
, 则是这私钥对应的公钥
- 公钥和私钥放一起,就是所谓的
key-pair
- 钱包 Account
公钥 + 密码
得到私钥
, 再用私钥操作 Account
交易 (Transaction)
- 交易就是链外数据和链上数据产生的一次交互
- 交易是对多个交易
指令
的打包,所以起内容主要就是各个交易指令,以及相应指令对应的发起人
和签名
- Transaction 定义:Code <!--EndFragment-->
pub struct Message {
/// The message header, identifying signed and read-only `account_keys`.
/// Header values only describe static `account_keys`, they do not describe
/// any additional account keys loaded via address table lookups.
pub header: MessageHeader,
/// List of accounts loaded by this transaction.
#[serde(with = "short_vec")]
pub account_keys: Vec<Pubkey>,
/// The blockhash of a recent block.
pub recent_blockhash: Hash,
/// Instructions that invoke a designated program, are executed in sequence,
/// and committed in one atomic transaction if all succeed.
///
/// # Notes
///
/// Program indexes must index into the list of message `account_keys` because
/// program id's cannot be dynamically loaded from a lookup table.
///
/// Account indexes must index into the list of addresses
/// constructed from the concatenation of three key lists:
/// 1) message `account_keys`
/// 2) ordered list of keys loaded from `writable` lookup table indexes
/// 3) ordered list of keys loaded from `readable` lookup table indexes
#[serde(with = "short_vec")]
pub instructions: Vec<CompiledInstruction>,
/// List of address table lookups used to load additional accounts
/// for this transaction.
#[serde(with = "short_vec")]
pub address_table_lookups: Vec<MessageAddressTableLookup>,
}
pub enum VersionedMessage {
Legacy(LegacyMessage),
V0(v0::Message),
}
pub struct VersionedTransaction {
/// List of signatures
#[serde(with = "short_vec")]
pub signatures: Vec<Signature>,
/// Message to sign.
pub message: VersionedMessage,
}
<!--StartFragment-->
交易指令
-
交易指令 (Instruction) 定义:Code
- 需要
程序 id
,account 资源
,data 数据
<!--EndFragment-->
- 需要
pub struct CompiledInstruction {
/// Index into the transaction keys array indicating the program account that executes this instruction.
pub program_id_index: u8,
/// Ordered indices into the transaction keys array indicating which accounts to pass to the program.
#[serde(with = "short_vec")]
pub accounts: Vec<u8>,
/// The program input data.
#[serde(with = "short_vec")]
pub data: Vec<u8>,
}
<!--StartFragment-->
合约
-
系统合约
-
系统合约是由节点在部署的时候生成的,普通用户无法更新,他们像普通合约一样,可以被其他合约或者 RPC 进行调用
-
常见的系统合约
-
System Program: 创建账号,转账等作用
-
BPF Loader Program: 部署和更新合约
-
Vote program: 创建并管理用户 POS 代理投票的状态和奖励
-
….
-
-
普通合约
- 普通合约是由用户开发并部署,Solana 官方也有一些官方开发的合约,如 Token、ATA 账号等合约
- 不同于有些公链,Solana 上的合约是可以被更新的,也可以被销毁。并且当销毁的时候,用于存储代码的 Account 所消耗的资源也会归还给部署者
<!--EndFragment--> <!--StartFragment-->
合约与 Account
- 在上面的 Account 介绍中,我们有个 owner 的成员,这个就表示这个 Account 是被哪个合约管理的,或者说哪个 合约可以对这个 Account 进行读写,类似 Linux 操作系统中,文件属于哪个用户
一般合约
,他的 Owner 都是BPF Loader
- 存放我们代币余额的内容的 owner 都是
Token合约
→SPL Token Program
<!--EndFragment--> <!--StartFragment-->
租约
Solana 区块链引入了一种独特的资金模型来处理账户存储空间的费用,称为“租金”(Rent
)
-
交易费用 gas fee 和 rent 是两个东西,交易费是为了处理网络上的交易或指令,如转账、执行智能合约操作等
-
租金率
- Solana 租金率是在网络范围内设置的,主要基于每年每字节设置的 LAMPORTS。目前,租金率为静态金额并存储在 Rent 系统变量中
-
租金豁免
- 保持最低 LAMPORT 余额超过 2 年租金的账户被视为“免租金”,不会产生租金
-
垃圾收集
- 未保持租金豁免状态或余额不足以支付租金的帐户将通过称为垃圾收集的过程从网络中删除
-
永久存储的条件
- 要使账户在 Solana 上永久存储,你需要为该账户豁免租金,即一次性存入足够的 LAMPORTS。仅支付交易费用而不关注租金,不能保证账户及其数据的永久存储
<!--EndFragment--> <!--StartFragment-->
SPL Token(代币)
在以太坊中,普通代币被一个叫 ERC20 的提案定了规范,可以认为普通代币合约统一叫做 ERC20 代币,在 Solana 中,这个 ERC20 代币就是 SPL 代币
<!--EndFragment--> 创建代币可以使用像GTokentool这样的一键发币平台,无代码操作,简单方便。
版权声明
本文仅代表作者观点,不代表区块链技术网立场。
本文系作者授权本站发表,未经许可,不得转载。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。