当前位置:网站首页>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 .
边栏推荐
- Redis超出最大内存错误OOM command not allowed when used memory > 'maxmemory'
- [play with FPGA learning 5 in simple terms ----- reset design]
- 从攻击面视角,看信创零信任方案实践
- JS -- take a number randomly from the array every call, and it cannot be the same as the last time
- Functional interfaces and method references
- TIPC introduction 1
- 三.芯片启动和时钟系统
- Gaode draws lines according to the track
- liftOver进行基因组坐标转换
- Verilog and VHDL signed and unsigned number correlation operations
猜你喜欢

Redis超出最大内存错误OOM command not allowed when used memory > 'maxmemory'

Jinshanyun - 2023 Summer Internship

2022年4月17日五心红娘团队收获双份喜报

I STM32 development environment, keil5/mdk5.14 installation tutorial (with download link)

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

Skills of PLC recorder in quickly monitoring multiple PLC bits
![[idea] use the plug-in to reverse generate code with one click](/img/b0/00375e61af764a77ea0150bf4f6d9d.png)
[idea] use the plug-in to reverse generate code with one click

MySQL比较运算符IN问题求解

tqdm的多行显示与单行显示

Tick Data and Resampling
随机推荐
Solve the problem of data blank in the quick sliding page of the uniapp list
Map set assignment to database
在连接mysql数据库的时候一直报错
Is the stock account given by qiniu business school safe? Can I open an account?
MTK full dump抓取
Verilog and VHDL signed and unsigned number correlation operations
Tick Data and Resampling
Resources读取2d纹理 转换为png格式
SSRF
Introduction to interface debugging tools
QT learning diary 7 - qmainwindow
Why does LabVIEW lose precision in floating point numbers
解决uniapp列表快速滑动页面数据空白问题
JS -- take a number randomly from the array every call, and it cannot be the same as the last time
TIPC addressing 2
Rest (XOR) position and thinking
三.芯片啟動和時鐘系統
deepTools对ChIP-seq数据可视化
Is it safe to open a stock account online? I'm a novice, please guide me
ROS lacks catkin_ pkg