Solana中的account
Posted mutourend
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Solana中的account相关的知识,希望对你有一定的参考价值。
1. 引言
wallet account对应的曲线为ed25519:
pub struct Keypair(ed25519_dalek::Keypair);
# solana config get //其中Keypair Path: /xxxxx/.config/solana/id.json 则为默认存储solana-keygen new的私钥地址
// 将https://www.sollet.io/# 中的私钥导出,存入Keypair Path: /xxxxx/.config/solana/id.json 中
# solana-keygen pubkey //显示的地址与 https://www.sollet.io/# 中的地址一致。
# solana address //显示的地址与 https://www.sollet.io/# 中的地址一致。
# solana config set --url https://api.testnet.solana.com //配置连接测试网
# solana airdrop 1 //获取1SOL 测试token
# solana balance //获取token余额
// 强制生成新的key
# solana-keygen new --no-passphrase --force
Bitcoin and Ethereum use Secp256k1, NEO uses secp256r1, Cardano and Solana Ed25519.
In any case, the overall process goes like this:
Private-key → ed25519-elliptic-curve-magic → toBase58 → Address
An account is essentially a file with an address (pubkey) that lives on the Solana blockchain. If you need to store data on chain it gets stored in an account. Accounts live in the memory of the network of validators. An account must pay rent (see below) in order to persist on chain.
Solana中的account分为:
- wallet account
- vote account
- stake account
- program account
Solana中的stake account 用于 delegate tokens to validators,来赚取奖励。
stake account的创建和维护不同于传统的wallet address,wallet address又名system account。
system account仅支持收发SOL,而stake account支持delegate token所需的复杂操作。
不同于以太坊和比特币,为了keep accounts alive,Solana中引入了称为rent的storage cost。
这种定期收取租金的做法有效地迫使Validator不要乐观地将过期账户存储到冷库中,从而节省存储成本,这对账户所有者不利,并可能导致他们的交易比其他人拖延更长的时间。另一方面,这可以防止恶意用户创建大量垃圾帐户,加重验证程序的负担。
当前主网和测试网的fixed rent fee为19.055441478439427 lamports per byte-epoch。一个epoch对应约2天。(1 lamport=10^{-9} SOL)
Rent计算中会包含account metadata(address, owner, lamports等等)的size,因此,最小的account,其rent计算的大小为128bytes,对应一个epoch的租金为2439 lamports。
而对于具有15000bytes的account,其每个epoch的租金为0.000288276SOL,2年的租金为0.10529088 SOL。
2. stake account
对于wallet address(又名system account),拥有该地址私钥的holder可控制该钱包。
每个stake account都有唯一的地址,可通过命令行或network explorer工具来查看账户信息。但是,stake account可以没有对应的私钥,即使有关联的私钥,也没法控制该stake account。
仅在 使用命令行方式创建stake account 时,才会有对应stake account address的keypair file,创建该keypair file的主要目的也仅仅是用于保证所创建的stake account address是新的且unique的。
2.1 stake account authority
特定类型的account可有一种或多种关联的signing authority。account authority用于对该账号控制的特定交易进行签名。
而在其他区块链系统中,控制account address对应keypair的holder可控制该account的所有行为。Solana的设计有所不同,借助不同的authority来控制account的不同行为。
每个stake account有2种不同的signing authority,用于控制基于该stake account的不同行为:
- stake authority
- withdraw authority
其中,stake authority主要用于对以下操作进行签名:
- delegating stake
- deactivating the stake delegation
- splitting the stake account, creating a new stake account with a portion of the funds in the first account
- merging two stake accounts into one
- setting a new stake authority
withdraw authority主要用于对以下操作进行签名:
- withdrawing un-delegated stake into a wallet address
- setting a new withdraw authority
- setting a new stake authority
当stake account创建时,就已设置了stake authority和withdraw authority,后续跟根据需要修改,stake authority 和 withdraw authority可为相同的地址,也可为2个不同的地址。其中withdraw authority keypair对账号的控制权更强,因为它可用于:
- 清算stake account中的tokens
- 当stake authority keypair lost或compromised时,可借助withdraw authority keypair来reset the stake authority。
因此,当管理stake account时,保护withdraw authority keypair至关重要。
$ ./solana stake-account E238XEFHdVXP5svYwa46R3gutLS5uyvgk4DGaUsPqmeM
Balance: 180064.987050082 SOL
Rent Exempt Reserve: 0.00228288 SOL
Delegated Stake: 180064.984767202 SOL
Active Stake: 180064.984767202 SOL
Delegated Vote Account Address: 13DmkMhdpmJJu7nU2ozAyPiKuopZbYShMHV3JAA7YVYC
Stake Authority: CbGav5iwgvGhJt8fJgha2ekE5pPguAisYwtcU23sHaZP
Withdraw Authority: CbGav5iwgvGhJt8fJgha2ekE5pPguAisYwtcU23sHaZP
借助$ ./solana validators
可查看到当前Solana网络中所有Validators的identity和vote account。
2.2 multiple delegations
每个stake account一次仅允许delegate给一个validator。该stake account中的tokens要么全部delegated,要么全部un-delegated,要么在称为delegated或un-delegated的过程中。若要将你的token的一部分(而不是全部)delegate给a validator,或者同时delegate给多个validators,则必须创建多个stake accounts。
可基于拥有token的wallet address来创建多个stake accounts,或者,创建一个大的stake account,然后借助stake authority来将该account切分为多个account,每个account的金额可自行设定。
可对多个stake account分配相同的stake authority和withdraw authority。
2.3 merging stake account
2个具有相同stake authority和lockup的stake authority可merge为一个单独的stake account。满足以下条件时,可进行merge操作:
- 2个deactivated stake account。
- an inactive stake into an activating stake during its activation epoch。
以下场景时,voter pubkey和vote credits应匹配:
- two activated stakes
- two activating accounts that share an activation epoch, during the activation epoch
除以上条件和场景之外的其他情况,merge操作都将失败。
2.4 delegation warmup and cooldown
当a stake account is delegated 或 a delegation is deactivated,该操作都不会立刻生效。
delegation和deactivation将需要几个epochs来完成。
同时,在单个epoch中,可delegated或deactivated的总stake也有限制,以防止网络中总体stake发生大幅突变。由于warmup和cooldown取决于网络中其他参与者的行为,具体的生效周期将很难预测。
详细的warmup和cooldown timing规则可参看:https://docs.solana.com/cluster/stake-delegation-and-rewards#stake-warmup-cooldown-withdrawal。
2.5 lockup
stake accounts有lockup,用于防止其拥有的tokens在特定的日期或epoch之前被withdraw。
当lockup时,stake account仍然可以:
- be delegated
- un-delegated
- split
- 正常变更其stake authority和withdraw authority。
但是,不支持对其进行withdraw into a wallet address操作。
A lockup can only be added when a stake account is first created, but it can be modified later, by the lockup authority or custodian, the address of which is also set when the account is created.
2.6 destroy a stake account
与Solana中的其他类型的account类似,balance为0 SOL的stake account将不再track。当一个stake account未delegated,且其拥有的所有token都被withdraw to a wallet address时,该account address将被destroyed,未来将需要手工re-created for the address to be used again。
3. vote account
在Solana中,若想成为Validator,则需要创建vote account。可通过solana-create-vote-account
来创建vote account。
除vote account address本身外,vote account的其他方面均可修改:
- 可使用
solana-vote-update-validator
来修改validator identity。 - 可使用
solana-vote-authorize-voter
来修改vote authority。 - 可使用
solana--vote-authorize-withdrawer
来修改withdraw authority。 - 可使用
solana-vote-update-commission
来修改commission。
3.1 vote account address
vote account address 的创建方式有2种:
- 来源于a keypair file的public key
- 根据a keypair file的public key和a seed string 派生出来的address
vote account address不需要对任何交易进行签名,仅用于look up the account information。
当某人需要delegate tokens in a stake account时,在该delegation command中需指定the vote account address of the validator to whom the token-holder wants to delegate。
3.2 validator identity account
validator identity为system account,用于 pay for all the vote transaction fees submitted to the vote account。
由于validator is expected to vote on most valid blocks it receives, validator identity is frequently (可能每秒多次) signing transactions and paying fees。因此,需要validator identity keypair需要以热钱包(keypair file)的方式存储于validator进程所运行的系统上。
通常,热钱包的安全性要低于离线或冷钱包,validator operator应在identity account中选择存储小额够一定期限内(数周或数月)用于投票的费用。这将有助于控制因节点硬盘或文件系统故障等导致的资金丢失风险。
validator的identity account应定期topped off from a more secure wallet。
当vote account创建时,需提供validator identity。
3.3 vote authority
vote authority keypair用于sign each vote transaction the validator node wants to submit to the cluster。
vote authority没必要与validator identity不同。因为vote authority和identity account雷士,均需要频繁的对交易签名,因此,没必要在validator进程所在的文件系统中再保存一个hot keypair。
vote authority可设置为与validator identity account一样。当两者一样时,在每个vote transaction中仅需要一个签名,来sign the vote and pay the transaction fee。由于Solana中的transaction fee是根据签名个数来评估的,使用一个签名相比于使用2个不同的账号进行2次签名,交易费用可节约一半。
可在vote account创建时设置vote authority,若未明确指出,其默认为与validator identity一样。可通过solana-vote-authorize-voter
来修改。
不过,vote authority在每个epoch最多可修改一次,且在下个epoch才生效。为了平滑的在epoch边界过度vote signing,可在solana-validator中附带–authorized-voter参数来明确指定。
3.4 vote account 的withdraw authority
withdraw authority keypair通过solana-withdraw-from-vote-account
命令来withdraw funds from a vote account。validator获得的任何网络激励都将存入vote account中,且仅可通过withdraw authority keypair签名来获取。
需要使用withdraw authority keypair来对以下交易进行签名:
- any transaction to change a vote account’s commission
- any transaction to change the validator identity on a vote account
由于vote account的balance会增加,因此需要将withdraw authority keypair以离线或冷钱包的方式保存,因为其并不需要频繁的对交易进行签名。
可使用solana--vote-authorize-withdrawer
命令来修改withdraw authority。
3.5 vote account commission
commission为:the percent of network rewards earned by a validator that are deposited into the validator’s vote account。剩余的rewards将分发给all of the stake accounts delegated to that vote account, proportional to the active stake weight of each stake account。
如,若a vote account的commission为10%,则all rewards earned by that validator in a given epoch的10%将在下一个epoch的第一个block中直接存入到the vote account,而剩下的90%将直接作为active stake存入各delegated stake accounts。
为了吸引更多的stake delegations,validator可设置更低的commission值。由于运行和维护validator节点需要一定的成本,应设置一个合理高的commission值来覆盖其运维成本。
可在创建vote account时附加–commission来在设置commission值,若未明确指定,该值默认为100%,即所有奖励直接存入vote account,而没有奖励会分发给delegated stake accounts中。
可通过solana-vote-update-commission
来修改commission,该值取值范围为0~100,--commision 10
表示为10% commission。
3.6 vote account authority key rotation
对于live validator,rotating the vote account authority keys需要以下特殊操作,详细参见:https://docs.solana.com/running-validator/vote-accounts#key-rotation。
4. program account
在Solana中,智能合约统称为program。每个链上program其本质就是个account,只是其标记为“Executable: true”,意味着它是an executable file。program一旦部署在链上,就可read并通过instructions来交互。
- program id:为program的public key。
- account ownership:accounts are owned by programs which are indicated by a program id in the metadata “owner” field。
native program,为网络中必须运行的特殊program,主要有:【详细参见:Solana native programs】
- system program:负责创建新的accounts,以及分配account ownership。
- BPF Loader:负责Solana合约的部署、升级以及instructions运行。
- 以及其他与solana programming关联的其他native programs。
token program:为实现不同于native SOL token的 fungible或non-fungible token的program。
associated token accounts:若an account holds any token other than the native Solana token, it will have an associated token account for each type of token it holds。
instruction:what is called in order to execute a function of a program。
Sysvar:为an account which enforces certain variables of the network such as epoch, rent, validator rewards, etc…
Program Derived Addresses (PDAs):PDAs enable the transfer of an account’s ownership from one program to another. This is useful for situations that require an escrow account such as auctions, DEXs, swaps, etc…
Cross Program Invocation:Calling a program from another program. This is helpful for more complex on chain actions. Such as executing an instruction of an associated token account program during a token swap。
参考资料
[1] Solana Stake Account Structure
[2] Solana Delegate Stake
[3] Solana Development Tutorial: Program 101
[4] Solana Development Tutorial: Environment setup
[5] [How to] create a Solana Address & delegate your SOL tokens using the Terminal (CLI) on your Mac
[6] Solana Vanity Address using GPUs
[7] Solana Clusters
[8] Vote account management
[9] Solana Programming Concepts
以上是关于Solana中的account的主要内容,如果未能解决你的问题,请参考以下文章
Solana中利用Anchor自动解析TokenAccount
Solana中利用Anchor自动解析TokenAccount