Michael.W基于Foundry精读Openzeppelin第29期——RefundEscrow.sol

0. 版本

[openzeppelin]:v4.8.3,[forge-std]:v1.5.6

0.1 RefundEscrow.sol

Github: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.8.3/contracts/utils/escrow/RefundEscrow.sol

RefundEscrow合约继承了ConditionalEscrow合约,是ConditionalEscrow合约的一种功能拓展。RefundEscrow合约提供了基础的存取eth功能,同时合约owner可以将合约切换到Refunding或Closed状态。Refunding状态下,owner可以将锁存的eth分发给对应refundee;Closed状态下,beneficiary可以提走合约内全部eth。

1. 目标合约

RefundEscrow本身就是一个contract(非abstract contract),可独立部署和调用。

全部foundry测试合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/test/utils/escrow/RefundEscrow.t.sol

2. 代码精读

2.1 state() && beneficiary()
  • state():查询本合约当前所处的状态;
  • beneficiary():查询受益人地址。
    // 用于合约eth转账
    using Address for address payable;
	// 定义表示合约存取状态的枚举变量
    enum State {
    	// Active状态:1.可以进行deposit
        Active,
        // Refunding状态:1.可以withdraw 2.禁止deposit
        Refunding,
        // Closed状态:1.可以将合约内全部eth转移给受益人 2.禁止deposit
        Closed
    }

    event RefundsClosed();
    event RefundsEnabled();
		
	// 合约当前存取状态(枚举变量)
    State private _state;
    // 受益人地址,即可以提走所有deposit的eth的地址
    address payable private immutable _beneficiary;

    // 部署合约时设置受益人地址
    constructor(address payable beneficiary_) {
    	// 检查受益人地址不可为零地址
        require(beneficiary_ != address(0), "RefundEscrow: beneficiary is the zero address");
        // 设置受益人地址
        _beneficiary = beneficiary_;
        // 合约部署成功后的状态为Active
        _state = State.Active;
    }

    function state() public view virtual returns (State) {
        return _state;
    }

    function beneficiary() public view virtual returns (address payable) {
        return _beneficiary;
    }

foundry代码验证

contract RefundEscrowTest is Test {
    RefundEscrow re;
    address payable beneficiary = payable(address(1 << 1));
    address payable payee = payable(address(1 << 2));
    address payable other = payable(address(1 << 3));

    function setUp() external {
        re = new RefundEscrow(beneficiary);
        // change balance of the owner
        vm.deal(address(this), 10 ether);
        // change balance of other
        vm.deal(other, 10 ether);
    }

    function test_Getter() external {
        // initial state
        require(re.state() == RefundEscrow.State.Active);
        assertEq(beneficiary, re.beneficiary());
    }
}
2.2 close() && enableRefunds()
  • close():owner将合约状态从Active改为Closed。注:当合约处于Closed状态时,beneficiary可以提走合约内全部eth。此时,deposit操作被禁止;
  • enableRefunds():owner将合约状态从Active改为Refunding。注:当合约处于Refunding状态时,owner可以为每个refundee提取eth。此时,deposit操作被禁止。
    function close() public virtual onlyOwner {
    	// 检查当前合约状态必须为Active
        require(state() == State.Active, "RefundEscrow: can only close while active");
        // 合约状态设置为Closed
        _state = State.Closed;
        // 抛出event
        emit RefundsClosed();
    }

    function enableRefunds() public virtual onlyOwner {
    	// 检查当前合约状态必须为Active
        require(state() == State.Active, "RefundEscrow: can only enable refunds while active");
        // 合约状态设置为Refunding
        _state = State.Refunding;
        // 抛出event
        emit RefundsEnabled();
    }

foundry代码验证

contract RefundEscrowTest is Test {
    RefundEscrow re;
    address payable beneficiary = payable(address(1 << 1));
    address payable payee = payable(address(1 << 2));
    address payable other = payable(address(1 << 3));

    // copy events from RefundEscrow.sol to do event assertion
    event RefundsClosed();
    event RefundsEnabled();

    function setUp() external {
        re = new RefundEscrow(beneficiary);
        // change balance of the owner
        vm.deal(address(this), 10 ether);
        // change balance of other
        vm.deal(other, 10 ether);
    }

    function test_StateChange() external {
        // case 1: Active to Closed
        vm.expectEmit(address(re));
        emit RefundsClosed();
        re.close();
        require(re.state() == RefundEscrow.State.Closed);
        // revert if not owner
        vm.prank(other);
        vm.expectRevert("Ownable: caller is not the owner");
        re.close();
        // revert if not Active state before close()
        vm.expectRevert("RefundEscrow: can only close while active");
        re.close();

        // case 2: Active to Refunding
        re = new RefundEscrow(beneficiary);
        vm.expectEmit(address(re));
        emit RefundsEnabled();
        re.enableRefunds();
        require(re.state() == RefundEscrow.State.Refunding);
        // revert if not owner
        vm.prank(other);
        vm.expectRevert("Ownable: caller is not the owner");
        re.enableRefunds();
        // revert if not Active state before enableRefunds()
        vm.expectRevert("RefundEscrow: can only enable refunds while active");
        re.enableRefunds();
    }
}
2.3 deposit(address refundee)

owner为指定的refundee deposit eth。

    function deposit(address refundee) public payable virtual override {
    	// 只有合约状态为Active,才可以进行deposit
        require(state() == State.Active, "RefundEscrow: can only deposit while active");
        // 调用Escrow.deposit()
        super.deposit(refundee);
    }

foundry代码验证

contract RefundEscrowTest is Test {
    RefundEscrow re;
    address payable beneficiary = payable(address(1 << 1));
    address payable payee = payable(address(1 << 2));
    address payable other = payable(address(1 << 3));

    function setUp() external {
        re = new RefundEscrow(beneficiary);
        // change balance of the owner
        vm.deal(address(this), 10 ether);
        // change balance of other
        vm.deal(other, 10 ether);
    }

    function test_Deposit() external {
        // revert if not at Active state
        // case 1: at Closed state
        re.close();
        vm.expectRevert("RefundEscrow: can only deposit while active");
        re.deposit{value : 1 ether}(payee);

        // case 2: at Refunding state
        re = new RefundEscrow(beneficiary);
        re.enableRefunds();
        vm.expectRevert("RefundEscrow: can only deposit while active");
        re.deposit{value : 1 ether}(payee);

        // revert if not owner
        re = new RefundEscrow(beneficiary);
        vm.expectRevert("Ownable: caller is not the owner");
        vm.prank(other);
        re.deposit{value : 1 ether}(payee);

        // success at Active state
        re.deposit{value : 1 ether}(payee);
        assertEq(1 ether, address(re).balance);
        assertEq(1 ether, re.depositsOf(payee));

        // deposit again
        re.deposit{value : 2 ether}(payee);
        assertEq(1 ether + 2 ether, address(re).balance);
        assertEq(1 ether + 2 ether, re.depositsOf(payee));

        // deposit to other
        re.deposit{value : 3 ether}(other);
        assertEq(1 ether + 2 ether + 3 ether, address(re).balance);
        assertEq(1 ether + 2 ether, re.depositsOf(payee));
        assertEq(3 ether, re.depositsOf(other));
    }
}
2.4 beneficiaryWithdraw() && withdrawalAllowed(address)
  • beneficiaryWithdraw():将本合约内全部eth转移给受益人。注:任何人都可以触发该操作;
  • withdrawalAllowed(address):实现ConditionalEscrow.withdrawalAllowed(),允许withdraw的条件:合约状态为Refunding。
    function beneficiaryWithdraw() public virtual {
   		// 检查当前合约状态必须为Closed
        require(state() == State.Closed, "RefundEscrow: beneficiary can only withdraw while closed");
        // 将本合约内全部eth转移给受益人
        beneficiary().sendValue(address(this).balance);
    }

    function withdrawalAllowed(address) public view override returns (bool) {
    	// 允许withdraw的条件:合约状态为Refunding
        return state() == State.Refunding;
    }
}

foundry代码验证

contract RefundEscrowTest is Test {
    RefundEscrow re;
    address payable beneficiary = payable(address(1 << 1));
    address payable payee = payable(address(1 << 2));
    address payable other = payable(address(1 << 3));

    function setUp() external {
        re = new RefundEscrow(beneficiary);
        // change balance of the owner
        vm.deal(address(this), 10 ether);
        // change balance of other
        vm.deal(other, 10 ether);
    }

    function test_WithdrawalAllowed() external {
        // case 1: at Active state
        assertFalse(re.withdrawalAllowed(address(0)));
        // case 2: at Closed state
        re.close();
        assertFalse(re.withdrawalAllowed(address(0)));
        // case 3: at Refunding state
        re = new RefundEscrow(beneficiary);
        re.enableRefunds();
        assertTrue(re.withdrawalAllowed(address(0)));
    }

    function test_BeneficiaryWithdraw() external {
        // case 1: revert at Refunding state
        re.enableRefunds();
        vm.expectRevert("RefundEscrow: beneficiary can only withdraw while closed");
        re.beneficiaryWithdraw();

        // case 2: revert at Active state
        re = new RefundEscrow(beneficiary);
        vm.expectRevert("RefundEscrow: beneficiary can only withdraw while closed");
        re.beneficiaryWithdraw();

        // case 3: success at Closed state
        vm.deal(address(re), 1 ether);
        re.close();
        assertEq(0, beneficiary.balance);
        re.beneficiaryWithdraw();
        assertEq(1 ether, beneficiary.balance);
    }
}

ps:
本人热爱图灵,热爱中本聪,热爱V神。
以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。
同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下!
如果需要转发,麻烦注明作者。十分感谢!

在这里插入图片描述

公众号名称:后现代泼痞浪漫主义奠基人

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
社会发展日新月异,用计算机应用实现数据管理功能已经算是很完善的了,但是随着移动互联网的到来,处理信息不再受制于地理位置的限制,处理信息及时高效,备受人们的喜爱。所以各大互联网厂商都瞄准移动互联网这个潮流进行各大布局,经过多年的大浪淘沙,各种移动操作系统的不断面世,而目前市场占有率最高的就是微信小程序,本次开发一套基于微信小程序的生签到系统,有管理员,教师,学生三个角色。管理员功能有个人中心,学生管理,教师管理,签到管理,学生签到管理,班课信息管理,加入班课管理,请假信息管理,审批信息管理,销假信息管理,系统管理。教师和学生都可以在微信端注册和登录,教师可以管理签到信息,管理班课信息,审批请假信息,查看学生签到,查看加入班级,查看审批信息和销假信息。学生可以查看教师发布的学生签到信息,可以自己选择加入班课信息,添加请假信息,查看审批信息,进行销假操作。基于微信小程序的生签到系统服务端用Java开发的网站后台,接收并且处理微信小程序端传入的json数据,数据库用到了MySQL数据库作为数据的存储。这样就让用户用着方便快捷,都通过同一个后台进行业务处理,而后台又可以根据并发量做好部署,用硬件和软件进行协作,满足于数据的交互式处理,让用户的数据存储更安全,得到数据更方便。
### 回答1: 赖春华是一位专注于Cloud Foundry云平台研究的学者。Cloud Foundry是一种开放源代码的云平台,旨在简化开发、部署和运维应用程序。赖春华通过对Cloud Foundry进行研究,可以得出以下几点结论。 首先,Cloud Foundry具有很高的灵活性和可扩展性。它可以支持多种编程语言和框架,并且可以运行于多个云提供商的基础设施上。这意味着开发人员可以选择自己喜欢的工具和环境来构建和部署应用程序,而不受限于特定的技术栈或云服务提供商。 其次,Cloud Foundry提供了完整的开发、部署和运维工具链。它包括了一套完整的命令行工具和用户界面,可以帮助开发人员快速创建、部署和管理应用程序。这些工具提供了自动化的部署过程、监控和日志记录功能,使得应用程序开发和运维变得更加简单和高效。 另外,Cloud Foundry还具有高可靠性和弹性。它使用容器化技术来隔离应用程序,确保它们能够在不同的环境中平稳运行。同时,它具有自动扩展功能,可以根据应用程序的负载情况来自动调整资源的分配,以保证应用程序的性能和稳定性。 最后,通过研究Cloud Foundry,赖春华发现它可以在多个行业和场景中应用。无论是企业内部的应用程序还是公有云上的应用程序,Cloud Foundry都可以为其提供可靠的支持和高效的运维。它的开放性和灵活性使得开发人员能够快速响应市场变化和用户需求。 总之,赖春华的Cloud Foundry云平台研究表明,Cloud Foundry是一种强大而灵活的云平台,可以帮助开发人员简化应用程序的开发、部署和运维过程,并提供高可靠性和弹性的支持。它具有广泛的适用性,对于各种规模和行业的应用程序都能够提供有效的解决方案。 ### 回答2: 赖春华云平台研究一主要涉及cloud foundry云平台的研究。Cloud Foundry是一种开源的云平台,它为应用程序开发人员提供了一种简单、高效、灵活的方式来部署、运行和管理应用程序。这种平台具有很多优点,使得它成为各种企业和组织的首选。 首先,Cloud Foundry提供了一种容器化的方法来打包和部署应用程序。它使用容器技术,如Docker,将应用程序与其依赖项隔离开来,并提供了一种可移植和可扩展的部署方式。这使得应用程序的部署变得简单而高效。 其次,Cloud Foundry具有高度的自动化和可编程性。它提供了丰富的API和命令行工具,使得开发人员能够通过代码来管理和配置整个平台。这种可编程性带来了更高的灵活性和可扩展性,使得开发人员能够根据自己的需求自定义和扩展平台功能。 另外,Cloud Foundry提供了强大的监控和日志管理功能。开发人员可以通过平台的监控工具来实时监测应用程序的性能和健康状态,及时发现问题并作出调整。同时,平台还提供了日志管理功能,帮助开发人员跟踪和分析应用程序的日志信息,以便定位和解决问题。 最后,Cloud Foundry具有良好的可扩展性和可靠性。它支持多种云服务提供商,如AWS、Azure和Google Cloud,使得开发人员能够根据自己的需求选择合适的云环境。同时,平台还支持多个应用程序实例的部署和自动负载均衡,以确保应用程序的高可用性和弹性扩展性。 总之,赖春华云平台研究一主要关注cloud foundry云平台的各种功能和优势。这种平台提供了简单、高效、灵活的应用程序部署和管理方式,支持容器化技术、自动化和可编程性,具有强大的监控和日志管理功能,以及良好的可扩展性和可靠性。 ### 回答3: 赖春华是一位研究Cloud Foundry云平台的专家。Cloud Foundry是一个开源的云平台,支持开发人员在云中构建、部署和扩展应用程序。它提供了一个统一的平台,可以简化应用程序的生命周管理,并提供了一致性和可靠性。 赖春华研究Cloud Foundry云平台主要是为了探索其在实际应用中的优势和挑战。该云平台具有许多优点,比如可扩展性、灵活性和安全性。它可以自动处理应用程序的部署和扩展,减轻了开发人员的负担。此外,Cloud Foundry还支持多种编程语言和框架,使开发人员能够使用自己最熟悉的工具开发应用程序。 然而,赖春华也认识到Cloud Foundry云平台存在一些挑战。首先,它需要一定的学习和理解成本,开发人员需要掌握其相关概念和工具。其次,云平台的集成和部署可能需要一些时间和资源。此外,由于云平台中涉及到多个组件和服务,确保其稳定性和性能也是一个挑战。 为了解决这些挑战,赖春华提出了一些建议。首先,他建议开发人员在使用Cloud Foundry之前应该进行充分的学习和了解,并提供适当的培训和支持。其次,他建议云平台的部署和集成应该进行良好的规划和测试,确保其稳定性和可靠性。最后,他还建议云平台的使用应该与其他开发人员进行交流和分享经验,从而共同解决问题和改进。 总的来说,赖春华的研究主要关注于Cloud Foundry云平台的优势和挑战,并提供了一些解决方案和建议。通过研究云平台,他希望能够为开发人员提供更好的工具和平台,从而加快应用程序的开发和部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值