solidity通用模式访问限制

2023-11-13

通用模式

访问限制

访问限制是智能合约的一种通用模式,但你不能限制任何人获取你的智能合约和交易的状态。当然,你可以通过加密来增加读取难度,但是如果你的智能合约需要读取该数据(指加密的数据),其他人也可以读取。

你可以通过将合约状态设置为私有来限制其他合约来读取你的合约状态。

此外,你可以限制其他人修改你的合约状态或者调用你的合约函数,这也是本章将要讨论的。

函数修饰符的使用可以让这些限制(访问限制)具有较好的可读性。

contract AccessRestriction {
    // These will be assigned at the construction
    // phase, where `msg.sender` is the account
    // creating this contract.
    //以下变量将在构造函数中赋值 
    //msg.sender是你的账户
    //创建本合约
    address public owner = msg.sender;
    uint public creationTime = now;

    // Modifiers can be used to change
    // the body of a function.
    // If this modifier is used, it will
    // prepend a check that only passes
    // if the function is called from
    // a certain address.

//修饰符可以用来修饰函数体,如果使用该修饰符,当该函数被其他地址调用时将会先检查是否允许调用(译注:就是说外部要调用本合约有修饰符的函数时会检查是否允许调用,比如该函数是私有的则外部不能调用。)

    modifier onlyBy(address _account)
    {
        if (msg.sender != _account)
            throw;
        // Do not forget the "_"! It will
        // be replaced by the actual function
        // body when the modifier is invoked.
        //account变量不要忘了“_” 
        _
    }

    /// Make `_newOwner` the new owner of this
    /// contract.
   //修改当前合约的宿主
    function changeOwner(address _newOwner)
        onlyBy(owner)
    {
        owner = _newOwner;
    }

    modifier onlyAfter(uint _time) {
        if (now < _time) throw;
        _
    }

    /// Erase ownership information.
    /// May only be called 6 weeks after
    /// the contract has been created.
   //清除宿主信息。只能在合约创建6周后调用
    function disown()
        onlyBy(owner)
        onlyAfter(creationTime + 6 weeks)
    {
        delete owner;
    }

    // This modifier requires a certain
    // fee being associated with a function call.
    // If the caller sent too much, he or she is
    // refunded, but only after the function body.
    // This is dangerous, because if the function
    // uses `return` explicitly, this will not be
    // done!
    //该修饰符和函数调用关联时需要消耗一部分费用。调用者发送的多余费用会在函数执行完成后返还,但这个是相当危险的,因为如果函数
    modifier costs(uint _amount) {
        if (msg.value < _amount)
            throw;
        _
        if (msg.value > _amount)
            msg.sender.send(_amount - msg.value);
    }

    function forceOwnerChange(address _newOwner)
        costs(200 ether)
    {
        owner = _newOwner;
        // just some example condition
        if (uint(owner) & 0 == 1)
            // in this case, overpaid fees will not
            // be refunded
            return;
        // otherwise, refund overpaid fees
    }}

A more specialised way in which access to function calls can be restricted will be discussed in the next example.

State Machine
Contracts often act as a state machine, which means that they have certain stages in which they behave differently or in which different functions can be called. A function call often ends a stage and transitions the contract into the next stage (especially if the contract models interaction). It is also common that some stages are automatically reached at a certain point in time.

An example for this is a blind auction contract which starts in the stage “accepting blinded bids”, then transitions to “revealing bids” which is ended by “determine auction autcome”.

Function modifiers can be used in this situation to model the states and guard against incorrect usage of the contract.

Example

In the following example, the modifier atStage ensures that the function can only be called at a certain stage.

Automatic timed transitions are handled by the modifier timeTransitions, which should be used for all functions.

Note

Modifier Order Matters. If atStage is combined with timedTransitions, make sure that you mention it after the latter, so that the new stage is taken into account.

Finally, the modifier transitionNext can be used to automatically go to the next stage when the function finishes.

Note

Modifier May be Skipped. Since modifiers are applied by simply replacing code and not by using a function call, the code in the transitionNext modifier can be skipped if the function itself uses return. If you want to do that, make sure to call nextStage manually from those functions.

contract StateMachine {
    enum Stages {
        AcceptingBlindedBids,
        RevealBids,
        AnotherStage,
        AreWeDoneYet,
        Finished
    }
    // This is the current stage.
    Stages public stage = Stages.AcceptingBlindedBids;

    uint public creationTime = now;

    modifier atStage(Stages _stage) {
        if (stage != _stage) throw;
        _
    }
    function nextStage() internal {
        stage = Stages(uint(stage) + 1);
    }
    // Perform timed transitions. Be sure to mention
    // this modifier first, otherwise the guards
    // will not take the new stage into account.
    modifier timedTransitions() {
        if (stage == Stages.AcceptingBlindedBids &&
                    now >= creationTime + 10 days)
            nextStage();
        if (stage == Stages.RevealBids &&
                now >= creationTime + 12 days)
            nextStage();
        // The other stages transition by transaction
    }

    // Order of the modifiers matters here!
    function bid()
        timedTransitions
        atStage(Stages.AcceptingBlindedBids)
    {
        // We will not implement that here
    }
    function reveal()
        timedTransitions
        atStage(Stages.RevealBids)
    {
    }

    // This modifier goes to the next stage
    // after the function is done.
    // If you use `return` in the function,
    // `nextStage` will not be called
    // automatically.
    modifier transitionNext()
    {
        _
        nextStage();
    }
    function g()
        timedTransitions
        atStage(Stages.AnotherStage)
        transitionNext
    {
        // If you want to use `return` here,
        // you have to call `nextStage()` manually.
    }
    function h()
        timedTransitions
        atStage(Stages.AreWeDoneYet)
        transitionNext
    {
    }
    function i()
        timedTransitions
        atStage(Stages.Finished)
    {
    }}

Next Previous

转自:
https://github.com/twq0076262/solidity-zh/edit/master/common-patterns.md

如果你希望高效的学习以太坊DApp开发,可以访问汇智网提供的最热门在线互动教程:

其他更多内容也可以访问这个以太坊博客

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

solidity通用模式访问限制 的相关文章

  • codeforces 1169 D. Good Triple

    题意 有长为n的串 其中有几个 ll rr 符合条件 首先 长度超过9的串一定符合条件 枚举左端点ll 右端点控制在ll 8就行 剩下的直接加 include
  • UNIX环境高级编程 学习笔记 第十五章 进程间通信

    进程间通信可通过传送打开的文件 也可以经由fork和exec函数来传送 还可以通过文件系统传送 IPC InterProcess Communication 进程间通信 是进程通信方式的统称 不同UNIX系统支持的IPC形式不同 虽然SUS

随机推荐

  • net::ERR_CONNECTION_REFUSED,Network Error

    net ERR CONNECTION REFUSED 项目部署服务器后报如图所示错误 但在本地调用后台RESRful接口数据没问题 最后发现是tomcat服务器没有开 开了后没有再次执行命令使后台运行 其实这个问题从两点能够发现 一是执行n
  • 解决Anaconda导入第三方包的各种问题

    1 win R win R 输入 HOMEPATH 然后找到 condarc 把里面的内容改为 ssl verify true show channel urls true channels http mirrors tuna tsingh
  • 点云数据做简单的平面的分割 三维场景中有平面,杯子,和其他物体 实现欧式聚类提取 对三维点云组成的场景进行分割

    点云分割是根据空间 几何和纹理等特征对点云进行划分 使得同一划分内的点云拥有相似的特征 点云的有效分割往往是许多应用的前提 例如逆向工作 CAD领域对零件的不同扫描表面进行分割 然后才能更好的进行空洞修复曲面重建 特征描述和提取 进而进行基
  • Qt 事件过滤器 - EventFilter

    事件过滤器 见名之意 就是将事件过滤一遍 将不需要的事件都清除掉 剩下需要的事件进行操作 可能讲得不是很透彻 那就看下图 就很明白了 原本事件应该直接发送给 组件对象 但是现在却先将事件发送给 过滤器对象 经过过滤的事件再发给 组件对象 如
  • xss level11

    Level11 1 2 毫无头绪 查看PHP源代码发现 是从头文件的referer获取的输入 3 用Burp抓包 修改头文件如下 4 再点击Proxy界面的forward 回到浏览器页面如下 5 点击即可 转载于 https www cnb
  • 走进强化学习

    一 什么是强化学习 强化学习是机器学习里面的一个分支 是一个智能体通过不断的与环境产生互动而不断改进它的行为 从而积累最大奖励的一个决策过程 智能体在完成某项任务时 首先通过动作A与周围环境进行交互 在动作A和环境的作用下 智能体会产生新的
  • CUDA 计算线程索引的一般公式

    第一种方法 CUDA thread index int blockId blockIdx z gridDim x gridDim y blockIdx y gridDim x blockIdx x int threadId blockId
  • Couldn‘t resolve host

    Centos6安装完并配置静态ip地址后 发现yum命令下载出现Couldn t resolve host ping www baidu com 出现域名解析错误 百度大部分答案是在 etc sysconfig network script
  • 用java求出1-1/2+1/3-1/4…..1/100的和

    public class sumPractice3 public static void main String args 需求 求出1 1 2 1 3 1 4 1 100的和 分子始终为1 double num 1 定义个变量用来存储计算
  • 三个基于WebRTC开源MCU框架的横向对比

    1 licode 官网地址 http lynckia com licode index html 官方demo地址 https chotis2 dit upm es Github地址 https github com lynckia lic
  • switch的用法

    switch语句 实际生活中 需要做出很多选择 大家都知道做选择可以使用if语句 但是如果选择太多 if语句使用起来就会很繁琐 这个时候就需要一个能将代码简化的语句 也就是我们今天的主角switch语句 switch语句是一个多分支选择语句
  • 【中等】【LeetCode刷题笔记(二十九)】之54.螺旋矩阵

    本文章由公号 开发小鸽 发布 欢迎关注 老规矩 妹妹镇楼 一 题目 一 题干 给定一个包含 m x n 个元素的矩阵 m 行 n 列 请按照顺时针螺旋顺序 返回矩阵中的所有元素 二 示例 示例 1 输入 1 2 3 4 5 6 7 8 9
  • 零基础如何使用IDEA启动前后端分离中的前端项目(Vue)?

    一 在IDEA中配置vue插件 点击File gt Settings gt Plugins gt 搜索vue js插件进行安装 下面的图中我已经安装好了 二 搭建node js环境 安装node js 可以去官网下载 安装过程就很简单 直接
  • 活动预告丨SMP十周年系列论坛第一期:社交机器人论坛开幕

    全国社会媒体处理大会十周年系列论坛 第一期 SMP2021社交机器人论坛 将于2021年11月13日 周六 上午线上举办 旨在探讨社交机器人领域的热点和前沿 探索构建更智能的社交机器人的学术研究和技术方案 人机对话系统是人工智能领域最具挑战
  • LocalDate、LocalDateTime互转String

    目录 1 LocalDate String互转 LocalDate转String String转LocalDate 2 LocalDateTime String互转 LocalDateTime转String String转LocalDate
  • c语言连点器脚本

    include
  • 数据分析学习之路——(五)用数据告诉你电影的市场趋势

    随着社会的多元化 越来越多的影视作品走入人们的生活中 但是近年来鲜有几部新制作的电影能俘获观众的心 到底是观众越来越挑剔 还是电影作品本身不够吸引力 如果你是有一个电影公司 你想制作一部电影作品 你有想过拍一部什么样的电影吗 你会选择一名什
  • 高级Bash脚本编程指南(24):时间/日期 命令

    高级Bash脚本编程指南 24 时间 日期 命令 成于坚持 败于止步 时间 日期和计时 date 直接调用date命令就会把日期和时间输出到 stdout上 这个命令有趣的地方在于它的格式化和分析选项上 root ubuntu resour
  • PAT乙级1016 部分A+B (15 分)

    1016 部分A B 15 分 一 问题描述 二 代码实现 include
  • solidity通用模式访问限制

    通用模式 访问限制 访问限制是智能合约的一种通用模式 但你不能限制任何人获取你的智能合约和交易的状态 当然 你可以通过加密来增加读取难度 但是如果你的智能合约需要读取该数据 指加密的数据 其他人也可以读取 你可以通过将合约状态设置为私有来限