Skip to content

Commit 479c0e8

Browse files
committed
First pass at storage layout fixes
1 parent 32e1fa2 commit 479c0e8

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

contracts/extensions/VotingReputation.sol

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,21 @@ contract VotingReputation is ColonyExtension, PatriciaTreeProofs, BasicMetaTrans
9191
uint256 submitPeriod; // Length of time for submitting votes
9292
uint256 revealPeriod; // Length of time for revealing votes
9393
uint256 escalationPeriod; // Length of time for escalating after a vote
94+
95+
uint256 motionCount;
96+
mapping (uint256 => Motion) motions;
97+
mapping (uint256 => mapping (address => mapping (uint256 => uint256))) stakes;
98+
mapping (uint256 => mapping (address => bytes32)) voteSecrets;
99+
100+
mapping (bytes32 => uint256) expenditurePastVotes; // expenditure slot signature => voting power
101+
mapping (bytes32 => uint256) expenditureMotionCounts; // expenditure struct signature => count
102+
94103
mapping(address => uint256) metatransactionNonces;
95104
function getMetatransactionNonce(address userAddress) override public view returns (uint256 nonce){
96-
return metatransactionNonces[userAddress];
105+
// This offset is a result of fixing the storage layout, and having to prevent metatransactions being able to be replayed as a result
106+
// of the nonce resetting. The broadcaster has made ~3000 transactions in total at time of commit, so we definitely won't have a single
107+
// account at 1 million nonce by then.
108+
return metatransactionNonces[userAddress] + 1000000;
97109
}
98110

99111
function incrementMetatransactionNonce(address user) override internal {
@@ -117,7 +129,7 @@ contract VotingReputation is ColonyExtension, PatriciaTreeProofs, BasicMetaTrans
117129
/// @notice Return the version number
118130
/// @return The version number
119131
function version() public pure override returns (uint256) {
120-
return 4;
132+
return 5;
121133
}
122134

123135
/// @notice Install the extension
@@ -183,7 +195,21 @@ contract VotingReputation is ColonyExtension, PatriciaTreeProofs, BasicMetaTrans
183195
}
184196

185197
/// @notice Called when upgrading the extension
186-
function finishUpgrade() public override auth {} // solhint-disable-line no-empty-blocks
198+
function finishUpgrade() public override auth {
199+
// For colonies that have been made since this the previous version's deployment,
200+
// or have done the majority of their motions since, let's at least avoid double-emitting events for motions with
201+
// the same id where we can, going forward.
202+
203+
// Load the value from the wrong storage slot in the previous version
204+
uint256 wrongSlotValue;
205+
assembly {
206+
wrongSlotValue := sload(add(motionCount.slot, 1))
207+
}
208+
// Set the correct storage slot to the larger of the two values.
209+
if (wrongSlotValue > motionCount){
210+
motionCount = wrongSlotValue;
211+
}
212+
} // solhint-disable-line no-empty-blocks
187213

188214
/// @notice Called when deprecating (or undeprecating) the extension
189215
function deprecate(bool _deprecated) public override auth {
@@ -215,15 +241,6 @@ contract VotingReputation is ColonyExtension, PatriciaTreeProofs, BasicMetaTrans
215241
bytes action;
216242
}
217243

218-
// Storage
219-
uint256 motionCount;
220-
mapping (uint256 => Motion) motions;
221-
mapping (uint256 => mapping (address => mapping (uint256 => uint256))) stakes;
222-
mapping (uint256 => mapping (address => bytes32)) voteSecrets;
223-
224-
mapping (bytes32 => uint256) expenditurePastVotes; // expenditure slot signature => voting power
225-
mapping (bytes32 => uint256) expenditureMotionCounts; // expenditure struct signature => count
226-
227244
// Public functions (interface)
228245

229246
/// @notice Create a motion
@@ -774,8 +791,8 @@ contract VotingReputation is ColonyExtension, PatriciaTreeProofs, BasicMetaTrans
774791
Motion storage motion = motions[_motionId];
775792
uint256 requiredStake = getRequiredStake(_motionId);
776793

777-
// Check for valid motion Id
778-
if (_motionId == 0 || _motionId > motionCount) {
794+
// Check for valid motion Id / motion
795+
if (_motionId == 0 || _motionId > motionCount || motion.action.length == 0) {
779796

780797
return MotionState.Null;
781798

test-smoke/colony-storage-consistent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ contract("Contract Storage", (accounts) => {
154154
console.log("miningCycleStateHash:", miningCycleAccount.stateRoot.toString("hex"));
155155
console.log("tokenLockingStateHash:", tokenLockingAccount.stateRoot.toString("hex"));
156156

157-
expect(colonyNetworkAccount.stateRoot.toString("hex")).to.equal("abc691df82610adc362fc162293bb1151a7869039b145bbd925d3c760a22778d");
157+
expect(colonyNetworkAccount.stateRoot.toString("hex")).to.equal("59a945f801398e8c6953538db14bde68dfc61fb4b90314744d6f3869fa3f6fa1");
158158
expect(colonyAccount.stateRoot.toString("hex")).to.equal("db3561898fd52285f65cf27eecfdfb556838f7626f5f425d9b293dcf703fae84");
159159
expect(metaColonyAccount.stateRoot.toString("hex")).to.equal("58f1833f0b94c47c028c91ededb70d6697624ecf98bc2cc7930bf55f40d2d931");
160160
expect(miningCycleAccount.stateRoot.toString("hex")).to.equal("1f3909ac9098d953ec1d197e6d7924384e96209770f445466ea2f0c0c39f4834");

0 commit comments

Comments
 (0)