1

I am testing. Looks to me like state variables are copied during inheritance.

Also when I use new, I am unsure what happens to state variables.

I have d13 state as car - bool. It is updated by d13. When I update using d12 I do not have a new state definition in d12 but it looks like a new state variable is defined in d12 (see storage display).

When I use d14, it looks like another new state variable is defined but I cannot even find that one.

Does anyone know how state variables are inherited and held in storage? What is the point of a design which copies the state variable definition into the new contract?

Code -

contract d13 {
    bool public car;
    function setCar() public returns (bool) {
        car=true;
        return car;
    }

    function removeCar() public returns (bool) {
        car=false;
        return car;
    }
    function showMemory() pure returns (uint) {
        uint ms;
        assembly {
           ms := msize()
        }
        return ms;
    }
}


contract d12 is d13 {
    function isCar() public view returns (bool) {
        return car; 
    }

    function newCar() public {
        car=true;
    }

    function oldCar() public {
        car=false;
    }
    function showMemory() pure returns (uint) {
        uint ms;
        assembly {
           ms := msize()
        }
        return ms;
    }
}

contract d14 {
    function isCar() view returns (bool) {
        return (new d13()).car();
    }

    function setCar() returns (bool) {
        d13 d13instance = new d13();
        d13instance.setCar();
        return (d13instance.car());
    }
    function showMemory() pure returns (uint) {
        uint ms;
        assembly {
           ms := msize()
        }
        return ms;
    }

}

Geth -

> eth.getStorageAt(d13con.address,0)
"0x0000000000000000000000000000000000000000000000000000000000000001"
> d13con.car()
true
> d13con.removeCar()
"0x01973e61dbbf3b44994b016122afc1bd6d94ab2476042aed8902eeca01b6ed80"
> d13con.car()
false
> d12con.isCar()
true
> d12con.oldCar()
"0x09bebed58d84621ac18ae6820634f7678be7b8514dd029b5685ec514f552342d"
> eth.pendingTransactions.length
1
> eth.pendingTransactions.length
1
> eth.pendingTransactions.length
0
> d12con.isCar()
false
> eth.getStorageAt(d13con.address,0)
"0x0000000000000000000000000000000000000000000000000000000000000000"
> d13con.setCar()
"0x9b425c7640c06dc8faf66976ba07ef53c7d8a2a8c362ae75756192c78076a65c"
> eth.pendingTransactions.length
0
> d13con.car()
true
> d12con.isCar()
false
> eth.getStorageAt(d12con.address,0)
"0x0000000000000000000000000000000000000000000000000000000000000000"
> d12con.newCar()
"0x300d0c641711436bc60add13b7e101e8acc22305704de4a16bd466db61dce37a"
> d12con.isCar()
false
> eth.pendingTransactions.length
0
> eth.getStorageAt(d12con.address,0)
"0x0000000000000000000000000000000000000000000000000000000000000001"
> d12con.isCar()
true
> eth.getStorageAt(d13con.address,0)
"0x0000000000000000000000000000000000000000000000000000000000000001"
> d14con.isCar()
false
> eth.getStorageAt(d14con.address,0)
"0x0000000000000000000000000000000000000000000000000000000000000000"
> d14con.setCar()
"0x71a896c7822a0413d64df03e96fdce1e77f61425e3a86124948dc99cda2718f6"
> eth.pendingTransactions.length
0
> d14con.isCar()
false
> eth.getStorageAt(d14con.address,0)
"0x0000000000000000000000000000000000000000000000000000000000000000"

I set a contract instance as d12con etc for d12.

  • Does it just copy the entire inherited contract into the inheriting one? What is the point of this? – Trevor Lee Oakley Mar 13 '18 at 8:13
1

I tested this and it seems that the inheriting contract basically takes a complete copy of the inherited contract.

Hence d12 actually updates the car state variable in d12 but it's not explicitly defined in d12.

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.