In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the global variables of solidity". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Layout of state variables in miscellaneous storage
Static size variables (except for mapping and dynamic size array types (other types of variables) are stored continuously from location 0. If possible, multiple entries of less than 32 bytes are compactly arranged in a single storage block, see the following rules:
The first item in the storage block is stored for low-order alignment.
Basic types use only the number of bytes that exactly store them.
If a base type does not fit the rest of the storage block, move to the next storage block.
Structure and array data always start a new block and occupy the entire block (according to these rules, structure or array items are compactly arranged).
Structure and array elements are stored one after another, just like the order in which they were declared.
Because of the unpredictable size of the allocation, the array types of mapping and dynamic size (both types) use sha3 calculations to find new starting positions to store values or array data. These starting positions are always full of blocks.
According to the above rules, the mapping or dynamic array itself is stored (not filled) in the storage block location p (or this rule is applied recursively from mapping to mapping or array). For a dynamic array, the storage block stores the number of array elements (byte arrays and strings are exceptions, see below). For mappings, the storage block is unused (but it is needed, so two identical mappings adjacent to each other will use a different hash distribution). The array data is located in sha3 (p), corresponding to a mapping key value k in sha3 (k. P) here. Is a connector). If the value is another non-basic type, the offset of the position is sha3 (k. p).
If bytes and string are of short type, they will be stored in the same storage block as their length. In particular: if the data is up to 31 bytes long, it is stored in high-order bytes (left-aligned) and low-byte storage length* 2. If it is a long type, the main storage block stores length* 2 + 1, and the data is stored in sha3 (shot).
Therefore, the paragraph of this contract is as follows:
Contract c {struct S {uint a; uint b;} uint x; mapping (uint = > mapping (uint = > S)) data;} esoteric features
In Solidity's type system, there are some types that do not have a corresponding type in the syntax. There is the type of function. However, if you use var (this keyword), the function is considered to be a local variable of this type:
Contract FunctionSelector {function select (bool useB, uint x) returns (uint z) {var f = a; if (useB) f = b; return f (x);} function a (uint x) returns (uint z) {return x * x;} function b (uint x) returns (uint z) {return 2 * x;}}
(in the program fragment above)
If select (false, x) is called, x * x will be calculated. If you call select (true, x), 2 * x will be calculated.
Internal-optimizer
The Solidity optimizer operates at the assembly level, so it can be used by other languages at the same time. It divides the order of instructions into basic blocks on JUMP and JUMPDEST. In these blocks, the instructions are parsed. Every change on the stack, memory, or storage is recorded as an expression. The expression includes an instruction and a pointer to a series of parameters to other expressions. Now the main meaning is to find the equivalent expression (in each input) and make the expression class. The optimizer first looks for it in the list of known expressions, and if it can't find it, simplify it according to constant + constant = sum_of_constants or X * 1 = X. Because this is recursive, if the second factor is a more complex expression, we can also apply the latter rule to calculate it. The modification of storage and memory location is that you don't know the difference between storage and memory location. If we write to the position x first and then to the position y, xmemery y is the input variable. The second can overwrite the first, so we don't know that x is stored after y. On the other hand, if a simplified expression xmury can calculate a non-zero constant, we know what x stores.
At the end of this process, we know that the expression must end in the stack with a series of changes to memory and storage. This information is stored on the block and the block is linked. In addition, information about the stack, storage, and memory configuration is forwarded to the next block. If we know all the JUMP and JUMPI instructions, we can build a complete program control flow diagram. If we do not know the target block (in principle, the jump target is obtained from the input), we must clear the information on all the storage blocks of the input state (because its target block is unknown). If the result of the conditional evaluation is a constant, it is converted to an unconditional jump.
As a final step, the code in each block can be completely regenerated. Start with the end expression of block in the stack and create a dependency graph. Every operation that is not on this diagram will be discarded. You can now generate code for storage and memory changes (discard unnecessary changes) in the order of the original code. Finally, all the values are generated on the stack in the correct position.
These steps apply to each basic block, and if it is small, replace it with newly generated code. If a basic block is split on JUMPI, the result of the conditional expression evaluates to a constant during analysis, and JUMP is replaced with a constant value. The code is as follows
Var x = 7 return data [7] = 9 return if (return [x]! = x + 2) else data 1
Simplify to the following form that can be compiled
Data [7] = 9% return 1
Even if the jump instruction is included at the beginning
Use the command line compiler
A Solidity library is built to target solc, the Solidity command-line compiler. Use solc-help to provide you with an explanation of all the options. The compiler can produce different outputs, from simple binaries to the assembly's abstract syntax tree (parse tree) to estimates of gas usage. If you only want to compile a file, you run solc-bin sourceFile.sol and the binary will be printed. Before you deploy your contract, use solc-optimize-bin sourceFile.sol to activate the optimizer. If you want to get some further solc output variables, you can use solc-o outputDirectory-bin-ast-asm sourceFile.sol, which notifies the compiler of the output to a separate file.
The command line compiler automatically reads the input file from the file system, but you can also provide the redirection path prefix=path as follows:
Solc github.com/ethereum/dapp-bin/=/usr/local/lib/dapp-bin/ = / usr/local/lib/fallback file.sol
This command tells the compiler to look in the / usr/local/lib/dapp-bin directory for files that start with github.com/ethereum/dapp-bin/, and if it cannot be found, look in the usr/local/lib/fallback directory (unprecedented suffixes always match).
Solc does not read files from outside the remapping target or from the external file system of explicitly defined source files, so it is written as import "/ etc/passwd"; the program can only work by adding = / as remapping.
If more than one match is found in the remapping, choose the match with the longest prefix in common.
If your contract uses libraries, you will notice that the bytecode contains substrings such as form LibraryName. You can use solc as a linker in these places to insert the library address:
Either add-libraries "Math:0x12345678901234567890 Heap:0xabcdef0123456" provides the address of each library, or stores a string in a file (one library per line)
Then run solc, followed by-libraries fileName.
If the solc is followed by the-link option, all input files will be interpreted as unlinked binaries (hexadecimal encoding), in the form of LibraryName, and the library is linked at this time (reading input from stdin and output from stdout). In this case, all options except-libraries will be ignored (including-o)
Tips and tips
To use delete in an array is to delete all elements in the array.
Using shorter types and structural elements, short types are grouped together for sorting. Sstore operations may be merged into a single sstore, which can reduce the cost of gas (sstore consumes 5000 or 20000 gas, so that's why you have to optimize). Use Tian gas's price estimation function (optimizer enable) to check!
Make your state variables public, and the compiler will create a getters for free.
If you have finished checking the input or status, try using function modifiers.
If your contract has a feature send, but you want to use the built-in send feature, please use address (contractVariable) .send (amount).
If you don't want your contract to receive ether through send, you can add a throw fallback function function () {throw;}.
Initialize the storage structure with a single assignment statement: X = MyStruct ({a: 1, b: 2})
Trap
Unfortunately, there are some subtle things about the compiler that haven't been told you yet.
In for (var I = 0; I < arrayName.length; iTunes +) {...}, the type of I is uint8, because this is the type with the lowest store value of 0. If there are more than 255 array elements, the loop will not be terminated.
List global variables
Block.coinbase (address): the address of the mine of the current block
Block.difficulty (uint): difficulty of the current block
Block.gaslimit (uint): the gaslimit of the current block
Block.number (uint): number of current blocks
Block.blockhash (function (uint) returns (bytes32)): the hash value of a given block, and only the hash value of the most recently worked 256blocks
Block.timestamp (uint): timestamp of the current block
Msg.data (bytes): complete calldata
Msg.gas (uint): remaining gas
Msg.sender (address): the sender of the message (currently called)
Msg.value (uint): the number of wei sent with the message
Now (uint): timestamp of the current block (alias for block.timestamp)
Tx.gasprice (uint): the gas price of the transaction
Tx.origin (address): the sender of the deal (full call chain)
Sha3 (...) Returns (bytes32): calculates the Ethereum-SHA3 hash value of a (compact arrangement) parameter
Sha256 (...) Returns (bytes32) calculates the SHA256 hash value of (a compact arrangement) parameter
Ripemd160 (...) Returns (bytes20): RIPEMD that calculates 256parameters (compact arrangement)
Ecrecover (bytes32, uint8, bytes32, bytes32) returns (address): public key recovery of Elliptic Curve signature
Addmod (uint x, uint y, uint k) returns (uint): calculate (x + y) K, add to any precision, and do not take 2 * * 256 as the remainder.
Mulmod (uint x, uint y, uint k) returns (uint): compute (XY) K, multiply to any precision, and do not take the remainder with 2 * 256s.
This (current contract's type): current contract, explicit translation on address
Super: contracts at the top of the hierarchical relationship
Selfdestruct (address): destroys the current contract and sends its funds to the specified address address
.balance: account balance in address address (in wei)
.send (uint256) returns (bool): sends a certain amount of wei to the address address, and returns false if it fails.
Function visibility definer function myFunction () returns (bool) {return true;}
Public: visible both externally and internally (create visitor functions that store / state variables)
Private: visible only in current contract
External: visible only externally (for functions only)-only in message calls (via this.fun)
Internal: visible only internally
Modifiers
Constant for state variables: Disallows assignment (except initialisation), does not occupy storage slot.
Constant for functions: Disallows modification of state-this is not enforced yet.
Anonymous for events: Does not store event signature as topic.
Indexed for event parameters: Stores the parameter as topic.
Modifier
Constant for state variables: does not allow assignment (except initialization) and does not occupy storage blocks.
Constant for functions: state change is not allowed-this is not currently mandatory.
Anonymous for events: topic cannot be stored as an event fingerprint.
Indexed for event parameters: stores topic as a parameter.
This is the end of the content of "what are the solidity global variables?" thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.