📜  Solidity – 特殊变量

📅  最后修改于: 2022-05-13 01:54:39.380000             🧑  作者: Mango

Solidity – 特殊变量

Solidity 中存在特殊的变量和函数,它们存在于全局命名空间中,主要用于提供有关区块链或实用函数的信息。它们有两种类型:

1) 区块和交易属性:

Block

Transaction Properties

block.coinbase (address payable)Current block miner’s address
block.difficulty (uint)Current block difficulty
msg.value (uint)Number of wei sent with the message
block.number (uint):Current block number
blockhash(uint blockNumber) returns (bytes32)Gives hash of the given block and will only work for the 256
most recent block due to the reason of scalability.
block.timestamp: Current block timestamp as seconds since unix epoch
gasleft() returns (uint256):Remaining gas
msg.sender (address payable)Sender of the message (current call)
msg.sig (bytes4)First four bytes of the calldata (i.e. function identifier)
now (uint)Current block timestamp (alias for block.timestamp)
tx.gasprice (uint)Gas price of the transaction
block.gaslimit (uint)Current block gaslimit
tx.origin (address payable)Sender of the transaction (full call chain)
msg.data (bytes calldata)Complete calldata

笔记:



  • 对于每次外部函数调用, msg的所有成员的值都可以更改。
  • block.timestampnowblockhash作为随机源并不安全。时间戳和区块哈希可能会受到矿工的影响。

2)ABI编解码功能:

Function

Properties

abi.decode(bytes memory encodedData, (…)) returns (…)Decodes the given data, while the types are given in parentheses as second argument.
abi.encode(…) returns (bytes memory)Encodes the given arguments
abi.encodePacked(…) returns (bytes memory)Performs packed encoding of the arguments.
abi.encodeWithSelector(bytes4 selector, …) returns (bytes memory)Encodes the given arguments starting from the second and prepends the given four-byte selector
abi.encodeWithSignature(string memory signature, …) returns (bytes memory)Equivalent to abi.encodeWithSelector(bytes4(keccak256(bytes(signature))), …)`

示例 #1:在下面的示例中,创建了一个合约来演示msg.sender作为存储卷号的安全方式。

Solidity
// Solidity program to 
// demonstrate msg.sender
pragma solidity ^0.6.6;
  
// Creating a smart contract
contract GeeksForGeeksRandom 
{
    // Creating a mapping
    mapping (address => uint) rollNo;
      
    // Defining a function to use 
    // msg.sender to store roll no.
    function setRollNO(uint _myNumber) public 
    {
        // Update our 'rollNo' mapping 
        // to store '_myNumber' under 
        // 'msg.sender'
        rollNo[msg.sender] = _myNumber;
    }
      
    // Defining a function to
    // return the roll no.
    function whatIsMyRollNumber()
             public view returns (uint) 
    {
        // Retrieve the value stored 
        // in the sender's address
        // Will be `0` if the sender 
        // hasn't called `setRollNO` yet
        return rollNo[msg.sender];
    }
}


Solidity
// Solidity program to 
// demonstrate abi.encoding
pragma solidity ^0.6.6;
  
// Creating a contract
contract GeeksForGeeks 
{    
    // Defining a function 
    // to use abi.encode()
    //It does padding to bytes
    function encode(string memory g) 
             public pure returns(bytes memory)
    {
        return abi.encode(g);
    }
      
    // encodepacked returns values in 
    // a packed way without padding
    function encodepacked(string memory g)
             public pure returns(bytes memory)
    {
         return abi.encodePacked(g);
    }
}


Solidity
// Solidity program to 
// demonstrate block.number
// and blockhash
pragma solidity ^0.4.0;
  
// Creating a contract
contract GeeksForGeeks 
{   
    // Declaring state variables
      
    // BlockNumber
    uint  BNumber; 
      
    // Hash of current block
    bytes32  BHashPresent; 
      
    // Hash of Previous Block
    bytes32  BHashPrevious; 
  
    // Defining a function to 
    // return hasdh value of 
    // the current block
    function PresentHash()
             public returns(bytes32)
    {
        BNumber = block.number;
        return BHashPresent = 
               block.blockhash(BNumber);
    }
      
    // Defining a function to 
    // return the hash value of
    // the previous block
    function PreviousHash() 
             public returns(bytes32)
    {
        BNumber = block.number;
        return BHashPrevious = 
               block.blockhash(BNumber - 1);
    }  
}


输出:

msg.sender

示例#2:在下面的示例中,使用演示变量abi.encode的函数创建了一个合约。

坚固性

// Solidity program to 
// demonstrate abi.encoding
pragma solidity ^0.6.6;
  
// Creating a contract
contract GeeksForGeeks 
{    
    // Defining a function 
    // to use abi.encode()
    //It does padding to bytes
    function encode(string memory g) 
             public pure returns(bytes memory)
    {
        return abi.encode(g);
    }
      
    // encodepacked returns values in 
    // a packed way without padding
    function encodepacked(string memory g)
             public pure returns(bytes memory)
    {
         return abi.encodePacked(g);
    }
}

输入:

Geeks

输出:

abi.encode()

示例#3:在下面的示例中,创建了一个合约来演示特殊变量block.numberblockhash。

坚固性

// Solidity program to 
// demonstrate block.number
// and blockhash
pragma solidity ^0.4.0;
  
// Creating a contract
contract GeeksForGeeks 
{   
    // Declaring state variables
      
    // BlockNumber
    uint  BNumber; 
      
    // Hash of current block
    bytes32  BHashPresent; 
      
    // Hash of Previous Block
    bytes32  BHashPrevious; 
  
    // Defining a function to 
    // return hasdh value of 
    // the current block
    function PresentHash()
             public returns(bytes32)
    {
        BNumber = block.number;
        return BHashPresent = 
               block.blockhash(BNumber);
    }
      
    // Defining a function to 
    // return the hash value of
    // the previous block
    function PreviousHash() 
             public returns(bytes32)
    {
        BNumber = block.number;
        return BHashPrevious = 
               block.blockhash(BNumber - 1);
    }  
}

输出:

block.number 和 blockhash