1

from what I understood, any state variables that are not private can be called and modified by any contract that inherets the parent contract.

it usually takes more than one contract to develope a DApp. so you eventualy need to make several contracts and may use state variables of other contracts. Or even if you don't, some one can copy your code and inherets it and modify(delete/update some data from them) your state variables.

making the sate variables private is like erasing the question. because you will lose the access to your state variables in other contracts you need.

there is a thing in solidity that if you make a state variable public. the solidity automatically creates a getter/ view fucntion for that variable callable by all users.

so how do you protect it from getting modified by other contracts/attackers?

I know that modifying them costs money but still , is there a safe way to prtect non-private state variables from getting attacked?

see the code below:

contract Reputation{
   uint[] points;          // internal, or public
}

contract Market is Reputaion{
   functio foo() public{
       points[0]= 1;
       delete points[1];
   }
}

Imagine if the Market contract is made by attacker to modify your data!

0

Several remarks here:

  1. When inheriting from a contract, you are creating a new instance that has -inherited- variables & functions from the parent contract. Therefore, if you modify any variable from this new contract (in address B), it is not affecting the original parent contract (in address A).

  2. Use of public, private, external or internal will depend whether you need to access methods from a contract, outside of a contract, from an inherited contract, etc, but be careful: all variables can ultimately be seen by using exploring tools. So this is more related to accessibility to methods.

  3. You will be able to modify a variable depending on the functions you define within your contract that allow to do so (and not from other inherited contracts). Other than that, there is no way to modify a variable (that's one of the key features from a blockchain).

3
  • so basically you are saying that points[] is a new different array in Market contract that has no relation to points[] in Reputation? It's like making several new object from an existing entity, but not exactly objects. just a new instance of variable.
    – Amin
    Dec 23 '20 at 8:01
  • 1
    Exactly, it's the essence of OOP (Object-Oriented Programming) like in other languages. By the way, to assign the value to that dynamic array, you might be using 'points.push(1)'. Then, you can update that value with 'points[0]=2'. Dec 23 '20 at 9:35
  • yeah, sure push is the way to add elements to dynamic array. my code was just for simplecity
    – Amin
    Dec 23 '20 at 9:37

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.