Solidity

你能解釋一下這個要求條件在這個智能合約中的含義嗎?

  • April 21, 2020

我想知道這個智能合約中的這個要求條件到底是什麼:

function placeBet(uint betMask, uint modulo, uint commitLastBlock, uint commit, uint8 v, bytes32 r, bytes32 s) external payable {
       // Check that the bet is in 'clean' state.
       Bet storage bet = bets[commit];
       require (bet.gambler == address(0), "Bet should be in a 'clean' state.");

       // Validate input data ranges.
       uint amount = msg.value;
       require (modulo > 1 && modulo <= MAX_MODULO, "Modulo should be within range.");
       require (amount >= MIN_BET && amount <= MAX_AMOUNT, "Amount should be within range.");
       require (betMask > 0 && betMask < MAX_BET_MASK, "Mask should be within range.");

       // Check that commit is valid - it has not expired and its signature is valid.
       require (block.number <= commitLastBlock, "Commit has expired.");
       require (verifySignature(commitLastBlock, commit, v, r, s), "ECDSA signature is not valid.");
       uint rollUnder;
       uint mask;

       if (modulo <= MAX_MASK_MODULO) {

           rollUnder = ((betMask * POPCNT_MULT) & POPCNT_MASK) % POPCNT_MODULO;
           mask = betMask;
       } else {
           // Larger modulos specify the right edge of half-open interval of
           // winning bet outcomes.
           require (betMask > 0 && betMask <= modulo, "High modulo range, betMask larger than modulo.");
           rollUnder = betMask;
       }

       // Winning amount and jackpot increase.
       uint possibleWinAmount;
       uint jackpotFee;

       (possibleWinAmount, jackpotFee) = getDiceWinAmount(amount, modulo, rollUnder);

       // Enforce max profit limit.
       require (possibleWinAmount <= amount + maxProfit, "maxProfit limit violation.");

       // Lock funds.
       lockedInBets += uint128(possibleWinAmount);
       jackpotSize += uint128(jackpotFee);

       // Check whether contract has enough funds to process this bet.
       require (jackpotSize + lockedInBets <= address(this).balance, "Cannot afford to lose this bet.");

       // Store bet parameters on blockchain.
       bet.amount = amount;
       bet.modulo = uint8(modulo);
       bet.rollUnder = uint8(rollUnder);
       bet.placeBlockNumber = uint40(block.number);
       bet.mask = uint40(mask);
       bet.gambler = msg.sender;

       //Record bet in logs.
      emit OnCommit(commit);
      emit BetPlaced(commit, bet.gambler, bet.amount, bet.mask, bet.modulo);
   }

我想知道以下要求條件的含義:

require (bet.gambler == address(0), “Bet 應該處於 ‘乾淨’ 狀態。”);

這些是我的疑問:如何在事件發出之前從區塊鏈獲取 bet.gambler?

請指教,謝謝

當您在合約中引入變數而不為其設置值時,其值將設置為預設值。在address類型的情況下,預設值為address(0)。因此,需要檢查的是沒有為該賭徒設置明確的地址;可能這意味著賭徒根本沒有被設置。

設置變數值與事件無關——事件從不設置任何變數。該bet變數是從名為 的(可能)儲存變數bets中檢索的。該變數的狀態保存在區塊鏈中,因此對於每筆交易它都保持不變(直到某些交易修改了該狀態)。因此,該函式可以訪問該變數的預設值並檢查是否gambler已設置。

引用自:https://ethereum.stackexchange.com/questions/82657