Hi guys, can it be upgraded if I write the code below?
struct User {
string name;
int age;
}
mapping(address => User) users;
Why
only pointer variable?
Hi guys, can it be upgraded if I write the code below?
struct User {
string name;
int age;
}
mapping(address => User) users;
only pointer variable?
Hey Tamir, welcome to the forum! Can you expand on what are the two versions of your code? Basically, you’d be upgrading from version A to version B - what does the code look like for each of them?
Version 1 => Version 2 => Version 3
Version 1
struct User {
string name;
int age;
}
mapping(address => User) users;
Version 2
struct User {
string name;
int age;
// notice add sex
int sex;
}
mapping(address => User) users;
Version 3
struct User {
string name;
int age;
// notice delete sex
int _deletedSex;
}
mapping(address => User) users;
// notice add groups
mapping(bytes4 => address) groups;
Thanks for sharing the code, @TamirTian! The upgrades you’ve listed are safe: since you are storing the structs in a mapping, they will be stored sparsely in the contract’s storage, so you are safe to add any new fields. As for the deletion, it’s perfect.
Note that unfortunately the zos
CLI cannot assert that the upgrade will be safe in a scenario like this, so you will get a notice stating that you should review it carefully (as you have done!). Also, it’s always a good practice to write some tests to check the upgrade process itself.