區塊鏈入門教程以太坊源碼分析以太坊隨機數生成方式二

  區塊鏈入門教程以太坊源碼分析以太坊隨機數生成方式二。
激勵

RNG的週期非常短,例如一個小時20個生成周期,如果沒有周期的利潤是0.001%,一個月的盈利會達到0.00001 20 24 30 = 0.144。 爲了達到14.4%每個月的盈利,並且RNG平均有n個參與者,運行智能合約C的費用爲 n 3 500 gasPrice + Ccost.(CCost是合約內部的gas消費,包括計算和存儲)假設每個隨機值平均有r個請求,每個請求的費用是 p ETH, 那麼收入是 rp. 所以每個參與者每一次參與會收到rp - 1500n gasPrice - Ccost) / n。當前的gasPrice是10 szabo, 合約的消費大概是1500n gas, 所以大概的淨收入是(rp/n-0.03)ETH. 假設每個RNG有10個參與者,並且抵押是1000ETH,所以如果RNG如果只請求一次,那麼一次的費用是0.4 ETH, 如果請求是10次,那麼一次請求的價格會被降到0.04ETH

The RANDAO acts as an infrastructure in the Ethereum system. It is called by other contracts. Contracts for different purposes require different random numbers: some need high security, such as lottery; some need steady responses and the request should be responded immediately, these contracts are normally low-value; some need a callback, they want to receive a notification with random numbers when numbers are ready.

Obviously it's impossible to meet different requirements in various scenarios with only one RNG contract, so a lot of contracts will be created with different initial parameters, but the basic rules are the same.

RANDAO作爲以太坊系統的基礎設施。被其他的合約調用。不同的合約因爲有不同的目的所以需要不同的隨機值:有些需要高度加密的,比如說抽獎;有些需要穩定的迴應,並且要求立即作出迴應,這些合約本身的價值不高;有些需要回調函數,當隨機值已經生成的時候需要接收到通知。

很明顯通過單一的RNG合約不可能滿足所有的不同的請求,所以使用了不同的初始值創建了很多智能合約,不過他們基本的規則是相同的。

For example, if we need high security, we can substantially increase the pledge of the first phase. Thus, the cost of leading to failure of RNG process by not revealing s is greatly increased. And for the contracts without much interest involved, the minimum number of participants and the pledge can be lower.

Let's look at an example of a dApp betting on odd or even numbers, we'll show how to adjust the contract's parameters to meet the desired security level, by making the cost of cheating higher than expected earnings. Assuming the bet is 1000 ETH, the betting contract calls a RNG contract C1, if C1 failed to generate a random number at requested block height, then betting contract waits for the next random number of C1, until there is one generated.

比如,如果你需要高度安全,我們可以大大的增加第一階段的抵押。這樣不提供s的導致失敗的概率會大大降低。對於那麼資金不是很充足的合約,那麼參與者的最小個數和抵押都可以降低。

讓我們看一個dapp的例子,這個例子用來賭數的奇數和偶數,我們會顯示如何調整合約的參數來匹配適合的安全程度,通過讓造假的成本大大高於收益。假設打賭是1000ETH,這個打賭的合約調用了RNG的合約C1, 如果C1在請求的區塊高度生成隨機數失敗了,打賭的合約會等待C1的下一個隨機數,直到有一個生成成功。

Let's build the RNG contract C1, and set the pledged ETH of C1 to 2000. The gambler G plays the betting dApp but also participates in the contract. When he finds himself in a disadvantageous position before he reveals his secret number, he can choose not to reveal s, so that the RNG failed and he got another chance. But he will lose the 2000 pledged ETH, so although he can get 1000 ETH expected return, it is still a bad deal. However, G can reduce his losses on C1 by some means, such as participating in C1 using two accounts, sending two sha3(s). if in a disadvantageous position, G will keep only one account's secret, and if only one participant expect G participate to in C1, G will only lose 1000 ETH in C1, but G will get 1000 ETH as expected return, which is a worthy try.

讓我們構建RNG智能合約C1, 並且設置抵押的值是2000。 賭徒G參與了dApp的賭注,同時參與了RNG的智能合約。在他提交s之前,發現自己處在不利的狀態。他可以選擇不提交自己的s,這樣RNG會失敗,他會得到下一個機會。 但是他會損失2000ETH的抵押,儘管他可以得到1000ETH的賭注,所以這樣並不是一個好的交易。然而賭徒G可以使用其他的方式來減少損失,比如G可以使用兩個賬號參與RNG,發送兩個sha3(s).如果在不利的狀態,G會讓一個賬號不提交s,這樣如果除了G之外只有另外一個其他的賬號,G只會在G1上面損失1000ETH,但是G如果賭贏了可以得到1000ETH,所以也值得一試。

This issue can be fixed by confiscating the pledged ETH, and not return them to participants as bonus. so a contract with 1000 pledged ETH will meet the requirement of the betting dApp.

這種情況可以通過沒收所有抵押來修復,不會把他們作爲獎勵返回。所以一個1000抵押的合約會符合×××的要求。

Besides confiscation, another scheme can prevent such attacks by introducing an additional system: RANDAO membership. To become a member you must pay dues, anyone paid their dues is a member. Members have different levels according to the dues they paid. Membership does not belong to a contract, but instead functions like a passport to participate in some RANDAO contracts. If a breach of any contract happens, that person's membership will be ended and the dues will be confiscated. Now we can add an additional agreement to C1, C1 will only accept numbers committed by members whose level of investment is high enough (membership dues over 1000 ETH). This will ensure that nobody has a financial motive to try an attack.

除了沒收,還有一個方案可以阻止這種***,那就是 RANDAO membership。 爲了成爲成員,你必須繳納成員費用。根據成員繳納的費用的多少把成員分成不同的等級, 成員系統不屬於智能合約,而是作爲一種類似護照的形式來參與一些RANDAO合約。 如果發生違約情況,這個成員的會員資格會被終止,成員會用會被沒收。現在我們可以給智能合約C1增加一個額外的協議,C1只接受會員會用大於一定值的成員來參與。 這樣來保證沒有任何人會有財務動機來發動***。

QA: Quest and Answer

Q: Why not let the miners participate in RNG? Why not use tx hash, nonce and other blockchain data? A: Miners have the ability to manipulate these blockchain data, and thus can indirectly affect RNG. If RNG contains blockchain data, it will give the miners capacity to construct random numbers in their favor.

Q: 爲什麼不讓礦工來參與到RNG中? 爲什麼不使用txhash,nonce或者其他區塊鏈數據? A:礦工有能力才操縱這些區塊鏈數據,而這些會對RNG產生影響。如果RNG包含了區塊鏈數據,會給予礦工按照自己的行爲構造隨機數的能力。

Q: the miners can ignore certain transactions that contain random number they dislike, how to deal with that? A: That's why we need a time window period. A reasonable period should be greater than 6 blocks, we believe that nobody can produce 6 blocks in succession. So if the participant is honest, and he send numbers immediately as long as each time window open, he doesn't need to worry about being excluded.

Q: 礦工有能力忽略特定的包含了隨機數的交易,如何處理這種情況? A: 這就是爲什麼我們需要時間間隔。 一個合理的時間間隔會大於6個區塊,我們任務沒有人能連續生成6個區塊。 所以如果參與者是忠誠的,而且在時間窗口內發送了那個數字, 那麼他不同擔心會被礦工排除在外。

Q: Why use all numbers of all participants, rather than a subset? A: The rule to pick a subset is deterministic, so participants will try to take specified position of the collection by various means, if they succeed, they will know in advance what the random number is generating from subsets. If the rule to pick a subset is randomised, then we still have the problem of true randomisation.

Q: 爲什麼使用所有的參與者的所有的值,而不是其子集? A: 選擇一個子集的規則是確定性的,所以參與者將嘗試通過各種方式來採集指定的集合位置,如果它們成功,他們將事先知道從子集中產生的隨機數。 如果選擇一個子集的規則是隨機的,那麼我們仍然存在真正的隨機化問題。

Q: Where does pledged dues go? A: It will be donated to a charity, or RANDAO to maintain funding. Q: 沒收的費用去哪了。 會捐獻給慈善機構,或者是RANDAO會維護一個基金。

Note: f(s1, s2, ..., sn) is a function with multiple inputs, for example r = s1 xor s2 xor s3 ... xor sn, or r = sha3(sn + sha3(sn-1 + ... (sha3(s2 + s1))))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章