In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to store JSON files on IPFS". In the operation of actual cases, many people will encounter such a dilemma, so 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!
How do you store JSON files on IPFS and use Oraclize to access data in smart contracts?
Etay Fong is a mature blockchain that enables developers to create smart contracts where programs executed on the blockchain can be triggered by transactions. Blockchains are often referred to as databases, but using blockchains as data storage is very expensive.
Storing 250GB on ethernet will cost you $106000000 at the current price ($530s, 4gwei). Generally speaking, we can tolerate high costs because we:
Not so much data will be saved on the etheric block chain.
The censorship, transparency and robustness of the blockchain are worthwhile.
If you are a novice in Etay Fong, please check this introduction.
Decentralized storage
IPFS (interstellar file system) has some guarantees for blockchain storage, that is, decentralized and tamper-proof, but does not cost more than traditional disk space. Running an EC2 t2.micro instance with EBS 250GB storage will cost you about $15 a month. A unique feature of IPFS is the way it handles files. Instead of using location-based addressing (such as domain names, IP addresses, file paths, etc.), it uses content-based addressing. After you add a file (or directory) to the IPFS repository, you can reference it through its encrypted hash.
$ipfs add article.jsonadded Qmd4PvCKbFbbB8krxajCSeHdLXQamdt7yFxFxzTbedwiYM article.json$ ipfs cat Qmd4PvCKbFbbB8krxajCSeHdLXQamdt7yFxFxzTbedwiYM {"title": "This is an awesome title", "content": "paragraph2\ r\ n\ r\ nparagraph3"} $curl https://ipfs.io/ipfs/Qmd4PvCKbFbbB8krxajCSeHdLXQamdt7yFxFxzTbedwiYM{ "title": "This is an awesome title", "content": "paragraph2\ r\ n\ r\ nparagraph3"}
You can then access the file using the IPFS client or any public gateway. You can also create a non-public gateway, make it writeable (read-only) by default, and implement an authorization scheme to programmatically access the IPFS network.
It is important to understand that IPFS is not a service and that other nodes will store your content. If your content is unpopular, if they don't have a fixed hash (they don't want to rent disk space), the garbage collector will remove it from other nodes. As long as at least one peer on the network does care about your files and is interested in storing them, other nodes on the network can easily access the file. Even if your file disappears from the network, you can add it again later, and unless its content changes, its address (hash) will be the same.
IPFS and Ethernet Square Intelligence contract
Although the Ethernet Square protocol does not provide any local way to connect to IPFS, we can go back to an offline solution like Oraclize to solve this problem. Oraclize allows you to use a variety of data to provide smart contracts. One of the available data sources is URL. We can use the public gateway to read from the JSON file on IPFS. Relying on a single gateway can be thin. Another data source we will use is IPFS. By using the JSON parser, which is part of the query, to read the Oraclize smart contract, we can extract specific fields in the JSON document.
Oraclize_query ("IPFS", "json (Qmd4PvCKbFbbB8krxajCSeHdLXQamdt7yFxFxzTbedwiYM) .title")
If Oraclize can get the file within 20 seconds, you can expect an asynchronous request. If you use a well-connected node to upload files, you don't need to worry about timeouts. Our EC2 (Frankfurt EU) instance is connected to approximately 750 peers. Getting files through public gateways or running daemons locally is almost instantaneous. The response is asynchronous and the oraclize_query call returns the query id (bytes32). You can use it as an identifier for data from Oraclize.
Function _ callback (bytes32 _ queryId, string _ data) public {require (msg.sender = = oraclize_cbAddress ()); process_data (_ data);}
For security reasons, we want to make sure that only Oraclize is allowed to call the _ _ callback function.
You can find the complete code base for blog examples on GitHub: tooploox/ipfs-eth-database
Performance and implementation
At first, I was worried about performance. Can it get the JSON file hosted on the IPFS as quickly as a centralized service sends a response? The result surprised me.
$wrk-d10s https://ipfs.io/ipfs/Qmd4PvCKbFbbB8krxajCSeHdLXQamdt7yFxFxzTbedwiYMRunning 10s test @ https://ipfs.io/ipfs/Qmd4PvCKbFbbB8krxajCSeHdLXQamdt7yFxFxzTbedwiYM 2 threads and 10 connections Thread Stats Avg Stdev Max + /-Stdev Latency 59.18ms 24.36ms 307.93ms 94.73% Req/Sec 86.34 15.48 101.00 85.57% 1695 requests in 10.05s, 1.38MB readRequests/sec: 168.72Transfer/sec: 140.70KB
When we review the blog, the author must enter only the IPFS hash value when the smart contract calls addPost. We use IPFS and Oraclize to read the title from the file to store it using the ethersquare event. We don't need to keep titles for other smart contracts, so using events is sufficient for our use cases. This may not be the most groundbreaking example, but it is a good example of how to optimize low transaction costs.
Pragma solidity 0.4.24; import ". / lib/usingOraclize.sol"; import ". / lib/strings.sol"; contract Blog is usingOraclize, Ownable {using strings for *; mapping (address = > string []) public hashesByAuthor; mapping (bytes32 = > string) public hashByQueryId; mapping (bytes32 = > address) public authorByHash; event PostAdded (address indexed author, string hash, uint timestamp, string title); event PostSubmitted (address indexed author, string hash, bytes32 queryId); uint private gasLimit Constructor (uint _ gasPrice, uint _ gasLimit) public {setCustomOraclizeGasPrice (_ gasPrice); setCustomOraclizeGasLimit (_ gasLimit);} function getPrice (string _ source) public view returns (uint) {return oraclize_getPrice (_ source);} function setCustomOraclizeGasPrice (uint _ gasPrice) public onlyOwner {oraclize_setCustomGasPrice (_ gasPrice);} function setCustomOraclizeGasLimit (uint _ gasLimit) public onlyOwner {gasLimit = _ gasLimit;} function withdraw () public onlyOwner {owner.transfer (address (this) .balance) } function _ callback (bytes32 _ queryId, string _ title) public {require (msg.sender = = oraclize_cbAddress ()); require (bytes (hashByQueryId [_ queryId]). Length! = 0); string memory hash = hashByQueryId [_ queryId]; address author = authorByHash [keccak256 (bytes (hash))]; hashesByAuthor [author] .push (hash); emit PostAdded (author, hash, now, _ title) } function addPost (string _ hash) public payable returns (bool) {require (authorByHash [keccak256 (bytes (_ hash))] = = address (0), "This post already exists"); require (msg.value > = oraclize_getPrice ("IPFS"), "The fee is too low"); bytes32 queryId = oraclize_query ("IPFS", "json (" .toSlice (). Concat (_ hash.toSlice ()). ToSlice (). Concat (") title" .toSlice (), gasLimit)) AuthorByHash [keccak256 (bytes (_ hash))] = msg.sender; hashByQueryId [queryId] = _ hash; emit PostSubmitted (msg.sender, _ hash, queryId); return true;} function getPriceOfAddingPost () public view returns (uint) {return oraclize_getPrice ("IPFS");}}
The front end uses Web3 to read events and build a list of all blog posts for a given author.
The contents of the discounted items are also stored on IPFS. It allows you to retain a fixed fee for adding new blog posts. We use a series of public IPFS, starting with ourselves. This makes sense, especially if you upload files from the same node. If you decide to run the gateway in write mode, you can also pin the file programmatically (it is read-only by default). We also allow users to specify their own gateways. If the user has IPFS Companion installed, he can run it using his own node.
BlogEvents.getPastEvents ("PostAdded", {fromBlock: 0, filter: {author}) .then (events = > {this.setState ({addedPosts: events.map (e = > e.returnValues)});}); / /. GetPost (gatewayIndex = 0) {this.fetchPostFromIpfs (gateways [gatewayIndex]) .catch () = > this.retry (gatewayIndex)} "how to store JSON files on IPFS" ends here. 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.