当前位置:网站首页>Principle of scalable contract delegatecall
Principle of scalable contract delegatecall
2022-07-02 11:35:00 【Ni xiansen_】
The principle of scalable contracts -DelegateCall
Introducing DelegateCall when , We need to bring Call Methods are introduced together , And make a comparison .
Let's talk about the concept first !
DelegateCall: There is a special type of message call , go by the name of Delegate invocation (delegatecall) . It is similar to the general message call (call) The difference is that , The code for the target address will be executed in the context of the calling contract , also msg.sender and msg.value unchanged . This means that a contract can dynamically load code from another address at run time .
I don't like to start with concepts , After all, it's too difficult to understand . Let's go to the code demonstration
Demo environment
Remix IDE:Remix Browser based IDE, Integrated compiler and Solidity Runtime environment , No server components are required , Support web page online writing 、 Deploy and test smart contracts .
This chapter is mainly for you to quickly understand DelegateCall Characteristics of , So the choice is based on Remix To demonstrate .
code
We are contracts New under the directory delegatecall.sol file , And then paste the following demo code into delegatecall.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract A {
address public msgsender;
function callFunc() public {
msgsender = msg.sender;
}
}
contract B {
address public msgsender;
address public a;
constructor(address _a) {
a = _a;
}
function call_a_call() public{
// isOk Used to receive whether the call is successful
(bool isOk,bytes memory result) = a.call(abi.encodeWithSignature("callFunc()"));
// If you fail , The abnormal
require(isOk,"call faild");
}
function delegatecall_a_delegatecall() public{
(bool isOk,bytes memory result) = a.delegatecall(abi.encodeWithSignature("callFunc()"));
require(isOk,"call faild");
}
}
Here we define two contracts A and B
A The contract has a msgsender State variables and callFunc() Method
B The contract has two state variables msgsender and a,msgsender To verify our experimental results ,a It's a deposit contract A Instance reference of , And two methods are defined , For demonstration B The contract passed call and delegatecall Two ways to call A The contract callFunc()
Then we press Ctrl + s Shortcut key , The contract code will be saved here , The compiler will automatically compile for us
Contract deployment
Click the button below to jump to Deployment and debugging page 
Deploy the contract first A, Redeployment contract B, because B State variables in a Refer to the A The address of
- Deployment contract
A
- Deployment contract
B
Deployment contractBNeed to pass inAThe address of
Contract interaction
Let's first click on > Button to expand our contract , And click the A Contracts and B The contract msgsender Button query ( Clicking the button will automatically call msgsender The query method of ) The value of the current state variable , Here we can see all for 0
Next we click on B contract delegatecall_a_delegatecall Button ( call delegatecall_a_delegatecall Method ), Then click... Again A Contracts and B The contract msgsender Button query 
Something strange happened !!!A The contract msgsender No value , however B The contract msgsender It became my own address 0x5B38Da6a....
Okay , We can combine the above experimental results , Then understand the concept at the beginning of the article
Destination address A The code of the contract will be called when B The contract Context In the implementation of , also msg.sender and msg.value unchanged . Context It's the operating environment , Including the contract State variables , So when the contract is executed callFunc() When ,callFunc() The method is in B Executed in the contract , The change is B Status variable of contract , and A The contract msgsender But it didn't change .
If B Two state variables of the contract msgsender and a Swap positions in the code , That's another story , Here comes another concept 《 Contract data storage layout 》
Next we click on B contract call_a_call Button , Then click... Again A Contracts and B The contract msgsender Button query 
Now? A The contract msgsender Have a value ,B The contract msgsender No change in value .
When calling B contract call_a_call When the method is used ,B The state variables of the contract have not changed ,A The contract msgsender Have a value , This explanation callFunc() The method is in A The contract Context Environment , here Context There is a change .
And we found that A The contract msgsender Turned into B The address of the contract , This shows that in the process of calling msg.sender and msg.value There is a change ,msg.sender It's no longer me 0x5B38Da6a.... It is B The address of the contract
summary
We will pay attention to what we wrote earlier Scalable contracts , When the Agency contract When the call is initiated , Agency contract And Logical contract Interaction is used Delegate invocation (DelegateCall). Logical contract The method of in Agency contract Context execution , And modified State variables It's also Agency contract Of . So the contract data has been Agency contract in , When Logical contract The existing data of the contract will not be affected when upgrading
There is a problem , Or suggest leaving a message , thank you .
边栏推荐
- [cloud native] 2.5 kubernetes core practice (Part 2)
- 程序员成长第六篇:如何选择公司?
- ASTParser 解析含有emum 枚举方法的类文件的踩坑记
- Resources reads 2D texture and converts it to PNG format
- Compilation errors and printout garbled problems caused by Chinese content in vs2019 code
- Gaode draws lines according to the track
- Summary of data export methods in powerbi
- Complement (Mathematical Simulation
- 二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
- Why does LabVIEW lose precision in floating point numbers
猜你喜欢

A white hole formed by antineutrons produced by particle accelerators

Map set assignment to database

三.芯片启动和时钟系统

一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)

What are the software product management systems? Inventory of 12 best product management tools

Tick Data and Resampling

念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研

CentOS8之mysql基本用法
![[play with FPGA learning 5 in simple terms ----- reset design]](/img/57/816a59787259dc32b52897c069e1a6.png)
[play with FPGA learning 5 in simple terms ----- reset design]

Webauthn - official development document
随机推荐
Installation of ROS gazebo related packages
[idea] use the plug-in to reverse generate code with one click
JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
Is the Ren domain name valuable? Is it worth investing? What is the application scope of Ren domain name?
SSRF
Resources reads 2D texture and converts it to PNG format
Iii. Système de démarrage et d'horloge à puce
Programmer growth Chapter 6: how to choose a company?
TIPC介绍1
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
Summary of data export methods in powerbi
Webauthn - official development document
enumrate的start属性的坑
Complement (Mathematical Simulation
Is the stock account given by qiniu business school safe? Can I open an account?
JS -- take a number randomly from the array every call, and it cannot be the same as the last time
TIPC introduction 1
sqlite 修改列类型
CentOS8之mysql基本用法
spritejs