文章目录
数据存储
概述
NEAR区块链上存储的所有数据均以键/值对的形式完成。 我们在SDK中创建了几种收集方法,这些方法可以帮助您在链上存储数据。
- near-sdk-as for AssemblyScript smart contracts
- near-sdk-rs for Rust smart contracts
有关存储的信息,请参阅[存储抵押]。(https://docs.near.org/docs/concepts/storage-staking)
AssemblyScript集合类型
大O记号 Big-O Notation
下表中的Big-O表示法值描述了在Near-sdk-as中发现的各种收集方法的时间复杂度。 这些方法的复杂性与NEAR上的气体消耗相关联,可帮助您决定在项目中使用哪个集合。 在我们的收集方法中可以找到三种类型:
* - to insert at the end of the vector using push_back (or push_front for deque)
** - to delete from the end of the vector using pop (or pop_front for deque), or delete using swap_remove which swaps the element with the last element of the vector and then removes it.
Gas消耗举例 Gas Consumption Examples
以下示例显示了使用上述方法在燃气燃烧存储和检索键/值对中的差异。 请注意,减少链上运行时环境的气体成本已扣除,仅显示数据读/写。
您可以通过访问collection-examples-as复制并测试您自己的数据集。(https://github.com/near-examples/collection-examples-rs)
PersistentVector
- To create:
import { PersistentVector } from "near-sdk-as";
let vector = new PersistentVector<string>("v"); // choose a unique prefix per collection
- To use:
vector.push(value);
vector.pop(value);
vector.length;
sdk链接: https://github.com/near/near-sdk-as/blob/master/sdk-core/assembly/collections/persistentVector.ts
PersistentSet
Built on top of the Storage class, this implements a persistent set without iterators.
is not iterable
more efficient in the number of reads and writes
PersistentMap
Implements a map built on top of the Storage. (https://github.com/near/near-sdk-as/blob/master/sdk-core/assembly/storage.ts)
- To create:
import { PersistentMap } from "near-sdk-as";
let map = new PersistentMap<string, string>("pmap"); // choose a unique prefix per collection
- To use:
map.set(key, value);
map.getSome(key);
Note: The Map doesn’t store keys, so if you need to retrieve them, include keys in the values.
… 还有多个,请查看原文。
PersistentDeque: Built on top of the Storage class, this implements a persistent bidirectional queue / double-ended queue / deque.
AVLTree:Implements a Tree Map based on AVL-tree Keys are ordered and iterable.
Rust 集合类型 Rust Collection Types
同上,相关结构,请查看原文!
Storage Staking
When you deploy a smart contract to NEAR, you pay for the storage that this contract requires using a mechanism called storage staking.
当您将智能合约部署到NEAR时,您需要使用一种称为storage staking的机制来支付该合约所需的存储费用。
In storage staking (sometimes called state staking), the account that owns a smart contract must stake (or lock) tokens according to the amount of data stored in that smart contract, effectively reducing the balance of the contract’s account.
在 storage staking(有时称为 state staking)中,拥有智能合约的帐户必须根据该智能合约中存储的数据量来stake (或锁定)令牌,从而有效地减少了合约帐户的余额。
如果您熟悉以太坊的定价模型,您可能会知道,该协议像NEAR一样,为每笔交易收取费用(称为“加油费”)。 与NEAR不同,以太坊的汽油费占了通过该交易存储的数据量。 从本质上讲,这意味着任何人都可以支付一次就可以在链上存储永久数据。 这是一种糟糕的经济设计,至少有两个原因:1.没有适当地激励运行网络的人(以太坊1的矿工)存储大量数据,因为在遥远的过去收取了高昂的汽油费 可能会永远增加存储成本,并且2.智能合约的用户需要为他们发送的存储在其中的数据付费,而不是向智能合约的所有者收费。
NEAR的设计如何与激励措施保持一致?
Storage-staked tokens are unavailable for other uses, such as validation staking. This increases the yield that validators will receive. Learn more in the economics whitepaper.
Storage-staked 的令牌不可用于其他用途, 例如验证抵押。 这增加了验证者将获得的产量。 在经济学白皮书(https://near.org/papers/economics-in-sharded-blockchain/)中了解更多信息。
tokens 什么时候可以抵押
On each incoming transaction that adds data.
让我们来看一个例子:
-
You launch a guest book app, deploying your app’s smart contract to the account example.near
您启动一个留言簿应用程序(https://examples.near.org/guest-book),将您应用程序的智能合约部署到该帐户 example.near 中。 -
Visitors to your app can add messages to the guest book. This means your users will, by default, pay a small gas fee to send their message to your contract.
应用程序的访问者可以将消息添加到留言簿。 这意味着,默认情况下,您的用户将支付少量汽油费,以将其消息发送到您的合约中。 -
When such a call comes in, NEAR will check that example.near has a large enough balance that it can stake an amount to cover the new storage needs. If it does not, the transaction will fail.
接到这样的调用时,NEAR将检查该示例。near的余额足够大,因此可以放款以满足新的存储需求。 否则,交易将失败。
“百万廉价数据添加”攻击 The “million cheap data additions” attack
Note that this can create an attack surface. To continue the example above, if sending data to your guest book costs users close to nothing while costing the contract owner significantly more, then a malicious user can exploit the imbalance to make maintaining the contract prohibitively expensive.
请注意,这可能会创建攻击面。 继续上面的示例,如果将数据发送到您的留言簿使用户几乎不付出任何代价,同时又使合约所有者付出了更多成本,那么恶意用户可以利用这种不平衡使合约的维护成本过高。
btw, you can remove data to unstake some tokens
顺便说一句,您可以删除数据以取消使用某些令牌
How much does it cost?
torage staking is priced in an amount set by the network, which is set to 1E19 yoctoNEAR per byte, or 100kb per NEAR token (Ⓝ). [1] [2]
存储抵押的定价由网络设置,设置为每个字节1E19 yoctoNEAR或每个NEAR令牌(Ⓝ)100kb。 [1] [2]
This value may change in the future. NEAR’s JSON RPC API provides a way to query this initial setting, but does not yet provide a way to query the “live” configuration value. Before it changes, this document will be updated to include information about how to query the live version.
此值将来可能会更改。 NEAR的JSON RPC API提供了一种查询此初始设置的方法,但尚未提供查询“实时”配置值的方法。 在更改之前,本文档将进行更新以包括有关如何查询实时版本的信息。
示例费用细目
TODO ,暂不翻译,查看原文!
其他降低成本的方法
对于网络运行人员来说,在链上存储数据并不便宜,NEAR会将这笔费用转嫁给了开发人员。那么,作为开发人员,您如何降低成本呢?有两种常见的测量方法:
- 使用二进制序列化格式,而不是JSON
- 离线存储数据
一个更好的策略可能是存储数据外链。如果你想保持分散的应用程序,一个流行的关链数据存储解决方案是IPFS。有了这个,你可以表示任何一组可预测的内容地址等数据:
QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG