Solidity的地址 数组如何判断是否包含一个给定的地址?
Q:
given address[] wallets. What is the correct method to check that the list contains a given address?
Does solidity provide any native list contains function?
If not, is the only way to do a full iteration of the array and check each element?
Are there more memory efficient ways of doing this, maybe a mapping?
A:
Solidity doesn't provide a contains method, you'd have to manually iterate and check.
Using an array for what you're trying to achieve would be a highly inefficient pattern. The best and most cost efficient method is use a mapping data structure. Set the key to be the address and the value to be a boolean. Lists that are too long have the possibility of running out of gas when you're trying to iterate over them.
If you need to iterate through all the keys in the mapping, then you'd need to have an external database to get all the keys. The database can be populated and updated based on events from the smart contract (i.e. an event when the address is added or removed).
最高效的方式不是 循环遍历数组(可能耗光所有的gas),而是 使用mapping,去做。
Example:
contract myWallets
{
mapping (address => bool) public Wallets; function setWallet(address _wallet) public{
Wallets[_wallet]=true;
} function contains(address _wallet) returns (bool){
return Wallets[_wallet];
}
}
Solidity的地址 数组如何判断是否包含一个给定的地址?的更多相关文章
- 项目SVN的IP地址发生变化时修改SVN为新的IP地址
在eclipse或者Myeclipse自带的svn:subclipse中修改ip地址 项目开发中有可能要修改SVN的IP地址,entries文件里面包含svn服务器的地址信息.每个文件夹都会产生一个e ...
- C#反射实现 C# 反射 判断类的延伸类型 使用代码生成工具Database2Sharp快速生成工作流模块控制器和视图代码 C# ADO.NET的SqlDataReader对象,判断是否包含指定字段 页面中添加锚点的几种方式 .net 简单实用Log4net(多个日志配置文件) C# 常用小点
C#反射实现 一.反射概念: 1.概念: 反射,通俗的讲就是我们在只知道一个对象的内部而不了解内部结构的情况下,通过反射这个技术可以使我们明确这个对象的内部实现. 在.NET中,反射是重要的机制, ...
- PHP 判断是否包含某字符串
PHP语言是一个功能强大的嵌入式HTML脚本语言,它的易用性让许多程序员选择使用.PHP判断字符串的包含,可以使用PHP的内置函数 strstr,strpos,stristr直接进行判断.也可以通过e ...
- PHP 判断是否包含在某个字符串中
1.用strpos函数,查找字符首次出现的位置,如果不存在则会返回false$str= 'abc';$needle= 'e';$pos = strpos($str, $needle);2.用strst ...
- JS判断一个字符串是否包含一个子串函数.
微信小程序 JS判断一个字符串是否包含一个子串函数. //str 字符串,name子串 contains:function(str,name){ if(str.indexOf( ...
- 【转载】C#中List集合使用Contains方法判断是否包含某个对象
在C#的List集合中,如果要查找List集合是否包含某一个值或者对象,如果不使用List集合类的扩展方法的话一般会使用for循环或者foreach遍历来查找,其实List集合类中的扩展方法Conta ...
- php 判断字符串包含
PHP语言是一个功能强大的嵌入式HTML脚本语言,它的易用性让许多程序员选择使用.PHP判断字符串的包含,可以使用PHP的内置函数 strstr,strpos,stristr直接进行判断.也可以通过e ...
- Javascript中数组的判断方法
摘要: 1.数组检测的方法: 1) typeof . 2) instanceof . 3) constructor . 4) Object.prototype.toString. 5) Array.i ...
- struts2 用if标签判断字符串包含
String testStr = "用来判断是否包含的字符串"; <s:property value="testStr"/> <s:if te ...
随机推荐
- 查看进程在CPU和内存占用的命令
1.使用top命令 输入M表示按内存排序,也就是RES这一列从大到小排序了 它占用了3.3%的内存,用134568除以4030416结果就是3.3左右 也就是说 总物理内存是3.84GB RES这一列 ...
- [JAVA]对象的别名问题
对于JAVA的基本数据类型,a=b就是把b的内容复制给a.若接着又修改了a,对b是没有影响的. 但是在为对象“赋值”的时候,情况发生了变化.对一个对象进行操作时,我们真正操作的是对象的引用. 下面对两 ...
- 互联网同步yum服务器,中科大 rsync createrepo
参考文章 https://blog.csdn.net/chenjia6605/article/details/82734945 1.本机安装所需工具: yum -y install rsync cre ...
- UE4:四种加载资源的方式
转自:https://blog.csdn.net/zhangxsv123/article/details/79707686 第一种: 如果该蓝图有C++类(或者说是从C++类创建的蓝图),直接进行加载 ...
- MySQL修改数据库、表、列、外键字符编码和排序编码
在重启Confluence应用时,突然遇见这个检查错误,查询总结需要修改Mysql数据库的所有字符编码和排序编码,报错如下: Confluence Help – This installation o ...
- POJ3259 Wormholes
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- django 分页和中间件
分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views h ...
- postgresql模糊查询json类型字段内某一属性值
需求场景: 目录以jsonb格式存储在数据库表t的chapter字段中,需要菜单路径中包含指定字符串(比如“语文”或者“上学期”)的menu 以下为chapter字段存储json示例: { " ...
- 63(原67).纯 CSS 创作单元素点阵 loader
原文地址:https://segmentfault.com/a/1190000015444368 感想:CSS又一次让我见识到它的强大之处 --> box-shadow . box-shadow ...
- DRF框架之 serializers 序列化组件
1. 什么是序列化,其实在python中我们就学了序列化工具json工具,就是吧信息存为类字典形式 2. DRF框架自带序列化的工具: serializers 3. DRF框架 serializers ...