Smart Contract Language Solidity Tutorial Series 6-Structure and mapping

Source: Internet
Author: User

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 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, which is part of subscribing to the Blockchain technology column.

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 with two membersstruct funder{Address addr;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 as a variable returns        //Create a struct instance, stored in storage, into mappingCampaigns[campaignid]= Campaign(Beneficiary,Goal, 0, 0);    }    function Contribute(UINT Campaignid) PublicPayable{Campaign Storage C=Campaigns[campaignid];        //Create a struct-body 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 PublicBalances;    function Update(UINT newbalance) Public {balances[msg.Sender]=Newbalance;    }}Contract Mappinguser{    function F() Public returns(UINT){Mappingexample m= New Mappingexample();        m.Update( -);        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

Blockchain-Learn blockchain to build 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.