Full Smart Contract Upgradeability Discussion

What is the error? I need more information in order to help you.

I posted the error first, its above your question asking for the code. I think it could be some config issue… but again no idea.

1 Like

Finally worked!! Not sure what it was… I copyed your code from github and specified the compiler on the truffle.config and it works. I can go on now, thanks!

1 Like

Dear @filip and others !

I am struggling understand, why don’t we just set the owner in the Storage contract?
This would solve issues like having to create the function initialized();

Since Proxy and Functional are inheriting from Storage, would not been better to set the owner only in Storage? We save a lot of work.

module.exports = async function (deployed,network,accounts){
  // Deploy contracts;
  const functional = await Functional.new();
  const proxy = await Proxy.new(functional.address);

  //Create proxy to fool truffle;
  var proxyFunctional = await Functional.at(proxy.address);

  //Set the number through the proxy;
  await proxyFunctional.setNumber(10);

  //Tested
  var x = await proxyFunctional.getNumber();
  console.log("First: " +x.toNumber());

  //Create a new functional contract and update the address the proxy uses;
  const functional2 = await Functional2.new();
  proxy.upgradeAddress(functional2.address);

  await proxyFunctional.setNumber(15);
  x = await proxyFunctional.getNumber();
  console.log("Second: " + x.toNumber());


  await proxyFunctional.setNumber(20,{from: accounts[1]}); //This one fails since it's from accounts[1];
  x = await proxyFunctional.getNumber();
  console.log("Third: " + x.toNumber());

}

Or even, can’t I just use my Ownable.sol and I set there the constructor owner = msg.sender and the modifier?

Got a question: Wouldn’t it make more sense to set the update function in the proxy contract to onlyOwner? Because literally anyone can call that function and make it point to his own smart contract and destroy everything or empy out the balance with his functions :smiley: Or have I missunderstood anything about the proxy contract functionality? @filip

1 Like


sorry to ask for it ,how can i fix with it ?

Hi, @filip

I have a question. In https://github.com/filipmartinsson/Smart-Contract-Security/blob/master/Upgradeable-Advanced/contracts/DogsUpdated.sol#L14 isn’t this exposed to reentrancy attacks? Shouldn’t we have followed the “Checks Effects Interactions” paradigm and apply _initialized = true; before setting the owner? We could possibly also assert() in the end to make sure it was correctly set.

Is my assumption correct (possible vulnerability), or am I complicating things?

EDIT: Also, this way of setting the owner only works when we’re assuming the contract will be initialized right after it has been deployed (truffle script), correct? Because, if otherwise, there is no way of checking if any other person could call initialize() and claim ownership of the contract, right?

Thanks!

OMG! If you get an error that says “expected 1 argument for getNumberOfDogs but got zero” delete the /build directory, recompile, and it will work again. Filip points out this workaround in “Part 10”! Thanks.

I wish this was pointed out earlier as it would’ve saved me a lot of time in Part 7! :crazy_face:

Also if you get a download error when doing truffle init uninstall Truffle and reinstall the exact version listed in the lecture 5.0.14. I had a different version from a previous course that had issues… more :crazy_face:

I have the same question…

@pmk , @illyushin, @dani69654
Yes we should set the owner in the constructor and add a require msg.sender == owner in the upgrade contract .
I ll open a pull request but do not hesitate to open one if you see this kind of issue.
Good catch :+1:

Hi @Daisy
Are you using a Chinese character ?
It seems that visual studio is not supporting it. Try to change your variables and functions name with the latin alphabet

I tried out to create an upgreadable smart contract and want to console log something like Filip did in his video (nrOfDogs). I have a get function that returns a uint and an address of a struct (return person.number, person.address). I want to console log this info, but how do I do this?

Also how do I google correctly to find a solution for a specific problem like this? :smiley: I tried different things out in my code but nothing worked and I couldn’t find anything online. Is there an API for what you can do in the migration files? lolScreenshot_6

Hi @illyushin

Yes you can look at this documentation for migration

Can you share your migration file ? Because it’s supposed to work as in the video, i can that during your deployment that you have 2 values displayed , one is a bignumber and the other one is the address so i think it works for you too. Try to convert the Big number to string if you want it to be more readable

1 Like

I have a question. I did everything like Filip in his video (I mean, Improving and Updating video…), but I could run the code without the error of reverting the transaction…
Here is my code:

const Dogs = artifacts.require("Dogs");
const DogsUpdated = artifacts.require("DogsUpdated");
const Proxy = artifacts.require("Proxy");

module.exports = async function(deployer, network, accounts){
  const dogs = await Dogs.new();
  const proxy = await Proxy.new(dogs.address);

  var proxyDog = await Dogs.at(proxy.address);
  await proxyDog.setNumberOfDogs(10);
  var nrOfDogs =await proxyDog.getNumberOfDogs();
  console.log(nrOfDogs.toNumber());

  const dogsUpdated = await DogsUpdated.new();
  proxy.upgrade(dogsUpdated.address);

  nrOfDogs =await proxyDog.getNumberOfDogs();
  console.log(nrOfDogs.toNumber());

  nrOfDogs= await proxyDog.setNumberOfDogs(30);
    
}

And here:

pragma solidity 0.5.16;

import "./Storage.sol";

contract DogsUpdated is Storage {

  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }

  constructor() public {
    owner = msg.sender;
  }

  function getNumberOfDogs() public view returns(uint256) {
    return _uintStorage["Dogs"];
  }
  function setNumberOfDogs(uint256 toSet) public onlyOwner {
    _uintStorage["Dogs"] = toSet;
  }

}

It throws no error but I cannot print the result to the console, so that means there is an issue but it is not reverted. A little bit weird but OK.

@gabba @filip powershell is showing me nothing when i am using “truffle develop” command …it is getting compiled correctly

Hi @Gandharv_dalal
Can you try

truffle develop --log

Hi @KryptoDr

Did you solve this issue ? Can you send a screenshot of your console output ?
Thank you

@gabba I tried but it’s still showing nothing

@Gandharv_dalal did you do the eth 101 and 201 course ? Did you have any issues during those courses ?
Can you show me your version of node/npm/truffle ?

Now i am getting this @gabba