库(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. Apache 去掉 www

    1 用phpstudy的网友打开“其他选项菜单”- “配置文件”-httpd-conf.找到 #LoadModule rewrite_module modules/mod_rewrite.so 把这一 ...

  2. hdu6325 Interstellar Travel 凸包变形

    题目传送门 题目大意: 给出n个平面坐标,保证第一个点和第n个点y值为0,其余点的x坐标都在中间,要从 i 点走到 j 点的要求是 i 点的横坐标严格小于 j 的横坐标,并且消耗的能量是(xi * y ...

  3. POJ - 1011 剪枝练习

    题意:给定n条拆掉的棍子,问能凑成最短的多条相同长度棍子的最短长度 x:当前第几条正在合成的棍子 y:目前正在尝试的拆掉的棍子 z:当前长度 剪枝方案: 1.按照长度单调性排序,减少重复搜索 2.如果 ...

  4. java获取缓存通用类

    1 class JedisHelper { public static <T, R> T getBySecond(String cacheKey, int seconds, R p, Fu ...

  5. asp.net mvc 静态化

    静态化的基本理解就是,常用的资源以文本形式保存,客户端访问时无需经过程序处理,直接下载 但是不存在的文件需要经过程序处理,文件内容如果需要有更动或删除,则直接删除文件本身 1.IIS Express ...

  6. phpcms V9 框架目录结构

    phpcms v9框架的目录结构分析:      了解v9框架的目录结构,有助于帮助我们快速建立起对v9框架的一个整体认识 打开"mycms"项目,有如下文件和目录      使用 ...

  7. c语言实现栈的增删查减

    #include"stdio.h"#include"stdlib.h"#define STACK_SIZE 100#define STACKINCREMENT ...

  8. oracle 单实例DG(搭建篇一)

    一,介绍 lodding... 二,安装前环境配置 01,依赖包的安装: yum install binutils-* yum install compat-libstdc++-* yum insta ...

  9. HTML问题 | 两个Input在同一行连着不留缝隙

    方法1:让两个 input 连在一起写 不换行 <div class="inputDiv"> <input type="text" place ...

  10. cloudemanager安装时出现8475 MainThread agent ERROR Heartbeating to 192.168.30.1:7182 failed问题解决方法(图文详解)

    不多说,直接上干货!   问题详情 解决这个问题简单的,是因为有进程占用了.比如 # ps aux | grep super root ? Ss : : /opt/cm-/lib64/cmf/agen ...