0

I am working on blockchain. My project is to store Role based access control policies(user-role assignment and role-permission assignment) in blockchain. Policies are written in 2 dimensional array (user name and his respective role are stored in array format, role and its respective permission are also stored in array format). I compiled and deployed this smart contract on Remix IDE(and also using geth). Through func1(), I gave 2D array(users-roles) as input and function successfully executed. Now, through another func2(), I gave 2D array(roles-permissions) as input and function successfully executed.

On executing func1() in Remix, block number(block 1) is displayed along with the transaction hash. On executing func2() in Remix, block number(block 2) is displayed along with the transaction hash.

My doubt is, does data given through different functions(func1() and func2()) are stored in same blocks of blockchain with transaction hash in different blocks and how this block is identified? If data is stored in the different blocks of blockchain, later when I cal func3() which takes username as input and gives roles that user possess, how does this function knows the block number in which user-role assignment(2D array stored using func1()) is stored??

Reply really helps a lot. Thank you.

  • Hi, I did not understand what you are trying to find out? Your function 1 and function 2 are placing data in contract storage by assigning it to a contract level variable right? The contract will have reference to the location where this data is stored and can fetch again. Any other function within the same contract can refer to the contract variable and in turn easily refer to this data. – Sanjay S B Jun 3 at 5:48
  • "The contract will have reference to the location where this data is stored and can fetch again." What do you mean by location? Is it block of blockchain where data is stored? If yes, how this block is identified? Later when I try to store some more data, does it is stored in the same block? Thank you. – Shirisha Kollapuram Jun 4 at 5:01
1

Ethereum uses trie data structures to manage data.

enter image description here

You will also notice, from the above diagram, that the root node hash of the storage trie (where all of the smart contract data is kept) actually points to the state trie, which in turn points to the blockchain.

There are two vastly different types of data in Ethereum; permanent data and ephemeral data. An example of permanent data would be a transaction. Once a transaction has been fully confirmed, it is recorded in the transaction trie; it is never altered. An example of ephemeral data would be the balance of a particular Ethereum account address. The balance of an account address is stored in the state trie and is altered whenever transactions against that particular account occur. It makes sense that permanent data, like mined transactions, and ephemeral data, like account balances, should be stored separately.

  • Hey, thank you for reply. But I am still confused about, If ephemeral data changes, then state trie changes. If state trie changes, then block is modified since state trie is pointed to block. But, only one block in the blockchain cannot be modified right?! How this problem is solved? – Shirisha Kollapuram Jun 4 at 5:28
  • Yes, You are Right @ShirishaKollapuram But I want to correct your sentence little bit. You are saying that If ephemeral data changes, then state trie changes. If state trie changes, then block is modified since state trie is pointed to block. But modified means new transaction entry included of state change in a current mining block. hope you will understand. – Mahesh Rajput Jun 4 at 5:41
0

The Ethereum blockchain (or "ledger") is the decentralized, massively replicated database in which the current state of all accounts is stored.

The contract which is also an account has it's data (the values of the contract level variables and balance details) stored in this ledger. When the contract is initially deployed, the transaction representing this information will be added to a new block and the block will get added to the blockchain. Subsequent transactions will get added to the latest blocks at their time. Which means the data could be spread across blocks that are far apart.

So the updates caused by function 1 and function 2 in your case could be stored in different blocks in the blockchain.

As Mahesh Rajput has also explained,

The blockchain uses a database called a Patricia tree (or "trie") to store all accounts; this is essentially a specialized kind of Merkle tree that acts as a generic key/value store.

You can read here in detail on how the Ethereum Blockchain is structured.

To answer this

how does this function knows the block number

from the link,

For each account, the tree stores a 4-tuple containing [account_nonce, ether_balance, code_hash, storage_root], where account_nonce is the number of transactions sent from the account (kept to prevent replay attacks), ether_balance is the balance of the account, code_hash is the hash of the code if the account is a contract and "" otherwise, and storage_root is the root of yet another Patricia tree which stores the storage data.

This storage_root will have the references to the various blocks where the relevant state (transactions that caused the state change) of your contract is stored.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.