DynamicArray – 动态卡雷区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,DynamicArray – 动态卡雷区块链毕设代写 是一篇很好的国外资料

DynamicArray

A Solidity Library for managing dynamic array of primitive types.

⚠️ Dynamic array of type bytes32 (Bytes32Array), address (AddressArray) and uint256 (UintArray) are supported.

Roadmap

  1. Initial Implementation ()
  2. TypeScript ()
  3. Improve shrink
    • Confirm: modifying length of dynamic array on storage will/won’t erase stale area? ()
    • Gas costly cheap?
  4. Use Assembly
    • Bounding check is accomplished by accessing array element by default. It’s safe without it. Use low-level Assembly to skip it.
  5. Support types narrower than 32 bytes
    • The architecture is not gas costly effetive for primitive types smaller than 16 bytes. Any idea? ?

Description

The goal is to build a dynamically sized array similar to C++ std::vector. Storage space is expensive on Ethereum; therefore, it is important to delete items when possible.

Write a Solidity library that implements “DynamicArray” with the following methods. You’ll have to fix the signatures to use the appropriate types and modifiers in Solidity.

  • set(array, position, value): overwrite position in array with value
  • get(array, position): return value at position in array. Exception if position is out of bounds.
  • push(array, value): add value to end of array, increase array size
  • pop(array): remove and return value at end of array, decrease array size
  • size(array): return the current size of array
  • capacity(array): return the total storage size of array
  • clear(array): set size of array to 0
  • shrink(array): delete items from array until size == capacity. This is the only time data is deleted.

For example, if you push 10 values, then size == 10 and capacity == 10. If you pop 5 values, then size == 5, capacity == 10. If you try to get(array, 7) then that should raise an error because 7 exceeds the size, even though it is within the capacity.

For inspiration, take a look at EnumerableMap and EnumerableSet from OpenZeppelin. Notice they use bytes32 as the underlying storage type, but expose type-specific functions for uint256 and address.

Usage

// SPDX-License-Identifier: MIT  pragma solidity ^0.8.0;  import "../DynamicArray.sol";  contract AddressBook {   using DynamicArray for DynamicArray.AddressArray;    DynamicArray.AddressArray private book;    function addAddress(address account) external {     book.push(account);   }    function getAddressAt(uint256 index) external view returns (address) {     return book.get(index);   }    function replaceAddressAt(uint256 index, address account) external {     book.set(index, account);   }    function removeAddressAtLast() external returns (address) {     return book.pop();   }    function numberOfAddresses() external view returns (uint256) {     return book.size();   }    function sizeOfBook() external view returns (uint256) {     return book.capacity();   }    function clearBook() external {     book.clear();   }    function deleteBook() external {     book.clear();     book.shrink();   } } 

Development

Configuration

npm install 

Test

npx hardhat test 
  Contract: DynamicArray     DynamicBytes32Array       √ starts empty       set         √ reverts when setting at invalid position (54ms)         √ overwrites an existing position (63ms)       get         √ reverts when getting at invalid position         √ gets an existing position       push         √ adds at end (38ms)         √ adds at end but doesn't occupy new storage when it's been occupied already (53ms)       pop         √ removes at end and doesn't delete storage         √ reverts when popping in empty       size         √ returns size         √ may be different with capacity       capacity         √ returns capacity         √ may be different with size       clear         √ sets the size to 0, and doesn't delete storage       shrink         √ deletes stale storage (61ms)     DynamicUintArray       √ starts empty       set         √ reverts when setting at invalid position         √ overwrites an existing position       get         √ reverts when getting at invalid position         √ gets an existing position       push         √ adds at end         √ adds at end but doesn't occupy new storage when it's been occupied already (43ms)       pop         √ removes at end and doesn't delete storage         √ reverts when popping in empty       size         √ returns size         √ may be different with capacity       capacity         √ returns capacity         √ may be different with size       clear         √ sets the size to 0, and doesn't delete storage       shrink         √ deletes stale storage (49ms)     DynamicAddressArray       √ starts empty       set         √ reverts when setting at invalid position         √ overwrites an existing position       get         √ reverts when getting at invalid position         √ gets an existing position       push         √ adds at end         √ adds at end but doesn't occupy new storage when it's been occupied already (44ms)       pop         √ removes at end and doesn't delete storage         √ reverts when popping in empty       size         √ returns size         √ may be different with capacity       capacity         √ returns capacity         √ may be different with size       clear         √ sets the size to 0, and doesn't delete storage       shrink         √ deletes stale storage (50ms)     45 passing (2s) 

DynamicArray

A Solidity Library for managing dynamic array of primitive types.

⚠️ Dynamic array of type bytes32 (Bytes32Array), address (AddressArray) and uint256 (UintArray) are supported.

Roadmap

  1. Initial Implementation ()
  2. TypeScript ()
  3. Improve shrink
    • Confirm: modifying length of dynamic array on storage will/won’t erase stale area? ()
    • Gas costly cheap?
  4. Use Assembly
    • Bounding check is accomplished by accessing array element by default. It’s safe without it. Use low-level Assembly to skip it.
  5. Support types narrower than 32 bytes
    • The architecture is not gas costly effetive for primitive types smaller than 16 bytes. Any idea? ?

Description

The goal is to build a dynamically sized array similar to C++ std::vector. Storage space is expensive on Ethereum; therefore, it is important to delete items when possible.

Write a Solidity library that implements “DynamicArray” with the following methods. You’ll have to fix the signatures to use the appropriate types and modifiers in Solidity.

  • set(array, position, value): overwrite position in array with value
  • get(array, position): return value at position in array. Exception if position is out of bounds.
  • push(array, value): add value to end of array, increase array size
  • pop(array): remove and return value at end of array, decrease array size
  • size(array): return the current size of array
  • capacity(array): return the total storage size of array
  • clear(array): set size of array to 0
  • shrink(array): delete items from array until size == capacity. This is the only time data is deleted.

For example, if you push 10 values, then size == 10 and capacity == 10. If you pop 5 values, then size == 5, capacity == 10. If you try to get(array, 7) then that should raise an error because 7 exceeds the size, even though it is within the capacity.

For inspiration, take a look at EnumerableMap and EnumerableSet from OpenZeppelin. Notice they use bytes32 as the underlying storage type, but expose type-specific functions for uint256 and address.

Usage

// SPDX-License-Identifier: MIT  pragma solidity ^0.8.0;  import "../DynamicArray.sol";  contract AddressBook {   using DynamicArray for DynamicArray.AddressArray;    DynamicArray.AddressArray private book;    function addAddress(address account) external {     book.push(account);   }    function getAddressAt(uint256 index) external view returns (address) {     return book.get(index);   }    function replaceAddressAt(uint256 index, address account) external {     book.set(index, account);   }    function removeAddressAtLast() external returns (address) {     return book.pop();   }    function numberOfAddresses() external view returns (uint256) {     return book.size();   }    function sizeOfBook() external view returns (uint256) {     return book.capacity();   }    function clearBook() external {     book.clear();   }    function deleteBook() external {     book.clear();     book.shrink();   } } 

Development

Configuration

npm install 

Test

npx hardhat test 
  Contract: DynamicArray     DynamicBytes32Array       √ starts empty       set         √ reverts when setting at invalid position (54ms)         √ overwrites an existing position (63ms)       get         √ reverts when getting at invalid position         √ gets an existing position       push         √ adds at end (38ms)         √ adds at end but doesn't occupy new storage when it's been occupied already (53ms)       pop         √ removes at end and doesn't delete storage         √ reverts when popping in empty       size         √ returns size         √ may be different with capacity       capacity         √ returns capacity         √ may be different with size       clear         √ sets the size to 0, and doesn't delete storage       shrink         √ deletes stale storage (61ms)     DynamicUintArray       √ starts empty       set         √ reverts when setting at invalid position         √ overwrites an existing position       get         √ reverts when getting at invalid position         √ gets an existing position       push         √ adds at end         √ adds at end but doesn't occupy new storage when it's been occupied already (43ms)       pop         √ removes at end and doesn't delete storage         √ reverts when popping in empty       size         √ returns size         √ may be different with capacity       capacity         √ returns capacity         √ may be different with size       clear         √ sets the size to 0, and doesn't delete storage       shrink         √ deletes stale storage (49ms)     DynamicAddressArray       √ starts empty       set         √ reverts when setting at invalid position         √ overwrites an existing position       get         √ reverts when getting at invalid position         √ gets an existing position       push         √ adds at end         √ adds at end but doesn't occupy new storage when it's been occupied already (44ms)       pop         √ removes at end and doesn't delete storage         √ reverts when popping in empty       size         √ returns size         √ may be different with capacity       capacity         √ returns capacity         √ may be different with size       clear         √ sets the size to 0, and doesn't delete storage       shrink         √ deletes stale storage (50ms)     45 passing (2s) 

部分转自网络,侵权联系删除区块链源码网

www.interchains.cc

https://www.interchains.cc/23260.html

区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 ! QQ3039046426
区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » DynamicArray – 动态卡雷区块链毕设代写

提供最优质的资源集合

立即查看 了解详情