Data Persistence

1、构建环境。

cd /home/ubuntu/contracts
mkdir addressbook
cd addressbook
touch addressbook.cpp

2、新建合约,有两要点注意

  1)class 与类名间添加 [[eosio::contract]]

  2)新类继承于 public eosio::contract

#include <eosiolib/eosio.hpp>

using namespace eosio;

class [[eosio::contract]] addressbook : public eosio::contract {
public: private: };

3、创建Table Record 结构. 注意在 struct 和 name 之间,要加上 [[eosio::table]].

struct [[eosio::table]] person {
name key;
string first_name;
string last_name;
string street;
string city;
string state; uint64_t primary_key() const { return key.value;}
};

4、定义表类型

typedef eosio::multi_index<"people"_n, person> address_index;

  1)Use the _n operator to define an eosio::name type and use that name the table. This table contains a number of different singular "persons", so name the table "people".

    _n是一个操作符,将"people"的 char* 类型转为 eosio::name类型。"people"即是表的名字。

  2)Pass in the singular person struct defined in the previous step.

      person是表的类型.

5、构造函数

addressbook(name receiver, name code,  datastream<const char*> ds):contract(receiver, code, ds) {}

  code parameter which is the account on the blockchain that the contract is being deployed to.

  code参数是发布合约的账户.

6、使用 require_auth 限制每个账户只能操作自己的数据。

void upsert(name user, std::string first_name, std::string last_name, std::string street, std::string city, std::string state) {
require_auth( user );
}

  utilize the require_auth method provided by the eosio.cdt. This method accepts one argument, an nametype, and asserts that the account executing the transaction equals the provided value.

7、实例化 Table。

  

void upsert(name user, std::string first_name, std::string last_name, std::string street, std::string city, std::string state) {
require_auth( user );
address_index addresses(_code, _code.value);
}

  addresses()构造函数中的两个参数分别是下面的意思:

  1. The "code", which represents the contract's account. This value is accessible through the scoped _code variable.
  2. The "scope" which make sure the uniqueness of the contract. In this case, since we only have one table we can use "_code" as well

8、使用 find 查找目标

void upsert(name user, std::string first_name, std::string last_name, std::string street, std::string city, std::string state) {
require_auth( user );
address_index addresses(_code, _code.value);
auto iterator = addresses.find(user.value);
if( iterator == addresses.end() )
{
//The user isn't in the table
}
else {
//The user is in the table
}
}

9、使用multi_index的emplace方法插入。

void upsert(name user, std::string first_name, std::string last_name, std::string street, std::string city, std::string state) {
require_auth( user );
address_index addresses(_code, _code.value);
auto iterator = addresses.find(user.value);
if( iterator == addresses.end() )
{
addresses.emplace(user, [&]( auto& row ) {
row.key = user;
row.first_name = first_name;
row.last_name = last_name;
row.street = street;
row.city = city;
row.state = state;
});
}
else {
//The user is in the table
}
}

  emplace接受两个参数:the "scope" of this record and a callback function.

9、update数据

else {
std::string changes;
addresses.modify(iterator, user, [&]( auto& row ) {
row.key = user;
row.first_name = first_name;
row.last_name = last_name;
row.street = street;
row.city = city;
row.state = state;
});
}

  第二个参数是scope。

10、erase 方法

void erase(name user) {
require_auth(user);
address_index addresses(_code, _code.value);
auto iterator = addresses.find(user.value);
eosio_assert(iterator != addresses.end(), "Record does not exist");
addresses.erase(iterator);
}

11、编译

eosio-cpp -o addressbook.wasm addressbook.cpp --abigen

12、Deploy the Contract

cleos create account eosio addressbook XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX -p eosio@active

cleos set contract addressbook /home/ubuntu/contracts/addressbook -p addressbook@active

13、Test the Contract

cleos push action addressbook upsert '["alice", "alice", "liddell", "123 drink me way", "wonderland", "amsterdam"]' -p alice@active

  Check that alice cannot add records for another user.

cleos push action addressbook upsert '["bob", "bob", "is a loser", "doesnt exist", "somewhere", "someplace"]' -p alice@active

  查看table

cleos get table addressbook addressbook people --lower alice --limit 

  查看 cleos 查看 table的命令

cleos get table addressbook addressbook people --lower alice --limit 

  

参考:

1、https://developers.eos.io/eosio-home/docs/data-persistence

Data Persistence的更多相关文章

  1. [Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular

    Communicating with a remote server via HTTP presents an extra level of complexity as there is an inc ...

  2. [Spring boot] Configuring and Accessing a Data Source

    We need our data persistence with configuring our datasouce: In application.properties: spring.h2.co ...

  3. Azure Redis Cache (1) 入门

    <Windows Azure Platform 系列文章目录> Microsoft Azure Redis Cache基于流行的开源Redis Cache. 1.功能 Redis 是一种高 ...

  4. Azure Redis Cache (3) 创建和使用P级别的Redis Cache

    <Windows Azure Platform 系列文章目录> 在笔者之前的文档里面已经说明了,Azure Redis Cache分为三个不同的级别: - 基本,Basic,不包含SLA ...

  5. ios 在程序中使用iCloud

    注意,这里说的使用icould不是用icloud进行系统备份,那个功能不需要我们写代码,备份到icloud的东西我们也不能操作.我们指的是以下这3种icloud使用方法: 这里有3中使用方法, Key ...

  6. android和ubifs

    原文地址: http://opendevkit.com/?e=37 1. ubifs号称性能比yaffs2 好,同时压缩可读写,文件系统image体较小同时可写,相当不错 2. ubifs制作 (1) ...

  7. iphone dev 入门实例4:CoreData入门

    The iPhone Core Data Example Application The application developed in this chapter will take the for ...

  8. MongoDB - Introduction to MongoDB

    MongoDB is an open-source document database that provides high performance, high availability, and a ...

  9. Conclusion

    Conclusion This concludes our brief look at building a simple, but fully functional, Zend Framework ...

随机推荐

  1. matlab学习(4) any 和cellfun用法

    1.对于向量来说,只要包含非0元素,则返回为1: 2.对于矩阵来说,any(X)依次判断X的每一列是否为ture,返回一个含0或1的向量. 3.any(X,DIM)对X的第DIM维操作, DIM=1即 ...

  2. Linux下使用Nginx代理访问json文件报404错误

    在网上看了很多,都说是IIS的问题,关键是使用servlet就可以正常访问,使用Nginx就不行,最后发现是其他问题,解决方案如下: 1.确认配置的路径是否正确,Nginx代理的路径和你访问的路径. ...

  3. 《Linux内核原理与分析》第五周作业

    课本:第4章 系统调用的三层机制(上) -用户态.内核态和中断 -用户态:在低的执行级别下,代码能够掌控的范围有所限制,只能访问部分内存. -内核态:在高的执行级别下,代码可以执行特权指令,访问任意的 ...

  4. @angular/cli 工程 项目 配置

    如果你创建工程项目用angular-cli 你的样式文件想使用scss, 并且让ng serve自动编译它们 你可以在创建项目的时候用 ng new sassy-project --style=sas ...

  5. Java高级特性 第5节 序列化和、反射机制

    一.序列化 1.序列化概述 在实际开发中,经常需要将对象的信息保存到磁盘中便于检索,但通过前面输入输出流的方法逐一对对象的属性信息进行操作,很繁琐并容易出错,而序列化提供了轻松解决这个问题的快捷方法. ...

  6. easyui-datebox 点击事件

    <div class="form-group col-xs-5 col-md-5 col-lg-6" style = "margin-left: 0px;" ...

  7. 迅雷磁力链接转BT种子工具

    种子文件目录:C:\Users\jifeng\AppData\Local\Temp\magnetex MagnetEx.exe 从迅雷5.8支持磁力链接的无视受限资源版提取 MagnetEx.exe ...

  8. 黄聪:详解申请微信h5支付方法,开通微信h5网页支付接口(转)

    版权声明:图文并茂的微信小程序教程!欢迎转载,请保留作者名字和链接:商业合作请联系子恒老师助理 QQ : 2334512685 https://blog.csdn.net/towtotow/artic ...

  9. IDEA忽略某些文件

    Settings→Editor→File Types 在下方的忽略文件和目录(Ignore files and folders)中添加自己需要过滤的内容   下图为我自己添加过滤的内容,例如:*.im ...

  10. Web App Checklist

    Mobile Web App checklist 目标: 高性能Mobile Web App 一.UX和UI 操作节目与边框之间留空隙: 防止操作过程中,触发系统缺省行为,有些是无法disable的. ...