In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve persistence of block chain data in EOS development". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Step 1: create a new directory
You created a contract catalog before, and it starts there now.
/ / shellcd CONTRACTS_DIR
Create a new directory for our contract and enter the directory:
/ / c++mkdir addressbookcd addressbook step 2: create and open a new file / / c++touch addressbook.cpp
Open the file in your favorite editor.
Step 3: write an extended standard class and include EOSIO
In the previous tutorial, you created a hello world contract and learned the basics. You will be familiar with the following structure, which has been named addressbook.
/ / c++#include # include using namespace eosio;class addressbook: public eosio::contract {public: private:}; step 4: create a data structure for the table
Before configuring and instantiating the table, you need to write a structure that represents the address book data structure. Think of this as "schema". Because it is an address book, the table will contain people, so create a struct named "person".
/ / c++struct person {}
When defining the schema of the multi_ index table, you need to use a unique value as the primary key.
For this contract, use a field named "key" of type account_name. This contract will provide a unique entry for each user, so the key will be consistent and have a unique value based on the user's account_name.
/ / c++struct person {account_name key;}
Because the contract is an address book, you should probably store some relevant details for each entry or person.
/ / c++struct person {account_name key; string first_name; string last_name; string street; string city; string state;}
Ok . The basic schema architecture is now complete. Next, define a primary_key method that will be used by the multi_index iterator. Each multi_index schema requires a primary key. To do this, simply create a method called primary_key () and return a value, in this case, the member key defined in the structure.
/ / c++struct person {account_name key; string first_name; string last_name; string street; string city; string state; uint64_t primary_key () const {return key;}}; step 5: configure multiple index tables
Now that we have defined the schema of the table using the structure, we need to configure the table. The eosio::multi_index constructor needs to be named and configured to use the structure we defined earlier.
/ / c++typedef eosio::multi_index address_index
We name N (N (base32 X), which is used to generate compile-time uint64_t from the base32-encoded string interpretation of X) as a table. The table contains many different individuals "persons", so the table is named "people".
Pass in the single person structure defined in the previous step
Declare the type of this table. This type will be used to instantiate this table later.
/ / c++//configure the tabletypedef eosio::multi_index address_index
With the above multi_index configuration, there is a multi_ index table named people, which is based on the schema or data structure of a single row of the table using struct person.
So far, our documents should be like this.
/ / c++#include # include using namespace eosio;class addressbook: public eosio::contract {public: private: struct [[eosio::table]] person {account_name key; std::string first_name; std::string last_name; std::string street; std::string city; std::string state; uint64_t primary_key () const {return key;}}; typedef eosio::multi_index address_index }; step 6: constructor
When using C++ classes, the first public method you should create is the constructor.
Our constructor will be responsible for setting up the contract initially.
The EOSIO contract expands the contract category. Initialize our parent contract class with contract scope. The scope argument passed by our constructor is the account on the block chain where the contract is being deployed.
/ / c++addressbook (account_name self): contract (self) {} step 7: add records to the table
Previously, the primary key of a multi-index table was defined as enforcing this contract to store only one record per user. In order to make it all work, you need to establish some assumptions about the design.
The only account that authorizes the modification of the address book is the user.
The primary_key of our table is unique, based on the user name.
For availability, contracts should be able to create and modify table rows through a single operation.
In eosio, the blockchain has a unique account, so account_name is an ideal candidate for primary_key in this particular use case. The account_name type is uint64_t.
Next, define the action for the user to add or update records. This operation needs to accept any values that it needs to be able to place (create) or modify.
Format the definition to make it easier to read. To simplify the user experience and interface, there is a method responsible for creating and modifying rows. Therefore, it is named "upsert", which is the combination of "update" and "insert".
/ / c++void upsert (account_name user, std::string first_name, std::string last_name, std::string street, std::string city, std::string state) {}
Earlier, it was mentioned that only users can control their records because the contract is opted to join. To do this, use the require_auth method provided by eosio.cdt. This method takes one parameter, the account_name type, and asserts that the account performing the transaction is equal to the value provided.
/ c++void upsert (account_name user, std::string first_name, std::string last_name, std::string street, std::string city, std::string state) {require_auth (user);}
Instantiate the table. Previously, the multi_ index table was configured and declared as address_index. To instantiate a table, consider these two required parameters:
"code" represents the account of the contract. You can access this value through the scope _ self variable.
Define the scope "scope" of the contract payer, and the contract in this use case is responsible for paying ram fees.
/ c++void upsert (account_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 (_ self, _ self);}
Next, query the iterator and set it as a variable, because this iterator will be used multiple times.
/ / c++void upsert (account_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 (_ self, _ self); auto iterator = addresses.find (user);}
Security has been established and tables are instantiated. Great!
Next, write the logic used to create or modify the table. Detects whether a specific user already exists.
To do this, use the find method of the table by passing the user parameter. The find method returns an iterator. Use this iterator to test the end method. The end method is an alias for "null".
/ c++void upsert (account_name user, std::string first_name, std::string last_name, std::string street, std::string city, std::string state) {require_auth (user); auto iterator = addresses.find (user); address_index addresses (_ self, _ self); if (addresses.find (user) = = addresses.end ()) {/ / The user isn't in the table} else {/ / The user is in the table}}
Use the multi_index method emplace to create a record in the table. This method accepts two parameters, the range "scope" of this record and the callback function.
The callback function of the emplace method must use lamba to create the interface. Assign the value of the row and the value provided to upsert in body.
/ c++void upsert (account_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 (_ self, _ self); auto iterator = addresses.find (user); 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}}
Next, deal with the modification or update of the "upsert" function. Using the modify method, pass some parameters
The iterator defined earlier is set to the declared user when this operation is called.
The scope "scope" or "ram payer" ram consumer, in this case, is the user, as previously decided when proposing the design of the contract.
The callback function is used to handle table modifications.
/ c++void upsert (account_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 (_ self, _ self); auto iterator = addresses.find (user); 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 {addresses.modify (iterator, user, [&] (auto& row) {row.first_name = first_name; row.last_name = last_name; row.street = street; row.city = city; row.state = state;});}}
The address book contract now has a functional operation that allows the user to create a row in the table if the record does not already exist and modify it if it already exists.
This is the end of the content of "how to achieve the persistence of blockchain data in EOS development". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.