📜 [專欄新文章] Gas Efficient Card Drawing in Solidity
✍️ Ping Chen
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Assign random numbers as the index of newly minted NFTs
Scenario
The fun of generative art NFT projects depends on randomness. The industry standard is “blind box”, where both the images’ serial number and the NFTs’ index are predetermined but will be shifted randomly when the selling period ends. (They call it “reveal”) This approach effectively solves the randomness issue. However, it also requires buyers to wait until the campaign terminates. What if buyers want to know the exact card right away? We’ll need a reliable onchain card drawing solution.
The creator of Astrogator🐊 isn’t a fan of blind boxes; instead, it thinks unpacking cards right after purchase is more interesting.
Spec
When initializing this NFT contract, the creator will determine the total supply of it. And there will be an iterable function that is randomly picking a number from the remaining pool. The number must be in range and must not collide with any existing ones.
Our top priority is accessibility/gas efficiency. Given that gas cost on Ethereum is damn high nowadays, we need an elegant algorithm to control gas expanse at an acceptable range.
Achieving robust randomness isn’t the primary goal here. We assume there’s no strong financial incentive to cheat, so the RNG isn’t specified. Implementers can bring their own source of randomness that they think is good enough.
Implementation
Overview
The implementation is pretty short and straightforward. Imagine there’s an array that contains all remaining(unsold) cards. When drawIndex() is called, it generates a (uniform) random seed to draw a card from the array, shortens the array, and returns the selected card.
Algorithm
Drawing X cards from a deck with the same X amount of cards is equal to shuffling the deck and dealing them sequentially. It’s not a surprise that our algorithm is similar to random shuffling, and the only difference is turning that classic algo into an interactive version.
A typical random shuffle looks like this: for an array with N elements, you randomly pick a number i in (0,N), swap array[0] and array[i], then choose another number i in (1,N), swap array[1] and array[i], and so on. Eventually, you’ll get a mathematically random array in O(N) time.
So, the concept of our random card dealing is the same. When a user mints a new card, the smart contract picks a number in the array as NFT index, then grabs a number from the tail to fill the vacancy, in order to keep the array continuous.
Tweak
Furthermore, as long as the space of the NFT index is known, we don’t need to declare/initialize an array(which is super gas-intensive). Instead, assume there’s such an array that the n-th element is n, we don’t actually initialize it (so it is an array only contains “0”) until the rule is broken.
For the convenience of explanation, let’s call that mapping cache. If cache[i] is empty, it should be interpreted as i instead of 0. On the other hand, when a number is chosen and used, we’ll need to fill it up with another unused number. An intuitive method is to pick a number from the end of the array, since the length of the array is going to decrease by 1.
By doing so, the gas cost in the worst-case scenario is bound to be constant.
Performance and limitation
Comparing with the normal ascending index NFT minting, our random NFT implementation requires two extra SSTORE and one extra SLOAD, which cost 12600 ~ 27600 (5000+20000+2600) excess gas per token minted.
Theoretically, any instantly generated onchain random number is vulnerable. We can restrict contract interaction to mitigate risk. The mitigation is far from perfect, but it is the tradeoff that we have to accept.
ping.eth
Gas Efficient Card Drawing in Solidity was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
「array at is not a function」的推薦目錄:
array at is not a function 在 AOPEN Taiwan Facebook 的最佳解答
人工智慧=未來趨勢?! AOPEN在浪潮跟你一起IoT🤣🤣
#李開復先生怎麼說
#各位看官意下如何
#人工智慧這樣行👊👊
#偵測到你無所遁形
#人潮就是錢潮🤑🤑🤑🤑🤑
#AOPEN #建碁
【李開復Quartz專文:「人形機器人」將進入千家萬戶是無稽】人工智能時代的到來已經指日可待了。但是目前情感機器人、家庭機器人都離我們很遠。下面是外媒Quartz跟我的約稿:
▲ 機器人能幫著賺錢、省錢、提高生產力,也助人們回歸人性
人工智能時代的到來已經指日可待了。但是目前情感機器人的發展方向有點南轅北轍。
首先,讓我們明確一下人工智能的幾個要點:
人工智能擅長對目標明確的工作進行優化(但是不能創造,沒有感情)。
機械控制的發展速度較人工智能軟件的發展要緩慢得多。
傳感器雖然得到迅猛發展,但價格昂貴、體積偏大且太耗電。
鑒於以上原因,人形機器人將馬上進入千家萬戶的說法,簡直是無稽之談。當機器人在言談舉止各方面都與人類極其相似時,普通家庭用戶對機器人的「人類素質」的期望也會變得高不可攀。僅僅這種期望所帶來的失望就足以讓很多公司的「未來十年讓科幻小說成為現實」的展望受挫,更別提消費市場對價格的苛刻要求了。
機器人的開發要牢記實用性這一原則:機器人或能創造效益,或能節省成本,或能提高生產,或可以提供娛樂。依託現有技術製造的工業機器人將高效製造出其它機器人;商用機器人將會帶來更多經濟收益(例如替代保安、前台和司機等職位);家用機器人將能發揮家用電器和玩具的功能——它們簡單易用且不具備任何「人性素質」。
這樣的機器人未必具備人類外形。工業機器人就是在黑暗廠房(例如富士康最先進的廠房)或者配備了智能升降機倉庫里(例如我們投資的開源機器人Dorabot)從事勞務的機器;商用機器人的形式和用途就更多樣了:它們也許就是一排攝像頭(例如曠視科技的產品),或者是一家自動商店(例如F5未來商店)。自動駕駛車將有車的外形——除了那種低速貨運、功能固定的運輸工具,例如機場鋪設的自動車道,或者從停車場到商店、主題公園的運輸設備(例如UISEE馭勢科技);消費機器人也許會像一個揚聲器(例如亞馬遜的Echo)、一台電視機、一台吸塵器(例如Roomba)、一個教學玩具(例如奇幻工房的Dash Bot)或者一台用於家庭聯繫的平板電腦(例如小魚在家)。
人工智能也會與時俱進嗎?這一點毋庸置疑。聲音識別技術將更精准,電腦視覺技術也會提高,SLAM技術將讓機器人的動作更加流暢,機器人將會翻譯,還會針對限定領域進行對話。機器人也可能會瞭解我們的情緒並能模仿人類的情緒。這種情緒模仿將從搞笑的、娛樂性的發展為一定程度上能產生共鳴的模仿。誠然,這種模仿也都不是自發性的。在未來數十年,機器人還不能獨立進行常識性的推理、創造及規劃工作,它們也不會擁有自我意識、情感及人類的慾望。那種「全知全能人工智能」尚不存在,而且現在已知的開發技術也無法開發出此類機器人。這種技術在未來數十年都不會出現,也許永遠都不會出現。
人形機器人的研發對人工智能科學家充滿了誘惑力,而對人形機器人的預測也順理成章地激發著科幻小說家們的創作靈感。但是我們和人工智能有著本質區別:我們會創造,AI只會在創造的基礎上優化;我們多愁善感,AI冷酷無情;我們具備常識判斷能力,而AI只會從特定領域的大數據獲得信息。一言以蔽之,人類所長正是AI所短,而AI所長也是人類所短。
展望未來,人類最前沿的領域將是創造及社交領域。因此,我們應該推動機器人向它們所擅長的領域發展,例如進行重復性工作、優化工作或者創造財富的實用性工作。而我們人類也應該做一些我們擅長的工作:創新、創造、社交溝通或者娛樂。
我一直倡導要開發一些實用性機器人,鼓勵人們進入服務行業。但我不支持製造「類人」機器人。這種機器人開發難度大,而且永遠無法滿足人們的期望,因此,這種機器人的勝算不大。我分析的正確與否暫且不論,但是有一點我們需要有清晰的認識,那就是,未來十年,AI將大規模地取代那些依靠人力的、重復性的、分析性的崗位。因此,我們要肩負起創造更多社會服務性崗位的職責,而不是空想或謀劃一個充斥著「不適用於人類」職位的社會。
◀英文原文▶
Robots should make money, save money, increase productivity, or deliver entertainment—and let humans be human
Robots should make money, save money, increase productivity, or deliver entertainment—and let humans be human
The age of artificial intelligence (AI) and robotics is upon us, but the current fad of emotional humanoid robots is not headed in the right direction.
First, let’s understand what robotics are based on:
AI algorithms which are very good at optimization of explicitly defined goals (but cannot create, and have no feelings)
Mechanical control which advances much slower than AI software algorithms
Sensors which are rapidly improving but are often still too expensive, too large, or too power-hungry
Given the above, it is ludicrous to think that human-like robots will roam our homes any time soon. When a robot looks like a person, talks like a person, and has features like a person, home users will have unattainable human-capability expectations. The disappointment alone will doom any company hoping to bring science fiction to the living room in the next decade, not to mention the price-sensitivity for consumer markets.
Robotics must begin with utilitarianism in mind—robots should make money, save money, increase productivity, or deliver entertainment. There will be industrial robots that build other robots in high-volume, manufactured with today’s technologies. There will be commercial robots that deliver economic value (such as replacing security, receptionists, and drivers). There will be consumer robots that mimic today’s appliances and toys, requiring no consumer education, and causing no human-capability expectation.
These robots won't look like a person. The industrial robot is a giant factory run in the dark by machines (like at Foxconn’s most advanced factories), or a warehouse with smart forklifts (like our investment Dorabot). The commercial robot comes in various forms and applications. It might look like an array of cameras (like our investment Megvii) or an automated store (like our investment F5 Future Store). The autonomous vehicle will look like a car, except will be first deployed in low-speed, freight, or fixed-function transport—such as in airport autonomous car-only lanes, or in transport from parking garages to shopping malls/theme parks (like our investment UISee). And the consumer robot may look like a speaker (like the Amazon Echo), a TV, a vacuum cleaner (like Roomba), an educational toy (like our investment Wonder Workshop Dash Bot), or a pad-on-steroids for family communications (like our investment Ainemo).
Will AI capabilities increase over time? Of course. Speech recognition will get better, computer vision will improve, SLAM will be improved to help the robot move around fluidly, and the robot will be able to translate languages, or have a dialog within limited domains. The robot may be able to read some of our emotions, or mimic certain human emotions. But this mimicking will go from laughable and entertaining to occasionally acceptable—and generally not genuine. For decades to come, robots by themselves will be unable to learn common sense reasoning, creativity, or planning. They also won't possess the self-awareness, feelings, and desires that humans do. This type of “general AI” does not exists, and there are no known engineering algorithms for it. I don’t expect to see those algorithms for decades, if ever.
Trying to make robots human-like is a natural temptation for robotics and AI scientists, and predicting humanoid robots comes naturally to science fiction writers. But we humans simply think differently from AI. We create and AI optimizes. We love and AI is stoic. We have common sense and AI learns patterns from big data in a singular domain. Simply stated, we are good at what AI is not, and AI is good at what we are not.
In the future, the human edge will be in creativity and social interaction. Therefore, we need to focus robotics development toward what they’re good at: repetitive tasks, optimization, and utilitarian value creation. We should also let people do what they’re good at: innovation, creation, human-to-human interaction, and performing services.
I am an advocate of making utilitarian robots, and encouraging people to go into service jobs. I am not an advocate of making humanoid service robots—it is too hard today, and will not meet people’s expectations; therefore they will likely fail. Whether or not my analysis is correct, we need to be reminded that in the next decade AI will replace a massive number of manual-labor, repetitive, and analytical jobs. We have a human responsibility to help create societal service jobs—not dream or plan a society in which all jobs come with a sign “humans need not apply.”