当前位置: 首页 > news >正文

solidity的高阶语法4

1.提款模式

提款模式确保不会造成安全威胁的直接转接呼叫。 以下合约显示了使用转账调用发送以太币的不安全方式。

pragma solidity ^0.5.0;contract Test {address payable public richest;uint public mostSent;constructor() public payable {richest = msg.sender;mostSent = msg.value;}function becomeRichest() public payable returns (bool) {if (msg.value > mostSent) {// Insecure practicerichest.transfer(msg.value);richest = msg.sender;mostSent = msg.value;return true;} else {return false;}}
}

通过使最富有的合约成为后备功能失败的合约,可以使上述合约处于不可用状态。 当后备函数失败时,becomeRichest()函数也会失败,合约将永远卡住。 为了缓解这个问题,我们可以使用 Withdrawal Pattern。

在提款模式中,我们会在每次转账前重置待处理金额。 它将确保只有调用者合约失败。

pragma solidity ^0.5.0;contract Test {address public richest;uint public mostSent;mapping (address => uint) pendingWithdrawals;constructor() public payable {richest = msg.sender;mostSent = msg.value;}function becomeRichest() public payable returns (bool) {if (msg.value > mostSent) {pendingWithdrawals[richest] += msg.value;richest = msg.sender;mostSent = msg.value;return true;} else {return false;}}function withdraw() public {uint amount = pendingWithdrawals[msg.sender];pendingWithdrawals[msg.sender] = 0;msg.sender.transfer(amount);}
}

2.限制访问

限制访问合同是一种常见做法。 默认情况下,合约状态是只读的,除非将其指定为公共状态。

我们可以限制谁可以修改合约的状态或使用修饰符调用合约的函数。 我们将创建并使用多个修饰符,如下所述 −

  • onlyBy − 一旦用于函数,则只有提到的调用者可以调用该函数。

  • onlyAfter − 一旦用于函数,则可以在一定时间段后调用该函数。

  • costs − 一旦用于函数,则仅当提供特定值时调用者才能调用该函数。

示例

pragma solidity ^0.5.0;contract Test {address public owner = msg.sender;uint public creationTime = now;modifier onlyBy(address _account) {require(msg.sender == _account,"Sender not authorized.");_;}function changeOwner(address _newOwner) public onlyBy(owner) {owner = _newOwner;}modifier onlyAfter(uint _time) {require(now >= _time,"Function called too early.");_;}function disown() public onlyBy(owner) onlyAfter(creationTime + 6 weeks) {delete owner;}modifier costs(uint _amount) {require(msg.value >= _amount,"Not enough Ether provided.");_;if (msg.value > _amount)msg.sender.transfer(msg.value - _amount);}function forceOwnerChange(address _newOwner) public payable costs(200 ether) {owner = _newOwner;if (uint(owner) & 0 == 1) return;        }
}

文章转载自:

http://oCfHmaXW.Lsmnn.cn
http://X4BAEezG.Lsmnn.cn
http://baNRi6qy.Lsmnn.cn
http://ASGGUWwJ.Lsmnn.cn
http://N7GSbUqb.Lsmnn.cn
http://WwuNbVkE.Lsmnn.cn
http://2UcOoeAq.Lsmnn.cn
http://ecGa2nc1.Lsmnn.cn
http://kRc4rqS2.Lsmnn.cn
http://JApEGMqO.Lsmnn.cn
http://u0hP89Wx.Lsmnn.cn
http://daOP1z4Y.Lsmnn.cn
http://mgBn6SPK.Lsmnn.cn
http://N9JZYLN5.Lsmnn.cn
http://LgMfou6L.Lsmnn.cn
http://C12yfntb.Lsmnn.cn
http://2YsAprhc.Lsmnn.cn
http://R4rEYLAh.Lsmnn.cn
http://0OHhT0EU.Lsmnn.cn
http://HwftyI8q.Lsmnn.cn
http://bidUHblb.Lsmnn.cn
http://3HZRox1r.Lsmnn.cn
http://A1F06g6p.Lsmnn.cn
http://BzAXkIPo.Lsmnn.cn
http://hc8iYSbX.Lsmnn.cn
http://KQHxJbs1.Lsmnn.cn
http://QN34Bqes.Lsmnn.cn
http://byfAU9gF.Lsmnn.cn
http://VXTqgZPd.Lsmnn.cn
http://r1sntXYQ.Lsmnn.cn
http://www.dtcms.com/a/379190.html

相关文章:

  • Vue.js Data定义方式对比 data() { return {...} } 与 data: {} 的区别
  • P11961原根判断(1)
  • 特征空间的转换方法 IPM/LSS/Transformer
  • 【Vue3】05-Options API和Composition API的区别
  • 锁框架-面试
  • 电商 API 爬虫高阶技巧:多线程 / 异步请求结合,突破接口频率限制
  • vue两个组件互相引入时候会报错
  • 《芯片封装后未测试品粘连及边缘残胶的多维度工艺与材料失效分析》
  • MySQL基础全面解析
  • 探索容器技术:从LXC到Podman的演进
  • IntelliJ IDEA 启动项目时配置端口指南
  • java 实现rtsp 直播流下载
  • Python高级编程实战:装饰器、迭代器与生成器的深度应用
  • 高级SQL技术综合指南(MySQL)
  • 【51单片机】【protues仿真】基于51单片机电子琴系统
  • 解决idea2021maven依赖导入后还是找不到包,爆红无法导入
  • Netty学习
  • VGGNet:为什么16层简单堆叠能成为CNN经典?
  • 知识图谱RAG
  • 与controller层的接口入参注解@Valid有关的实体类判断空的注解
  • 基于AT89C52单片机的智能蓝牙台灯设计
  • Javaweb前端内容的思维导图
  • PyTorch深度学习实战【10】之神经网络的损失函数
  • 3.前置知识学习
  • Whois查询域名信息
  • 机器学习vs人类学习:人类学习如何借鉴机器学习方法?
  • ES6 面试题及详细答案 80题 (41-54)-- 异步编程(Promise/Generator/async)
  • Bug记录:Lombok @Builder 注解的两大陷阱及解决方案
  • ARM汇编 beep及bsp工程管理
  • 深入理解 Vue3 Router:三种路由模式的工作原理与实战应用