current position:Home>Does solid have a simple and universal storage model?

Does solid have a simple and universal storage model?

2022-02-03 20:47:20 Q & A of Denglian community

about Solidity beginners , Simply and appropriately using data structures to organize data is a challenge . For the data organization on the blockchain , Is there a simple and general model to help us with the data on the chain ?




Take the answer 1:

about Solidity beginners , Simply and appropriately using data structures to organize data is a challenge . For the data organization on the blockchain , Is there a simple and general model to help us with the data on the chain ?

Here are some Simple and useful patterns , Arrange in increasing order of commonality .

For brevity , The event log is omitted . In practice , Want to send an event for each important state change .

Use arrays to represent simple lists

advantage

  • Reliable chronological order
  • Provide count
  • By index id Random access ( No key)

shortcoming

  • You can't use any key visit
  • There is no guarantee of uniqueness
  • Do not check for duplicates
  • Uncontrolled growth of elements

give an example :

pragma solidity ^0.4.6;contract simpleList {  struct EntityStruct {    address entityAddress;    uint entityData;    // more fields  }  EntityStruct[] public entityStructs;  function newEntity(address entityAddress, uint entityData) public returns(uint rowNumber) {    EntityStruct memory newEntity;    newEntity.entityAddress = entityAddress;    newEntity.entityData    = entityData;    return entityStructs.push(newEntity)-1;  }  function getEntityCount() public constant returns(uint entityCount) {    return entityStructs.length;  }}

Use structure in mapping (Mapping with Struct)

advantage

  • Through unique key(id) Do random interviews
  • key Guarantee of uniqueness
  • At every “ Record ” Contains arrays , mapping , Structure

shortcoming

  • Cannot enumerate keys
  • Cannot count keys
  • Manual checks are required to distinguish between default values and explicit values “ whole 0” Record

give an example :

contract mappingWithStruct {  struct EntityStruct {    uint entityData;    bool isEntity;  }  mapping (address => EntityStruct) public entityStructs;  function isEntity(address entityAddress) public constant returns(bool isIndeed) {    return entityStructs[entityAddress].isEntity;  }  function newEntity(address entityAddress, uint entityData) public returns(bool success) {    if(isEntity(entityAddress)) throw;     entityStructs[entityAddress].entityData = entityData;    entityStructs[entityAddress].isEntity = true;    return true;  }  function deleteEntity(address entityAddress) public returns(bool success) {    if(!isEntity(entityAddress)) throw;    entityStructs[entityAddress].isEntity = false;    return true;  }  function updateEntity(address entityAddress, uint entityData) public returns(bool success) {    if(!isEntity(entityAddress)) throw;    entityStructs[entityAddress].entityData = entityData;    return true;  }}

Use with unique ID Array of structs

advantage

  • Press the subscript ( Line number ) Random access

  • Guarantee Id Uniqueness

  • With each “ Record ” Contains arrays , Mapping and structure

  • Random access by Row number

  • Assurance of Id uniqueness

  • Enclose arrays, mappings and structs with each "record"

shortcoming

  • Random access is not supported
  • Uncontrolled growth of the list

give an example :

contract arrayWithUniqueIds {  struct EntityStruct {    address entityAddress;   //   only ID    uint entityData;  }  EntityStruct[] public entityStructs;  mapping(address => bool) knownEntity;  function isEntity(address entityAddress) public constant returns(bool isIndeed) {    return knownEntity[entityAddress];  }  function getEntityCount() public constant returns(uint entityCount) {    return entityStructs.length;  }  function newEntity(address entityAddress, uint entityData) public returns(uint rowNumber) {    if(isEntity(entityAddress)) throw;    EntityStruct memory newEntity;    newEntity.entityAddress = entityAddress;    newEntity.entityData = entityData;    knownEntity[entityAddress] = true;    return entityStructs.push(newEntity) - 1;  }  function updateEntity(uint rowNumber, address entityAddress, uint entityData) public returns(bool success) {    if(!isEntity(entityAddress)) throw;    if(entityStructs[rowNumber].entityAddress != entityAddress) throw;    entityStructs[rowNumber].entityData    = entityData;    return true;  }}

Structure mapping with index

advantage

  • Through unique ID Or line number ( Index subscript ) Random access
  • Guarantee Id Uniqueness
  • At every “ Record ” Contains arrays , Mapping and structure
  • Maintain the order of element declarations
  • The number of records can be counted
  • Each item can be enumerated
  • By setting Boolean values ” soft “ Delete the project

shortcoming

  • Uncontrolled growth of the list

give an example :

contract MappedStructsWithIndex {  struct EntityStruct {    uint entityData;    bool isEntity;  }  mapping(address => EntityStruct) public entityStructs;  address[] public entityList;  function isEntity(address entityAddress) public constant returns(bool isIndeed) {      return entityStructs[entityAddress].isEntity;  }  function getEntityCount() public constant returns(uint entityCount) {    return entityList.length;  }  function newEntity(address entityAddress, uint entityData) public returns(uint rowNumber) {    if(isEntity(entityAddress)) throw;    entityStructs[entityAddress].entityData = entityData;    entityStructs[entityAddress].isEntity = true;    return entityList.push(entityAddress) - 1;  }  function updateEntity(address entityAddress, uint entityData) public returns(bool success) {    if(!isEntity(entityAddress)) throw;    entityStructs[entityAddress].entityData    = entityData;    return true;  }}

The structure mapping of the index can be deleted

advantage

  • Through unique ID Or random access by line number
  • Guarantee Id Uniqueness
  • At every “ Record ” Contains arrays , Mapping and structure
  • Count the number of records
  • enumerable ID
  • Use the delete function to logically control the size of the list

shortcoming

  • Increased code complexity
  • Storage costs are higher
  • The key list is unordered

2019 to update

stay Solidity 0.5.1, This pattern already has a library implementation Solidity We will increase, delete, and change inspections - Blog UnorderedKeySet GitHub library

give an example :

contract mappedWithUnorderedIndexAndDelete {  struct EntityStruct {    uint entityData;    uint listPointer;  //  Record the location of the element   }  mapping(address => EntityStruct) public entityStructs;  address[] public entityList;  function isEntity(address entityAddress) public constant returns(bool isIndeed) {    if(entityList.length == 0) return false;    return (entityList[entityStructs[entityAddress].listPointer] == entityAddress);  }  function getEntityCount() public constant returns(uint entityCount) {    return entityList.length;  }  function newEntity(address entityAddress, uint entityData) public returns(bool success) {    if(isEntity(entityAddress)) throw;    entityStructs[entityAddress].entityData = entityData;    entityStructs[entityAddress].listPointer = entityList.push(entityAddress) - 1;    return true;  }  function updateEntity(address entityAddress, uint entityData) public returns(bool success) {    if(!isEntity(entityAddress)) throw;    entityStructs[entityAddress].entityData = entityData;    return true;  }  function deleteEntity(address entityAddress) public returns(bool success) {    if(!isEntity(entityAddress)) throw;    uint rowToDelete = entityStructs[entityAddress].listPointer;    address keyToMove   = entityList[entityList.length-1];    entityList[rowToDelete] = keyToMove;  //  The last element is placed at the position of the deleted array     entityStructs[keyToMove].listPointer = rowToDelete;    entityList.length--;    return true;  }}

The last one here has one explain as well as This explanation

also How to be in Solidity in Store folders or object trees ?

One more Solidity Double linked list :linkedList.sol

Original Q & a link :https://ethereum.stackexchange.com/questions/13167/are-there-well-solved-and-simple-storage-patterns-for-solidity

The Q & A is organized by the blockchain community members in simple terms .

Block chain in depth - Systematically learn blockchain , All of the school district block chains are here , Create the best blockchain technology blog .




copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202032047168874.html

Random recommended