Smart Contract Language Solidity Tutorial Series 6-Structure and mapping

Source: Internet
Author: User

Solidity Tutorial Series 6th-solidity structure and mapping.
For a complete list of articles in the solidity series, see Category-solidity.

Write in front

Solidity is the Ethereum Smart Contract programming language, you should know about Ethereum and smart contracts before reading this article.
If you don't understand, it's recommended that you look at Ethereum first.

Part of this article series is a reference to the solidity official document (currently the latest version: 0.4.20) for translation, and the other part is solidity in-depth analysis.

Structural Body (Structs)

Solidity provides a struct to define a custom type, and a custom type is a reference type.
Let's take a look at the following example:

Pragma solidity ^0.4.11;contract Crowdfunding {//define a new type of struct funder {address addr with two members;    UINT Amount;        } struct Campaign {address beneficiary;        UINT Fundinggoal;        UINT Numfunders;        UINT Amount;    Mapping (UINT = funder) funders;    } UINT Numcampaigns;    Mapping (UINT = Campaign) campaigns; function newcampaign (address beneficiary, UINT goal) public returns (UINT Campaignid) {Campaignid = numcampaigns++ ; Campaignid Returns//creates a struct instance as a variable, stored in storage, put into mapping campaigns[campaignid] = Campaign (Beneficiary, Goa    L, 0, 0);        } function contribute (UINT Campaignid) public payable {Campaign storage c = Campaigns[campaignid];        Creating a struct reference with the mapping counterpart//can also be initialized with funder (Msg.sender, Msg.value).        c.funders[c.numfunders++] = Funder ({addr:msg.sender, amount:msg.value});    C.amount + = Msg.value; } function checkgoalreached (UINT Campaignid) public returns (BOOL reached) {Campaign storage c = Campaigns[campaignid];        if (C.amount < c.fundinggoal) return false;        UINT amount = C.amount;        C.amount = 0;        C.beneficiary.transfer (amount);    return true; }}

The above is a simplified version of the crowdfunding contract, but it allows us to understand the basic concepts of structs , which can be used for mapping and arrays as elements. It can also contain types such as mappings and arrays.

A struct cannot be declared as a member of its own struct at the same time, and this restriction is based on the size of the struct must be finite.
However, a struct can be a member of a value type mapping .

Note that in a function, assigning a struct to a local variable (by default, the storage type) is actually a copy of the reference, so modifying the value of the local variable will affect the original variable.

Of course, you can also modify the value directly by accessing the member without necessarily assigning a value to a local variable, such as Campaigns[campaignid].amount = 0

Mapping (Mappings)

A mapping type, a mapping relational storage structure for a key-value pair. Defined as mapping (_keytype = _keyvalue). The key type allows almost all types () except mappings, variable-length arrays, contracts, enumerations, and structs. There are no restrictions on value types, and you can include mapping types for any type.

mappings can be seen as a hash table, where all possible keys are created by virtualization and mapped to a type's default value (binary full zero representation). In the mapping table, the data of the key is not stored, only its keccak256 hash value is stored, and this hash value needs to be used to find the value.
Because of this, the mappings are not of length, and there is no concept of a key collection or a collection of values.

A mapping type that can be used only as a state variable, or as a reference to an storage type in an intrinsic function.

You can have solidity create an accessor by marking the map as public. The corresponding value is returned by providing a key value to access it as a parameter.
The value type of the map can also be a map, and the process is repeated by providing the key corresponding to the mapped value when accessed using the accessor.
Take a look at an example:

pragma solidity ^0.4.0;contract MappingExample {    mapping(address => uint) public balances;    function update(uint newBalance) public {        balances[msg.sender] = newBalance;    }}contract MappingUser {    function f() public returns (uint) {        MappingExample m = new MappingExample();        m.update(100);        return m.balances(this);    }}

Attention:
The mapping does not provide a way to iterate over the output and can implement one of these data structures on its own. Reference iterable Mapping

Reference documents

Solidity official documents

In-depth blockchain-the system learns blockchain to create the best blockchain technology blog.

Smart Contract Language Solidity Tutorial Series 6-Structure and mapping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.