当前位置:网站首页>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 contractB
Need to pass inA
The 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 .
边栏推荐
- TIPC messaging3
- Multi line display and single line display of tqdm
- [play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
- Is the stock account given by qiniu business school safe? Can I open an account?
- [idea] use the plug-in to reverse generate code with one click
- RPA进阶(二)Uipath应用实践
- PHP tea sales and shopping online store
- SQLite modify column type
- LVM operation
- Resources读取2d纹理 转换为png格式
猜你喜欢
在连接mysql数据库的时候一直报错
Verilog and VHDL signed and unsigned number correlation operations
2022年4月17日五心红娘团队收获双份喜报
Importerror: impossible d'importer le nom « graph» de « graphviz»
CentOS8之mysql基本用法
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
Win11 arm system configuration Net core environment variable
[play with FPGA learning 5 in simple terms ----- reset design]
随机推荐
MySQL比较运算符IN问题求解
Webauthn - official development document
Summary of data export methods in powerbi
Jenkins installation
Amazon cloud technology community builder application window opens
What are the software product management systems? Inventory of 12 best product management tools
TIPC 寻址2
Eight sorting summaries
Attribute acquisition method and operation notes of C # multidimensional array
js中给数组添加元素的方法有哪些
通过券商经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
三.芯片啟動和時鐘系統
【IDEA】使用插件一键逆向生成代码
Installation of ROS gazebo related packages
TDSQL|就业难?腾讯云数据库微认证来帮你
Never forget, there will be echoes | hanging mirror sincerely invites you to participate in the opensca user award research
PHP tea sales and shopping online store
Liftover for genome coordinate conversion
I STM32 development environment, keil5/mdk5.14 installation tutorial (with download link)
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting