当前位置:网站首页>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 .
边栏推荐
- 在连接mysql数据库的时候一直报错
- ros缺少xacro的包
- Win11 arm system configuration Net core environment variable
- Is bond fund safe? Does the bond buying foundation lose principal?
- [play with FPGA learning 5 in simple terms ----- reset design]
- MySQL比较运算符IN问题求解
- 【IDEA】使用插件一键逆向生成代码
- PLC-Recorder快速监控多个PLC位的技巧
- 念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研
- Some things configured from ros1 to ros2
猜你喜欢

Solve the problem of data blank in the quick sliding page of the uniapp list

Tdsql | difficult employment? Tencent cloud database micro authentication to help you

A white hole formed by antineutrons produced by particle accelerators

JS -- take a number randomly from the array every call, and it cannot be the same as the last time

Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)

map集合赋值到数据库

CentOS8之mysql基本用法

PHP tea sales and shopping online store

Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting

QT learning diary 7 - qmainwindow
随机推荐
Is it safe to open a stock account online? I'm a novice, please guide me
Internship report skywalking distributed link tracking?
RPA进阶(二)Uipath应用实践
TIPC addressing 2
JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
Regular and common formulas
C#多维数组的属性获取方法及操作注意
Pit of the start attribute of enumrate
Is it safe to open a stock account through the QR code of the securities manager? Or is it safe to open an account in a securities company?
Order by injection
js中给数组添加元素的方法有哪些
由粒子加速器产生的反中子形成的白洞
原生方法合并word
The difference between self and static in PHP in methods
Approximate sum count (approximate
map集合赋值到数据库
Is bond fund safe? Does the bond buying foundation lose principal?
[cloud native] 2.5 kubernetes core practice (Part 2)
CentOS8之mysql基本用法
The working day of the month is calculated from the 1st day of each month