库(Libraries)

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

  1. pragma solidity ^0.4.16;
  2. library Set {
  3. struct Data {
  4. mapping(uint => bool) flags;
  5. }
  6. function insert(Data storage self, uint value) public returns (bool) {
  7. if (self.flags[value])
  8. return false;
  9. self.flags[value] = true;
  10. return true;
  11. }
  12. function remove(Data storage self, uint value) public returns (bool) {
  13. if (!self.flags[value])
  14. return false;
  15. self.flags[value] = false;
  16. return true;
  17. }
  18. function contains(Data storage self, uint value) public view returns (bool) {
  19. return self.flags[value];
  20. }
  21. }
  22. contract C {
  23. Set.Data knownValues;
  24. function register(uint value) public {
  25. require(Set.insert(knownValues, value));
  26. }
  27. // In this contract, we can also directly access knownValues.flags, if we want.
  28. }

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

  1. pragma solidity ^0.4.16;
  2. library BigInt {
  3. struct bigint {
  4. uint[] limbs;
  5. }
  6. function fromUint(uint x) internal pure returns (bigint r) {
  7. r.limbs = new uint[](1);
  8. r.limbs[0] = x;
  9. }
  10. function add(bigint _a, bigint _b) internal pure returns (bigint r) {
  11. r.limbs = new uint[](max(_a.limbs.length, _b.limbs.length));
  12. uint carry = 0;
  13. for (uint i = 0; i < r.limbs.length; ++i) {
  14. uint a = limb(_a, i);
  15. uint b = limb(_b, i);
  16. r.limbs[i] = a + b + carry;
  17. if (a + b < a || (a + b == uint(-1) && carry > 0))
  18. carry = 1;
  19. else
  20. carry = 0;
  21. }
  22. if (carry > 0) {
  23. // too bad, we have to add a limb
  24. uint[] memory newLimbs = new uint[](r.limbs.length + 1);
  25. for (i = 0; i < r.limbs.length; ++i)
  26. newLimbs[i] = r.limbs[i];
  27. newLimbs[i] = carry;
  28. r.limbs = newLimbs;
  29. }
  30. }
  31. function limb(bigint _a, uint _limb) internal pure returns (uint) {
  32. return _limb < _a.limbs.length ? _a.limbs[_limb] : 0;
  33. }
  34. function max(uint a, uint b) private pure returns (uint) {
  35. return a > b ? a : b;
  36. }
  37. }
  38. contract C {
  39. using BigInt for BigInt.bigint;
  40. function f() public pure {
  41. var x = BigInt.fromUint(7);
  42. var y = BigInt.fromUint(uint(-1));
  43. var z = x.add(y);
  44. }
  45. }

关键字Using For

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

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

  1. pragma solidity ^0.4.16;
  2. library Set {
  3. struct Data {
  4. mapping(uint => bool) flags;
  5. }
  6. function insert(Data storage self, uint value) public returns (bool) {
  7. if (self.flags[value])
  8. return false; // already there
  9. self.flags[value] = true;
  10. return true;
  11. }
  12. function remove(Data storage self, uint value) public returns (bool) {
  13. if (!self.flags[value])
  14. return false; // not there
  15. self.flags[value] = false;
  16. return true;
  17. }
  18. function contains(Data storage self, uint value) public view returns (bool) {
  19. return self.flags[value];
  20. }
  21. }
  22. contract C {
  23. using Set for Set.Data; // this is the crucial change
  24. Set.Data knownValues;
  25. function register(uint value) public {
  26. require(knownValues.insert(value));
  27. }
  28. }

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

  1. pragma solidity ^0.4.16;
  2. library Search {
  3. function indexOf(uint[] storage self, uint value) public view returns (uint) {
  4. for (uint i = 0; i < self.length; i++)
  5. if (self[i] == value) return i;
  6. return uint(-1);
  7. }
  8. }
  9. contract C {
  10. using Search for uint[];
  11. uint[] data;
  12. function append(uint value) public {
  13. data.push(value);
  14. }
  15. function replace(uint _old, uint _new) public {
  16. uint index = data.indexOf(_old);
  17. if (index == uint(-1))
  18. data.push(_new);
  19. else
  20. data[index] = _new;
  21. }
  22. }

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. Linux之旅(二)

    上一章讲到,mysql 安装遇到问题 ...莫名其妙好了,此处已回想不起为什么好了,只得跳过. 六.安装php7 参考教程[ http://www.thinkphp.cn/topic/48196.ht ...

  2. UVALive - 4682

    /*H E A D*/ struct Trie{ int son[maxn<<2][2]; int b[67],tot; void init(){ // memset(son,0,size ...

  3. [转] Emmet-前端开发神器

    [From] https://segmentfault.com/a/1190000007812543 Emmet是一款编辑器插件,支持多种编辑器支持.在前端开发中,Emmet 使用缩写语法快速编写 H ...

  4. python 函数基础知识整理

    一.函数的定义: 定义:def 关键词开头,空格之后接函数名称和圆括号(),最后还有一个":". def 是固定的,不能变,必须是连续的def三个字母,不能分开... 空格 为了将 ...

  5. sha1.js

    function encodeUTF8(s) { var i, r = [], c, x; for (i = 0; i < s.length; i++) if ((c = s.charCodeA ...

  6. css引入 以及选择器040

    css的介绍: css(Cascading Style Sheet)  层叠样式表 作用就是给HTML页面标签议案家各种样式 定义网页效果 简单来说 就是讲网页内容和显示样式进行分离 , 提高了显示功 ...

  7. 解决运行vue项目的报错This relative module was not found:

    运行vue项目出现这样的报错. This relative module was not found: * ../../assets/img/spot.png !./src/components/on ...

  8. (转)10 分钟内快速构建能够承载海量数据的 nginx 日志分析与报警平台

    10 分钟内快速构建能够承载海量数据的 nginx 日志分析与报警平台 原文:https://blog.qiniu.com/archives/8713

  9. vs2013下c++调用python脚本函数 出现的一些问题总结

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9530834.html 首先是配置: 使用VS2013创建工程. 将libs中的python27 ...

  10. Steamworks and Unity – P2P多人游戏

    之前我们讨论过“如何把Steamworks.Net和Unity整合起来”,这是一个很好的开始,现在我们研究深一点,谈一谈Steam中的多人游戏.这不是教程,但是可以指导你在你的游戏中如何使用Steam ...