库(Libraries)

库类似合约,实现仅在专门地址部署一次,使用EVM的DELEGATECALL的功能重复使用的目的。意思是当库函数被调用后,代码执行在被调用的合约的环境。例如,使用this调用合约,可以访问的调用合约的storage.孤立的库代码仅能访问变量,不能修改变量

pragma solidity ^0.4.16;

library Set {
struct Data {
mapping(uint => bool) flags;
} function insert(Data storage self, uint value) public returns (bool) {
if (self.flags[value])
return false; self.flags[value] = true;
return true;
} function remove(Data storage self, uint value) public returns (bool) {
if (!self.flags[value])
return false; self.flags[value] = false;
return true;
} function contains(Data storage self, uint value) public view returns (bool) {
return self.flags[value];
}
} contract C {
Set.Data knownValues; function register(uint value) public {
require(Set.insert(knownValues, value));
}
// In this contract, we can also directly access knownValues.flags, if we want.
}

如何使用库的memory类型和内部函数实现自定义类型,而无须使用外部方法调用

pragma solidity ^0.4.16;

library BigInt {
struct bigint {
uint[] limbs;
} function fromUint(uint x) internal pure returns (bigint r) {
r.limbs = new uint[](1);
r.limbs[0] = x;
} function add(bigint _a, bigint _b) internal pure returns (bigint r) {
r.limbs = new uint[](max(_a.limbs.length, _b.limbs.length));
uint carry = 0; for (uint i = 0; i < r.limbs.length; ++i) {
uint a = limb(_a, i);
uint b = limb(_b, i);
r.limbs[i] = a + b + carry; if (a + b < a || (a + b == uint(-1) && carry > 0))
carry = 1;
else
carry = 0;
} if (carry > 0) {
// too bad, we have to add a limb
uint[] memory newLimbs = new uint[](r.limbs.length + 1);
for (i = 0; i < r.limbs.length; ++i)
newLimbs[i] = r.limbs[i]; newLimbs[i] = carry;
r.limbs = newLimbs;
}
} function limb(bigint _a, uint _limb) internal pure returns (uint) {
return _limb < _a.limbs.length ? _a.limbs[_limb] : 0;
} function max(uint a, uint b) private pure returns (uint) {
return a > b ? a : b;
}
} contract C {
using BigInt for BigInt.bigint; function f() public pure {
var x = BigInt.fromUint(7);
var y = BigInt.fromUint(uint(-1));
var z = x.add(y);
}
}

关键字Using For

using A for B 附加库函数A到类型B,类似python的self

using A for * 附加库函数A到所有类型

pragma solidity ^0.4.16;

library Set {
struct Data {
mapping(uint => bool) flags;
} function insert(Data storage self, uint value) public returns (bool) {
if (self.flags[value])
return false; // already there self.flags[value] = true;
return true;
} function remove(Data storage self, uint value) public returns (bool) {
if (!self.flags[value])
return false; // not there self.flags[value] = false;
return true;
} function contains(Data storage self, uint value) public view returns (bool) {
return self.flags[value];
}
} contract C {
using Set for Set.Data; // this is the crucial change
Set.Data knownValues; function register(uint value) public {
require(knownValues.insert(value));
}
}

使用另外方式扩展基本类型

pragma solidity ^0.4.16;

library Search {
function indexOf(uint[] storage self, uint value) public view returns (uint) {
for (uint i = 0; i < self.length; i++)
if (self[i] == value) return i;
return uint(-1);
}
} contract C {
using Search for uint[];
uint[] data; function append(uint value) public {
data.push(value);
} function replace(uint _old, uint _new) public {
uint index = data.indexOf(_old);
if (index == uint(-1))
data.push(_new);
else
data[index] = _new;
}
}

solidity语言14的更多相关文章

  1. 用solidity语言开发代币智能合约

    智能合约开发是以太坊编程的核心之一,而代币是区块链应用的关键环节,下面我们来用solidity语言开发一个代币合约的实例,希望对大家有帮助. 以太坊的应用被称为去中心化应用(DApp),DApp的开发 ...

  2. 第一行代码:以太坊(2)-使用Solidity语言开发和测试智能合约

    智能合约是以太坊的核心之一,用户可以利用智能合约实现更灵活的代币以及其他DApp.不过在深入讲解如何开发智能合约之前,需要先介绍一下以太坊中用于开发智能合约的Solidity语言,以及相关的开发和测试 ...

  3. Solidity语言系列教程

    Solidity 是一门面向合约的.为实现智能合约而创建的高级编程语言.这门语言受到了 C++,Python 和 Javascript 语言的影响,设计的目的是能在 以太坊虚拟机(EVM) 上运行. ...

  4. solidity语言介绍以及开发环境准备

    solidity语言介绍以及开发环境准备   Solidity 是一门面向合约的.为实现智能合约而创建的高级编程语言.这门语言受到了 C++,Python 和 Javascript 语言的影响,设计的 ...

  5. 用C++生成solidity语言描述的buchi自动机的初级经验

    我的项目rvtool(https://github.com/Zeraka/rvtool)中增加了生成solidity语言格式的监控器的模块. solidity特殊之处在于,它是运行在以太坊虚拟机环境中 ...

  6. Solidity语言基础 和 Etherum ERC20合约基础

    1. 类型只能从第一次赋值中推断出来,因此以下代码中的循环是无限的,  小. for (var i = 0; i < 2000; i++) { ... } --- Solidity Types ...

  7. solidity语言

    IDE:Atom 插件:autocomplete-solidity 代码自动补齐   linter-solium,linter-solidity代码检查错误   language-ethereum支持 ...

  8. solidity语言13

    函数过载 合约内允许定义同名函数,但是输入参数不一致 pragma solidity ^0.4.17; contract A { function f(uint _in) public pure re ...

  9. solidity语言12

    View Functions 函数声明为视图,将无权修改状态 pragma solidity ^0.4.16; contract C { function f(uint a, uint b) publ ...

随机推荐

  1. 关于:“无法序列化会话状态。在“StateServer”或“SQLServer”模式下,...的问题

    关于:“无法序列化会话状态.在“StateServer”或“SQLServer”模式下,...的问题 错误描述: 无法序列化会话状态.在“StateServer”或“SQLServer”模式下,ASP ...

  2. Codeforces Round #549 (Div. 2) Solution

    传送门 A.The Doors 看懂题目就会写的题 给一个 $01$ 序列,找到最早的位置使得 $0$ 或 $1$ 已经全部出现 #include<iostream> #include&l ...

  3. Java正则表达式-捕获组

    private Set<String> getCodes(String s) { Set<String> resultSet = new HashSet<>(); ...

  4. [转] 如何批量删除Docker中已经停止的容器

    [From]https://blog.csdn.net/csdn_duomaomao/article/details/78587103 方法一: #显示所有的容器,过滤出Exited状态的容器,取出这 ...

  5. once函数,简约不简单的

    module.exports = once once.proto = once(function () { Object.defineProperty(Function.prototype, 'onc ...

  6. 完全原生javascript简约日历插件,js、html

    效果图: 效果如图所示,尽管看上去并不是很美观,但是,基本上的功能还是已经完成了,码了一天多的时间,权当做复习一下js吧. 整个做下来差不多码了500多行代码~其实只是很多的样式也包括了在其中了,虽然 ...

  7. 2019.03.26 读书笔记 关于event

    event 主要是给委托加了一层保护,不能任意的 class.delegate=null,class.delegate=fun1,不能由调用者去任意支配,而是由class自己去增加或减少,用+=.-= ...

  8. sqlplus连接oracle语法

    sqlplus文件在product\11.2.0\dbhome_1\BIN目录下. 连接语法:用户名/密码@ip/服务名

  9. 阿里云centos 7 中tomcat 自启动

    这里我的tomcat的安装路径为 /usr/local/tomcat 1 为tomcat添加自启动参数 catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,因此 ...

  10. 这真的该用try-catch吗?

    前言 我有个技能,就是把“我”说的听起来特别像“老子”. 以前是小喽啰的时候,会跟领导说“我!不加班.”,听起来就像“老子不加班!”一样.到最后发现,我确实没有把计划内的工作拖到需要加班才能完成,这个 ...