Overview
I encountered some pitfalls in writing contracts recently. Let's make a summary;
The introduction mainly involves three aspects:
- Brief description of blockchain
- Difficulties encountered when writing simple smart contracts in combination with business scenarios (Part 1)
- Assembly instructions (Part II)
Body
Before getting started, I will briefly introduce what blockchain is, what blockchain is used for (features), and what smart contracts are used, why do I have to write only contracts? (my personal understanding is for reference only)
What is blockchain
A blockchain is a globally shared, transactional database.
Translation: blockchain is a global shared transaction database)
Blockchain features
Now that blockchain is so popular, there is always a reason for fire. What is it? Reflecting the commercial value of the blockchain itself, simply put it bluntly, it can make money based on its unique characteristics;
- Distributed Structure
- Trust Mechanism
- Openness and transparency
- Time Series cannot be tampered
How blockchain works
- The client will initiate a transaction and broadcast it to other nodes on the network after Digital Signature and wait for confirmation.
- Nodes in the network verify the received data records. After the verification, the data is recorded in a block.
- All accept nodes in the network perform consensus algorithms on the block. After the block passes the consensus algorithm process, it is formally stored in the blockchain. All nodes in the network accept the block. The method to accept is to treat the random hash value of the block as the newest hash value of the deblock, new Block Removal will provide permanent and transparent transaction records and be extended based on the blockchain to achieve capital transfer.
What is smart contract
A contract in the sense of solidity is a collection of code (its functions) and data (its State) that resides at a specific address on the ethereum blockchain.
Translation: in the sense of solidity, a contract is a collection of code (its functions) and data (its status). It is located at the specific address of ethereum blockchain.
Keywords: Code (its functions), data (its State), address
Pitfall
I encountered several problems when writing a contract. In fact, these problems can be found on Google, but it also takes a lot of time. Some of them are not necessarily correct yet. I will summarize and summarize the following questions; it will be convenient for you later;
- Can the contract method return the struct structure? What can I do?
A: Yes, but only in internal function CILS.
Internal is the same as function writing.
- Can the contract method concatenate strings?
A: You have to do it manually for now.
To put it bluntly is no, to achieve their own; solution 1: https://github.com/Arachnid/solidity-stringutils/blob/master/src/strings.sol
Solution 2
/// Internal access function, internal // concatenates string _ A, _ b1, _ b2, and _ c into a string function strconcat (string _ A, bytes1 _ b1, bytes1 _ b2, string _ C) Internal pure returns (string) {bytes memory _ BA = bytes (_ A); bytes memory _ BC = bytes (_ C ); string memory ABCDE = new string (_ BA. length + _ b1.length + _ b2.length + _ BC. length); bytes memory babcde = bytes (ABCDE); uint K = 0; For (uint I = 0; I <_ BA. length; I ++) babcde [k ++] = _ BA [I]; for (I = 0; I <_ b1.length; I ++) babcde [k ++] = _ b1 [I]; for (I = 0; I <_ b2.length; I ++) babcde [k ++] = _ b2 [I]; for (I = 0; I <_ BC. length; I ++) babcde [k ++] = _ BC [I]; return string (babcde );}
- Can the contract method return the array type?
A: Yes, but no dynamic array is returned. What does it mean? int [] uint [] can be used. If it is string [], you must set the length for string, otherwise, compilation fails. What should I do if returns (string []) is required for the business? The solution of the small series is to replace return string, concatenate each string, and splice the character with invisible characters in the ASCII code, such as 0x01, 0x02, and so on. Do not use common characters; (I used hyperledger fabric's Java SDK for parsing)
Description code: Here is a sentence,
All identifiers(contract names,function names and variable names) are restricted to the ASCII character set. It is possible to store UTF-8 encoded data in string variables.
- Can I perform arithmetic operations on the address type?
A: The Address type is a 160-bit value that does not allow any arithmetic operations.
- Does the version of solidity affect compilation of EVM?
A: As far as I know, this is not affected, because it is only executed for the EVM, And the Solidity is compiled through the bin and ABI files generated after Solc compilation;
If the version of solidity is higher than that of the Solc compiling environment, compilation fails.
If there is no dry content in this article, let's talk about it at will;
Blockchain-based smart contract solidity