最近在寫合約時遇到一些坑,作一下總結;java
介紹主要分一下三個方面:git
進入正題以前,我稍微簡單介紹一下什麼是區塊鏈,區塊鏈幹嗎用的(特色),智能合約是作什麼的,爲何要寫只能合約(一下是我的理解僅供參考)github
A blockchain is a globally shared, transactional database.算法
譯文:區塊鏈是一個全局共享的事物數據庫;(就是個數據庫)數據庫
既然當前區塊鏈這麼火,那麼總要有火的理由吧,它到底用來幹什麼的呢?而體現區塊鏈自己的商業價值,說白了就是能靠它的獨有的特色賺錢;數組
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.網絡
譯文:Solidity意義上的合約是代碼(其功能)和數據(其狀態)的集合,它位於以太坊區塊鏈的特定地址。分佈式
關鍵字:code (its functions) 、 data (its state)、addresside
在寫合約時遇到一下幾個問題; 其實這些問題google上都能查到,但也是須要大量時間,有些還不必定對,小編對下述問題作一個總結與概括;之後也給本身方便;函數
答:Yes, but only in internal function calls.
使用 internal 時,與平時的 function 寫法同樣便可
答:You have to do it manually for now.
說白了就是沒有,要本身實現; 方案1:https://github.com/Arachnid/solidity-stringutils/blob/master/src/strings.sol
方案2:這是小編根據google查得作了一些小改的,僅供參考
/// 內部訪問函數,internal /// 實現將string _a, _b1,_b2,_c拼接成一個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); }
答:能夠,但不能返回動態數組; 什麼意思呢,就是說能夠 int[] uint[] 等等,若是是string[] 則須要給string []定length長度,不然編譯失敗; 那麼業務上就須要 returns(string[])怎麼辦? 小編的解決方案是return string來代替,將每個string拼接起來,拼接符能夠用ASCII碼中不可見字符如:0x01,0x02等等,千萬別用常見字符;(以前作hyperledger fabric的java sdk解析時發現也用到了)
說明編碼這裏順帶說一句,
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.
答:The address type is a 160-bit value that does not allow any arithmetic operations.
答:據我所知,沒有影響,由於對evm來講它只是執行了,solidity經過solc編譯以後產生的bin、abi文件,進行編譯;
solidity的版本若高於solc編譯環境的版本則會沒法編譯
本文篇幅沒啥乾貨,就隨意聊聊淺談一下;