Is it possible to retrieve/query data stored in the blockchain

Is it possible to retrieve/query data stored in the blockchain



I want to store and retrieve data in the blockchain.



I mate a blockchain expert yesterday and I want to quote his premise :



It is not possible to query data stored in the blockchain. One can store data and it will be there in the blocks of the blockchain but one cannot retrieve data stored in the blockchain.



I know using data structures like an array of structures or mappings it is very much possible to retrieve data stored in the blockchain.



Is storing data in an array of structures or mappings is not the right way of storing data in the blockchain?



If one does not use data structures like an array of structures and mapping and just update values of data stored in structure then one cannot retrieve data stored in the blockchain.



To be more precise consider the following example from this tutorial-


pragma solidity ^0.4.18;
// written for Solidity version 0.4.18 and above that doesnt break functionality

contract Voting
// an event that is called whenever a Candidate is added so the frontend could
// appropriately display the candidate with the right element id (it is used
// to vote for the candidate, since it is one of arguments for the function "vote")
event AddedCandidate(uint candidateID);

// describes a Voter, which has an id and the ID of the candidate they voted for
struct Voter
bytes32 uid; // bytes32 type are basically strings
uint candidateIDVote;

// describes a Candidate
struct Candidate
bytes32 name;
bytes32 party;
// "bool doesExist" is to check if this Struct exists
// This is so we can keep track of the candidates
bool doesExist;


// These state variables are used keep track of the number of Candidates/Voters
// and used to as a way to index them
uint numCandidates; // declares a state variable - number Of Candidates
uint numVoters;


// Think of these as a hash table, with the key as a uint and value of
// the struct Candidate/Voter. These mappings will be used in the majority
// of our transactions/calls
// These mappings will hold all the candidates and Voters respectively
mapping (uint => Candidate) candidates;
mapping (uint => Voter) voters;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* These functions perform transactions, editing the mappings *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function addCandidate(bytes32 name, bytes32 party) public
// candidateID is the return variable
uint candidateID = numCandidates++;
// Create new Candidate Struct with name and saves it to storage.
candidates[candidateID] = Candidate(name,party,true);
AddedCandidate(candidateID);


function vote(bytes32 uid, uint candidateID) public
// checks if the struct exists for that candidate
if (candidates[candidateID].doesExist == true)
uint voterID = numVoters++; //voterID is the return variable
voters[voterID] = Voter(uid,candidateID);



/* * * * * * * * * * * * * * * * * * * * * * * * * *
* Getter Functions, marked by the key word "view" *
* * * * * * * * * * * * * * * * * * * * * * * * * */


// finds the total amount of votes for a specific candidate by looping
// through voters
function totalVotes(uint candidateID) view public returns (uint)
uint numOfVotes = 0; // we will return this
for (uint i = 0; i < numVoters; i++)
// if the voter votes for this specific candidate, we increment the number
if (voters[i].candidateIDVote == candidateID)
numOfVotes++;


return numOfVotes;


function getNumOfCandidates() public view returns(uint)
return numCandidates;


function getNumOfVoters() public view returns(uint)
return numVoters;

// returns candidate information, including its ID, name, and party
function getCandidate(uint candidateID) public view returns (uint,bytes32, bytes32)
return (candidateID,candidates[candidateID].name,candidates[candidateID].party);




In the above example data of Voter and Candidate is stored in mapping voters and candidates respectively. It is possible to retrieve voter and candidate data by using vaterID and candidateID respectively.


Voter


Candidate


mapping


voters


candidates


voter


candidate


vaterID


candidateID



Is the above example demonstrates the right way to store data in the blockchain?




1 Answer
1



It is not possible to query data stored in the blockchain. One can store data and it will be there in the blocks of the blockchain but one cannot retrieve data stored in the blockchain.



This is simply incorrect. You can absolutely store and retrieve data from a blockchain.



The sample code you share is a common approach to storing related data, and reading it back out. Future versions of solidity will support returning structs as a unit, eliminating the need for multiple return types as in the getCanditate function too.



Moreover, even data marked as private can be ready by directly accessing the storage using the eth_getStorageAt function, although it is not visible to other smart contracts.


eth_getStorageAt



Thanks for contributing an answer to Ethereum Stack Exchange!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages