Solidity
在乙太坊區塊鏈中儲存每個 IPFS 雜湊需要多少錢
我查看了一些有關計算 Gas 成本的資源,例如我在這個執行緒上找到的這個電子表格。但是電子表格只有計算氣體成本,但沒有儲存散列。我有點困惑,在區塊鏈合約中儲存 IPFS 雜湊的方式也基於這個執行緒有點不同。我認為它必須儲存字元串或字節是直截了當的,但它需要在 Struct 中?任何人都可以幫助我。* 抱歉,我還是智能合約程式的新手
在乙太坊區塊鏈上儲存數據時,每個計算任務都有成本(以氣體單位為單位),您通常希望盡可能降低應用程序的交易成本。
您想 在智能合約中儲存 IPFS 雜湊(或 Multihash)(或 CID),例如QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u
第一:閱讀本文 以了解多雜湊
這是儲存 IPFS 雜湊與相關氣體成本的三種解決方案。
Assuming gasPrice: 20 000 000 000 (wei)
1. 將其儲存為字元串
智能合約:
contract IPFSStorage { string hash; function storeCIDAsString(string _hash) public { hash = _hash; } }
松露測試:
it('should store the IPFS CID as a string', async () => { instance.storeCIDAsString(cid, {'from': accounts[0]}).then(function(txReceipt) { console.log('# should store the IPFS CID as a string'); let gasUsed = txReceipt.receipt.gasUsed; console.log("gasUsed: " + gasUsed + " units"); let gasCost = gasUsed*gasPrice; console.log("gasCost (wei): " + gasCost + " wei"); let gasCostEth = web3.fromWei(gasCost, 'ether') console.log("gasCost (ether): " + gasCostEth + " ether"); }).catch(function (error) { console.log(error); }); });
結果:
# should store the IPFS CID as a string gasUsed: 85986 units gasCost (wei): 1719720000000000 wei gasCost (ether): 0.00171972 ether
2. 將其儲存為結構
智能合約:
contract IPFSStorage { struct Multihash { bytes32 hash; bytes2 hash_function; uint8 size; } Multihash multihash; function storeCIDAsStruct(bytes32 _hash, bytes2 _hash_function, uint8 _size) public { Multihash memory multihashMemory; multihash.hash = _hash; multihash.hash_function = _hash_function; multihash.size = _size; multihash = multihashMemory; } }
松露測試:
it('should store the IPFS CID as a struct', async () => { let mh = multihashes.fromB58String(Buffer.from(cid)) let args = { hashFunction: '0x' + mh.slice(0, 2).toString('hex'), digest: '0x' + mh.slice(2).toString('hex'), size: mh.length - 2 } instance.storeCIDAsStruct(args.digest, args.hashFunction, args.size, {'from': accounts[0]}).then(function(txReceipt) { console.log('# should store the IPFS CID as a struct'); let gasUsed = txReceipt.receipt.gasUsed; console.log("gasUsed: " + gasUsed + " units"); let gasCost = gasUsed*gasPrice; console.log("gasCost (wei): " + gasCost + " wei"); let gasCostEth = web3.fromWei(gasCost, 'ether') console.log("gasCost (ether): " + gasCostEth + " ether"); }).catch(function (error) { console.log(error); }); });
結果:
# should store the IPFS CID as a struct gasUsed: 55600 units gasCost (wei): 1112000000000000 wei gasCost (ether): 0.001112 ether
3. 將其儲存在事件日誌中
智能合約:
contract IPFSStorage { event CIDStoredInTheLog(string _hash); function storeCIDInTheLog(string _hash) public { emit CIDStoredInTheLog(_hash); } }
松露測試:
it('should store the IPFS CID in the logs', async () => { instance.storeCIDInTheLog(cid, {'from': accounts[0]}).then(function(txReceipt) { console.log('# should store the IPFS CID in the logs'); let gasUsed = txReceipt.receipt.gasUsed; console.log("gasUsed: " + gasUsed + " units"); let gasCost = gasUsed*gasPrice; console.log("gasCost (wei): " + gasCost + " wei"); let gasCostEth = web3.fromWei(gasCost, 'ether') console.log("gasCost (ether): " + gasCostEth + " ether"); }).catch(function (error) { console.log(error); }); });
結果:
# should store the IPFS CID in the logs gasUsed: 27501 units gasCost (wei): 550020000000000 wei gasCost (ether): 0.00055002 ether
如您所見,每個解決方案都有效,唯一的區別是在區塊鏈中儲存 IPFS 雜湊的交易的氣體成本。
你可以在這裡找到程式碼(Truffle 項目)