📜 [專欄新文章] Reason Why You Should Use EIP1167 Proxy Contract. (With Tutorial)
✍️ Ping Chen
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
EIP1167 minimal proxy contract is a standardized, gas-efficient way to deploy a bunch of contract clones from a factory.
1. Who may consider using EIP1167
For some DApp that are creating clones of a contract for its users, a “factory pattern” is usually introduced. Users simply interact with the factory to get a copy. For example, Gnosis Multisig Wallet has a factory. So, instead of copy-and-paste the source code to Remix, compile, key in some parameters, and deploy it by yourself, you can just ask the factory to create a wallet for you since the contract code has already been on-chain.
The problem is: we need standalone contract instances for each user, but then we’ll have many copies of the same bytecode on the blockchain, which seems redundant. Take multisig wallet as an example, different multisig wallet instances have separate addresses to receive assets and store the wallet’s owners’ addresses, but they can share the same program logic by referring to the same library. We call them ‘proxy contracts’.
One of the most famous proxy contract users is Uniswap. It also has a factory pattern to create exchanges for each ERC20 tokens. Different from Gnosis Multisig, Uniswap only has one exchange instance that contains full bytecode as the program logic, and the remainders are all proxies. So, when you go to Etherscan to check out the code, you’ll see a short bytecode, which is unlikely an implementation of an exchange.
0x3660006000376110006000366000732157a7894439191e520825fe9399ab8655e0f7085af41558576110006000f3
What it does is blindly relay every incoming transaction to the reference contract 0x2157a7894439191e520825fe9399ab8655e0f708by delegatecall.
Every proxy is a 100% replica of that contract but serving for different tokens.
The length of the creation code of Uniswap exchange implementation is 12468 bytes. A proxy contract, however, has only 46 bytes, which is much more gas efficient. So, if your DApp is in a scenario of creating copies of a contract, no matter for each user, each token, or what else, you may consider using proxy contracts to save gas.
2. Why use EIP1167
According to the proposal, EIP is a “minimal proxy contract”. It is currently the known shortest(in bytecode) and lowest gas consumption overhead implementation of proxy contract. Though most ERCs are protocols or interfaces, EIP1167 is the “best practice” of a proxy contract. It uses some EVM black magic to optimize performance.
EIP1167 not only minimizes length, but it is also literally a “minimal” proxy that does nothing but proxying. It minimizes trust. Unlike other upgradable proxy contracts that rely on the honesty of their administrator (who can change the implementation), address in EIP1167 is hardcoded in bytecode and remain unchangeable.
That brings convenience to the community.
Etherscan automatically displays code for EIP1167 proxies.
When you see an EIP1167 proxy, you can definitely regard it as the contract that it points to. For instance, if Etherscan finds a contract meets the format of EIP1167, and the reference implementation’s code has been published, it will automatically use that code for the proxy contract. Unfortunately, non-standard EIP1167 proxies like Uniswap will not benefit from this kind of network effect.
3. How to upgrade a contract to EIP1167 compatible
*Please read all the steps before use, otherwise there might have problems.
A. Build a clone factory
For Vyper, there’s a function create_with_code_of(address)that creates a proxy and returns its address. For Solidity, you may find a reference implementation here.
function createClone(address target) internal returns (address result){ bytes20 targetBytes = bytes20(target); assembly { let clone := mload(0x40) mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) mstore(add(clone, 0x14), targetBytes) mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) result := create(0, clone, 0x37) }}
You can either deploy the implementation contract first or deploy it with the factory’s constructor. I’ll suggest the former, so you can optimize it with higher runs.
contract WalletFactory is CloneFactory { address Template = "0xc0ffee"; function createWallet() external returns (address newWallet) { newWallet = createClone(Template); }}
B. Replace constructor with initializer
When it comes to a contract, there are two kinds of code: creation code and runtime code. Runtime code is the actual business logic stored in the contract’s code slot. Creation code, on the other hand, is runtime code plus an initialization process. When you compile a solidity source code, the output bytecode you get is creation code. And the permanent bytecode you can find on the blockchain is runtime code.
For EIP1167 proxies, we say it ‘clones’ a contract. It actually clones a contract’s runtime code. But if the contract that it is cloning has a constructor, the clone is not 100% precise. So, we need to slightly modify our implementation contract. Replace the constructor with an ‘initializer’, which is part of the permanent code but can only be called once.
// constructorconstructor(address _owner) external { owner = _owner;}// initializerfunction set(address _owner) external { require(owner == address(0)); owner = _owner;}
Mind that initializer is not a constructor, so theoretically it can be called multiple times. You need to maintain the edge case by yourself. Take the code above as an example, when the contract is initialized, the owner must never be set to 0, or anyone can modify it.
C. Don’t assign value outside a function
As mentioned, a creation code contains runtime code and initialization process. A so-called “initialization process” is not only a constructor but also all the variable assignments outside a function. If an EIP1167 proxy points to a contract that assigns value outside a function, it will again have different behavior. We need to remove them.
There are two approaches to solve this problem. The first one is to turn all the variables that need to be assigned to constant. By doing so, they are no longer a variable written in the contract’s storage, but a constant value that hardcoded everywhere it is used.
bytes32 public constant symbol = "4441490000000000000000000000000000000000000000000000000000000000";uint256 public constant decimals = 18;
Second, if you really want to assign a non-constant variable while initializing, then just add it to the initializer.
mapping(address => bool) public isOwner;uint public dailyWithdrawLimit;uint public signaturesRequired;
function set(address[] _owner, uint limit, uint required) external { require(dailyWithdrawLimit == 0 && signaturesRequired == 0); dailyWithdrawLimit = limit; signaturesRequired = required; //DO SOMETHING ELSE}
Our ultimate goal is to eliminate the difference between runtime code and creation code, so EIP1167 proxy can 100% imitate its implementation.
D. Put them all together
A proxy contract pattern splits the deployment process into two. But the factory can combine two steps into one, so users won’t feel different.
contract multisigWallet { //wallet interfaces function set(address[] owners, uint required, uint limit) external;}contract walletFactory is cloneFactory { address constant template = "0xdeadbeef"; function create(address[] owners, uint required, uint limit) external returns (address) { address wallet = createClone(template); multisigWallet(wallet).set(owners, required, limit); return wallet; }}
Since both the factory and the clone/proxy has exactly the same interface, no modification is required for all the existing DApp, webpage, and tools, just enjoy the benefit of proxy contracts!
4. Drawbacks
Though proxy contract can lower the storage fee of deploying multiple clones, it will slightly increase the gas cost of each operation in the future due to the usage of delegatecall. So, if the contract is not so long(in bytes), and you expect it’ll be called millions of times, it’ll eventually be more efficient to not use EIP1167 proxies.
In addition, proxy pattern also introduces a different attack vector to the system. For EIP1167 proxies, trust is minimized since the address they point to is hardcoded in bytecode. But, if the reference contract is not permanent, some problems may happen.
You might ever hear of parity multisig wallet hack. There are multiple proxies(not EIP1167) that refer to the same implementation. However, the wallet has a self-destruct function, which empties both the storage and the code of a contract. Unfortunately, there was a bug in Parity wallet’s access control and someone accidentally gained the ownership of the original implementation. That did not directly steal assets from other parity wallets, but then the hacker deleted the original implementation, making all the remaining wallets a shell without functionality, and lock assets in it forever.
https://cointelegraph.com/news/parity-multisig-wallet-hacked-or-how-come
Conclusion
In brief, the proxy factory pattern helps you to deploy a bunch of contract clones with a considerably lower gas cost. EIP1167 defines a bytecode format standard for minimal proxy and it is supported by Etherscan.
To upgrade a contract to EIP1167 compatible, you have to remove both constructor and variable assignment outside a function. So that runtime code will contain all business logic that proxies may need.
Here’s a use case of EIP1167 proxy contract: create adapters for ERC1155 tokens to support ERC20 interface.
pelith/erc-1155-adapter
References
https://eips.ethereum.org/EIPS/eip-1167
https://blog.openzeppelin.com/on-the-parity-wallet-multisig-hack-405a8c12e8f7/
Donation:
pingchen.eth
0xc1F9BB72216E5ecDc97e248F65E14df1fE46600a
Reason Why You Should Use EIP1167 Proxy Contract. (With Tutorial) was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有2部Youtube影片,追蹤數超過8萬的網紅葉宇峻彈吉他,也在其Youtube影片中提到,#葉宇峻彈吉他 #吉他教學 和我一起開箱 Audient iD14 錄音卡! 謝謝 #台灣高空 ! ============= 訂閱葉宇峻彈吉他▶https://goo.gl/3g7gn6 FB▶https://goo.gl/66igqb IG▶https://bit.ly/2KM96QT 加入...
c# interface教學 在 Taipei Ethereum Meetup Facebook 的精選貼文
📜 [專欄新文章] The next generation Ethereum Virtual Machine — Ewasm VM
✍️ Peter Lai
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
The next generation Ethereum Virtual Machine — Ewasm VM
The next generation Ethereum Virtual Machine — Crosslink 2019 Taiwan
這篇文章是 Crosslink 2019 Taiwan 的一個議程紀錄:The next generation Ethereum Virtual Machine,由來自 Second State 的工程部 VP Hung-Ying Tai 分享 Ewasm VM 目前研究內容及未來的方向,內容非常精彩,包含了 EVM bytecode 、 Webassembly、Ewasm1.0 以及 Ewasm2.0 。
EVM bytecode 及 Webassembly(WASM)
以太坊的智能合約交易在執行時,例如 :轉 Token 到別的地址,我們是將 EVM bytecode 讀進以太坊的虛擬機執行,而 EVM bytecode 有以下幾點特色:
256 位元且堆疊式(staked-based)的虛擬機
很多高階的指令,例如:SSTORE, SLOAD, SHA3, EC, Call/Create contract
與實體系統架構(通常是 32/64 位元)有差異,而 256 位元則需要靠模擬來完成
較少程式語言(Vyper, Solidity, …)
Webassembly(WASM)是為了讓不同程式語言開發的套件都能在瀏覽器使用的一種二進位程式語言,WASM 有以下幾點特色:
堆疊式(staked-based)的虛擬機:有獨立的區域空間(暫存器或是記憶體),存取堆疊前 3 個物件(EVM 存取 16 個)
支持 32 / 64 位元的操作
沒有高階的指令
RISC 指令集也可以對應到 CPU ISA
較大的社群:主流的瀏覽器都支援,也有較多的程式語言(C++, Rust, GO, …)
Ewasm 1.0
接下來看看以太坊 Ewasm 的特性:
Ewasm 是 wasm 的子集合
因為不能有誤差,所以不支援浮點數運算
只能 import 以太坊的函式庫,避免 importㄒ系統函式庫
在每段指令之前插入 useGAS 來計算 GAS 的花費
Ethereum Environment Interface
EVM 裡有很多像是 SSLOAD, SHA3 的高階指令,這些指令在 Ewasm 1.0 裡,因為 WASM 可以動態讀取函式庫(模組),以太坊定義了 Ethereum Environment Interface 讓客戶端可以用不同的語言實作相對應的函示庫,而且也更容易完成 prototype 跟升級。
下圖是 Ethereum Environment Interface 定義的函數列表。
Ethereum Environment Interface Definition.
如何移除非法的指令?
Ewasm 使用 system contract 移除非法指令以及加入 useGas 的 bytecode,像是浮點數或是非法的 import,有以下兩種做法:
使用 smart contract 檢查合約的 bytecode
像目前的 precompiles 運行在客戶端上,在部署前先檢查合約
下圖是 Ewasm 1.0 的 stack,在部署合約前 Ewasm bytecode 會先經過 Sentinal 的檢查,成功部署後客戶端如果要執行合約會透過 EVM-C 跟 Heru(Wasm Engine)溝通。
Ewasm Stack
效能問題
究竟使用 Ewasm 效能真的會比較快嗎?講者分享各 EVM 執行 Sha1 以及 BN128mul 的結果,可以發現 EVM 在運行 BN128mul 時會是最快,主要是因為 WASM 支持 32 / 64 位元的操作,256 位元則需要另外模擬(1 個 256 位元的運算可以換成 25 個 64 位元的運算),所以 WASM 在跑 BN128mul 時才會比較慢。
Ewasm 2.0
Ewasm 2.0 的智能合約改叫 Execution Environments(EE),與 Ewasm 1.0 不一樣的有下列幾點
EE 全部都是 WASM 寫的
因為支援 cross shard,每個 EE 都是在一個 shard 上執行
EE 只能拿到 state root,而在合約的執行寫法也跟原來不一樣
EE 是 stateless
下圖可以看到 ERC20 Token 在 Ewasm 2.0 跟 Ewasm 1.0 storage 的比較,Ewasm 1.0 每個 data 都會有相對應的 key,而 Ewasm 2.0 只有存 state root,所以只能跟 state root 互動。
Ewasm 2.0 vs Ewasm 1.0
Phase One and Done
目前 Ewasm 2.0 到 phase one and done 的階段,也有測試的網路可以在 shard block 執行 EE,以太坊也有開源 Ewasm 2.0 的測試工具 Scout。
Hello World for Ewasm 2.0
上圖是 Eth 2 的 Hello World EE,可以看到 main 函數裡第一行讀取 pre state root,接下來驗證 block data size 是不是為 0,最後再將 state root 存回去,Eth 2 的智能合約寫起來都會像這樣。
結論
Ewasm 1.0 目前已經支援 EVM 1 大部分的功能也有測試鏈了,second state 開發一個編譯器 soll,能將 solidity 編譯成 Ewasm,想研究的人可以參考看看。
Ewasm 2.0 目前還在研究中,下圖是講者給大家分享的研究及貢獻的方向。
A MAYBE Roadmap
參考
Crosslink 簡報
webassembly.org
scout
soll
Ewasm overview and the precompile problem: Alex Beregszaszi and Casey Detrio @ Ethereum \\ Part 1 — YouTube
Ewasm overview and the precompile problem: Alex Beregszaszi and Casey Detrio @ Ethereum \\ Part 2 — YouTube
Wasm for blockchain&Eth2 execution: Paul Dworzanski,Alex Beregszaszi,Casey Detrio@Ethereum \\ Part 2 — YouTube
Ewasm for sharding
Ewasm updates
Ewasm design
wasm-intro
The next generation Ethereum Virtual Machine — Ewasm VM was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
c# interface教學 在 台灣物聯網實驗室 IOT Labs Facebook 的精選貼文
GitHub 最強 AI 數學解題神器!拍照上傳秒給答案,連微積分都難不倒他
Posted on2019/05/27
TO 精選觀點
【為什麼我們要挑選這篇文章】AI 應用越發奇葩,過去寫數學一支筆、一顆腦,現在連腦都不用,帶個 AI 就行。
中國 GitHub 大神研發數學 AI,透過深度學習與影像識別,打造最強解題神器。每個學生都夢寐以求的數學 AI 究竟怎麼下載?怎麼用?接著看下去。(責任編輯:陳伯安)
作者:量子位/ 曉查 銅靈
一位叫 Roger 的中國學霸小哥的拍照做題程式 mathAI 一下子火了,這個 AI,堪稱數學解題神器。
輸入一張包含手寫數學題的圖片,AI 就能辨識出輸入的數學公式,然後給出計算結果。
不僅加減乘除基本運算,就連高等數學中的微積分都不在話下。
就像下面這樣:
還在苦苦糾結高數作業如何求解?還在東奔西走的找學霸借作業?手握 mathAI,不就是手握了新時代的解題利器嗎?
此項目程式碼已半開源
短短幾天時間,這個項目在微博就收穫了上百次轉發。看到畫風如此新奇,似乎還能開啓無限可能應用,網友們紛紛召喚自己的印象筆記(中國版 Evernote)小助手收藏,大呼:以後教數學就是它了。
作者表示,這個專案已經是半開源狀態了,目前開源的部分可以辨識計算加減乘除簡單運算。
如果想要辨識更加複雜的運算式,可以參考數學公式辨識的論文自己進行擴展。
具體來看看這個解題神器。
深度學習辨識數學題,正確率逼近 80%
全能型選手 mathAI 是怎麼實現這個功能的?
作者在 Github 中介紹說,整個程式使用 python 實現,具體處理流程包括:圖像預處理 → 字元辨識 → 數學公式辨識 → 數學公式語義理解 → 結果輸出。
整個系統的處理流程如下:
圖片預處理主要以 OpenCV 作為主要工具,將圖片中的字元單獨切割出來,避免無關變數對字元辨識的影響。
隨後,國際數學公式辨識比賽資料集(CROHME)對通過卷積神經網路進行訓練。
此外,還進行結構分析,對字元的空間關係進行判定。比如一個字元的上標和下標,含義自然不一樣。
在語義分析階段,就需要匯集上面得到的資訊,判斷運算該如何進行了。節點屬性傳遞過程如下圖所示:
作者在用 160 道手寫測試題進行了測試:
結果表明,平均字元辨識率達到了 96.23%,且系統做題的平均正確率達到了 79.38%。
手把手教學怎麼用
來,實際上手操作下。
作者給出兩種使用模式:網頁模式和介面模式(Interface)。介面模式比較直觀,只需打開網頁上傳圖片即可自動給出解題結果。
下面以介面模式為例介紹一下 mathAI 的安裝使用方法。
首先需要安裝 flask、虛擬環境、科學資料庫 numpy、sympy 等,它們都可以用 pip 安裝。
pip install flask
pip install virtualenv
將項目的 lib.zip 檔解壓到系統目錄的 venv 資料夾下。(lib.zip 可以回覆 lib 獲取)
配置置好運行環境後,用 PyCharm 打開下載好的專案,在載入過程中,PyCharm 會自動安裝好專案依賴的軟體資料庫。
使用命令列進入專案所在目錄,並啓動虛擬環境:
. venv/bin/activate
將 FLASK_ENV 環境變數設置為啓用開發模式:
export FLASK_ENV=development
然後使用指令運行 flask 網站框架 :
export FLASK_APP=welcome.py
flask run
打開流覽器,在位址中輸入 127.0.0.1:5000,即可打開項目網頁。在網頁中輸入一張包含數學公式的圖片,就好返回運算結果。
目前 GitHub 專案頁上的程式碼只支持加減乘除這樣的簡單運算。
中國神人 Roger 的其他 GitHub 發明
做出這個自動求解系統的,還是一位元中國少年。
這位 GitHub ID 為 Roger,本名羅文傑,是中山大學資料科學與電腦學院的研一在讀碩士生,主要攻讀電腦視覺方向。
不僅這個解題神器,在 Roger 的 GitHub 主頁上還能看到其此前參與的很多有趣研究。
比如這個基於帖子的校園互助交友平臺 LiBond。用戶可以在裡面發佈任務,然後使用虛擬幣荔枝進行交易。
羅同學的設想是,有閒置時間的同學可以在此平臺上幫助他人,然後結交好朋友,荔枝幣還能用來兌換喜歡的物品。
再比如,一個基於 C++ 的無禁手五子棋 AI,可以通過 openGL 實現圖形介面。
在這個項目中,羅同學使用了最經典的極大極小博弈樹、alpha-beta 剪枝、置換表等演算法,還附上了核心程式碼。
確認過眼神,是學霸無疑了。
資料來源:https://buzzorange.com/techorange/2019/05/27/math-ai/…
c# interface教學 在 葉宇峻彈吉他 Youtube 的最讚貼文
#葉宇峻彈吉他 #吉他教學
和我一起開箱 Audient iD14 錄音卡!
謝謝 #台灣高空 !
=============
訂閱葉宇峻彈吉他▶https://goo.gl/3g7gn6
FB▶https://goo.gl/66igqb
IG▶https://bit.ly/2KM96QT
加入葉宇峻彈吉他頻道會員 ▶https://reurl.cc/RdQQbZ
頻道會員懶人包 ▶https://reurl.cc/lLAkz9
【使用器材】
Guitar:FUJIGEN MSA-HP-C, Corona ST, Squier Classic Vibe
Effect:Nux, Positive Bias Fx2
Cable:Providence H205 E207, Nux B-5
String:SG Strings 10-46
【我的熱門影片】https://reurl.cc/K6v059
【學吉他 彈 lick】https://reurl.cc/ObeMxR
【技巧系列教學】https://reurl.cc/XX9GOE
【和弦分析教學】https://reurl.cc/rln9zN
【破解音階指型】https://reurl.cc/31Z4rX
【五聲音階教學】https://reurl.cc/0zydz9
【調式音階教學】https://reurl.cc/al7drl
【背景音樂練習】https://reurl.cc/A1z2qj
【器材應用分析】https://reurl.cc/9zQ3XO
【藍調吉他教學】https://reurl.cc/1Qpb4W
♫《吉他伴奏大全》線上課程報名網址▶https://hahow.in/cr/paupauband ♫
【葉宇峻彈吉他現在招生中】
歡迎想要學習彈吉他,音樂的朋友們和我聯絡。
台灣師範大學流行音樂產學演奏碩士畢。
以理論建構觀念輔佐技術練習,以學習音樂的方式帶你一起進步成長。我也支援遠距視訊教學,目前已累積來自於全球各地及台灣跨地區的學生,很歡迎你私人信件或是以任何方式和我聯絡細節及上課內容。
教學項目包含:
木吉他自彈自唱/Fingerstyle演奏曲/各式風格電吉他/樂理分析/即興演奏
教學|合作邀約|各式活動|聯絡方式:
LINE:paupauband
Skype ID:paupauband2
微信:paupauband
bilibili:葉宇峻彈吉他
mail:paupauband@hotmail.com

c# interface教學 在 葉宇峻彈吉他 Youtube 的最佳解答
#nux #mg30 #guitar
Very quickly factory preset sound demo(1A~32D) with the new NUX MG-30.
20A~D tone are designed by me.
完整示範 NUX MG-30 的原廠所有音色。然後20A~D是我編寫的喔!
=============
訂閱葉宇峻彈吉他▶https://goo.gl/3g7gn6
FB▶https://goo.gl/66igqb
IG▶https://bit.ly/2KM96QT
加入葉宇峻彈吉他頻道會員 ▶https://reurl.cc/RdQQbZ
頻道會員懶人包 ▶https://reurl.cc/lLAkz9
【使用器材】
Guitar:FUJIGEN MSA-HP-C, Corona ST, Squier Classic Vibe
Effect:Nux, Positive Bias Fx2
Cable:Providence H205 E207, Nux B-5
String:SG Strings 10-46
【我的熱門影片】https://reurl.cc/K6v059
【學吉他 彈 lick】https://reurl.cc/ObeMxR
【技巧系列教學】https://reurl.cc/XX9GOE
【和弦分析教學】https://reurl.cc/rln9zN
【破解音階指型】https://reurl.cc/31Z4rX
【五聲音階教學】https://reurl.cc/0zydz9
【調式音階教學】https://reurl.cc/al7drl
【背景音樂練習】https://reurl.cc/A1z2qj
【器材應用分析】https://reurl.cc/9zQ3XO
【藍調吉他教學】https://reurl.cc/1Qpb4W
♫《吉他伴奏大全》線上課程報名網址▶https://hahow.in/cr/paupauband ♫
【葉宇峻彈吉他現在招生中】
歡迎想要學習彈吉他,音樂的朋友們和我聯絡。
台灣師範大學流行音樂產學演奏碩士畢。
以理論建構觀念輔佐技術練習,以學習音樂的方式帶你一起進步成長。我也支援遠距視訊教學,目前已累積來自於全球各地及台灣跨地區的學生,很歡迎你私人信件或是以任何方式和我聯絡細節及上課內容。
教學項目包含:
木吉他自彈自唱/Fingerstyle演奏曲/各式風格電吉他/樂理分析/即興演奏
教學|合作邀約|各式活動|聯絡方式:
LINE:paupauband
Skype ID:paupauband2
微信:paupauband
bilibili:葉宇峻彈吉他
mail:paupauband@hotmail.com
