📜 [專欄新文章] Scaling Ethereum 參賽心得
✍️ Johnson
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Scaling Ethereum 是一場由 ETHGlobal 所舉辦的線上黑客松,也是我第一次參加與以太坊有關的黑客松活動,這篇文章就來分享一人參賽的過程與心得。
源起
一開始是在 telegram 群組中得知這場比賽的消息,因緣際會之下剛好有人想組隊參賽,於是就在報名截止的前一天一起跟著報名了。
報名的方式除了填一些基本資料外,最特別的是還要 stack 以太幣,也就是要傳送 0.01 顆以太幣給主辦方,規則是必須在比賽的最後,有提交作品的人才能贖回 0.01 顆以太幣,之後看到 meme 頻道有人留言:
When your project is incomplete but you submit to get back stake.
一方面,這確實也會激勵你好好把比賽完成,就算沒做完也要有些成果上去,這也是主辦方秉持的精神,他們認為大家來黑客松相互學習成長,競賽獎金則是其次。
獎金
比賽方式是由 25 個左右的贊助者(sponsor)分別提供獎金,每個 sponsor 都有錄製一段影片,說明怎麼獲得他們的獎金,大部分會要你使用他們開發的工具,或者必須跟 sponsor 在做的研究有關,去實作出創新的作品。可參考:Prizes — Scaling Ethereum
你的專案可以選擇要投入哪個 sponsor 的獎金,一個專案可以投入多個 sponsor 底下,這樣獲獎機會也會比較高。
我選擇的 sponsor 是 zkSync,他們的說明如下:
zkSync is a user-centric zkRollup developed by Matter Labs. It uses zero-knowledge proofs to keep data availability on mainnet to achieve exponentially lower transaction costs. You may have seen us powering projects such as payments and Gitcoin Grants. We are currently rapidly developing zkSync 2.0, which will feature EVM-compatibility in testnet May 2021, soon followed by zkPorter, our new exponential scalability solution.
PrizeszkSync will be awarding their Prizes as follows:
- 1 winner — 4,000 USDC
- 2 winners — 2,000 USDC
- 4 winners — 500 USDC
We encourage builders to utilize zkSync SDK’s, implemented in JavaScript/Typescript and Rust. Prizes will be awarded to projects that make it simpler and easier for non-technical users to use zkSync, other ideas include integrations of current tools such as in Gitcoin Grants and tools for easy mass payments and multi-sigs.
社群互動
這個 hackathon 很棒的地方是他把使用者體驗做的很好。每個人都會有自己的 dashboard 顯示目前專案的進度和一些訊息。
Check-In #1 和 Check-In #2 的階段會要你提供專案的構想,你隨時都可以修改。主辦方會看你提交的資訊,幫助你找到適合的 sponsor,或是給你一些建議,就算是一人參賽也能感受到回饋。
整個賽程期間,社群都是使用 discord 在互動,discord 裡頭有很多頻道,像是基本的大會報告的頻道,或是一些不重要的迷因、閒聊頻道都有。
每個 sponsor 也都有自己的頻道,我就會在 sponsor-zksync 的頻道詢問技術的問題,例如我想問問 zkSync 一些關於專案構想的意見:
Hi there, I want to build a gas fee relayer which make my ERC-20 token transfer without transaction fee, to be more precise, delegating gas payment by another party. I think this is done by GSN https://opengsn.org/ , but maybe it could built on L2 with zkSync? I’m not sure, could somebody give me some advice about this topic?
zkSync 團隊的人回應我:
This is an amazing idea! This can totally be built, as we support batching transactions which can be used for all kinds of creative things such as paying for transaction fees in an erc-20 token. Your idea seems like a combination of that and the gitcoin grants integration. To get started, I suggest you watch the short 10 minute presentation I made on using the SDK and batching. Looking forward to your project!!
在 Check-In #2 的時候,我提交新版的專案構想,有一個欄位是問:「目前專案遇到什麼阻礙?」我的問題應該是被主辦方貼給 zkSync 的團隊,於是 zkSync 的團隊成員就用 discord 私訊我,貼了一些程式碼教我怎麼使用他們的 Javascript SDK,這突如其來的救援也幫了大忙。
除此之外,主辦方每個禮拜都會寄 email 通知一些重要的活動,賽程期間舉辦了四個 Summits 研討會,邀請世界各地有名的以太坊開發者分享議題,主辦方還有一個自己的 TV 網頁,直播所有的線上活動。這些活動都有錄影,可以在 youtube 看到過去所有的演講內容:https://www.youtube.com/c/ETHGlobal/videos
因為我的作品是使用 zkSync 的 Javascript SDK 製作的,好像也只能投稿 zkSync 作為獎金的 sponsor,不過主辦方在最後一個禮拜,也寄 email 告訴我說可以多投稿不同的 sponsors 看看,他依據我的專案構想給我一些適合的 sponsors 作為參考。
不過最後我還是只投稿了 zkSync,有點懶著再看其他 sponsors 的文件,也覺得其他 sponsors 的題目需要花比較大的功夫才能完成,一個人能力有限,就做點簡單的東西就好。
關於我的專案 — Gas Relay Service
在以太坊的世界,每一筆交易都需要額外付一筆交易費,也就是以太坊的 gas fee。
我的專案是讓「收款人」能夠幫「付款人」支付以太坊的手續費。
在黑客松之前,我就想研究「第三方支付手續費」的議題,因此我大部分時間其實都在研究一般的 meta-transactions 是怎麼實作的,有興趣的人可以看看 simple meta-transactions 的原始碼:https://github.com/chnejohnson/simple-meta-transaction
之後我才開始玩 zkSync 的 SDK,並研究怎麼在 Layer 2 實現第三方支付手續費的問題,以下就附上作品連結以及簡單的專案介紹給有興趣的人參考:https://showcase.ethglobal.co/scaling/gas-relay-service-on-zksync
The target is that token sender can choose to find another account to pay for fee. The another account can be (1) the token receiver’s account, (2) sender’s another account, (3) third party’s account.
In this project, I finished the demo, which is the (1) above, that receiver pay gas fee for the sender.
有趣的是,我在研究 meta-transactions 時學到很多智能合約的寫法,結果在最後專案上都沒用到(沒寫到合約的程式),zkSync Javascript SDK 其實很簡單,他們的文件寫得很清楚。最後 Demo 還是用 zkSync 團隊的成品修改來的…XD。
所幸在沒有懂太多技術的前提下完成了這場黑客松的專案,成功贖回了 0.01 顆以太幣。
評審與決選
整個賽程來到最後一個禮拜,主辦方安排兩天的時間進行 Judges,使用 zoom 進行線上研討會,一個人基本上是 7 分鐘,前 4 分鐘播放 Demo 簡報,後三分鐘會有評審問問題。
第一個問題是說:「Demo 中你是使用 zkSync 的錢包網頁去操作,那實際上你做得部分是什麼?」
我就回答我在他們的網頁上加了一顆按鈕,使用他們的 SDK 做出 gas relay 的功能,還有一個後端的 server 去作 relay。
第二個問題大概是問:「什麼樣的情境下會需要由 receiver 幫 sender 支付 gas fee?」
我的回答是,在一般超商購物的情境,消費者通常只支付商品的價格,不會支付額外的交易費,我認為以太坊的手續費應該屬於軟體的營運成本,由賣方支付比較適合。那如果賣方希望手續費的成本是由消費者承擔,可以直接調高商品的價格。
當然,我英文講得零零落落,希望評審有聽懂就是了…
最後一場直播就是 Finale 決選,主辦方選出十二個隊伍,公開再 Demo 一次,以及提供線上觀眾詢問問題,至此整個賽程就差不多進入尾聲。
決選後的不久,主辦方就公布了這次有獲得獎金的隊伍,幸運拿到了 zkSync 頒發的小獎~
zkSync — Matter Labs
- Zeneth — 2000 USDC
- ZeroSwap — 1500 USDC
- Kangaroo — 500 USDC
- Gas Relay Service — 500 USDC
後記
這次的參賽隊伍中,Zeneth 跟我的主題非常相似:
Zeneth — Use Flashbots to enable arbitrary meta-transactions so EOAs can enter L2s without ETH
另一個我覺得有趣的專案是 Alexandria:
Alexandria — A dApp using STARKs to verify aspects of your identity without revealing more than you should
沒想到主辦方 ETHGlobal 下個月又要再舉辦一場黑客松,有興趣的人可以看看:https://defi.ethglobal.co/ ,這次的主題是 De-Fi。
最後,只要有到 ETHGlobal 的 TV 網頁參加 Summit 研討會的直播,就能夠獲得 POAP 勳章,它就是一個酷東西~😋
POAP: Proof of Attendance Protocol
Scaling Ethereum 參賽心得 was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有10000部Youtube影片,追蹤數超過2,910的網紅コバにゃんチャンネル,也在其Youtube影片中提到,...
「c# transaction教學」的推薦目錄:
- 關於c# transaction教學 在 Taipei Ethereum Meetup Facebook 的最佳貼文
- 關於c# transaction教學 在 Taipei Ethereum Meetup Facebook 的最佳貼文
- 關於c# transaction教學 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於c# transaction教學 在 コバにゃんチャンネル Youtube 的最讚貼文
- 關於c# transaction教學 在 大象中醫 Youtube 的最佳解答
- 關於c# transaction教學 在 大象中醫 Youtube 的最讚貼文
- 關於c# transaction教學 在 菜雞新訓記(3): 使用Dapper 來連線到資料庫CRUD 吧 的評價
- 關於c# transaction教學 在 91 敏捷開發之路- SOA/microservices 架構下 - Facebook 的評價
- 關於c# transaction教學 在 How to rollback a transaction using dapper - Stack Overflow 的評價
c# transaction教學 在 Taipei Ethereum Meetup Facebook 的最佳貼文
📜 [專欄新文章] [ZKP 讀書會] Tornado Cash
✍️ Jerry Ho
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Disclaimer: 本人與Tornade Cash專案及其員工無任何利益往來。
Tornado Cash是一個Ethereum上的原生隱私轉帳解決方案,使用zk-SNARK+Merkle Tree的路徑證明作為其核心隱私保護機制。
你知我知,Ethereum上的交易記錄是公開的,這使得任何一個人只要知道你的address,便可以在https://etherscan.io/ 之類的網站上查出有多少人和這個位置進行過交易,你做過什麼消費行為或是交易行為等。
或許這聽來不像是個問題,而想要隱藏自己的交易記錄甚至聽起來反而像是不法分子的銷贓行為。
但試想下開情境:因為我曾經使用ethereum捐款給一個政治不正確的專案/組織,而我在接受dd/kyc/reference check的時候因為我的ethereum address就寫在自己的blog上而被查了個底朝天,因而被拒絕入職/拒絕開戶/拒絕服務。
這並不是一個很遙遠的情境…
Tl;dr
解決交易隱私問題分為兩個層次,Assuming你的目的是讓自己的金錢流向無法被追蹤。
層次一:我的錢「丟進了」Tornado Cash的contract,我要如何在不使用與轉入時同一個address的情況下— 若是同一個address就沒有隱私可言了 — 取出我的錢?contract如何知道我存過錢,餘額還夠,所以現在我來領錢了他讓我領?
層次二:就算層次一成立,我的隱私如何達成?到底有多隱私?到底有多不隱私?
技術上來說(細節下文詳談),層次一使用zero-knowledge的set-membership proof來證明,透過預先在Merkle Tree中「登記」一個自己的entry/leaf,tornado cash稱為note,爾後在提款時提出該leaf之zk proof,來解決這個提款時的認證問題。
層次二則是所謂的藏樹於林。既然轉出和轉入無法被連結在一起,那麼只要使用Tornado Cash的人數夠多,總轉出和總轉入的交易總筆數就會太多,以致無法輕易重新關聯轉入與轉出地址背後的真人。
使用界面
https://tornado.cash/
當然你也可以直接和合約地址互動啦
上圖左方紅框為存入幣種與金額大小,右方紅框為該額度對應之帳戶內有多少顆「樹」。
記得藏樹於林嗎?右方的 Anonymity set 就是告訴你現在森林的規模有多大。數量一大,跑資料分析試圖重新關聯某筆特定存款到某筆特定提款就變得更為困難。
提款界面如上。
值得注意的是,提款時的以上兩個選項(Wallet/Relayer),是在目前Account Abstraction尚未實現時的一個折衷方案。
這裡有個死循環:既然我提款的時候需要支付gas,那麼我的gas從哪裡來?是不是勢必得從交易所或是其他帳號來?簡言之,若是無法直接新建立一個地址然後直接將其作為Tornado Cash提款用,達到的隱私強度就大打折扣。
Relayer就是針對這個問題所設計的。透過付出一些手續費來提供社群架設relayer node的誘因,提款時該筆轉帳的gas費用,便可以讓relayer node來負責先出。relayer node收到使用者的zk proof後將其轉交給tornado cash的合約,合約就會會將應有的relayer手續費與扣除手續費+gas後的款項分別轉給relayer與使用者。
社群治理
Tornado Cash天生是一個比較沒有銅臭味的專案 — 社群治理和funded的味道相當強烈。
透過預先設計好的proxy contract與staking/locking機制,任何一個Tornado Cash的使用者都能夠提出對合約實行的改動建議,並交由社群來投票決定是否要執行該改動。
技術細節可以參照此篇,同時Tornado Cash的第一輪社群治理提案也剛投票過關,回顧可參考此處之討論。
誘因設計
本文作者比較任性不在意錢,請移駕此處閱讀官方如何設計Anonymity Mining來確保以下兩點:
機制能讓使用者願意加入存錢,提供流動性同時也讓樹林變大,增加隱私程度。
產生TORN(ERC20 token)與領取TORN的機制,透過在原本的tornado cash上面再加一層,來避免TORN激勵層錯誤的設計導致下一層之隱私洩漏(激勵層出事不影響核心隱私之意)。
技術細節
首先本文不打算解釋何為zero-knowledge proof,請接受以下描述:
若有一NP statement分類上是satisfiability problem(例:merkle tree中的hash chaining H(H(H(a,b),c),d) ),則我們可以設計出一個arithmetic circuit來確保能夠有效率的產生proof, 有效率的驗證, 無法產生假的且能說服人的proof…且其電路驗證的statement是我們想要的,像是此例中的merkle tree opening.
存款
存款者透過送出C = H(k, r) 以及存入之數額給tornado cash的合約來進行存款的動作。其中k在之後會成為存款者領錢的憑證,稱為nullifier,r則是增加randomness而已,此二值需要記下。此時合約端會將這個C(commitment)丟入Merkle Tree上其中一個空的leaf,並更新root hash。存款者還需要記下自己的C對應之leaf index。
產生proof,用此proof作為提款憑證
用一段話來概括,若是我
知道Merkle Tree上某個leaf的commitment的preimage, 代表我能在電路中證明我知道H(k, r) 中的 k, r, 同時不洩漏k, r到底是多少(zk特性, magic)。
我知道該leaf至root的路徑上會經過哪些點,我也提供了一個可以讓電路驗證root hash的hash chaining過程,代表我知道他是從哪個leaf開始走的。因而,這證明了我提出的1.中的commitment確實屬於某顆公開的、大家都知道的merkle tree中的特定leaf(就是我之前存款對應到的leaf)。
就可以在不需要提供像是原本存款地址的簽章之類的驗證機制情況下,透過zk proof,亦能正確做permission control讓unlinkable的提款能夠成真。
另,讀者可以看到在proof中已然預設了relayer的存在。這使得上開所提到之「使用者提款, 拜託relayer執行=>relayer預付gas發起transaction,將內容送給tornado cash合約=>合約處理proof並將款項拆成兩份給relayer與使用者」這個行為得以成立,且relayer無法得知或假造proof內容。
提款流程
基本上在上方的產生證明都講過一次了,這邊就是pseudo code順過一次提款流程而已,大家自己看啊。
值得一提的是,使用者除了需要提出上一部分提到的證明之外,還需要將k的部分額外拿出來再做一次H(k),將值一併傳給contract。
這裡的設計哲學,簡單來講是這樣的:zero-knowledge太強了,強到就算證明了我知道H(k, r)的k跟r, 收到的驗證者並沒有辦法知道H(k)是什麼東西。為了讓同一筆款項不會被提領兩次,在提款流程中合約會將「每一筆成功提款中的H(k)」記錄下來,另外開個表存著。爾後若是其他提款交易中的H(k)與表中的重複了,這就代表有人試圖想要騙合約重複提款,自然該提款嘗試就不會成立。
洗錢失敗例
工程師都知道使用者從來不看說明書,看了可能也不會懂。
Koh Wei Jie分析了Kucoin的駭客事件。Kucoin的駭客使用Tornado Cash來洗錢,但忽略了Tornado Cash官方一直三令五申的使用需知,因而讓款項在進入Tornado Cash跑了一輪之後還是能夠被追蹤,哈哈UCCU。
簡單來說,hacker為了節省多次使用relayer的手續費,而將大多數的提領過程都變成直接提領到wallet。雖然該wallet的位置是全新產生的沒有gas,但是透過只讓第一次的提款使用relayer,hacker便能從第一次提款中取得手續費並分發給其他全新產生的wallet address。
那問題在哪?還要問?
要達到隱私需要保持藏樹於林原則,同時使用者不應自己破壞tornado cash幫你達成的address unlinkability。這位hacker因為愛省手續費,所以違背了後者;同時他因為太心急又愛省手續費,太快、分太少次提領、每次提領的數額又太大了,所以side-channel去給他做簡單的traffic analysis就能夠用虛無假設推出:「綜觀歷史上所有的存款位置與數額,扣掉駭客存錢的那些位址之後,我們還需要14個unique address/user共謀,才能有能力一次提這麼多錢。」
這看起來可能嗎?自然是不可能的。
所以這位駭客就是錯誤的沒有遵守藏樹於林的原則,才導致自己的金流重新被和帳號聯繫在一起。
提供一些延伸閱讀,圈子內的”名人”對這種不看說明書的使用者的看法:
tornado * Gavin Andresen
如何避免洗錢失敗
我自己的投影片,我自己翻譯:
打開你的VPN 打開你的TOR 打開你的無痕瀏覽器分頁 用上你全新的VM PC VPS instance 最好連data-link layer安全都顧到 產生全新的地址不要懶惰 自己跑一個fullnode 乖乖用relayer付手續費提款 領錢之後記得把C(k,r)的記錄刪掉 不要急一次存或提領大額 時間拉長數目減少…..
簡而言之:要設計相對安全但又讓使用者可以直覺上手的安全系統真的很他媽難 - 使用者永遠會想辦法抄近路,然後系統的security assumption就爆炸了。
結論上來講,你想要多安全取決於你在臺大水源校區的腳踏車平常都上幾個大鎖=想付出多少成本。只要不要學Kucoin Hacker那樣連鎖都不鎖車還是新的,大部分時間都沒啥問題 lol。
參考資料與文中出現過的連結,不按先後順序:
https://tornado.cash/Tornado.cash_whitepaper_v1.4.pdf
https://tornado.cash/audits/TornadoCash_cryptographic_review_ABDK.pdf
https://tornado.cash/audits/TornadoCash_circuit_audit_ABDK.pdf
https://torn.community/t/whats-next-for-tornado-cash-governance/250
https://weijiek.medium.com/deanonymising-the-kucoin-hacker-418fa5e9911d
https://tornado-cash.medium.com/tornado-cash-governance-proposal-a55c5c7d0703#2084
https://eips.ethereum.org/EIPS/eip-2938
http://gavinandresen.ninja/private-thoughts
[ZKP 讀書會] Tornado Cash was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
c# transaction教學 在 Taipei Ethereum Meetup Facebook 的精選貼文
📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
c# transaction教學 在 91 敏捷開發之路- SOA/microservices 架構下 - Facebook 的推薦與評價
Developing transactional business applications using the microservice architecture is challenging, because domain models, transactions and queries are resistant ... ... <看更多>
c# transaction教學 在 How to rollback a transaction using dapper - Stack Overflow 的推薦與評價
... <看更多>
c# transaction教學 在 菜雞新訓記(3): 使用Dapper 來連線到資料庫CRUD 吧 的推薦與評價
不過就我個人認為,既然都在C# 這個環境了,又是嘗試向已知的資料表取值,取出來的值通常都還會進行進一步的 ... 不太熟悉的朋友可以參考SQL語法教學. ... <看更多>