当前位置:网站首页>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 .
边栏推荐
- Verilog and VHDL signed and unsigned number correlation operations
- TIPC 寻址2
- Xiao Sha's pain (double pointer
- Approximate sum count (approximate
- Installation of ROS gazebo related packages
- 接口调试工具概论
- 程序员成长第六篇:如何选择公司?
- Multi line display and single line display of tqdm
- 二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
- Importerror: impossible d'importer le nom « graph» de « graphviz»
猜你喜欢
![[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

Redis exceeds the maximum memory error oom command not allowed when used memory & gt; ' maxmemory'

JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个

MTK full dump grab

III Chip startup and clock system

V2x SIM dataset (Shanghai Jiaotong University & New York University)

Jinshanyun - 2023 Summer Internship

How does the whole network display IP ownership?

Always report errors when connecting to MySQL database

Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
随机推荐
Approximate sum count (approximate
Regular and common formulas
Resources reads 2D texture and converts it to PNG format
Solve the problem of data blank in the quick sliding page of the uniapp list
What are the methods of adding elements to arrays in JS
What are the software product management systems? Inventory of 12 best product management tools
Supermarket (heap overload
Rest (XOR) position and thinking
启牛商学院给的股票账户安全吗?能开户吗?
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
RPA advanced (II) uipath application practice
Liftover for genome coordinate conversion
Eight sorting summaries
对毕业季即将踏入职场的年轻人的一点建议
Xiao Sha's pain (double pointer
String (Analog
TIPC Service and Topology Tracking4
QT learning diary 8 - resource file addition
Jenkins installation